diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index be7f24d4af399fec940d0d4bd7c6721ec1bf1b42..6753e53ff55f2e6d9fd6de3c8699021116df9d17 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 0000000000000000000000000000000000000000..1f26e773854b11c7a7ed736e691f365c389c9981 --- /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 0000000000000000000000000000000000000000..d107c934b9d772a73cac3b13524c020a52988195 --- /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 0000000000000000000000000000000000000000..45ed91a01cc093ef1a44b08b90131dd203efa276 --- /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 0000000000000000000000000000000000000000..1b910144fb1ff33a40a44b1d2a491b1ab05b598b --- /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 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /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 0000000000000000000000000000000000000000..d57aa11bf32ce64cbe0f8771de88412bf222b691 --- /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 0000000000000000000000000000000000000000..97f84526b71b94de824bf581af6ef9acea843799 --- /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 0000000000000000000000000000000000000000..6d5c5bb6d01483f4855f2af441602b6e684cb0a8 --- /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 0000000000000000000000000000000000000000..1b910144fb1ff33a40a44b1d2a491b1ab05b598b --- /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 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /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 389adbab80dadc4693752ff1796c9a5f2bb84616..b727a550f4a23e6383fc74f1dddd46d5cd987267 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 e79265e3785f8beb6fe2a64d83843c15d2a8eb7b..f3ae0d0ef584e29b08f43c2f15d6a3a603ead42f 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 cdcd93d233d273b6501031432c963f4692eb891b..05becbb997d0da292a7fddaaf45512794e211207 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 3eb7e4f027680184105474496c77cc4453b20e63..720d2def3394a7bb4a8b8bb6ca131724658a05bf 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; } }