1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "procaddserverconfigstub_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20
21 #include "domain_account_callback.h"
22 #include "domain_account_callback_service.h"
23 #include "domain_account_manager_service.h"
24 #include "fuzz_data.h"
25 #include "idomain_account.h"
26
27 using namespace std;
28 using namespace OHOS::AccountSA;
29
30 namespace OHOS {
31 namespace {
32 const int ENUM_MAX = 4;
33 const uint32_t TOKEN_LEN = 10;
34 const std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IDomainAccount";
35
36 class TestGetDomainAccountInfoCallback : public DomainAccountCallbackStub {
37 public:
TestGetDomainAccountInfoCallback()38 TestGetDomainAccountInfoCallback(){};
39 virtual ~TestGetDomainAccountInfoCallback();
40 void OnResult(const int32_t errCode, Parcel &parcel) override;
41 };
42
~TestGetDomainAccountInfoCallback()43 TestGetDomainAccountInfoCallback::~TestGetDomainAccountInfoCallback() {}
44
OnResult(const int32_t errCode,Parcel & parcel)45 void TestGetDomainAccountInfoCallback::OnResult(const int32_t errCode, Parcel &parcel)
46 {
47 return;
48 }
49 }
50
ProcAddServerConfigStubFuzzTest(const uint8_t * data,size_t size)51 bool ProcAddServerConfigStubFuzzTest(const uint8_t* data, size_t size)
52 {
53 if ((data == nullptr) || (size == 0)) {
54 return false;
55 }
56 FuzzData fuzzData(data, size);
57 MessageParcel dataTemp;
58 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
59 return false;
60 }
61 if (!dataTemp.WriteString(fuzzData.GenerateRandomString())) {
62 return false;
63 }
64 MessageParcel reply;
65 MessageOption option;
66 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::ADD_SERVER_CONFIG);
67 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
68 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
69
70 return true;
71 }
72
ProcGetAccountStatusStubFuzzTest(const uint8_t * data,size_t size)73 bool ProcGetAccountStatusStubFuzzTest(const uint8_t *data, size_t size)
74 {
75 if ((data == nullptr) || (size == 0)) {
76 return false;
77 }
78
79 MessageParcel dataTemp;
80 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
81 return false;
82 }
83
84 DomainAccountInfo info;
85 FuzzData fuzzData(data, size);
86 info.domain_ = fuzzData.GenerateRandomString();
87 info.accountName_ = fuzzData.GenerateRandomString();
88 info.accountId_ = fuzzData.GenerateRandomString();
89 info.isAuthenticated = fuzzData.GenerateRandomBool();
90 info.serverConfigId_ = fuzzData.GenerateRandomString();
91 int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
92 info.status_ = static_cast<DomainAccountStatus>(typeNumber);
93
94 if (!dataTemp.WriteParcelable(&info)) {
95 return false;
96 }
97
98 MessageParcel reply;
99 MessageOption option;
100 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_ENQUIRY);
101 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
102 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
103
104 return true;
105 }
106
ProcGetDomainAccessTokenStubFuzzTest(const uint8_t * data,size_t size)107 bool ProcGetDomainAccessTokenStubFuzzTest(const uint8_t *data, size_t size)
108 {
109 if ((data == nullptr) || (size == 0)) {
110 return false;
111 }
112
113 MessageParcel dataTemp;
114 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
115 return false;
116 }
117
118 DomainAccountInfo info;
119 FuzzData fuzzData(data, size);
120 info.domain_ = fuzzData.GenerateRandomString();
121 info.accountName_ = fuzzData.GenerateRandomString();
122 info.accountId_ = fuzzData.GenerateRandomString();
123 info.isAuthenticated = fuzzData.GenerateRandomBool();
124 info.serverConfigId_ = fuzzData.GenerateRandomString();
125 int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
126 info.status_ = static_cast<DomainAccountStatus>(typeNumber);
127
128 if (!dataTemp.WriteParcelable(&info)) {
129 return false;
130 }
131
132 AAFwk::WantParams workParams;
133 if (!dataTemp.WriteParcelable(&workParams)) {
134 return false;
135 }
136
137 auto testCallback = new TestGetDomainAccountInfoCallback();
138
139 if (testCallback == nullptr) {
140 return false;
141 }
142
143 if (!dataTemp.WriteRemoteObject(testCallback->AsObject())) {
144 return false;
145 }
146
147 MessageParcel reply;
148 MessageOption option;
149 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_GET_ACCESS_TOKEN);
150 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
151 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
152
153 return true;
154 }
155
ProcRegisterAccountStatusListenerStubFuzzTest(const uint8_t * data,size_t size)156 bool ProcRegisterAccountStatusListenerStubFuzzTest(const uint8_t *data, size_t size)
157 {
158 if ((data == nullptr) || (size == 0)) {
159 return false;
160 }
161
162 MessageParcel dataTemp;
163 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
164 return false;
165 }
166
167 auto testCallback = new TestGetDomainAccountInfoCallback();
168
169 if (testCallback == nullptr) {
170 return false;
171 }
172
173 if (!dataTemp.WriteRemoteObject(testCallback->AsObject())) {
174 return false;
175 }
176
177 MessageParcel reply;
178 MessageOption option;
179 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_REGISTER);
180 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
181 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
182
183 return true;
184 }
185
ProcUnregisterAccountStatusListenerStubFuzzTest(const uint8_t * data,size_t size)186 bool ProcUnregisterAccountStatusListenerStubFuzzTest(const uint8_t *data, size_t size)
187 {
188 if ((data == nullptr) || (size == 0)) {
189 return false;
190 }
191
192 MessageParcel dataTemp;
193 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
194 return false;
195 }
196
197 auto testCallback = new TestGetDomainAccountInfoCallback();
198
199 if (testCallback == nullptr) {
200 return false;
201 }
202
203 if (!dataTemp.WriteRemoteObject(testCallback->AsObject())) {
204 return false;
205 }
206
207 MessageParcel reply;
208 MessageOption option;
209 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_UNREGISTER);
210 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
211 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
212
213 return true;
214 }
215
ProcUpdateAccountTokenStubFuzzTest(const uint8_t * data,size_t size)216 bool ProcUpdateAccountTokenStubFuzzTest(const uint8_t *data, size_t size)
217 {
218 if ((data == nullptr) || (size == 0)) {
219 return false;
220 }
221
222 MessageParcel dataTemp;
223 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
224 return false;
225 }
226
227 DomainAccountInfo info;
228 FuzzData fuzzData(data, size);
229 info.domain_ = fuzzData.GenerateRandomString();
230 info.accountName_ = fuzzData.GenerateRandomString();
231 info.accountId_ = fuzzData.GenerateRandomString();
232 info.isAuthenticated = fuzzData.GenerateRandomBool();
233 info.serverConfigId_ = fuzzData.GenerateRandomString();
234 int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
235 info.status_ = static_cast<DomainAccountStatus>(typeNumber);
236
237 if (!dataTemp.WriteParcelable(&info)) {
238 return false;
239 }
240
241 std::vector<uint8_t> buffer;
242 for (uint32_t i = 0; i < TOKEN_LEN; i++) {
243 uint8_t bit = fuzzData.GetData<uint8_t>();
244 buffer.emplace_back(bit);
245 }
246 if (!dataTemp.WriteUInt8Vector(buffer)) {
247 return false;
248 }
249
250 MessageParcel reply;
251 MessageOption option;
252 uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_UPDATE_ACCOUNT_TOKEN);
253 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
254 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
255
256 return true;
257 }
258 }
259
260 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)261 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
262 {
263 /* Run your code on data */
264 OHOS::ProcAddServerConfigStubFuzzTest(data, size);
265 OHOS::ProcGetAccountStatusStubFuzzTest(data, size);
266 OHOS::ProcGetDomainAccessTokenStubFuzzTest(data, size);
267 OHOS::ProcRegisterAccountStatusListenerStubFuzzTest(data, size);
268 OHOS::ProcUnregisterAccountStatusListenerStubFuzzTest(data, size);
269 OHOS::ProcUpdateAccountTokenStubFuzzTest(data, size);
270 return 0;
271 }
272