• 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 #include "template_cache_manager_fuzzer.h"
17 
18 #include <cinttypes>
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include "parcel.h"
23 
24 #include "iam_common_defines.h"
25 #include "iam_fuzz_test.h"
26 #include "iam_logger.h"
27 #include "template_cache_manager.h"
28 
29 #undef private
30 
31 #ifdef LOG_LABEL
32 #undef LOG_LABEL
33 #endif
34 #define LOG_TAG "USER_AUTH_SA"
35 
36 using namespace std;
37 using namespace OHOS::UserIam::Common;
38 using namespace OHOS::UserIam::UserAuth;
39 
40 namespace OHOS {
41 namespace UserIam {
42 namespace UserAuth {
43 namespace {
44 
45 TemplateCacheManager g_templateCacheManger;
46 
FuzzUpdateTemplateCache(Parcel & parcel)47 void FuzzUpdateTemplateCache(Parcel &parcel)
48 {
49     IAM_LOGI("begin");
50     AuthType authType = PIN;
51     g_templateCacheManger.UpdateTemplateCache(authType);
52     IAM_LOGI("end");
53 }
54 
FuzzProcessUserIdChange(Parcel & parcel)55 void FuzzProcessUserIdChange(Parcel &parcel)
56 {
57     IAM_LOGI("begin");
58     int newUserId = 200;
59     g_templateCacheManger.ProcessUserIdChange(newUserId);
60     IAM_LOGI("end");
61 }
62 
63 using FuzzFunc = decltype(FuzzUpdateTemplateCache);
64 FuzzFunc *g_fuzzFuncs[] = {
65     FuzzUpdateTemplateCache,
66     FuzzProcessUserIdChange,
67 };
68 
TemplateCacheManagerFuzzTest(const uint8_t * data,size_t size)69 void TemplateCacheManagerFuzzTest(const uint8_t *data, size_t size)
70 {
71     Parcel parcel;
72     parcel.WriteBuffer(data, size);
73     parcel.RewindRead(0);
74     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs)) / sizeof(FuzzFunc *);
75     auto fuzzFunc = g_fuzzFuncs[index];
76     fuzzFunc(parcel);
77     return;
78 }
79 } // namespace
80 } // namespace UserAuth
81 } // namespace UserIam
82 } // namespace OHOS
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87     OHOS::UserIam::UserAuth::TemplateCacheManagerFuzzTest(data, size);
88     return 0;
89 }
90