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
GetChar32Param()207 char32_t GetChar32Param()
208 {
209 std::random_device rd;
210 static uniform_int_distribution<char32_t> u;
211 static default_random_engine e(rd());
212 return u(e);
213 }
214
GetStringParam()215 string GetStringParam()
216 {
217 string param = "";
218 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
219 while (len--) {
220 param += GetCharParam();
221 }
222 cout << "String param length is: " << param.length() << endl;
223 return param;
224 }
225
226 template<class T>
GetUnsignVectorParam()227 vector<T> GetUnsignVectorParam()
228 {
229 vector<T> param;
230 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
231 while (len--) {
232 T t = GetUnsignParam<T>();
233 param.push_back(t);
234 }
235 cout << "Uint vector param length is: " << param.size() << endl;
236 return param;
237 }
238
239 template<class T>
GetClassParam()240 T GetClassParam()
241 {
242 T param;
243 return param;
244 }
245
GetBoolVectorParam()246 std::vector<bool> GetBoolVectorParam()
247 {
248 vector<bool> param;
249 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
250 while (len--) {
251 int t = GetBoolParam();
252 param.push_back(t);
253 }
254 cout << "Bool vector param length is: " << param.size() << endl;
255 return param;
256 }
257
GetShortVectorParam()258 std::vector<short> GetShortVectorParam()
259 {
260 vector<short> param;
261 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
262 while (len--) {
263 short t = GetShortParam();
264 param.push_back(t);
265 }
266 cout << "Short vector param length is: " << param.size() << endl;
267 return param;
268 }
269
GetLongVectorParam()270 std::vector<long> GetLongVectorParam()
271 {
272 vector<long> param;
273 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
274 while (len--) {
275 long t = GetLongParam();
276 param.push_back(t);
277 }
278 cout << "Long vector param length is: " << param.size() << endl;
279 return param;
280 }
281
GetIntVectorParam()282 vector<int> GetIntVectorParam()
283 {
284 vector<int> param;
285 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
286 while (len--) {
287 int t = GetIntParam();
288 param.push_back(t);
289 }
290 cout << "Int vector param length is: " << param.size() << endl;
291 return param;
292 }
293
GetFloatVectorParam()294 std::vector<float> GetFloatVectorParam()
295 {
296 vector<float> param;
297 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
298 while (len--) {
299 float t = GetIntParam();
300 param.push_back(t);
301 }
302 cout << "Float vector param length is: " << param.size() << endl;
303 return param;
304 }
305
GetDoubleVectorParam()306 std::vector<double> GetDoubleVectorParam()
307 {
308 vector<double> param;
309 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
310 while (len--) {
311 double t = GetIntParam();
312 param.push_back(t);
313 }
314 cout << "Double vector param length is: " << param.size() << endl;
315 return param;
316 }
317
GetCharVectorParam()318 vector<char> GetCharVectorParam()
319 {
320 vector<char> param;
321 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
322 while (len--) {
323 char t = GetCharParam();
324 param.push_back(t);
325 }
326 cout << "Char vector param length is: " << param.size() << endl;
327 return param;
328 }
329
GetChar32VectorParam()330 vector<char32_t> GetChar32VectorParam()
331 {
332 vector<char32_t> param;
333 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
334 while (len--) {
335 char32_t t = GetChar32Param();
336 param.push_back(t);
337 }
338 cout << "Char32_t vector param length is: " << param.size() << endl;
339 return param;
340 }
341
GetStringVectorParam()342 vector<string> GetStringVectorParam()
343 {
344 vector<string> param;
345 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
346 while (len--) {
347 string t = GetStringParam();
348 param.push_back(t);
349 }
350 cout << "String vector param length is: " << param.size() << endl;
351 return param;
352 }
353
GetS8VectorParam()354 vector<int8_t> GetS8VectorParam()
355 {
356 vector<int8_t> param;
357 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
358 while (len--) {
359 int8_t temp = GetS8Param();
360 param.push_back(temp);
361 }
362 cout << "Int8_t vector param length is: " << param.size() << endl;
363 return param;
364 }
365
GetS16VectorParam()366 vector<int16_t> GetS16VectorParam()
367 {
368 vector<int16_t> param;
369 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
370 while (len--) {
371 int16_t temp = GetS16Param();
372 param.push_back(temp);
373 }
374 cout << "Int16_t vector param length is: " << param.size() << endl;
375 return param;
376 }
377
GetS32VectorParam()378 vector<int32_t> GetS32VectorParam()
379 {
380 vector<int32_t> param;
381 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
382 while (len--) {
383 int32_t temp = GetS32Param();
384 param.push_back(temp);
385 }
386 cout << "Int32_t vector param length is: " << param.size() << endl;
387 return param;
388 }
389
GetS64VectorParam()390 vector<int64_t> GetS64VectorParam()
391 {
392 vector<int64_t> param;
393 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
394 while (len--) {
395 int64_t temp = GetS64Param();
396 param.push_back(temp);
397 }
398 cout << "Int32_t vector param length is: " << param.size() << endl;
399 return param;
400 }
401
GetParamAppAccountInfo()402 AppAccountInfo GetParamAppAccountInfo()
403 {
404 AppAccountInfo accountInfo(GetStringParam(), GetStringParam());
405 return accountInfo;
406 }
407
GetVectorParamAppAccountInfo()408 std::vector<AppAccountInfo> GetVectorParamAppAccountInfo()
409 {
410 std::vector<OHOS::AccountSA::AppAccountInfo> param;
411 size_t len = GenRandom(RANDOM_MIN, RANDOM_MAX);
412 while (len--) {
413 AppAccountInfo accInfo(GetStringParam(), GetStringParam());
414 param.push_back(accInfo);
415 }
416 cout << "Char vector param length is: " << param.size() << endl;
417 return param;
418 }
419
GetAppAccountSubscriber()420 std::shared_ptr<AppAccountSubscriber> GetAppAccountSubscriber()
421 {
422 return std::make_shared<TestAppAccountSubscriber>();
423 }
424
GetParamOsAccountType()425 OHOS::AccountSA::OsAccountType GetParamOsAccountType()
426 {
427 switch (GetIntParam() % INDEX_THREE) {
428 case INDEX_ZERO:
429 return OHOS::AccountSA::OsAccountType::ADMIN;
430 break;
431 case INDEX_ONE:
432 return OHOS::AccountSA::OsAccountType::NORMAL;
433 break;
434 case INDEX_TWO:
435 return OHOS::AccountSA::OsAccountType::GUEST;
436 break;
437 default:
438 return OHOS::AccountSA::OsAccountType::NORMAL;
439 break;
440 }
441 }
442
GetParamOsAccountInfo()443 OsAccountInfo GetParamOsAccountInfo()
444 {
445 if (GetBoolParam()) {
446 OsAccountInfo osAccountInfo;
447 return osAccountInfo;
448 } else {
449 OsAccountInfo osAccountInfo(GetIntParam(), GetStringParam(), GetParamOsAccountType(), GetS64Param());
450 return osAccountInfo;
451 }
452 }
453
GetParamDomainAccountInfo()454 DomainAccountInfo GetParamDomainAccountInfo()
455 {
456 if (GetBoolParam()) {
457 DomainAccountInfo domainAccountInfo;
458 return domainAccountInfo;
459 } else {
460 DomainAccountInfo domainAccountInfo(GetStringParam(), GetStringParam());
461 return domainAccountInfo;
462 }
463 }
464 } // namespace AccountSA
465 } // namespace OHOS