• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef HKS_ENABLE_LITE_HAP
17 #define HKS_ENABLE_LITE_HAP
18 
19 #undef HKS_USE_RKC_IN_STANDARD
20 #define HKS_USE_RKC_IN_STANDARD
21 
22 #undef HKS_KEY_STORE_LITE_HAP
23 #define HKS_KEY_STORE_LITE_HAP "/data/lite/hap"
24 
25 #undef HKS_CONFIG_RKC_STORE_PATH
26 #define HKS_CONFIG_RKC_STORE_PATH "/data"
27 
28 #include "hksstorage_fuzzer.h"
29 
30 #include <string>
31 
32 #include "hks_api.h"
33 #include "hks_config.h"
34 #include "hks_log.h"
35 #include "hks_mem.h"
36 #include "hks_param.h"
37 #include "hks_storage_file_lock.h"
38 #include "hks_storage_manager.c"
39 #include "hks_storage_utils.c"
40 #include "hks_template.h"
41 #include "hks_type.h"
42 #include "hks_type_inner.h"
43 
44 const std::string TEST_PROCESS_NAME = "test_process";
45 const std::string TEST_USER_ID = "123465";
46 const std::string TEST_KEY_ALIAS = "key_alias";
47 constexpr uint32_t TEST_BLOB_SIZE = 16;
48 constexpr uint32_t PARAM_NUM_ONE = 1;
49 constexpr uint32_t PARAM_NUM_TWO = 2;
50 constexpr uint8_t TEST_BLOB[TEST_BLOB_SIZE] = {0};
51 constexpr size_t MAX_TEST_COUNT = 1024;
52 
53 namespace OHOS {
54 namespace Security {
55 namespace Hks {
56 
BuildParamSet(const struct HksParam * param,uint32_t paramCnt,struct HksParamSet ** paramSetOut)57 static int32_t BuildParamSet(const struct HksParam *param, uint32_t paramCnt, struct HksParamSet **paramSetOut)
58 {
59     int32_t ret;
60     struct HksParamSet *paramSet = nullptr;
61     do {
62         ret = HksInitParamSet(&paramSet);
63         HKS_IF_NOT_SUCC_BREAK(ret)
64 
65         if (param != nullptr && paramCnt > 0) {
66             ret = HksAddParams(paramSet, param, paramCnt);
67             HKS_IF_NOT_SUCC_BREAK(ret)
68         }
69 
70         ret = HksBuildParamSet(&paramSet);
71         HKS_IF_NOT_SUCC_BREAK(ret)
72     } while (0);
73     if (ret != HKS_SUCCESS) {
74         HksFreeParamSet(&paramSet);
75     }
76     *paramSetOut = paramSet;
77     return HKS_SUCCESS;
78 }
79 
80 static const struct HksParam g_genParams[] = {
81     { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
82     { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
83     { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
84     { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
85     { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
86     { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
87 };
88 
PrepareBlob()89 static void PrepareBlob()
90 {
91     HksBlob processName = {
92         .size = TEST_PROCESS_NAME.size() + 1,
93         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
94     };
95     HksBlob userId = {
96         .size = TEST_USER_ID.size() + 1,
97         .data = (uint8_t *)&TEST_USER_ID[0]
98     };
99     HksBlob keyAlias = {
100         .size = TEST_KEY_ALIAS.size() + 1,
101         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
102     };
103     HksBlob keyBlob = {
104         .size = TEST_BLOB_SIZE,
105         .data = (uint8_t *)TEST_BLOB,
106     };
107     HksProcessInfo hksProcessInfo = {
108         .userId = userId,
109         .processName = processName
110     };
111 
112     struct HksParamSet *paramSet = nullptr;
113     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
114     HksManageStoreKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
115         &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
116     HksFreeParamSet(&paramSet);
117 }
118 
HksStorageMultithreadTest001()119 static void HksStorageMultithreadTest001()
120 {
121     HksBlob processName = {
122         .size = TEST_PROCESS_NAME.size() + 1,
123         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
124     };
125     HksBlob userId = {
126         .size = TEST_USER_ID.size() + 1,
127         .data = (uint8_t *)&TEST_USER_ID[0]
128     };
129     HksBlob keyBlob = {
130         .size = TEST_BLOB_SIZE,
131         .data = (uint8_t *)TEST_BLOB,
132     };
133     HksBlob keyAlias = {
134         .size = TEST_KEY_ALIAS.size() + 1,
135         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
136     };
137     HksProcessInfo hksProcessInfo = {
138         .userId = userId,
139         .processName = processName
140     };
141 
142     struct HksParamSet *paramSet = nullptr;
143     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
144     HksManageStoreKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
145         &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
146     HksFreeParamSet(&paramSet);
147 }
148 
HksStorageMultithreadTest002()149 static void HksStorageMultithreadTest002()
150 {
151     PrepareBlob();
152 
153     HksBlob processName = {
154         .size = TEST_PROCESS_NAME.size() + 1,
155         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
156     };
157     HksBlob userId = {
158         .size = TEST_USER_ID.size() + 1,
159         .data = (uint8_t *)&TEST_USER_ID[0]
160     };
161     HksBlob keyAlias = {
162         .size = TEST_KEY_ALIAS.size() + 1,
163         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
164     };
165     uint8_t buff[TEST_BLOB_SIZE] = {0};
166     HksBlob keyBlob = {
167         .size = TEST_BLOB_SIZE,
168         .data = buff,
169     };
170     HksProcessInfo hksProcessInfo = {
171         .userId = userId,
172         .processName = processName
173     };
174 
175     struct HksParamSet *paramSet = nullptr;
176     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
177     HksManageStoreGetKeyBlob(&hksProcessInfo, paramSet,
178         &keyAlias, &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
179     HksFreeParamSet(&paramSet);
180 }
181 
HksStorageMultithreadTest003()182 static void HksStorageMultithreadTest003()
183 {
184     PrepareBlob();
185 
186     HksBlob processName = {
187         .size = TEST_PROCESS_NAME.size() + 1,
188         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
189     };
190     HksBlob userId = {
191         .size = TEST_USER_ID.size() + 1,
192         .data = (uint8_t *)&TEST_USER_ID[0]
193     };
194     HksBlob keyAlias = {
195         .size = TEST_KEY_ALIAS.size() + 1,
196         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
197     };
198     HksProcessInfo hksProcessInfo = {
199         .userId = userId,
200         .processName = processName
201     };
202     struct HksParamSet *paramSet = nullptr;
203     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
204     HksManageStoreDeleteKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
205         HksStorageType::HKS_STORAGE_TYPE_KEY);
206     HksFreeParamSet(&paramSet);
207 }
208 
HksStorageFileLockTest001()209 static void HksStorageFileLockTest001()
210 {
211     std::string path = "/test/test";
212     HksStorageFileLock *lock = HksStorageFileLockCreate(&path[0]);
213     HksStorageFileLockRead(lock);
214     HksStorageFileUnlockRead(lock);
215     HksStorageFileLockWrite(lock);
216     HksStorageFileUnlockWrite(lock);
217     HksStorageFileLockRelease(lock);
218 }
219 
HksStorageFileLockTest002()220 static void HksStorageFileLockTest002()
221 {
222     std::string pathBase = "/test/test";
223     std::vector<HksStorageFileLock *> locks;
224     for (size_t i = 0; i < MAX_TEST_COUNT; i++) {
225         std::string path = pathBase + std::to_string(i);
226         HksStorageFileLock *lock = HksStorageFileLockCreate(&path[0]);
227         if (lock != nullptr) {
228             locks.push_back(lock);
229         }
230     }
231     locks.size();
232     for (auto lock : locks) {
233         HksStorageFileLockRelease(lock);
234     }
235 }
236 
HksStorageFileLockTest003()237 static void HksStorageFileLockTest003()
238 {
239     std::string path = "/test/test";
240     std::vector<HksStorageFileLock *> locks;
241     for (size_t i = 0; i < MAX_TEST_COUNT; i++) {
242         HksStorageFileLock *lock = HksStorageFileLockCreate(&path[0]);
243         if (lock != nullptr) {
244             locks.push_back(lock);
245         }
246     }
247 
248     for (auto lock : locks) {
249         HksStorageFileLockRelease(lock);
250     }
251 }
252 
HksStorageFileLockTest004()253 static void HksStorageFileLockTest004()
254 {
255     std::string path = "/test/test";
256     HksStorageFileLock *lock1 = HksStorageFileLockCreate(&path[0]);
257     HksStorageFileLock *lock2 = HksStorageFileLockCreate(&path[0]);
258 
259     HksStorageFileLockRead(lock1);
260     HksStorageFileUnlockRead(lock1);
261     HksStorageFileLockRead(lock2);
262     HksStorageFileUnlockRead(lock2);
263 
264     HksStorageFileLockWrite(lock1);
265     HksStorageFileUnlockWrite(lock1);
266     HksStorageFileLockWrite(lock2);
267     HksStorageFileUnlockWrite(lock2);
268 
269     HksStorageFileLockRelease(lock1);
270     HksStorageFileLockRelease(lock2);
271 }
272 
HksStorageTest001()273 static void HksStorageTest001()
274 {
275     HKS_LOG_I("enter HksStorageTest001");
276     const char input = '#';
277     char outPut;
278     ResumeInvalidCharacter(input, &outPut);
279 }
280 
HksStorageTest002()281 static void HksStorageTest002()
282 {
283     HKS_LOG_I("enter HksStorageTest002");
284     const char input = '$';
285     char outPut;
286     ResumeInvalidCharacter(input, &outPut);
287 }
288 
HksStorageTest003()289 static void HksStorageTest003()
290 {
291     HKS_LOG_I("enter HksStorageTest003");
292     const char input = '%';
293     char outPut;
294     ResumeInvalidCharacter(input, &outPut);
295 }
296 
HksStorageTest004()297 static void HksStorageTest004()
298 {
299     HKS_LOG_I("enter HksStorageTest004");
300     const char input = '&';
301     char outPut;
302     ResumeInvalidCharacter(input, &outPut);
303 }
304 
HksStorageTest005()305 static void HksStorageTest005()
306 {
307     HKS_LOG_I("enter HksStorageTest005");
308     const char input = '(';
309     char outPut;
310     ResumeInvalidCharacter(input, &outPut);
311 }
312 
HksStorageTest006()313 static void HksStorageTest006()
314 {
315     HKS_LOG_I("enter HksStorageTest006");
316     const char input = ')';
317     char outPut;
318     ResumeInvalidCharacter(input, &outPut);
319 }
320 
HksStorageManagerTest001()321 static void HksStorageManagerTest001()
322 {
323     HKS_LOG_I("enter HksStorageManagerTest001");
324     uint32_t userId = 1;
325     uint32_t uid = 1;
326     struct HksProcessInfo processInfo001 = {
327         .processName = {
328             .data = (uint8_t *)&uid,
329             .size = sizeof(uid)
330         },
331         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
332         .accessTokenId = 1
333     };
334     struct HksBlob alias = {
335         .data = (uint8_t *)"HksStorageManagerTest001", .size = strlen("HksStorageManagerTest001")
336     };
337     struct HksParamSet *paramSet = nullptr;
338     HksInitParamSet(&paramSet);
339     struct HksParam params[] = {
340         {
341             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
342             .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE
343         }, {
344             .tag = HKS_TAG_SPECIFIC_USER_ID,
345             .uint32Param = 1
346         }
347     };
348     HksAddParams(paramSet, params, PARAM_NUM_TWO);
349     HksBuildParamSet(&paramSet);
350 
351     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
352     InitStorageMaterial(&processInfo001, paramSet, &alias, HKS_STORAGE_TYPE_KEY, &material);
353 
354     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
355     HksMemCmp(HKS_KEY_STORE_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
356 
357     HksMemCmp("1", material.uidPath, strlen(material.uidPath));
358     HksMemCmp("1", material.userIdPath, strlen(material.userIdPath));
359 
360     FreeStorageMaterial(&material);
361     HksFreeParamSet(&paramSet);
362 }
363 
HksStorageManagerTest002()364 static void HksStorageManagerTest002()
365 {
366     HKS_LOG_I("enter HksStorageManagerTest002");
367     uint32_t userId = 2;
368     uint32_t uid = 2;
369     struct HksProcessInfo processInfo002 = {
370         .processName = {
371             .data = (uint8_t *)&uid,
372             .size = sizeof(uid)
373         },
374         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
375         .accessTokenId = 2
376     };
377     struct HksBlob alias = {
378         .data = (uint8_t *)"HksStorageManagerTest002", .size = strlen("HksStorageManagerTest002")
379     };
380     struct HksParamSet *paramSet = nullptr;
381     HksInitParamSet(&paramSet);
382     struct HksParam params[] = {
383         {
384             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
385             .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE
386         }
387     };
388     HksAddParams(paramSet, params, PARAM_NUM_ONE);
389     HksBuildParamSet(&paramSet);
390 
391     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
392     InitStorageMaterial(&processInfo002, paramSet, &alias, HKS_STORAGE_TYPE_KEY, &material);
393 
394     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
395     HksMemCmp(HKS_KEY_STORE_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
396 
397     HksMemCmp("2", material.uidPath, strlen(material.uidPath));
398     HksMemCmp("2", material.userIdPath, strlen(material.userIdPath));
399 
400     FreeStorageMaterial(&material);
401     HksFreeParamSet(&paramSet);
402 }
403 
HksStorageManagerTest003()404 static void HksStorageManagerTest003()
405 {
406     HKS_LOG_I("enter HksStorageManagerTest003");
407     uint32_t userId = 3;
408     uint32_t uid = 3;
409     struct HksProcessInfo processInfo003 = {
410         .processName = {
411             .data = (uint8_t *)&uid,
412             .size = sizeof(uid)
413         },
414         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
415         .accessTokenId = 3
416     };
417     struct HksBlob alias = {
418         .data = (uint8_t *)"HksStorageManagerTest003", .size = strlen("HksStorageManagerTest003")
419     };
420     struct HksParamSet *paramSet = nullptr;
421     HksInitParamSet(&paramSet);
422     struct HksParam params[] = {
423         {
424             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
425             .uint32Param = HKS_AUTH_STORAGE_LEVEL_ECE
426         }
427     };
428     HksAddParams(paramSet, params, PARAM_NUM_ONE);
429     HksBuildParamSet(&paramSet);
430 
431     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
432     InitStorageMaterial(&processInfo003, paramSet, &alias, HKS_STORAGE_TYPE_KEY, &material);
433 
434     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
435     HksMemCmp(HKS_KEY_STORE_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
436 
437     HksMemCmp("3", material.uidPath, strlen(material.uidPath));
438     HksMemCmp("3", material.userIdPath, strlen(material.userIdPath));
439 
440     FreeStorageMaterial(&material);
441     HksFreeParamSet(&paramSet);
442 }
443 
HksStorageManagerTest004()444 static void HksStorageManagerTest004()
445 {
446     HKS_LOG_I("enter HksStorageManagerTest004");
447     uint32_t userId = 0;
448     uint32_t uid = 0;
449     struct HksProcessInfo processInfo004 = {
450         .processName = {
451             .data = (uint8_t *)&uid,
452             .size = sizeof(uid)
453         },
454         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
455         .accessTokenId = 4
456     };
457     struct HksBlob alias = {
458         .data = (uint8_t *)"HksStorageManagerTest004", .size = strlen("HksStorageManagerTest004")
459     };
460     struct HksParamSet *paramSet = nullptr;
461     HksInitParamSet(&paramSet);
462     struct HksParam params[] = {
463         {
464             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
465             .uint32Param = HKS_AUTH_STORAGE_LEVEL_OLD_DE_TMP
466         }
467     };
468     HksAddParams(paramSet, params, PARAM_NUM_ONE);
469     HksBuildParamSet(&paramSet);
470 
471     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
472     InitStorageMaterial(&processInfo004, paramSet, &alias, HKS_STORAGE_TYPE_KEY, &material);
473 
474     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
475     HksMemCmp(HKS_KEY_STORE_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
476 
477     HksMemCmp("+0+0+0+0", material.uidPath, strlen(material.uidPath));
478     HksMemCmp("", material.userIdPath, strlen(material.userIdPath));
479 
480     FreeStorageMaterial(&material);
481     HksFreeParamSet(&paramSet);
482 }
483 
HksStorageManagerTest005()484 static void HksStorageManagerTest005()
485 {
486     HKS_LOG_I("enter HksStorageManagerTest005");
487     uint32_t userId = 100;
488     uint32_t uid = 0;
489     struct HksProcessInfo processInfo005 = {
490         .processName = {
491             .data = (uint8_t *)&uid,
492             .size = sizeof(uid)
493         },
494         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
495         .accessTokenId = 5
496     };
497     struct HksBlob alias = {
498         .data = (uint8_t *)"HksStorageManagerTest005", .size = strlen("HksStorageManagerTest005")
499     };
500     struct HksParamSet *paramSet = nullptr;
501     HksInitParamSet(&paramSet);
502     struct HksParam params[] = {
503         {
504             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
505             .uint32Param = HKS_AUTH_STORAGE_LEVEL_OLD_DE_TMP
506         }
507     };
508     HksAddParams(paramSet, params, PARAM_NUM_ONE);
509     HksBuildParamSet(&paramSet);
510 
511     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
512     InitStorageMaterial(&processInfo005, paramSet, &alias, HKS_STORAGE_TYPE_KEY, &material);
513 
514     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
515     HksMemCmp(HKS_KEY_STORE_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
516 
517     HksMemCmp("+0+0+0+0", material.uidPath, strlen(material.uidPath));
518     HksMemCmp("d+0+0+0", material.userIdPath, strlen(material.userIdPath));
519 
520     FreeStorageMaterial(&material);
521     HksFreeParamSet(&paramSet);
522 }
523 
HksStorageManagerTest006()524 static void HksStorageManagerTest006()
525 {
526     HKS_LOG_I("enter HksStorageManagerTest006");
527     uint32_t userId = 6;
528     uint32_t uid = 6;
529     struct HksProcessInfo processInfo006 = {
530         .processName = {
531             .data = (uint8_t *)&uid,
532             .size = sizeof(uid)
533         },
534         .userIdInt = userId, .userId = { .data = (uint8_t *)&userId, .size = sizeof(userId) }, .uidInt = uid,
535         .accessTokenId = 6
536     };
537     struct HksBlob alias = {
538         .data = (uint8_t *)"HksStorageManagerTest006", .size = strlen("HksStorageManagerTest006")
539     };
540     struct HksParamSet *paramSet = nullptr;
541     HksInitParamSet(&paramSet);
542     struct HksParam params[] = {
543         {
544             .tag = HKS_TAG_AUTH_STORAGE_LEVEL,
545             .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE
546         }
547     };
548     HksAddParams(paramSet, params, PARAM_NUM_ONE);
549     HksBuildParamSet(&paramSet);
550 
551     struct HksStoreMaterial material = { ECE_PATH, 0 };
552     InitStorageMaterial(&processInfo006, paramSet, &alias, HKS_STORAGE_TYPE_ROOT_KEY, &material);
553 
554     HksMemCmp(alias.data, material.keyAliasPath, strlen(material.keyAliasPath));
555     HksMemCmp(HKS_KEY_STORE_ROOT_KEY_PATH, material.storageTypePath, strlen(material.storageTypePath));
556 
557     HksMemCmp("6", material.uidPath, strlen(material.uidPath));
558     HksMemCmp("6", material.userIdPath, strlen(material.userIdPath));
559 
560     FreeStorageMaterial(&material);
561     HksFreeParamSet(&paramSet);
562 }
563 
HksStorageUtilTest001()564 static void HksStorageUtilTest001()
565 {
566     HKS_LOG_I("enter HksStorageUtilTest001");
567     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
568     material.keyAliasPath = const_cast<char *>("alias");
569     material.storageTypePath = const_cast<char *>("key");
570     material.uidPath = const_cast<char *>("123");
571     material.userIdPath = const_cast<char *>("999");
572 #ifdef SUPPORT_STORAGE_BACKUP
573     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
574 #else
575     struct HksStoreFileInfo fileInfo = { { 0 } };
576 #endif
577     HksGetFileInfo(&material, &fileInfo);
578     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
579 
580     const char *expectPath = HKS_KEY_STORE_PATH "/999/123/key";
581     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
582 }
583 
HksStorageUtilTest002()584 static void HksStorageUtilTest002()
585 {
586     HKS_LOG_I("enter HksStorageUtilTest002");
587     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
588     material.keyAliasPath = const_cast<char *>("alias");
589     material.storageTypePath = const_cast<char *>("key");
590     material.uidPath = const_cast<char *>("222");
591     material.userIdPath = const_cast<char *>("");
592 #ifdef SUPPORT_STORAGE_BACKUP
593     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
594 #else
595     struct HksStoreFileInfo fileInfo = { { 0 } };
596 #endif
597     HksGetFileInfo(&material, &fileInfo);
598     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
599 
600     const char *expectPath = HKS_KEY_STORE_PATH "/222/key";
601     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
602 }
603 
HksStorageUtilTest003()604 static void HksStorageUtilTest003()
605 {
606     HKS_LOG_I("enter HksStorageUtilTest003");
607     struct HksStoreMaterial material = { CE_PATH, 0 };
608     material.keyAliasPath = const_cast<char *>("alias");
609     material.storageTypePath = const_cast<char *>("key");
610     material.uidPath = const_cast<char *>("333");
611     material.userIdPath = const_cast<char *>("100");
612 #ifdef SUPPORT_STORAGE_BACKUP
613     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
614 #else
615     struct HksStoreFileInfo fileInfo = { { 0 } };
616 #endif
617     HksGetFileInfo(&material, &fileInfo);
618     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
619 
620     const char *expectPath = HKS_CE_ROOT_PATH "/100/" HKS_STORE_SERVICE_PATH "/333/key";
621     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
622 }
623 
HksStorageUtilTest004()624 static void HksStorageUtilTest004()
625 {
626     HKS_LOG_I("enter HksStorageUtilTest004");
627     struct HksStoreMaterial material = { ECE_PATH, 0 };
628     material.keyAliasPath = const_cast<char *>("alias");
629     material.storageTypePath = const_cast<char *>("key");
630     material.uidPath = const_cast<char *>("444");
631     material.userIdPath = const_cast<char *>("100");
632 #ifdef SUPPORT_STORAGE_BACKUP
633     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
634 #else
635     struct HksStoreFileInfo fileInfo = { { 0 } };
636 #endif
637     HksGetFileInfo(&material, &fileInfo);
638     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
639 
640     const char *expectPath = HKS_ECE_ROOT_PATH "/100/" HKS_STORE_SERVICE_PATH "/444/key";
641     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
642 }
643 
HksStorageUtilTest005()644 static void HksStorageUtilTest005()
645 {
646     HKS_LOG_I("enter HksStorageUtilTest005");
647     struct HksStoreMaterial material = { TMP_PATH, 0 };
648     material.keyAliasPath = const_cast<char *>("alias");
649     material.storageTypePath = const_cast<char *>("key");
650     material.uidPath = const_cast<char *>("555");
651     material.userIdPath = const_cast<char *>("555");
652 #ifdef SUPPORT_STORAGE_BACKUP
653     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
654 #else
655     struct HksStoreFileInfo fileInfo = { { 0 } };
656 #endif
657     HksGetFileInfo(&material, &fileInfo);
658     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
659 
660     const char *expectPath = HKS_KEY_STORE_TMP_PATH "/555/555/key";
661     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
662 }
663 
HksStorageUtilTest006()664 static void HksStorageUtilTest006()
665 {
666     HKS_LOG_I("enter HksStorageUtilTest006");
667     struct HksStoreMaterial material = { LITE_HAP_PATH, 0 };
668     material.keyAliasPath = const_cast<char *>("alias");
669     material.storageTypePath = const_cast<char *>("key");
670     material.uidPath = const_cast<char *>("hks_client");
671     material.userIdPath = const_cast<char *>("");
672 #ifdef SUPPORT_STORAGE_BACKUP
673     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
674 #else
675     struct HksStoreFileInfo fileInfo = { { 0 } };
676 #endif
677     HksGetFileInfo(&material, &fileInfo);
678     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
679 
680     const char *expectPath = HKS_KEY_STORE_LITE_HAP "/hks_client/key";
681     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
682 }
683 
HksStorageUtilTest007()684 static void HksStorageUtilTest007()
685 {
686     HKS_LOG_I("enter HksStorageUtilTest007");
687     struct HksStoreMaterial material = { RKC_IN_STANDARD_PATH, 0 };
688     material.keyAliasPath = const_cast<char *>("alias");
689     material.storageTypePath = const_cast<char *>("key");
690     material.uidPath = const_cast<char *>("hks_client");
691     material.userIdPath = const_cast<char *>("0");
692 #ifdef SUPPORT_STORAGE_BACKUP
693     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
694 #else
695     struct HksStoreFileInfo fileInfo = { { 0 } };
696 #endif
697     HksGetFileInfo(&material, &fileInfo);
698     HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath));
699 
700     const char *expectPath = HKS_KEY_RKC_PATH "/hks_client/key";
701     HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath));
702 }
703 }
704 }
705 }
706 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)707 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
708 {
709     (void)data;
710     (void)size;
711     OHOS::Security::Hks::HksStorageMultithreadTest001();
712     OHOS::Security::Hks::HksStorageMultithreadTest002();
713     OHOS::Security::Hks::HksStorageMultithreadTest003();
714     OHOS::Security::Hks::HksStorageFileLockTest001();
715     OHOS::Security::Hks::HksStorageFileLockTest002();
716     OHOS::Security::Hks::HksStorageFileLockTest003();
717     OHOS::Security::Hks::HksStorageFileLockTest004();
718     OHOS::Security::Hks::HksStorageTest001();
719     OHOS::Security::Hks::HksStorageTest002();
720     OHOS::Security::Hks::HksStorageTest003();
721     OHOS::Security::Hks::HksStorageTest004();
722     OHOS::Security::Hks::HksStorageTest005();
723     OHOS::Security::Hks::HksStorageTest006();
724     OHOS::Security::Hks::HksStorageManagerTest001();
725     OHOS::Security::Hks::HksStorageManagerTest002();
726     OHOS::Security::Hks::HksStorageManagerTest003();
727     OHOS::Security::Hks::HksStorageManagerTest004();
728     OHOS::Security::Hks::HksStorageManagerTest005();
729     OHOS::Security::Hks::HksStorageManagerTest006();
730     OHOS::Security::Hks::HksStorageUtilTest001();
731     OHOS::Security::Hks::HksStorageUtilTest002();
732     OHOS::Security::Hks::HksStorageUtilTest003();
733     OHOS::Security::Hks::HksStorageUtilTest004();
734     OHOS::Security::Hks::HksStorageUtilTest005();
735     OHOS::Security::Hks::HksStorageUtilTest006();
736     OHOS::Security::Hks::HksStorageUtilTest007();
737     return 0;
738 }
739