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 "accessibleabilitymanagerstateobserverstub_fuzzer.h"
17 #include "accessible_ability_manager_state_observer_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 ManagerStateObserverImplFuzzTest : public AccessibleAbilityManagerStateObserverStub {
29 public:
30     ManagerStateObserverImplFuzzTest() = default;
31     ~ManagerStateObserverImplFuzzTest() = default;
32 
OnStateChanged(const uint32_t stateType)33     void OnStateChanged(const uint32_t stateType) override {}
34 };
35 
36 template<class T>
GetData(T & code,const uint8_t * databuffer,size_t size)37 size_t GetData(T &code, const uint8_t *databuffer, size_t size)
38 {
39     size_t objectSize = sizeof(code);
40     if (objectSize > size) {
41         return 0;
42     }
43     return memcpy_s(&code, objectSize, databuffer, objectSize) == EOK ? objectSize : 0;
44 }
45 
StateObserverOnRemoteRequestFuzzTest(const uint8_t * databuffer,size_t size)46 bool StateObserverOnRemoteRequestFuzzTest(const uint8_t* databuffer, size_t size)
47 {
48     if (databuffer == nullptr || size < DATA_MIN_SIZE) {
49         return false;
50     }
51 
52     size_t position = 0;
53     uint32_t code = 0;
54     MessageParcel stateObserverData;
55     MessageParcel stateObserverReply;
56     MessageOption option(MessageOption::TF_SYNC);
57 
58     GetData<uint32_t>(code, &databuffer[position], size - position);
59     ManagerStateObserverImplFuzzTest callBack;
60     stateObserverData.WriteInterfaceToken(ManagerStateObserverImplFuzzTest::GetDescriptor());
61     callBack.OnRemoteRequest(code, stateObserverData, stateObserverReply, option);
62     return true;
63 }
64 } // namespace Accessibility
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::Accessibility::StateObserverOnRemoteRequestFuzzTest(data, size);
72     return 0;
73 }