diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index dcd72551009d1133e9938937aa813640219517cd..45b5cea026cda8777ac4b2f398c2673750692d45 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -54,6 +54,8 @@ namespace OHOS { namespace Notification { static const uint32_t DEFAULT_SLOT_FLAGS = 59; // 0b111011 +constexpr char REMINDER_CAPABILITY[] = "reminder_capability"; +constexpr char SOUND_CAPABILITY[] = "sound_capability"; class AdvancedNotificationService final : public AnsManagerStub, public std::enable_shared_from_this { public: @@ -1600,7 +1602,9 @@ private: ErrCode AssignValidNotificationSlot(const std::shared_ptr &record, const sptr &bundleOption); ErrCode UpdateSlotReminderModeBySlotFlags(const sptr &bundle, uint32_t slotFlags); - ErrCode CheckSoundPermission(const sptr &request, std::string bundleName); + bool VerifyCloudCapability(const int32_t &uid, const std::string &capability); + ErrCode CheckSoundPermission(const sptr &request, + sptr &bundleOption); void GenerateSlotReminderMode(const sptr &slot, const sptr &bundle, bool isSpecifiedSlot = false, uint32_t defaultSlotFlags = DEFAULT_SLOT_FLAGS); static void CloseAlert(const std::shared_ptr &record); diff --git a/services/ans/include/notification_extension_wrapper.h b/services/ans/include/notification_extension_wrapper.h index e86d9849d1d1e741bc89001c39bc7868ae796a28..c6acd9f3de78b99ea71bcc218db4aec3ae4b86d6 100644 --- a/services/ans/include/notification_extension_wrapper.h +++ b/services/ans/include/notification_extension_wrapper.h @@ -42,6 +42,7 @@ public: typedef void (*UPDATE_BY_BUNDLE)(const std::string bundleName, int deleteType); typedef int32_t (*REMINDER_CONTROL)(const std::string &bundleName); typedef int32_t (*BANNER_CONTROL)(const std::string &bundleName); + typedef int32_t (*VERIFY_CLOUD_CAPABILITY)(const int32_t &uid, const std::string &permission); typedef bool (*NOTIFICATIONDIALOGCONTROL)(); #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER @@ -66,6 +67,7 @@ public: void UpdateByBundle(const std::string bundleName, int deleteType); int32_t ReminderControl(const std::string &bundleName); int32_t BannerControl(const std::string &bundleName); + int32_t VerifyCloudCapability(const int32_t &uid, const std::string &capability); bool NotificationDialogControl(); #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER @@ -90,6 +92,7 @@ private: UPDATE_BY_BUNDLE updateByBundle_ = nullptr; REMINDER_CONTROL reminderControl_ = nullptr; BANNER_CONTROL bannerControl_ = nullptr; + VERIFY_CLOUD_CAPABILITY verifyCloudCapability_ = nullptr; NOTIFICATIONDIALOGCONTROL notificationDialogControl_ = nullptr; bool isRegisterDataSettingObserver = false; diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index befff238ceeb88bcf036846b7a47f255a68aef66..29ec8e9b997c9543e79492724487c6b0cf54d302 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -111,7 +111,7 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt break; } - result = CheckSoundPermission(request, bundleOption->GetBundleName()); + result = CheckSoundPermission(request, bundleOption); if (result != ERR_OK) { message.ErrorCode(result).Message("Check sound failed."); NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index d260df2a0bcc04828fd569caa85301bec8b801df..eed7750f11213510d5c70efcc52ea078c6ebd6d5 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -364,15 +364,7 @@ AdvancedNotificationService::AdvancedNotificationService() AdvancedNotificationService::~AdvancedNotificationService() { - ANS_LOGD("called"); - NotificationSubscriberManager::GetInstance()->UnRegisterOnSubscriberAddCallback(); - - StopFilters(); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DistributedNotificationManager::GetInstance()->UngegisterCallback(); -#endif - SelfClean(); - slotFlagsDefaultMap_.clear(); + ANS_LOGI("deconstructor"); } void AdvancedNotificationService::SelfClean() @@ -1903,8 +1895,18 @@ bool AdvancedNotificationService::IsNeedNotifyConsumed(const sptrVerifyCloudCapability(uid, capability); + return (ctrlResult == ERR_OK) ? true : false; +#else + return false; +#endif +} + ErrCode AdvancedNotificationService::CheckSoundPermission(const sptr &request, - std::string bundleName) + sptr &bundleOption) { ANS_LOGD("called"); if (request->GetSound().empty()) { @@ -1919,8 +1921,8 @@ ErrCode AdvancedNotificationService::CheckSoundPermission(const sptrneedUpdateCache_.load()); + ANS_LOGD("Check sound permission: %{public}d, %{public}s, %{public}d", + length, bundleOption->GetBundleName().c_str(), soundPermissionInfo_->needUpdateCache_.load()); if (soundPermissionInfo_->needUpdateCache_.load()) { std::lock_guard lock(soundPermissionInfo_->dbMutex_); if (soundPermissionInfo_->needUpdateCache_.load()) { @@ -1932,7 +1934,9 @@ ErrCode AdvancedNotificationService::CheckSoundPermission(const sptrallPackage_ && soundPermissionInfo_->bundleName_.count(bundleName) == 0) { + if (!soundPermissionInfo_->allPackage_ + && soundPermissionInfo_->bundleName_.count(bundleOption->GetBundleName()) == 0 + && !VerifyCloudCapability(bundleOption->GetUid(), SOUND_CAPABILITY)) { request->SetSound(""); } return ERR_OK; diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index ca91a82d6ce5296822babbff23199a561c50b982..80e28048979f33ae8c59dc402e00872d5580cfaa 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1753,6 +1753,12 @@ void AdvancedNotificationService::CloseAlert(const std::shared_ptrGetDefaultUidByBundleName(bundleName, userId); + if (VerifyCloudCapability(uid, REMINDER_CAPABILITY)) { + return true; + } if (DelayedSingleton::GetInstance()->IsReminderEnabled(bundleName)) { return true; } diff --git a/services/ans/src/notification_extension_wrapper.cpp b/services/ans/src/notification_extension_wrapper.cpp index 90d9c760440447887ce1b8a4ee96a7a5a2fdb2d2..8a78e4881c62f871d73abcacb6728d7aded37436 100644 --- a/services/ans/src/notification_extension_wrapper.cpp +++ b/services/ans/src/notification_extension_wrapper.cpp @@ -151,7 +151,12 @@ void ExtensionWrapper::InitExtentionWrapper() ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror()); return; } - ANS_LOGD("extension wrapper init success"); + verifyCloudCapability_ = (VERIFY_CLOUD_CAPABILITY)dlsym(extensionWrapperHandle_, "VerifyCloudCapability"); + if (verifyCloudCapability_ == nullptr) { + ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror()); + return; + } + ANS_LOGI("extension wrapper init success"); } void ExtensionWrapper::CheckIfSetlocalSwitch() @@ -238,6 +243,15 @@ int32_t ExtensionWrapper::BannerControl(const std::string &bundleName) return bannerControl_(bundleName); } +int32_t ExtensionWrapper::VerifyCloudCapability(const int32_t &uid, const std::string &capability) +{ + if (verifyCloudCapability_ == nullptr) { + ANS_LOGE("VerifyCloudCapability wrapper symbol failed"); + return -1; + } + return verifyCloudCapability_(uid, capability); +} + #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER void ExtensionWrapper::IsPrivilegeMessage(const sptr &request) { diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 0c1b19a507caa03949dad4c6369c11a0e9b54c9e..fa5240ce86f811f99dc41ce2395dc2e079971f59 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -322,6 +322,101 @@ ohos_unittest("notification_service_test") { "advanced_notification_service_test.cpp", "advanced_notification_slot_service_test.cpp", "advanced_notification_utils_test.cpp", + "mock/blob.cpp", + "mock/distributed_kv_data_manager.cpp", + "mock/mock_access_token_helper.cpp", + "mock/mock_accesstoken_kit.cpp", + "mock/mock_bundle_manager_helper.cpp", + "mock/mock_bundle_mgr.cpp", + "mock/mock_event_handler.cpp", + "mock/mock_ipc.cpp", + "mock/mock_push_callback_stub.cpp", + "mock/mock_single_kv_store.cpp", + "notification_dialog_test/mock_os_account_manager_annex.cpp", + ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", + ] + + if (distributed_notification_supported) { + defines += [ "DISTRIBUTED_NOTIFICATION_SUPPORTED" ] + deps += [ "${services_path}/distributed:libans_distributed" ] + include_dirs += [ "${services_path}/distributed/include" ] + } + + external_deps = [ + "ability_base:base", + "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", + "googletest:gmock_main", + "common_event_service:cesfwk_innerkits", + "device_manager:devicemanagersdk", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gtest_main", + "hilog:libhilog", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "image_framework:image_native", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + if (device_usage) { + external_deps += [ "device_usage_statistics:usagestatsinner" ] + defines += [ "DEVICE_USAGE_STATISTICS_ENABLE" ] + } + + if (player_framework) { + external_deps += [ "player_framework:media_client" ] + defines += [ "PLAYER_FRAMEWORK_ENABLE" ] + } + + cflags = [] + if (hisysevent_usage) { + external_deps += [ "hisysevent:libhisysevent" ] + cflags += [ "-DHAS_HISYSEVENT_PART" ] + } + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +ohos_unittest("notification_clone_test") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/ans/include", + "${services_path}/ans/test/unittest/mock/include", + ] + + defines = [] + + sources = [ + "${test_path}/mock/mock_tokenid_kit.cpp", "clone_test/notification_clone_bundle_info_test.cpp", "clone_test/notification_clone_disturb_service.cpp", "clone_test/notification_clone_bundle_service_test.cpp", @@ -1616,6 +1711,7 @@ group("unittest") { ":notification_rdb_data_mgr_test", ":notification_service_publish_test", ":notification_service_test", + ":notification_clone_test", ":notification_subscriber_manager_branch_test", ":notification_subscriber_manager_test", ":os_account_manager_helper_test", diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp index d48872dd6860b1d433b53ed4cdc2beeede01b7ae..4a9fdecd333f561696dc1fab572414a1db9831fe 100644 --- a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp @@ -503,7 +503,7 @@ HWTEST_F(AnsBranchTest, SubscribeSelf_279003, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr info = new NotificationSubscribeInfo(); - ASSERT_EQ(advancedNotificationService_->SubscribeSelf(subscriber->GetImpl()), ERR_ANS_TASK_ERR); + ASSERT_EQ(advancedNotificationService_->SubscribeSelf(subscriber->GetImpl()), ERR_OK); } /** diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 316ad312ca061f86d50137ac31db17c3ed08183d..80d17f2b109fd3e0de8f8703db7114db383bb0bd 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -678,9 +678,9 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_08300, { auto subscriber = new TestAnsSubscriber(); sptr info = new NotificationSubscribeInfo(); - EXPECT_NE((int)advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), (int)ERR_OK); + EXPECT_EQ((int)advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), (int)ERR_OK); ASSERT_EQ((int)advancedNotificationService_->Subscribe(nullptr, info), (int)ERR_ANS_INVALID_PARAM); - EXPECT_NE((int)advancedNotificationService_->Unsubscribe(subscriber->GetImpl(), nullptr), (int)ERR_OK); + EXPECT_EQ((int)advancedNotificationService_->Unsubscribe(subscriber->GetImpl(), nullptr), (int)ERR_OK); } /** @@ -1544,8 +1544,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17400, auto subscriber = new TestAnsSubscriber(); sptr info = new NotificationSubscribeInfo(); - EXPECT_NE(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_OK); - EXPECT_NE(advancedNotificationService_->Unsubscribe(subscriber->GetImpl(), info), ERR_OK); + EXPECT_EQ(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_OK); + EXPECT_EQ(advancedNotificationService_->Unsubscribe(subscriber->GetImpl(), info), ERR_OK); GTEST_LOG_(INFO) << "Subscribe_1000 test end"; } @@ -1562,7 +1562,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17500, auto subscriber = new TestAnsSubscriber(); sptr info = new NotificationSubscribeInfo(); - EXPECT_NE(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_OK); + EXPECT_EQ(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_OK); ASSERT_EQ(advancedNotificationService_->Unsubscribe(nullptr, info), ERR_ANS_INVALID_PARAM); GTEST_LOG_(INFO) << "Unsubscribe_1000 test end"; diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp index dd5e4a78fa5c9cd3dbef6eb72f8cf08d999df9f9..8bd713cfd9f637d79c72a4fa72e16ee3542040c3 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp @@ -1692,8 +1692,10 @@ HWTEST_F(AdvancedNotificationServiceUnitTest, CheckSoundPermission_100, Function sound += "."; // sound length larger than 2048 request->SetSound(sound); std::string bundle = "bundle"; + int32_t uid = 10; + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid); - auto ret = advancedNotificationService_->CheckSoundPermission(request, bundle); + auto ret = advancedNotificationService_->CheckSoundPermission(request, bundleOption); ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); } @@ -1709,8 +1711,10 @@ HWTEST_F(AdvancedNotificationServiceUnitTest, CheckSoundPermission_200, Function std::string sound = "1"; request->SetSound(sound); std::string bundle = "bundle"; + int32_t uid = 10; + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid); - auto ret = advancedNotificationService_->CheckSoundPermission(request, bundle); + auto ret = advancedNotificationService_->CheckSoundPermission(request, bundleOption); ASSERT_EQ(ret, (int)ERR_OK); } diff --git a/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp b/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp index a0f1e12d9fc9bfd0c134cefe6db4a086ee3e817a..223ba0270570f8763ac707cf5889ad10d04183cb 100644 --- a/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp +++ b/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp @@ -53,7 +53,7 @@ HWTEST_F(DhNotificationCloneBundleTest, OnRestore_Test_001, Function | SmallTest { nlohmann::json jsonObject; int32_t userId = 100; - auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); + auto advancedNotificationService_ = AdvancedNotificationService::GetInstance(); sptr date = nullptr; std::vector> profiles = { date }; diff --git a/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp b/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp index 828f72499767d9f6525cd8b4ee39bb0f76689507..cbe5d815a4ff580eec51dfd8c59cfe83498530fc 100644 --- a/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp +++ b/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp @@ -63,11 +63,8 @@ protected: HWTEST_F(NotificationCloneBundleTest, OnBackUp_00001, Function | SmallTest | Level1) { nlohmann::json jsonObject; - int32_t userId = 300; - auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); - - MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); - EXPECT_CALL(*mockCloneUtil, GetActiveUserId()).WillOnce(Return(userId)); + int32_t userId = 100; + auto advancedNotificationService_ = AdvancedNotificationService::GetInstance(); sptr date = nullptr; std::vector> profiles = { date }; diff --git a/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp index 58cef3cd6f9e486f6ab183aab4ad9c621781e3b5..519841b874aa74f5c173b1149b552d5d7dc221a8 100644 --- a/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp +++ b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp @@ -65,10 +65,7 @@ HWTEST_F(NotificationCloneDisturbTest, OnBackUp_00001, Function | SmallTest | Le { nlohmann::json jsonObject; int32_t userId = 100; - auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); - - MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); - EXPECT_CALL(*mockCloneUtil, GetActiveUserId()).WillOnce(Return(userId)); + auto advancedNotificationService_ = AdvancedNotificationService::GetInstance(); sptr date = nullptr; std::vector> profiles = { date }; @@ -142,40 +139,6 @@ HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_002, Function | SmallT bundle.SetAppIndex(1); trustList.push_back(bundle); - - MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); - EXPECT_CALL(*mockCloneUtil, GetBundleUid(_, _, _)).WillOnce(Return(54321)); - - notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); - - EXPECT_EQ(exitBunldleList.size(), 0); - EXPECT_EQ(notExitBunldleList.size(), 1); -} - -/** - * @tc.name: GetProfileUid_Test_003 - * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_003, Function | SmallTest | Level1) -{ - int32_t userId = 1; - std::map uidMap; - std::vector trustList; - std::vector exitBunldleList; - std::vector notExitBunldleList; - - // Create a bundle - NotificationBundleOption bundle; - bundle.SetBundleName("com.example.app"); - bundle.SetAppIndex(1); - - trustList.push_back(bundle); - - MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); - EXPECT_CALL(*mockCloneUtil, GetBundleUid(_, _, _)).WillOnce(Return(-1)); - notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); EXPECT_EQ(exitBunldleList.size(), 0);