1 /* 2 * Copyright (c) 2025 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 #include "gtest/gtest.h" 17 #define private public 18 #define protected public 19 #include "common_notification_publish_process.h" 20 #include "ans_inner_errors.h" 21 #include "live_publish_process.h" 22 #include "accesstoken_kit.h" 23 #include "notification_content.h" 24 #include "notification_constant.h" 25 #undef private 26 #undef protected 27 28 using namespace testing::ext; 29 using namespace OHOS::Security::AccessToken; 30 namespace OHOS { 31 namespace Notification { 32 33 extern void MockIsVerfyPermisson(bool isVerify); 34 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet); 35 extern void MockIsSystemAppByFullTokenID(bool isSystemApp); 36 extern void MockDlpType(DlpType mockRet); 37 extern void MockIsSystemApp(bool isSystemApp); 38 39 class PublishProcessTest : public testing::Test { 40 public: PublishProcessTest()41 PublishProcessTest() 42 {} ~PublishProcessTest()43 ~PublishProcessTest() 44 {} SetUpTestCas(void)45 static void SetUpTestCas(void) {}; TearDownTestCase(void)46 static void TearDownTestCase(void) {}; SetUp()47 void SetUp() {}; TearDown()48 void TearDown() {}; 49 }; 50 51 /** 52 * @tc.name: Test CommonPublishCheck 53 * @tc.desc: Test CommonPublishCheck 54 * @tc.type: FUNC 55 */ 56 HWTEST_F(PublishProcessTest, BaseCommonPublishCheck_00001, Function | SmallTest | Level1) 57 { 58 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 59 MockIsSystemAppByFullTokenID(false); 60 sptr<NotificationRequest> request(new NotificationRequest(1)); 61 request->SetReceiverUserId(100); 62 63 CommonNotificationPublishProcess process; 64 auto res = process.CommonPublishCheck(request); 65 ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); 66 } 67 68 /** 69 * @tc.name: Test CommonPublishCheck 70 * @tc.desc: Test CommonPublishCheck 71 * @tc.type: FUNC 72 */ 73 HWTEST_F(PublishProcessTest, BaseCommonPublishCheck_00002, Function | SmallTest | Level1) 74 { 75 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 76 MockIsSystemAppByFullTokenID(true); 77 MockIsVerfyPermisson(false); 78 MockIsSystemApp(true); 79 sptr<NotificationRequest> request(new NotificationRequest(1)); 80 request->SetReceiverUserId(100); 81 82 CommonNotificationPublishProcess process; 83 auto res = process.CommonPublishCheck(request); 84 ASSERT_EQ(res, ERR_ANS_PERMISSION_DENIED); 85 } 86 87 /** 88 * @tc.name: Test CommonPublishProcess 89 * @tc.desc: Test CommonPublishProcess 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(PublishProcessTest, BaseCommonPublishProcess_00001, Function | SmallTest | Level1) 93 { 94 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 95 MockDlpType(DlpType::DLP_READ); 96 sptr<NotificationRequest> request(new NotificationRequest(1)); 97 98 CommonNotificationPublishProcess process; 99 auto res = process.CommonPublishProcess(request); 100 ASSERT_EQ(res, ERR_ANS_DLP_HAP); 101 } 102 103 /** 104 * @tc.name: Test PublishNotificationByApp 105 * @tc.desc: Test PublishNotificationByApp 106 * @tc.type: FUNC 107 */ 108 HWTEST_F(PublishProcessTest, PublishNotificationByApp_00001, Function | SmallTest | Level1) 109 { 110 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 111 MockIsSystemAppByFullTokenID(false); 112 sptr<NotificationRequest> request(new NotificationRequest(1)); 113 request->SetReceiverUserId(100); 114 115 CommonNotificationPublishProcess progress; 116 auto res = progress.PublishNotificationByApp(request); 117 ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); 118 } 119 120 /** 121 * @tc.name: Test PublishNotificationByApp 122 * @tc.desc: Test PublishNotificationByApp 123 * @tc.type: FUNC 124 */ 125 HWTEST_F(PublishProcessTest, PublishNotificationByApp_00002, Function | SmallTest | Level1) 126 { 127 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 128 sptr<NotificationRequest> request(new NotificationRequest(1)); 129 request->SetInProgress(true); 130 131 CommonNotificationPublishProcess progress; 132 auto res = progress.PublishNotificationByApp(request); 133 ASSERT_EQ(request->IsInProgress(), false); 134 } 135 136 /** 137 * @tc.name: Test PublishNotificationByApp 138 * @tc.desc: Test PublishNotificationByApp 139 * @tc.type: FUNC 140 */ 141 HWTEST_F(PublishProcessTest, PublishNotificationByApp_00003, Function | SmallTest | Level1) 142 { 143 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 144 MockDlpType(DlpType::DLP_READ); 145 sptr<NotificationRequest> request(new NotificationRequest(1)); 146 147 CommonNotificationPublishProcess progress; 148 auto res = progress.PublishNotificationByApp(request); 149 ASSERT_EQ(res, ERR_ANS_DLP_HAP); 150 } 151 152 /** 153 * @tc.name: Test LivePublishPreWork 154 * @tc.desc: Test LivePublishPreWork 155 * @tc.type: FUNC 156 */ 157 HWTEST_F(PublishProcessTest, LivePublishPreWork_00001, Function | SmallTest | Level1) 158 { 159 sptr<NotificationRequest> request(new NotificationRequest(1)); 160 request->SetRemoveAllowed(false); 161 MockIsSystemApp(true); 162 LivePublishProcess progress; 163 auto res = progress.PublishPreWork(request, true); 164 ASSERT_EQ(request->IsRemoveAllowed(), true); 165 ASSERT_EQ(res.GetErrCode(), ERR_OK); 166 } 167 168 /** 169 * @tc.name: Test LivePublishPreWork 170 * @tc.desc: Test LivePublishPreWork 171 * @tc.type: FUNC 172 */ 173 HWTEST_F(PublishProcessTest, LivePublishPreWork_00002, Function | SmallTest | Level1) 174 { 175 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 176 MockIsSystemApp(false); 177 sptr<NotificationRequest> request(new NotificationRequest(1)); 178 179 LivePublishProcess progress; 180 auto res = progress.PublishPreWork(request, true); 181 ASSERT_EQ(res.GetErrCode(), ERR_ANS_INVALID_PARAM); 182 } 183 184 /** 185 * @tc.name: Test LivePublishNotificationByApp_00001 186 * @tc.desc: Test LivePublishNotificationByApp_00001 187 * @tc.type: FUNC 188 */ 189 HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00001, Function | SmallTest | Level1) 190 { 191 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 192 MockIsSystemApp(false); 193 sptr<NotificationRequest> request(new NotificationRequest(1)); 194 std::shared_ptr<NotificationLocalLiveViewContent> liveViewContent = 195 std::make_shared<NotificationLocalLiveViewContent>(); 196 std::shared_ptr<NotificationContent> content = 197 std::make_shared<NotificationContent>(liveViewContent); 198 request->SetContent(content); 199 request->SetReceiverUserId(100); 200 201 LivePublishProcess progress; 202 auto res = progress.PublishPreWork(request, false); 203 ASSERT_EQ(res.GetErrCode(), ERR_ANS_NON_SYSTEM_APP); 204 } 205 206 /** 207 * @tc.name: Test LivePublishNotificationByApp_00002 208 * @tc.desc: Test LivePublishNotificationByApp_00002 209 * @tc.type: FUNC 210 */ 211 HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00002, Function | SmallTest | Level1) 212 { 213 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 214 sptr<NotificationRequest> request(new NotificationRequest(1)); 215 request->SetInProgress(true); 216 217 LivePublishProcess progress; 218 auto res = progress.PublishNotificationByApp(request); 219 ASSERT_EQ(request->IsInProgress(), false); 220 } 221 222 /** 223 * @tc.name: Test LivePublishNotificationByApp_00002 224 * @tc.desc: Test LivePublishNotificationByApp_00002 225 * @tc.type: FUNC 226 */ 227 HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00003, Function | SmallTest | Level1) 228 { 229 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 230 MockDlpType(DlpType::DLP_READ); 231 sptr<NotificationRequest> request(new NotificationRequest(1)); 232 request->SetInProgress(true); 233 234 LivePublishProcess progress; 235 auto res = progress.PublishNotificationByApp(request); 236 ASSERT_EQ(res, ERR_ANS_DLP_HAP); 237 } 238 239 240 /** 241 * @tc.name: Test LiveCheckLocalLiveViewSubscribed_00001 242 * @tc.desc: Test LiveCheckLocalLiveViewSubscribed_00001 243 * @tc.type: FUNC 244 */ 245 HWTEST_F(PublishProcessTest, LiveCheckLocalLiveViewSubscribed_00001, Function | SmallTest | Level1) 246 { 247 sptr<NotificationRequest> request(new NotificationRequest(1)); 248 request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); 249 250 std::shared_ptr<NotificationLiveViewContent> liveViewContent = 251 std::make_shared<NotificationLiveViewContent>(); 252 liveViewContent->SetIsOnlyLocalUpdate(true); 253 std::shared_ptr<NotificationContent> content = 254 std::make_shared<NotificationContent>(liveViewContent); 255 request->SetContent(content); 256 257 LivePublishProcess progress; 258 auto res = progress.CheckLocalLiveViewSubscribed(request, true, 100); 259 ASSERT_FALSE(res); 260 } 261 } //namespace Notification 262 } //namespace OHOS