• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "touchpadthreefingerstapswitch_fuzzer.h"
17 
18 #include "mmi_service.h"
19 
20 #include "multimodal_input_connect_stub.h"
21 #include "mmi_log.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "TouchPadThreeFingersTapSwitchFuzzTest"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr size_t MAX_ITERATION_COUNT = 2;
30 } // namespace
31 
32 template <typename 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 objSize = sizeof(T);
36     if (objSize > size) {
37         return 0;
38     }
39     errno_t ret = memcpy_s(&object, objSize, data, objSize);
40     return (ret == EOK) ? objSize : 0;
41 }
42 
TouchPadThreeFingersTapSwitchFuzzTest(const uint8_t * data,size_t size)43 void TouchPadThreeFingersTapSwitchFuzzTest(const uint8_t *data, size_t size)
44 {
45     if (data == nullptr || size < sizeof(int32_t)) {
46         return;
47     }
48 
49     size_t startPos = 0;
50     for (size_t i = 0; i < MAX_ITERATION_COUNT && (startPos + sizeof(int32_t) <= size); ++i) {
51         int32_t rnd = 0;
52         startPos += GetObject(data + startPos, size - startPos, rnd);
53         bool enable = (rnd % 2 == 0);
54 
55         MMIService::GetInstance()->SetTouchpadThreeFingersTapSwitch(enable);
56         MMI_HILOGD("SetTouchpadThreeFingersTapSwitch: %d", enable);
57 
58         bool status = !enable;
59         int32_t ret = MMIService::GetInstance()->GetTouchpadThreeFingersTapSwitch(status);
60         if (ret == 0) {
61             MMI_HILOGD("GetTouchpadThreeFingersTapSwitch: status=%{public}d", status);
62         } else {
63             MMI_HILOGD("GetTouchpadThreeFingersTapSwitch failed");
64         }
65     }
66 }
67 
68 } // namespace MMI
69 } // namespace OHOS
70 
71 // fuzz entry point
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74     OHOS::MMI::TouchPadThreeFingersTapSwitchFuzzTest(data, size);
75     return 0;
76 }