1 /*
2  * Copyright (c) 2024 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 "accessibilityelementoperatorcallbackstub_fuzzer.h"
17 #include "accessibility_element_operator_callback_stub.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace Accessibility {
22 namespace {
23     constexpr size_t DATA_MIN_SIZE = 100;
24     constexpr char END_CHAR = '\0';
25     constexpr size_t LEN = 10;
26 }
27 
28 class ElementOperatorCallbackImplFuzzTest : public AccessibilityElementOperatorCallbackStub {
29 public:
30     ElementOperatorCallbackImplFuzzTest() = default;
31     ~ElementOperatorCallbackImplFuzzTest() = default;
32 
SetSearchElementInfoByAccessibilityIdResult(const std::vector<AccessibilityElementInfo> & infos,const int32_t requestId)33     void SetSearchElementInfoByAccessibilityIdResult(const std::vector<AccessibilityElementInfo> &infos,
34         const int32_t requestId) override {}
SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> & infos,const int32_t requestId)35     void SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> &infos,
36         const int32_t requestId) override {}
SetFindFocusedElementInfoResult(const AccessibilityElementInfo & info,const int32_t requestId)37     void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info,
38         const int32_t requestId) override {}
SetFocusMoveSearchResult(const AccessibilityElementInfo & info,const int32_t requestId)39     void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override {}
SetExecuteActionResult(const bool succeeded,const int32_t requestId)40     void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override {}
SetCursorPositionResult(const int32_t cursorPosition,const int32_t requestId)41     void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId) override {}
42 };
43 
44 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)45 size_t GetObject(T &object, const uint8_t *data, size_t size)
46 {
47     size_t objectSize = sizeof(object);
48     if (objectSize > size) {
49         return 0;
50     }
51     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
52 }
53 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)54 bool OnRemoteRequestFuzzTest(const uint8_t* data, size_t size)
55 {
56     if (data == nullptr || size < DATA_MIN_SIZE) {
57         return false;
58     }
59 
60     size_t position = 0;
61     uint32_t code = 0;
62     MessageParcel mdata;
63     MessageParcel reply;
64     MessageOption option(MessageOption::TF_SYNC);
65 
66     position += GetObject<uint32_t>(code, &data[position], size - position);
67     ElementOperatorCallbackImplFuzzTest callBackImp;
68     mdata.WriteInterfaceToken(ElementOperatorCallbackImplFuzzTest::GetDescriptor());
69     callBackImp.OnRemoteRequest(code, mdata, reply, option);
70     return true;
71 }
72 } // namespace Accessibility
73 } // namespace OHOS
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     OHOS::Accessibility::OnRemoteRequestFuzzTest(data, size);
80     return 0;
81 }