• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "accessibleabilityclient_fuzzer.h"
17 #include "accessibility_element_info.h"
18 #include "accessibility_gesture_inject_path.h"
19 #include "accessibility_ui_test_ability.h"
20 #include "accessible_ability_listener.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace {
25     constexpr size_t DATA_MIN_SIZE = 416;
26     constexpr char END_CHAR = '\0';
27     constexpr size_t LEN = 10;
28     constexpr size_t VEC_SIZE = 5;
29     constexpr size_t MAP_SIZE = 5;
30 } // namespace
31 
32 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)33 size_t GetObject(T &object, const uint8_t *data, size_t size)
34 {
35     size_t objectSize = sizeof(object);
36     if (objectSize > size) {
37         return 0;
38     }
39     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
40 }
41 
42 class AccessibleAbilityListenerForFuzzTest : public Accessibility::AccessibleAbilityListener {
43 public:
44     virtual ~AccessibleAbilityListenerForFuzzTest() = default;
OnAbilityConnected()45     void OnAbilityConnected() override {}
OnAbilityDisconnected()46     void OnAbilityDisconnected() override {}
OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo & eventInfo)47     void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo &eventInfo) override {}
OnKeyPressEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)48     bool OnKeyPressEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent) override
49     {
50         return false;
51     }
52 };
53 
GenerateRect(OHOS::Accessibility::Rect & bounds,const uint8_t * data,size_t size)54 static size_t GenerateRect(OHOS::Accessibility::Rect &bounds, const uint8_t* data, size_t size)
55 {
56     size_t position = 0;
57     int32_t posX = 0;
58     int32_t posY = 0;
59     position += GetObject<int32_t>(posX, &data[position], size - position);
60     position += GetObject<int32_t>(posY, &data[position], size - position);
61     bounds.SetLeftTopScreenPostion(posX, posY);
62 
63     position += GetObject<int32_t>(posX, &data[position], size - position);
64     position += GetObject<int32_t>(posY, &data[position], size - position);
65     bounds.SetRightBottomScreenPostion(posX, posY);
66     return position;
67 }
68 
GenerateRangeInfo(OHOS::Accessibility::RangeInfo & rangeInfo,const uint8_t * data,size_t size)69 static size_t GenerateRangeInfo(OHOS::Accessibility::RangeInfo &rangeInfo, const uint8_t* data, size_t size)
70 {
71     size_t position = 0;
72     int32_t int32Data = 0;
73     position += GetObject<int32_t>(int32Data, &data[position], size - position);
74     rangeInfo.SetMin(int32Data);
75 
76     position += GetObject<int32_t>(int32Data, &data[position], size - position);
77     rangeInfo.SetMax(int32Data);
78 
79     position += GetObject<int32_t>(int32Data, &data[position], size - position);
80     rangeInfo.SetCurrent(int32Data);
81     return position;
82 }
83 
GenerateGridInfo(OHOS::Accessibility::GridInfo & grid,const uint8_t * data,size_t size)84 static size_t GenerateGridInfo(OHOS::Accessibility::GridInfo &grid, const uint8_t* data, size_t size)
85 {
86     size_t position = 0;
87     int32_t rowCount = 0;
88     int32_t columnCount = 0;
89     int32_t selectionMode = 0;
90     position += GetObject<int32_t>(rowCount, &data[position], size - position);
91     position += GetObject<int32_t>(columnCount, &data[position], size - position);
92     position += GetObject<int32_t>(selectionMode, &data[position], size - position);
93     grid.SetGrid(rowCount, columnCount, selectionMode);
94     return position;
95 }
96 
GenerateGridItemInfo(OHOS::Accessibility::GridItemInfo & gridItem,const uint8_t * data,size_t size)97 static size_t GenerateGridItemInfo(OHOS::Accessibility::GridItemInfo &gridItem, const uint8_t* data, size_t size)
98 {
99     size_t position = 0;
100     int32_t columnIndex_ = 0;
101     int32_t rowIndex = 0;
102     int32_t columnSpan = 0;
103     int32_t rowSpan = 0;
104     position += GetObject<int32_t>(columnIndex_, &data[position], size - position);
105     position += GetObject<int32_t>(rowIndex, &data[position], size - position);
106     position += GetObject<int32_t>(columnSpan, &data[position], size - position);
107     position += GetObject<int32_t>(rowSpan, &data[position], size - position);
108     bool heading = data[position++] & 0x01;
109     bool selected = data[position++] & 0x01;
110     gridItem.SetGridItemInfo(rowIndex, rowSpan, columnIndex_, columnSpan, heading, selected);
111     return position;
112 }
113 
GenerateAccessibilityElementInfoP1(OHOS::Accessibility::AccessibilityElementInfo & sourceElementInfo,const uint8_t * data,size_t size,size_t & position)114 static void GenerateAccessibilityElementInfoP1(OHOS::Accessibility::AccessibilityElementInfo &sourceElementInfo,
115     const uint8_t* data, size_t size, size_t& position)
116 {
117     int32_t int32Data = 0;
118     position += GetObject<int32_t>(int32Data, &data[position], size - position);
119     sourceElementInfo.SetPageId(int32Data);
120 
121     position += GetObject<int32_t>(int32Data, &data[position], size - position);
122     sourceElementInfo.SetWindowId(int32Data);
123 
124     position += GetObject<int32_t>(int32Data, &data[position], size - position);
125     sourceElementInfo.SetAccessibilityId(int32Data);
126 
127     position += GetObject<int32_t>(int32Data, &data[position], size - position);
128     sourceElementInfo.SetParent(int32Data);
129 
130     position += GetObject<int32_t>(int32Data, &data[position], size - position);
131     sourceElementInfo.SetTextLengthLimit(int32Data);
132 
133     position += GetObject<int32_t>(int32Data, &data[position], size - position);
134     sourceElementInfo.SetCurrentIndex(int32Data);
135 
136     position += GetObject<int32_t>(int32Data, &data[position], size - position);
137     sourceElementInfo.SetBeginIndex(int32Data);
138 
139     position += GetObject<int32_t>(int32Data, &data[position], size - position);
140     sourceElementInfo.SetEndIndex(int32Data);
141 
142     position += GetObject<int32_t>(int32Data, &data[position], size - position);
143     sourceElementInfo.SetLiveRegion(int32Data);
144 
145     position += GetObject<int32_t>(int32Data, &data[position], size - position);
146     sourceElementInfo.SetLabeled(int32Data);
147 
148     position += GetObject<int32_t>(int32Data, &data[position], size - position);
149     sourceElementInfo.SetSelectedBegin(int32Data);
150 
151     position += GetObject<int32_t>(int32Data, &data[position], size - position);
152     sourceElementInfo.SetSelectedEnd(int32Data);
153 
154     position += GetObject<int32_t>(int32Data, &data[position], size - position);
155     sourceElementInfo.SetInputType(int32Data);
156 
157     position += GetObject<int32_t>(int32Data, &data[position], size - position);
158     sourceElementInfo.SetItemCounts(int32Data);
159 
160     position += GetObject<int32_t>(int32Data, &data[position], size - position);
161     sourceElementInfo.SetTextMovementStep(static_cast<OHOS::Accessibility::TextMoveUnit>(int32Data));
162 
163     for (size_t i = 0; i < VEC_SIZE; i++) {
164         position += GetObject<int32_t>(int32Data, &data[position], size - position);
165         sourceElementInfo.AddChild(int32Data);
166     }
167 }
168 
GenerateAccessibilityElementInfoP2(OHOS::Accessibility::AccessibilityElementInfo & sourceElementInfo,const uint8_t * data,size_t size,size_t & position)169 static void GenerateAccessibilityElementInfoP2(OHOS::Accessibility::AccessibilityElementInfo &sourceElementInfo,
170     const uint8_t* data, size_t size, size_t& position)
171 {
172     char name[LEN + 1];
173     name[LEN] = END_CHAR;
174     for (size_t i = 0; i < LEN; i++) {
175         position += GetObject<char>(name[i], &data[position], size - position);
176     }
177     std::string bundleName(name);
178     sourceElementInfo.SetBundleName(bundleName);
179 
180     for (size_t i = 0; i < LEN; i++) {
181         position += GetObject<char>(name[i], &data[position], size - position);
182     }
183     std::string componentType(name);
184     sourceElementInfo.SetComponentType(componentType);
185 
186     for (size_t i = 0; i < LEN; i++) {
187         position += GetObject<char>(name[i], &data[position], size - position);
188     }
189     std::string text(name);
190     sourceElementInfo.SetContent(text);
191 
192     for (size_t i = 0; i < LEN; i++) {
193         position += GetObject<char>(name[i], &data[position], size - position);
194     }
195     std::string hintText(name);
196     sourceElementInfo.SetHint(hintText);
197 
198     for (size_t i = 0; i < LEN; i++) {
199         position += GetObject<char>(name[i], &data[position], size - position);
200     }
201     std::string contentDescription(name);
202     sourceElementInfo.SetDescriptionInfo(contentDescription);
203 
204     for (size_t i = 0; i < LEN; i++) {
205         position += GetObject<char>(name[i], &data[position], size - position);
206     }
207     std::string resourceName(name);
208     sourceElementInfo.SetComponentResourceId(resourceName);
209 
210     for (size_t i = 0; i < LEN; i++) {
211         position += GetObject<char>(name[i], &data[position], size - position);
212     }
213     std::string inspectorKey(name);
214     sourceElementInfo.SetInspectorKey(inspectorKey);
215 
216     for (size_t i = 0; i < LEN; i++) {
217         position += GetObject<char>(name[i], &data[position], size - position);
218     }
219     std::string error(name);
220     sourceElementInfo.SetError(error);
221 }
222 
GenerateAccessibilityElementInfoP3(OHOS::Accessibility::AccessibilityElementInfo & sourceElementInfo,const uint8_t * data,size_t size,size_t & position)223 static void GenerateAccessibilityElementInfoP3(OHOS::Accessibility::AccessibilityElementInfo &sourceElementInfo,
224     const uint8_t* data, size_t size, size_t& position)
225 {
226     sourceElementInfo.SetCheckable(data[position++] & 0x01);
227     sourceElementInfo.SetChecked(data[position++] & 0x01);
228     sourceElementInfo.SetFocusable(data[position++] & 0x01);
229     sourceElementInfo.SetFocused(data[position++] & 0x01);
230     sourceElementInfo.SetVisible(data[position++] & 0x01);
231     sourceElementInfo.SetAccessibilityFocus(data[position++] & 0x01);
232     sourceElementInfo.SetSelected(data[position++] & 0x01);
233     sourceElementInfo.SetClickable(data[position++] & 0x01);
234     sourceElementInfo.SetLongClickable(data[position++] & 0x01);
235     sourceElementInfo.SetEnabled(data[position++] & 0x01);
236     sourceElementInfo.SetPassword(data[position++] & 0x01);
237     sourceElementInfo.SetScrollable(data[position++] & 0x01);
238     sourceElementInfo.SetEditable(data[position++] & 0x01);
239     sourceElementInfo.SetPopupSupported(data[position++] & 0x01);
240     sourceElementInfo.SetPluraLineSupported(data[position++] & 0x01);
241     sourceElementInfo.SetDeletable(data[position++] & 0x01);
242     sourceElementInfo.SetHinting(data[position++] & 0x01);
243     sourceElementInfo.SetEssential(data[position++] & 0x01);
244     sourceElementInfo.SetContentInvalid(data[position++] & 0x01);
245     sourceElementInfo.SetValidElement(data[position++] & 0x01);
246 
247     OHOS::Accessibility::Rect bounds;
248     position += GenerateRect(bounds, &data[position], size - position);
249     sourceElementInfo.SetRectInScreen(bounds);
250 
251     OHOS::Accessibility::RangeInfo rangeInfo;
252     position += GenerateRangeInfo(rangeInfo, &data[position], size - position);
253     sourceElementInfo.SetRange(rangeInfo);
254 
255     OHOS::Accessibility::GridInfo grid;
256     position += GenerateGridInfo(grid, &data[position], size - position);
257     sourceElementInfo.SetGrid(grid);
258 
259     OHOS::Accessibility::GridItemInfo gridItem;
260     position += GenerateGridItemInfo(gridItem, &data[position], size - position);
261     sourceElementInfo.SetGridItem(gridItem);
262 
263     int32_t int32Data = 0;
264     char name[LEN + 1];
265     name[LEN] = END_CHAR;
266     for (size_t count = 0; count < VEC_SIZE; count++) {
267         position += GetObject<int32_t>(int32Data, &data[position], size - position);
268         for (size_t i = 0; i < LEN; i++) {
269             position += GetObject<char>(name[i], &data[position], size - position);
270         }
271         std::string description(name);
272         OHOS::Accessibility::AccessibleAction action(
273             static_cast<OHOS::Accessibility::ActionType>(int32Data), description);
274         sourceElementInfo.AddAction(action);
275     }
276 }
277 
GenerateAccessibilityElementInfo(OHOS::Accessibility::AccessibilityElementInfo & sourceElementInfo,const uint8_t * data,size_t size)278 static size_t GenerateAccessibilityElementInfo(OHOS::Accessibility::AccessibilityElementInfo &sourceElementInfo,
279     const uint8_t* data, size_t size)
280 {
281     size_t position = 0;
282     GenerateAccessibilityElementInfoP1(sourceElementInfo, data, size, position);
283     GenerateAccessibilityElementInfoP2(sourceElementInfo, data, size, position);
284     GenerateAccessibilityElementInfoP3(sourceElementInfo, data, size, position);
285     return position;
286 }
287 
GenerateAccessibilityWindowInfo(OHOS::Accessibility::AccessibilityWindowInfo & sourceWindowInfo,const uint8_t * data,size_t size)288 static size_t GenerateAccessibilityWindowInfo(OHOS::Accessibility::AccessibilityWindowInfo &sourceWindowInfo,
289     const uint8_t* data, size_t size)
290 {
291     size_t position = 0;
292     uint64_t uint64Data = 0;
293     position += GetObject<uint64_t>(uint64Data, &data[position], size - position);
294     sourceWindowInfo.SetDisplayId(uint64Data);
295 
296     uint32_t uint32Data = 0;
297     position += GetObject<uint32_t>(uint32Data, &data[position], size - position);
298     sourceWindowInfo.SetWindowMode(uint32Data);
299 
300     position += GetObject<uint32_t>(uint32Data, &data[position], size - position);
301     sourceWindowInfo.SetWindowType(uint32Data);
302 
303     int32_t int32Data = 0;
304     position += GetObject<int32_t>(int32Data, &data[position], size - position);
305     sourceWindowInfo.SetAccessibilityWindowType(static_cast<OHOS::Accessibility::AccessibilityWindowType>(int32Data));
306 
307     position += GetObject<int32_t>(int32Data, &data[position], size - position);
308     sourceWindowInfo.SetWindowLayer(int32Data);
309 
310     position += GetObject<int32_t>(int32Data, &data[position], size - position);
311     sourceWindowInfo.SetWindowId(int32Data);
312 
313     sourceWindowInfo.SetActive(data[position++] & 0x01);
314     sourceWindowInfo.SetFocused(data[position++] & 0x01);
315     sourceWindowInfo.SetAccessibilityFocused(data[position++] & 0x01);
316 
317     OHOS::Accessibility::Rect bounds;
318     position += GenerateRect(bounds, &data[position], size - position);
319     sourceWindowInfo.SetRectInScreen(bounds);
320     return position;
321 }
322 
GenerateAccessibilityEventInfo(OHOS::Accessibility::AccessibilityEventInfo & sourceEventInfo,const uint8_t * data,size_t size)323 static size_t GenerateAccessibilityEventInfo(OHOS::Accessibility::AccessibilityEventInfo &sourceEventInfo,
324     const uint8_t* data, size_t size)
325 {
326     size_t position = 0;
327     uint32_t uint32Data = 0;
328     position += GetObject<uint32_t>(uint32Data, &data[position], size - position);
329     sourceEventInfo.SetEventType(static_cast<OHOS::Accessibility::EventType>(uint32Data));
330 
331     position += GetObject<uint32_t>(uint32Data, &data[position], size - position);
332     sourceEventInfo.SetGestureType(static_cast<OHOS::Accessibility::GestureType>(uint32Data));
333 
334     char name[LEN + 1];
335     name[LEN] = END_CHAR;
336     for (size_t i = 0; i < LEN; i++) {
337         position += GetObject<char>(name[i], &data[position], size - position);
338     }
339     std::string bundleName(name);
340     sourceEventInfo.SetBundleName(bundleName);
341 
342     for (size_t i = 0; i < LEN; i++) {
343         position += GetObject<char>(name[i], &data[position], size - position);
344     }
345     std::string notificationContent(name);
346     sourceEventInfo.SetNotificationContent(notificationContent);
347 
348     int32_t int32Data = 0;
349     position += GetObject<int32_t>(int32Data, &data[position], size - position);
350     sourceEventInfo.SetTriggerAction(static_cast<OHOS::Accessibility::ActionType>(int32Data));
351 
352     position += GetObject<int32_t>(int32Data, &data[position], size - position);
353     sourceEventInfo.SetTextMovementStep(static_cast<OHOS::Accessibility::TextMoveUnit>(int32Data));
354 
355     position += GetObject<int32_t>(int32Data, &data[position], size - position);
356     sourceEventInfo.SetWindowContentChangeTypes(
357         static_cast<OHOS::Accessibility::WindowsContentChangeTypes>(int32Data));
358 
359     position += GetObject<int32_t>(int32Data, &data[position], size - position);
360     sourceEventInfo.SetWindowChangeTypes(static_cast<OHOS::Accessibility::WindowUpdateType>(int32Data));
361 
362     position += GetObject<int32_t>(int32Data, &data[position], size - position);
363     sourceEventInfo.SetNotificationInfo(static_cast<OHOS::Accessibility::NotificationCategory>(int32Data));
364 
365     position += GetObject<int32_t>(int32Data, &data[position], size - position);
366     sourceEventInfo.SetPageId(int32Data);
367 
368     int64_t int64Data = 0;
369     position += GetObject<int64_t>(int64Data, &data[position], size - position);
370     sourceEventInfo.SetTimeStamp(int64Data);
371 
372     return position;
373 }
374 
DoSomethingInterestingWithRegisterAbilityListener(const uint8_t * data,size_t size)375 bool DoSomethingInterestingWithRegisterAbilityListener(const uint8_t* data, size_t size)
376 {
377     std::shared_ptr<AccessibleAbilityListenerForFuzzTest> listener =
378         std::make_shared<AccessibleAbilityListenerForFuzzTest>();
379     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->RegisterAbilityListener(listener);
380     return true;
381 }
382 
DoSomethingInterestingWithGetFocus(const uint8_t * data,size_t size)383 bool DoSomethingInterestingWithGetFocus(const uint8_t* data, size_t size)
384 {
385     if (data == nullptr || size < DATA_MIN_SIZE) {
386         return false;
387     }
388 
389     size_t startPos = 0;
390     int32_t focusType = 0;
391     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
392     GetObject<int32_t>(focusType, &data[startPos], size - startPos);
393     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetFocus(focusType, resultElementInfo);
394 
395     return true;
396 }
397 
DoSomethingInterestingWithGetFocusByElementInfo(const uint8_t * data,size_t size)398 bool DoSomethingInterestingWithGetFocusByElementInfo(const uint8_t* data, size_t size)
399 {
400     if (data == nullptr || size < DATA_MIN_SIZE) {
401         return false;
402     }
403 
404     size_t startPos = 0;
405     int32_t focusType = 0;
406     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
407     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
408     startPos += GetObject<int32_t>(focusType, &data[startPos], size - startPos);
409     GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
410     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetFocusByElementInfo(
411         sourceElementInfo, focusType, resultElementInfo);
412 
413     return true;
414 }
415 
DoSomethingInterestingWithInjectGesture(const uint8_t * data,size_t size)416 bool DoSomethingInterestingWithInjectGesture(const uint8_t* data, size_t size)
417 {
418     if (data == nullptr || size < DATA_MIN_SIZE) {
419         return false;
420     }
421 
422     size_t startPos = 0;
423     Accessibility::AccessibilityGesturePosition position;
424     float point = .0f;
425     startPos += GetObject<float>(point, &data[startPos], size - startPos);
426     position.positionX_ = point;
427 
428     startPos += GetObject<float>(point, &data[startPos], size - startPos);
429     position.positionY_ = point;
430 
431     std::shared_ptr<Accessibility::AccessibilityGestureInjectPath> gesturePath =
432         std::make_shared<Accessibility::AccessibilityGestureInjectPath>();
433     gesturePath->AddPosition(position);
434 
435     int64_t int64Data = 0;
436     GetObject<int64_t>(int64Data, &data[startPos], size - startPos);
437     gesturePath->SetDurationTime(int64Data);
438     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->InjectGesture(gesturePath);
439     return true;
440 }
441 
DoSomethingInterestingWithGetRoot(const uint8_t * data,size_t size)442 bool DoSomethingInterestingWithGetRoot(const uint8_t* data, size_t size)
443 {
444     if (data == nullptr || size < DATA_MIN_SIZE) {
445         return false;
446     }
447 
448     size_t startPos = 0;
449     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
450     GenerateAccessibilityElementInfo(resultElementInfo, &data[startPos], size - startPos);
451     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetRoot(resultElementInfo);
452 
453     return true;
454 }
455 
DoSomethingInterestingWithGetRootByWindow(const uint8_t * data,size_t size)456 bool DoSomethingInterestingWithGetRootByWindow(const uint8_t* data, size_t size)
457 {
458     if (data == nullptr || size < DATA_MIN_SIZE) {
459         return false;
460     }
461 
462     size_t startPos = 0;
463     OHOS::Accessibility::AccessibilityWindowInfo sourceWindowInfo;
464     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
465     GenerateAccessibilityWindowInfo(sourceWindowInfo, &data[startPos], size - startPos);
466     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetRootByWindow(
467         sourceWindowInfo, resultElementInfo);
468 
469     return true;
470 }
471 
DoSomethingInterestingWithGetWindow(const uint8_t * data,size_t size)472 bool DoSomethingInterestingWithGetWindow(const uint8_t* data, size_t size)
473 {
474     if (data == nullptr || size < DATA_MIN_SIZE) {
475         return false;
476     }
477 
478     size_t startPos = 0;
479     int32_t windowId = 0;
480     OHOS::Accessibility::AccessibilityWindowInfo resultWindowInfo;
481     GetObject<int32_t>(windowId, &data[startPos], size - startPos);
482     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetWindow(windowId, resultWindowInfo);
483 
484     return true;
485 }
486 
DoSomethingInterestingWithGetWindows(const uint8_t * data,size_t size)487 bool DoSomethingInterestingWithGetWindows(const uint8_t* data, size_t size)
488 {
489     if (data == nullptr || size < DATA_MIN_SIZE) {
490         return false;
491     }
492 
493     size_t startPos = 0;
494     std::vector<OHOS::Accessibility::AccessibilityWindowInfo> resultWindowInfos;
495     OHOS::Accessibility::AccessibilityWindowInfo windowInfo;
496     GenerateAccessibilityWindowInfo(windowInfo, &data[startPos], size - startPos);
497     resultWindowInfos.push_back(windowInfo);
498     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetWindows(resultWindowInfos);
499     return true;
500 }
501 
DoSomethingInterestingWithGetWindowsByDisplayId(const uint8_t * data,size_t size)502 bool DoSomethingInterestingWithGetWindowsByDisplayId(const uint8_t* data, size_t size)
503 {
504     if (data == nullptr || size < DATA_MIN_SIZE) {
505         return false;
506     }
507 
508     size_t startPos = 0;
509     uint64_t displayId = 0;
510     std::vector<OHOS::Accessibility::AccessibilityWindowInfo> resultWindowInfos;
511     GetObject<uint64_t>(displayId, &data[startPos], size - startPos);
512     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetWindows(displayId, resultWindowInfos);
513 
514     return true;
515 }
516 
DoSomethingInterestingWithGetNext(const uint8_t * data,size_t size)517 bool DoSomethingInterestingWithGetNext(const uint8_t* data, size_t size)
518 {
519     if (data == nullptr || size < DATA_MIN_SIZE) {
520         return false;
521     }
522 
523     size_t startPos = 0;
524     int32_t direction = 0;
525     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
526     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
527     startPos += GetObject<int32_t>(direction, &data[startPos], size - startPos);
528     GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
529     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetNext(sourceElementInfo,
530         static_cast<OHOS::Accessibility::FocusMoveDirection>(direction), resultElementInfo);
531 
532     return true;
533 }
534 
DoSomethingInterestingWithGetChildElementInfo(const uint8_t * data,size_t size)535 bool DoSomethingInterestingWithGetChildElementInfo(const uint8_t* data, size_t size)
536 {
537     if (data == nullptr || size < DATA_MIN_SIZE) {
538         return false;
539     }
540 
541     size_t startPos = 0;
542     int32_t index = 0;
543     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
544     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
545     startPos += GetObject<int32_t>(index, &data[startPos], size - startPos);
546     GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
547     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetChildElementInfo(
548         index, sourceElementInfo, resultElementInfo);
549     return true;
550 }
551 
DoSomethingInterestingWithGetChildren(const uint8_t * data,size_t size)552 bool DoSomethingInterestingWithGetChildren(const uint8_t* data, size_t size)
553 {
554     if (data == nullptr || size < DATA_MIN_SIZE) {
555         return false;
556     }
557 
558     size_t startPos = 0;
559     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
560     std::vector<OHOS::Accessibility::AccessibilityElementInfo> resultElementInfos;
561     GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
562     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetChildren(sourceElementInfo, resultElementInfos);
563 
564     return true;
565 }
566 
DoSomethingInterestingWithGetByContent(const uint8_t * data,size_t size)567 bool DoSomethingInterestingWithGetByContent(const uint8_t* data, size_t size)
568 {
569     if (data == nullptr || size < DATA_MIN_SIZE) {
570         return false;
571     }
572 
573     size_t startPos = 0;
574     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
575     std::vector<OHOS::Accessibility::AccessibilityElementInfo> resultElementInfos;
576     startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
577     char name[LEN + 1];
578     name[LEN] = END_CHAR;
579     for (size_t i = 0; i < LEN; i++) {
580         startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
581     }
582     std::string text(name);
583     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetByContent(
584         sourceElementInfo, text, resultElementInfos);
585 
586     return true;
587 }
588 
DoSomethingInterestingWithGetSource(const uint8_t * data,size_t size)589 bool DoSomethingInterestingWithGetSource(const uint8_t* data, size_t size)
590 {
591     if (data == nullptr || size < DATA_MIN_SIZE) {
592         return false;
593     }
594 
595     size_t startPos = 0;
596     OHOS::Accessibility::AccessibilityEventInfo sourceEventInfo;
597     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
598     GenerateAccessibilityEventInfo(sourceEventInfo, &data[startPos], size - startPos);
599     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetSource(sourceEventInfo, resultElementInfo);
600 
601     return true;
602 }
603 
DoSomethingInterestingWithGetParentElementInfo(const uint8_t * data,size_t size)604 bool DoSomethingInterestingWithGetParentElementInfo(const uint8_t* data, size_t size)
605 {
606     if (data == nullptr || size < DATA_MIN_SIZE) {
607         return false;
608     }
609 
610     size_t startPos = 0;
611     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
612     OHOS::Accessibility::AccessibilityElementInfo resultElementInfo;
613     GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
614     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetParentElementInfo(
615         sourceElementInfo, resultElementInfo);
616 
617     return true;
618 }
619 
DoSomethingInterestingWithExecuteAction(const uint8_t * data,size_t size)620 bool DoSomethingInterestingWithExecuteAction(const uint8_t* data, size_t size)
621 {
622     if (data == nullptr || size < DATA_MIN_SIZE) {
623         return false;
624     }
625 
626     size_t startPos = 0;
627     OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo;
628     startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos);
629     int32_t action = 0;
630     startPos += GetObject<int32_t>(action, &data[startPos], size - startPos);
631     std::map<std::string, std::string> actionArguments;
632     for (size_t count = 0; count < MAP_SIZE; count++) {
633         char name[LEN + 1];
634         name[LEN] = END_CHAR;
635         for (size_t i = 0; i < LEN; i++) {
636             startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
637         }
638         std::string action1(name);
639         for (size_t i = 0; i < LEN; i++) {
640             startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
641         }
642         std::string action2(name);
643         actionArguments.insert(std::make_pair(action1, action2));
644     }
645     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->ExecuteAction(
646         sourceElementInfo, static_cast<OHOS::Accessibility::ActionType>(action), actionArguments);
647 
648     return true;
649 }
650 
DoSomethingInterestingWithSetTargetBundleName(const uint8_t * data,size_t size)651 bool DoSomethingInterestingWithSetTargetBundleName(const uint8_t* data, size_t size)
652 {
653     if (data == nullptr || size < DATA_MIN_SIZE) {
654         return false;
655     }
656 
657     size_t startPos = 0;
658     std::vector<std::string> targetBundleNames;
659     for (size_t count = 0; count < VEC_SIZE; count++) {
660         char name[LEN + 1];
661         name[LEN] = END_CHAR;
662         for (size_t i = 0; i < LEN; i++) {
663             startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
664         }
665         std::string targetBundleName(name);
666         targetBundleNames.push_back(targetBundleName);
667     }
668     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->SetTargetBundleName(targetBundleNames);
669 
670     return true;
671 }
672 
DoSomethingInterestingWithSetCacheMode(const uint8_t * data,size_t size)673 bool DoSomethingInterestingWithSetCacheMode(const uint8_t* data, size_t size)
674 {
675     if (data == nullptr || size < DATA_MIN_SIZE) {
676         return false;
677     }
678 
679     size_t startPos = 0;
680     int32_t cacheMode = 0;
681     GetObject<int32_t>(cacheMode, &data[startPos], size - startPos);
682     OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->SetCacheMode(cacheMode);
683 
684     return true;
685 }
686 } // namespace OHOS
687 
688 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)689 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
690 {
691     /* Run your code on data */
692     OHOS::DoSomethingInterestingWithRegisterAbilityListener(data, size);
693     OHOS::DoSomethingInterestingWithGetFocus(data, size);
694     OHOS::DoSomethingInterestingWithGetFocusByElementInfo(data, size);
695     OHOS::DoSomethingInterestingWithInjectGesture(data, size);
696     OHOS::DoSomethingInterestingWithGetRoot(data, size);
697     OHOS::DoSomethingInterestingWithGetRootByWindow(data, size);
698     OHOS::DoSomethingInterestingWithGetWindow(data, size);
699     OHOS::DoSomethingInterestingWithGetWindows(data, size);
700     OHOS::DoSomethingInterestingWithGetWindowsByDisplayId(data, size);
701     OHOS::DoSomethingInterestingWithGetNext(data, size);
702     OHOS::DoSomethingInterestingWithGetChildElementInfo(data, size);
703     OHOS::DoSomethingInterestingWithGetChildren(data, size);
704     OHOS::DoSomethingInterestingWithGetByContent(data, size);
705     OHOS::DoSomethingInterestingWithGetSource(data, size);
706     OHOS::DoSomethingInterestingWithGetParentElementInfo(data, size);
707     OHOS::DoSomethingInterestingWithExecuteAction(data, size);
708     OHOS::DoSomethingInterestingWithSetTargetBundleName(data, size);
709     OHOS::DoSomethingInterestingWithSetCacheMode(data, size);
710     return 0;
711 }