• 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         DomainAccountInfo domainAccountInfo = GetParamDomainAccountInfo();
102         OsAccountManager::CreateOsAccountForDomain(GetParamOsAccountType(), domainAccountInfo, nullptr);
103     };
104 
105     callFunctionMap_["OsAccountManagerRemoveOsAccount"] = []() { OsAccountManager::RemoveOsAccount(GetIntParam()); };
106 
107     callFunctionMap_["OsAccountManagerIsOsAccountExists"] = []() {
108         bool isOsAccountExists = GetBoolParam();
109         OsAccountManager::IsOsAccountExists(GetIntParam(), isOsAccountExists);
110     };
111 
112     callFunctionMap_["OsAccountManagerIsOsAccountActived"] = []() {
113         bool isOsAccountActived = GetBoolParam();
114         OsAccountManager::IsOsAccountActived(GetIntParam(), isOsAccountActived);
115     };
116 
117     callFunctionMap_["OsAccountManagerIsOsAccountConstraintEnable"] = []() {
118         bool isConstraintEnable = GetBoolParam();
119         std::string constraint = GetStringParam();
120         OsAccountManager::IsOsAccountConstraintEnable(GetIntParam(), constraint, isConstraintEnable);
121     };
122 
123     callFunctionMap_["OsAccountManagerIsOsAccountVerified"] = []() {
124         bool isVerified = GetBoolParam();
125         OsAccountManager::IsOsAccountVerified(GetIntParam(), isVerified);
126     };
127 
128     callFunctionMap_["OsAccountManagerGetCreatedOsAccountsCount"] = []() {
129         unsigned int osAccountsCount = GetUIntParam();
130         OsAccountManager::GetCreatedOsAccountsCount(osAccountsCount);
131     };
132 
133     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromProcess"] = []() {
134         int id = GetIntParam();
135         OsAccountManager::GetOsAccountLocalIdFromProcess(id);
136     };
137 
138     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromUid"] = []() {
139         int id = GetIntParam();
140         OsAccountManager::GetOsAccountLocalIdFromUid(GetIntParam(), id);
141     };
142 
143     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdFromDomain"] = []() {
144         int id = GetIntParam();
145         DomainAccountInfo domainAccountInfo = GetParamDomainAccountInfo();
146         OsAccountManager::GetOsAccountLocalIdFromDomain(domainAccountInfo, id);
147     };
148 
149     callFunctionMap_["OsAccountManagerQueryMaxOsAccountNumber"] = []() {
150         int maxOsAccountNumber = GetIntParam();
151         OsAccountManager::QueryMaxOsAccountNumber(maxOsAccountNumber);
152     };
153 
154     callFunctionMap_["OsAccountManagerGetOsAccountAllConstraints"] = []() {
155         std::vector<std::string> constraints = GetStringVectorParam();
156         OsAccountManager::GetOsAccountAllConstraints(GetIntParam(), constraints);
157     };
158 
159     callFunctionMap_["OsAccountManagerQueryAllCreatedOsAccounts"] = []() {
160         std::vector<OsAccountInfo> osAccountInfos;
161         OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
162     };
163 
164     callFunctionMap_["OsAccountManagerQueryActiveOsAccountIds"] = []() {
165         std::vector<int> osAccountIds;
166         OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
167     };
168 
169     callFunctionMap_["OsAccountManagerQueryCurrentOsAccount"] = []() {
170         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
171         OsAccountManager::QueryCurrentOsAccount(osAccountInfo);
172     };
173 
174     callFunctionMap_["OsAccountManagerQueryOsAccountById"] = []() {
175         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
176         OsAccountManager::QueryOsAccountById(GetIntParam(), osAccountInfo);
177     };
178 
179     callFunctionMap_["OsAccountManagerGetOsAccountTypeFromProcess"] = []() {
180         OsAccountType type = GetParamOsAccountType();
181         OsAccountManager::GetOsAccountTypeFromProcess(type);
182     };
183 
184     callFunctionMap_["OsAccountManagerGetOsAccountProfilePhoto"] = []() {
185         std::string photo = GetStringParam();
186         OsAccountManager::GetOsAccountProfilePhoto(GetIntParam(), photo);
187     };
188 
189     callFunctionMap_["OsAccountManagerIsMultiOsAccountEnable"] = []() {
190         bool isMultiOsAccountEnable = GetBoolParam();
191         OsAccountManager::IsMultiOsAccountEnable(isMultiOsAccountEnable);
192     };
193 
194     callFunctionMap_["OsAccountManagerSetOsAccountName"] = []() {
195         std::string localName = GetStringParam();
196         OsAccountManager::SetOsAccountName(GetIntParam(), localName);
197     };
198 
199     callFunctionMap_["OsAccountManagerSetOsAccountConstraints"] = []() {
200         std::vector<std::string> constraints = GetStringVectorParam();
201         OsAccountManager::SetOsAccountConstraints(GetIntParam(), constraints, GetBoolParam());
202     };
203 
204     callFunctionMap_["OsAccountManagerSetOsAccountProfilePhoto"] = []() {
205         std::string photo = GetStringParam();
206         OsAccountManager::SetOsAccountProfilePhoto(GetIntParam(), photo);
207     };
208 
209     callFunctionMap_["OsAccountManagerGetDistributedVirtualDeviceId"] = []() {
210         std::string deviceId = GetStringParam();
211         OsAccountManager::GetDistributedVirtualDeviceId(deviceId);
212     };
213 
214     callFunctionMap_["OsAccountManagerActivateOsAccount"] = []() {
215         int id = GetIntParam();
216         OsAccountManager::ActivateOsAccount(id);
217     };
218 
219     callFunctionMap_["OsAccountManagerStartOsAccount"] = []() {
220         int id = GetIntParam();
221         OsAccountManager::StartOsAccount(id);
222     };
223 
224     callFunctionMap_["OsAccountManagerStopOsAccount"] = []() {
225         int id = GetIntParam();
226         OsAccountManager::StopOsAccount(id);
227     };
228 
229     callFunctionMap_["OsAccountManagerGetOsAccountLocalIdBySerialNumber"] = []() {
230         int id = GetIntParam();
231         int64_t serialNumber = GetS64Param();
232         OsAccountManager::GetOsAccountLocalIdBySerialNumber(serialNumber, id);
233     };
234 
235     callFunctionMap_["OsAccountManagerGetSerialNumberByOsAccountLocalId"] = []() {
236         int id = GetIntParam();
237         int64_t serialNumber = GetS64Param();
238         OsAccountManager::GetSerialNumberByOsAccountLocalId(id, serialNumber);
239     };
240 
241     callFunctionMap_["OsAccountManagerSubscribeOsAccount"] = []() {
242         std::shared_ptr<TestOsAccountSubscriber> subscriber;
243         OsAccountManager::SubscribeOsAccount(subscriber);
244     };
245 
246     callFunctionMap_["OsAccountManagerUnsubscribeOsAccount"] = []() {
247         std::shared_ptr<TestOsAccountSubscriber> subscriber;
248         OsAccountManager::UnsubscribeOsAccount(subscriber);
249     };
250 
251     callFunctionMap_["OsAccountManagerGetOsAccountSwitchMod"] = []() { OsAccountManager::GetOsAccountSwitchMod(); };
252 
253     callFunctionMap_["OsAccountManagerIsCurrentOsAccountVerified"] = []() {
254         bool isVerified = GetBoolParam();
255         OsAccountManager::IsCurrentOsAccountVerified(isVerified);
256     };
257 
258     callFunctionMap_["OsAccountManagerIsOsAccountCompleted"] = []() {
259         bool isOsAccountCompleted = GetBoolParam();
260         OsAccountManager::IsOsAccountCompleted(GetIntParam(), isOsAccountCompleted);
261     };
262 
263     callFunctionMap_["OsAccountManagerSetCurrentOsAccountIsVerified"] = []() {
264         bool isVerified = GetBoolParam();
265         OsAccountManager::SetCurrentOsAccountIsVerified(isVerified);
266     };
267 
268     callFunctionMap_["OsAccountManagerSetOsAccountIsVerified"] = []() {
269         OsAccountManager::SetOsAccountIsVerified(GetIntParam(), GetBoolParam());
270     };
271 
272     callFunctionMap_["OsAccountManagerGetCreatedOsAccountNumFromDatabase"] = []() {
273         std::string storeID = GetStringParam();
274         int createdOsAccountNum = GetIntParam();
275         OsAccountManager::GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
276     };
277 
278     callFunctionMap_["OsAccountManagerGetSerialNumberFromDatabase"] = []() {
279         std::string storeID = GetStringParam();
280         int64_t serialNumber = GetS64Param();
281         OsAccountManager::GetSerialNumberFromDatabase(storeID, serialNumber);
282     };
283 
284     callFunctionMap_["OsAccountManagerGetMaxAllowCreateIdFromDatabase"] = []() {
285         std::string storeID = GetStringParam();
286         int id = GetIntParam();
287         OsAccountManager::GetMaxAllowCreateIdFromDatabase(storeID, id);
288     };
289 
290     callFunctionMap_["OsAccountManagerGetOsAccountFromDatabase"] = []() {
291         std::string storeID = GetStringParam();
292         int id = GetIntParam();
293         OsAccountInfo osAccountInfo = GetParamOsAccountInfo();
294         OsAccountManager::GetOsAccountFromDatabase(storeID, id, osAccountInfo);
295     };
296 
297     callFunctionMap_["OsAccountManagerGetOsAccountListFromDatabase"] = []() {
298         std::string storeID = GetStringParam();
299         std::vector<OsAccountInfo> osAccountList;
300         OsAccountManager::GetOsAccountListFromDatabase(storeID, osAccountList);
301     };
302 }
303 
FuzzTestManager()304 FuzzTestManager::FuzzTestManager()
305 {
306     RegisterAppAccountManager();
307     RegisterOsAccountManager();
308 }
309 
SetJsonFunction(std::string functionName)310 void FuzzTestManager::SetJsonFunction(std::string functionName)
311 {
312     remainderMap_.emplace(functionName, cycle_);
313 }
314 
SetCycle(uint16_t cycle)315 void FuzzTestManager::SetCycle(uint16_t cycle)
316 {
317     cycle_ = cycle;
318     for_each(remainderMap_.begin(), remainderMap_.end(), [cycle](std::unordered_map<std::string, int>::reference temp) {
319         temp.second = cycle;
320     });
321 }
322 
GetRandomInt(int minNum,int maxNum)323 int GetRandomInt(int minNum, int maxNum)
324 {
325     return GetU16Param() % (maxNum - minNum + 1) + minNum;
326 }
327 
action(int a)328 void action(int a)
329 {
330     std::cout << "Interrupt signal (" << a << ") received.\n";
331 }
332 
StartFuzzTest()333 void FuzzTestManager::StartFuzzTest()
334 {
335     std::cout << __func__ << std::endl;
336     OHOS::FuzzConfigParser jsonParser;
337     OHOS::FuzzTestData tempData;
338 
339     std::cout << "parseFromFile start" << std::endl;
340     jsonParser.ParseFromFile4FuzzTest(FUZZ_TEST_CONFIG_FILE_PATH, tempData);
341     std::cout << "flag :" << tempData.mainLoopFlag << std::endl;
342     for_each(tempData.methodVec.begin(), tempData.methodVec.end(), [this](std::vector<std::string>::reference s) {
343         SetJsonFunction(s);
344     });
345     SetCycle(tempData.mainLoopFlag);
346 
347     std::vector<std::string> index;
348     std::unordered_map<std::string, int>::iterator it = remainderMap_.begin();
349     while (it != remainderMap_.end()) {
350         if (it->second <= 0) {
351             it = remainderMap_.erase(it);
352         } else {
353             index.push_back(it->first);
354             ++it;
355         }
356     }
357 
358     std::cout << remainderMap_.size() << "--------fuzz test start--------" << callFunctionMap_.size() << std::endl;
359     while (remainderMap_.size() > 0) {
360         std::string functionName;
361         int offset = GetRandomInt(0, index.size() - 1);
362         functionName = index[offset];
363         if (callFunctionMap_.find(functionName) != callFunctionMap_.end()) {
364             std::cout << "call function : " << functionName << std::endl;
365             callFunctionMap_[functionName]();
366             std::cout << "function end  :" << functionName << std::endl;
367         } else {
368             std::cout << "can't find function : " << functionName << std::endl;
369         }
370         remainderMap_[functionName]--;
371         if (remainderMap_[functionName] <= 0) {
372             remainderMap_.erase(functionName);
373             index.erase(index.begin() + offset);
374         };
375     }
376     std::cout << remainderMap_.size() << "--------fuzz test end--------" << std::endl;
377 }
378 }  // namespace AccountSA
379 }  // namespace OHOS
380 #endif