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 "screenlockmanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "message_parcel.h"
22 #include "screenlock_app_manager.h"
23 #include "screenlock_callback.h"
24 #include "screenlock_manager.h"
25 #include "screenlock_manager_interface.h"
26 #include "screenlock_system_ability.h"
27 #include "screenlock_system_ability_callback.h"
28
29 using namespace OHOS::ScreenLock;
30
31 namespace OHOS {
32 constexpr size_t THRESHOLD = 10;
33 constexpr int32_t OFFSET = 4;
34 constexpr size_t LENGTH = 1;
35 constexpr size_t RANDNUM_ZERO = 0;
36 constexpr size_t RANDNUM_ONE = 1;
37 constexpr size_t RANDNUM_TWO = 2;
38
ConvertToUint32(const uint8_t * ptr)39 uint32_t ConvertToUint32(const uint8_t *ptr)
40 {
41 if (ptr == nullptr) {
42 return 0;
43 }
44 uint32_t bigvar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
45 return bigvar;
46 }
47
FuzzScreenlockManager(const uint8_t * rawData,size_t size)48 bool FuzzScreenlockManager(const uint8_t *rawData, size_t size)
49 {
50 uint32_t code = ConvertToUint32(rawData);
51 EventListener eventListener;
52 if (code == RANDNUM_ZERO) {
53 return ScreenLockManager::GetInstance()->IsScreenLocked();
54 }
55 if (code == RANDNUM_ONE) {
56 return ScreenLockManager::GetInstance()->GetSecure();
57 }
58 if (code == RANDNUM_TWO) {
59 sptr<ScreenlockCallback> listener_ = new ScreenlockCallback(eventListener);
60 int32_t ret = ScreenLockManager::GetInstance()->Lock(listener_);
61 return ret == E_SCREENLOCK_OK;
62 }
63 return true;
64 }
65
UnlockFuzzTest(const uint8_t * rawData,size_t size)66 bool UnlockFuzzTest(const uint8_t *rawData, size_t size)
67 {
68 EventListener eventListener;
69 sptr<ScreenlockCallback> listener_ = new ScreenlockCallback(eventListener);
70 if (size < LENGTH) {
71 return true;
72 }
73 int32_t ret = ScreenLockManager::GetInstance()->Unlock(static_cast<Action>(rawData[0] % 3), listener_);
74 return ret == E_SCREENLOCK_OK;
75 }
76
IsLockedFuzzTest(const uint8_t * rawData,size_t size)77 bool IsLockedFuzzTest(const uint8_t *rawData, size_t size)
78 {
79 if (size < LENGTH) {
80 return true;
81 }
82 bool isLocked = static_cast<bool>(rawData[0] % 2);
83 int32_t ret = ScreenLockManager::GetInstance()->IsLocked(isLocked);
84 return ret == E_SCREENLOCK_OK;
85 }
86
FuzzScreenlockAppManager(const uint8_t * rawData,size_t size)87 bool FuzzScreenlockAppManager(const uint8_t *rawData, size_t size)
88 {
89 uint32_t code = ConvertToUint32(rawData);
90 rawData = rawData + OFFSET;
91 size = size - OFFSET;
92 EventListener eventListener;
93 if (code == RANDNUM_ZERO) {
94 sptr<ScreenlockSystemAbilityCallback> listener_ = new ScreenlockSystemAbilityCallback(eventListener);
95 int32_t ret = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener_);
96 return ret == E_SCREENLOCK_OK;
97 }
98 if (code == RANDNUM_ONE) {
99 int param = 0;
100 std::string event(reinterpret_cast<const char *>(rawData), size);
101 int32_t ret = ScreenLockAppManager::GetInstance()->SendScreenLockEvent(event, param);
102 return ret == E_SCREENLOCK_OK;
103 }
104 return true;
105 }
106 } // namespace OHOS
107
108 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
110 {
111 if (size < OHOS::THRESHOLD) {
112 return 0;
113 }
114
115 /* Run your code on data */
116 OHOS::FuzzScreenlockManager(data, size);
117 OHOS::UnlockFuzzTest(data, size);
118 OHOS::IsLockedFuzzTest(data, size);
119 OHOS::FuzzScreenlockAppManager(data, size);
120 return 0;
121 }