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 "accessibilityenableabilitylistsobserverstub_fuzzer.h"
17 #include "accessibility_enable_ability_lists_observer_stub.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace Accessibility {
22 namespace {
23     constexpr size_t DATA_MIN_SIZE = 200;
24     constexpr char END_CHAR = '\0';
25     constexpr size_t LEN = 10;
26 }
27 
28 class EnableAbilityListsObserverTmplFuzzTest : public AccessibilityEnableAbilityListsObserverStub {
29 public:
30     EnableAbilityListsObserverTmplFuzzTest() = default;
31     ~EnableAbilityListsObserverTmplFuzzTest() = default;
32 
OnAccessibilityEnableAbilityListsChanged()33     void OnAccessibilityEnableAbilityListsChanged() override {};
OnAccessibilityInstallAbilityListsChanged()34     void OnAccessibilityInstallAbilityListsChanged() override {};
35 };
36 
37 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)38 size_t GetObject(T &object, const uint8_t *data, size_t size)
39 {
40     size_t objectSize = sizeof(object);
41     if (objectSize > size) {
42         return 0;
43     }
44     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
45 }
46 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)47 bool OnRemoteRequestFuzzTest(const uint8_t* data, size_t size)
48 {
49     if (data == nullptr || size < DATA_MIN_SIZE) {
50         return false;
51     }
52 
53     size_t position = 0;
54     uint32_t onCode = 0;
55     MessageParcel onData;
56     MessageParcel onReply;
57     MessageOption option(MessageOption::TF_SYNC);
58 
59     GetObject<uint32_t>(onCode, &data[position], size - position);
60     EnableAbilityListsObserverTmplFuzzTest callBackImp;
61     onData.WriteInterfaceToken(EnableAbilityListsObserverTmplFuzzTest::GetDescriptor());
62     callBackImp.OnRemoteRequest(onCode, onData, onReply, option);
63     return true;
64 }
65 } // namespace Accessibility
66 } // namespace OHOS
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71     /* Run your code on data */
72     OHOS::Accessibility::OnRemoteRequestFuzzTest(data, size);
73     return 0;
74 }