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