From 7ef3d3514d3cb805f28f2bfa6eb6807c88fb678e Mon Sep 17 00:00:00 2001 From: xiaojianfeng Date: Thu, 21 Apr 2022 20:56:16 +0800 Subject: [PATCH] add fuzztest Signed-off-by: xiaojianfeng Change-Id: Id7a67daa4a7d85b7ef8376aa4a7135b39b6e4d63 --- dm/test/BUILD.gn | 1 + dm/test/fuzztest/BUILD.gn | 22 ++ .../fuzztest/displaymanager_fuzzer/BUILD.gn | 53 +++ .../displaymanager_fuzzer/corpus/init | 14 + .../displaymanager_fuzzer.cpp | 188 ++++++++++ .../displaymanager_fuzzer.h | 21 ++ .../displaymanager_fuzzer/project.xml | 25 ++ dm/test/fuzztest/screen_fuzzer/BUILD.gn | 52 +++ dm/test/fuzztest/screen_fuzzer/corpus/init | 14 + dm/test/fuzztest/screen_fuzzer/project.xml | 25 ++ .../fuzztest/screen_fuzzer/screen_fuzzer.cpp | 116 +++++++ .../fuzztest/screen_fuzzer/screen_fuzzer.h | 21 ++ .../fuzztest/screenmanager_fuzzer/BUILD.gn | 53 +++ .../fuzztest/screenmanager_fuzzer/corpus/init | 14 + .../fuzztest/screenmanager_fuzzer/project.xml | 25 ++ .../screenmanager_fuzzer.cpp | 325 ++++++++++++++++++ .../screenmanager_fuzzer.h | 21 ++ interfaces/innerkits/wm/window_manager.h | 6 +- wm/test/BUILD.gn | 1 + wm/test/fuzztest/BUILD.gn | 22 ++ wm/test/fuzztest/window_fuzzer/BUILD.gn | 52 +++ wm/test/fuzztest/window_fuzzer/corpus/init | 14 + wm/test/fuzztest/window_fuzzer/project.xml | 25 ++ .../fuzztest/window_fuzzer/window_fuzzer.cpp | 88 +++++ .../fuzztest/window_fuzzer/window_fuzzer.h | 21 ++ .../fuzztest/windowmanager_fuzzer/BUILD.gn | 53 +++ .../fuzztest/windowmanager_fuzzer/corpus/init | 14 + .../fuzztest/windowmanager_fuzzer/project.xml | 25 ++ .../windowmanager_fuzzer.cpp | 122 +++++++ .../windowmanager_fuzzer.h | 21 ++ wm/test/fuzztest/windowscene_fuzzer/BUILD.gn | 53 +++ .../fuzztest/windowscene_fuzzer/corpus/init | 14 + .../fuzztest/windowscene_fuzzer/project.xml | 25 ++ .../windowscene_fuzzer/windowscene_fuzzer.cpp | 88 +++++ .../windowscene_fuzzer/windowscene_fuzzer.h | 21 ++ 35 files changed, 1652 insertions(+), 3 deletions(-) create mode 100644 dm/test/fuzztest/BUILD.gn create mode 100644 dm/test/fuzztest/displaymanager_fuzzer/BUILD.gn create mode 100644 dm/test/fuzztest/displaymanager_fuzzer/corpus/init create mode 100644 dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.cpp create mode 100644 dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.h create mode 100644 dm/test/fuzztest/displaymanager_fuzzer/project.xml create mode 100644 dm/test/fuzztest/screen_fuzzer/BUILD.gn create mode 100644 dm/test/fuzztest/screen_fuzzer/corpus/init create mode 100644 dm/test/fuzztest/screen_fuzzer/project.xml create mode 100644 dm/test/fuzztest/screen_fuzzer/screen_fuzzer.cpp create mode 100644 dm/test/fuzztest/screen_fuzzer/screen_fuzzer.h create mode 100644 dm/test/fuzztest/screenmanager_fuzzer/BUILD.gn create mode 100644 dm/test/fuzztest/screenmanager_fuzzer/corpus/init create mode 100644 dm/test/fuzztest/screenmanager_fuzzer/project.xml create mode 100644 dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.cpp create mode 100644 dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.h create mode 100644 wm/test/fuzztest/BUILD.gn create mode 100644 wm/test/fuzztest/window_fuzzer/BUILD.gn create mode 100644 wm/test/fuzztest/window_fuzzer/corpus/init create mode 100644 wm/test/fuzztest/window_fuzzer/project.xml create mode 100644 wm/test/fuzztest/window_fuzzer/window_fuzzer.cpp create mode 100644 wm/test/fuzztest/window_fuzzer/window_fuzzer.h create mode 100644 wm/test/fuzztest/windowmanager_fuzzer/BUILD.gn create mode 100644 wm/test/fuzztest/windowmanager_fuzzer/corpus/init create mode 100644 wm/test/fuzztest/windowmanager_fuzzer/project.xml create mode 100644 wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.cpp create mode 100644 wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.h create mode 100644 wm/test/fuzztest/windowscene_fuzzer/BUILD.gn create mode 100644 wm/test/fuzztest/windowscene_fuzzer/corpus/init create mode 100644 wm/test/fuzztest/windowscene_fuzzer/project.xml create mode 100644 wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.cpp create mode 100644 wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.h diff --git a/dm/test/BUILD.gn b/dm/test/BUILD.gn index 591db18c..83393cb0 100644 --- a/dm/test/BUILD.gn +++ b/dm/test/BUILD.gn @@ -14,6 +14,7 @@ group("test") { testonly = true deps = [ + "fuzztest:fuzztest", "systemtest:systemtest", "unittest:unittest", ] diff --git a/dm/test/fuzztest/BUILD.gn b/dm/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..071f08a1 --- /dev/null +++ b/dm/test/fuzztest/BUILD.gn @@ -0,0 +1,22 @@ +# 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. + +group("fuzztest") { + testonly = true + + deps = [ + "displaymanager_fuzzer:fuzztest", + "screen_fuzzer:fuzztest", + "screenmanager_fuzzer:fuzztest", + ] +} diff --git a/dm/test/fuzztest/displaymanager_fuzzer/BUILD.gn b/dm/test/fuzztest/displaymanager_fuzzer/BUILD.gn new file mode 100644 index 00000000..05d1e3a4 --- /dev/null +++ b/dm/test/fuzztest/displaymanager_fuzzer/BUILD.gn @@ -0,0 +1,53 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/dm" + +##############################fuzztest########################################## +ohos_fuzztest("DisplayManagerFuzzTest") { + fuzz_config_file = + "//foundation/windowmanager/dm/test/fuzztest/displaymanager_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/interfaces/innerkits/dm", + "//utils/native/base/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "displaymanager_fuzzer.cpp" ] + public_deps = [ + "//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client", + "//foundation/windowmanager/dm:libdm", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":DisplayManagerFuzzTest", + ] +} +############################################################################### diff --git a/dm/test/fuzztest/displaymanager_fuzzer/corpus/init b/dm/test/fuzztest/displaymanager_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/dm/test/fuzztest/displaymanager_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/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.cpp b/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.cpp new file mode 100644 index 00000000..5b6c8adb --- /dev/null +++ b/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.cpp @@ -0,0 +1,188 @@ +/* + * 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 "displaymanager_fuzzer.h" + +#include "display_manager.h" + +namespace OHOS ::Rosen { +class DisplayListener : public DisplayManager::IDisplayListener { +public: + virtual void OnCreate(DisplayId) override + { + } + virtual void OnDestroy(DisplayId) override + { + } + virtual void OnChange(DisplayId) override + { + } +}; + +class DisplayPowerEventListener : public IDisplayPowerEventListener { +public: + virtual void OnDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) override + { + } +}; + +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; +} + +bool DisplayFuzzTest(const uint8_t* data, size_t size) +{ + DisplayId displayId; + ScreenId screenId; + if (data == nullptr || size < sizeof(displayId) + sizeof(screenId)) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + sptr displayListener = new DisplayListener(); + std::vector> displays = displayManager.GetAllDisplays(); + std::vector displayIds = displayManager.GetAllDisplayIds(); + displayManager.GetDefaultDisplayId(); + sptr defaultDisplay = displayManager.GetDefaultDisplay(); + + startPos += GetObject(displayId, data + startPos, size - startPos); + displayManager.GetDisplayById(displayId); + startPos += GetObject(screenId, data + startPos, size - startPos); + displayManager.GetDisplayByScreen(screenId); + displayManager.RegisterDisplayListener(displayListener); + displayManager.UnregisterDisplayListener(displayListener); + return true; +} + +bool GetScreenshotFuzzTest(const uint8_t* data, size_t size) +{ + DisplayId displayId; + Media::Rect rect; + Media::Size mediaSize; + int rotation; + if (data == nullptr || size < sizeof(displayId) + sizeof(rect) + sizeof(size) + sizeof(rotation)) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + startPos += GetObject(displayId, data + startPos, size - startPos); + displayManager.GetScreenshot(displayId); + startPos += GetObject(rect, data + startPos, size - startPos); + startPos += GetObject(mediaSize, data + startPos, size - startPos); + startPos += GetObject(rotation, data + startPos, size - startPos); + displayManager.GetScreenshot(displayId, rect, mediaSize, rotation); + return true; +} + +bool DisplayPowerFuzzTest(const uint8_t* data, size_t size) +{ + uint32_t reason; + DisplayId displayId; + uint32_t state; + if (data == nullptr || size < sizeof(reason) + sizeof(displayId) + sizeof(state)) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + sptr listener = new DisplayPowerEventListener(); + displayManager.RegisterDisplayPowerEventListener(listener); + + startPos += GetObject(reason, data + startPos, size - startPos); + displayManager.WakeUpBegin(static_cast(reason)); + displayManager.WakeUpEnd(); + displayManager.SuspendBegin(static_cast(reason)); + displayManager.SuspendEnd(); + + startPos += GetObject(state, data + startPos, size - startPos); + DisplayStateCallback callback = [](DisplayState state) { + }; + displayManager.SetDisplayState(static_cast(state), callback); + startPos += GetObject(displayId, data + startPos, size - startPos); + displayManager.GetDisplayState(displayId); + + displayManager.UnregisterDisplayPowerEventListener(listener); + return true; +} + +bool ScreenBrightnessFuzzTest(const uint8_t* data, size_t size) +{ + uint64_t screenId; + uint32_t level; + if (data == nullptr || size < sizeof(screenId) + sizeof(level)) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + + startPos += GetObject(screenId, data + startPos, size - startPos); + startPos += GetObject(level, data + startPos, size - startPos); + displayManager.SetScreenBrightness(screenId, level); + displayManager.GetScreenBrightness(screenId); + return true; +} + +bool FreezeFuzzTest(const uint8_t* data, size_t size) +{ + // 10 displays + if (data == nullptr || size < sizeof(DisplayId) * 10) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + // 10 displays + std::vector displays(10); + for (DisplayId& id : displays) { + startPos += GetObject(id, data + startPos, size - startPos); + } + displayManager.Freeze(displays); + displayManager.Unfreeze(displays); + return true; +} + +bool NotifyDisplayEventFuzzTest(const uint8_t* data, size_t size) +{ + // 10 displays + if (data == nullptr || size < sizeof(DisplayEvent)) { + return false; + } + size_t startPos = 0; + DisplayManager& displayManager = DisplayManager::GetInstance(); + uint32_t event; + startPos += GetObject(event, data + startPos, size - startPos); + displayManager.NotifyDisplayEvent(static_cast(event)); + return true; +} +} // namespace.OHOS::Rosen + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::Rosen::DisplayFuzzTest(data, size); + OHOS::Rosen::GetScreenshotFuzzTest(data, size); + OHOS::Rosen::DisplayPowerFuzzTest(data, size); + OHOS::Rosen::ScreenBrightnessFuzzTest(data, size); + OHOS::Rosen::FreezeFuzzTest(data, size); + OHOS::Rosen::NotifyDisplayEventFuzzTest(data, size); + return 0; +} + diff --git a/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.h b/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.h new file mode 100644 index 00000000..35c8eede --- /dev/null +++ b/dm/test/fuzztest/displaymanager_fuzzer/displaymanager_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_DISPLAY_MANAGER_FUZZER_H +#define TEST_FUZZTEST_DISPLAY_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "displaymanager_fuzzer" + +#endif \ No newline at end of file diff --git a/dm/test/fuzztest/displaymanager_fuzzer/project.xml b/dm/test/fuzztest/displaymanager_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/dm/test/fuzztest/displaymanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/dm/test/fuzztest/screen_fuzzer/BUILD.gn b/dm/test/fuzztest/screen_fuzzer/BUILD.gn new file mode 100644 index 00000000..58ae9c92 --- /dev/null +++ b/dm/test/fuzztest/screen_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/dm" + +##############################fuzztest########################################## +ohos_fuzztest("ScreenFuzzTest") { + fuzz_config_file = "//foundation/windowmanager/dm/test/fuzztest/screen_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/interfaces/innerkits/dm", + "//utils/native/base/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "screen_fuzzer.cpp" ] + public_deps = [ + "//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client", + "//foundation/windowmanager/dm:libdm", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":ScreenFuzzTest", + ] +} +############################################################################### diff --git a/dm/test/fuzztest/screen_fuzzer/corpus/init b/dm/test/fuzztest/screen_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/dm/test/fuzztest/screen_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/dm/test/fuzztest/screen_fuzzer/project.xml b/dm/test/fuzztest/screen_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/dm/test/fuzztest/screen_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.cpp b/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.cpp new file mode 100644 index 00000000..7676c16d --- /dev/null +++ b/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.cpp @@ -0,0 +1,116 @@ +/* + * 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 "screen_fuzzer.h" + +#include + +#include "display_manager.h" +#include "display.h" +#include "dm_common.h" +#include "screen.h" +#include "screen_manager.h" + +namespace OHOS::Rosen { +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; +} + +bool ScreenFuzzTest(const uint8_t *data, size_t size) +{ + DisplayManager& displayManager = DisplayManager::GetInstance(); + sptr display = displayManager.GetDefaultDisplay(); + if (display == nullptr) { + return false; + } + ScreenId screenId = display->GetScreenId(); + sptr screen = ScreenManager::GetInstance().GetScreenById(screenId); + if (screen == nullptr) { + return false; + } + uint32_t modeId; + Orientation orientation; + if (data == nullptr || size < sizeof(modeId) + sizeof(orientation)) { + return false; + } + size_t startPos = 0; + startPos += GetObject(modeId, data + startPos, size - startPos); + startPos += GetObject(orientation, data + startPos, size - startPos); + screen->SetScreenActiveMode(modeId); + screen->SetOrientation(orientation); + screen->SetScreenActiveMode(0); + screen->SetOrientation(Orientation::UNSPECIFIED); + return true; +} + +bool ColorGamutsFuzzTest(const uint8_t *data, size_t size) +{ + DisplayManager& displayManager = DisplayManager::GetInstance(); + sptr display = displayManager.GetDefaultDisplay(); + if (display == nullptr) { + return false; + } + ScreenId screenId = display->GetScreenId(); + sptr screen = ScreenManager::GetInstance().GetScreenById(screenId); + if (screen == nullptr) { + return false; + } + int32_t colorGamutIdx; + uint32_t gamutMap; + if (data == nullptr || size < sizeof(colorGamutIdx) + sizeof(gamutMap)) { + return false; + } + size_t startPos = 0; + startPos += GetObject(colorGamutIdx, data + startPos, size - startPos); + startPos += GetObject(gamutMap, data + startPos, size - startPos); + std::vector colorGamuts; + screen->GetScreenSupportedColorGamuts(colorGamuts); + size_t colorGamutsSize = colorGamuts.size(); + if (colorGamutsSize == 0) { + return false; + } + int32_t index = colorGamutIdx % colorGamutsSize; + screen->SetScreenColorGamut(index); + ScreenColorGamut colorGamut; + screen->GetScreenColorGamut(colorGamut); + if (colorGamuts[index] != colorGamut) { + std::cout << "colorGamuts not equals." << std::endl; + } + screen->SetScreenGamutMap(static_cast(gamutMap)); + ScreenGamutMap screenGamutMap; + screen->GetScreenGamutMap(screenGamutMap); + if (static_cast(gamutMap) != screenGamutMap) { + std::cout << "screenGamutMaps not equals." << std::endl; + } + return true; +} +} // namespace.OHOS::Rosen + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::Rosen::ScreenFuzzTest(data, size); + OHOS::Rosen::ColorGamutsFuzzTest(data, size); + return 0; +} + diff --git a/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.h b/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.h new file mode 100644 index 00000000..6a95d476 --- /dev/null +++ b/dm/test/fuzztest/screen_fuzzer/screen_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_SCREEN_FUZZER_H +#define TEST_FUZZTEST_SCREEN_FUZZER_H + +#define FUZZ_PROJECT_NAME "screen_fuzzer" + +#endif \ No newline at end of file diff --git a/dm/test/fuzztest/screenmanager_fuzzer/BUILD.gn b/dm/test/fuzztest/screenmanager_fuzzer/BUILD.gn new file mode 100644 index 00000000..62de19d7 --- /dev/null +++ b/dm/test/fuzztest/screenmanager_fuzzer/BUILD.gn @@ -0,0 +1,53 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/dm" + +##############################fuzztest########################################## +ohos_fuzztest("ScreenManagerFuzzTest") { + fuzz_config_file = + "//foundation/windowmanager/dm/test/fuzztest/screenmanager_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/interfaces/innerkits/dm", + "//utils/native/base/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "screenmanager_fuzzer.cpp" ] + public_deps = [ + "//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client", + "//foundation/windowmanager/dm:libdm", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":ScreenManagerFuzzTest", + ] +} +############################################################################### diff --git a/dm/test/fuzztest/screenmanager_fuzzer/corpus/init b/dm/test/fuzztest/screenmanager_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/dm/test/fuzztest/screenmanager_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/dm/test/fuzztest/screenmanager_fuzzer/project.xml b/dm/test/fuzztest/screenmanager_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/dm/test/fuzztest/screenmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.cpp b/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.cpp new file mode 100644 index 00000000..35c625dd --- /dev/null +++ b/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.cpp @@ -0,0 +1,325 @@ +/* + * 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 "screenmanager_fuzzer.h" + +#include + +#include "dm_common.h" +#include "screen.h" +#include "screen_manager.h" + +namespace OHOS::Rosen { +class ScreenListener : public ScreenManager::IScreenListener { +public: + virtual void OnConnect(ScreenId screenId) override + { + }; + + virtual void OnDisconnect(ScreenId screenId) override + { + } + + virtual void OnChange(ScreenId screenId) override + { + } +}; + +class ScreenGroupListener : public ScreenManager::IScreenGroupListener { +public: + virtual void OnChange(const std::vector& screenIds, ScreenGroupChangeEvent event) override + { + } +}; + +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; +} + +bool ScreenPowerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t screenPowerState; + uint32_t powerStateChangeReason; + if (data == nullptr || size < sizeof(screenPowerState) + sizeof(powerStateChangeReason)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + + startPos += GetObject(screenPowerState, data + startPos, size - startPos); + startPos += GetObject(powerStateChangeReason, data + startPos, size - startPos); + screenManager.SetScreenPowerForAll(static_cast(screenPowerState), + static_cast(startPos)); + auto allScreen = screenManager.GetAllScreens(); + for (auto screen: allScreen) { + ScreenPowerState powerState = screenManager.GetScreenPower(screen->GetId()); + if (powerState != static_cast(screenPowerState)) { + std::cout << "ScreenPowerState is not equals to powerState." << std::endl; + } + } + + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} + +bool MakeMirrorWithVirtualScreenFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(VirtualScreenOption)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + + std::string name; + startPos += GetObject(name, data + startPos, size - startPos); + VirtualScreenOption option = { name }; + startPos += GetObject(option.width_, data + startPos, size - startPos); + startPos += GetObject(option.height_, data + startPos, size - startPos); + startPos += GetObject(option.density_, data + startPos, size - startPos); + startPos += GetObject(option.flags_, data + startPos, size - startPos); + startPos += GetObject(option.isForShot_, data + startPos, size - startPos); + ScreenId screenId = screenManager.CreateVirtualScreen(option); + if (screenId == SCREEN_ID_INVALID) { + return false; + } + screenManager.SetVirtualScreenSurface(screenId, nullptr); + auto screes = screenManager.GetAllScreens(); + auto iter = std::find_if(screes.begin(), screes.end(), [screenId](sptr screen) { + return screenId == screen->GetId(); + }); + if (iter == screes.end()) { + std::cout << "screenId is not in all screens." << std::endl; + } + // make mirror + ScreenId groupId = screenManager.MakeMirror(0, { screenId }); + if (groupId == SCREEN_ID_INVALID) { + screenManager.DestroyVirtualScreen(screenId); + return false; + } + sptr group = screenManager.GetScreenGroup(groupId); + if (group == nullptr) { + screenManager.DestroyVirtualScreen(screenId); + return false; + } + std::vector ids = group->GetChildIds(); + if (std::find(ids.begin(), ids.end(), screenId) == ids.end()) { + std::cout << "screenId is not in group children." << std::endl; + } + screenManager.RemoveVirtualScreenFromGroup(ids); + screenManager.DestroyVirtualScreen(screenId); + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} + +bool MakeExpandWithVirtualScreenFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(VirtualScreenOption)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + + std::string name; + startPos += GetObject(name, data + startPos, size - startPos); + VirtualScreenOption option = { name }; + startPos += GetObject(option.width_, data + startPos, size - startPos); + startPos += GetObject(option.height_, data + startPos, size - startPos); + startPos += GetObject(option.density_, data + startPos, size - startPos); + startPos += GetObject(option.flags_, data + startPos, size - startPos); + startPos += GetObject(option.isForShot_, data + startPos, size - startPos); + ScreenId screenId = screenManager.CreateVirtualScreen(option); + if (screenId == SCREEN_ID_INVALID) { + return false; + } + screenManager.SetVirtualScreenSurface(screenId, nullptr); + auto screes = screenManager.GetAllScreens(); + auto iter = std::find_if(screes.begin(), screes.end(), [screenId](sptr screen) { + return screenId == screen->GetId(); + }); + if (iter == screes.end()) { + std::cout << "screenId is not in all screens." << std::endl; + } + // make expand + std::vector options = {{0, 0, 0}, {screenId, 0, 0}}; + ScreenId groupId = screenManager.MakeExpand(options); + if (groupId == SCREEN_ID_INVALID) { + screenManager.DestroyVirtualScreen(screenId); + return false; + } + sptr group = screenManager.GetScreenGroup(groupId); + if (group == nullptr) { + screenManager.DestroyVirtualScreen(screenId); + return false; + } + std::vector ids = group->GetChildIds(); + if (std::find(ids.begin(), ids.end(), screenId) == ids.end()) { + std::cout << "screenId is not in group children." << std::endl; + } + screenManager.RemoveVirtualScreenFromGroup(ids); + screenManager.DestroyVirtualScreen(screenId); + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} + +bool CreateAndDestroyVirtualScreenFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(VirtualScreenOption)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + + std::string name; + startPos += GetObject(name, data + startPos, size - startPos); + VirtualScreenOption option = { name }; + startPos += GetObject(option.width_, data + startPos, size - startPos); + startPos += GetObject(option.height_, data + startPos, size - startPos); + startPos += GetObject(option.density_, data + startPos, size - startPos); + startPos += GetObject(option.flags_, data + startPos, size - startPos); + startPos += GetObject(option.isForShot_, data + startPos, size - startPos); + ScreenId screenId = screenManager.CreateVirtualScreen(option); + if (screenId == SCREEN_ID_INVALID) { + return false; + } + screenManager.DestroyVirtualScreen(screenId); + startPos += GetObject(screenId, data + startPos, size - startPos); + screenManager.DestroyVirtualScreen(screenId); + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} + +bool SetVirtualScreenSurfaceFuzzTest(const uint8_t *data, size_t size) +{ + ScreenId screenId; + if (data == nullptr || size < sizeof(screenId)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + startPos += GetObject(screenId, data + startPos, size - startPos); + screenManager.SetVirtualScreenSurface(screenId, nullptr); + return true; +} + +bool RemoveVirtualScreenFromGroupFuzzTest(const uint8_t *data, size_t size) +{ + ScreenId screenId; + if (data == nullptr || size < sizeof(screenId)) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + startPos += GetObject(screenId, data + startPos, size - startPos); + std::vector screenIds = {screenId, screenId, screenId}; + screenManager.RemoveVirtualScreenFromGroup(screenIds); + return true; +} + +bool MakeMirrorFuzzTest(const uint8_t *data, size_t size) +{ + ScreenId screenId; + // 10 screens. + if (data == nullptr || size < sizeof(ScreenId) * 10) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + + std::vector screenIds; + // 10 screens + for (size_t i = 0; i < 10; i++) { + startPos += GetObject(screenId, data + startPos, size - startPos); + screenIds.emplace_back(screenId); + } + startPos += GetObject(screenId, data + startPos, size - startPos); + screenManager.MakeMirror(screenId, screenIds); + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} + +bool MakeExpandFuzzTest(const uint8_t *data, size_t size) +{ + ScreenId screenId; + // 10 screens. + if (data == nullptr || size < sizeof(ScreenId) * 10) { + return false; + } + size_t startPos = 0; + ScreenManager &screenManager = ScreenManager::GetInstance(); + sptr screenListener = new ScreenListener(); + screenManager.RegisterScreenListener(screenListener); + sptr screenGroupListener = new ScreenGroupListener(); + screenManager.RegisterScreenGroupListener(screenGroupListener); + std::vector options; + // 10 screens + for (size_t i = 0; i < 10; i++) { + startPos += GetObject(screenId, data + startPos, size - startPos); + ExpandOption option = {screenId, 0, 0}; + options.emplace_back(option); + } + screenManager.MakeExpand(options); + screenManager.UnregisterScreenGroupListener(screenGroupListener); + screenManager.UnregisterScreenListener(screenListener); + return true; +} +} // namespace.OHOS::Rosen + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::Rosen::ScreenPowerFuzzTest(data, size); + OHOS::Rosen::MakeMirrorWithVirtualScreenFuzzTest(data, size); + OHOS::Rosen::MakeMirrorFuzzTest(data, size); + OHOS::Rosen::MakeExpandWithVirtualScreenFuzzTest(data, size); + OHOS::Rosen::MakeExpandFuzzTest(data, size); + OHOS::Rosen::CreateAndDestroyVirtualScreenFuzzTest(data, size); + OHOS::Rosen::SetVirtualScreenSurfaceFuzzTest(data, size); + OHOS::Rosen::RemoveVirtualScreenFromGroupFuzzTest(data, size); + return 0; +} + diff --git a/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.h b/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.h new file mode 100644 index 00000000..19765f03 --- /dev/null +++ b/dm/test/fuzztest/screenmanager_fuzzer/screenmanager_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_SCREEN_MANAGER_FUZZER_H +#define TEST_FUZZTEST_SCREEN_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "screenmanager_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/wm/window_manager.h b/interfaces/innerkits/wm/window_manager.h index 91acbc2a..9c5bc3d7 100644 --- a/interfaces/innerkits/wm/window_manager.h +++ b/interfaces/innerkits/wm/window_manager.h @@ -54,7 +54,7 @@ public: sptr abilityToken_; }; -class IFocusChangedListener : public RefBase { +class IFocusChangedListener : virtual public RefBase { public: virtual void OnFocused(const sptr& focusChangeInfo) = 0; @@ -82,7 +82,7 @@ public: bool isVisible_ { false }; }; -class IVisibilityChangedListener : public RefBase { +class IVisibilityChangedListener : virtual public RefBase { public: virtual void OnWindowVisibilityChanged(const std::vector>& windowVisibilityInfo) = 0; }; @@ -118,7 +118,7 @@ private: bool VectorMarshalling(Parcel& parcel) const; static void VectorUnmarshalling(Parcel& parcel, AccessibilityWindowInfo* windowInfo); }; -class IWindowUpdateListener : public RefBase { +class IWindowUpdateListener : virtual public RefBase { public: virtual void OnWindowUpdate(const sptr& windowInfo, WindowUpdateType type) = 0; }; diff --git a/wm/test/BUILD.gn b/wm/test/BUILD.gn index 591db18c..83393cb0 100644 --- a/wm/test/BUILD.gn +++ b/wm/test/BUILD.gn @@ -14,6 +14,7 @@ group("test") { testonly = true deps = [ + "fuzztest:fuzztest", "systemtest:systemtest", "unittest:unittest", ] diff --git a/wm/test/fuzztest/BUILD.gn b/wm/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..61757343 --- /dev/null +++ b/wm/test/fuzztest/BUILD.gn @@ -0,0 +1,22 @@ +# 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. + +group("fuzztest") { + testonly = true + + deps = [ + "window_fuzzer:fuzztest", + "windowmanager_fuzzer:fuzztest", + "windowscene_fuzzer:fuzztest", + ] +} diff --git a/wm/test/fuzztest/window_fuzzer/BUILD.gn b/wm/test/fuzztest/window_fuzzer/BUILD.gn new file mode 100644 index 00000000..ac9161c2 --- /dev/null +++ b/wm/test/fuzztest/window_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/wm" + +##############################fuzztest########################################## +ohos_fuzztest("WindowFuzzTest") { + fuzz_config_file = "//foundation/windowmanager/wm/test/fuzztest/window_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/wmserver/include", + "//foundation/windowmanager/interfaces/innerkits/wm", + "//utils/native/base/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "window_fuzzer.cpp" ] + public_deps = [ + "//foundation/windowmanager/wm:libwm", + "//foundation/windowmanager/wmserver:libwms", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":WindowFuzzTest", + ] +} +############################################################################### diff --git a/wm/test/fuzztest/window_fuzzer/corpus/init b/wm/test/fuzztest/window_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/wm/test/fuzztest/window_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/wm/test/fuzztest/window_fuzzer/project.xml b/wm/test/fuzztest/window_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/wm/test/fuzztest/window_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/wm/test/fuzztest/window_fuzzer/window_fuzzer.cpp b/wm/test/fuzztest/window_fuzzer/window_fuzzer.cpp new file mode 100644 index 00000000..1db362fc --- /dev/null +++ b/wm/test/fuzztest/window_fuzzer/window_fuzzer.cpp @@ -0,0 +1,88 @@ +/* + * 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 "parcel.h" +#include "window.h" +#include "window_manager.h" + +using namespace OHOS::Rosen; + +namespace OHOS { +namespace { + constexpr size_t DATA_MIN_SIZE = 2; +} +class FocusChangedListener : public IFocusChangedListener { +public: + virtual void OnFocused(const sptr& focusChangeInfo) override + { + } + + virtual void OnUnfocused(const sptr& focusChangeInfo) override + { + } +}; + +class SystemBarChangedListener : public ISystemBarChangedListener { +public: + virtual void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override + { + } +}; + +class VisibilityChangedListener : public IVisibilityChangedListener { +public: + virtual void OnWindowVisibilityChanged(const std::vector>& windowVisibilityInfo) override + { + } +}; + +class WindowUpdateListener : public IWindowUpdateListener { +public: + virtual void OnWindowUpdate(const sptr& windowInfo, WindowUpdateType type) override + { + } +}; + +bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + std::string name = "WindowFuzzTest"; + sptr option = nullptr; + sptr window = Window::Create(name, option); + if (window == nullptr) { + return false; + } + window->Show(0); + Orientation orientation = static_cast(data[0]); + window->SetRequestedOrientation(static_cast(data[0])); + if (window->GetRequestedOrientation() != orientation) { + return false; + } + window->Hide(0); + window->Destroy(); + return true; +} +} // namespace.OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DoSomethingInterestingWithMyAPI(data, size); + return 0; +} + diff --git a/wm/test/fuzztest/window_fuzzer/window_fuzzer.h b/wm/test/fuzztest/window_fuzzer/window_fuzzer.h new file mode 100644 index 00000000..d559d07f --- /dev/null +++ b/wm/test/fuzztest/window_fuzzer/window_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_WINDOW_FUZZER_H +#define TEST_FUZZTEST_WINDOW_FUZZER_H + +#define FUZZ_PROJECT_NAME "window_fuzzer" + +#endif \ No newline at end of file diff --git a/wm/test/fuzztest/windowmanager_fuzzer/BUILD.gn b/wm/test/fuzztest/windowmanager_fuzzer/BUILD.gn new file mode 100644 index 00000000..c043ebbe --- /dev/null +++ b/wm/test/fuzztest/windowmanager_fuzzer/BUILD.gn @@ -0,0 +1,53 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/wm" + +##############################fuzztest########################################## +ohos_fuzztest("WindowManagerFuzzTest") { + fuzz_config_file = + "//foundation/windowmanager/wm/test/fuzztest/windowmanager_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/wmserver/include", + "//foundation/windowmanager/interfaces/innerkits/wm", + "//utils/native/base/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "windowmanager_fuzzer.cpp" ] + public_deps = [ + "//foundation/windowmanager/wm:libwm", + "//foundation/windowmanager/wmserver:libwms", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":WindowManagerFuzzTest", + ] +} +############################################################################### diff --git a/wm/test/fuzztest/windowmanager_fuzzer/corpus/init b/wm/test/fuzztest/windowmanager_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/wm/test/fuzztest/windowmanager_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/wm/test/fuzztest/windowmanager_fuzzer/project.xml b/wm/test/fuzztest/windowmanager_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/wm/test/fuzztest/windowmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.cpp b/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.cpp new file mode 100644 index 00000000..9caebad0 --- /dev/null +++ b/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.cpp @@ -0,0 +1,122 @@ +/* + * 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 "parcel.h" +#include "window_manager.h" + +using namespace OHOS::Rosen; + +namespace OHOS { +namespace { + constexpr size_t DATA_MIN_SIZE = 2; +} +class FocusChangedListener : public IFocusChangedListener { +public: + virtual void OnFocused(const sptr& focusChangeInfo) override + { + } + + virtual void OnUnfocused(const sptr& focusChangeInfo) override + { + } +}; + +class SystemBarChangedListener : public ISystemBarChangedListener { +public: + virtual void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override + { + } +}; + +class VisibilityChangedListener : public IVisibilityChangedListener { +public: + virtual void OnWindowVisibilityChanged(const std::vector>& windowVisibilityInfo) override + { + } +}; + +class WindowUpdateListener : public IWindowUpdateListener { +public: + virtual void OnWindowUpdate(const sptr& windowInfo, WindowUpdateType type) override + { + } +}; + +bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + WindowManager& windowManager = WindowManager::GetInstance(); + Parcel accessibilityWindowInfoParcel; + if (accessibilityWindowInfoParcel.WriteBuffer(data, size)) { + sptr windowInfo = + AccessibilityWindowInfo::Unmarshalling(accessibilityWindowInfoParcel); + windowManager.GetAccessibilityWindowInfo(windowInfo); + } + + Parcel focusChangeInfoParcel; + if (focusChangeInfoParcel.WriteBuffer(data, size)) { + FocusChangeInfo::Unmarshalling(focusChangeInfoParcel); + } + Parcel parcel; + sptr focusChangeInfo = new FocusChangeInfo(); + focusChangeInfo->Marshalling(parcel); + + Parcel windowVisibilityInfoParcel; + if (windowVisibilityInfoParcel.WriteBuffer(data, size)) { + WindowVisibilityInfo::Unmarshalling(windowVisibilityInfoParcel); + } + sptr windowVisibilityInfo = new WindowVisibilityInfo(); + windowVisibilityInfo->Marshalling(parcel); + + Parcel windowInfoParcel; + if (windowInfoParcel.WriteBuffer(data, size)) { + WindowInfo::Unmarshalling(windowInfoParcel); + } + sptr windowInfo = new WindowInfo(); + windowInfo->Marshalling(parcel); + + sptr accessibilityWindowInfo = new AccessibilityWindowInfo(); + accessibilityWindowInfo->currentWindowInfo_ = windowInfo; + accessibilityWindowInfo->windowList_.emplace_back(windowInfo); + accessibilityWindowInfo->Marshalling(parcel); + + windowManager.MinimizeAllAppWindows(static_cast(data[0])); + sptr focusChangedListener = new FocusChangedListener(); + windowManager.RegisterFocusChangedListener(focusChangedListener); + sptr systemBarChangedListener = new SystemBarChangedListener(); + windowManager.RegisterSystemBarChangedListener(systemBarChangedListener); + sptr visibilityChangedListener = new VisibilityChangedListener(); + windowManager.RegisterVisibilityChangedListener(visibilityChangedListener); + sptr windowUpdateListener = new WindowUpdateListener(); + windowManager.RegisterWindowUpdateListener(windowUpdateListener); + windowManager.SetWindowLayoutMode(static_cast(data[1]), static_cast(data[0])); + windowManager.UnregisterFocusChangedListener(focusChangedListener); + windowManager.UnregisterSystemBarChangedListener(systemBarChangedListener); + windowManager.UnregisterVisibilityChangedListener(visibilityChangedListener); + windowManager.UnregisterWindowUpdateListener(windowUpdateListener); + return true; +} +} // namespace.OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DoSomethingInterestingWithMyAPI(data, size); + return 0; +} + diff --git a/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.h b/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.h new file mode 100644 index 00000000..ae6240c6 --- /dev/null +++ b/wm/test/fuzztest/windowmanager_fuzzer/windowmanager_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_WINDOW_MANAGER_FUZZER_H +#define TEST_FUZZTEST_WINDOW_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "windowmanager_fuzzer" + +#endif \ No newline at end of file diff --git a/wm/test/fuzztest/windowscene_fuzzer/BUILD.gn b/wm/test/fuzztest/windowscene_fuzzer/BUILD.gn new file mode 100644 index 00000000..ad40c1e0 --- /dev/null +++ b/wm/test/fuzztest/windowscene_fuzzer/BUILD.gn @@ -0,0 +1,53 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "window_manager/wm" + +##############################fuzztest########################################## +ohos_fuzztest("WindowSceneFuzzTest") { + fuzz_config_file = + "//foundation/windowmanager/wm/test/fuzztest/windowscene_fuzzer" + module_out_path = module_output_path + include_dirs = [ + "//foundation/windowmanager/wmserver/include", + "//foundation/windowmanager/interfaces/innerkits/wm", + "//utils/native/base/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "windowscene_fuzzer.cpp" ] + public_deps = [ + "//foundation/windowmanager/wm:libwm", + "//foundation/windowmanager/wmserver:libwms", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":WindowSceneFuzzTest", + ] +} +############################################################################### diff --git a/wm/test/fuzztest/windowscene_fuzzer/corpus/init b/wm/test/fuzztest/windowscene_fuzzer/corpus/init new file mode 100644 index 00000000..bc977bd9 --- /dev/null +++ b/wm/test/fuzztest/windowscene_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/wm/test/fuzztest/windowscene_fuzzer/project.xml b/wm/test/fuzztest/windowscene_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/wm/test/fuzztest/windowscene_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.cpp b/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.cpp new file mode 100644 index 00000000..1db362fc --- /dev/null +++ b/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.cpp @@ -0,0 +1,88 @@ +/* + * 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 "parcel.h" +#include "window.h" +#include "window_manager.h" + +using namespace OHOS::Rosen; + +namespace OHOS { +namespace { + constexpr size_t DATA_MIN_SIZE = 2; +} +class FocusChangedListener : public IFocusChangedListener { +public: + virtual void OnFocused(const sptr& focusChangeInfo) override + { + } + + virtual void OnUnfocused(const sptr& focusChangeInfo) override + { + } +}; + +class SystemBarChangedListener : public ISystemBarChangedListener { +public: + virtual void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override + { + } +}; + +class VisibilityChangedListener : public IVisibilityChangedListener { +public: + virtual void OnWindowVisibilityChanged(const std::vector>& windowVisibilityInfo) override + { + } +}; + +class WindowUpdateListener : public IWindowUpdateListener { +public: + virtual void OnWindowUpdate(const sptr& windowInfo, WindowUpdateType type) override + { + } +}; + +bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + std::string name = "WindowFuzzTest"; + sptr option = nullptr; + sptr window = Window::Create(name, option); + if (window == nullptr) { + return false; + } + window->Show(0); + Orientation orientation = static_cast(data[0]); + window->SetRequestedOrientation(static_cast(data[0])); + if (window->GetRequestedOrientation() != orientation) { + return false; + } + window->Hide(0); + window->Destroy(); + return true; +} +} // namespace.OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DoSomethingInterestingWithMyAPI(data, size); + return 0; +} + diff --git a/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.h b/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.h new file mode 100644 index 00000000..1c790153 --- /dev/null +++ b/wm/test/fuzztest/windowscene_fuzzer/windowscene_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_FUZZTEST_WINDOW_SCENE_FUZZER_H +#define TEST_FUZZTEST_WINDOW_SCENE_FUZZER_H + +#define FUZZ_PROJECT_NAME "windowscene_fuzzer" + +#endif \ No newline at end of file