From 9e7f6297805826ddc52750a5e2cba470e70f6f2f Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Tue, 12 Jul 2022 23:18:34 +0800 Subject: [PATCH] codecheck modify Signed-off-by: fangJinliang1 Change-Id: I859623a3bf2e23a309c6e2095016141cea045ac2 Signed-off-by: fangJinliang1 --- frameworks/test/moduletest/BUILD.gn | 2 +- .../moduletest/mock/mock_accesstoken_kit.cpp | 5 ++ .../include/advanced_notification_service.h | 1 + .../ans/src/advanced_notification_service.cpp | 69 +++++++++++++++++++ .../advanced_notification_service_test.cpp | 29 ++++++++ services/test/moduletest/BUILD.gn | 6 +- services/test/moduletest/ans_module_test.cpp | 2 + 7 files changed, 111 insertions(+), 3 deletions(-) diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index 4a802beb3..9a6740271 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 7d8f042d3..c09cf087c 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 4804e5949..8b349142d 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 c7912b901..40060eaaf 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 e0e43a51e..b724d6573 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 e3a7f31a6..17c9d1d6b 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 5c891cd2b..6822d4e82 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() -- Gitee