• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef ACCOUNT_FUZZTESTMANAGER_H
16 #define ACCOUNT_FUZZTESTMANAGER_H
17 #include "fuzz_test_manager.h"
18 #include <unistd.h>
19 #include "app_account_manager.h"
20 #include "fuzz_config_parser.h"
21 #include "getparam.h"
22 #include "hilog_wrapper.h"
23 
24 #undef private
25 #undef protected
26 namespace OHOS {
27 namespace AccountSA {
28 std::shared_ptr<FuzzTestManager> FuzzTestManager::instance_ = nullptr;
29 
30 // AppAccount
RegisterAppAccountManager()31 void FuzzTestManager::RegisterAppAccountManager()
32 {
33     callFunctionMap_["AppAccountManagerAddAccount"] = []() { AppAccountManager::AddAccount(GetStringParam()); };
34     callFunctionMap_["AppAccountManagerDeleteAccount"] = []() { AppAccountManager::DeleteAccount(GetStringParam()); };
35     callFunctionMap_["AppAccountManagerGetAccountExtraInfo"] = []() {
36         std::string extraInfo = GetStringParam();
37         AppAccountManager::GetAccountExtraInfo(GetStringParam(), extraInfo);
38     };
39     callFunctionMap_["AppAccountManagerSetAccountExtraInfo"] = []() {
40         AppAccountManager::SetAccountExtraInfo(GetStringParam(), GetStringParam());
41     };
42     callFunctionMap_["AppAccountManagerEnableAppAccess"] = []() {
43         AppAccountManager::EnableAppAccess(GetStringParam(), GetStringParam());
44     };
45     callFunctionMap_["AppAccountManagerDisableAppAccess"] = []() {
46         AppAccountManager::DisableAppAccess(GetStringParam(), GetStringParam());
47     };
48     callFunctionMap_["AppAccountManagerCheckAppAccountSyncEnable"] = []() {
49         bool syncEnable = GetBoolParam();
50         AppAccountManager::CheckAppAccountSyncEnable(GetStringParam(), syncEnable);
51     };
52     callFunctionMap_["AppAccountManagerSetAppAccountSyncEnable"] = []() {
53         AppAccountManager::SetAppAccountSyncEnable(GetStringParam(), GetBoolParam());
54     };
55     callFunctionMap_["AppAccountManagerGetAssociatedData"] = []() {
56         std::string value = GetStringParam();
57         AppAccountManager::GetAssociatedData(GetStringParam(), GetStringParam(), value);
58     };
59     callFunctionMap_["AppAccountManagerSetAssociatedData"] = []() {
60         AppAccountManager::SetAssociatedData(GetStringParam(), GetStringParam(), GetStringParam());
61     };
62     callFunctionMap_["AppAccountManagerGetAccountCredential"] = []() {
63         std::string credential = GetStringParam();
64         AppAccountManager::GetAccountCredential(GetStringParam(), GetStringParam(), credential);
65     };
66     callFunctionMap_["AppAccountManagerSetAccountCredential"] = []() {
67         AppAccountManager::SetAccountCredential(GetStringParam(), GetStringParam(), GetStringParam());
68     };
69     callFunctionMap_["AppAccountManagerGetOAuthToken"] = []() {
70         std::string token = GetStringParam();
71         AppAccountManager::GetOAuthToken(GetStringParam(), GetStringParam(), GetStringParam(), token);
72     };
73     callFunctionMap_["AppAccountManagerSetOAuthToken"] = []() {
74         AppAccountManager::SetOAuthToken(GetStringParam(), GetStringParam(), GetStringParam());
75     };
76     callFunctionMap_["AppAccountManagerGetAllAccounts"] = []() {
77         std::vector<AppAccountInfo> param = GetVectorParamAppAccountInfo();
78         AppAccountManager::GetAllAccounts(GetStringParam(), param);
79     };
80     callFunctionMap_["AppAccountManagerGetAllAccessibleAccounts"] = []() {
81         std::vector<AppAccountInfo> param = GetVectorParamAppAccountInfo();
82         AppAccountManager::GetAllAccessibleAccounts(param);
83     };
84     callFunctionMap_["AppAccountManagerSubscribeAppAccount"] = []() {
85         AppAccountManager::SubscribeAppAccount(GetAppAccountSubscriber());
86     };
87     callFunctionMap_["AppAccountManagerUnsubscribeAppAccount"] = []() {
88         AppAccountManager::UnsubscribeAppAccount(GetAppAccountSubscriber());
89     };
90 }
91 
92 // OsAccount
RegisterOsAccountManager()93 void FuzzTestManager::RegisterOsAccountManager()
94 {
95     callFunctionMap_["OsAccountManagerCreateOsAccount"] = []() {
96         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
97         OsAccountManager::CreateOsAccount(GetStringParam(), GetParamOsAccountType(), osAccountInfo);
98     };
99 
100     callFunctionMap_["OsAccountManagerCreateOsAccountForDomain"] = []() {
101         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
102         DomainAccountInfo domainAccountInfo = GetParamDomainAccountInfo();
103         OsAccountManager::CreateOsAccountForDomain(GetParamOsAccountType(), domainAccountInfo, osAccountInfo);
104     };
105 
106     callFunctionMap_["OsAccountManagerRemoveOsAccount"] = []() { OsAccountManager::RemoveOsAccount(GetIntParam()); };
107 
108     callFunctionMap_["OsAccountManagerIsOsAccountExists"] = []() {
109         bool isOsAccountExists = GetBoolParam();
110         OsAccountManager::IsOsAccountExists(GetIntParam(), isOsAccountExists);
111     };
112 
113     callFunctionMap_["OsAccountManagerIsOsAccountActived"] = []() {
114         bool isOsAccountActived = GetBoolParam();
115         OsAccountManager::IsOsAccountActived(GetIntParam(), isOsAccountActived);
116     };
117 
118     callFunctionMap_["OsAccountManagerIsOsAccountConstraintEnable"] = []() {
119         bool isConstraintEnable = GetBoolParam();
120         std::string constraint = GetStringParam();
121         OsAccountManager::IsOsAccountConstraintEnable(GetIntParam(), constraint, isConstraintEnable);
122     };
123 
124     callFunctionMap_["OsAccountManagerIsOsAccountVerified"] = []() {
125         bool isVerified = GetBoolParam();
126         OsAccountManager::IsOsAccountVerified(GetIntParam(), isVerified);
127     };
128 
129     callFunctionMap_["OsAccountManagerGetCreatedOsAccountsCount"] = []() {
130         unsigned int osAccountsCount = GetUIntParam();
131         OsAccountManager::GetCreatedOsAccountsCount(osAccountsCount);
132     };
133 
134     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromProcess"] = []() {
135         int id = GetIntParam();
136         OsAccountManager::GetOsAccountLocalIdFromProcess(id);
137     };
138 
139     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromUid"] = []() {
140         int id = GetIntParam();
141         OsAccountManager::GetOsAccountLocalIdFromUid(GetIntParam(), id);
142     };
143 
144     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromDomain"] = []() {
145         int id = GetIntParam();
146         DomainAccountInfo domainAccountInfo = GetParamDomainAccountInfo();
147         OsAccountManager::GetOsAccountLocalIdFromDomain(domainAccountInfo, id);
148     };
149 
150     callFunctionMap_["OsAccountManagerQueryMaxOsAccountNumber"] = []() {
151         int maxOsAccountNumber = GetIntParam();
152         OsAccountManager::QueryMaxOsAccountNumber(maxOsAccountNumber);
153     };
154 
155     callFunctionMap_["OsAccountManagerGetOsAccountAllConstraints"] = []() {
156         std::vector<std::string> constraints = GetStringVectorParam();
157         OsAccountManager::GetOsAccountAllConstraints(GetIntParam(), constraints);
158     };
159 
160     callFunctionMap_["OsAccountManagerQueryAllCreatedOsAccounts"] = []() {
161         std::vector<OsAccountInfo> osAccountInfos;
162         OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
163     };
164 
165     callFunctionMap_["OsAccountManagerQueryActiveOsAccountIds"] = []() {
166         std::vector<int> osAccountIds;
167         OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
168     };
169 
170     callFunctionMap_["OsAccountManagerQueryCurrentOsAccount"] = []() {
171         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
172         OsAccountManager::QueryCurrentOsAccount(osAccountInfo);
173     };
174 
175     callFunctionMap_["OsAccountManagerQueryOsAccountById"] = []() {
176         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
177         OsAccountManager::QueryOsAccountById(GetIntParam(), osAccountInfo);
178     };
179 
180     callFunctionMap_["OsAccountManagerGetOsAccountTypeFromProcess"] = []() {
181         OsAccountType type = GetParamOsAccountType();
182         OsAccountManager::GetOsAccountTypeFromProcess(type);
183     };
184 
185     callFunctionMap_["OsAccountManagerGetOsAccountProfilePhoto"] = []() {
186         std::string photo = GetStringParam();
187         OsAccountManager::GetOsAccountProfilePhoto(GetIntParam(), photo);
188     };
189 
190     callFunctionMap_["OsAccountManagerIsMultiOsAccountEnable"] = []() {
191         bool isMultiOsAccountEnable = GetBoolParam();
192         OsAccountManager::IsMultiOsAccountEnable(isMultiOsAccountEnable);
193     };
194 
195     callFunctionMap_["OsAccountManagerSetOsAccountName"] = []() {
196         std::string localName = GetStringParam();
197         OsAccountManager::SetOsAccountName(GetIntParam(), localName);
198     };
199 
200     callFunctionMap_["OsAccountManagerSetOsAccountConstraints"] = []() {
201         std::vector<std::string> constraints = GetStringVectorParam();
202         OsAccountManager::SetOsAccountConstraints(GetIntParam(), constraints, GetBoolParam());
203     };
204 
205     callFunctionMap_["OsAccountManagerSetOsAccountProfilePhoto"] = []() {
206         std::string photo = GetStringParam();
207         OsAccountManager::SetOsAccountProfilePhoto(GetIntParam(), photo);
208     };
209 
210     callFunctionMap_["OsAccountManagerGetDistributedVirtualDeviceId"] = []() {
211         std::string deviceId = GetStringParam();
212         OsAccountManager::GetDistributedVirtualDeviceId(deviceId);
213     };
214 
215     callFunctionMap_["OsAccountManagerActivateOsAccount"] = []() {
216         int id = GetIntParam();
217         OsAccountManager::ActivateOsAccount(id);
218     };
219 
220     callFunctionMap_["OsAccountManagerStartOsAccount"] = []() {
221         int id = GetIntParam();
222         OsAccountManager::StartOsAccount(id);
223     };
224 
225     callFunctionMap_["OsAccountManagerStopOsAccount"] = []() {
226         int id = GetIntParam();
227         OsAccountManager::StopOsAccount(id);
228     };
229 
230     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdBySerialNumber"] = []() {
231         int id = GetIntParam();
232         int64_t serialNumber = GetS64Param();
233         OsAccountManager::GetOsAccountLocalIdBySerialNumber(serialNumber, id);
234     };
235 
236     callFunctionMap_["OsAccountManagerGetSerialNumberByOsAccountLocalId"] = []() {
237         int id = GetIntParam();
238         int64_t serialNumber = GetS64Param();
239         OsAccountManager::GetSerialNumberByOsAccountLocalId(id, serialNumber);
240     };
241 
242     callFunctionMap_["OsAccountManagerSubscribeOsAccount"] = []() {
243         std::shared_ptr<TestOsAccountSubscriber> subscriber;
244         OsAccountManager::SubscribeOsAccount(subscriber);
245     };
246 
247     callFunctionMap_["OsAccountManagerUnsubscribeOsAccount"] = []() {
248         std::shared_ptr<TestOsAccountSubscriber> subscriber;
249         OsAccountManager::UnsubscribeOsAccount(subscriber);
250     };
251 
252     callFunctionMap_["OsAccountManagerGetOsAccountSwitchMod"] = []() { OsAccountManager::GetOsAccountSwitchMod(); };
253 
254     callFunctionMap_["OsAccountManagerIsCurrentOsAccountVerified"] = []() {
255         bool isVerified = GetBoolParam();
256         OsAccountManager::IsCurrentOsAccountVerified(isVerified);
257     };
258 
259     callFunctionMap_["OsAccountManagerIsOsAccountCompleted"] = []() {
260         bool isOsAccountCompleted = GetBoolParam();
261         OsAccountManager::IsOsAccountCompleted(GetIntParam(), isOsAccountCompleted);
262     };
263 
264     callFunctionMap_["OsAccountManagerSetCurrentOsAccountIsVerified"] = []() {
265         bool isVerified = GetBoolParam();
266         OsAccountManager::SetCurrentOsAccountIsVerified(isVerified);
267     };
268 
269     callFunctionMap_["OsAccountManagerSetOsAccountIsVerified"] = []() {
270         OsAccountManager::SetOsAccountIsVerified(GetIntParam(), GetBoolParam());
271     };
272 
273     callFunctionMap_["OsAccountManagerGetCreatedOsAccountNumFromDatabase"] = []() {
274         std::string storeID = GetStringParam();
275         int createdOsAccountNum = GetIntParam();
276         OsAccountManager::GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
277     };
278 
279     callFunctionMap_["OsAccountManagerGetSerialNumberFromDatabase"] = []() {
280         std::string storeID = GetStringParam();
281         int64_t serialNumber = GetS64Param();
282         OsAccountManager::GetSerialNumberFromDatabase(storeID, serialNumber);
283     };
284 
285     callFunctionMap_["OsAccountManagerGetMaxAllowCreateIdFromDatabase"] = []() {
286         std::string storeID = GetStringParam();
287         int id = GetIntParam();
288         OsAccountManager::GetMaxAllowCreateIdFromDatabase(storeID, id);
289     };
290 
291     callFunctionMap_["OsAccountManagerGetOsAccountFromDatabase"] = []() {
292         std::string storeID = GetStringParam();
293         int id = GetIntParam();
294         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
295         OsAccountManager::GetOsAccountFromDatabase(storeID, id, osAccountInfo);
296     };
297 
298     callFunctionMap_["OsAccountManagerGetOsAccountListFromDatabase"] = []() {
299         std::string storeID = GetStringParam();
300         std::vector<OsAccountInfo> osAccountList;
301         OsAccountManager::GetOsAccountListFromDatabase(storeID, osAccountList);
302     };
303 }
304 
FuzzTestManager()305 FuzzTestManager::FuzzTestManager()
306 {
307     RegisterAppAccountManager();
308     RegisterOsAccountManager();
309 }
310 
SetJsonFunction(std::string functionName)311 void FuzzTestManager::SetJsonFunction(std::string functionName)
312 {
313     remainderMap_.emplace(functionName, cycle_);
314 }
315 
SetCycle(uint16_t cycle)316 void FuzzTestManager::SetCycle(uint16_t cycle)
317 {
318     cycle_ = cycle;
319     for_each(remainderMap_.begin(), remainderMap_.end(), [cycle](std::unordered_map<std::string, int>::reference temp) {
320         temp.second = cycle;
321     });
322 }
323 
GetRandomInt(int minNum,int maxNum)324 int GetRandomInt(int minNum, int maxNum)
325 {
326     return GetU16Param() % (maxNum - minNum + 1) + minNum;
327 }
328 
action(int a)329 void action(int a)
330 {
331     std::cout << "Interrupt signal (" << a << ") received.\n";
332 }
333 
StartFuzzTest()334 void FuzzTestManager::StartFuzzTest()
335 {
336     std::cout << __func__ << std::endl;
337     OHOS::FuzzConfigParser jsonParser;
338     OHOS::FuzzTestData tempData;
339 
340     std::cout << "parseFromFile start" << std::endl;
341     jsonParser.ParseFromFile4FuzzTest(FUZZ_TEST_CONFIG_FILE_PATH, tempData);
342     std::cout << "flag :" << tempData.mainLoopFlag << std::endl;
343     for_each(tempData.methodVec.begin(), tempData.methodVec.end(), [this](std::vector<std::string>::reference s) {
344         SetJsonFunction(s);
345     });
346     SetCycle(tempData.mainLoopFlag);
347 
348     std::vector<std::string> index;
349     std::unordered_map<std::string, int>::iterator it = remainderMap_.begin();
350     while (it != remainderMap_.end()) {
351         if (it->second <= 0) {
352             it = remainderMap_.erase(it);
353         } else {
354             index.push_back(it->first);
355             ++it;
356         }
357     }
358 
359     std::cout << remainderMap_.size() << "--------fuzz test start--------" << callFunctionMap_.size() << std::endl;
360     while (remainderMap_.size() > 0) {
361         std::string functionName;
362         int offset = GetRandomInt(0, index.size() - 1);
363         functionName = index[offset];
364         if (callFunctionMap_.find(functionName) != callFunctionMap_.end()) {
365             std::cout << "call function : " << functionName << std::endl;
366             callFunctionMap_[functionName]();
367             std::cout << "function end  :" << functionName << std::endl;
368         } else {
369             std::cout << "can't find function : " << functionName << std::endl;
370         }
371         remainderMap_[functionName]--;
372         if (remainderMap_[functionName] <= 0) {
373             remainderMap_.erase(functionName);
374             index.erase(index.begin() + offset);
375         };
376     }
377     std::cout << remainderMap_.size() << "--------fuzz test end--------" << std::endl;
378 }
379 }  // namespace AccountSA
380 }  // namespace OHOS
381 #endif