From 24fe07575669dede59751a6d5d629d27978d200e Mon Sep 17 00:00:00 2001 From: liuyanzhi Date: Sun, 9 Oct 2022 21:24:02 +0800 Subject: [PATCH] add fuzz test Signed-off-by: liuyanzhi Change-Id: I0888f5c5044f5f9c171e6770649696cf0c5e09f0 --- test/fuzztest/BUILD.gn | 2 + .../addnotificationslot_fuzzer/BUILD.gn | 56 ++++++++++++++++ .../addnotificationslot_fuzzer.cpp | 66 +++++++++++++++++++ .../addnotificationslot_fuzzer.h | 23 +++++++ .../addnotificationslot_fuzzer/corpus/init | 13 ++++ .../addnotificationslot_fuzzer/project.xml | 25 +++++++ test/fuzztest/cancelasbundle_fuzzer/BUILD.gn | 55 ++++++++++++++++ .../cancelasbundle_fuzzer.cpp | 42 ++++++++++++ .../cancelasbundle_fuzzer.h | 25 +++++++ .../cancelasbundle_fuzzer/corpus/init | 13 ++++ .../cancelasbundle_fuzzer/project.xml | 25 +++++++ .../cancelnotification_fuzzer.cpp | 4 ++ .../getnotificationslot_fuzzer.cpp | 10 ++- .../publishnotification_fuzzer.cpp | 3 + .../removenotificationslot_fuzzer.cpp | 10 ++- 15 files changed, 367 insertions(+), 5 deletions(-) create mode 100644 test/fuzztest/addnotificationslot_fuzzer/BUILD.gn create mode 100644 test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.cpp create mode 100644 test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.h create mode 100644 test/fuzztest/addnotificationslot_fuzzer/corpus/init create mode 100644 test/fuzztest/addnotificationslot_fuzzer/project.xml create mode 100644 test/fuzztest/cancelasbundle_fuzzer/BUILD.gn create mode 100644 test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.cpp create mode 100644 test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.h create mode 100644 test/fuzztest/cancelasbundle_fuzzer/corpus/init create mode 100644 test/fuzztest/cancelasbundle_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index be7f24d4a..6753e53ff 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -15,8 +15,10 @@ group("fuzztest") { testonly = true deps = [ + "addnotificationslot_fuzzer:AddNotificationSlotFuzzTest", "addnotificationslots_fuzzer:AddNotificationSlotsFuzzTest", "addslotbytype_fuzzer:AddSlotByTypeFuzzTest", + "cancelasbundle_fuzzer:CancelAsBundleFuzzTest", "cancelnotification_fuzzer:CancelNotificationFuzzTest", "getnotificationslot_fuzzer:GetNotificationSlotFuzzTest", "getnotificationslotnumasbundle_fuzzer:GetNotificationSlotNumAsBundleFuzzTest", diff --git a/test/fuzztest/addnotificationslot_fuzzer/BUILD.gn b/test/fuzztest/addnotificationslot_fuzzer/BUILD.gn new file mode 100644 index 000000000..1f26e7738 --- /dev/null +++ b/test/fuzztest/addnotificationslot_fuzzer/BUILD.gn @@ -0,0 +1,56 @@ +# 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. + +#####################hydra-fuzz################### +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "${component_name}/fuzztest" + +##############################fuzztest########################################## +ohos_fuzztest("AddNotificationSlotFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${component_path}/test/fuzztest/addnotificationslot_fuzzer" + + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "addnotificationslot_fuzzer.cpp" ] + + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_framework:image_native", + "relational_store:native_rdb", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":AddNotificationSlotFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.cpp b/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.cpp new file mode 100644 index 000000000..d107c934b --- /dev/null +++ b/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.cpp @@ -0,0 +1,66 @@ +/* + * 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 "addnotificationslot_fuzzer.h" + +#include "notification_helper.h" + +namespace OHOS { + namespace { + constexpr uint8_t ENABLE = 2; + constexpr uint8_t SLOT_LEVEL_NUM = 6; + constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4; + constexpr uint8_t SLOT_TYPE_NUM = 5; + } + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + std::string stringData(data); + + Notification::NotificationSlot slot; + slot.SetDescription(stringData); + slot.SetEnableLight(*data % ENABLE); + slot.SetEnableVibration(*data % ENABLE); + slot.SetLedLightColor(GetU32Data(data)); + + uint8_t level = *data % SLOT_LEVEL_NUM; + Notification::NotificationSlot::NotificationLevel notificatoinLevel = + Notification::NotificationSlot::NotificationLevel(level); + slot.SetLevel(notificatoinLevel); + + uint8_t visibleness = *data % SLOT_VISIBLENESS_TYPE_NUM; + Notification::NotificationConstant::VisiblenessType visiblenessType = + Notification::NotificationConstant::VisiblenessType(visibleness); + slot.SetLockscreenVisibleness(visiblenessType); + + uint8_t type = *data % SLOT_TYPE_NUM; + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); + slot.SetType(slotType); + + return Notification::NotificationHelper::AddNotificationSlot(slot) == ERR_OK; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + char *ch = ParseData(data, size); + if (ch != nullptr) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } + return 0; +} diff --git a/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.h b/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.h new file mode 100644 index 000000000..45ed91a01 --- /dev/null +++ b/test/fuzztest/addnotificationslot_fuzzer/addnotificationslot_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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_ADDSNOTIFICATIONSLOT_FUZZER_ADDSNOTIFICATIONSLOT_FUZZER_H +#define TEST_FUZZTEST_ADDSNOTIFICATIONSLOT_FUZZER_ADDSNOTIFICATIONSLOT_FUZZER_H + +#include "fuzz_common_base.h" + +#define FUZZ_PROJECT_NAME "addnotificationslot_fuzzer" + +#endif // TEST_FUZZTEST_ADDSNOTIFICATIONSLOT_FUZZER_ADDSNOTIFICATIONSLOT_FUZZER_H diff --git a/test/fuzztest/addnotificationslot_fuzzer/corpus/init b/test/fuzztest/addnotificationslot_fuzzer/corpus/init new file mode 100644 index 000000000..1b910144f --- /dev/null +++ b/test/fuzztest/addnotificationslot_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# 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/test/fuzztest/addnotificationslot_fuzzer/project.xml b/test/fuzztest/addnotificationslot_fuzzer/project.xml new file mode 100644 index 000000000..6e8ad2cfd --- /dev/null +++ b/test/fuzztest/addnotificationslot_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cancelasbundle_fuzzer/BUILD.gn b/test/fuzztest/cancelasbundle_fuzzer/BUILD.gn new file mode 100644 index 000000000..d57aa11bf --- /dev/null +++ b/test/fuzztest/cancelasbundle_fuzzer/BUILD.gn @@ -0,0 +1,55 @@ +# 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. + +#####################hydra-fuzz################### +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "${component_name}/fuzztest" + +##############################fuzztest########################################## +ohos_fuzztest("CancelAsBundleFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${component_path}/test/fuzztest/cancelasbundle_fuzzer" + + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "cancelasbundle_fuzzer.cpp" ] + + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_framework:image_native", + "relational_store:native_rdb", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":CancelAsBundleFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.cpp b/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.cpp new file mode 100644 index 000000000..97f84526b --- /dev/null +++ b/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.cpp @@ -0,0 +1,42 @@ +/* + * 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 "cancelasbundle_fuzzer.h" + +#include "notification_helper.h" + +namespace OHOS { + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + std::string representativeBundle(data); + int32_t notificationId = static_cast(GetU32Data(data)); + int32_t userId = static_cast(GetU32Data(data)); + return Notification::NotificationHelper::CancelAsBundle( + notificationId, representativeBundle, userId) == ERR_OK; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } + return 0; +} diff --git a/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.h b/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.h new file mode 100644 index 000000000..6d5c5bb6d --- /dev/null +++ b/test/fuzztest/cancelasbundle_fuzzer/cancelasbundle_fuzzer.h @@ -0,0 +1,25 @@ +/* + * 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_CANCELASBUNDLE_FUZZER_CANCELASBUNDLE_FUZZER_H +#define TEST_FUZZTEST_CANCELASBUNDLE_FUZZER_CANCELASBUNDLE_FUZZER_H + +#include "fuzz_common_base.h" + +#define FUZZ_PROJECT_NAME "cancelasbundle_fuzzer" + +#include + +#endif // TEST_FUZZTEST_CANCELASBUNDLE_FUZZER_CANCELASBUNDLE_FUZZER_H diff --git a/test/fuzztest/cancelasbundle_fuzzer/corpus/init b/test/fuzztest/cancelasbundle_fuzzer/corpus/init new file mode 100644 index 000000000..1b910144f --- /dev/null +++ b/test/fuzztest/cancelasbundle_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# 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/test/fuzztest/cancelasbundle_fuzzer/project.xml b/test/fuzztest/cancelasbundle_fuzzer/project.xml new file mode 100644 index 000000000..6e8ad2cfd --- /dev/null +++ b/test/fuzztest/cancelasbundle_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp index 389adbab8..b727a550f 100644 --- a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp +++ b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp @@ -22,6 +22,10 @@ namespace OHOS { { std::string label(data); int32_t notificationId = static_cast(GetU32Data(data)); + // test CancelAllNotifications function + Notification::NotificationHelper::CancelAllNotifications(); + // test CancelNotification function + Notification::NotificationHelper::CancelNotification(notificationId); return Notification::NotificationHelper::CancelNotification(label, notificationId) == ERR_OK; } } diff --git a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp index e79265e37..f3ae0d0ef 100644 --- a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp +++ b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp @@ -17,14 +17,20 @@ #include "notification_helper.h" -constexpr uint8_t SLOT_TYPE_NUM = 5; - namespace OHOS { + namespace { + constexpr uint8_t SLOT_TYPE_NUM = 5; + } bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { uint8_t type = *data % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); sptr slot = nullptr; + // test GetNotificationSlots function + std::vector> slots; + slots.emplace_back(slot); + Notification::NotificationHelper::GetNotificationSlots(slots); + // test GetNotificationSlot function return Notification::NotificationHelper::GetNotificationSlot(slotType, slot) == ERR_OK; } } diff --git a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp index cdcd93d23..05becbb99 100644 --- a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp +++ b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp @@ -75,6 +75,9 @@ namespace OHOS { Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(types); request.SetSlotType(slotType); + Notification::NotificationHelper::PublishNotification(request, stringData); + + Notification::NotificationHelper::PublishNotification(request); return Notification::NotificationHelper::PublishNotification(stringData, request) == ERR_OK; } } diff --git a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp index 3eb7e4f02..720d2def3 100644 --- a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp +++ b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp @@ -17,14 +17,18 @@ #include "notification_helper.h" -constexpr uint8_t SLOT_TYPE_NUM = 5; - namespace OHOS { + namespace { + constexpr uint8_t SLOT_TYPE_NUM = 5; + } bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { uint8_t type = *data % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); - return Notification::NotificationHelper::RemoveNotificationSlot(slotType) == ERR_OK; + // test RemoveNotificationSlot function + Notification::NotificationHelper::RemoveNotificationSlot(slotType); + // test RemoveAllSlots function + return Notification::NotificationHelper::RemoveAllSlots() == ERR_OK; } } -- Gitee