1 /*
2 * Copyright (c) 2021-2022 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 * miscservices under the License is miscservices 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 "screenlockutils_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21
22 #include "preferences_util.h"
23
24
25 using namespace OHOS::ScreenLock;
26
27 namespace OHOS {
28 constexpr size_t THRESHOLD = 10;
29 constexpr size_t LENGTH = 1;
30
FuzzScreenlockUtils(const uint8_t * rawData,size_t size)31 bool FuzzScreenlockUtils(const uint8_t *rawData, size_t size)
32 {
33 if (size < LENGTH) {
34 return true;
35 }
36 auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
37 if (preferencesUtil == nullptr) {
38 return false;
39 }
40 int userId = 100;
41
42 // string
43 std::string stringlVal = "test";
44 preferencesUtil->SaveString(std::to_string(userId), stringlVal);
45 preferencesUtil->ObtainString(std::to_string(userId), stringlVal);
46 preferencesUtil->RemoveKey(std::to_string(userId));
47 preferencesUtil->Refresh();
48
49 // int
50 int defaulVal = 1;
51 preferencesUtil->SaveInt(std::to_string(userId), defaulVal);
52 preferencesUtil->ObtainInt(std::to_string(userId), defaulVal);
53 preferencesUtil->RemoveKey(std::to_string(userId));
54 preferencesUtil->Refresh();
55
56 // bool
57 bool boolVal = 0;
58 preferencesUtil->SaveBool(std::to_string(userId), boolVal);
59 preferencesUtil->ObtainBool(std::to_string(userId), boolVal);
60 preferencesUtil->RemoveKey(std::to_string(userId));
61 preferencesUtil->Refresh();
62
63 // long
64 int64_t longVal = 101;
65 preferencesUtil->SaveLong(std::to_string(userId), longVal);
66 preferencesUtil->ObtainLong(std::to_string(userId), longVal);
67 preferencesUtil->RemoveKey(std::to_string(userId));
68 preferencesUtil->Refresh();
69
70 // float
71 float floatVal = 1.0;
72 preferencesUtil->SaveFloat(std::to_string(userId), floatVal);
73 preferencesUtil->ObtainFloat(std::to_string(userId), floatVal);
74 preferencesUtil->RemoveKey(std::to_string(userId));
75 preferencesUtil->Refresh();
76
77 bool result = preferencesUtil->IsExistKey(std::to_string(userId));
78
79 preferencesUtil->RemoveAll();
80 preferencesUtil->RefreshSync();
81
82 return true;
83 }
84
85 } // namespace OHOS
86
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
89 {
90 if (size < OHOS::THRESHOLD) {
91 return 0;
92 }
93
94 /* Run your code on data */
95 OHOS::FuzzScreenlockUtils(data, size);
96 return 0;
97 }