From 0adfa5474299139b8146391372d817f61192727c Mon Sep 17 00:00:00 2001 From: liuyanzhi Date: Wed, 18 Jan 2023 01:19:57 +0000 Subject: [PATCH] add ut test Signed-off-by: liuyanzhi Change-Id: I53360af33b248509ce1476f1a3b0f2c5b39f2724 --- notification.gni | 1 + services/ans/test/unittest/BUILD.gn | 79 +++ ...anced_notification_service_branch_test.cpp | 482 ++++++++++++++++++ .../mock_access_token_helper.cpp | 48 ++ .../mock_notification.cpp | 51 ++ 5 files changed, 661 insertions(+) create mode 100755 services/ans/test/unittest/advanced_notification_service_branch_test/advanced_notification_service_branch_test.cpp create mode 100755 services/ans/test/unittest/advanced_notification_service_branch_test/mock_access_token_helper.cpp create mode 100755 services/ans/test/unittest/advanced_notification_service_branch_test/mock_notification.cpp diff --git a/notification.gni b/notification.gni index 23ca14d78..e1069c3f4 100644 --- a/notification.gni +++ b/notification.gni @@ -16,6 +16,7 @@ ability_runtime_inner_api_path = "${ability_runtime_path}/interfaces/inner_api" ability_runtime_services_path = "${ability_runtime_path}/services" ability_runtime_kits_path = "${ability_runtime_path}/frameworks/kits" ability_runtime_napi_path = "${ability_runtime_path}/frameworks/js/napi" +ace_engine_patch = "//foundation/arkui/ace_engine" component_name = "distributed_notification_service" component_path = "//base/notification/distributed_notification_service" services_path = "${component_path}/services" diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 855487248..67a17b560 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -478,6 +478,83 @@ ohos_unittest("notification_rdb_data_mgr_test") { part_name = "${component_name}" } +ohos_unittest("advanced_notification_service_branch_test") { + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/ans/include", + "${services_path}/ans/test/unittest/mock/include", + ] + + defines = [] + + sources = [ + "${services_path}/ans/src/advanced_notification_service.cpp", + "${services_path}/ans/src/advanced_notification_service_ability.cpp", + "${services_path}/ans/src/event_report.cpp", + "${services_path}/ans/src/notification_preferences.cpp", + "${services_path}/ans/src/notification_preferences_database.cpp", + "${services_path}/ans/src/notification_preferences_info.cpp", + "${services_path}/ans/src/notification_rdb_data_mgr.cpp", + "${services_path}/ans/src/notification_slot_filter.cpp", + "${services_path}/ans/src/notification_subscriber_manager.cpp", + "${services_path}/ans/src/permission_filter.cpp", + "${services_path}/ans/src/reminder_data_manager.cpp", + "${services_path}/ans/src/reminder_event_manager.cpp", + "${services_path}/ans/src/reminder_timer_info.cpp", + "${services_path}/ans/src/system_event_observer.cpp", + "advanced_notification_service_branch_test/advanced_notification_service_branch_test.cpp", + "advanced_notification_service_branch_test/mock_access_token_helper.cpp", + "advanced_notification_service_branch_test/mock_notification.cpp", + "mock/mock_bundle_manager_helper.cpp", + "mock/mock_ipc.cpp", + ] + + deps = [ + "${core_path}:ans_core", + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", + "${ace_engine_patch}/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/googletest:gtest_main", + ] + + if (distributed_notification_supported) { + defines += [ "DISTRIBUTED_NOTIFICATION_SUPPORTED" ] + deps += [ "${services_path}/distributed:libans_distributed" ] + include_dirs += [ "${services_path}/distributed/include" ] + } + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + "multimedia_player_framework:media_client", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + group("unittest") { testonly = true deps = [] @@ -495,4 +572,6 @@ group("unittest") { deps += [ ":notification_preferences_database_branch_test" ] deps += [ ":notification_rdb_data_mgr_test" ] + + deps += [ ":advanced_notification_service_branch_test" ] } diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test/advanced_notification_service_branch_test.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test/advanced_notification_service_branch_test.cpp new file mode 100755 index 000000000..72545311c --- /dev/null +++ b/services/ans/test/unittest/advanced_notification_service_branch_test/advanced_notification_service_branch_test.cpp @@ -0,0 +1,482 @@ +/* + * 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 +#include + +#define private public +#define protected public +#include "advanced_notification_service.h" +#undef private +#undef protected +#include "ans_inner_errors.h" +#include "ans_ut_constant.h" +#include "mock_ipc_skeleton.h" + +extern void MockGetUserId(bool mockRet); +extern void MockGetBundleName(bool mockRet); +extern void MockVerifyNativeToken(bool mockRet); +extern void MockVerifyCallerPermission(bool mockRet); + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class AdvancedNotificationServiceBranchTest : public testing::Test { +public: + static void SetUpTestCase() {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.number : AdvancedNotificationService_00100 + * @tc.name : AdvancedNotificationService_00100 + * @tc.desc : test ActiveNotificationDump function and record->notification == nullptr record->request == nullptr + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00100, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = 1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = nullptr; + record->request = nullptr; + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00200 + * @tc.name : AdvancedNotificationService_00200 + * @tc.desc : test ActiveNotificationDump function and userId != SUBSCRIBE_USER_INIT + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00200, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = 1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + advancedNotificationService.notificationList_.push_back(record); + MockGetUserId(false); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00300 + * @tc.name : AdvancedNotificationService_00300 + * @tc.desc : test ActiveNotificationDump function and bundle != record->notification->GetBundleName(). + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00300, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + advancedNotificationService.notificationList_.push_back(record); + MockGetUserId(false); + MockGetBundleName(false); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00400 + * @tc.name : AdvancedNotificationService_00400 + * @tc.desc : test ActiveNotificationDump function and record->deviceId is not empty. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00400, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + record->deviceId = ""; + advancedNotificationService.notificationList_.push_back(record); + MockGetUserId(false); + MockGetBundleName(false); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00500 + * @tc.name : AdvancedNotificationService_00500 + * @tc.desc : test ActiveNotificationDump function and record->request->GetOwnerUid() > 0. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00500, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + int32_t uid = 1; + record->request->SetOwnerUid(uid); + record->deviceId = ""; + advancedNotificationService.notificationList_.push_back(record); + MockGetUserId(false); + MockGetBundleName(false); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00600 + * @tc.name : AdvancedNotificationService_00600 + * @tc.desc : test ActiveNotificationDump function and record->request->GetOwnerUid() < 0. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00600, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + int32_t uid = -1; + record->request->SetOwnerUid(uid); + record->deviceId = ""; + advancedNotificationService.notificationList_.push_back(record); + MockGetUserId(false); + MockGetBundleName(false); + EXPECT_EQ(advancedNotificationService.ActiveNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00700 + * @tc.name : AdvancedNotificationService_00700 + * @tc.desc : test DistributedNotificationDump function and record->notification == nullptr. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00700, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = 1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = nullptr; + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00800 + * @tc.name : AdvancedNotificationService_00800 + * @tc.desc : test DistributedNotificationDump function and userId != SUBSCRIBE_USER_INIT. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00800, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = 1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + MockGetUserId(false); + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_00900 + * @tc.name : AdvancedNotificationService_00900 + * @tc.desc : test DistributedNotificationDump function and bundle is not empty. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_00900, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + MockGetUserId(false); + MockGetBundleName(false); + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_01000 + * @tc.name : AdvancedNotificationService_01000 + * @tc.desc : test DistributedNotificationDump function and record->deviceId is empty. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01000, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->deviceId = ""; + MockGetUserId(false); + MockGetBundleName(false); + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_01100 + * @tc.name : AdvancedNotificationService_01100 + * @tc.desc : test DistributedNotificationDump function and record->request->GetOwnerUid() > 0. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01100, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + int32_t uid = 1; + record->request->SetOwnerUid(uid); + record->deviceId = ""; + MockGetUserId(false); + MockGetBundleName(false); + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_01200 + * @tc.name : AdvancedNotificationService_01200 + * @tc.desc : test DistributedNotificationDump function and record->request->GetOwnerUid() < 0. + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01200, Function | SmallTest | Level1) +{ + std::string bundle = ""; + int32_t userId = -1; + std::vector dumpInfo; + AdvancedNotificationService advancedNotificationService; + std::shared_ptr record = std::make_shared(); + record->notification = new Notification(); + record->request = new NotificationRequest(); + int32_t uid = -1; + record->request->SetOwnerUid(uid); + record->deviceId = ""; + MockGetUserId(false); + MockGetBundleName(false); + advancedNotificationService.notificationList_.push_back(record); + EXPECT_EQ(advancedNotificationService.DistributedNotificationDump(bundle, userId, dumpInfo), ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationService_01300 + * @tc.name : AdvancedNotificationService_01300 + * @tc.desc : Test CheckPermission function and result is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01300, Function | SmallTest | Level1) +{ + std::string permission = ""; + AdvancedNotificationService advancedNotificationService; + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + EXPECT_EQ(advancedNotificationService.CheckPermission(permission), false); +} + +/** + * @tc.number : AdvancedNotificationService_01400 + * @tc.name : AdvancedNotificationService_01400 + * @tc.desc : Test PrepareNotificationRequest function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01400, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr req = new NotificationRequest(); + bool isAgentTrue = true; + req->SetIsAgentNotification(isAgentTrue); + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.PrepareNotificationRequest(req), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_01500 + * @tc.name : AdvancedNotificationService_01500 + * @tc.desc : Test CancelAsBundle function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01500, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + int32_t notificationId = 1; + std::string representativeBundle = ""; + int32_t userId = 2; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.CancelAsBundle(notificationId, representativeBundle, userId), + ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_01600 + * @tc.name : AdvancedNotificationService_01600 + * @tc.desc : Test AddSlots function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01600, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + std::vector> slots; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.AddSlots(slots), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_01700 + * @tc.name : AdvancedNotificationService_01700 + * @tc.desc : Test Delete function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01700, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + std::string key = ""; + int32_t removeReason = 1; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.Delete(key, removeReason), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_01800 + * @tc.name : AdvancedNotificationService_01800 + * @tc.desc : Test DeleteByBundle function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01800, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr bundleOption = nullptr; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.DeleteByBundle(bundleOption), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_01900 + * @tc.name : AdvancedNotificationService_01900 + * @tc.desc : Test DeleteAll function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_01900, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.DeleteAll(), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_02000 + * @tc.name : AdvancedNotificationService_02000 + * @tc.desc : Test GetSlotsByBundle function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_02000, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr bundleOption = nullptr; + std::vector> slots; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.GetSlotsByBundle(bundleOption, slots), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_02100 + * @tc.name : AdvancedNotificationService_02100 + * @tc.desc : Test UpdateSlots function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_02100, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr bundleOption = nullptr; + std::vector> slots; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.UpdateSlots(bundleOption, slots), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_02200 + * @tc.name : AdvancedNotificationService_02200 + * @tc.desc : Test SetShowBadgeEnabledForBundle function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_02200, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr bundleOption = nullptr; + bool enabled = true; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.SetShowBadgeEnabledForBundle(bundleOption, enabled), + ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AdvancedNotificationService_02300 + * @tc.name : AdvancedNotificationService_02300 + * @tc.desc : Test GetShowBadgeEnabledForBundle function and CheckPermission is false + */ +HWTEST_F(AdvancedNotificationServiceBranchTest, AdvancedNotificationService_02300, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); + + sptr bundleOption = nullptr; + bool enabled = true; + + MockVerifyNativeToken(false); + MockVerifyCallerPermission(false); + AdvancedNotificationService advancedNotificationService; + EXPECT_EQ(advancedNotificationService.GetShowBadgeEnabledForBundle(bundleOption, enabled), + ERR_ANS_PERMISSION_DENIED); +} +} // namespace Notification +} // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test/mock_access_token_helper.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test/mock_access_token_helper.cpp new file mode 100755 index 000000000..fb564ddcf --- /dev/null +++ b/services/ans/test/unittest/advanced_notification_service_branch_test/mock_access_token_helper.cpp @@ -0,0 +1,48 @@ +/* + * 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 "access_token_helper.h" + +namespace { + bool g_mockVerifyNativeTokenRet = true; + bool g_mockVerifyCallerPermissionRet = true; +} + +void MockVerifyNativeToken(bool mockRet) +{ + g_mockVerifyNativeTokenRet = mockRet; +} + +void MockVerifyCallerPermission(bool mockRet) +{ + g_mockVerifyCallerPermissionRet = mockRet; +} + +using namespace OHOS::Security::AccessToken; + +namespace OHOS { +namespace Notification { +bool AccessTokenHelper::VerifyNativeToken(const AccessTokenID &callerToken) +{ + return g_mockVerifyNativeTokenRet; +} + +bool AccessTokenHelper::VerifyCallerPermission( + const AccessTokenID &tokenCaller, const std::string &permission) +{ + return g_mockVerifyCallerPermissionRet; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test/mock_notification.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test/mock_notification.cpp new file mode 100755 index 000000000..a4e8001a0 --- /dev/null +++ b/services/ans/test/unittest/advanced_notification_service_branch_test/mock_notification.cpp @@ -0,0 +1,51 @@ +/* + * 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 "notification.h" + +namespace { + bool g_mockGetUserIdRet = true; + bool g_mockGetBundleNameRet = true; +} + +void MockGetUserId(bool mockRet) +{ + g_mockGetUserIdRet = mockRet; +} + +void MockGetBundleName(bool mockRet) +{ + g_mockGetBundleNameRet = mockRet; +} + +namespace OHOS { +namespace Notification { +int32_t Notification::GetUserId() const +{ + if (g_mockGetUserIdRet == false) { + return -1; + } + return 1; +} + +std::string Notification::GetBundleName() const +{ + if (g_mockGetBundleNameRet == false) { + return ""; + } + return ""; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file -- Gitee