• 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 #include <cfloat>
16 #include <chrono>
17 #include <climits>
18 #include <functional>
19 #include <iostream>
20 #include <random>
21 #include "../include/getparam.h"
22 
23 using namespace std;
24 namespace OHOS {
25 namespace AccountSA {
26 const int ACCOUNT_FUZZ_BOOL = 2;
27 const int ACCOUNT_FUZZ_CHAR_LEFT = -128;
28 const int ACCOUNT_FUZZ_CHAR_RIGHT = 127;
29 const int INDEX_ZERO = 0;
30 const int INDEX_ONE = 1;
31 const int INDEX_TWO = 2;
32 const int INDEX_THREE = 3;
33 const size_t RANDOM_MIN = 0;
34 const size_t RANDOM_MAX = 255;
35 
GetBoolParam()36 bool GetBoolParam()
37 {
38     bool param = false;
39     if (GetIntParam() % ACCOUNT_FUZZ_BOOL == 0) {
40         param = true;
41     }
42     cout << "Bool param is: " << param << endl;
43     return param;
44 }
45 
GenRandom(size_t min,size_t max)46 size_t GenRandom(size_t min, size_t max)
47 {
48     std::random_device rd;
49     static uniform_int_distribution<size_t> u(min, max);
50     static default_random_engine e(rd());
51     size_t param = u(e);
52     return param;
53 }
54 
GetS8Param()55 int8_t GetS8Param()
56 {
57     std::random_device rd;
58     static uniform_int_distribution<int8_t> u(INT8_MIN, INT8_MAX);
59     static default_random_engine e(rd());
60     int8_t param = u(e);
61     cout << "Int8_t param is: " << param << endl;
62     return param;
63 }
64 
GetS16Param()65 int16_t GetS16Param()
66 {
67     std::random_device rd;
68     static uniform_int_distribution<int16_t> u(INT16_MIN, INT16_MAX);
69     static default_random_engine e(rd());
70     int16_t param = u(e);
71     cout << "Int16_t param is: " << param << endl;
72     return param;
73 }
74 
GetS32Param()75 int32_t GetS32Param()
76 {
77     std::random_device rd;
78     static uniform_int_distribution<int32_t> u(INT32_MIN, INT32_MAX);
79     static default_random_engine e(rd());
80     int32_t param = u(e);
81     cout << "Int32_t param is: " << param << endl;
82     return param;
83 }
84 
GetS64Param()85 int64_t GetS64Param()
86 {
87     std::random_device rd;
88     static uniform_int_distribution<int64_t> u(INT64_MIN, INT64_MAX);
89     static default_random_engine e(rd());
90     int64_t param = u(e);
91     cout << "Int64_t param is: " << param << endl;
92     return param;
93 }
94 
95 template<class T>
GetUnsignParam()96 T GetUnsignParam()
97 {
98     std::random_device rd;
99     static uniform_int_distribution<T> u;
100     static default_random_engine e(rd());
101     T param = u(e);
102     return param;
103 }
104 
GetSizeTParam()105 size_t GetSizeTParam()
106 {
107     size_t param = GetUnsignParam<size_t>();
108     return param;
109 }
110 
GetU8Param()111 uint8_t GetU8Param()
112 {
113     uint8_t param = GetUnsignParam<uint8_t>();
114     cout << "Uint8_t param is: " << param << endl;
115     return param;
116 }
117 
GetUIntParam()118 unsigned int GetUIntParam()
119 {
120     unsigned int param = GetUnsignParam<unsigned int>();
121     cout << "Unsigned int param is: " << param << endl;
122     return param;
123 }
124 
GetU16Param()125 uint16_t GetU16Param()
126 {
127     uint16_t param = GetUnsignParam<uint16_t>();
128     cout << "Uint16_t param is: " << param << endl;
129     return param;
130 }
131 
GetU32Param()132 uint32_t GetU32Param()
133 {
134     uint32_t param = GetUnsignParam<uint32_t>();
135     cout << "Uint32_t param is: " << param << endl;
136     return param;
137 }
138 
GetU64Param()139 uint64_t GetU64Param()
140 {
141     uint64_t param = GetUnsignParam<uint64_t>();
142     cout << "Uint64_t param is: " << param << endl;
143     return param;
144 }
145 
GetShortParam()146 short GetShortParam()
147 {
148     std::random_device rd;
149     static uniform_int_distribution<short> u(SHRT_MIN, SHRT_MAX);
150     static default_random_engine e(rd());
151     short param = u(e);
152     cout << "Short param is: " << param << endl;
153     return param;
154 }
155 
GetLongParam()156 long GetLongParam()
157 {
158     std::random_device rd;
159     static uniform_int_distribution<long> u(LONG_MIN, LONG_MAX);
160     static default_random_engine e(rd());
161     long param = u(e);
162     cout << "Long param is: " << param << endl;
163     return param;
164 }
165 
GetIntParam()166 int GetIntParam()
167 {
168     std::random_device rd;
169     static uniform_int_distribution<> u(INT_MIN, INT_MAX);
170     static default_random_engine e(rd());
171     int param = u(e);
172     cout << "Int param is: " << param << endl;
173     return param;
174 }
175 
GetDoubleParam()176 double GetDoubleParam()
177 {
178     double param = 0;
179     std::random_device rd;
180     static uniform_real_distribution<double> u(DBL_MIN, DBL_MAX);
181     static default_random_engine e(rd());
182     param = u(e);
183     cout << "double param is: " << param << endl;
184     return param;
185 }
186 
GetFloatParam()187 float GetFloatParam()
188 {
189     float param = 0;
190     std::random_device rd;
191     static uniform_real_distribution<float> u(FLT_MIN, FLT_MAX);
192     static default_random_engine e(rd());
193     param = u(e);
194     cout << "Float param is: " << param << endl;
195     return param;
196 }
197 
GetCharParam()198 char GetCharParam()
199 {
200     std::random_device rd;
201     static uniform_int_distribution<> u(ACCOUNT_FUZZ_CHAR_LEFT, ACCOUNT_FUZZ_CHAR_RIGHT);
202     static default_random_engine e(rd());
203     char param = u(e);
204     return param;
205 }
206 
GetStringParam()207 string GetStringParam()
208 {
209     string param = "";
210     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
211     while (len--) {
212         param += GetCharParam();
213     }
214     cout << "String param length is: " << param.length() << endl;
215     return param;
216 }
217 
218 template<class T>
GetUnsignVectorParam()219 vector<T> GetUnsignVectorParam()
220 {
221     vector<T> param;
222     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
223     while (len--) {
224         T t = GetUnsignParam<T>();
225         param.push_back(t);
226     }
227     cout << "Uint vector param length is: " << param.size() << endl;
228     return param;
229 }
230 
231 template<class T>
GetClassParam()232 T GetClassParam()
233 {
234     T param;
235     return param;
236 }
237 
GetBoolVectorParam()238 std::vector<bool> GetBoolVectorParam()
239 {
240     vector<bool> param;
241     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
242     while (len--) {
243         int t = GetBoolParam();
244         param.push_back(t);
245     }
246     cout << "Bool vector param length is: " << param.size() << endl;
247     return param;
248 }
249 
GetShortVectorParam()250 std::vector<short> GetShortVectorParam()
251 {
252     vector<short> param;
253     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
254     while (len--) {
255         short t = GetShortParam();
256         param.push_back(t);
257     }
258     cout << "Short vector param length is: " << param.size() << endl;
259     return param;
260 }
261 
GetLongVectorParam()262 std::vector<long> GetLongVectorParam()
263 {
264     vector<long> param;
265     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
266     while (len--) {
267         long t = GetLongParam();
268         param.push_back(t);
269     }
270     cout << "Long vector param length is: " << param.size() << endl;
271     return param;
272 }
273 
GetIntVectorParam()274 vector<int> GetIntVectorParam()
275 {
276     vector<int> param;
277     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
278     while (len--) {
279         int t = GetIntParam();
280         param.push_back(t);
281     }
282     cout << "Int vector param length is: " << param.size() << endl;
283     return param;
284 }
285 
GetFloatVectorParam()286 std::vector<float> GetFloatVectorParam()
287 {
288     vector<float> param;
289     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
290     while (len--) {
291         float t = GetIntParam();
292         param.push_back(t);
293     }
294     cout << "Float vector param length is: " << param.size() << endl;
295     return param;
296 }
297 
GetDoubleVectorParam()298 std::vector<double> GetDoubleVectorParam()
299 {
300     vector<double> param;
301     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
302     while (len--) {
303         double t = GetIntParam();
304         param.push_back(t);
305     }
306     cout << "Double vector param length is: " << param.size() << endl;
307     return param;
308 }
309 
GetCharVectorParam()310 vector<char> GetCharVectorParam()
311 {
312     vector<char> param;
313     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
314     while (len--) {
315         char t = GetCharParam();
316         param.push_back(t);
317     }
318     cout << "Char vector param length is: " << param.size() << endl;
319     return param;
320 }
321 
GetStringVectorParam()322 vector<string> GetStringVectorParam()
323 {
324     vector<string> param;
325     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
326     while (len--) {
327         string t = GetStringParam();
328         param.push_back(t);
329     }
330     cout << "String vector param length is: " << param.size() << endl;
331     return param;
332 }
333 
GetS8VectorParam()334 vector<int8_t> GetS8VectorParam()
335 {
336     vector<int8_t> param;
337     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
338     while (len--) {
339         int8_t temp = GetS8Param();
340         param.push_back(temp);
341     }
342     cout << "Int8_t vector param length is: " << param.size() << endl;
343     return param;
344 }
345 
GetS16VectorParam()346 vector<int16_t> GetS16VectorParam()
347 {
348     vector<int16_t> param;
349     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
350     while (len--) {
351         int16_t temp = GetS16Param();
352         param.push_back(temp);
353     }
354     cout << "Int16_t vector param length is: " << param.size() << endl;
355     return param;
356 }
357 
GetS32VectorParam()358 vector<int32_t> GetS32VectorParam()
359 {
360     vector<int32_t> param;
361     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
362     while (len--) {
363         int32_t temp = GetS32Param();
364         param.push_back(temp);
365     }
366     cout << "Int32_t vector param length is: " << param.size() << endl;
367     return param;
368 }
369 
GetS64VectorParam()370 vector<int64_t> GetS64VectorParam()
371 {
372     vector<int64_t> param;
373     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
374     while (len--) {
375         int64_t temp = GetS64Param();
376         param.push_back(temp);
377     }
378     cout << "Int32_t vector param length is: " << param.size() << endl;
379     return param;
380 }
381 
GetParamAppAccountInfo()382 AppAccountInfo GetParamAppAccountInfo()
383 {
384     AppAccountInfo accountInfo(GetStringParam(), GetStringParam());
385     return accountInfo;
386 }
387 
GetVectorParamAppAccountInfo()388 std::vector<AppAccountInfo> GetVectorParamAppAccountInfo()
389 {
390     std::vector<OHOS::AccountSA::AppAccountInfo> param;
391     size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
392     while (len--) {
393         AppAccountInfo accInfo(GetStringParam(), GetStringParam());
394         param.push_back(accInfo);
395     }
396     cout << "Char vector param length is: " << param.size() << endl;
397     return param;
398 }
399 
GetAppAccountSubscriber()400 std::shared_ptr<AppAccountSubscriber> GetAppAccountSubscriber()
401 {
402     return std::make_shared<TestAppAccountSubscriber>();
403 }
404 
GetParamOsAccountType()405 OHOS::AccountSA::OsAccountType GetParamOsAccountType()
406 {
407     switch (GetIntParam() % INDEX_THREE) {
408         case INDEX_ZERO:
409             return OHOS::AccountSA::OsAccountType::ADMIN;
410             break;
411         case INDEX_ONE:
412             return OHOS::AccountSA::OsAccountType::NORMAL;
413             break;
414         case INDEX_TWO:
415             return OHOS::AccountSA::OsAccountType::GUEST;
416             break;
417         default:
418             return OHOS::AccountSA::OsAccountType::NORMAL;
419             break;
420     }
421 }
422 
GetParamOsAccountInfo()423 OsAccountInfo GetParamOsAccountInfo()
424 {
425     if (GetBoolParam()) {
426         OsAccountInfo osAccountInfo;
427         return osAccountInfo;
428     } else {
429         OsAccountInfo osAccountInfo(GetIntParam(), GetStringParam(), GetParamOsAccountType(), GetS64Param());
430         return osAccountInfo;
431     }
432 }
433 
GetParamDomainAccountInfo()434 DomainAccountInfo GetParamDomainAccountInfo()
435 {
436     if (GetBoolParam()) {
437         DomainAccountInfo domainAccountInfo;
438         return domainAccountInfo;
439     } else {
440         DomainAccountInfo domainAccountInfo(GetStringParam(), GetStringParam());
441         return domainAccountInfo;
442     }
443 }
444 }  // namespace AccountSA
445 }  // namespace OHOS