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