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 "accessibleabilitymanagerservicestub_fuzzer.h"
17 #include "accessible_ability_manager_service_stub.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Accessibility {
22 namespace {
23 constexpr size_t FOO_MAX_LEN = 1024;
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr size_t BASE_CODE = 800;
26 constexpr size_t MESSAGE_SIZE = 61;
27 constexpr size_t FUZZ_NUM1 = 1;
28 constexpr size_t FUZZ_NUM2 = 2;
29 constexpr size_t FUZZ_NUM3 = 3;
30 constexpr size_t FUZZ_NUM8 = 8;
31 constexpr size_t FUZZ_NUM16 = 16;
32 constexpr size_t FUZZ_NUM24 = 24;
33 }
34
35 class AccessibleAbilityManagerServiceStubFuzzTest : public AccessibleAbilityManagerServiceStub {
36 public:
37 AccessibleAbilityManagerServiceStubFuzzTest() = default;
38 ~AccessibleAbilityManagerServiceStubFuzzTest() = default;
39
SendEvent(const AccessibilityEventInfo & uiEvent)40 RetError SendEvent(const AccessibilityEventInfo &uiEvent) override {return RET_OK;}
41
RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> & callback)42 uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override {return 0;}
43
RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> & callback)44 uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver>
45 &callback) override {return 0;}
46
RegisterEnableAbilityListsObserver(const sptr<IAccessibilityEnableAbilityListsObserver> & observer)47 void RegisterEnableAbilityListsObserver(
48 const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override {}
49
GetAbilityList(const uint32_t abilityTypes,const int32_t stateType,std::vector<AccessibilityAbilityInfo> & infos)50 RetError GetAbilityList(const uint32_t abilityTypes, const int32_t stateType,
51 std::vector<AccessibilityAbilityInfo> &infos) override {return RET_OK;}
52
RegisterElementOperator(const int32_t windowId,const sptr<IAccessibilityElementOperator> & operation,bool isApp=false)53 RetError RegisterElementOperator(const int32_t windowId,
54 const sptr<IAccessibilityElementOperator> &operation, bool isApp = false) override {return RET_OK;}
55
DeregisterElementOperator(const int32_t windowId)56 RetError DeregisterElementOperator(const int32_t windowId) override {return RET_OK;}
57
GetCaptionProperty(AccessibilityConfig::CaptionProperty & caption)58 RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override {return RET_OK;}
GetEnabledState()59 bool GetEnabledState() override {return true;}
GetCaptionState(bool & state)60 RetError GetCaptionState(bool &state) override {return RET_OK;}
GetTouchGuideState()61 bool GetTouchGuideState() override {return true;}
GetGestureState()62 bool GetGestureState() override {return true;}
GetKeyEventObserverState()63 bool GetKeyEventObserverState() override {return true;}
64
SetCaptionProperty(const AccessibilityConfig::CaptionProperty & caption)65 RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override {return RET_OK;}
SetCaptionState(const bool state)66 RetError SetCaptionState(const bool state) override {return RET_OK;}
67
EnableAbility(const std::string & name,const uint32_t capabilities)68 RetError EnableAbility(const std::string &name, const uint32_t capabilities) override {return RET_OK;}
GetEnabledAbilities(std::vector<std::string> & enabledAbilities)69 RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override {return RET_OK;}
DisableAbility(const std::string & name)70 RetError DisableAbility(const std::string &name) override {return RET_OK;}
GetActiveWindow()71 int32_t GetActiveWindow() override {return 0;}
72
EnableUITestAbility(const sptr<IRemoteObject> & obj)73 RetError EnableUITestAbility(const sptr<IRemoteObject> &obj) override {return RET_OK;}
DisableUITestAbility()74 RetError DisableUITestAbility() override {return RET_OK;}
75
SetScreenMagnificationState(const bool state)76 RetError SetScreenMagnificationState(const bool state) override {return RET_OK;}
SetShortKeyState(const bool state)77 RetError SetShortKeyState(const bool state) override {return RET_OK;}
SetMouseKeyState(const bool state)78 RetError SetMouseKeyState(const bool state) override {return RET_OK;}
SetMouseAutoClick(const int32_t time)79 RetError SetMouseAutoClick(const int32_t time) override {return RET_OK;}
SetShortkeyTarget(const std::string & name)80 RetError SetShortkeyTarget(const std::string &name) override {return RET_OK;}
SetShortkeyMultiTarget(const std::vector<std::string> & name)81 RetError SetShortkeyMultiTarget(const std::vector<std::string> &name) override {return RET_OK;}
SetHighContrastTextState(const bool state)82 RetError SetHighContrastTextState(const bool state) override {return RET_OK;}
SetInvertColorState(const bool state)83 RetError SetInvertColorState(const bool state) override {return RET_OK;}
SetAnimationOffState(const bool state)84 RetError SetAnimationOffState(const bool state) override {return RET_OK;}
SetAudioMonoState(const bool state)85 RetError SetAudioMonoState(const bool state) override {return RET_OK;}
SetDaltonizationState(const bool state)86 RetError SetDaltonizationState(const bool state) override {return RET_OK;}
SetDaltonizationColorFilter(const uint32_t filter)87 RetError SetDaltonizationColorFilter(const uint32_t filter) override {return RET_OK;}
SetContentTimeout(const uint32_t time)88 RetError SetContentTimeout(const uint32_t time) override {return RET_OK;}
SetBrightnessDiscount(const float discount)89 RetError SetBrightnessDiscount(const float discount) override {return RET_OK;}
SetAudioBalance(const float balance)90 RetError SetAudioBalance(const float balance) override {return RET_OK;}
SetClickResponseTime(const uint32_t time)91 RetError SetClickResponseTime(const uint32_t time) override {return RET_OK;}
SetIgnoreRepeatClickState(const bool state)92 RetError SetIgnoreRepeatClickState(const bool state) override {return RET_OK;}
SetIgnoreRepeatClickTime(const uint32_t time)93 RetError SetIgnoreRepeatClickTime(const uint32_t time) override {return RET_OK;}
94
GetScreenMagnificationState(bool & state)95 RetError GetScreenMagnificationState(bool &state) override {return RET_OK;}
GetShortKeyState(bool & state)96 RetError GetShortKeyState(bool &state) override {return RET_OK;}
GetMouseKeyState(bool & state)97 RetError GetMouseKeyState(bool &state) override {return RET_OK;}
GetMouseAutoClick(int32_t & time)98 RetError GetMouseAutoClick(int32_t &time) override {return RET_OK;}
GetShortkeyTarget(std::string & name)99 RetError GetShortkeyTarget(std::string &name) override {return RET_OK;}
GetShortkeyMultiTarget(std::vector<std::string> & name)100 RetError GetShortkeyMultiTarget(std::vector<std::string> &name) override {return RET_OK;}
GetHighContrastTextState(bool & state)101 RetError GetHighContrastTextState(bool &state) override {return RET_OK;}
GetInvertColorState(bool & state)102 RetError GetInvertColorState(bool &state) override {return RET_OK;}
GetAnimationOffState(bool & state)103 RetError GetAnimationOffState(bool &state) override {return RET_OK;}
GetAudioMonoState(bool & state)104 RetError GetAudioMonoState(bool &state) override {return RET_OK;}
GetDaltonizationState(bool & state)105 RetError GetDaltonizationState(bool &state) override {return RET_OK;}
GetDaltonizationColorFilter(uint32_t & type)106 RetError GetDaltonizationColorFilter(uint32_t &type) override {return RET_OK;}
GetContentTimeout(uint32_t & timer)107 RetError GetContentTimeout(uint32_t &timer) override {return RET_OK;}
GetBrightnessDiscount(float & brightness)108 RetError GetBrightnessDiscount(float &brightness) override {return RET_OK;}
GetAudioBalance(float & balance)109 RetError GetAudioBalance(float &balance) override {return RET_OK;}
GetClickResponseTime(uint32_t & time)110 RetError GetClickResponseTime(uint32_t& time) override {return RET_OK;}
GetIgnoreRepeatClickState(bool & state)111 RetError GetIgnoreRepeatClickState(bool& state) override {return RET_OK;}
GetIgnoreRepeatClickTime(uint32_t & time)112 RetError GetIgnoreRepeatClickTime(uint32_t& time) override {return RET_OK;}
GetAllConfigs(AccessibilityConfigData & configData)113 void GetAllConfigs(AccessibilityConfigData& configData) override {}
GetRealWindowAndElementId(int32_t & windowId,int64_t & elementId)114 void GetRealWindowAndElementId(int32_t& windowId, int64_t& elementId) override {}
GetSceneBoardInnerWinId(int32_t windowId,int64_t elementId,int32_t & innerWid)115 void GetSceneBoardInnerWinId(int32_t windowId, int64_t elementId, int32_t& innerWid) override {}
GetFocusedWindowId(int32_t & focusedWindowId)116 RetError GetFocusedWindowId(int32_t &focusedWindowId) override {return RET_OK;}
RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> & callback)117 uint32_t RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> &callback) override {return 0;}
118 };
119
GetU32Data(const char * ptr)120 uint32_t GetU32Data(const char* ptr)
121 {
122 return (ptr[0] << FUZZ_NUM24) | (ptr[FUZZ_NUM1] << FUZZ_NUM16) |
123 (ptr[FUZZ_NUM2] << FUZZ_NUM8) | (ptr[FUZZ_NUM3]);
124 }
125
OnRemoteRequestSvcFuzzTest(const char * data,size_t size)126 bool OnRemoteRequestSvcFuzzTest(const char* data, size_t size)
127 {
128 uint32_t code = (GetU32Data(data) % MESSAGE_SIZE) + BASE_CODE;
129 MessageParcel datas;
130 std::u16string descriptor = AccessibleAbilityManagerServiceStubFuzzTest::GetDescriptor();
131 datas.WriteInterfaceToken(descriptor);
132 datas.WriteBuffer(data, size);
133 datas.RewindRead(0);
134 MessageParcel reply;
135 MessageOption option;
136 AccessibleAbilityManagerServiceStubFuzzTest serviceStub;
137 serviceStub.OnRemoteRequest(code, datas, reply, option);
138 return true;
139 }
140 } // namespace Accessibility
141 } // namespace OHOS
142
143 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)144 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
145 {
146 /* Run your code on data */
147 if (data == nullptr) {
148 return 0;
149 }
150
151 if (size < OHOS::Accessibility::U32_AT_SIZE) {
152 return 0;
153 }
154
155 /* Validate the length of size */
156 if (size > OHOS::Accessibility::FOO_MAX_LEN) {
157 return 0;
158 }
159
160 char* ch = (char *)malloc(size + 1);
161 if (ch == nullptr) {
162 return 0;
163 }
164
165 (void)memset_s(ch, size + 1, 0x00, size + 1);
166 if (memcpy_s(ch, size, data, size) != EOK) {
167 free(ch);
168 ch = nullptr;
169 return 0;
170 }
171 /* Run your code on data */
172 OHOS::Accessibility::OnRemoteRequestSvcFuzzTest(ch, size);
173 free(ch);
174 ch = nullptr;
175 return 0;
176 }