diff --git a/interfaces/innerkits/test/fuzztest/BUILD.gn b/interfaces/innerkits/test/fuzztest/BUILD.gn index 98d402f7..cf58fbb7 100644 --- a/interfaces/innerkits/test/fuzztest/BUILD.gn +++ b/interfaces/innerkits/test/fuzztest/BUILD.gn @@ -14,5 +14,9 @@ group("fuzztest") { testonly = true - deps = [ "accessibilityconfig_fuzzer:fuzztest" ] + deps = [ + "accessibilityconfig_fuzzer:fuzztest", + "accessibilitysystemabilityclient_fuzzer:fuzztest", + "accessibleabilityclient_fuzzer:fuzztest", + ] } diff --git a/interfaces/innerkits/test/fuzztest/accessibilityconfig_fuzzer/accessibilityconfig_fuzzer.cpp b/interfaces/innerkits/test/fuzztest/accessibilityconfig_fuzzer/accessibilityconfig_fuzzer.cpp index 8e72be40..0f0bbe10 100644 --- a/interfaces/innerkits/test/fuzztest/accessibilityconfig_fuzzer/accessibilityconfig_fuzzer.cpp +++ b/interfaces/innerkits/test/fuzztest/accessibilityconfig_fuzzer/accessibilityconfig_fuzzer.cpp @@ -18,7 +18,7 @@ namespace OHOS { namespace { - constexpr size_t DATA_MIN_SIZE = 46; + constexpr size_t DATA_MIN_SIZE = 98; constexpr char END_CHAR = '\0'; constexpr size_t LEN = 10; } // namespace @@ -76,20 +76,52 @@ bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) size_t startPos = 0; abConfig.SetScreenMagnificationState(data[startPos++] & 0x01); + abConfig.SetMouseKeyState(data[startPos++] & 0x01); abConfig.SetCaptionState(data[startPos++] & 0x01); + abConfig.SetHighContrastTextState(data[startPos++] & 0x01); + abConfig.SetInvertColorState(data[startPos++] & 0x01); + abConfig.SetAnimationOffState(data[startPos++] & 0x01); + abConfig.SetAudioMonoState(data[startPos++] & 0x01); uint32_t temp = 0; startPos += GetObject(temp, &data[startPos], size - startPos); abConfig.SetContentTimeout(temp); + startPos += GetObject(temp, &data[startPos], size - startPos); + abConfig.SetDaltonizationColorFilter(static_cast(temp)); + + startPos += GetObject(temp, &data[startPos], size - startPos); + abConfig.SetMouseAutoClick(static_cast(temp)); + float tempFloat = 0; startPos += GetObject(tempFloat, &data[startPos], size - startPos); abConfig.SetBrightnessDiscount(tempFloat); + startPos += GetObject(tempFloat, &data[startPos], size - startPos); + abConfig.SetAudioBalance(tempFloat); + + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[startPos], LEN); + std::string nameStr(name); + abConfig.SetShortkeyTarget(nameStr); + startPos += LEN; + OHOS::AccessibilityConfig::CaptionProperty property; startPos += GenerateCaptionProperty(property, &data[startPos], size - startPos); abConfig.SetCaptionProperty(property); + std::memcpy(&name, &data[startPos], LEN); + std::string abilityName1(name); + startPos += LEN; + startPos += GetObject(temp, &data[startPos], size - startPos); + abConfig.EnableAbility(abilityName1, temp); + + std::memcpy(&name, &data[startPos], LEN); + std::string abilityName2(name); + startPos += LEN; + abConfig.DisableAbility(abilityName2); + return true; } } diff --git a/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/BUILD.gn b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/BUILD.gn new file mode 100644 index 00000000..aee42d61 --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("AccessibilitySystemAbilityClientFuzzTest") { + module_out_path = "accessibility/asacfwk" + fuzz_config_file = "../../fuzztest/accessibilitysystemabilityclient_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "accessibilitysystemabilityclient_fuzzer.cpp" ] + deps = [ + "//base/accessibility/interfaces/innerkits/asacfwk:accessibilityclient", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":AccessibilitySystemAbilityClientFuzzTest", + ] +} +############################################################################### diff --git a/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.cpp b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.cpp new file mode 100644 index 00000000..9dc7e453 --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.cpp @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "accessibilitysystemabilityclient_fuzzer.h" +#include "accessibility_system_ability_client.h" + +namespace OHOS { +namespace Accessibility { +namespace { + constexpr size_t DATA_MIN_SIZE = 200; + constexpr char END_CHAR = '\0'; + constexpr size_t LEN = 10; +} + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + std::memcpy(&object, data, objectSize); + return objectSize; +} + +class ElementOperatorForFuzzTest : public AccessibilityElementOperator { +public: + virtual ~ElementOperatorForFuzzTest() = default; + void SearchElementInfoByAccessibilityId(const int32_t elementId, + const int32_t requestId, AccessibilityElementOperatorCallback &callback, const int32_t mode) {} + + void SearchElementInfosByText(const int32_t elementId, const std::string &text, + const int32_t requestId, AccessibilityElementOperatorCallback &callback) {} + + void FindFocusedElementInfo(const int32_t elementId, const int32_t focusType, const int32_t requestId, + AccessibilityElementOperatorCallback &callback) {} + + void FocusMoveSearch(const int32_t elementId, const int32_t direction, const int32_t requestId, + AccessibilityElementOperatorCallback &callback) {} + + void ExecuteAction(const int32_t elementId, const int32_t action, + const std::map &actionArguments, + const int32_t requestId, AccessibilityElementOperatorCallback &callback) {} + void ClearFocus() {} + void OutsideTouch() {} +}; + +class StateObserverForFuzzTest : public AccessibilityStateObserver { +public: + virtual ~StateObserverForFuzzTest() = default; + void OnStateChanged(const bool state) {} +}; + +static size_t CreateEventInfo(AccessibilityEventInfo &eventInfo, const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t channelId = 0; + position += GetObject(channelId, &data[position], size - position); + eventInfo.SetChannelId(channelId); + int32_t componentId = 0; + position += GetObject(componentId, &data[position], size - position); + eventInfo.SetSource(componentId); + int32_t windowId = 0; + position += GetObject(windowId, &data[position], size - position); + eventInfo.SetWindowId(windowId); + int32_t index = 0; + position += GetObject(index, &data[position], size - position); + eventInfo.SetCurrentIndex(index); + position += GetObject(index, &data[position], size - position); + eventInfo.SetBeginIndex(index); + position += GetObject(index, &data[position], size - position); + eventInfo.SetEndIndex(index); + int32_t itemCounts = 0; + position += GetObject(itemCounts, &data[position], size - position); + eventInfo.SetItemCounts(itemCounts); + int32_t recordCount = 0; + position += GetObject(recordCount, &data[position], size - position); + eventInfo.SetRecordCount(recordCount); + WindowsContentChangeTypes changeTypes; + position += GetObject(changeTypes, &data[position], size - position); + eventInfo.SetWindowContentChangeTypes(changeTypes); + WindowUpdateType updateTypes; + position += GetObject(updateTypes, &data[position], size - position); + eventInfo.SetWindowChangeTypes(updateTypes); + EventType eventType; + position += GetObject(eventType, &data[position], size - position); + eventInfo.SetEventType(eventType); + int64_t timeStamp = 0; + position += GetObject(timeStamp, &data[position], size - position); + eventInfo.SetTimeStamp(timeStamp); + TextMoveUnit granularity; + position += GetObject(granularity, &data[position], size - position); + eventInfo.SetTextMovementStep(granularity); + ActionType action; + position += GetObject(action, &data[position], size - position); + eventInfo.SetTriggerAction(action); + NotificationCategory category; + position += GetObject(category, &data[position], size - position); + eventInfo.SetNotificationInfo(category); + GestureType gestureType; + position += GetObject(gestureType, &data[position], size - position); + eventInfo.SetGestureType(gestureType); + int32_t pageId = 0; + position += GetObject(pageId, &data[position], size - position); + eventInfo.SetPageId(pageId); + + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[position], LEN); + std::string className(name); + eventInfo.SetComponentType(className); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string beforeText(name); + eventInfo.SetBeforeText(beforeText); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string content(name); + eventInfo.AddContent(content); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string lastContent(name); + eventInfo.SetLatestContent(lastContent); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string contentDescription(name); + eventInfo.SetDescription(contentDescription); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string bundleName(name); + eventInfo.SetBundleName(bundleName); + position += LEN; + + return position; +} + +bool RegisterElementOperatorFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + auto instance = AccessibilitySystemAbilityClient::GetInstance(); + if (!instance) { + return false; + } + + size_t position = 0; + int32_t windowId = 0; + position += GetObject(windowId, &data[position], size - position); + std::shared_ptr elementOperator = std::make_shared(); + instance->RegisterElementOperator(windowId, elementOperator); + + position += GetObject(windowId, &data[position], size - position); + instance->DeregisterElementOperator(windowId); + return true; +} + +bool GetAbilityListFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + auto instance = AccessibilitySystemAbilityClient::GetInstance(); + if (!instance) { + return false; + } + + size_t position = 0; + uint32_t abilityTypes = 0; + position += GetObject(abilityTypes, &data[position], size - position); + AbilityStateType stateType; + position += GetObject(stateType, &data[position], size - position); + std::vector infos; + instance->GetAbilityList(abilityTypes, stateType, infos); + return true; +} + +bool SendEventFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + auto instance = AccessibilitySystemAbilityClient::GetInstance(); + if (!instance) { + return false; + } + + size_t position = 0; + EventType eventType; + position += GetObject(eventType, &data[position], size - position); + int32_t componentId = 0; + position += GetObject(componentId, &data[position], size - position); + instance->SendEvent(eventType, componentId); + + AccessibilityEventInfo eventInfo; + (void)CreateEventInfo(eventInfo, data, size - position); + instance->SendEvent(eventInfo); + return true; +} + +bool SubscribeStateObserverFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + auto instance = AccessibilitySystemAbilityClient::GetInstance(); + if (!instance) { + return false; + } + + size_t position = 0; + uint32_t eventTypes = 0; + position += GetObject(eventTypes, &data[position], size - position); + std::shared_ptr observer = std::make_shared(); + instance->SubscribeStateObserver(observer, eventTypes); + + position += GetObject(eventTypes, &data[position], size - position); + instance->UnsubscribeStateObserver(observer, eventTypes); + return true; +} +} // namespace Accessibility +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::Accessibility::RegisterElementOperatorFuzzTest(data, size); + OHOS::Accessibility::GetAbilityListFuzzTest(data, size); + OHOS::Accessibility::SendEventFuzzTest(data, size); + OHOS::Accessibility::SubscribeStateObserverFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.h b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.h new file mode 100644 index 00000000..4865b37d --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/accessibilitysystemabilityclient_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ACCESSIBILITYSYSTEMABILITYCLIENT_FUZZER_H +#define ACCESSIBILITYSYSTEMABILITYCLIENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "accessibility_system_ability_client_fuzzer" +#endif // ACCESSIBILITYSYSTEMABILITYCLIENT_FUZZER_H \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/corpus/init b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/project.xml b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibilitysystemabilityclient_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/BUILD.gn b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/BUILD.gn new file mode 100644 index 00000000..3d526717 --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("AccessibleAbilityClientFuzzTest") { + module_out_path = "accessibility/aafwk" + fuzz_config_file = "../../fuzztest/accessibleabilityclient_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "accessibleabilityclient_fuzzer.cpp" ] + deps = [ "//base/accessibility/interfaces/innerkits/aafwk:accessibleability" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":AccessibleAbilityClientFuzzTest", + ] +} +############################################################################### diff --git a/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.cpp b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.cpp new file mode 100644 index 00000000..dc301c6a --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.cpp @@ -0,0 +1,601 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "accessibleabilityclient_fuzzer.h" +#include "accessibility_element_info.h" +#include "accessibility_ui_test_ability.h" + +namespace OHOS { +namespace { + constexpr size_t DATA_MIN_SIZE = 416; + constexpr char END_CHAR = '\0'; + constexpr size_t LEN = 10; + constexpr size_t VEC_SIZE = 5; + constexpr size_t MAP_SIZE = 5; +} // namespace + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + std::memcpy(&object, data, objectSize); + return objectSize; +} + +static size_t GenerateRect(OHOS::Accessibility::Rect &bounds, const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t posX = 0; + int32_t posY = 0; + position += GetObject(posX, &data[position], size - position); + position += GetObject(posY, &data[position], size - position); + bounds.SetLeftTopScreenPostion(posX, posY); + + position += GetObject(posX, &data[position], size - position); + position += GetObject(posY, &data[position], size - position); + bounds.SetRightBottomScreenPostion(posX, posY); + return position; +} + +static size_t GenerateRangeInfo(OHOS::Accessibility::RangeInfo &rangeInfo, const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t int32Data = 0; + position += GetObject(int32Data, &data[position], size - position); + rangeInfo.SetMin(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + rangeInfo.SetMax(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + rangeInfo.SetCurrent(int32Data); + return position; +} + +static size_t GenerateGridInfo(OHOS::Accessibility::GridInfo &grid, const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t rowCount = 0; + int32_t columnCount = 0; + int32_t selectionMode = 0; + position += GetObject(rowCount, &data[position], size - position); + position += GetObject(columnCount, &data[position], size - position); + position += GetObject(selectionMode, &data[position], size - position); + grid.SetGrid(rowCount, columnCount, selectionMode); + return position; +} + +static size_t GenerateGridItemInfo(OHOS::Accessibility::GridItemInfo &gridItem, const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t columnIndex_ = 0; + int32_t rowIndex = 0; + int32_t columnSpan = 0; + int32_t rowSpan = 0; + position += GetObject(columnIndex_, &data[position], size - position); + position += GetObject(rowIndex, &data[position], size - position); + position += GetObject(columnSpan, &data[position], size - position); + position += GetObject(rowSpan, &data[position], size - position); + bool heading = data[position++] & 0x01; + bool selected = data[position++] & 0x01; + gridItem.SetGridItemInfo(rowIndex, rowSpan, columnIndex_, columnSpan, heading, selected); + return position; +} + +static size_t GenerateAccessibilityElementInfo(OHOS::Accessibility::AccessibilityElementInfo &sourceElementInfo, + const uint8_t* data, size_t size) +{ + size_t position = 0; + int32_t int32Data = 0; + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetWindowId(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetAccessibilityId(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetParent(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetTextLengthLimit(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetChannelId(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetCurrentIndex(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetBeginIndex(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetEndIndex(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetLiveRegion(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetLabeled(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetSelectedBegin(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetSelectedEnd(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.SetInputType(int32Data); + + for (size_t i = 0; i < VEC_SIZE; i++) { + position += GetObject(int32Data, &data[position], size - position); + sourceElementInfo.AddChild(int32Data); + } + + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[position], LEN); + std::string bundleName(name); + sourceElementInfo.SetBundleName(bundleName); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string componentType(name); + sourceElementInfo.SetComponentType(componentType); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string text(name); + sourceElementInfo.SetContent(text); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string hintText(name); + sourceElementInfo.SetHint(hintText); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string contentDescription(name); + sourceElementInfo.SetDescriptionInfo(contentDescription); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string resourceName(name); + sourceElementInfo.SetComponentResourceId(resourceName); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string inspectorKey(name); + sourceElementInfo.SetInspectorKey(inspectorKey); + position += LEN; + + std::memcpy(&name, &data[position], LEN); + std::string error(name); + sourceElementInfo.SetError(error); + position += LEN; + + sourceElementInfo.SetCheckable(data[position++] & 0x01); + sourceElementInfo.SetChecked(data[position++] & 0x01); + sourceElementInfo.SetFocusable(data[position++] & 0x01); + sourceElementInfo.SetFocused(data[position++] & 0x01); + sourceElementInfo.SetVisible(data[position++] & 0x01); + sourceElementInfo.SetAccessibilityFocus(data[position++] & 0x01); + sourceElementInfo.SetSelected(data[position++] & 0x01); + sourceElementInfo.SetClickable(data[position++] & 0x01); + sourceElementInfo.SetLongClickable(data[position++] & 0x01); + sourceElementInfo.SetEnabled(data[position++] & 0x01); + sourceElementInfo.SetPassword(data[position++] & 0x01); + sourceElementInfo.SetScrollable(data[position++] & 0x01); + sourceElementInfo.SetEditable(data[position++] & 0x01); + sourceElementInfo.SetPopupSupported(data[position++] & 0x01); + sourceElementInfo.SetPluraLineSupported(data[position++] & 0x01); + sourceElementInfo.SetDeletable(data[position++] & 0x01); + sourceElementInfo.SetHinting(data[position++] & 0x01); + sourceElementInfo.SetEssential(data[position++] & 0x01); + sourceElementInfo.SetContentInvalid(data[position++] & 0x01); + sourceElementInfo.SetValidElement(data[position++] & 0x01); + + OHOS::Accessibility::Rect bounds; + position += GenerateRect(bounds, &data[position], size - position); + sourceElementInfo.SetRectInScreen(bounds); + + OHOS::Accessibility::RangeInfo rangeInfo; + position += GenerateRangeInfo(rangeInfo, &data[position], size - position); + sourceElementInfo.SetRange(rangeInfo); + + OHOS::Accessibility::GridInfo grid; + position += GenerateGridInfo(grid, &data[position], size - position); + sourceElementInfo.SetGrid(grid); + + OHOS::Accessibility::GridItemInfo gridItem; + position += GenerateGridItemInfo(gridItem, &data[position], size - position); + sourceElementInfo.SetGridItem(gridItem); + + for (size_t i = 0; i < VEC_SIZE; i++) { + position += GetObject(int32Data, &data[position], size - position); + std::memcpy(&name, &data[position], LEN); + position += LEN; + std::string description(name); + + OHOS::Accessibility::AccessibleAction action( + static_cast(int32Data), description); + sourceElementInfo.AddAction(action); + } + return position; +} + +static size_t GenerateAccessibilityWindowInfo(OHOS::Accessibility::AccessibilityWindowInfo &sourceWindowInfo, + const uint8_t* data, size_t size) +{ + size_t position = 0; + uint64_t uint64Data = 0; + position += GetObject(uint64Data, &data[position], size - position); + sourceWindowInfo.SetDisplayId(uint64Data); + + int32_t int32Data = 0; + position += GetObject(int32Data, &data[position], size - position); + sourceWindowInfo.SetWindowLayer(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceWindowInfo.SetWindowId(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceWindowInfo.SetAnchorId(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceWindowInfo.SetChannelId(int32Data); + + sourceWindowInfo.SetActive(data[position++] & 0x01); + sourceWindowInfo.SetFocused(data[position++] & 0x01); + sourceWindowInfo.SetAccessibilityFocused(data[position++] & 0x01); + + OHOS::Accessibility::Rect bounds; + position += GenerateRect(bounds, &data[position], size - position); + sourceWindowInfo.SetRectInScreen(bounds); + return position; +} + +static size_t GenerateAccessibilityEventInfoPartial(OHOS::Accessibility::AccessibilityEventInfo &sourceEventInfo, + const uint8_t* data, size_t size) +{ + size_t position = 0; + uint32_t uint32Data = 0; + position += GetObject(uint32Data, &data[position], size - position); + sourceEventInfo.SetEventType(static_cast(uint32Data)); + + position += GetObject(uint32Data, &data[position], size - position); + sourceEventInfo.SetGestureType(static_cast(uint32Data)); + + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[position], LEN); + std::string bundleName(name); + sourceEventInfo.SetBundleName(bundleName); + position += LEN; + + int32_t int32Data = 0; + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetTriggerAction(static_cast(int32Data)); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetTextMovementStep(static_cast(int32Data)); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetWindowContentChangeTypes( + static_cast(int32Data)); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetWindowChangeTypes(static_cast(int32Data)); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetRecordCount(int32Data); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetNotificationInfo(static_cast(int32Data)); + + position += GetObject(int32Data, &data[position], size - position); + sourceEventInfo.SetPageId(int32Data); + + int64_t int64Data = 0; + position += GetObject(int64Data, &data[position], size - position); + sourceEventInfo.SetTimeStamp(int64Data); + + return position; +} + +static size_t GenerateAccessibilityEventInfo(OHOS::Accessibility::AccessibilityEventInfo &sourceEventInfo, + const uint8_t* data, size_t size) +{ + size_t position = 0; + position += GenerateAccessibilityEventInfoPartial(sourceEventInfo, &data[position], size - position); + + for (size_t i = 0; i < VEC_SIZE; i++) { + OHOS::Accessibility::AccessibilityEventInfo record; + position += GenerateAccessibilityEventInfoPartial(record, &data[position], size - position); + sourceEventInfo.AddRecord(record); + } + return position; +} + +bool DoSomethingInterestingWithGetFocus(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + int32_t focusType = 0; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GetObject(focusType, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetFocus(focusType, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetFocusByElementInfo(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + int32_t focusType = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GetObject(focusType, &data[startPos], size - startPos); + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetFocusByElementInfo( + sourceElementInfo, focusType, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetRootByWindow(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityWindowInfo sourceWindowInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GenerateAccessibilityWindowInfo(sourceWindowInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetRootByWindow( + sourceWindowInfo, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetWindows(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + uint64_t displayId = 0; + std::vector resultWindowInfos; + startPos += GetObject(displayId, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetWindows(displayId, resultWindowInfos); + + return true; +} + +bool DoSomethingInterestingWithExecuteCommonAction(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + uint32_t commonAction = 0; + startPos += GetObject(commonAction, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->ExecuteCommonAction( + static_cast(commonAction)); + + return true; +} + +bool DoSomethingInterestingWithGetNext(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + int32_t direction = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GetObject(direction, &data[startPos], size - startPos); + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetNext(sourceElementInfo, + static_cast(direction), resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetChildElementInfo(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + int32_t index = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GetObject(index, &data[startPos], size - startPos); + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetChildElementInfo( + index, sourceElementInfo, resultElementInfo); + return true; +} + +bool DoSomethingInterestingWithGetByContent(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + std::vector resultElementInfos; + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[startPos], LEN); + std::string text(name); + startPos += LEN; + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetByContent( + sourceElementInfo, text, resultElementInfos); + + return true; +} + +bool DoSomethingInterestingWithGetAnchor(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityWindowInfo sourceWindowInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GenerateAccessibilityWindowInfo(sourceWindowInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetAnchor(sourceWindowInfo, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetSource(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityEventInfo sourceEventInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GenerateAccessibilityEventInfo(sourceEventInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetSource(sourceEventInfo, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithGetParentElementInfo(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + OHOS::Accessibility::AccessibilityElementInfo resultElementInfo; + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->GetParentElementInfo( + sourceElementInfo, resultElementInfo); + + return true; +} + +bool DoSomethingInterestingWithExecuteAction(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + OHOS::Accessibility::AccessibilityElementInfo sourceElementInfo; + startPos += GenerateAccessibilityElementInfo(sourceElementInfo, &data[startPos], size - startPos); + int32_t action = 0; + startPos += GetObject(action, &data[startPos], size - startPos); + std::map actionArguments; + for (size_t i = 0; i < MAP_SIZE; i++) { + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[startPos], LEN); + std::string action1(name); + startPos += LEN; + + std::memcpy(&name, &data[startPos], LEN); + std::string action2(name); + startPos += LEN; + actionArguments.insert(std::make_pair(action1, action2)); + } + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->ExecuteAction( + sourceElementInfo, static_cast(action), actionArguments); + + return true; +} + +bool DoSomethingInterestingWithSetEventTypeFilter(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + uint32_t filter = 0; + startPos += GetObject(filter, &data[startPos], size - startPos); + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->SetEventTypeFilter(filter); + + return true; +} + +bool DoSomethingInterestingWithSetTargetBundleName(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + + size_t startPos = 0; + std::vector targetBundleNames; + for (size_t i = 0; i < VEC_SIZE; i++) { + char name[LEN + 1]; + name[LEN] = END_CHAR; + std::memcpy(&name, &data[startPos], LEN); + std::string targetBundleName(name); + startPos += LEN; + targetBundleNames.push_back(targetBundleName); + } + OHOS::Accessibility::AccessibilityUITestAbility::GetInstance()->SetTargetBundleName(targetBundleNames); + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DoSomethingInterestingWithGetFocus(data, size); + OHOS::DoSomethingInterestingWithGetFocusByElementInfo(data, size); + OHOS::DoSomethingInterestingWithGetRootByWindow(data, size); + OHOS::DoSomethingInterestingWithGetWindows(data, size); + OHOS::DoSomethingInterestingWithExecuteCommonAction(data, size); + OHOS::DoSomethingInterestingWithGetNext(data, size); + OHOS::DoSomethingInterestingWithGetChildElementInfo(data, size); + OHOS::DoSomethingInterestingWithGetByContent(data, size); + OHOS::DoSomethingInterestingWithGetAnchor(data, size); + OHOS::DoSomethingInterestingWithGetSource(data, size); + OHOS::DoSomethingInterestingWithGetParentElementInfo(data, size); + OHOS::DoSomethingInterestingWithExecuteAction(data, size); + OHOS::DoSomethingInterestingWithSetEventTypeFilter(data, size); + OHOS::DoSomethingInterestingWithSetTargetBundleName(data, size); + return 0; +} \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.h b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.h new file mode 100644 index 00000000..4f86ab2f --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/accessibleabilityclient_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ACCESSIBLEABILITYCLIENT_FUZZER_H +#define ACCESSIBLEABILITYCLIENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "accessible_ability_client_fuzzer" +#endif // ACCESSIBLEABILITYCLIENT_FUZZER_H \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/corpus/init b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/project.xml b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/interfaces/innerkits/test/fuzztest/accessibleabilityclient_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +