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 "axis_event.h"
17 #include "axisevent_fuzzer.h"
18 #include "mmi_log.h"
19
20 #include "securec.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "AxisEventFuzzTest"
24
25 namespace OHOS {
26 namespace MMI {
27 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)28 size_t GetObject(T &object, const uint8_t *data, size_t size)
29 {
30 size_t objectSize = sizeof(object);
31 if (objectSize > size) {
32 return 0;
33 }
34 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
35 if (ret != EOK) {
36 return 0;
37 }
38 return objectSize;
39 }
40
AxisEventFuzzTest(const uint8_t * data,size_t size)41 bool AxisEventFuzzTest(const uint8_t *data, size_t size)
42 {
43 AxisEvent::from(nullptr);
44 auto axisEvent = AxisEvent::Create();
45 if (axisEvent == nullptr) {
46 return false;
47 }
48
49 size_t startPos = 0;
50 int32_t rowsBefore;
51 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
52 axisEvent->SetAxisAction(rowsBefore);
53
54 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
55 axisEvent->SetAxisType(rowsBefore);
56
57 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
58 axisEvent->SetAxisValue(rowsBefore);
59
60 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
61 axisEvent->ActionToShortStr(rowsBefore);
62
63 axisEvent->GetAxisAction();
64 axisEvent->GetAxisType();
65 axisEvent->GetAxisValue();
66 MMI_HILOGD("AxisEventFuzzTest");
67 return true;
68 }
69 } // MMI
70 } // OHOS
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75 /* Run your code on data */
76 if (data == nullptr) {
77 return 0;
78 }
79
80 OHOS::MMI::AxisEventFuzzTest(data, size);
81 return 0;
82 }
83