• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "accessibility_ipc_interface_code.h"
19 #include "accessible_ability_manager_service_stub.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace Accessibility {
24 namespace {
25 constexpr size_t FOO_MAX_LEN = 1024;
26 constexpr size_t U32_AT_SIZE = 4;
27 constexpr size_t BASE_CODE = 800;
28 constexpr size_t MESSAGE_SIZE = SERVICE_CODE_END - SEND_EVENT;
29 constexpr size_t FUZZ_NUM1 = 1;
30 constexpr size_t FUZZ_NUM2 = 2;
31 constexpr size_t FUZZ_NUM3 = 3;
32 constexpr size_t FUZZ_NUM8 = 8;
33 constexpr size_t FUZZ_NUM16 = 16;
34 constexpr size_t FUZZ_NUM24 = 24;
35 constexpr uint8_t DEVISOR_TWO = 2;
36 } // namespace
37 
38 class AccessibleAbilityManagerServiceStubFuzzTest : public AccessibleAbilityManagerServiceStub {
39 public:
40     AccessibleAbilityManagerServiceStubFuzzTest() = default;
41     ~AccessibleAbilityManagerServiceStubFuzzTest() = default;
42 
SendEvent(const AccessibilityEventInfo & uiEvent,const int32_t flag)43     RetError SendEvent(const AccessibilityEventInfo &uiEvent, const int32_t flag) override
44     {
45         return RET_OK;
46     }
47 
RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> & callback)48     uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override
49     {
50         return 0;
51     }
52 
RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> & callback)53     uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> &callback) override
54     {
55         return 0;
56     }
57 
RegisterEnableAbilityListsObserver(const sptr<IAccessibilityEnableAbilityListsObserver> & observer)58     void RegisterEnableAbilityListsObserver(const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override
59     {
60     }
61 
GetAbilityList(const uint32_t abilityTypes,const int32_t stateType,std::vector<AccessibilityAbilityInfo> & infos)62     RetError GetAbilityList(
63         const uint32_t abilityTypes, const int32_t stateType, std::vector<AccessibilityAbilityInfo> &infos) override
64     {
65         return RET_OK;
66     }
67 
RegisterElementOperator(const int32_t windowId,const sptr<IAccessibilityElementOperator> & operation,bool isApp=false)68     RetError RegisterElementOperator(
69         const int32_t windowId, const sptr<IAccessibilityElementOperator> &operation, bool isApp = false) override
70     {
71         return RET_OK;
72     }
73 
RegisterElementOperator(Registration parameter,const sptr<IAccessibilityElementOperator> & operation,bool isApp=false)74     RetError RegisterElementOperator(
75         Registration parameter, const sptr<IAccessibilityElementOperator> &operation, bool isApp = false) override
76     {
77         return RET_OK;
78     }
79 
DeregisterElementOperator(const int32_t windowId)80     RetError DeregisterElementOperator(const int32_t windowId) override
81     {
82         return RET_OK;
83     }
84 
DeregisterElementOperator(const int32_t windowId,const int32_t treeId)85     RetError DeregisterElementOperator(const int32_t windowId, const int32_t treeId) override
86     {
87         return RET_OK;
88     }
89 
GetCaptionProperty(AccessibilityConfig::CaptionProperty & caption)90     RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override
91     {
92         return RET_OK;
93     }
GetEnabledState()94     bool GetEnabledState() override
95     {
96         return true;
97     }
GetCaptionState(bool & state)98     RetError GetCaptionState(bool &state) override
99     {
100         return RET_OK;
101     }
GetTouchGuideState()102     bool GetTouchGuideState() override
103     {
104         return true;
105     }
GetGestureState()106     bool GetGestureState() override
107     {
108         return true;
109     }
GetKeyEventObserverState()110     bool GetKeyEventObserverState() override
111     {
112         return true;
113     }
GetScreenReaderState()114     bool GetScreenReaderState() override
115     {
116         return true;
117     }
118 
SetCaptionProperty(const AccessibilityConfig::CaptionProperty & caption)119     RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override
120     {
121         return RET_OK;
122     }
SetCaptionState(const bool state)123     RetError SetCaptionState(const bool state) override
124     {
125         return RET_OK;
126     }
127 
EnableAbility(const std::string & name,const uint32_t capabilities)128     RetError EnableAbility(const std::string &name, const uint32_t capabilities) override
129     {
130         return RET_OK;
131     }
GetEnabledAbilities(std::vector<std::string> & enabledAbilities)132     RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override
133     {
134         return RET_OK;
135     }
DisableAbility(const std::string & name)136     RetError DisableAbility(const std::string &name) override
137     {
138         return RET_OK;
139     }
GetActiveWindow()140     int32_t GetActiveWindow() override
141     {
142         return 0;
143     }
144 
EnableUITestAbility(const sptr<IRemoteObject> & obj)145     RetError EnableUITestAbility(const sptr<IRemoteObject> &obj) override
146     {
147         return RET_OK;
148     }
DisableUITestAbility()149     RetError DisableUITestAbility() override
150     {
151         return RET_OK;
152     }
153 
SetScreenMagnificationState(const bool state)154     RetError SetScreenMagnificationState(const bool state) override
155     {
156         return RET_OK;
157     }
SetShortKeyState(const bool state)158     RetError SetShortKeyState(const bool state) override
159     {
160         return RET_OK;
161     }
SetMouseKeyState(const bool state)162     RetError SetMouseKeyState(const bool state) override
163     {
164         return RET_OK;
165     }
SetMouseAutoClick(const int32_t time)166     RetError SetMouseAutoClick(const int32_t time) override
167     {
168         return RET_OK;
169     }
SetShortkeyTarget(const std::string & name)170     RetError SetShortkeyTarget(const std::string &name) override
171     {
172         return RET_OK;
173     }
SetShortkeyMultiTarget(const std::vector<std::string> & name)174     RetError SetShortkeyMultiTarget(const std::vector<std::string> &name) override
175     {
176         return RET_OK;
177     }
SetHighContrastTextState(const bool state)178     RetError SetHighContrastTextState(const bool state) override
179     {
180         return RET_OK;
181     }
SetInvertColorState(const bool state)182     RetError SetInvertColorState(const bool state) override
183     {
184         return RET_OK;
185     }
SetAnimationOffState(const bool state)186     RetError SetAnimationOffState(const bool state) override
187     {
188         return RET_OK;
189     }
SetAudioMonoState(const bool state)190     RetError SetAudioMonoState(const bool state) override
191     {
192         return RET_OK;
193     }
SetDaltonizationState(const bool state)194     RetError SetDaltonizationState(const bool state) override
195     {
196         return RET_OK;
197     }
SetDaltonizationColorFilter(const uint32_t filter)198     RetError SetDaltonizationColorFilter(const uint32_t filter) override
199     {
200         return RET_OK;
201     }
SetContentTimeout(const uint32_t time)202     RetError SetContentTimeout(const uint32_t time) override
203     {
204         return RET_OK;
205     }
SetBrightnessDiscount(const float discount)206     RetError SetBrightnessDiscount(const float discount) override
207     {
208         return RET_OK;
209     }
SetAudioBalance(const float balance)210     RetError SetAudioBalance(const float balance) override
211     {
212         return RET_OK;
213     }
SetClickResponseTime(const uint32_t time)214     RetError SetClickResponseTime(const uint32_t time) override
215     {
216         return RET_OK;
217     }
SetIgnoreRepeatClickState(const bool state)218     RetError SetIgnoreRepeatClickState(const bool state) override
219     {
220         return RET_OK;
221     }
SetIgnoreRepeatClickTime(const uint32_t time)222     RetError SetIgnoreRepeatClickTime(const uint32_t time) override
223     {
224         return RET_OK;
225     }
226 
GetScreenMagnificationState(bool & state)227     RetError GetScreenMagnificationState(bool &state) override
228     {
229         return RET_OK;
230     }
GetShortKeyState(bool & state)231     RetError GetShortKeyState(bool &state) override
232     {
233         return RET_OK;
234     }
GetMouseKeyState(bool & state)235     RetError GetMouseKeyState(bool &state) override
236     {
237         return RET_OK;
238     }
GetMouseAutoClick(int32_t & time)239     RetError GetMouseAutoClick(int32_t &time) override
240     {
241         return RET_OK;
242     }
GetShortkeyTarget(std::string & name)243     RetError GetShortkeyTarget(std::string &name) override
244     {
245         return RET_OK;
246     }
GetShortkeyMultiTarget(std::vector<std::string> & name)247     RetError GetShortkeyMultiTarget(std::vector<std::string> &name) override
248     {
249         return RET_OK;
250     }
GetHighContrastTextState(bool & state)251     RetError GetHighContrastTextState(bool &state) override
252     {
253         return RET_OK;
254     }
GetInvertColorState(bool & state)255     RetError GetInvertColorState(bool &state) override
256     {
257         return RET_OK;
258     }
GetAnimationOffState(bool & state)259     RetError GetAnimationOffState(bool &state) override
260     {
261         return RET_OK;
262     }
GetAudioMonoState(bool & state)263     RetError GetAudioMonoState(bool &state) override
264     {
265         return RET_OK;
266     }
GetDaltonizationState(bool & state)267     RetError GetDaltonizationState(bool &state) override
268     {
269         return RET_OK;
270     }
GetDaltonizationColorFilter(uint32_t & type)271     RetError GetDaltonizationColorFilter(uint32_t &type) override
272     {
273         return RET_OK;
274     }
GetContentTimeout(uint32_t & timer)275     RetError GetContentTimeout(uint32_t &timer) override
276     {
277         return RET_OK;
278     }
GetBrightnessDiscount(float & brightness)279     RetError GetBrightnessDiscount(float &brightness) override
280     {
281         return RET_OK;
282     }
GetAudioBalance(float & balance)283     RetError GetAudioBalance(float &balance) override
284     {
285         return RET_OK;
286     }
GetClickResponseTime(uint32_t & time)287     RetError GetClickResponseTime(uint32_t &time) override
288     {
289         return RET_OK;
290     }
GetIgnoreRepeatClickState(bool & state)291     RetError GetIgnoreRepeatClickState(bool &state) override
292     {
293         return RET_OK;
294     }
GetIgnoreRepeatClickTime(uint32_t & time)295     RetError GetIgnoreRepeatClickTime(uint32_t &time) override
296     {
297         return RET_OK;
298     }
GetAllConfigs(AccessibilityConfigData & configData)299     void GetAllConfigs(AccessibilityConfigData &configData) override
300     {
301     }
GetRealWindowAndElementId(int32_t & windowId,int64_t & elementId)302     void GetRealWindowAndElementId(int32_t &windowId, int64_t &elementId) override
303     {
304     }
GetSceneBoardInnerWinId(int32_t windowId,int64_t elementId,int32_t & innerWid)305     void GetSceneBoardInnerWinId(int32_t windowId, int64_t elementId, int32_t &innerWid) override
306     {
307     }
GetFocusedWindowId(int32_t & focusedWindowId)308     RetError GetFocusedWindowId(int32_t &focusedWindowId) override
309     {
310         return RET_OK;
311     }
RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> & callback)312     uint32_t RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> &callback) override
313     {
314         return 0;
315     }
PostDelayUnloadTask()316     void PostDelayUnloadTask() override
317     {
318     }
RemoveRequestId(int32_t requestId)319     void RemoveRequestId(int32_t requestId) override
320     {
321     }
GetRootParentId(int32_t windowsId,int32_t treeId)322     int64_t GetRootParentId(int32_t windowsId, int32_t treeId) override
323     {
324         return 0;
325     }
GetAllTreeId(int32_t windowId,std::vector<int32_t> & treeIds)326     RetError GetAllTreeId(int32_t windowId, std::vector<int32_t> &treeIds) override
327     {
328         return RET_OK;
329     }
330 };
331 
GetU32Data(const uint8_t * ptr)332 uint32_t GetU32Data(const uint8_t *ptr)
333 {
334     return (ptr[0] << FUZZ_NUM24) | (ptr[FUZZ_NUM1] << FUZZ_NUM16) | (ptr[FUZZ_NUM2] << FUZZ_NUM8) | (ptr[FUZZ_NUM3]);
335 }
336 
OnRemoteRequestSvcFuzzTest(const uint8_t * data,size_t size)337 bool OnRemoteRequestSvcFuzzTest(const uint8_t *data, size_t size)
338 {
339     uint32_t code = (GetU32Data(data) % MESSAGE_SIZE) + BASE_CODE;
340     MessageParcel datas;
341     std::u16string descriptor = AccessibleAbilityManagerServiceStubFuzzTest::GetDescriptor();
342     datas.WriteInterfaceToken(descriptor);
343     datas.WriteBuffer(data, size);
344     datas.RewindRead(0);
345     MessageParcel reply;
346     MessageOption option;
347     AccessibleAbilityManagerServiceStubFuzzTest serviceStub;
348     serviceStub.OnRemoteRequest(code, datas, reply, option);
349     return true;
350 }
351 
HandleSetDaltonizationStateTest(const uint8_t * data,size_t size)352 bool HandleSetDaltonizationStateTest(const uint8_t *data, size_t size)
353 {
354     MessageParcel datas;
355     bool isSetDaltonizationState = data[0] % DEVISOR_TWO;
356     std::u16string descriptor = AccessibleAbilityManagerServiceStubFuzzTest::GetDescriptor();
357     datas.WriteInterfaceToken(descriptor);
358     datas.WriteBool(isSetDaltonizationState);
359     MessageParcel reply;
360     MessageOption option;
361     AccessibleAbilityManagerServiceStubFuzzTest serviceStub;
362 
363     serviceStub.OnRemoteRequest(
364         static_cast<uint32_t>(AccessibilityInterfaceCode::SET_DALTONIZATION_STATE), datas, reply, option);
365     return true;
366 }
367 } // namespace Accessibility
368 } // namespace OHOS
369 
370 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)371 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
372 {
373     /* Run your code on data */
374     if (data == nullptr) {
375         return 0;
376     }
377 
378     if (size < OHOS::Accessibility::U32_AT_SIZE) {
379         return 0;
380     }
381 
382     OHOS::Accessibility::OnRemoteRequestSvcFuzzTest(data, size);
383     OHOS::Accessibility::HandleSetDaltonizationStateTest(data, size);
384     return 0;
385 }