1 /* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #define private public 17 #include "notification_config_parse.h" 18 #undef private 19 20 #include <gtest/gtest.h> 21 22 using namespace testing::ext; 23 namespace OHOS { 24 namespace Notification { 25 26 namespace { 27 const std::string TEST_BUNDLENAME = "bundleName"; 28 const std::string TEST_UID = "uid"; 29 const std::string TEST_OWNER_BUNDLENAME = "com.example.test"; 30 const int32_t TEST_CREATOR_UID = 12345; 31 } // namespace 32 33 class NotificationConfigParseTest : public testing::Test { 34 public: SetUpTestCas()35 static void SetUpTestCas() {}; TearDownTestCase()36 static void TearDownTestCase() {}; SetUp()37 void SetUp() {}; TearDown()38 void TearDown() {}; 39 }; 40 41 /** 42 * @tc.name : GetCollaborationFilter_00100 43 * @tc.number : GetCollaborationFilter_00100 44 * @tc.desc : Test GetCollaborationFilter function without json. 45 */ 46 HWTEST_F(NotificationConfigParseTest, GetCollaborationFilter_00100, Function | SmallTest | Level1) 47 { 48 std::shared_ptr<NotificationConfigParse> configParse = std::make_shared<NotificationConfigParse>(); 49 if (!configParse->notificationConfigJsons_.empty()) { 50 configParse->notificationConfigJsons_.clear(); 51 } 52 configParse->GetCollaborationFilter(); 53 54 EXPECT_TRUE(configParse->uidList_.empty()); 55 EXPECT_TRUE(configParse->bundleNameList_.empty()); 56 57 bool result = configParse->IsInCollaborationFilter(TEST_OWNER_BUNDLENAME, TEST_CREATOR_UID); 58 EXPECT_FALSE(result); 59 } 60 61 /** 62 * @tc.name : GetCollaborationFilter_00200 63 * @tc.number : GetCollaborationFilter_00200 64 * @tc.desc : Test GetCollaborationFilter function. 65 */ 66 HWTEST_F(NotificationConfigParseTest, GetCollaborationFilter_00200, Function | SmallTest | Level1) 67 { 68 std::shared_ptr<NotificationConfigParse> configParse = std::make_shared<NotificationConfigParse>(); 69 nlohmann::json jsonObject = { 70 {"notificationService", { 71 {"collaborationFilter", { 72 {"uid", {12345, 67890}}, 73 {"bundleName", {"com.example.test"}} 74 }} 75 }} 76 }; 77 if (!configParse->notificationConfigJsons_.empty()) { 78 configParse->notificationConfigJsons_.clear(); 79 } 80 configParse->notificationConfigJsons_.push_back(jsonObject); 81 configParse->GetCollaborationFilter(); 82 83 EXPECT_FALSE(configParse->uidList_.empty()); 84 EXPECT_FALSE(configParse->bundleNameList_.empty()); 85 86 bool result = configParse->IsInCollaborationFilter("", 0); 87 EXPECT_FALSE(result); 88 89 result = configParse->IsInCollaborationFilter("", TEST_CREATOR_UID); 90 EXPECT_TRUE(result); 91 92 result = configParse->IsInCollaborationFilter(TEST_OWNER_BUNDLENAME, 0); 93 EXPECT_TRUE(result); 94 } 95 96 /** 97 * @tc.name : GetFilterUidAndBundleName_00100 98 * @tc.number : GetFilterUidAndBundleName_00100 99 * @tc.desc : Test GetFilterUidAndBundleName function with empty bundleName, uid. 100 */ 101 HWTEST_F(NotificationConfigParseTest, GetFilterUidAndBundleName_00100, Function | SmallTest | Level1) 102 { 103 std::shared_ptr<NotificationConfigParse> configParse = std::make_shared<NotificationConfigParse>(); 104 nlohmann::json emptyCollaborationFilter = { 105 {"notificationService", { 106 {"collaborationFilter", { 107 {"uid", nlohmann::json::array()}, 108 {"bundleName", nlohmann::json::array()} 109 }} 110 }} 111 }; 112 if (!configParse->notificationConfigJsons_.empty()) { 113 configParse->notificationConfigJsons_.clear(); 114 } 115 configParse->notificationConfigJsons_.push_back(emptyCollaborationFilter); 116 bool ret = configParse->GetFilterUidAndBundleName(TEST_BUNDLENAME); 117 EXPECT_TRUE(ret); 118 } 119 } //namespace Notification 120 } //namespace OHOS 121