diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index 4a802beb389538c43dacfb57f518735206b772cd..9a67402712fc6ce235e479428845b9f26edac495 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -120,10 +120,10 @@ ohos_moduletest("ans_innerkits_module_publish_test") { ] sources = [ - "${services_path}/test/moduletest/mock/mock_access_token_helper.cpp", "ans_innerkits_module_publish_test.cpp", "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", + "mock/mock_accesstoken_kit.cpp", "mock/mock_bundle_manager.cpp", "mock/mock_bundle_manager_helper.cpp", "mock/mock_bundle_mgr_proxy.cpp", diff --git a/frameworks/test/moduletest/mock/mock_accesstoken_kit.cpp b/frameworks/test/moduletest/mock/mock_accesstoken_kit.cpp index 7d8f042d3907a75a702b0237d96dd384facad9ad..c09cf087c4049d05a0865b9831b3baf44cb189b1 100644 --- a/frameworks/test/moduletest/mock/mock_accesstoken_kit.cpp +++ b/frameworks/test/moduletest/mock/mock_accesstoken_kit.cpp @@ -22,6 +22,11 @@ int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& { return PERMISSION_GRANTED; } + +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + return TOKEN_INVALID; +} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 4804e594975f4929eeafe1e71894aa12f3ba599a..8b349142d4ca155a227bc5a564b5832b245c1848 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -850,6 +850,7 @@ private: void SendEnableNotificationSlotHiSysEvent(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode); void SendFlowControlOccurHiSysEvent(const std::shared_ptr &record); + ErrCode PublishNotificationBySa(const sptr &request); private: static sptr instance_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index c7912b901ffb5664b3ca12173c5257c939ca1022..40060eaaf43fa7be6fc2a3b2976c3fb7439060b6 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -498,6 +498,11 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt ANS_LOGD("%{public}s", __FUNCTION__); ErrCode result = ERR_OK; + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (isSubsystem) { + return PublishNotificationBySa(request); + } + do { if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT && !IsSystemApp()) { result = ERR_ANS_NON_SYSTEM_APP; @@ -3974,5 +3979,69 @@ ErrCode AdvancedNotificationService::GetSyncNotificationEnabledWithoutApp(const return ERR_INVALID_OPERATION; #endif } + +ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr &request) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + int32_t uid = request->GetCreatorUid(); + if (uid <= 0) { + ANS_LOGE("CreatorUid[%{public}d] error", uid); + return ERR_ANS_INVALID_UID; + } + + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + ANS_LOGE("failed to get bundleManager!"); + return ERR_ANS_INVALID_BUNDLE; + } + std::string bundle = bundleManager->GetBundleNameByUid(uid); + if (request->GetCreatorBundleName().empty()) { + request->SetCreatorBundleName(bundle); + } + if (request->GetOwnerBundleName().empty()) { + request->SetOwnerBundleName(bundle); + } + + request->SetCreatorPid(IPCSkeleton::GetCallingPid()); + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); + request->SetCreatorUserId(userId); + ANS_LOGD("creator uid=%{public}d, userId=%{public}d, bundleName=%{public}s ", uid, userId, bundle.c_str()); + + ErrCode result = CheckPictureSize(request); + if (result != ERR_OK) { + ANS_LOGE("Failed to check picture size"); + return result; + } + + sptr bundleOption = new NotificationBundleOption(bundle, uid); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create bundleOption"); + return ERR_ANS_NO_MEMORY; + } + + std::shared_ptr record = std::make_shared(); + record->request = request; + record->bundleOption = bundleOption; + record->notification = new (std::nothrow) Notification(request); + if (record->notification == nullptr) { + ANS_LOGE("Failed to create notification"); + return ERR_ANS_NO_MEMORY; + } + + handler_->PostSyncTask([this, &record]() { + if (AssignToNotificationList(record) != ERR_OK) { + ANS_LOGE("Failed to assign notification list"); + return; + } + + UpdateRecentNotification(record->notification, false, 0); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); + }); + + return result; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index e0e43a51e784ca1170272e14f28f750d4bb99459..b724d6573a4f899a777c7e823263688a88d589a3 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -55,6 +55,7 @@ sptr AdvancedNotificationServiceTest::advancedNotif void AdvancedNotificationServiceTest::SetUpTestCase() { advancedNotificationService_ = AdvancedNotificationService::GetInstance(); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); } void AdvancedNotificationServiceTest::TearDownTestCase() @@ -1737,5 +1738,33 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12200, new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), notificationId, label); EXPECT_EQ(result, (int)ERR_OK); } + +/** + * @tc.name: AdvancedNotificationServiceTest_12300 + * @tc.desc: SA publish notification, Failed to publish when creatorUid default. + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12300, Function | SmallTest | Level1) +{ + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); + TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION); + sptr req = new NotificationRequest(); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); + req->SetLabel("req's label"); + std::string label = "publish's label"; + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + normalContent->SetText("normalContent's text"); + normalContent->SetTitle("normalContent's title"); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + req->SetContent(content); + EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_ANS_INVALID_UID); + SleepForFC(); + + req->SetCreatorUid(1); + EXPECT_EQ(advancedNotificationService_->Publish(label, req), 0); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index e3a7f31a6b9881d29e3a8b7a20c4ca57e9e02b4b..17c9d1d6b226a8de7ac5b2ad4b4664dabf897052 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -24,6 +24,8 @@ ohos_moduletest("ans_module_test") { "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "${services_path}/ans/include", + "${services_path}/ans/test/unittest", + "${services_path}/ans/test/unittest/mock/include", "${services_path}/test/moduletest/mock/include", ] @@ -41,15 +43,15 @@ ohos_moduletest("ans_module_test") { "${services_path}/ans/src/reminder_event_manager.cpp", "${services_path}/ans/src/reminder_timer_info.cpp", "${services_path}/ans/src/system_event_observer.cpp", + "${services_path}/ans/test/unittest/mock/mock_access_token_helper.cpp", + "${services_path}/ans/test/unittest/mock/mock_ipc.cpp", "ans_module_test.cpp", "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", - "mock/mock_access_token_helper.cpp", "mock/mock_bundle_manager.cpp", "mock/mock_bundle_manager_helper.cpp", "mock/mock_bundle_mgr_proxy.cpp", "mock/mock_event_handler.cpp", - "mock/mock_ipc.cpp", "mock/mock_single_kv_store.cpp", ] diff --git a/services/test/moduletest/ans_module_test.cpp b/services/test/moduletest/ans_module_test.cpp index 5c891cd2bc75b13741a4837384ab8cf123b080b8..6822d4e820981510321e06c92989a463b7f08362 100644 --- a/services/test/moduletest/ans_module_test.cpp +++ b/services/test/moduletest/ans_module_test.cpp @@ -15,6 +15,7 @@ #include #include +#include "mock_ipc_skeleton.h" #include "notification_preferences.h" #define private public #include "advanced_notification_service.h" @@ -92,6 +93,7 @@ void AnsModuleTest::SetUpTestCase() passed = false; g_advancedNotificationService = new AdvancedNotificationService(); NotificationPreferences::GetInstance().ClearNotificationInRestoreFactorySettings(); + IPCSkeleton::SetCallingTokenID(1); } void AnsModuleTest::TearDownTestCase()