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