From 60adc6d3ba18b4539b620764e69a6e90b49798e4 Mon Sep 17 00:00:00 2001 From: gaojiaqi Date: Wed, 18 Dec 2024 18:33:53 +0800 Subject: [PATCH] update Signed-off-by: gaojiaqi --- frameworks/reminder/src/reminder_request.cpp | 10 +- .../test/unittest/reminder_request_test.cpp | 56 +-- interfaces/inner_api/reminder_request.h | 10 +- services/reminder/BUILD.gn | 1 - .../reminder/include/reminder_agent_service.h | 83 +++-- .../reminder/src/reminder_agent_service.cpp | 336 +++++++++--------- .../src/reminder_datashare_helper.cpp | 10 +- .../reminder/src/reminder_store_strategy.cpp | 18 +- services/reminder/test/unittest/BUILD.gn | 63 +--- .../access_token_helper_test.cpp | 90 ----- .../mock/include/mock_accesstoken_kit.h | 25 +- .../mock/include/mock_notification_helper.h} | 27 +- .../mock/include/mock_reminder_data_manager.h | 28 ++ .../unittest/mock/mock_accesstoken_kit.cpp | 58 +-- .../mock/mock_notification_helper.cpp} | 51 +-- ...per.cpp => mock_reminder_data_manager.cpp} | 31 +- .../unittest/reminder_data_manager_test.cpp | 40 +-- .../test/unittest/reminder_service_test.cpp | 259 +++++++++----- .../reminder_service_publish_test.cpp | 175 --------- .../reminderrequest_fuzzer.cpp | 4 +- 20 files changed, 568 insertions(+), 807 deletions(-) delete mode 100644 services/reminder/test/unittest/access_token_helper_test/access_token_helper_test.cpp rename services/reminder/{include/reminder_access_token_helper.h => test/unittest/mock/include/mock_notification_helper.h} (44%) create mode 100644 services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h rename services/reminder/{src/reminder_access_token_helper.cpp => test/unittest/mock/mock_notification_helper.cpp} (36%) rename services/reminder/test/unittest/mock/{mock_access_token_helper.cpp => mock_reminder_data_manager.cpp} (49%) delete mode 100644 services/reminder/test/unittest/reminder_service_test/reminder_service_publish_test.cpp diff --git a/frameworks/reminder/src/reminder_request.cpp b/frameworks/reminder/src/reminder_request.cpp index f9608a829..5fc758663 100644 --- a/frameworks/reminder/src/reminder_request.cpp +++ b/frameworks/reminder/src/reminder_request.cpp @@ -240,12 +240,12 @@ void ReminderRequest::SetExpired(bool isExpired) isExpired_ = isExpired; } -void ReminderRequest::InitCreatorBundleName(const std::string &creatorBundleName) +void ReminderRequest::SetCreatorBundleName(const std::string &creatorBundleName) { creatorBundleName_ = creatorBundleName; } -void ReminderRequest::InitCreatorUid(const int32_t creatorUid) +void ReminderRequest::SetCreatorUid(const int32_t creatorUid) { creatorUid_ = creatorUid; } @@ -261,17 +261,17 @@ void ReminderRequest::InitReminderId() ANSR_LOGI("reminderId_=%{public}d", reminderId_); } -void ReminderRequest::InitUserId(const int32_t &userId) +void ReminderRequest::SetUserId(const int32_t &userId) { userId_ = userId; } -void ReminderRequest::InitUid(const int32_t &uid) +void ReminderRequest::SetUid(const int32_t &uid) { uid_ = uid; } -void ReminderRequest::InitBundleName(const std::string &bundleName) +void ReminderRequest::SetBundleName(const std::string &bundleName) { bundleName_ = bundleName; } diff --git a/frameworks/reminder/test/unittest/reminder_request_test.cpp b/frameworks/reminder/test/unittest/reminder_request_test.cpp index 56de62428..cd4ca8ffb 100644 --- a/frameworks/reminder/test/unittest/reminder_request_test.cpp +++ b/frameworks/reminder/test/unittest/reminder_request_test.cpp @@ -1090,12 +1090,12 @@ HWTEST_F(ReminderRequestTest, AddActionButtons_00001, Function | SmallTest | Lev } /** - * @tc.name: InitUserId_00001 - * @tc.desc: Test InitUserId parameters. + * @tc.name: SetUserId_00001 + * @tc.desc: Test SetUserId parameters. * @tc.type: FUNC * @tc.require: issueI65R21 */ -HWTEST_F(ReminderRequestTest, InitUserId_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetUserId_00001, Function | SmallTest | Level1) { std::shared_ptr reminderRequestChild = std::make_shared(); ASSERT_NE(nullptr, reminderRequestChild); @@ -1104,8 +1104,8 @@ HWTEST_F(ReminderRequestTest, InitUserId_00001, Function | SmallTest | Level1) std::string function = "this is function"; int32_t userId = 1; int32_t uid = 2; - reminderRequestChild->InitUserId(userId); - reminderRequestChild->InitUid(uid); + reminderRequestChild->SetUserId(userId); + reminderRequestChild->SetUid(uid); reminderRequestChild->SetState(deSet, newState, function); uint8_t result1 = reminderRequestChild->GetState(); EXPECT_EQ(result1, 2); @@ -1132,8 +1132,8 @@ HWTEST_F(ReminderRequestTest, OnStart_00001, Function | SmallTest | Level1) std::string function = "this is function"; int32_t userId = 1; int32_t uid = 2; - reminderRequestChild->InitUserId(userId); - reminderRequestChild->InitUid(uid); + reminderRequestChild->SetUserId(userId); + reminderRequestChild->SetUid(uid); reminderRequestChild->SetState(deSet, newState, function); reminderRequestChild->OnStart(); reminderRequestChild->OnStop(); @@ -1687,30 +1687,30 @@ HWTEST_F(ReminderRequestTest, SetGroupId_00001, Function | SmallTest | Level1) } /** - * @tc.name: InitBundleName_00001 - * @tc.desc: Test InitBundleName with normal parameters. + * @tc.name: SetBundleName_00001 + * @tc.desc: Test SetBundleName with normal parameters. * @tc.type: FUNC * @tc.require: issueI89858 */ -HWTEST_F(ReminderRequestTest, InitBundleName_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetBundleName_00001, Function | SmallTest | Level1) { auto rrc = std::make_shared(); std::string bundleName = "com.example.myapplication"; - rrc->InitBundleName(bundleName); + rrc->SetBundleName(bundleName); EXPECT_EQ(rrc->GetBundleName(), bundleName); } /** - * @tc.name: InitBundleName_00002 - * @tc.desc: Test InitBundleName with special parameters. + * @tc.name: SetBundleName_00002 + * @tc.desc: Test SetBundleName with special parameters. * @tc.type: FUNC * @tc.require: issueI89858 */ -HWTEST_F(ReminderRequestTest, InitBundleName_00002, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetBundleName_00002, Function | SmallTest | Level1) { auto rrc = std::make_shared(); std::string bundleName = "com.example.myapplication.~!@#$%^&*()"; - rrc->InitBundleName(bundleName); + rrc->SetBundleName(bundleName); EXPECT_EQ(rrc->GetBundleName(), bundleName); } @@ -1780,30 +1780,30 @@ HWTEST_F(ReminderRequestTest, UpdateNotificationCommon_00300, Function | SmallTe } /** - * @tc.name: InitCreatorBundleName_00001 - * @tc.desc: Test InitCreatorBundleName with normal parameters. + * @tc.name: SetCreatorBundleName_00001 + * @tc.desc: Test SetCreatorBundleName with normal parameters. * @tc.type: FUNC * @tc.require: issue#I8R55M */ -HWTEST_F(ReminderRequestTest, InitCreatorBundleName_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetCreatorBundleName_00001, Function | SmallTest | Level1) { auto rrc = std::make_shared(); std::string bundleName = "com.example.myapplication"; - rrc->InitCreatorBundleName(bundleName); + rrc->SetCreatorBundleName(bundleName); EXPECT_EQ(rrc->GetCreatorBundleName(), bundleName); } /** - * @tc.name: InitCreatorBundleName_00002 - * @tc.desc: Test InitCreatorBundleName with special parameters. + * @tc.name: SetCreatorBundleName_00002 + * @tc.desc: Test SetCreatorBundleName with special parameters. * @tc.type: FUNC * @tc.require: issue#I8R55M */ -HWTEST_F(ReminderRequestTest, InitCreatorBundleName_00002, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetCreatorBundleName_00002, Function | SmallTest | Level1) { auto rrc = std::make_shared(); std::string bundleName = "com.example.myapplication.~!@#$%^&*()"; - rrc->InitCreatorBundleName(bundleName); + rrc->SetCreatorBundleName(bundleName); EXPECT_EQ(rrc->GetCreatorBundleName(), bundleName); } @@ -2106,18 +2106,18 @@ HWTEST_F(ReminderRequestTest, RecoverActionButtonJsonMode_00003, Function | Smal } /** - * @tc.name: InitCreatorUid_00001 - * @tc.desc: Test InitCreatorUid. + * @tc.name: SetCreatorUid_00001 + * @tc.desc: Test SetCreatorUid. * @tc.type: FUNC * @tc.require: issue#I94VJT */ -HWTEST_F(ReminderRequestTest, InitCreatorUid_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderRequestTest, SetCreatorUid_00001, Function | SmallTest | Level1) { auto rrc = std::make_shared(); - rrc->InitCreatorUid(100); + rrc->SetCreatorUid(100); EXPECT_EQ(rrc->GetCreatorUid(), 100); - rrc->InitCreatorUid(-1); + rrc->SetCreatorUid(-1); EXPECT_EQ(rrc->GetCreatorUid(), -1); } diff --git a/interfaces/inner_api/reminder_request.h b/interfaces/inner_api/reminder_request.h index 24efe5eed..909550d8d 100644 --- a/interfaces/inner_api/reminder_request.h +++ b/interfaces/inner_api/reminder_request.h @@ -512,14 +512,14 @@ public: * * @param creatorBundleName Indicates the creator bundle name which the reminder belong to */ - void InitCreatorBundleName(const std::string &creatorBundleName); + void SetCreatorBundleName(const std::string &creatorBundleName); /** * @brief Inites reminder creator uid when publish reminder success. * * @param uid Indicates the creator uid which the reminder belong to */ - void InitCreatorUid(const int32_t creatorUid); + void SetCreatorUid(const int32_t creatorUid); /** * @brief Inits reminder id when publish reminder success. @@ -536,7 +536,7 @@ public: * * @param userId Indicates the userId which the reminder belong to. */ - void InitUserId(const int32_t &userId); + void SetUserId(const int32_t &userId); /** * @brief Inites reminder uid when publish reminder success. @@ -546,14 +546,14 @@ public: * * @param uid Indicates the uid which the reminder belong to. */ - void InitUid(const int32_t &uid); + void SetUid(const int32_t &uid); /** * @brief Inites reminder bundle name when publish reminder success. * * @param bundleName Indicates the bundle name which the reminder belong to */ - void InitBundleName(const std::string &bundleName); + void SetBundleName(const std::string &bundleName); /** * @brief Check the reminder is alerting or not. diff --git a/services/reminder/BUILD.gn b/services/reminder/BUILD.gn index 7e7ca5142..43bdb90db 100644 --- a/services/reminder/BUILD.gn +++ b/services/reminder/BUILD.gn @@ -51,7 +51,6 @@ ohos_source_set("reminder_service_sources") { defines = [] sources = [ - "src/reminder_access_token_helper.cpp", "src/reminder_agent_service.cpp", "src/reminder_agent_service_ability.cpp", "src/reminder_bundle_manager_helper.cpp", diff --git a/services/reminder/include/reminder_agent_service.h b/services/reminder/include/reminder_agent_service.h index 45b308cf3..3b5e20e03 100644 --- a/services/reminder/include/reminder_agent_service.h +++ b/services/reminder/include/reminder_agent_service.h @@ -13,32 +13,19 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_SERVICE_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_SERVICE_H +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H -#include -#include -#include #include #include -#include "event_handler.h" -#include "event_runner.h" #include "ffrt.h" -#include "refbase.h" - -#include "ans_const_define.h" #include "reminder_agent_service_stub.h" -#include "reminder_request_adaptation.h" - -namespace OHOS { -namespace Notification { -static const uint32_t DEFAULT_SLOT_FLAGS = 59; // 0b111011 +namespace OHOS::Notification { class ReminderAgentService final : public ReminderAgentServiceStub, public std::enable_shared_from_this { public: - ~ReminderAgentService() override = default; DISALLOW_COPY_AND_MOVE(ReminderAgentService); @@ -51,20 +38,16 @@ public: static sptr GetInstance(); /** - * @brief Check reminder permission - */ - bool CheckReminderPermission(); - - /** - * @brief Publishes a reminder notification. + * @brief Publishes a reminder request. * - * @param reminder Identifies the reminder notification request that needs to be published. + * @param reminder Identifies the reminder request that needs to be published. + * @param reminderId The ID of the reminder request. * @return Returns ERR_OK on success, others on failure. */ - ErrCode PublishReminder(const ReminderRequest &reminder, int32_t& reminderId) override; + ErrCode PublishReminder(const ReminderRequest& reminder, int32_t& reminderId) override; /** - * @brief Cancel a reminder notifications. + * @brief Cancel a reminder request. * * @param reminderId Identifies the reminders id that needs to be canceled. * @return Returns ERR_OK on success, others on failure. @@ -72,31 +55,31 @@ public: ErrCode CancelReminder(const int32_t reminderId) override; /** - * @brief Get all valid reminder notifications. + * @brief Cancel all reminder requests. * - * @param reminders Identifies the list of all valid notifications. * @return Returns ERR_OK on success, others on failure. */ - ErrCode GetValidReminders(std::vector &reminders) override; + ErrCode CancelAllReminders() override; /** - * @brief Cancel all reminder notifications. + * @brief Get all valid reminder requests. * + * @param reminders Identifies the list of all valid reminder requests. * @return Returns ERR_OK on success, others on failure. */ - ErrCode CancelAllReminders() override; + ErrCode GetValidReminders(std::vector& reminders) override; /** - * @brief Add exclude date for reminder + * @brief Add exclude date for reminder. * * @param reminderId Identifies the reminders id. - * @param date exclude date + * @param date exclude date. * @return Returns ERR_OK on success, others on failure. */ ErrCode AddExcludeDate(const int32_t reminderId, const int64_t date) override; /** - * @brief Clear exclude date for reminder + * @brief Clear exclude date for reminder. * * @param reminderId Identifies the reminders id. * @return Returns ERR_OK on success, others on failure. @@ -104,26 +87,49 @@ public: ErrCode DelExcludeDates(const int32_t reminderId) override; /** - * @brief Get exclude date for reminder + * @brief Get exclude date for reminder. * * @param reminderId Identifies the reminders id. - * @param dates exclude dates + * @param dates exclude dates. * @return Returns ERR_OK on success, others on failure. */ ErrCode GetExcludeDates(const int32_t reminderId, std::vector& dates) override; void TryPostDelayUnloadTask(int64_t delayTime); + private: ReminderAgentService() = default; void PostDelayUnloadTask(); - void ChangeReminderAgentLoadConfig(int8_t reminderAgentState); + void ChangeReminderAgentLoadConfig(const int8_t reminderAgentState); - sptr CreateTarReminderRequest(const ReminderRequest &reminder); + /** + * @brief Create reminder request pointer. + */ + sptr CreateReminderRequest(const ReminderRequest& reminder); + /** + * @brief Init reminder info. + */ ErrCode InitReminderRequest(sptr& tarReminder, const std::string& bundle, const int32_t callingUid); + + /** + * @brief Check reminder permission. + */ + bool CheckReminderPermission(); + + /** + * @brief Check caller is system app. + */ + bool IsSystemApp(); + + /** + * @brief Get client bundle name. + */ + std::string GetClientBundleName(const int32_t callingUid); + private: ffrt::task_handle tryUnloadTask_ = nullptr; static sptr instance_; @@ -131,7 +137,6 @@ private: static std::mutex unloadMutex_; int8_t reminderAgentState_ = -1; }; -} // namespace Notification -} // namespace OHOS +} // namespace OHOS::Notification #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_SERVICE_H diff --git a/services/reminder/src/reminder_agent_service.cpp b/services/reminder/src/reminder_agent_service.cpp index c1f9d6089..8a5299538 100644 --- a/services/reminder/src/reminder_agent_service.cpp +++ b/services/reminder/src/reminder_agent_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -15,107 +15,90 @@ #include "reminder_agent_service.h" +#include "reminder_data_manager.h" +#include "reminder_request_alarm.h" #include "reminder_request_timer.h" #include "reminder_request_calendar.h" -#include "reminder_request_alarm.h" #include "reminder_bundle_manager_helper.h" -#include "reminder_access_token_helper.h" -#include "reminder_utils.h" -#include "in_process_call_wrapper.h" -#include "ffrt_inner.h" - - -#include -#include -#include -#include +#include "ffrt_inner.h" +#include "tokenid_kit.h" +#include "ipc_skeleton.h" #include "accesstoken_kit.h" -#include "ans_inner_errors.h" #include "ans_log_wrapper.h" -#include "errors.h" - -#include "ipc_skeleton.h" -#include "notification_constant.h" -#include "notification_request.h" +#include "ans_inner_errors.h" #include "os_account_manager.h" -#include "hitrace_meter_adapter.h" -#include "reminder_data_manager.h" #include "notification_helper.h" -#include "reminder_os_account_manager_helper.h" -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED -#include "distributed_notification_manager.h" -#include "distributed_preferences.h" -#include "distributed_screen_status_manager.h" -#endif +#include "hitrace_meter_adapter.h" +#include "in_process_call_wrapper.h" -namespace OHOS { -namespace Notification { +#include + +namespace OHOS::Notification { +namespace { constexpr int8_t REMINDER_AGENT_SERVICE_LOAD_STATE = 1; constexpr int8_t REMINDER_AGENT_SERVICE_UNLOAD_STATE = 0; +constexpr int32_t REMINDER_AGENT_SERVICE_ID = 3204; +constexpr int64_t DELAY_TIME = 60 * 1000 * 1000; constexpr const char* REMINDER_AGENT_SERVICE_CONFIG_PATH = "/data/service/el1/public/notification/reminder_agent_service_config"; -constexpr int64_t DELAY_TIME = 60 * 1000 * 1000; -constexpr int32_t REMINDER_AGENT_SERVICE_ID = 3204; +} + sptr ReminderAgentService::instance_; std::mutex ReminderAgentService::instanceMutex_; std::mutex ReminderAgentService::unloadMutex_; + sptr ReminderAgentService::GetInstance() { - std::lock_guard lock(instanceMutex_); + std::lock_guard locker(instanceMutex_); if (instance_ == nullptr) { instance_ = new (std::nothrow) ReminderAgentService(); if (instance_ == nullptr) { - ANS_LOGE("Failed to create AdvancedNotificationService instance"); + ANSR_LOGE("Failed to create ReminderAgentService instance."); return nullptr; } } - return instance_; } -inline bool ReminderAgentService::CheckReminderPermission() -{ - Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); - ErrCode result = Security::AccessToken::AccessTokenKit::VerifyAccessToken( - callerToken, "ohos.permission.PUBLISH_AGENT_REMINDER"); - return result == Security::AccessToken::PermissionState::PERMISSION_GRANTED; -} - -ErrCode ReminderAgentService::PublishReminder(const ReminderRequest &reminder, int32_t& reminderId) +ErrCode ReminderAgentService::PublishReminder(const ReminderRequest& reminder, int32_t& reminderId) { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("Publish reminder"); - sptr tarReminder = CreateTarReminderRequest(reminder); - if (!tarReminder) { - ANSR_LOGE("ReminderRequest object is nullptr"); - return ERR_ANS_INVALID_PARAM; + ANSR_LOGD("call."); + sptr tarReminder = CreateReminderRequest(reminder); + if (tarReminder == nullptr) { + ANSR_LOGE("ReminderRequest object is nullptr."); + return ERR_REMINDER_INVALID_PARAM; } - int32_t callingUid = IPCSkeleton::GetCallingUid(); - std::string bundle = GetClientBundleName(); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + std::string bundleName = GetClientBundleName(callingUid); bool isAllowUseReminder = false; - NotificationHelper::AllowUseReminder(bundle, isAllowUseReminder); + NotificationHelper::AllowUseReminder(bundleName, isAllowUseReminder); if (!isAllowUseReminder) { ANSR_LOGW("The number of reminders exceeds the limit[0]."); return ERR_REMINDER_NUMBER_OVERLOAD; } - InitReminderRequest(tarReminder, bundle, callingUid); + ErrCode ret = InitReminderRequest(tarReminder, bundleName, callingUid); + if (ret != ERR_OK) { + return ret; + } + NotificationBundleOption bundleOption(tarReminder->GetBundleName(), tarReminder->GetUid()); bool allowedNotify = false; - ErrCode result = IN_PROCESS_CALL(NotificationHelper::IsAllowedNotify(bundleOption, allowedNotify)); - if (!tarReminder->IsSystemApp() && (result != ERR_OK || !allowedNotify)) { - ANSR_LOGW("The application does not request enable notification"); + ret = IN_PROCESS_CALL(NotificationHelper::IsAllowedNotify(bundleOption, allowedNotify)); + if (!tarReminder->IsSystemApp() && (ret != ERR_OK || !allowedNotify)) { + ANSR_LOGW("The application does not request enable notification."); return ERR_REMINDER_NOTIFICATION_NOT_ENABLE; } auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { return ERR_NO_INIT; } - int32_t ret = rdm->PublishReminder(tarReminder, callingUid); + ret = rdm->PublishReminder(tarReminder, callingUid); if (ret == ERR_OK) { reminderId = tarReminder->GetReminderId(); ChangeReminderAgentLoadConfig(REMINDER_AGENT_SERVICE_LOAD_STATE); @@ -124,89 +107,19 @@ ErrCode ReminderAgentService::PublishReminder(const ReminderRequest &reminder, i return ret; } -sptr ReminderAgentService::CreateTarReminderRequest(const ReminderRequest &reminder) -{ - sptr tarReminder = nullptr; - switch (reminder.GetReminderType()) { - case (ReminderRequest::ReminderType::TIMER): { - ANSR_LOGI("Publish timer"); - ReminderRequestTimer &timer = (ReminderRequestTimer &)reminder; - tarReminder = new (std::nothrow) ReminderRequestTimer(timer); - break; - } - case (ReminderRequest::ReminderType::ALARM): { - ANSR_LOGI("Publish alarm"); - ReminderRequestAlarm &alarm = (ReminderRequestAlarm &)reminder; - tarReminder = new (std::nothrow) ReminderRequestAlarm(alarm); - break; - } - case (ReminderRequest::ReminderType::CALENDAR): { - ANSR_LOGI("Publish calendar"); - ReminderRequestCalendar &calendar = (ReminderRequestCalendar &)reminder; - tarReminder = new (std::nothrow) ReminderRequestCalendar(calendar); - break; - } - default: { - ANSR_LOGW("PublishReminder fail."); - } - } - return tarReminder; -} - -ErrCode ReminderAgentService::InitReminderRequest(sptr& tarReminder, - const std::string& bundle, const int32_t callingUid) -{ - ANSR_LOGD("is system app: %{public}d", ReminderAccessTokenHelper::IsSystemApp()); - tarReminder->SetSystemApp(ReminderAccessTokenHelper::IsSystemApp()); - tarReminder->InitCreatorBundleName(bundle); - tarReminder->InitCreatorUid(callingUid); - if (tarReminder->GetWantAgentInfo() == nullptr || tarReminder->GetMaxScreenWantAgentInfo() == nullptr) { - ANSR_LOGE("wantagent info is nullptr"); - return ERR_ANS_INVALID_PARAM; - } - std::string wantAgentName = tarReminder->GetWantAgentInfo()->pkgName; - std::string msWantAgentName = tarReminder->GetMaxScreenWantAgentInfo()->pkgName; - if (wantAgentName != msWantAgentName && wantAgentName != "" && msWantAgentName != "") { - ANSR_LOGE("wantAgentName is not same to msWantAgentName, wantAgentName:%{public}s, msWantAgentName:%{public}s", - wantAgentName.c_str(), msWantAgentName.c_str()); - return ERR_ANS_INVALID_PARAM; - } - std::string bundleName = bundle; - if (wantAgentName != bundle && wantAgentName != "") { - ANSR_LOGI("Set agent reminder, bundle:%{public}s, wantAgentName:%{public}s", bundle.c_str(), - wantAgentName.c_str()); - bundleName = wantAgentName; - } else if (msWantAgentName != bundle && msWantAgentName != "") { - ANSR_LOGI("Set agent reminder, bundle:%{public}s, msWantAgentName:%{public}s", bundle.c_str(), - msWantAgentName.c_str()); - bundleName = msWantAgentName; - } - tarReminder->InitBundleName(bundleName); - int32_t activeUserId = -1; - if (ReminderOsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { - ANSR_LOGE("failed to get active user id"); - return ERR_ANS_INVALID_BUNDLE; - } - tarReminder->InitUserId(activeUserId); - tarReminder->InitUid(ReminderBundleManagerHelper::GetInstance()->GetDefaultUidByBundleName( - tarReminder->GetBundleName(), tarReminder->GetUserId())); - return ERR_OK; -} - ErrCode ReminderAgentService::CancelReminder(const int32_t reminderId) { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("Cancel Reminder"); + ANSR_LOGD("call."); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } - int32_t callingUid = IPCSkeleton::GetCallingUid(); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { return ERR_NO_INIT; } - ErrCode result = rdm->CancelReminder(reminderId, callingUid); + ErrCode result = rdm->CancelReminder(reminderId, IPCSkeleton::GetCallingUid()); TryPostDelayUnloadTask(DELAY_TIME); return result; } @@ -214,42 +127,39 @@ ErrCode ReminderAgentService::CancelReminder(const int32_t reminderId) ErrCode ReminderAgentService::CancelAllReminders() { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("Cancel all reminders"); + ANSR_LOGD("call."); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } - int32_t callingUid = IPCSkeleton::GetCallingUid(); - std::string bundleName = GetClientBundleName(); - int32_t userId = -1; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { return ERR_NO_INIT; } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + std::string bundleName = GetClientBundleName(callingUid); + int32_t userId = -1; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); ErrCode result = rdm->CancelAllReminders(bundleName, userId, callingUid); TryPostDelayUnloadTask(DELAY_TIME); return result; } -ErrCode ReminderAgentService::GetValidReminders(std::vector &reminders) +ErrCode ReminderAgentService::GetValidReminders(std::vector& reminders) { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("GetValidReminders"); + ANSR_LOGD("call."); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } - - reminders.clear(); - int32_t callingUid = IPCSkeleton::GetCallingUid(); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { return ERR_NO_INIT; } - rdm->GetValidReminders(callingUid, reminders); - ANSR_LOGD("Valid reminders size=%{public}zu", reminders.size()); + reminders.clear(); + rdm->GetValidReminders(IPCSkeleton::GetCallingUid(), reminders); TryPostDelayUnloadTask(DELAY_TIME); return ERR_OK; } @@ -257,18 +167,16 @@ ErrCode ReminderAgentService::GetValidReminders(std::vectorAddExcludeDate(reminderId, date, callingUid); + ErrCode result = rdm->AddExcludeDate(reminderId, date, IPCSkeleton::GetCallingUid()); TryPostDelayUnloadTask(DELAY_TIME); return result; } @@ -276,19 +184,16 @@ ErrCode ReminderAgentService::AddExcludeDate(const int32_t reminderId, const int ErrCode ReminderAgentService::DelExcludeDates(const int32_t reminderId) { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("Del Exclude Dates"); + ANSR_LOGD("call."); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } - - int32_t callingUid = IPCSkeleton::GetCallingUid(); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { - ANSR_LOGW("Reminder data manager not init!"); return ERR_NO_INIT; } - ErrCode result = rdm->DelExcludeDates(reminderId, callingUid); + ErrCode result = rdm->DelExcludeDates(reminderId, IPCSkeleton::GetCallingUid()); TryPostDelayUnloadTask(DELAY_TIME); return result; } @@ -296,18 +201,16 @@ ErrCode ReminderAgentService::DelExcludeDates(const int32_t reminderId) ErrCode ReminderAgentService::GetExcludeDates(const int32_t reminderId, std::vector& dates) { HITRACE_METER_NAME(HITRACE_TAG_OHOS, __PRETTY_FUNCTION__); - ANSR_LOGI("Get Exclude Dates"); + ANSR_LOGD("call."); if (!CheckReminderPermission()) { - ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER"); + ANSR_LOGW("Permission denied: ohos.permission.PUBLISH_AGENT_REMINDER."); return ERR_REMINDER_PERMISSION_DENIED; } - int32_t callingUid = IPCSkeleton::GetCallingUid(); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { - ANSR_LOGW("Reminder data manager not init!"); return ERR_NO_INIT; } - ErrCode result = rdm->GetExcludeDates(reminderId, callingUid, dates); + ErrCode result = rdm->GetExcludeDates(reminderId, IPCSkeleton::GetCallingUid(), dates); TryPostDelayUnloadTask(DELAY_TIME); return result; } @@ -322,7 +225,6 @@ void ReminderAgentService::TryPostDelayUnloadTask(int64_t delayTime) std::lock_guard lock(unloadMutex_); auto rdm = ReminderDataManager::GetInstance(); if (rdm == nullptr) { - ANSR_LOGW("Reminder data manager not init!"); return; } int32_t reminderCount = rdm->QueryActiveReminderCount(); @@ -336,21 +238,20 @@ void ReminderAgentService::TryPostDelayUnloadTask(int64_t delayTime) void ReminderAgentService::PostDelayUnloadTask() { - ANSR_LOGI("do unload task"); + ANSR_LOGI("Do unload task."); ChangeReminderAgentLoadConfig(REMINDER_AGENT_SERVICE_UNLOAD_STATE); auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { - ANSR_LOGE("get samgr failed"); + ANSR_LOGE("Get samgr failed."); return; } int32_t ret = samgrProxy->UnloadSystemAbility(REMINDER_AGENT_SERVICE_ID); if (ret != ERR_OK) { - ANSR_LOGE("remove system ability failed"); - return; + ANSR_LOGE("Remove reminder service failed."); } } -void ReminderAgentService::ChangeReminderAgentLoadConfig(int8_t reminderAgentState) +void ReminderAgentService::ChangeReminderAgentLoadConfig(const int8_t reminderAgentState) { if (reminderAgentState_ != reminderAgentState) { OHOS::SaveStringToFile(REMINDER_AGENT_SERVICE_CONFIG_PATH, std::to_string(reminderAgentState), true); @@ -358,5 +259,106 @@ void ReminderAgentService::ChangeReminderAgentLoadConfig(int8_t reminderAgentSta } } -} // namespace Notification -} // namespace OHOS +sptr ReminderAgentService::CreateReminderRequest(const ReminderRequest& reminder) +{ + sptr tarReminder = nullptr; + switch (reminder.GetReminderType()) { + case ReminderRequest::ReminderType::TIMER: { + ANSR_LOGI("Publish timer."); + ReminderRequestTimer& timer = (ReminderRequestTimer&)reminder; + tarReminder = new (std::nothrow) ReminderRequestTimer(timer); + break; + } + case ReminderRequest::ReminderType::ALARM: { + ANSR_LOGI("Publish alarm."); + ReminderRequestAlarm& alarm = (ReminderRequestAlarm&)reminder; + tarReminder = new (std::nothrow) ReminderRequestAlarm(alarm); + break; + } + case ReminderRequest::ReminderType::CALENDAR: { + ANSR_LOGI("Publish calendar."); + ReminderRequestCalendar& calendar = (ReminderRequestCalendar&)reminder; + tarReminder = new (std::nothrow) ReminderRequestCalendar(calendar); + break; + } + default: { + ANSR_LOGW("PublishReminder fail."); + break; + } + } + return tarReminder; +} + +ErrCode ReminderAgentService::InitReminderRequest(sptr& tarReminder, + const std::string& bundle, const int32_t callingUid) +{ + if (tarReminder->GetWantAgentInfo() == nullptr || tarReminder->GetMaxScreenWantAgentInfo() == nullptr) { + ANSR_LOGE("WantAgentInfo is nullptr."); + return ERR_REMINDER_INVALID_PARAM; + } + std::string wantAgentName = tarReminder->GetWantAgentInfo()->pkgName; + std::string maxWantAgentName = tarReminder->GetMaxScreenWantAgentInfo()->pkgName; + if (wantAgentName != maxWantAgentName && wantAgentName != "" && maxWantAgentName != "") { + ANSR_LOGE("WantAgentName[%{public}s] is not same to maxWantAgentName[%{public}s].", + wantAgentName.c_str(), maxWantAgentName.c_str()); + return ERR_REMINDER_INVALID_PARAM; + } + std::string bundleName = bundle; + if (wantAgentName != bundle && wantAgentName != "") { + ANSR_LOGI("Set agent reminder, bundle:%{public}s, wantAgentName:%{public}s.", bundle.c_str(), + wantAgentName.c_str()); + bundleName = wantAgentName; + } else if (maxWantAgentName != bundle && maxWantAgentName != "") { + ANSR_LOGI("Set agent reminder, bundle:%{public}s, maxWantAgentName:%{public}s.", bundle.c_str(), + maxWantAgentName.c_str()); + bundleName = maxWantAgentName; + } + + int32_t activeUserId = -1; + if (AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(activeUserId) != ERR_OK) { + ANSR_LOGE("Failed to get active user id."); + return ERR_REMINDER_INVALID_PARAM; + } + std::shared_ptr bundleManager = ReminderBundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + ANSR_LOGE("Failed to bundle manager."); + return ERR_REMINDER_INVALID_PARAM; + } + tarReminder->SetSystemApp(IsSystemApp()); + tarReminder->SetBundleName(bundleName); + tarReminder->SetCreatorBundleName(bundle); + tarReminder->SetCreatorUid(callingUid); + tarReminder->SetUserId(activeUserId); + tarReminder->SetUid(bundleManager->GetDefaultUidByBundleName(bundleName, activeUserId)); + return ERR_OK; +} + +inline bool ReminderAgentService::CheckReminderPermission() +{ + Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); + ErrCode result = Security::AccessToken::AccessTokenKit::VerifyAccessToken( + callerToken, "ohos.permission.PUBLISH_AGENT_REMINDER"); + return result == Security::AccessToken::PermissionState::PERMISSION_GRANTED; +} + +inline bool ReminderAgentService::IsSystemApp() +{ + Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); + Security::AccessToken::ATokenTypeEnum type = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken); + if (type != Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) { + return false; + } + uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + +inline std::string ReminderAgentService::GetClientBundleName(const int32_t callingUid) +{ + std::string bundleName; + std::shared_ptr bundleManager = ReminderBundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundleName = bundleManager->GetBundleNameByUid(callingUid); + } + return bundleName; +} +} // namespace OHOS::Notification diff --git a/services/reminder/src/reminder_datashare_helper.cpp b/services/reminder/src/reminder_datashare_helper.cpp index 16f731406..ab14722bf 100644 --- a/services/reminder/src/reminder_datashare_helper.cpp +++ b/services/reminder/src/reminder_datashare_helper.cpp @@ -402,12 +402,12 @@ std::map> ReminderDataShareHelper::CreateReminder void ReminderDataShareHelper::InitNormalInfo(sptr& reminder) { reminder->SetRingDuration(0); - reminder->InitUserId(curUserId_); - reminder->InitUid(uid_); - reminder->InitCreatorUid(dataUid_); + reminder->SetUserId(curUserId_); + reminder->SetUid(uid_); + reminder->SetCreatorUid(dataUid_); reminder->SetShare(true); - reminder->InitBundleName(ReminderCalendarShareTable::NAME); - reminder->InitCreatorBundleName(ReminderCalendarShareTable::DATA_NAME); + reminder->SetBundleName(ReminderCalendarShareTable::NAME); + reminder->SetCreatorBundleName(ReminderCalendarShareTable::DATA_NAME); reminder->SetSystemApp(true); reminder->SetTapDismissed(true); } diff --git a/services/reminder/src/reminder_store_strategy.cpp b/services/reminder/src/reminder_store_strategy.cpp index ee5e93245..64ec82b85 100644 --- a/services/reminder/src/reminder_store_strategy.cpp +++ b/services/reminder/src/reminder_store_strategy.cpp @@ -116,11 +116,11 @@ void ReminderStrategy::RecoverIdFromOldVersion(sptr& reminder, int32_t userId = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::USER_ID, userId); - reminder->InitUserId(userId); + reminder->SetUserId(userId); int32_t uid = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::UID, uid); - reminder->InitUid(uid); + reminder->SetUid(uid); int32_t reminderType = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REMINDER_TYPE, reminderType); @@ -148,7 +148,7 @@ void ReminderStrategy::RecoverContextFromOldVersion(sptr& remin { std::string bundleName; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::PKG_NAME, bundleName); - reminder->InitBundleName(bundleName); + reminder->SetBundleName(bundleName); std::string title; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::TITLE, title); @@ -176,7 +176,7 @@ void ReminderStrategy::RecoverContextFromOldVersion(sptr& remin std::string creatorBundleName; ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CREATOR_BUNDLE_NAME, creatorBundleName); - reminder->InitCreatorBundleName(creatorBundleName); + reminder->SetCreatorBundleName(creatorBundleName); } void ReminderStrategy::RecoverFromOldVersion(sptr& reminder, @@ -265,11 +265,11 @@ void ReminderStrategy::RecoverIdFromDb(sptr& reminder, int32_t userId = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::USER_ID, userId); - reminder->InitUserId(userId); + reminder->SetUserId(userId); int32_t uid = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::UID, uid); - reminder->InitUid(uid); + reminder->SetUid(uid); int32_t reminderType = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::REMINDER_TYPE, reminderType); @@ -293,7 +293,7 @@ void ReminderStrategy::RecoverIdFromDb(sptr& reminder, int32_t creatorUid; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CREATOR_UID, creatorUid); - reminder->InitCreatorUid(creatorUid); + reminder->SetCreatorUid(creatorUid); int32_t titleResourceId = 0; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TITLE_RESOURCE_ID, titleResourceId); @@ -319,7 +319,7 @@ void ReminderStrategy::RecoverContextFromDb(sptr& reminder, { std::string bundleName; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::PACKAGE_NAME, bundleName); - reminder->InitBundleName(bundleName); + reminder->SetBundleName(bundleName); std::string customButtonUri; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CUSTOM_BUTTON_URI, customButtonUri); @@ -347,7 +347,7 @@ void ReminderStrategy::RecoverContextFromDb(sptr& reminder, std::string creatorBundleName; ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CREATOR_BUNDLE_NAME, creatorBundleName); - reminder->InitCreatorBundleName(creatorBundleName); + reminder->SetCreatorBundleName(creatorBundleName); } void ReminderStrategy::RecoverFromDb(sptr& reminder, diff --git a/services/reminder/test/unittest/BUILD.gn b/services/reminder/test/unittest/BUILD.gn index 300fd3cc8..485e6bf73 100644 --- a/services/reminder/test/unittest/BUILD.gn +++ b/services/reminder/test/unittest/BUILD.gn @@ -30,7 +30,6 @@ ohos_unittest("reminder_unit_test") { defines = [] sources = [ - "mock/mock_access_token_helper.cpp", "mock/mock_bundle_manager_helper.cpp", "mock/mock_ipc.cpp", "reminder_data_manager_test.cpp", @@ -131,13 +130,12 @@ ohos_unittest("reminder_service_publish_test") { sources = [ "${test_path}/mock/mock_tokenid_kit.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_ipc.cpp", "mock/mock_os_account_manager_annex.cpp", - "reminder_service_test/reminder_service_publish_test.cpp", + "reminder_service_test.cpp", ] deps = [ @@ -201,63 +199,6 @@ ohos_unittest("reminder_service_publish_test") { part_name = "${component_name}" } -ohos_unittest("reminder_access_token_helper_test") { - module_out_path = module_output_path - include_dirs = [ - ".", - "include", - "/${services_path}/reminder/include", - "${services_path}/reminder/test/unittest/mock/include", - ] - - sources = [ - "${services_path}/reminder/src/reminder_access_token_helper.cpp", - "${services_path}/reminder/test/unittest/mock/mock_accesstoken_kit.cpp", - "${test_path}/mock/mock_tokenid_kit.cpp", - "access_token_helper_test/access_token_helper_test.cpp", - ] - - deps = [ "${frameworks_module_reminder_path}:reminder_innerkits" ] - - external_deps = [ - "ability_base:want", - "ability_base:zuri", - "ability_runtime:abilitykit_native", - "ability_runtime:app_manager", - "ability_runtime:wantagent_innerkits", - "access_token:libaccesstoken_sdk", - "access_token:libtokenid_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "common_event_service:cesfwk_innerkits", - "eventhandler:libeventhandler", - "hilog:libhilog", - "hitrace:hitrace_meter", - "hitrace:libhitracechain", - "image_framework:image_native", - "init:libbegetutil", - "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 (player_framework) { - external_deps += [ "player_framework:media_client" ] - defines = [ "PLAYER_FRAMEWORK_ENABLE" ] - } - cflags = [ - "-Dprivate=public", - "-Dprotected=public", - ] - subsystem_name = "${subsystem_name}" - part_name = "${component_name}" -} - ohos_unittest("reminder_bundle_manager_helper_branch_test") { module_out_path = module_output_path include_dirs = [ @@ -389,7 +330,6 @@ ohos_unittest("reminder_service_test") { sources = [ "${test_path}/mock/mock_tokenid_kit.cpp", - "mock/mock_access_token_helper.cpp", "mock/mock_accesstoken_kit.cpp", "mock/mock_bundle_manager_helper.cpp", "mock/mock_bundle_mgr.cpp", @@ -547,7 +487,6 @@ ohos_unittest("reminder_agent_service_ability_test") { group("unittest") { testonly = true deps = [ - ":reminder_access_token_helper_test", ":reminder_bundle_manager_helper_branch_test", ":reminder_os_account_manager_helper_test", ":reminder_service_publish_test", diff --git a/services/reminder/test/unittest/access_token_helper_test/access_token_helper_test.cpp b/services/reminder/test/unittest/access_token_helper_test/access_token_helper_test.cpp deleted file mode 100644 index 0109d29ad..000000000 --- a/services/reminder/test/unittest/access_token_helper_test/access_token_helper_test.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2024 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 -#include "accesstoken_kit.h" -#include "reminder_access_token_helper.h" -#include "ans_log_wrapper.h" -#include "ipc_skeleton.h" -#include "mock_accesstoken_kit.h" -using namespace testing::ext; -using namespace OHOS::Security::AccessToken; - -namespace OHOS { -namespace Notification { -class AccessTokenHelperTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp() override; - void TearDown() override; - - std::shared_ptr stub_; -}; - -void AccessTokenHelperTest::SetUpTestCase() -{ -} - -void AccessTokenHelperTest::TearDownTestCase() -{ -} - -void AccessTokenHelperTest::SetUp() -{ - stub_ = std::make_shared(); -} - -void AccessTokenHelperTest::TearDown() -{ -} - -/** - * @tc.number : AccessTokenHelperTest - * @tc.name : VerifyNativeToken_00100 - * @tc.desc : VerifyNativeToken success - */ -HWTEST_F(AccessTokenHelperTest, VerifyNativeToken_00100, Function | SmallTest | Level1) -{ - AccessTokenID tokenID = 0; - MockAccesstokenKit::MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); - EXPECT_TRUE(stub_->VerifyNativeToken(tokenID)); -} - -/** - * @tc.number : AccessTokenHelperTest - * @tc.name : IsSystemApp_00100 - * @tc.desc : IsSystemApp Token Type TOKEN_NATIVE - */ -HWTEST_F(AccessTokenHelperTest, IsSystemApp_00100, Function | SmallTest | Level1) -{ - MockAccesstokenKit::MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); - EXPECT_FALSE(stub_->IsSystemApp()); -} - -/** - * @tc.number : AccessTokenHelperTest - * @tc.name : IsSystemApp_00200 - * @tc.desc : IsSystemApp Token Type TOKEN_HAP - */ -HWTEST_F(AccessTokenHelperTest, IsSystemApp_00200, Function | SmallTest | Level1) -{ - MockAccesstokenKit::MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); - EXPECT_TRUE(stub_->IsSystemApp()); -} - -} // namespace Notification -} // namespace OHOS diff --git a/services/reminder/test/unittest/mock/include/mock_accesstoken_kit.h b/services/reminder/test/unittest/mock/include/mock_accesstoken_kit.h index 4a76b9f60..93c22d6d0 100644 --- a/services/reminder/test/unittest/mock/include/mock_accesstoken_kit.h +++ b/services/reminder/test/unittest/mock/include/mock_accesstoken_kit.h @@ -13,25 +13,18 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_MOCK_ACCESSTOKEN_KIT_H -#define BASE_NOTIFICATION_MOCK_ACCESSTOKEN_KIT_H +#ifndef BASE_NOTIFICATION_REMINDER_MOCK_ACCESSTOKEN_KIT_H +#define BASE_NOTIFICATION_REMINDER_MOCK_ACCESSTOKEN_KIT_H -#include "accesstoken_kit.h" -#include "ans_log_wrapper.h" -#include "reminder_ut_constant.h" -#include "ipc_skeleton.h" +#include -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class MockAccesstokenKit { public: - static void MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum mockRet); - static void MockDlpType(Security::AccessToken::DlpType mockRet); - static void MockApl(Security::AccessToken::ATokenAplEnum mockRet); - static void MockIsVerfyPermisson(bool isVerify); - static void MockIsSystemApp(bool isSystemApp); + static void MockGetTokenTypeFlag(const int32_t flag); + static void MockIsVerifyPermisson(const bool isVerify); + static void MockIsSystemApp(const bool isSystemApp); }; -} // namespace AppExecFwk -} // namespace OHOS +} // namespace OHOS::Notification -#endif // BASE_NOTIFICATION_MOCK_ACCESSTOKEN_KIT_H +#endif // BASE_NOTIFICATION_REMINDER_MOCK_ACCESSTOKEN_KIT_H diff --git a/services/reminder/include/reminder_access_token_helper.h b/services/reminder/test/unittest/mock/include/mock_notification_helper.h similarity index 44% rename from services/reminder/include/reminder_access_token_helper.h rename to services/reminder/test/unittest/mock/include/mock_notification_helper.h index ea0afa225..9ab34c3f3 100644 --- a/services/reminder/include/reminder_access_token_helper.h +++ b/services/reminder/test/unittest/mock/include/mock_notification_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2024 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 @@ -13,22 +13,17 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_ACCESS_TOKEN_HELPER_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_ACCESS_TOKEN_HELPER_H +#ifndef BASE_NOTIFICATION_REMINDER_MOCK_NOTIFICATION_HELPER_H +#define BASE_NOTIFICATION_REMINDER_MOCK_NOTIFICATION_HELPER_H -#include -#include "refbase.h" +#include -#include "accesstoken_kit.h" - -namespace OHOS { -namespace Notification { -class ReminderAccessTokenHelper { +namespace OHOS::Notification { +class MockNotificationHelper { public: - static bool IsSystemApp(); - static bool VerifyNativeToken(const OHOS::Security::AccessToken::AccessTokenID &callerToken); -private: + static void MockIsAllowUseReminder(const bool isAllowUseReminder); + static void MockIsAllowedNotify(const bool isAllowedNotify, const int32_t ret); }; -} // namespace Notification -} // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_ACCESS_TOKEN_HELPER_H \ No newline at end of file +} // namespace OHOS::Notification + +#endif // BASE_NOTIFICATION_REMINDER_MOCK_NOTIFICATION_HELPER_H diff --git a/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h new file mode 100644 index 000000000..902b47f9b --- /dev/null +++ b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef BASE_NOTIFICATION_REMINDER_MOCK_REMINDER_DATA_MANAGER_H +#define BASE_NOTIFICATION_REMINDER_MOCK_REMINDER_DATA_MANAGER_H + +#include + +namespace OHOS::Notification { +class MockReminderDataManager { +public: + static void MockPublishReminder(const int32_t ret); +}; +} // namespace OHOS::Notification + +#endif // BASE_NOTIFICATION_REMINDER_MOCK_REMINDER_DATA_MANAGER_H diff --git a/services/reminder/test/unittest/mock/mock_accesstoken_kit.cpp b/services/reminder/test/unittest/mock/mock_accesstoken_kit.cpp index d382290da..4350ea2f7 100644 --- a/services/reminder/test/unittest/mock/mock_accesstoken_kit.cpp +++ b/services/reminder/test/unittest/mock/mock_accesstoken_kit.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2024 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 @@ -15,66 +15,44 @@ #include "mock_accesstoken_kit.h" -using namespace OHOS::Security::AccessToken; -namespace OHOS { -namespace Notification { +#include "tokenid_kit.h" +#include "accesstoken_kit.h" + +namespace OHOS::Notification { namespace { -ATokenTypeEnum g_mockGetTokenTypeFlagRet = ATokenTypeEnum::TOKEN_INVALID; -DlpType g_mockDlpType = DlpType::DLP_COMMON; -ATokenAplEnum g_mockApl = ATokenAplEnum::APL_NORMAL; -bool g_isSystemApp = true; +int32_t g_mockTokenTypeFlag = static_cast(Security::AccessToken::ATokenTypeEnum::TOKEN_INVALID); bool g_mockVerfyPermisson = true; +bool g_isSystemApp = true; } -void MockAccesstokenKit::MockGetTokenTypeFlag(ATokenTypeEnum mockRet) -{ - g_mockGetTokenTypeFlagRet = mockRet; -} -void MockAccesstokenKit::MockDlpType(DlpType mockRet) +void MockAccesstokenKit::MockGetTokenTypeFlag(const int32_t flag) { - g_mockDlpType = mockRet; -} -void MockAccesstokenKit::MockApl(ATokenAplEnum mockRet) -{ - g_mockApl = mockRet; + g_mockTokenTypeFlag = flag; } -void MockAccesstokenKit::MockIsVerfyPermisson(bool isVerify) +void MockAccesstokenKit::MockIsVerifyPermisson(const bool isVerify) { g_mockVerfyPermisson = isVerify; } -void MockAccesstokenKit::MockIsSystemApp(bool isSystemApp) +void MockAccesstokenKit::MockIsSystemApp(const bool isSystemApp) { g_isSystemApp = isSystemApp; } } -} -namespace OHOS { -namespace Security { -namespace AccessToken { + +namespace OHOS::Security::AccessToken { int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) { - if (!Notification::g_mockVerfyPermisson) { - return PERMISSION_DENIED; - } - - if (tokenID == Notification::NON_NATIVE_TOKEN) { - return PERMISSION_DENIED; - } - return PERMISSION_GRANTED; + return Notification::g_mockVerfyPermisson ? PERMISSION_GRANTED : PERMISSION_DENIED; } ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) { - return Notification::g_mockGetTokenTypeFlagRet; + return static_cast(Notification::g_mockTokenTypeFlag); } -int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& info) +bool TokenIdKit::IsSystemAppByFullTokenID(uint64_t tokenId) { - info.dlpType = Notification::g_mockDlpType; - info.apl = Notification::g_mockApl; - return 0; + return Notification::g_isSystemApp; } -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +} // namespace OHOS::Security::AccessToken diff --git a/services/reminder/src/reminder_access_token_helper.cpp b/services/reminder/test/unittest/mock/mock_notification_helper.cpp similarity index 36% rename from services/reminder/src/reminder_access_token_helper.cpp rename to services/reminder/test/unittest/mock/mock_notification_helper.cpp index 4410cb0e6..c4a2fd698 100644 --- a/services/reminder/src/reminder_access_token_helper.cpp +++ b/services/reminder/test/unittest/mock/mock_notification_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2024 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 @@ -13,34 +13,37 @@ * limitations under the License. */ -#include "reminder_access_token_helper.h" +#include "mock_notification_helper.h" -#include "ans_log_wrapper.h" -#include "ipc_skeleton.h" -#include "tokenid_kit.h" -#include "reminder_utils.h" +#include "notification_helper.h" -using namespace OHOS::Security::AccessToken; +namespace OHOS::Notification { +namespace { +bool g_mockIsAllowUseReminder = true; +bool g_mockIsAllowedNotify = true; +int32_t g_mockIsAllowedNotifyRet = 0; +} + +void MockNotificationHelper::MockIsAllowUseReminder(const bool isAllowUseReminder) +{ + g_mockIsAllowUseReminder = isAllowUseReminder; +} + +void MockNotificationHelper::MockIsAllowedNotify(const bool isAllowedNotify, const int32_t ret) +{ + g_mockIsAllowedNotify = isAllowedNotify; + g_mockIsAllowedNotifyRet = ret; +} -namespace OHOS { -namespace Notification { -bool ReminderAccessTokenHelper::IsSystemApp() +ErrCode NotificationHelper::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - ATokenTypeEnum type = AccessTokenKit::GetTokenTypeFlag(tokenId); - if (type == ATokenTypeEnum::TOKEN_HAP) { - uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID(); - if (Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) { - return true; - } - } - return false; + isAllowUseReminder = g_mockIsAllowUseReminder; + return 0; } -bool ReminderAccessTokenHelper::VerifyNativeToken(const AccessTokenID &callerToken) +ErrCode NotificationHelper::IsAllowedNotify(const NotificationBundleOption& bundleOption, bool& allowed) { - ATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken); - return (tokenType == ATokenTypeEnum::TOKEN_NATIVE); + allowed = g_mockIsAllowedNotify; + return g_mockIsAllowedNotifyRet; } -} // namespace Notification -} // namespace OHOS +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/mock_access_token_helper.cpp b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp similarity index 49% rename from services/reminder/test/unittest/mock/mock_access_token_helper.cpp rename to services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp index 5bc5dfd60..0cc7e7b85 100644 --- a/services/reminder/test/unittest/mock/mock_access_token_helper.cpp +++ b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2024 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 @@ -13,26 +13,23 @@ * limitations under the License. */ -#include "reminder_access_token_helper.h" -#include "reminder_ut_constant.h" -#include "ipc_skeleton.h" +#include "mock_reminder_data_manager.h" -namespace OHOS { -namespace Notification { +#include "reminder_data_manager.h" -bool ReminderAccessTokenHelper::VerifyNativeToken(const Security::AccessToken::AccessTokenID &callerToken) -{ - return callerToken == NATIVE_TOKEN; +namespace OHOS::Notification { +namespace { +int32_t g_mockPublishReminderRet = 0; } -bool ReminderAccessTokenHelper::IsSystemApp() +void MockReminderDataManager::MockPublishReminder(const int32_t ret) { - Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - if (tokenId == NATIVE_TOKEN) { - return true; - } - return false; + g_mockPublishReminderRet = ret; } -} // namespace Notification -} // namespace OHOS \ No newline at end of file +ErrCode ReminderDataManager::PublishReminder(const sptr& reminder, + const int32_t callingUid) +{ + return g_mockPublishReminderRet; +} +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/reminder_data_manager_test.cpp b/services/reminder/test/unittest/reminder_data_manager_test.cpp index 9221b31d1..4a0bd0dc7 100644 --- a/services/reminder/test/unittest/reminder_data_manager_test.cpp +++ b/services/reminder/test/unittest/reminder_data_manager_test.cpp @@ -70,18 +70,18 @@ HWTEST_F(ReminderDataManagerTest, GetVaildReminders_00001, Level1) manager->store_->Init(); int32_t callingUid = 98765; sptr reminder1 = new ReminderRequestTimer(static_cast(50)); - reminder1->InitCreatorBundleName("test_getvalid"); - reminder1->InitCreatorUid(callingUid); - reminder1->InitBundleName("test_getvalid"); - reminder1->InitUid(callingUid); + reminder1->SetCreatorBundleName("test_getvalid"); + reminder1->SetCreatorUid(callingUid); + reminder1->SetBundleName("test_getvalid"); + reminder1->SetUid(callingUid); reminder1->SetExpired(false); manager->PublishReminder(reminder1, callingUid); sptr reminder2 = new ReminderRequestTimer(51); - reminder2->InitCreatorBundleName("test_getvalid"); - reminder2->InitCreatorUid(callingUid); - reminder2->InitBundleName("test_getvalid"); - reminder2->InitUid(callingUid); + reminder2->SetCreatorBundleName("test_getvalid"); + reminder2->SetCreatorUid(callingUid); + reminder2->SetBundleName("test_getvalid"); + reminder2->SetUid(callingUid); reminder2->SetExpired(true); manager->PublishReminder(reminder2, callingUid); @@ -451,7 +451,7 @@ HWTEST_F(ReminderDataManagerTest, ReminderDataManagerTest_016, Level1) std::shared_ptr buttonDataShareUpdate1 = std::make_shared(); reminder1->SetSystemApp(true); - reminder1->InitUserId(100); + reminder1->SetUserId(100); buttonDataShareUpdate1->uri = "datashareTest://com.acts.dataShareTest"; buttonDataShareUpdate1->equalTo = "namestringli" "iddouble3.0statusbooltrue"; @@ -672,12 +672,12 @@ HWTEST_F(ReminderDataManagerTest, IsBelongToSameAppTest_001, Level1) HWTEST_F(ReminderDataManagerTest, CheckIsSameAppTest_001, Level1) { sptr reminder = new ReminderRequestTimer(10); - reminder->InitCreatorBundleName("test"); + reminder->SetCreatorBundleName("test"); int32_t callingUid = 100; - reminder->InitCreatorUid(callingUid); + reminder->SetCreatorUid(callingUid); EXPECT_TRUE(manager->CheckIsSameApp(reminder, callingUid)); - reminder->InitCreatorUid(-1); + reminder->SetCreatorUid(-1); EXPECT_FALSE(manager->CheckIsSameApp(reminder, callingUid)); } @@ -745,14 +745,14 @@ HWTEST_F(ReminderDataManagerTest, ExcludeDate_0001, Level1) EXPECT_TRUE(ret == ERR_REMINDER_NOT_EXIST); sptr reminder = new ReminderRequestCalendar(10); - reminder->InitCreatorBundleName("test1"); - reminder->InitUserId(-1); + reminder->SetCreatorBundleName("test1"); + reminder->SetUserId(-1); reminder->reminderId_ = 100; manager->reminderVector_.push_back(reminder); result = manager->CheckExcludeDateParam(100, callingUid); EXPECT_TRUE(result == nullptr); - reminder->InitCreatorBundleName("test"); + reminder->SetCreatorBundleName("test"); reminder->reminderType_ = ReminderRequest::ReminderType::TIMER; result = manager->CheckExcludeDateParam(100, callingUid); EXPECT_TRUE(result == nullptr); @@ -818,11 +818,11 @@ HWTEST_F(ReminderDataManagerTest, CancelAllReminders_00001, Level1) HWTEST_F(ReminderDataManagerTest, IsMatched_00001, Level1) { sptr reminder = new ReminderRequestTimer(50); - reminder->InitCreatorBundleName("test_IsMatched"); - reminder->InitCreatorUid(98765); - reminder->InitBundleName("test_IsMatched"); - reminder->InitUid(98765); - reminder->InitUserId(100); + reminder->SetCreatorBundleName("test_IsMatched"); + reminder->SetCreatorUid(98765); + reminder->SetBundleName("test_IsMatched"); + reminder->SetUid(98765); + reminder->SetUserId(100); bool ret = manager->IsMatched(reminder, 101, 98765, true); EXPECT_EQ(ret, false); ret = manager->IsMatched(reminder, 100, 98765, false); diff --git a/services/reminder/test/unittest/reminder_service_test.cpp b/services/reminder/test/unittest/reminder_service_test.cpp index 5bf778d34..35aaebf2d 100644 --- a/services/reminder/test/unittest/reminder_service_test.cpp +++ b/services/reminder/test/unittest/reminder_service_test.cpp @@ -13,33 +13,17 @@ * limitations under the License. */ -#include "errors.h" -#include -#include -#include -#include - #include "gtest/gtest.h" -#include - #include "reminder_agent_service.h" -#include "ans_const_define.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "reminder_ut_constant.h" -#include "iremote_object.h" -#include "accesstoken_kit.h" -#include "mock_ipc_skeleton.h" + +#include #include "mock_accesstoken_kit.h" -#include "mock_os_account_manager.h" -#include "mock_bundle_mgr.h" +#include "mock_notification_helper.h" +#include "mock_reminder_data_manager.h" using namespace testing::ext; -using namespace OHOS::Media; - -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class ReminderAgentServiceTest : public testing::Test { public: static void SetUpTestCase(); @@ -48,14 +32,15 @@ public: void TearDown(); private: + void TestAddSlot(NotificationConstant::SlotType type); void TestAddLiveViewSlot(bool isForceControl); void MockSystemApp(); private: - static sptr ReminderAgentService_; + static sptr reminderService_; }; -sptr ReminderAgentServiceTest::ReminderAgentService_ = nullptr; +sptr ReminderAgentServiceTest::reminderService_ = nullptr; void ReminderAgentServiceTest::SetUpTestCase() { @@ -68,10 +53,10 @@ void ReminderAgentServiceTest::SetUp() { GTEST_LOG_(INFO) << "SetUp start"; - ReminderAgentService_ = new (std::nothrow) ReminderAgentService(); + reminderService_ = new (std::nothrow) ReminderAgentService(); IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); - ReminderAgentService_->CancelAllReminders(); + MockAccesstokenKit::MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); GTEST_LOG_(INFO) << "SetUp end"; } @@ -79,7 +64,7 @@ void ReminderAgentServiceTest::SetUp() void ReminderAgentServiceTest::TearDown() { IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); - ReminderAgentService_ = nullptr; + reminderService_ = nullptr; GTEST_LOG_(INFO) << "TearDown"; } @@ -90,99 +75,201 @@ void ReminderAgentServiceTest::MockSystemApp() MockAccesstokenKit::MockIsVerfyPermisson(true); } -inline void SleepForFC() +/** + * @tc.number : ReminderAgentServiceTest_001 + * @tc.name : ReminderAgentServiceTest_001 + * @tc.desc : Test GetInstance function + * @tc.require : issueI5S4VP + */ +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_001, Function | SmallTest | Level1) { - // For Reminder Flow Control - std::this_thread::sleep_for(std::chrono::seconds(1)); + ASSERT_NE(ReminderAgentService::GetInstance(), nullptr); + ASSERT_NE(ReminderAgentService::GetInstance(), nullptr); } /** - * @tc.number : ReminderAgentServiceTest_13500 - * @tc.name : Reminder_GetValidReminders_0100 - * @tc.desc : Test GetValidReminders function when the result is ERR_NO_INIT + * @tc.number : ReminderAgentServiceTest_002 + * @tc.name : ReminderAgentServiceTest_002 + * @tc.desc : Test PublishReminder function * @tc.require : issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_13500, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_002, Function | SmallTest | Level1) { - std::vector reminders; - ASSERT_EQ(ReminderAgentService_->GetValidReminders(reminders), (int)ERR_NO_INIT); + // test CreateReminderRequest + int32_t reminderId = 0; + ReminderRequest reminder; + ASSERT_EQ(reminderService_->PublishReminder(reminder, reminderId), ERR_REMINDER_INVALID_PARAM); + + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ReminderRequestAlarm alarm; + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); + + // test AllowUseReminder + MockNotificationHelper::MockIsAllowUseReminder(false); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_REMINDER_NUMBER_OVERLOAD); + MockNotificationHelper::MockIsAllowUseReminder(true); + + // test InitReminderRequest + auto wantInfo = alarm.GetWantAgentInfo(); + alarm.SetWantAgentInfo(nullptr); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_REMINDER_INVALID_PARAM); + alarm.SetWantAgentInfo(wantInfo); + + // test IsSystemApp and IsAllowedNotify + alarm.SetSystemApp(false); + MockNotificationHelper::MockIsAllowedNotify(false, 1); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_REMINDER_NOTIFICATION_NOT_ENABLE); + MockNotificationHelper::MockIsAllowedNotify(false, 0); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_REMINDER_NOTIFICATION_NOT_ENABLE); + MockNotificationHelper::MockIsAllowedNotify(true, 0); + + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_NO_INIT); + alarm.SetSystemApp(true); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::PublishReminder; + MockReminderDataManager::MockPublishReminder(1); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), 1); + MockReminderDataManager::MockPublishReminder(0); + ASSERT_EQ(reminderService_->PublishReminder(alarm, reminderId), 0); +} + +/** + * @tc.number : ReminderAgentServiceTest_003 + * @tc.name : ReminderAgentServiceTest_003 + * @tc.desc : Test CancelReminder function + * @tc.require : issueI5S4VP + */ +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_003, Function | SmallTest | Level1) +{ + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->CancelReminder(0), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); + + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->CancelReminder(0), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::CancelReminder; + ASSERT_NE(reminderService_->CancelReminder(0), 1); } /** - * @tc.number : ReminderAgentServiceTest_16900 - * @tc.name : Reminder_GetActiveNotifications_0100 - * @tc.desc : Test function with bundle option is null - * @tc.require : #I60KYN + * @tc.number : ReminderAgentServiceTest_004 + * @tc.name : ReminderAgentServiceTest_004 + * @tc.desc : Test CancelAllReminders function + * @tc.require : issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_16900, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_004, Function | SmallTest | Level1) { - GTEST_LOG_(INFO) << "Reminder_GetActiveNotifications_0100 test start"; + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->CancelAllReminders(), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); - MockBundleMgr::MockIsNonBundleName(true); - MockSystemApp(); - int32_t reminderId = 1; - ASSERT_EQ(ReminderAgentService_->CancelReminder(reminderId), ERR_NO_INIT); + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->CancelAllReminders(), ERR_NO_INIT); + ReminderDataManager::InitInstance(); - ASSERT_EQ(ReminderAgentService_->CancelAllReminders(), ERR_NO_INIT); + // test ReminderDataManager::CancelAllReminders; + ASSERT_NE(reminderService_->CancelAllReminders(), 0); +} +/** + * @tc.number : ReminderAgentServiceTest_004 + * @tc.name : ReminderAgentServiceTest_004 + * @tc.desc : Test GetValidReminders function + * @tc.require : issueI5S4VP + */ +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_004, Function | SmallTest | Level1) +{ + // test CheckReminderPermission std::vector reminders; - ASSERT_EQ(ReminderAgentService_->GetValidReminders(reminders), ERR_NO_INIT); + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->GetValidReminders(reminders), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); - MockBundleMgr::MockIsNonBundleName(false); - GTEST_LOG_(INFO) << "Reminder_GetActiveNotifications_0100 test end"; + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->GetValidReminders(reminders), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::GetValidReminders; + ASSERT_NE(reminderService_->GetValidReminders(reminders), 0); } /** - * @tc.number : AddExcludeDate_00001 - * @tc.name : Test AddExcludeDate - * @tc.desc : Test AddExcludeDate function when the result is ERR_NO_INIT + * @tc.number : ReminderAgentServiceTest_005 + * @tc.name : ReminderAgentServiceTest_005 + * @tc.desc : Test AddExcludeDate function * @tc.require : issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, AddExcludeDate_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_005, Function | SmallTest | Level1) { - int32_t reminderId = 10; - uint64_t time = 124325; - MockAccesstokenKit::MockIsVerfyPermisson(false); - ASSERT_EQ(ReminderAgentService_->AddExcludeDate(reminderId, time), (int)ERR_REMINDER_PERMISSION_DENIED); - MockAccesstokenKit::MockIsVerfyPermisson(true); - MockBundleMgr::MockIsNonBundleName(false); - ASSERT_EQ(ReminderAgentService_->AddExcludeDate(reminderId, time), (int)ERR_NO_INIT); - MockAccesstokenKit::MockIsVerfyPermisson(false); + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->AddExcludeDate(0, 0), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); + + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->AddExcludeDate(0, 0), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::AddExcludeDate; + ASSERT_NE(reminderService_->AddExcludeDate(0, 0), 0); } /** - * @tc.number : DelExcludeDates_00002 - * @tc.name : Test DelExcludeDates - * @tc.desc : Test DelExcludeDates function when the result is ERR_NO_INIT + * @tc.number : ReminderAgentServiceTest_006 + * @tc.name : ReminderAgentServiceTest_006 + * @tc.desc : Test DelExcludeDates function * @tc.require : issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, DelExcludeDates_00002, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_006, Function | SmallTest | Level1) { - int32_t reminderId = 10; - MockAccesstokenKit::MockIsVerfyPermisson(false); - ASSERT_EQ(ReminderAgentService_->DelExcludeDates(reminderId), (int)ERR_REMINDER_PERMISSION_DENIED); - MockAccesstokenKit::MockIsVerfyPermisson(true); - MockBundleMgr::MockIsNonBundleName(false); - ASSERT_EQ(ReminderAgentService_->DelExcludeDates(reminderId), (int)ERR_NO_INIT); - MockAccesstokenKit::MockIsVerfyPermisson(false); + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->DelExcludeDates(0), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); + + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->DelExcludeDates(0), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::DelExcludeDates; + ASSERT_NE(reminderService_->DelExcludeDates(0), 0); } /** - * @tc.number : GetExcludeDates_00001 - * @tc.name : Test GetExcludeDates - * @tc.desc : Test GetExcludeDates function when the result is ERR_NO_INIT + * @tc.number : ReminderAgentServiceTest_006 + * @tc.name : ReminderAgentServiceTest_006 + * @tc.desc : Test DelExcludeDates function * @tc.require : issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, GetExcludeDates_00001, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_006, Function | SmallTest | Level1) { - int32_t reminderId = 10; - std::vector times; - MockAccesstokenKit::MockIsVerfyPermisson(false); - ASSERT_EQ(ReminderAgentService_->GetExcludeDates(reminderId, times), (int)ERR_REMINDER_PERMISSION_DENIED); - MockAccesstokenKit::MockIsVerfyPermisson(true); - MockBundleMgr::MockIsNonBundleName(false); - ASSERT_EQ(ReminderAgentService_->GetExcludeDates(reminderId, times), (int)ERR_NO_INIT); - MockAccesstokenKit::MockIsVerfyPermisson(false); + std::vector dates; + // test CheckReminderPermission + MockAccesstokenKit::MockIsVerifyPermisson(false); + ASSERT_EQ(reminderService_->GetExcludeDates(0, dates), ERR_REMINDER_PERMISSION_DENIED); + MockAccesstokenKit::MockIsVerifyPermisson(true); + + // test ReminderDataManager::GetInstance() + ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; + ASSERT_EQ(reminderService_->GetExcludeDates(0, dates), ERR_NO_INIT); + ReminderDataManager::InitInstance(); + + // test ReminderDataManager::GetExcludeDates; + ASSERT_NE(reminderService_->GetExcludeDates(0, dates), 0); } -} // namespace Notification -} // namespace OHOS +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/reminder_service_test/reminder_service_publish_test.cpp b/services/reminder/test/unittest/reminder_service_test/reminder_service_publish_test.cpp deleted file mode 100644 index bc87dc864..000000000 --- a/services/reminder/test/unittest/reminder_service_test/reminder_service_publish_test.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2021-2024 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 "errors.h" -#include "notification_content.h" -#include "notification_request.h" -#include -#include -#include -#include - -#include "gtest/gtest.h" -#include - -#include "reminder_agent_service.h" -#include "ans_const_define.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "reminder_ut_constant.h" -#include "iremote_object.h" -#include "mock_ipc_skeleton.h" -#include "mock_os_account_manager.h" -#include "mock_accesstoken_kit.h" -#include "mock_bundle_mgr.h" - -#include "reminder_bundle_manager_helper.h" - - -using namespace testing::ext; -using namespace OHOS::Media; - -namespace OHOS { -namespace Notification { - -class ReminderAgentServicePublishTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - -private: - void TestAddSlot(NotificationConstant::SlotType type); - void TestAddLiveViewSlot(bool isForceControl); - void MockSystemApp(); - -private: - static sptr reminderService_; -}; - -sptr ReminderAgentServicePublishTest::reminderService_ = nullptr; - -void ReminderAgentServicePublishTest::SetUpTestCase() -{ - MockOsAccountManager::MockIsOsAccountExists(true); -} - -void ReminderAgentServicePublishTest::TearDownTestCase() {} - -void ReminderAgentServicePublishTest::SetUp() -{ - GTEST_LOG_(INFO) << "SetUp start"; - - reminderService_ = new (std::nothrow) ReminderAgentService(); - IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); - IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); - - MockAccesstokenKit::MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); - GTEST_LOG_(INFO) << "SetUp end"; -} - -void ReminderAgentServicePublishTest::TearDown() -{ - IPCSkeleton::SetCallingUid(SYSTEM_APP_UID); - reminderService_ = nullptr; - GTEST_LOG_(INFO) << "TearDown"; -} - -inline void SleepForFC() -{ - // For ANS Flow Control - std::this_thread::sleep_for(std::chrono::seconds(1)); -} - -void ReminderAgentServicePublishTest::MockSystemApp() -{ - MockAccesstokenKit::MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockAccesstokenKit::MockIsSystemApp(true); - MockAccesstokenKit::MockIsVerfyPermisson(true); -} - -/** - * @tc.number : ReminderAgentServicePublishTest_13200 - * @tc.name : ANS_PublishReminder_0100 - * @tc.desc : Test PublishReminder function - * @tc.require : issueI5S4VP - */ -HWTEST_F(ReminderAgentServicePublishTest, ReminderAgentServicePublishTest_13200, Function | SmallTest | Level1) -{ - int32_t reminderId = 0; - ReminderRequest reminder; - ASSERT_EQ(reminderService_->PublishReminder(reminder, reminderId), ERR_ANS_INVALID_PARAM); -} - -/** - * @tc.number : ReminderAgentServicePublishTest_13300 - * @tc.name : ANS_CancelReminder_0100 - * @tc.desc : Test CancelReminder function when the result is ERR_NO_INIT - * @tc.require : issueI5S4VP - */ -HWTEST_F(ReminderAgentServicePublishTest, ReminderAgentServicePublishTest_13300, Function | SmallTest | Level1) -{ - int32_t reminderId = 1; - ASSERT_EQ(reminderService_->CancelReminder(reminderId), (int)ERR_NO_INIT); -} - -/** - * @tc.number : ReminderAgentServicePublishTest_13400 - * @tc.name : ANS_CancelAllReminders_0100 - * @tc.desc : Test CancelAllReminders function when the result is ERR_NO_INIT - * @tc.require : issueI5S4VP - */ -HWTEST_F(ReminderAgentServicePublishTest, ReminderAgentServicePublishTest_13400, Function | SmallTest | Level1) -{ - ASSERT_EQ(reminderService_->CancelAllReminders(), (int)ERR_NO_INIT); -} - -/** - * @tc.number : ReminderAgentServicePublishTest_17900 - * @tc.name : PublishReminder_1000 - * @tc.desc : Test PublishReminder function. - * @tc.require : #I61RF2 - */ -HWTEST_F(ReminderAgentServicePublishTest, ReminderAgentServicePublishTest_17900, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "GetAppTargetBundle_1000 test start"; - - int32_t reminderId = 1; - ReminderRequest reminder; - ASSERT_NE(reminderService_->PublishReminder(reminder, reminderId), ERR_OK); - - GTEST_LOG_(INFO) << "GetAppTargetBundle_1000 test end"; -} - -/** - * @tc.number : ReminderAgentServicePublishTest_18000 - * @tc.name : PublishReminder_2000 - * @tc.desc : Test PublishReminder function. - * @tc.require : #I61RF2 - */ -HWTEST_F(ReminderAgentServicePublishTest, ReminderAgentServicePublishTest_18000, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test start"; - - MockBundleMgr::MockIsNonBundleName(true); - int32_t reminderId = 1; - ReminderRequest reminder; - ASSERT_NE(reminderService_->PublishReminder(reminder, reminderId), ERR_OK); - MockBundleMgr::MockIsNonBundleName(false); - GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test end"; -} -} // namespace Notification -} // namespace OHOS diff --git a/test/fuzztest/reminderrequest_fuzzer/reminderrequest_fuzzer.cpp b/test/fuzztest/reminderrequest_fuzzer/reminderrequest_fuzzer.cpp index e9bacbe9d..a460a00d2 100644 --- a/test/fuzztest/reminderrequest_fuzzer/reminderrequest_fuzzer.cpp +++ b/test/fuzztest/reminderrequest_fuzzer/reminderrequest_fuzzer.cpp @@ -39,8 +39,8 @@ namespace OHOS { bool enabled = fdp->ConsumeBool(); reminderRequest.SetExpired(enabled); reminderRequest.InitReminderId(); - reminderRequest.InitUserId(reminderId); - reminderRequest.InitUid(reminderId); + reminderRequest.SetUserId(reminderId); + reminderRequest.SetUid(reminderId); reminderRequest.IsExpired(); reminderRequest.IsShowing(); reminderRequest.OnClose(enabled); -- Gitee