From 0b0298c69015a70836e38e3f9d90ba2a04af4591 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Thu, 1 Sep 2022 09:54:44 +0800 Subject: [PATCH] add userId judge Signed-off-by: fangJinliang1 Change-Id: If0bcb825cb48d8b0182017d30d778dd73f8387e6 Signed-off-by: fangJinliang1 --- .../core/common/include/ans_const_define.h | 1 + services/ans/src/system_event_observer.cpp | 5 +++ .../advanced_notification_service_test.cpp | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index 66454a4a0..bfd0d5309 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -36,6 +36,7 @@ constexpr uint32_t SYSTEM_SERVICE_UID = 1000; constexpr int32_t SUBSCRIBE_USER_INIT = -1; constexpr int32_t SUBSCRIBE_USER_ALL = -2; +constexpr int32_t DEFAULT_USER_ID = 100; constexpr int32_t SUBSCRIBE_USER_SYSTEM_BEGIN = 0; constexpr int32_t SUBSCRIBE_USER_SYSTEM_END = 99; constexpr int32_t NOTIFICATION_MIN_COUNT = 0; diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 8ada7d50d..33a8d2eeb 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -16,6 +16,7 @@ #include "system_event_observer.h" #include "advanced_notification_service.h" +#include "ans_const_define.h" #include "bundle_constants.h" #include "common_event_manager.h" #include "common_event_support.h" @@ -85,6 +86,10 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) NotificationPreferences::GetInstance().InitSettingFromDisturbDB(); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { int32_t userId = data.GetCode(); + if (userId <= DEFAULT_USER_ID) { + ANS_LOGE("Illegal userId, userId[%{public}d].", userId); + return; + } if (callbacks_.onResourceRemove != nullptr) { callbacks_.onResourceRemove(userId); } diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 8e08d8926..5e58897e6 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -26,9 +26,12 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "ans_ut_constant.h" +#include "common_event_manager.h" +#include "common_event_support.h" #include "mock_ipc_skeleton.h" #include "notification_preferences.h" #include "notification_subscriber.h" +#include "system_event_observer.h" using namespace testing::ext; using namespace OHOS::Media; @@ -1626,5 +1629,42 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12400, IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK); } + +/* + * @tc.name: AdvancedNotificationServiceTest_12500 + * @tc.desc: When the user removed event is received and the userid is less than or equal to 100, + * the notification cannot be deleted + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12500, Function | SmallTest | Level1) +{ + sptr req = new (std::nothrow) NotificationRequest(1); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::OTHER); + 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); + req->SetCreatorUserId(DEFAULT_USER_ID); + EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK); + SleepForFC(); + + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED)); + data.SetCode(DEFAULT_USER_ID); + advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data); + + std::stringstream key; + key << "_" << req->GetCreatorUserId() << "_" << req->GetCreatorUid() << "_" + << req->GetLabel() << "_" << req->GetNotificationId(); + + EXPECT_EQ(advancedNotificationService_->IsNotificationExists(key.str()), true); +} } // namespace Notification } // namespace OHOS \ No newline at end of file -- Gitee