1 /*
2 * Copyright (c) 2025 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 "shieldstatus_fuzzer.h"
17
18 #include "securec.h"
19 #include "input_manager.h"
20 #include "mmi_log.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "ShieldStatusFuzzTest"
24
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr int32_t BYTES_PER_COMBINATION = 2;
29 constexpr size_t MAX_SHIELD_COMBINATIONS = 3;
30 } // namespace
31
32 template <class T>
GetObject(const uint8_t * data,size_t size,T & object)33 size_t GetObject(const uint8_t *data, size_t size, T &object)
34 {
35 size_t objectSize = sizeof(T);
36 if (objectSize > size) {
37 return 0;
38 }
39 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
40 if (ret != EOK) {
41 return 0;
42 }
43 return objectSize;
44 }
45
ShieldStatusFuzzTest(const uint8_t * data,size_t size)46 void ShieldStatusFuzzTest(const uint8_t *data, size_t size)
47 {
48 size_t startPos = 0;
49 for (size_t i = 0; i < MAX_SHIELD_COMBINATIONS && startPos + sizeof(int32_t) * BYTES_PER_COMBINATION <= size; ++i) {
50 int32_t shieldMode = 0;
51 size_t readSize = GetObject(data + startPos, size - startPos, shieldMode);
52 if (readSize == 0) {
53 MMI_HILOGD("Failed to read shieldMode");
54 break;
55 }
56 startPos += readSize;
57 int32_t random = 0;
58 readSize = GetObject(data + startPos, size - startPos, random);
59 if (readSize == 0) {
60 MMI_HILOGD("Failed to read random for isShield");
61 break;
62 }
63 startPos += readSize;
64 bool isShield = ((random % 2) == 0);
65 int32_t retSet = InputManager::GetInstance()->SetShieldStatus(shieldMode, isShield);
66 if (retSet != 0) {
67 MMI_HILOGD("SetShieldStatus failed. mode=%{public}d", shieldMode);
68 continue;
69 }
70 bool readBackShield = !isShield;
71 int32_t retGet = InputManager::GetInstance()->GetShieldStatus(shieldMode, readBackShield);
72 if (retGet != 0) {
73 MMI_HILOGD("GetShieldStatus failed. mode=%{public}d", shieldMode);
74 continue;
75 }
76
77 if (readBackShield != isShield) {
78 MMI_HILOGD("Mismatch: Set=%{public}d, mode=%{public}d",
79 isShield, shieldMode);
80 } else {
81 MMI_HILOGD("Match: mode=%{public}d, state=%{public}d", shieldMode, isShield);
82 }
83 }
84 }
85
86 } // namespace MMI
87 } // namespace OHOS
88
89 // Fuzzer entry point
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92 OHOS::MMI::ShieldStatusFuzzTest(data, size);
93 return 0;
94 }