From 403fd9584250d2307406210713b22fd6c8bfcaba Mon Sep 17 00:00:00 2001 From: zhengzhuolan Date: Fri, 20 Jun 2025 14:28:24 +0800 Subject: [PATCH] Add code Signed-off-by: zhengzhuolan --- .../include/advanced_notification_service.h | 5 + .../ans/include/notification_preferences.h | 7 + .../include/notification_preferences_info.h | 3 + .../advanced_notification_publish.cpp | 5 + .../advanced_notification_publish_service.cpp | 46 +++++ .../ans/src/advanced_notification_service.cpp | 5 + .../advanced_notification_slot_service.cpp | 3 +- services/ans/src/notification_preferences.cpp | 50 +++++ .../ans/src/notification_preferences_info.cpp | 15 ++ services/ans/src/system_event_observer.cpp | 6 + ...nced_notification_publish_service_test.cpp | 75 ++++++++ .../notification_preferences_info_test.cpp | 37 ++++ .../notification_preferences_test.cpp | 181 ++++++++++++++++++ 13 files changed, 437 insertions(+), 1 deletion(-) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index dcd725510..5b21799c5 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1332,6 +1332,10 @@ public: bool IsDisableNotification(const std::string &bundleName); + bool IsDisableNotificationByKiosk(const std::string &bundleName); + + bool IsDisableNotificationForSaByKiosk(const std::string &bundleName, const sptr &request); + bool IsNeedToControllerByDisableNotification(const sptr &request); void SetAndPublishSubscriberExistFlag(const std::string& deviceType, bool existFlag); @@ -1651,6 +1655,7 @@ private: const bool easyAbroad); void ClearSlotTypeData(const sptr &request, int32_t callingUid, int32_t sourceType); ErrCode RegisterPushCallbackTokenCheck(); + bool IsEnableNotificationByKioskAppTrustList(const std::string &bundleName); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &data) diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 29e10cdaf..2e5f02138 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -503,6 +503,12 @@ public: bool SetBundleRemoveFlag(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, int32_t sourceType); + void SetKioskModeStatus(bool isKioskMode); + + bool IsKioskMode(); + + bool GetkioskAppTrustList(std::vector &kioskAppTrustList); + private: bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo, const sptr &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const; @@ -533,6 +539,7 @@ private: std::shared_ptr preferncesDB_ = nullptr; bool isCachedMirrorNotificationEnabledStatus_ = false; std::vector mirrorNotificationEnabledStatus_ = {}; + bool isKioskMode_ = false; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index 87f75e428..ce9c93cc4 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -324,12 +324,15 @@ public: bool GetDisableNotificationInfo(NotificationDisable ¬ificationDisable); void AddDisableNotificationInfo(const std::string &value); ErrCode GetAllLiveViewEnabledBundles(const int32_t userId, std::vector &bundleOption); + void SetkioskAppTrustList(const std::vector &kioskAppTrustList); + bool GetkioskAppTrustList(std::vector &kioskAppTrustList) const; private: std::map isEnabledAllNotification_; std::map> doNotDisturbDate_; std::map> doNotDisturbProfiles_; std::map infos_; + std::vector kioskAppTrustList_; struct DisableNotificationInfo { int32_t disabled = -1; diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index aa04b8736..52be23cec 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -227,6 +227,11 @@ ErrCode AdvancedNotificationService::PublishNotificationForIndirectProxy(const s .ErrorCode(result).Message("bundle in Disable Notification list, bundleName=" + bundle); return; } + if (IsDisableNotificationByKiosk(bundle)) { + ANS_LOGE("bundle not in kiosk trust list, bundleName=%{public}s", bundle.c_str()); + result = ERR_ANS_REJECTED_WITH_DISABLE_NOTIFICATION; + return; + } if (AssignValidNotificationSlot(record, bundleOption) != ERR_OK) { ANS_LOGE("Can not assign valid slot!"); } diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 64636a809..955d207be 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -781,6 +781,11 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptrGetBundleName().empty() && !(request->GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW && request->IsAgentNotification())) { ErrCode ret = AssignValidNotificationSlot(record, bundleOption); @@ -1066,6 +1071,47 @@ bool AdvancedNotificationService::IsDisableNotification(const std::string &bundl return false; } +bool AdvancedNotificationService::IsDisableNotificationByKiosk(const std::string &bundleName) +{ + bool isKioskMode = NotificationPreferences::GetInstance()->IsKioskMode(); + if (isKioskMode && !IsEnableNotificationByKioskAppTrustList(bundleName)) { + return true; + } + return false; +} + +bool AdvancedNotificationService::IsDisableNotificationForSaByKiosk( + const std::string &bundleName, const sptr &request) +{ + if (request == nullptr) { + ANS_LOGE("request is nullptr"); + return false; + } + bool isAppAgent = false; + if (request->IsAgentNotification() && !bundleName.empty()) { + isAppAgent = true; + } + bool isKioskMode = NotificationPreferences::GetInstance()->IsKioskMode(); + if (isKioskMode && isAppAgent && !IsEnableNotificationByKioskAppTrustList(bundleName)) { + return true; + } + return false; +} + +bool AdvancedNotificationService::IsEnableNotificationByKioskAppTrustList(const std::string &bundleName) +{ + std::vector kioskAppTrustList; + if (NotificationPreferences::GetInstance()->GetkioskAppTrustList(kioskAppTrustList)) { + auto it = std::find(kioskAppTrustList.begin(), kioskAppTrustList.end(), bundleName); + if (it != kioskAppTrustList.end()) { + return true; + } + } else { + ANS_LOGD("no kiosk app trust list has been set up"); + } + return false; +} + bool AdvancedNotificationService::IsNeedToControllerByDisableNotification(const sptr &request) { if (request == nullptr) { diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 77b40d6a5..d476d6a23 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -719,6 +719,11 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification(const sptrGetOwnerBundleName()).c_str()); + result = ERR_ANS_REJECTED_WITH_DISABLE_NOTIFICATION; + return; + } if (record->request->GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW && !LivePublishProcess::GetInstance()->CheckLocalLiveViewSubscribed(record->request, isUpdateByOwner, uid)) { result = ERR_ANS_INVALID_PARAM; diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 8c833accc..602eec5bf 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -977,7 +977,8 @@ ErrCode AdvancedNotificationService::SetAdditionConfig(const std::string &key, c ANS_LOGD("SetAdditionConfig called (%{public}s, %{public}s).", key.c_str(), value.c_str()); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_1); message.Message(" key:" + key + " value" + value); - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER) && + !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_MANAGE_EDM_POLICY)) { ANS_LOGE("Permission denied."); message.ErrorCode(ERR_ANS_PERMISSION_DENIED).Append(" Permission denied"); NotificationAnalyticsUtil::ReportModifyEvent(message); diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index dcff9f9fd..6288106a4 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -19,6 +19,7 @@ #include #include +#include "ability_manager_client.h" #include "access_token_helper.h" #include "ans_const_define.h" #include "ans_inner_errors.h" @@ -26,6 +27,7 @@ #include "ans_trace_wrapper.h" #include "ans_permission_def.h" #include "bundle_manager_helper.h" +#include "in_process_call_wrapper.h" #include "nlohmann/json.hpp" #include "os_account_manager_helper.h" #include "notification_analytics_util.h" @@ -1478,6 +1480,37 @@ bool NotificationPreferences::GetDisableNotificationInfo(NotificationDisable &no return true; } +bool NotificationPreferences::GetkioskAppTrustList(std::vector &kioskAppTrustList) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (preferencesInfo_.GetkioskAppTrustList(kioskAppTrustList)) { + ANS_LOGD("info get disable notification success"); + return true; + } + std::string value = ""; + int32_t userId = -1; + if (GetKvFromDb("kiosk_app_trust_list", value, userId) != ERR_OK) { + ANS_LOGD("Get kiosk app trust list failed."); + return false; + } + if (value.empty() || !nlohmann::json::accept(value)) { + ANS_LOGE("Invalid json string"); + return false; + } + nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false); + if (jsonObject.is_null() || jsonObject.empty()) { + ANS_LOGE("Invalid JSON object"); + return false; + } + if (jsonObject.is_discarded() || !jsonObject.is_array()) { + ANS_LOGE("Parse kiosk app trust list failed due to data is discarded or not array"); + return false; + } + kioskAppTrustList = jsonObject.get>(); + preferencesInfo_.SetkioskAppTrustList(kioskAppTrustList); + return true; +} + ErrCode NotificationPreferences::SetSubscriberExistFlag(const std::string& deviceType, bool existFlag) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1576,6 +1609,23 @@ bool NotificationPreferences::SetBundleRemoveFlag(const sptrSetBundleRemoveFlag(bundleOption, slotType, sourceType); } +void NotificationPreferences::SetKioskModeStatus(bool isKioskMode) +{ + isKioskMode_ = isKioskMode; +} + +bool NotificationPreferences::IsKioskMode() +{ + AAFwk::KioskStatus kioskStatus; + auto ret = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetKioskStatus(kioskStatus)); + if (ret != ERR_OK) { + ANS_LOGE("Get KioskStatus failed"); + return isKioskMode_; + } + isKioskMode_ = kioskStatus.isKioskMode_; + return isKioskMode_; +} + #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER int32_t NotificationPreferences::GetKvFromDb( const std::string &key, std::string &value, const int32_t &userId, int32_t &retCode) diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 92d810f4b..e3efa2603 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -443,6 +443,21 @@ bool NotificationPreferencesInfo::GetDisableNotificationInfo(NotificationDisable return true; } +void NotificationPreferencesInfo::SetkioskAppTrustList(const std::vector &kioskAppTrustList) +{ + kioskAppTrustList_ = kioskAppTrustList; +} + +bool NotificationPreferencesInfo::GetkioskAppTrustList(std::vector &kioskAppTrustList) const +{ + if (kioskAppTrustList_.empty()) { + ANS_LOGE("kioskAppTrustList is empty"); + return false; + } + kioskAppTrustList = kioskAppTrustList_; + return true; +} + void NotificationPreferencesInfo::AddDisableNotificationInfo(const std::string &value) { NotificationDisable notificationDisable; diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index db7602425..844d2d8ec 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -46,6 +46,8 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_ON); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_OFF); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); @@ -152,6 +154,10 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) } } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START) { NotificationCloneManager::GetInstance().OnRestoreStart(want); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_ON) { + NotificationPreferences::GetInstance()->SetKioskModeStatus(true); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_OFF) { + NotificationPreferences::GetInstance()->SetKioskModeStatus(false); } else { OnReceiveEventInner(data); } diff --git a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp index ba8b989f7..da669a166 100644 --- a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp @@ -2285,5 +2285,80 @@ HWTEST_F(AnsPublishServiceTest, ClearSlotTypeData_00001, Function | SmallTest | ASSERT_EQ(sourceType, 2); } +/** + * @tc.name: IsEnableNotificationByKioskAppTrustList_001 + * @tc.desc: Test IsEnableNotificationByKioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(AnsPublishServiceTest, IsEnableNotificationByKioskAppTrustList_001, Function | SmallTest | Level1) +{ + std::string bundleName = ""; + bool result = advancedNotificationService_->IsEnableNotificationByKioskAppTrustList(bundleName); + EXPECT_FALSE(result); + bundleName = "com.test.example"; + NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.emplace_back(bundleName); + result = advancedNotificationService_->IsEnableNotificationByKioskAppTrustList(bundleName); + EXPECT_TRUE(result); +} + +/** + * @tc.name: IsDisableNotificationByKiosk_001 + * @tc.desc: Test IsDisableNotificationByKiosk + * @tc.type: FUNC + */ +HWTEST_F(AnsPublishServiceTest, IsDisableNotificationByKiosk_001, Function | SmallTest | Level1) +{ + std::string bundleName = ""; + bool result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName); + EXPECT_FALSE(result); + NotificationPreferences::GetInstance()->SetKioskModeStatus(true); + result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName); + EXPECT_TRUE(result); + bundleName = "com.test.example"; + NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.emplace_back(bundleName); + result = advancedNotificationService_->IsDisableNotificationByKiosk(bundleName); + EXPECT_FALSE(result); +} + +/** + * @tc.name: IsDisableNotificationForSaByKiosk_001 + * @tc.desc: Test IsDisableNotificationForSaByKiosk + * @tc.type: FUNC + */ +HWTEST_F(AnsPublishServiceTest, IsDisableNotificationForSaByKiosk_001, Function | SmallTest | Level1) +{ + std::string bundleName = "com.test.example"; + sptr request = nullptr; + bool result = advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, request); + EXPECT_FALSE(result); + + request = new (std::nothrow) NotificationRequest(); + ASSERT_NE(request, nullptr); + request->isAgent_ = false; + result = advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, request); + EXPECT_FALSE(result); + + request->isAgent_ = true; + bundleName = ""; + result = advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, request); + EXPECT_FALSE(result); +} + +/** + * @tc.name: IsDisableNotificationForSaByKiosk_002 + * @tc.desc: Test IsDisableNotificationForSaByKiosk + * @tc.type: FUNC + */ +HWTEST_F(AnsPublishServiceTest, IsDisableNotificationForSaByKiosk_002, Function | SmallTest | Level1) +{ + std::string bundleName = "com.test.example"; + sptr request = new (std::nothrow) NotificationRequest(); + ASSERT_NE(request, nullptr); + request->isAgent_ = true; + NotificationPreferences::GetInstance()->isKioskMode_ = true; + NotificationPreferences::GetInstance()->preferencesInfo_.kioskAppTrustList_.clear(); + bool result = advancedNotificationService_->IsDisableNotificationForSaByKiosk(bundleName, request); + EXPECT_TRUE(result); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/notification_preferences_info_test.cpp b/services/ans/test/unittest/notification_preferences_info_test.cpp index 920c960e4..746173395 100644 --- a/services/ans/test/unittest/notification_preferences_info_test.cpp +++ b/services/ans/test/unittest/notification_preferences_info_test.cpp @@ -389,5 +389,42 @@ HWTEST_F(NotificationPreferencesInfoTest, GetAllLiveViewEnabledBundles_0300, Tes ASSERT_EQ(res, ERR_OK); ASSERT_EQ(bundleOption.size(), 1); } + +/** + * @tc.name: SetkioskAppTrustList_0100 + * @tc.desc: test SetkioskAppTrustList. + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesInfoTest, SetkioskAppTrustList_0100, TestSize.Level1) +{ + std::shared_ptr preferencesInfo = std::make_shared(); + ASSERT_NE(preferencesInfo, nullptr); + std::vector kioskAppTrustList; + kioskAppTrustList.push_back("testBundleName"); + preferencesInfo->SetkioskAppTrustList(kioskAppTrustList); + ASSERT_EQ(preferencesInfo->kioskAppTrustList_.size(), 1); +} + +/** + * @tc.name: GetkioskAppTrustList_0100 + * @tc.desc: test GetkioskAppTrustList. + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesInfoTest, GetkioskAppTrustList_0100, TestSize.Level1) +{ + std::shared_ptr preferencesInfo = std::make_shared(); + ASSERT_NE(preferencesInfo, nullptr); + std::vector resultList; + auto ret = preferencesInfo->GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); + + std::vector kioskAppTrustList; + kioskAppTrustList.push_back("testBundleName"); + preferencesInfo->SetkioskAppTrustList(kioskAppTrustList); + ASSERT_EQ(preferencesInfo->kioskAppTrustList_.size(), 1); + + ret = preferencesInfo->GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, true); +} } } diff --git a/services/ans/test/unittest/notification_preferences_test.cpp b/services/ans/test/unittest/notification_preferences_test.cpp index e8e149cc4..7bb4c203b 100644 --- a/services/ans/test/unittest/notification_preferences_test.cpp +++ b/services/ans/test/unittest/notification_preferences_test.cpp @@ -2408,5 +2408,186 @@ HWTEST_F(NotificationPreferencesTest, GetBundleRemoveFlag_001, Function | SmallT res = notificationPreferences.GetBundleRemoveFlag(bundleOption, slotType, 0); ASSERT_EQ(res, true); } + +/** + * @tc.name: SetKioskModeStatus_001 + * @tc.desc: Test SetKioskModeStatus + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, SetKioskModeStatus_001, Function | SmallTest | Level1) +{ + bool isKioskMode = true; + NotificationPreferences notificationPreferences; + notificationPreferences.SetKioskModeStatus(isKioskMode); + ASSERT_EQ(notificationPreferences.isKioskMode_, true); +} + +/** + * @tc.name: IsKioskMode_001 + * @tc.desc: Test IsKioskMode + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, IsKioskMode_001, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.isKioskMode_ = true; + auto ret = notificationPreferences.IsKioskMode(); + ASSERT_EQ(ret, true); + notificationPreferences.isKioskMode_ = false; + ret = notificationPreferences.IsKioskMode(); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_001 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_001, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + std::vector kioskAppTrustList; + kioskAppTrustList.push_back("testBundleName1"); + kioskAppTrustList.push_back("testBundleName2"); + notificationPreferences.preferencesInfo_.SetkioskAppTrustList(kioskAppTrustList); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, true); +} + +/** + * @tc.name: GetkioskAppTrustList_002 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_002, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_003 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_003, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = ""; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_004 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_004, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = "invalid json string"; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_005 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_005, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = "null"; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_006 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_006, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = "[]"; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_007 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_007, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = R"({"key": "value"})"; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: GetkioskAppTrustList_008 + * @tc.desc: Test GetkioskAppTrustList + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_008, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + + std::string key = "kiosk_app_trust_list"; + std::string value = R"(["com.example.app1", "com.example.app2", "com.example.app3"])"; + int32_t userId = -1; + auto result = notificationPreferences.SetKvToDb(key, value, userId); + ASSERT_EQ(result, ERR_OK); + std::vector resultList; + auto ret = notificationPreferences.GetkioskAppTrustList(resultList); + ASSERT_EQ(ret, true); +} } // namespace Notification } // namespace OHOS -- Gitee