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 {}
SetSearchDefaultFocusByWindowIdResult(const std::vector<AccessibilityElementInfo> & infos,const int32_t requestId)35 void SetSearchDefaultFocusByWindowIdResult(const std::vector<AccessibilityElementInfo> &infos,
36 const int32_t requestId) override {}
SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> & infos,const int32_t requestId)37 void SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> &infos,
38 const int32_t requestId) override {}
SetFindFocusedElementInfoResult(const AccessibilityElementInfo & info,const int32_t requestId)39 void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info,
40 const int32_t requestId) override {}
SetFocusMoveSearchResult(const AccessibilityElementInfo & info,const int32_t requestId)41 void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override {}
SetExecuteActionResult(const bool succeeded,const int32_t requestId)42 void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override {}
SetCursorPositionResult(const int32_t cursorPosition,const int32_t requestId)43 void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId) override {}
44 };
45
46 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)47 size_t GetObject(T &object, const uint8_t *data, size_t size)
48 {
49 size_t objectSize = sizeof(object);
50 if (objectSize > size) {
51 return 0;
52 }
53 return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
54 }
55
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)56 bool OnRemoteRequestFuzzTest(const uint8_t* data, size_t size)
57 {
58 if (data == nullptr || size < DATA_MIN_SIZE) {
59 return false;
60 }
61
62 size_t position = 0;
63 uint32_t code = 0;
64 MessageParcel mdata;
65 MessageParcel reply;
66 MessageOption option(MessageOption::TF_SYNC);
67
68 position += GetObject<uint32_t>(code, &data[position], size - position);
69 ElementOperatorCallbackImplFuzzTest callBackImp;
70 mdata.WriteInterfaceToken(ElementOperatorCallbackImplFuzzTest::GetDescriptor());
71 callBackImp.OnRemoteRequest(code, mdata, reply, option);
72 return true;
73 }
74 } // namespace Accessibility
75 } // namespace OHOS
76
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80 /* Run your code on data */
81 OHOS::Accessibility::OnRemoteRequestFuzzTest(data, size);
82 return 0;
83 }