From 58e6aecd8ce5a4774a8ee8f11be40e45c3996949 Mon Sep 17 00:00:00 2001 From: liuyanzhi Date: Tue, 11 Oct 2022 08:49:25 +0800 Subject: [PATCH] add fuzz test Signed-off-by: liuyanzhi Change-Id: I54cf641f0fe9649c0372e89772ce76caca6aaa9f --- test/fuzztest/BUILD.gn | 2 + .../getbundleimportance_fuzzer/BUILD.gn | 55 ++++++++++++++++++ .../getbundleimportance_fuzzer/corpus/init | 13 +++++ .../getbundleimportance_fuzzer.cpp | 55 ++++++++++++++++++ .../getbundleimportance_fuzzer.h | 23 ++++++++ .../getbundleimportance_fuzzer/project.xml | 25 +++++++++ .../BUILD.gn | 56 +++++++++++++++++++ .../corpus/init | 13 +++++ .../project.xml | 25 +++++++++ .../removenotificationsbybundle_fuzzer.cpp | 47 ++++++++++++++++ .../removenotificationsbybundle_fuzzer.h | 23 ++++++++ .../setnotificationbadgenum_fuzzer.cpp | 10 ++++ 12 files changed, 347 insertions(+) create mode 100644 test/fuzztest/getbundleimportance_fuzzer/BUILD.gn create mode 100644 test/fuzztest/getbundleimportance_fuzzer/corpus/init create mode 100644 test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_fuzzer.cpp create mode 100644 test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_fuzzer.h create mode 100644 test/fuzztest/getbundleimportance_fuzzer/project.xml create mode 100644 test/fuzztest/removenotificationsbybundle_fuzzer/BUILD.gn create mode 100644 test/fuzztest/removenotificationsbybundle_fuzzer/corpus/init create mode 100644 test/fuzztest/removenotificationsbybundle_fuzzer/project.xml create mode 100644 test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.cpp create mode 100644 test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.h diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 6753e53ff..39303ad3a 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -20,10 +20,12 @@ group("fuzztest") { "addslotbytype_fuzzer:AddSlotByTypeFuzzTest", "cancelasbundle_fuzzer:CancelAsBundleFuzzTest", "cancelnotification_fuzzer:CancelNotificationFuzzTest", + "getbundleimportance_fuzzer:GetBundleImportanceFuzzTest", "getnotificationslot_fuzzer:GetNotificationSlotFuzzTest", "getnotificationslotnumasbundle_fuzzer:GetNotificationSlotNumAsBundleFuzzTest", "publishnotification_fuzzer:PublishNotificationFuzzTest", "removenotification_fuzzer:RemoveNotificationFuzzTest", + "removenotificationsbybundle_fuzzer:RemoveNotificationsByBundleFuzzTest", "removenotificationslot_fuzzer:RemoveNotificationSlotFuzzTest", "setnotificationbadgenum_fuzzer:SetNotificationBadgeNumFuzzTest", ] diff --git a/test/fuzztest/getbundleimportance_fuzzer/BUILD.gn b/test/fuzztest/getbundleimportance_fuzzer/BUILD.gn new file mode 100644 index 000000000..2dcda5735 --- /dev/null +++ b/test/fuzztest/getbundleimportance_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("GetBundleImportanceFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${component_path}/test/fuzztest/getbundleimportance_fuzzer" + + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "getbundleimportance_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", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_framework:image_native", + "relational_store:native_rdb", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":GetBundleImportanceFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/getbundleimportance_fuzzer/corpus/init b/test/fuzztest/getbundleimportance_fuzzer/corpus/init new file mode 100644 index 000000000..1b910144f --- /dev/null +++ b/test/fuzztest/getbundleimportance_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/getbundleimportance_fuzzer/getbundleimportance_fuzzer.cpp b/test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_fuzzer.cpp new file mode 100644 index 000000000..b245c280b --- /dev/null +++ b/test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_fuzzer.cpp @@ -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. + */ + +#include "getbundleimportance_fuzzer.h" + +#include "notification_helper.h" +namespace OHOS { + namespace { + constexpr uint8_t NOTIFICATION_LEVEL_NUM = 6; + } + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + // test CanPublishNotificationAsBundle function + std::string representativeBundle(data); + bool canPublish = true; + Notification::NotificationHelper::CanPublishNotificationAsBundle(representativeBundle, canPublish); + // test SetNotificationBadgeNum function and no parameter + Notification::NotificationHelper::SetNotificationBadgeNum(); + // test IsAllowedNotify function + bool allowed = true; + Notification::NotificationHelper::IsAllowedNotify(allowed); + // test IsAllowedNotifySelf function + Notification::NotificationHelper::IsAllowedNotifySelf(allowed); + // test GetBundleImportance function + uint8_t levels = *data % NOTIFICATION_LEVEL_NUM; + Notification::NotificationSlot::NotificationLevel level = + Notification::NotificationSlot::NotificationLevel(levels); + return Notification::NotificationHelper::GetBundleImportance(level) == 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/getbundleimportance_fuzzer/getbundleimportance_fuzzer.h b/test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_fuzzer.h new file mode 100644 index 000000000..d5476841f --- /dev/null +++ b/test/fuzztest/getbundleimportance_fuzzer/getbundleimportance_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_GETBUNDLEIMPORTANCE_FUZZER_GETBUNDLEIMPORTANCE_FUZZER_H +#define TEST_FUZZTEST_GETBUNDLEIMPORTANCE_FUZZER_GETBUNDLEIMPORTANCE_FUZZER_H + +#include "fuzz_common_base.h" + +#define FUZZ_PROJECT_NAME "getbundleimportance_fuzzer" + +#endif // TEST_FUZZTEST_GETBUNDLEIMPORTANCE_FUZZER_GETBUNDLEIMPORTANCE_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/getbundleimportance_fuzzer/project.xml b/test/fuzztest/getbundleimportance_fuzzer/project.xml new file mode 100644 index 000000000..6e8ad2cfd --- /dev/null +++ b/test/fuzztest/getbundleimportance_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/removenotificationsbybundle_fuzzer/BUILD.gn b/test/fuzztest/removenotificationsbybundle_fuzzer/BUILD.gn new file mode 100644 index 000000000..1ae72f56c --- /dev/null +++ b/test/fuzztest/removenotificationsbybundle_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("RemoveNotificationsByBundleFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${component_path}/test/fuzztest/removenotificationsbybundle_fuzzer" + + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "removenotificationsbybundle_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 += [ ":RemoveNotificationsByBundleFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/removenotificationsbybundle_fuzzer/corpus/init b/test/fuzztest/removenotificationsbybundle_fuzzer/corpus/init new file mode 100644 index 000000000..1b910144f --- /dev/null +++ b/test/fuzztest/removenotificationsbybundle_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/removenotificationsbybundle_fuzzer/project.xml b/test/fuzztest/removenotificationsbybundle_fuzzer/project.xml new file mode 100644 index 000000000..6e8ad2cfd --- /dev/null +++ b/test/fuzztest/removenotificationsbybundle_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.cpp b/test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.cpp new file mode 100644 index 000000000..31fe973f1 --- /dev/null +++ b/test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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 "removenotificationsbybundle_fuzzer.h" + +#include "notification_helper.h" + +namespace OHOS { + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + // test RemoveNotificationsByBundle function + std::string stringData(data); + int32_t usingData = static_cast(GetU32Data(data)); + Notification::NotificationBundleOption bundleOption; + bundleOption.SetBundleName(stringData); + bundleOption.SetUid(usingData); + Notification::NotificationHelper::RemoveNotificationsByBundle(bundleOption); + // test RemoveAllNotifications function + return Notification::NotificationHelper::RemoveAllNotifications(bundleOption); + } +} + +/* 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/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.h b/test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_fuzzer.h new file mode 100644 index 000000000..3d8af2765 --- /dev/null +++ b/test/fuzztest/removenotificationsbybundle_fuzzer/removenotificationsbybundle_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_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_H +#define TEST_FUZZTEST_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_H + +#include "fuzz_common_base.h" + +#define FUZZ_PROJECT_NAME "removenotificationsbybundle_fuzzer" + +#endif // TEST_FUZZTEST_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_REMOVENOTIFICATIONSBYBUNDLE_FUZZER_H diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp index 0c9400963..2ba0e8a3c 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp @@ -19,6 +19,16 @@ namespace OHOS { bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { + // test RequestEnableNotification function + std::string deviceId(data); + Notification::NotificationHelper::RequestEnableNotification(deviceId); + // test AreNotificationsSuspended function + bool suspended = true; + Notification::NotificationHelper::AreNotificationsSuspended(suspended); + // test HasNotificationPolicyAccessPermission function + bool hasPermission = true; + Notification::NotificationHelper::HasNotificationPolicyAccessPermission(hasPermission); + // test SetNotificationBadgeNum function return Notification::NotificationHelper::SetNotificationBadgeNum(GetU32Data(data)) == ERR_OK; } } -- Gitee