diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 485b0a453f85bff167939d9e11b6d449f4ced2a9..fe3200325902e967e047390da07dc03684500c9f 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -20,9 +20,11 @@ group("fuzztest") { "addnotificationslotgroups_fuzzer:AddNotificationSlotGroupsFuzzTest", "addnotificationslots_fuzzer:AddNotificationSlotsFuzzTest", "addslotbytype_fuzzer:AddSlotByTypeFuzzTest", + "cancelnotification_fuzzer:CancelNotificationFuzzTest", "getnotificationslot_fuzzer:GetNotificationSlotFuzzTest", "getnotificationslotgroup_fuzzer:GetNotificationSlotGroupFuzzTest", "getnotificationslotnumasbundle_fuzzer:GetNotificationSlotNumAsBundleFuzzTest", + "publishnotification_fuzzer:PublishNotificationFuzzTest", "removenotification_fuzzer:RemoveNotificationFuzzTest", "removenotificationslot_fuzzer:RemoveNotificationSlotFuzzTest", "removenotificationslotgroup_fuzzer:RemoveNotificationSlotGroupFuzzTest", diff --git a/test/fuzztest/cancelnotification_fuzzer/BUILD.gn b/test/fuzztest/cancelnotification_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e30e01020dce5a1c2b00fc752bf91d83557fafc3 --- /dev/null +++ b/test/fuzztest/cancelnotification_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/ans_standard/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "ans_standard/services" + +##############################fuzztest########################################## +ohos_fuzztest("CancelNotificationFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//base/notification/ans_standard/test/fuzztest/cancelnotification_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "cancelnotification_fuzzer.cpp" ] + + deps = [ + "${frameworks_path}/ans/native:ans_innerkits", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":CancelNotificationFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d76b9da5cdfab63f92b36d1be67f780954b5b9 --- /dev/null +++ b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp @@ -0,0 +1,35 @@ +/* + * 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 "cancelnotification_fuzzer.h" + +#include "notification_helper.h" + +namespace OHOS { + bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + { + std::string label = reinterpret_cast(data); + int32_t notificationId = static_cast(U32_AT(data)); + return Notification::NotificationHelper::CancelNotification(label, notificationId) == ERR_OK; + } +} + +/* 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/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..51bb27c9da63d87f29f127d91528758c1682c78b --- /dev/null +++ b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h @@ -0,0 +1,28 @@ +/* + * 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_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H +#define TEST_FUZZTEST_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H + +#define FUZZ_PROJECT_NAME "cancelnotification_fuzzer" + +#include + +uint32_t U32_AT(const uint8_t *ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} +#endif // TEST_FUZZTEST_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H diff --git a/test/fuzztest/cancelnotification_fuzzer/corpus/init b/test/fuzztest/cancelnotification_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..1b910144fb1ff33a40a44b1d2a491b1ab05b598b --- /dev/null +++ b/test/fuzztest/cancelnotification_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/cancelnotification_fuzzer/project.xml b/test/fuzztest/cancelnotification_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/cancelnotification_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/publishnotification_fuzzer/BUILD.gn b/test/fuzztest/publishnotification_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d99bd90e78e57423505bb1d6059a5830aee02961 --- /dev/null +++ b/test/fuzztest/publishnotification_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. + +#####################hydra-fuzz################### +import("//base/notification/ans_standard/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "ans_standard/services" + +##############################fuzztest########################################## +ohos_fuzztest("PublishNotificationFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/notification/ans_standard/test/fuzztest/publishnotification_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "publishnotification_fuzzer.cpp" ] + + deps = [ + "${frameworks_path}/ans/native:ans_innerkits", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":PublishNotificationFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/publishnotification_fuzzer/corpus/init b/test/fuzztest/publishnotification_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..1b910144fb1ff33a40a44b1d2a491b1ab05b598b --- /dev/null +++ b/test/fuzztest/publishnotification_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/publishnotification_fuzzer/project.xml b/test/fuzztest/publishnotification_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/publishnotification_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e77c99167020793edcbd74cdd007cb45a55ee853 --- /dev/null +++ b/test/fuzztest/publishnotification_fuzzer/publishnotification_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 "publishnotification_fuzzer.h" + +#include "notification_helper.h" + +namespace OHOS { + namespace { + constexpr uint8_t ENABLE = 2; + constexpr uint8_t SLOT_TYPE_NUM = 5; + constexpr uint8_t FLAG_STATUS = 3; + } + bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + { + std::string stringData = reinterpret_cast(data); + Notification::NotificationRequest request; + request.SetAlertOneTime(*data % ENABLE); + + int32_t style = static_cast(U32_AT(data)); + Notification::NotificationRequest::BadgeStyle badgeStyle = + Notification::NotificationRequest::BadgeStyle(style); + request.SetBadgeIconStyle(badgeStyle); + request.SetBadgeNumber(style); + request.SetClassification(stringData); + + uint32_t color = U32_AT(data); + request.SetColor(color); + request.SetColorEnabled(*data % ENABLE); + + std::shared_ptr contentType = + std::make_shared(); + contentType->SetText(stringData); + contentType->SetTitle(stringData); + contentType->SetAdditionalText(stringData); + std::shared_ptr content = + std::make_shared(contentType); + request.SetContent(content); + request.SetCountdownTimer(*data % ENABLE); + request.SetCreatorBundleName(stringData); + request.SetDeliveryTime(style); + + std::shared_ptr notificationFlages = + std::make_shared(); + int32_t soundEnabled = static_cast(*data % FLAG_STATUS); + Notification::NotificationConstant::FlagStatus sound = + Notification::NotificationConstant::FlagStatus(soundEnabled); + notificationFlages->SetSoundEnabled(sound); + notificationFlages->SetVibrationEnabled(sound); + request.SetFlags(notificationFlages); + + Notification::NotificationRequest::GroupAlertType groupAlertType = + Notification::NotificationRequest::GroupAlertType(color); + request.SetGroupAlertType(groupAlertType); + + request.SetGroupName(stringData); + request.SetGroupOverview(*data % ENABLE); + request.SetLabel(stringData); + request.SetNotificationId(style); + request.SetOwnerBundleName(stringData); + + uint8_t types = *data % SLOT_TYPE_NUM; + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(types); + request.SetSlotType(slotType); + + return Notification::NotificationHelper::PublishNotification(stringData, request) == ERR_OK; + } +} + +/* 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/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..cf6356f20f33aa7e4d950aa3049aeb2cc616c0f9 --- /dev/null +++ b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h @@ -0,0 +1,29 @@ +/* + * 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_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H +#define TEST_FUZZTEST_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H + +#define FUZZ_PROJECT_NAME "publishnotification_fuzzer" + +#include + +uint32_t U32_AT(const uint8_t *ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} + +#endif // TEST_FUZZTEST_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H