1 /*
2 * Copyright (c) 2023-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 #include <chrono>
17 #include <functional>
18 #include <thread>
19
20 #include "gtest/gtest.h"
21
22 #define private public
23
24 #include "advanced_notification_service.h"
25 #include "ans_const_define.h"
26 #include "ans_inner_errors.h"
27 #include "ans_log_wrapper.h"
28 #include "ans_subscriber_listener.h"
29 #include "ans_ut_constant.h"
30 #include "iremote_object.h"
31 #include "want_agent_info.h"
32 #include "want_agent_helper.h"
33 #include "want_params.h"
34 #include "mock_ipc_skeleton.h"
35 #include "notification_preferences.h"
36 #include "notification_constant.h"
37 #include "notification_record.h"
38 #include "notification_subscriber.h"
39 #include "refbase.h"
40
41 extern void MockVerifyNativeToken(bool mockRet);
42 extern void MockVerifyShellToken(bool mockRet);
43 extern void MockGetDistributedEnableInApplicationInfo(bool mockRet, uint8_t mockCase = 0);
44 extern void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0);
45
46 using namespace testing::ext;
47 using namespace OHOS::Media;
48 using namespace OHOS::Security::AccessToken;
49
50 namespace OHOS {
51 namespace Notification {
52 extern void MockIsVerfyPermisson(bool isVerify);
53 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
54 extern void MockIsSystemApp(bool isSystemApp);
55 extern void MockDistributedNotificationEnabled(bool isEnable);
56 extern void MockIsNonBundleName(bool isNonBundleName);
57
58 class AnsBranchTest : public testing::Test {
59 public:
60 static void SetUpTestCase();
61 static void TearDownTestCase();
62 void SetUp();
63 void TearDown();
64 static void InitNotificationRecord(std::shared_ptr<NotificationRecord> &record,
65 const NotificationLiveViewContent::LiveViewStatus &status);
66 private:
67 void TestAddSlot(NotificationConstant::SlotType type);
68
69 private:
70 static sptr<AdvancedNotificationService> advancedNotificationService_;
71 };
72
73 sptr<AdvancedNotificationService> AnsBranchTest::advancedNotificationService_ = nullptr;
74
SetUpTestCase()75 void AnsBranchTest::SetUpTestCase() {}
76
TearDownTestCase()77 void AnsBranchTest::TearDownTestCase() {}
78
SetUp()79 void AnsBranchTest::SetUp()
80 {
81 GTEST_LOG_(INFO) << "SetUp start";
82
83 advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
84 IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN);
85 IPCSkeleton::SetCallingUid(SYSTEM_APP_UID);
86 advancedNotificationService_->CancelAll("");
87 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
88 MockIsSystemApp(true);
89 GTEST_LOG_(INFO) << "SetUp end";
90 }
91
TearDown()92 void AnsBranchTest::TearDown()
93 {
94 IPCSkeleton::SetCallingUid(SYSTEM_APP_UID);
95 advancedNotificationService_ = nullptr;
96 GTEST_LOG_(INFO) << "TearDown";
97 }
98
SleepForFC()99 inline void SleepForFC()
100 {
101 // For ANS Flow Control
102 std::this_thread::sleep_for(std::chrono::seconds(1));
103 }
104
105 class TestAnsSubscriber : public NotificationSubscriber {
106 public:
OnDied()107 void OnDied() override
108 {}
OnConnected()109 void OnConnected() override
110 {}
OnDisconnected()111 void OnDisconnected() override
112 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)113 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
114 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)115 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
116 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)117 void OnCanceled(const std::shared_ptr<Notification> &request,
118 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
119 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)120 void OnEnabledNotificationChanged(
121 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
122 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)123 void OnConsumed(const std::shared_ptr<Notification> &request,
124 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
125 {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)126 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
127 {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)128 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
129 {}
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)130 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
131 &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
132 {}
133 };
134
TestAddSlot(NotificationConstant::SlotType type)135 void AnsBranchTest::TestAddSlot(NotificationConstant::SlotType type)
136 {
137 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
138 MockIsSystemApp(true);
139 MockIsVerfyPermisson(true);
140 std::vector<sptr<NotificationSlot>> slots;
141 sptr<NotificationSlot> slot = new NotificationSlot(type);
142 slots.push_back(slot);
143 ASSERT_EQ(advancedNotificationService_->AddSlots(slots), (int)ERR_OK);
144 }
145
146 /**
147 * @tc.number : AnsBranchTest_222000
148 * @tc.name : PrepareNotificationRequest_1000
149 * @tc.desc : Test PrepareNotificationRequest function return ERR_ANS_NON_SYSTEM_APP.
150 * @tc.require : #I6P8UI
151 */
152 HWTEST_F(AnsBranchTest, AnsBranchTest_222000, Function | SmallTest | Level1)
153 {
154 sptr<NotificationRequest> req = new NotificationRequest();
155 EXPECT_NE(req, nullptr);
156
157 req->SetIsAgentNotification(true);
158 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
159 MockIsSystemApp(false);
160 MockIsVerfyPermisson(false);
161
162 ASSERT_EQ(advancedNotificationService_->PrepareNotificationRequest(req), ERR_ANS_NON_SYSTEM_APP);
163 }
164
165 /**
166 * @tc.number : AnsBranchTest_223000
167 * @tc.name : PrepareNotificationRequest_2000
168 * @tc.desc : Test PrepareNotificationRequest function return ERR_ANS_PERMISSION_DENIED.
169 * @tc.require : #I6P8UI
170 */
171 HWTEST_F(AnsBranchTest, AnsBranchTest_223000, Function | SmallTest | Level1)
172 {
173 sptr<NotificationRequest> req = new NotificationRequest();
174 EXPECT_NE(req, nullptr);
175
176 req->SetIsAgentNotification(true);
177 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
178 MockIsVerfyPermisson(false);
179 ASSERT_EQ(advancedNotificationService_->PrepareNotificationRequest(req), ERR_ANS_PERMISSION_DENIED);
180 }
181
182 /**
183 * @tc.number : AnsBranchTest_224000
184 * @tc.name : Publish_1000
185 * @tc.desc : Test Publish function req is false.
186 * @tc.require : #I6P8UI
187 */
188 HWTEST_F(AnsBranchTest, AnsBranchTest_224000, Function | SmallTest | Level1)
189 {
190 std::string label = "publish's label";
191 ASSERT_EQ(advancedNotificationService_->Publish(label, nullptr), ERR_ANS_INVALID_PARAM);
192 }
193
194 /**
195 * @tc.number : AnsBranchTest_225000
196 * @tc.name : CancelAsBundle_1000
197 * @tc.desc : Test CancelAsBundle function return ERR_ANS_NON_SYSTEM_APP.
198 * @tc.require : #I6P8UI
199 */
200 HWTEST_F(AnsBranchTest, AnsBranchTest_225000, Function | SmallTest | Level1)
201 {
202 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
203 MockIsSystemApp(false);
204
205 int32_t notificationId = 1;
206 std::string representativeBundle = "RepresentativeBundle";
207 int32_t userId = 1;
208 ASSERT_EQ(advancedNotificationService_->CancelAsBundle(
209 notificationId, representativeBundle, userId), ERR_ANS_NON_SYSTEM_APP);
210 }
211
212 /**
213 * @tc.number : AnsBranchTest_226000
214 * @tc.name : CancelAsBundle_2000
215 * @tc.desc : Test CancelAsBundle function return ERR_ANS_PERMISSION_DENIED.
216 * @tc.require : #I6P8UI
217 */
218 HWTEST_F(AnsBranchTest, AnsBranchTest_226000, Function | SmallTest | Level1)
219 {
220 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
221 MockIsVerfyPermisson(false);
222
223 int32_t notificationId = 1;
224 std::string representativeBundle = "RepresentativeBundle";
225 int32_t userId = 1;
226 ASSERT_EQ(advancedNotificationService_->CancelAsBundle(
227 notificationId, representativeBundle, userId), ERR_ANS_PERMISSION_DENIED);
228 }
229
230 /**
231 * @tc.number : AnsBranchTest_227000
232 * @tc.name : AddSlots_2000
233 * @tc.desc : Test AddSlots function return ERR_ANS_PERMISSION_DENIED.
234 * @tc.require : #I6P8UI
235 */
236 HWTEST_F(AnsBranchTest, AnsBranchTest_227000, Function | SmallTest | Level1)
237 {
238 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
239 MockIsVerfyPermisson(false);
240
241 std::vector<sptr<NotificationSlot>> slots;
242 sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::OTHER);
243 sptr<NotificationSlot> slot1 = new NotificationSlot(NotificationConstant::OTHER);
244 slots.push_back(slot0);
245 slots.push_back(slot1);
246 ASSERT_EQ(advancedNotificationService_->AddSlots(slots), ERR_ANS_PERMISSION_DENIED);
247 }
248
249 /**
250 * @tc.number : AnsBranchTest_228000
251 * @tc.name : Delete_1000
252 * @tc.desc : Test Delete function return ERR_ANS_PERMISSION_DENIED.
253 * @tc.require : #I6P8UI
254 */
255 HWTEST_F(AnsBranchTest, AnsBranchTest_228000, Function | SmallTest | Level1)
256 {
257 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
258 MockIsVerfyPermisson(false);
259
260 const std::string key = "key";
261 ASSERT_EQ(advancedNotificationService_->Delete(
262 key, NotificationConstant::CANCEL_REASON_DELETE), ERR_ANS_PERMISSION_DENIED);
263 }
264
265 /**
266 * @tc.number : AnsBranchTest_229000
267 * @tc.name : DeleteByBundle_1000
268 * @tc.desc : Test DeleteByBundle function return ERR_ANS_PERMISSION_DENIED.
269 * @tc.require : #I6P8UI
270 */
271 HWTEST_F(AnsBranchTest, AnsBranchTest_229000, Function | SmallTest | Level1)
272 {
273 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
274 MockIsVerfyPermisson(false);
275
276 ASSERT_EQ(advancedNotificationService_->DeleteByBundle(
277 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID)), ERR_ANS_PERMISSION_DENIED);
278 }
279
280 /**
281 * @tc.number : AnsBranchTest_230000
282 * @tc.name : DeleteByBundle_2000
283 * @tc.desc : Test DeleteByBundle function return ERR_ANS_NON_SYSTEM_APP.
284 * @tc.require : #I6P8UI
285 */
286 HWTEST_F(AnsBranchTest, AnsBranchTest_230000, Function | SmallTest | Level1)
287 {
288 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
289 MockIsSystemApp(false);
290
291 ASSERT_EQ(advancedNotificationService_->DeleteByBundle(
292 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID)), ERR_ANS_NON_SYSTEM_APP);
293 }
294
295 /**
296 * @tc.number : AnsBranchTest_231000
297 * @tc.name : DeleteAll_1000
298 * @tc.desc : Test DeleteAll function return ERR_ANS_PERMISSION_DENIED.
299 * @tc.require : #I6P8UI
300 */
301 HWTEST_F(AnsBranchTest, AnsBranchTest_231000, Function | SmallTest | Level1)
302 {
303 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
304 MockIsVerfyPermisson(false);
305
306 ASSERT_EQ(advancedNotificationService_->DeleteAll(), ERR_ANS_PERMISSION_DENIED);
307 }
308
309 /**
310 * @tc.number : AnsBranchTest_232000
311 * @tc.name : GetSlotsByBundle_1000
312 * @tc.desc : Test GetSlotsByBundle function return ERR_ANS_NON_SYSTEM_APP.
313 * @tc.require : #I6P8UI
314 */
315 HWTEST_F(AnsBranchTest, AnsBranchTest_232000, Function | SmallTest | Level1)
316 {
317 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
318 MockIsSystemApp(false);
319
320 std::vector<sptr<NotificationSlot>> slots;
321 ASSERT_EQ(advancedNotificationService_->GetSlotsByBundle(
322 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots), ERR_ANS_NON_SYSTEM_APP);
323 }
324
325 /**
326 * @tc.number : AnsBranchTest_233000
327 * @tc.name : GetSlotsByBundle_2000
328 * @tc.desc : Test GetSlotsByBundle function return ERR_ANS_PERMISSION_DENIED.
329 * @tc.require : #I6P8UI
330 */
331 HWTEST_F(AnsBranchTest, AnsBranchTest_233000, Function | SmallTest | Level1)
332 {
333 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
334 MockIsVerfyPermisson(false);
335
336 std::vector<sptr<NotificationSlot>> slots;
337 ASSERT_EQ(advancedNotificationService_->GetSlotsByBundle(
338 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots), ERR_ANS_PERMISSION_DENIED);
339 }
340
341 /**
342 * @tc.number : AnsBranchTest_234000
343 * @tc.name : UpdateSlots_1000
344 * @tc.desc : Test UpdateSlots function return ERR_ANS_NON_SYSTEM_APP.
345 * @tc.require : #I6P8UI
346 */
347 HWTEST_F(AnsBranchTest, AnsBranchTest_234000, Function | SmallTest | Level1)
348 {
349 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
350 MockIsSystemApp(false);
351
352 std::vector<sptr<NotificationSlot>> slots;
353 sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::OTHER);
354 slots.push_back(slot0);
355 ASSERT_EQ(advancedNotificationService_->UpdateSlots(
356 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots), ERR_ANS_NON_SYSTEM_APP);
357 }
358
359 /**
360 * @tc.number : AnsBranchTest_235000
361 * @tc.name : UpdateSlots_1000
362 * @tc.desc : Test UpdateSlots function return ERR_ANS_PERMISSION_DENIED.
363 * @tc.require : #I6P8UI
364 */
365 HWTEST_F(AnsBranchTest, AnsBranchTest_235000, Function | SmallTest | Level1)
366 {
367 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
368 MockIsVerfyPermisson(false);
369
370 std::vector<sptr<NotificationSlot>> slots;
371 sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::OTHER);
372 slots.push_back(slot0);
373 ASSERT_EQ(advancedNotificationService_->UpdateSlots(
374 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), slots), ERR_ANS_PERMISSION_DENIED);
375 }
376
377 /**
378 * @tc.number : AnsBranchTest_236000
379 * @tc.name : SetShowBadgeEnabledForBundle_1000
380 * @tc.desc : Test SetShowBadgeEnabledForBundle function return ERR_ANS_PERMISSION_DENIED.
381 * @tc.require : #I6P8UI
382 */
383 HWTEST_F(AnsBranchTest, AnsBranchTest_236000, Function | SmallTest | Level1)
384 {
385 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
386 MockIsVerfyPermisson(false);
387
388 ASSERT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundle(
389 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), true), ERR_ANS_PERMISSION_DENIED);
390 }
391
392 /**
393 * @tc.number : AnsBranchTest_237000
394 * @tc.name : GetShowBadgeEnabledForBundle_1000
395 * @tc.desc : Test GetShowBadgeEnabledForBundle function return ERR_ANS_NON_SYSTEM_APP.
396 * @tc.require : #I6P8UI
397 */
398 HWTEST_F(AnsBranchTest, AnsBranchTest_237000, Function | SmallTest | Level1)
399 {
400 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
401 MockIsSystemApp(false);
402
403 bool allow = false;
404 ASSERT_EQ(advancedNotificationService_->GetShowBadgeEnabledForBundle(
405 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), allow), ERR_ANS_NON_SYSTEM_APP);
406 }
407
408 /**
409 * @tc.number : AnsBranchTest_238000
410 * @tc.name : GetShowBadgeEnabledForBundle_2000
411 * @tc.desc : Test GetShowBadgeEnabledForBundle function return ERR_ANS_PERMISSION_DENIED.
412 * @tc.require : #I6P8UI
413 */
414 HWTEST_F(AnsBranchTest, AnsBranchTest_238000, Function | SmallTest | Level1)
415 {
416 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
417 MockIsVerfyPermisson(false);
418
419 bool allow = false;
420 ASSERT_EQ(advancedNotificationService_->GetShowBadgeEnabledForBundle(
421 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), allow), ERR_ANS_PERMISSION_DENIED);
422 }
423
424 /**
425 * @tc.number : AnsBranchTest_239000
426 * @tc.name : Subscribe_1000
427 * @tc.desc : Test Subscribe function return ERR_ANS_NON_SYSTEM_APP.
428 * @tc.require : #I6P8UI
429 */
430 HWTEST_F(AnsBranchTest, AnsBranchTest_239000, Function | SmallTest | Level1)
431 {
432 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
433 MockIsSystemApp(false);
434
435 auto subscriber = new TestAnsSubscriber();
436 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
437 ASSERT_EQ(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_ANS_NON_SYSTEM_APP);
438 }
439
440 /**
441 * @tc.number : AnsBranchTest_240000
442 * @tc.name : Subscribe_1000
443 * @tc.desc : Test Subscribe function return ERR_ANS_PERMISSION_DENIED.
444 * @tc.require : #I6P8UI
445 */
446 HWTEST_F(AnsBranchTest, AnsBranchTest_240000, Function | SmallTest | Level1)
447 {
448 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
449 MockIsVerfyPermisson(false);
450
451 auto subscriber = new TestAnsSubscriber();
452 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
453 ASSERT_EQ(advancedNotificationService_->Subscribe(subscriber->GetImpl(), info), ERR_ANS_PERMISSION_DENIED);
454 }
455
456 /**
457 * @tc.number : AnsBranchTest_241000
458 * @tc.name : Unsubscribe_1000
459 * @tc.desc : Test Unsubscribe function return ERR_ANS_PERMISSION_DENIED.
460 * @tc.require : #I6P8UI
461 */
462 HWTEST_F(AnsBranchTest, AnsBranchTest_241000, Function | SmallTest | Level1)
463 {
464 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
465 MockIsVerfyPermisson(false);
466
467 auto subscriber = new TestAnsSubscriber();
468 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
469 ASSERT_EQ(advancedNotificationService_->Unsubscribe(subscriber->GetImpl(), info), ERR_ANS_INVALID_PARAM);
470 }
471
472 /**
473 * @tc.number : SubscribeSelf_279001
474 * @tc.require : issue
475 */
476 HWTEST_F(AnsBranchTest, SubscribeSelf_279001, Function | SmallTest | Level1)
477 {
478 auto res = advancedNotificationService_->SubscribeSelf(nullptr);
479 ASSERT_EQ(res, ERR_ANS_INVALID_PARAM);
480 }
481
482 /**
483 * @tc.number : SubscribeSelf_279002
484 * @tc.require : issue
485 */
486 HWTEST_F(AnsBranchTest, SubscribeSelf_279002, Function | SmallTest | Level1)
487 {
488 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
489 MockIsSystemApp(false);
490
491 auto subscriber = new TestAnsSubscriber();
492 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
493 ASSERT_EQ(advancedNotificationService_->SubscribeSelf(subscriber->GetImpl()), ERR_ANS_NON_SYSTEM_APP);
494 }
495
496 /**
497 * @tc.number : SubscribeSelf_279003
498 * @tc.require : issue
499 */
500 HWTEST_F(AnsBranchTest, SubscribeSelf_279003, Function | SmallTest | Level1)
501 {
502 MockIsSystemApp(true);
503
504 auto subscriber = new TestAnsSubscriber();
505 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
506 ASSERT_EQ(advancedNotificationService_->SubscribeSelf(subscriber->GetImpl()), ERR_OK);
507 }
508
509 /**
510 * @tc.number : AnsBranchTest_242000
511 * @tc.name : GetAllActiveNotifications_1000
512 * @tc.desc : Test GetAllActiveNotifications function return ERR_ANS_PERMISSION_DENIED.
513 * @tc.require : #I6P8UI
514 */
515 HWTEST_F(AnsBranchTest, AnsBranchTest_242000, Function | SmallTest | Level1)
516 {
517 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
518 MockIsVerfyPermisson(false);
519
520 std::vector<sptr<Notification>> allNotifications;
521 ASSERT_EQ(advancedNotificationService_->GetAllActiveNotifications(allNotifications), ERR_ANS_PERMISSION_DENIED);
522 }
523
524 /**
525 * @tc.number : AnsBranchTest_243000
526 * @tc.name : GetSpecialActiveNotifications_1000
527 * @tc.desc : Test GetSpecialActiveNotifications function return ERR_ANS_NON_SYSTEM_APP.
528 * @tc.require : #I6P8UI
529 */
530 HWTEST_F(AnsBranchTest, AnsBranchTest_243000, Function | SmallTest | Level1)
531 {
532 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
533 MockIsSystemApp(false);
534
535 std::vector<std::string> keys;
536 std::vector<sptr<Notification>> specialActiveNotifications;
537 ASSERT_EQ(advancedNotificationService_->GetSpecialActiveNotifications(
538 keys, specialActiveNotifications), ERR_ANS_NON_SYSTEM_APP);
539 }
540
541 /**
542 * @tc.number : AnsBranchTest_244000
543 * @tc.name : GetSpecialActiveNotifications_2000
544 * @tc.desc : Test GetSpecialActiveNotifications function return ERR_ANS_PERMISSION_DENIED.
545 * @tc.require : #I6P8UI
546 */
547 HWTEST_F(AnsBranchTest, AnsBranchTest_244000, Function | SmallTest | Level1)
548 {
549 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
550 MockIsVerfyPermisson(false);
551
552 std::vector<std::string> keys;
553 std::vector<sptr<Notification>> specialActiveNotifications;
554 ASSERT_EQ(advancedNotificationService_->GetSpecialActiveNotifications(
555 keys, specialActiveNotifications), ERR_ANS_PERMISSION_DENIED);
556 }
557
558 /**
559 * @tc.number : AnsBranchTest_245000
560 * @tc.name : SetNotificationsEnabledForAllBundles_2000
561 * @tc.desc : Test SetNotificationsEnabledForAllBundles function return ERR_ANS_NON_SYSTEM_APP.
562 * @tc.require : #I6P8UI
563 */
564 HWTEST_F(AnsBranchTest, AnsBranchTest_245000, Function | SmallTest | Level1)
565 {
566 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
567 MockIsSystemApp(false);
568
569 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForAllBundles(
570 std::string(), true), ERR_ANS_NON_SYSTEM_APP);
571 }
572
573 /**
574 * @tc.number : AnsBranchTest_246000
575 * @tc.name : SetNotificationsEnabledForAllBundles_1000
576 * @tc.desc : Test SetNotificationsEnabledForAllBundles function return ERR_ANS_PERMISSION_DENIED.
577 * @tc.require : #I6P8UI
578 */
579 HWTEST_F(AnsBranchTest, AnsBranchTest_246000, Function | SmallTest | Level1)
580 {
581 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
582 MockIsVerfyPermisson(false);
583
584 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForAllBundles(
585 std::string(), true), ERR_ANS_PERMISSION_DENIED);
586 }
587
588 /**
589 * @tc.number : AnsBranchTest_247000
590 * @tc.name : SetNotificationsEnabledForSpecialBundle_1000
591 * @tc.desc : Test SetNotificationsEnabledForSpecialBundle function return ERR_ANS_PERMISSION_DENIED.
592 * @tc.require : #I6P8UI
593 */
594 HWTEST_F(AnsBranchTest, AnsBranchTest_247000, Function | SmallTest | Level1)
595 {
596 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
597 MockIsVerfyPermisson(false);
598
599 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
600 std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false),
601 ERR_ANS_PERMISSION_DENIED);
602 }
603
604 /**
605 * @tc.number : AnsBranchTest_248000
606 * @tc.name : IsAllowedNotify_1000
607 * @tc.desc : Test IsAllowedNotify function return ERR_ANS_NON_SYSTEM_APP.
608 * @tc.require : #I6P8UI
609 */
610 HWTEST_F(AnsBranchTest, AnsBranchTest_248000, Function | SmallTest | Level1)
611 {
612 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
613 MockIsSystemApp(false);
614
615 bool allowed = false;
616 ASSERT_EQ(advancedNotificationService_->IsAllowedNotify(allowed), ERR_ANS_NON_SYSTEM_APP);
617 }
618
619 /**
620 * @tc.number : AnsBranchTest_249000
621 * @tc.name : IsAllowedNotify_2000
622 * @tc.desc : Test IsAllowedNotify function return ERR_ANS_PERMISSION_DENIED.
623 * @tc.require : #I6P8UI
624 */
625 HWTEST_F(AnsBranchTest, AnsBranchTest_249000, Function | SmallTest | Level1)
626 {
627 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
628 MockIsVerfyPermisson(false);
629
630 bool allowed = false;
631 ASSERT_EQ(advancedNotificationService_->IsAllowedNotify(allowed), ERR_ANS_PERMISSION_DENIED);
632 }
633
634 /**
635 * @tc.number : AnsBranchTest_250000
636 * @tc.name : GetAppTargetBundle_1000
637 * @tc.desc : Test GetAppTargetBundle function return ERR_ANS_NON_SYSTEM_APP.
638 * @tc.require : #I6P8UI
639 */
640 HWTEST_F(AnsBranchTest, AnsBranchTest_250000, Function | SmallTest | Level1)
641 {
642 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
643 MockIsSystemApp(false);
644
645 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
646 sptr<NotificationBundleOption> targetBundle(nullptr);
647 bundleOption->SetBundleName("test");
648 ASSERT_EQ(advancedNotificationService_->GetAppTargetBundle(bundleOption, targetBundle), ERR_ANS_NON_SYSTEM_APP);
649 }
650
651 /**
652 * @tc.number : AnsBranchTest_251000
653 * @tc.name : IsSpecialBundleAllowedNotify_1000
654 * @tc.desc : Test IsSpecialBundleAllowedNotify function return ERR_ANS_NON_SYSTEM_APP.
655 * @tc.require : #I6P8UI
656 */
657 HWTEST_F(AnsBranchTest, AnsBranchTest_251000, Function | SmallTest | Level1)
658 {
659 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
660 MockIsSystemApp(false);
661
662 bool allowed = true;
663 ASSERT_EQ(advancedNotificationService_->IsSpecialBundleAllowedNotify(
664 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), allowed), ERR_ANS_NON_SYSTEM_APP);
665 }
666
667 /**
668 * @tc.number : AnsBranchTest_252000
669 * @tc.name : IsSpecialBundleAllowedNotify_2000
670 * @tc.desc : Test IsSpecialBundleAllowedNotify function return ERR_ANS_PERMISSION_DENIED.
671 * @tc.require : #I6P8UI
672 */
673 HWTEST_F(AnsBranchTest, AnsBranchTest_252000, Function | SmallTest | Level1)
674 {
675 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
676 MockIsVerfyPermisson(false);
677
678 bool allowed = true;
679 ASSERT_EQ(advancedNotificationService_->IsSpecialBundleAllowedNotify(
680 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), allowed), ERR_ANS_PERMISSION_DENIED);
681 }
682
683 /**
684 * @tc.number : AnsBranchTest_254000
685 * @tc.name : IsSpecialBundleAllowedNotify_4000
686 * @tc.desc : Test IsSpecialBundleAllowedNotify function return ERR_ANS_PERMISSION_DENIED.
687 * @tc.require : #I6P8UI
688 */
689 HWTEST_F(AnsBranchTest, AnsBranchTest_254000, Function | SmallTest | Level1)
690 {
691 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
692 MockIsVerfyPermisson(true);
693
694 MockIsNonBundleName(true);
695 bool allowed = true;
696 ASSERT_EQ(advancedNotificationService_->IsSpecialBundleAllowedNotify(
697 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), allowed), ERR_ANS_INVALID_BUNDLE);
698 }
699
700 /**
701 * @tc.number : AnsBranchTest_255000
702 * @tc.name : RemoveNotification_1000
703 * @tc.desc : Test RemoveNotification function return ERR_ANS_PERMISSION_DENIED.
704 * @tc.require : #I6P8UI
705 */
706 HWTEST_F(AnsBranchTest, AnsBranchTest_255000, Function | SmallTest | Level1)
707 {
708 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
709 MockIsVerfyPermisson(false);
710
711 int32_t notificationId = 1;
712 std::string label = "testRemove";
713 auto result = advancedNotificationService_->RemoveNotification(
714 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID),
715 notificationId, label, NotificationConstant::CANCEL_REASON_DELETE);
716 ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
717 }
718
719 /**
720 * @tc.number : AnsBranchTest_256000
721 * @tc.name : RemoveAllNotifications_1000
722 * @tc.desc : Test RemoveAllNotifications function return ERR_ANS_PERMISSION_DENIED.
723 * @tc.require : #I6P8UI
724 */
725 HWTEST_F(AnsBranchTest, AnsBranchTest_256000, Function | SmallTest | Level1)
726 {
727 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
728 MockIsVerfyPermisson(false);
729
730 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
731 ASSERT_EQ(advancedNotificationService_->RemoveAllNotifications(bundleOption), ERR_ANS_PERMISSION_DENIED);
732 }
733
734 /**
735 * @tc.number : AnsBranchTest_257000
736 * @tc.name : GetSlotNumAsBundle_1000
737 * @tc.desc : Test GetSlotNumAsBundle function return ERR_ANS_NON_SYSTEM_APP.
738 * @tc.require : #I6P8UI
739 */
740 HWTEST_F(AnsBranchTest, AnsBranchTest_257000, Function | SmallTest | Level1)
741 {
742 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
743 MockIsSystemApp(false);
744
745 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
746 uint64_t num = 1;
747 ASSERT_EQ(advancedNotificationService_->GetSlotNumAsBundle(bundleOption, num), ERR_ANS_NON_SYSTEM_APP);
748 }
749
750 /**
751 * @tc.number : AnsBranchTest_258000
752 * @tc.name : GetSlotNumAsBundle_2000
753 * @tc.desc : Test GetSlotNumAsBundle function return ERR_ANS_PERMISSION_DENIED.
754 * @tc.require : #I6P8UI
755 */
756 HWTEST_F(AnsBranchTest, AnsBranchTest_258000, Function | SmallTest | Level1)
757 {
758 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
759 MockIsVerfyPermisson(false);
760
761 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
762 uint64_t num = 1;
763 ASSERT_EQ(advancedNotificationService_->GetSlotNumAsBundle(bundleOption, num), ERR_ANS_PERMISSION_DENIED);
764 }
765
766 /**
767 * @tc.number : AnsBranchTest_259000
768 * @tc.name : RemoveGroupByBundle_2000
769 * @tc.desc : Test RemoveGroupByBundle function return ERR_ANS_PERMISSION_DENIED.
770 * @tc.require : #I6P8UI
771 */
772 HWTEST_F(AnsBranchTest, AnsBranchTest_259000, Function | SmallTest | Level1)
773 {
774 MockVerifyNativeToken(false);
775 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
776 MockIsVerfyPermisson(false);
777
778 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
779 std::string groupName = "group";
780 ASSERT_EQ(advancedNotificationService_->RemoveGroupByBundle(bundleOption, groupName), ERR_ANS_PERMISSION_DENIED);
781 }
782
783 /**
784 * @tc.number : AnsBranchTest_260000
785 * @tc.name : SetDoNotDisturbDate_1000
786 * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_NON_SYSTEM_APP.
787 * @tc.require : #I6P8UI
788 */
789 HWTEST_F(AnsBranchTest, AnsBranchTest_260000, Function | SmallTest | Level1)
790 {
791 MockVerifyNativeToken(false);
792 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
793 MockIsSystemApp(false);
794
795 sptr<NotificationDoNotDisturbDate> date =
796 new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
797 ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(date), ERR_ANS_NON_SYSTEM_APP);
798 ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(date), ERR_ANS_NON_SYSTEM_APP);
799 }
800
801 /**
802 * @tc.number : AnsBranchTest_261000
803 * @tc.name : SetDoNotDisturbDate_2000
804 * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_PERMISSION_DENIED.
805 * @tc.require : #I6P8UI
806 */
807 HWTEST_F(AnsBranchTest, AnsBranchTest_261000, Function | SmallTest | Level1)
808 {
809 MockVerifyNativeToken(false);
810 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
811 MockIsVerfyPermisson(false);
812
813 sptr<NotificationDoNotDisturbDate> date =
814 new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
815 ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(date), ERR_ANS_PERMISSION_DENIED);
816 ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(date), ERR_ANS_PERMISSION_DENIED);
817 }
818
819 /**
820 * @tc.number : AnsBranchTest_262000
821 * @tc.name : DoesSupportDoNotDisturbMode_1000
822 * @tc.desc : Test DoesSupportDoNotDisturbMode function return ERR_ANS_NON_SYSTEM_APP.
823 * @tc.require : #I6P8UI
824 */
825 HWTEST_F(AnsBranchTest, AnsBranchTest_262000, Function | SmallTest | Level1)
826 {
827 MockIsSystemApp(false);
828 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
829
830 bool doesSupport = true;
831 ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(doesSupport), ERR_ANS_NON_SYSTEM_APP);
832 }
833
834 /**
835 * @tc.number : AnsBranchTest_263000
836 * @tc.name : DoesSupportDoNotDisturbMode_2000
837 * @tc.desc : Test DoesSupportDoNotDisturbMode function return ERR_ANS_PERMISSION_DENIED.
838 * @tc.require : #I6P8UI
839 */
840 HWTEST_F(AnsBranchTest, AnsBranchTest_263000, Function | SmallTest | Level1)
841 {
842 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
843 MockIsVerfyPermisson(false);
844
845 bool doesSupport = true;
846 ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(doesSupport), ERR_ANS_PERMISSION_DENIED);
847 }
848
849 /**
850 * @tc.number : AnsBranchTest_264000
851 * @tc.name : EnableDistributed_1000
852 * @tc.desc : Test EnableDistributed function return ERR_ANS_NON_SYSTEM_APP.
853 * @tc.require : #I6P8UI
854 */
855 HWTEST_F(AnsBranchTest, AnsBranchTest_264000, Function | SmallTest | Level1)
856 {
857 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
858 MockIsSystemApp(false);
859
860 bool enabled = true;
861 sptr<NotificationBundleOption> bundleOption =
862 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
863 ASSERT_EQ(advancedNotificationService_->EnableDistributed(enabled), ERR_ANS_NON_SYSTEM_APP);
864 ASSERT_EQ(advancedNotificationService_->EnableDistributedByBundle(bundleOption, enabled), ERR_ANS_NON_SYSTEM_APP);
865 ASSERT_EQ(advancedNotificationService_->IsDistributedEnableByBundle(bundleOption, enabled), ERR_ANS_NON_SYSTEM_APP);
866 }
867
868 /**
869 * @tc.number : AnsBranchTest_284000
870 * @tc.name : EnableDistributed_2000
871 * @tc.desc : Test EnableDistributed function return ERR_ANS_PERMISSION_DENIED.
872 * @tc.require : #I6P8UI
873 */
874 HWTEST_F(AnsBranchTest, AnsBranchTest_284000, Function | SmallTest | Level1)
875 {
876 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
877 MockIsVerfyPermisson(false);
878
879 bool enabled = true;
880 sptr<NotificationBundleOption> bundleOption =
881 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
882 ASSERT_EQ(advancedNotificationService_->EnableDistributed(enabled), ERR_ANS_PERMISSION_DENIED);
883 ASSERT_EQ(advancedNotificationService_->EnableDistributedByBundle(
884 bundleOption, enabled), ERR_ANS_PERMISSION_DENIED);
885 ASSERT_EQ(advancedNotificationService_->IsDistributedEnableByBundle(
886 bundleOption, enabled), ERR_ANS_PERMISSION_DENIED);
887 }
888
889 /**
890 * @tc.number : AnsBranchTest_265000
891 * @tc.name : GetDeviceRemindType_1000
892 * @tc.desc : Test GetDeviceRemindType function return ERR_ANS_NON_SYSTEM_APP.
893 * @tc.require : #I6P8UI
894 */
895 HWTEST_F(AnsBranchTest, AnsBranchTest_265000, Function | SmallTest | Level1)
896 {
897 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
898 MockIsSystemApp(false);
899
900 int32_t remindType = -1;
901 ASSERT_EQ(advancedNotificationService_->GetDeviceRemindType(remindType), ERR_ANS_NON_SYSTEM_APP);
902 }
903
904 /**
905 * @tc.number : AnsBranchTest_266000
906 * @tc.name : GetDeviceRemindType_2000
907 * @tc.desc : Test GetDeviceRemindType function return ERR_ANS_PERMISSION_DENIED.
908 * @tc.require : #I6P8UI
909 */
910 HWTEST_F(AnsBranchTest, AnsBranchTest_266000, Function | SmallTest | Level1)
911 {
912 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
913 MockIsVerfyPermisson(false);
914
915 int32_t remindType = -1;
916 ASSERT_EQ(advancedNotificationService_->GetDeviceRemindType(remindType), ERR_ANS_PERMISSION_DENIED);
917 }
918
919 /**
920 * @tc.number : AnsBranchTest_267000
921 * @tc.name : IsSpecialUserAllowedNotify_1000
922 * @tc.desc : Test IsSpecialUserAllowedNotify function return ERR_ANS_PERMISSION_DENIED.
923 * @tc.require : #I6P8UI
924 */
925 HWTEST_F(AnsBranchTest, AnsBranchTest_267000, Function | SmallTest | Level1)
926 {
927 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
928 MockIsVerfyPermisson(false);
929
930 int32_t userId = 3;
931 bool allowed = true;
932 bool enable = true;
933 ASSERT_EQ(advancedNotificationService_->IsSpecialUserAllowedNotify(
934 userId, allowed), (int)ERR_ANS_PERMISSION_DENIED);
935 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledByUser(
936 userId, enable), (int)ERR_ANS_PERMISSION_DENIED);
937 }
938
939 /**
940 * @tc.number : AnsBranchTest_267100
941 * @tc.name : IsSpecialUserAllowedNotify_1000
942 * @tc.desc : Test IsSpecialUserAllowedNotify function return ERR_ANS_NON_SYSTEM_APP.
943 * @tc.require : #I6P8UI
944 */
945 HWTEST_F(AnsBranchTest, AnsBranchTest_267100, Function | SmallTest | Level1)
946 {
947 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
948 MockIsSystemApp(false);
949
950 int32_t userId = 3;
951 bool allowed = true;
952 bool enable = true;
953 ASSERT_EQ(advancedNotificationService_->IsSpecialUserAllowedNotify(
954 userId, allowed), (int)ERR_ANS_NON_SYSTEM_APP);
955 ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledByUser(
956 userId, enable), (int)ERR_ANS_NON_SYSTEM_APP);
957 }
958
959 /**
960 * @tc.number : AnsBranchTest_268000
961 * @tc.name : SetDoNotDisturbDate_1000
962 * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_NON_SYSTEM_APP.
963 * @tc.require : #I6P8UI
964 */
965 HWTEST_F(AnsBranchTest, AnsBranchTest_268000, Function | SmallTest | Level1)
966 {
967 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
968 MockIsSystemApp(false);
969
970 int32_t userId = 3;
971 sptr<NotificationDoNotDisturbDate> date = nullptr;
972 ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_NON_SYSTEM_APP);
973 ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_ANS_NON_SYSTEM_APP);
974 }
975
976 /**
977 * @tc.number : AnsBranchTest_269000
978 * @tc.name : SetDoNotDisturbDate_2000
979 * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_PERMISSION_DENIED.
980 * @tc.require : #I6P8UI
981 */
982 HWTEST_F(AnsBranchTest, AnsBranchTest_269000, Function | SmallTest | Level1)
983 {
984 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
985 MockIsVerfyPermisson(false);
986
987 int32_t userId = 3;
988 sptr<NotificationDoNotDisturbDate> date = nullptr;
989 ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_PERMISSION_DENIED);
990 ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_ANS_PERMISSION_DENIED);
991 }
992
993 /**
994 * @tc.number : AnsBranchTest_270000
995 * @tc.name : SetEnabledForBundleSlot_1000
996 * @tc.desc : Test SetEnabledForBundleSlot function return ERR_ANS_PERMISSION_DENIED.
997 * @tc.require : #I6P8UI
998 */
999 HWTEST_F(AnsBranchTest, AnsBranchTest_270000, Function | SmallTest | Level1)
1000 {
1001 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1002 MockIsVerfyPermisson(false);
1003
1004 bool enabled = false;
1005 bool isForceControl = false;
1006 auto result = advancedNotificationService_->SetEnabledForBundleSlot(
1007 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID),
1008 NotificationConstant::SlotType::SOCIAL_COMMUNICATION, enabled, false);
1009
1010 ASSERT_EQ(result, ERR_ANS_PERMISSION_DENIED);
1011 auto result1 = advancedNotificationService_->GetEnabledForBundleSlot(
1012 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID),
1013 NotificationConstant::SlotType::SOCIAL_COMMUNICATION, enabled);
1014 ASSERT_EQ(result1, ERR_ANS_PERMISSION_DENIED);
1015 }
1016
1017 /**
1018 * @tc.number : AnsBranchTest_271000
1019 * @tc.name : SetEnabledForBundleSlot_1000
1020 * @tc.desc : Test SetEnabledForBundleSlot function return ERR_ANS_PERMISSION_DENIED.
1021 * @tc.require : #I6P8UI
1022 */
1023 HWTEST_F(AnsBranchTest, AnsBranchTest_271000, Function | SmallTest | Level1)
1024 {
1025 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1026 MockVerifyShellToken(false);
1027
1028 std::string cmd = "CMD";
1029 std::string bundle = "Bundle";
1030 int32_t userId = 4;
1031 std::vector<std::string> dumpInfo;
1032 ASSERT_EQ(advancedNotificationService_->ShellDump(
1033 cmd, bundle, userId, 0, dumpInfo), (int)ERR_ANS_PERMISSION_DENIED);
1034 }
1035
1036 /**
1037 * @tc.number : AnsBranchTest_272000
1038 * @tc.name : SetSyncNotificationEnabledWithoutApp_1000
1039 * @tc.desc : Test SetSyncNotificationEnabledWithoutApp function return ERR_ANS_NON_SYSTEM_APP.
1040 * @tc.require : #I6P8UI
1041 */
1042 HWTEST_F(AnsBranchTest, AnsBranchTest_272000, Function | SmallTest | Level1)
1043 {
1044 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1045 MockIsSystemApp(false);
1046
1047 int32_t userId = 3;
1048 bool enabled = true;
1049 ASSERT_EQ(advancedNotificationService_->SetSyncNotificationEnabledWithoutApp(
1050 userId, enabled), ERR_ANS_NON_SYSTEM_APP);
1051 ASSERT_EQ(advancedNotificationService_->GetSyncNotificationEnabledWithoutApp(
1052 userId, enabled), ERR_ANS_NON_SYSTEM_APP);
1053 }
1054
1055 /**
1056 * @tc.number : AnsBranchTest_273000
1057 * @tc.name : SetSyncNotificationEnabledWithoutApp_2000
1058 * @tc.desc : Test SetSyncNotificationEnabledWithoutApp function return ERR_ANS_PERMISSION_DENIED.
1059 * @tc.require : #I6P8UI
1060 */
1061 HWTEST_F(AnsBranchTest, AnsBranchTest_273000, Function | SmallTest | Level1)
1062 {
1063 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1064 MockIsVerfyPermisson(false);
1065
1066 int32_t userId = 3;
1067 bool enabled = true;
1068 ASSERT_EQ(advancedNotificationService_->SetSyncNotificationEnabledWithoutApp(
1069 userId, enabled), ERR_ANS_PERMISSION_DENIED);
1070 ASSERT_EQ(advancedNotificationService_->GetSyncNotificationEnabledWithoutApp(
1071 userId, enabled), ERR_ANS_PERMISSION_DENIED);
1072 }
1073
1074 /**
1075 * @tc.number : AnsBranchTest_274000
1076 * @tc.name : EnableDistributedByBundle_3000
1077 * @tc.desc : Test EnableDistributedByBundle function return ERR_ANS_NON_SYSTEM_APP.
1078 * @tc.require : #I6P8UI
1079 */
1080 HWTEST_F(AnsBranchTest, AnsBranchTest_274000, Function | SmallTest | Level1)
1081 {
1082 MockGetDistributedEnableInApplicationInfo(false, 2);
1083 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1084 MockIsSystemApp(false);
1085 bool enabled = true;
1086 sptr<NotificationBundleOption> bundleOption =
1087 new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID);
1088 ASSERT_EQ(advancedNotificationService_->EnableDistributedByBundle(
1089 bundleOption, enabled), ERR_ANS_NON_SYSTEM_APP);
1090 }
1091
1092 /**
1093 * @tc.number : AnsBranchTest_275000
1094 * @tc.name : EnableDistributedSelf_2000
1095 * @tc.desc : Test EnableDistributedSelf function return ERR_ANS_PERMISSION_DENIED.
1096 * @tc.require : #I6P8UI
1097 */
1098 HWTEST_F(AnsBranchTest, AnsBranchTest_275000, Function | SmallTest | Level1)
1099 {
1100 MockDistributedNotificationEnabled(false);
1101 MockIsNonBundleName(false);
1102 bool enabled = true;
1103 ASSERT_EQ(advancedNotificationService_->EnableDistributedSelf(enabled), (int)ERR_ANS_PERMISSION_DENIED);
1104 }
1105
1106 /**
1107 * @tc.number : AnsBranchTest_276000
1108 * @tc.name : IsDistributedEnableByBundle_3000
1109 * @tc.desc : Test IsDistributedEnableByBundle function return ERR_ANS_PERMISSION_DENIED.
1110 * @tc.require : #I6P8UI
1111 */
1112 HWTEST_F(AnsBranchTest, AnsBranchTest_276000, Function | SmallTest | Level1)
1113 {
1114 MockIsSystemApp(true);
1115 MockIsVerfyPermisson(true);
1116 MockVerifyNativeToken(true);
1117 MockGetDistributedEnableInApplicationInfo(true, 2);
1118 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(
1119 TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1120 bool enabled = false;
1121 ASSERT_EQ(advancedNotificationService_->IsDistributedEnableByBundle(bundleOption, enabled), ERR_OK);
1122 }
1123
1124 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1125 /**
1126 * @tc.number : AnsBranchTest_277000
1127 * @tc.name : DoDistributedPublish_3000
1128 * @tc.desc : Test DoDistributedPublish function return ERR_ANS_PERMISSION_DENIED.
1129 * @tc.require : #I6P8UI
1130 */
1131 HWTEST_F(AnsBranchTest, AnsBranchTest_277000, Function | SmallTest | Level1)
1132 {
1133 MockGetDistributedEnableInApplicationInfo(false, 2);
1134 MockGetOsAccountLocalIdFromUid(false, 1);
1135 MockDistributedNotificationEnabled(false);
1136 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(
1137 TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1138 std::shared_ptr<NotificationRecord> record = nullptr;
1139 ASSERT_EQ(advancedNotificationService_->DoDistributedPublish(bundleOption, record), ERR_OK);
1140 }
1141
1142 /**
1143 * @tc.number : DoDistributedPublish_4000
1144 * @tc.name : DoDistributedPublish
1145 * @tc.desc : Test DoDistributedPublish function return ERR_ANS_MISSIONPER_DENIED.
1146 * @tc.require : #I6P8UI
1147 */
1148 HWTEST_F(AnsBranchTest, DoDistributedPublish_4000, Function | SmallTest | Level1)
1149 {
1150 MockGetDistributedEnableInApplicationInfo(true, 1);
1151 MockGetOsAccountLocalIdFromUid(false, 2);
1152 MockDistributedNotificationEnabled(true);
1153 advancedNotificationService_->EnableDistributed(true);
1154 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(
1155 TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1156 int notificationId = 1;
1157 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
1158 request->SetNotificationId(notificationId);
1159 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundleOption);
1160 ASSERT_EQ(advancedNotificationService_->DoDistributedPublish(bundleOption, record),
1161 (int)ERR_ANS_DISTRIBUTED_GET_INFO_FAILED);
1162 auto ret = advancedNotificationService_->DoDistributedDelete(
1163 "1", "DoDistributedPublish_4000", record->notification);
1164 ASSERT_EQ(ret, (int)ERR_ANS_DISTRIBUTED_OPERATION_FAILED);
1165 }
1166
1167 /**
1168 * @tc.number : AnsBranchTest_278000
1169 * @tc.name : GetDistributedEnableInApplicationInfo_3000
1170 * @tc.desc : Test GetDistributedEnableInApplicationInfo function return ERR_ANS_PERMISSION_DENIED.
1171 * @tc.require : #I6P8UI
1172 */
1173 HWTEST_F(AnsBranchTest, AnsBranchTest_278000, Function | SmallTest | Level1)
1174 {
1175 MockGetDistributedEnableInApplicationInfo(false, 2);
1176 MockGetOsAccountLocalIdFromUid(false, 3);
1177 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(
1178 TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID);
1179 bool enabled = false;;
1180 ASSERT_EQ(advancedNotificationService_->GetDistributedEnableInApplicationInfo(
1181 bundleOption, enabled), ERR_ANS_INVALID_PARAM);
1182 }
1183 #endif
1184
InitNotificationRecord(std::shared_ptr<NotificationRecord> & record,const NotificationLiveViewContent::LiveViewStatus & status)1185 void AnsBranchTest::InitNotificationRecord(std::shared_ptr<NotificationRecord> &record,
1186 const NotificationLiveViewContent::LiveViewStatus &status)
1187 {
1188 NotificationRequest notificationRequest;
1189 notificationRequest.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1190 auto liveContent = std::make_shared<NotificationLiveViewContent>();
1191 liveContent->SetLiveViewStatus(status);
1192 auto content = std::make_shared<NotificationContent>(liveContent);
1193 notificationRequest.SetContent(content);
1194
1195 record->request = sptr<NotificationRequest>::MakeSptr(notificationRequest);
1196 record->notification = new (std::nothrow) Notification(record->request);
1197 }
1198
1199 /**
1200 * @tc.number : AnsBranchTest_279000
1201 * @tc.name : UpdateNotificationTimerInfo_0001
1202 * @tc.desc : Check set update and finish timer when create notification request
1203 * @tc.require : issue
1204 */
1205 HWTEST_F(AnsBranchTest, AnsBranchTest_279000, Function | SmallTest | Level1)
1206 {
1207 using Status = NotificationLiveViewContent::LiveViewStatus;
1208 auto record = std::make_shared<NotificationRecord>();
1209 InitNotificationRecord(record, Status::LIVE_VIEW_CREATE);
1210 ASSERT_EQ(record->notification->GetFinishTimer(), NotificationConstant::INVALID_TIMER_ID);
1211 ASSERT_EQ(record->notification->GetUpdateTimer(), NotificationConstant::INVALID_TIMER_ID);
1212 auto result = advancedNotificationService_->UpdateNotificationTimerInfo(record);
1213 ASSERT_EQ(result, ERR_OK);
1214 EXPECT_NE(record->notification->GetFinishTimer(), NotificationConstant::INVALID_TIMER_ID);
1215 EXPECT_NE(record->notification->GetUpdateTimer(), NotificationConstant::INVALID_TIMER_ID);
1216 }
1217
1218 /**
1219 * @tc.number : AnsBranchTest_279001
1220 * @tc.name : UpdateNotificationTimerInfo_0002
1221 * @tc.desc : Check set update and finish timer when update notification request
1222 * @tc.require : issue
1223 */
1224 HWTEST_F(AnsBranchTest, AnsBranchTest_279001, Function | SmallTest | Level1)
1225 {
1226 using Status = NotificationLiveViewContent::LiveViewStatus;
1227 auto record = std::make_shared<NotificationRecord>();
1228 InitNotificationRecord(record, Status::LIVE_VIEW_INCREMENTAL_UPDATE);
1229 record->notification->SetUpdateTimer(2);
1230 record->notification->SetFinishTimer(3);
1231 auto result = advancedNotificationService_->UpdateNotificationTimerInfo(record);
1232 ASSERT_EQ(result, ERR_OK);
1233 /* finish timer not change, but update timer changed */
1234 EXPECT_NE(record->notification->GetUpdateTimer(), 2);
1235 ASSERT_EQ(record->notification->GetFinishTimer(), 3);
1236 }
1237
1238 /**
1239 * @tc.number : AnsBranchTest_279002
1240 * @tc.name : UpdateNotificationTimerInfo_0003
1241 * @tc.desc : Check cancel update and finish timer when end notification request
1242 * @tc.require : issue
1243 */
1244 HWTEST_F(AnsBranchTest, AnsBranchTest_279002, Function | SmallTest | Level1)
1245 {
1246 using Status = NotificationLiveViewContent::LiveViewStatus;
1247 auto record = std::make_shared<NotificationRecord>();
1248 InitNotificationRecord(record, Status::LIVE_VIEW_END);
1249 record->notification->SetUpdateTimer(2);
1250 record->notification->SetFinishTimer(3);
1251
1252 auto result = advancedNotificationService_->UpdateNotificationTimerInfo(record);
1253 ASSERT_EQ(result, ERR_OK);
1254 /* finish timer not change, but update timer changed */
1255 ASSERT_EQ(record->notification->GetUpdateTimer(), NotificationConstant::INVALID_TIMER_ID);
1256 ASSERT_EQ(record->notification->GetFinishTimer(), NotificationConstant::INVALID_TIMER_ID);
1257 }
1258
1259 HWTEST_F(AnsBranchTest, AnsBranchTest_279003, Function | SmallTest | Level1)
1260 {
1261 auto record = std::make_shared<NotificationRecord>();
1262 NotificationRequest notificationRequest;
1263 notificationRequest.SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1264 auto basicContent = std::make_shared<NotificationNormalContent>();
1265 auto content = std::make_shared<NotificationContent>(basicContent);
1266 notificationRequest.SetContent(content);
1267
1268 record->request = sptr<NotificationRequest>::MakeSptr(notificationRequest);
1269 record->notification = new (std::nothrow) Notification(record->request);
1270
1271 auto result = advancedNotificationService_->UpdateNotificationTimerInfo(record);
1272
1273 ASSERT_EQ(result, ERR_OK);
1274 /* finish timer not change, but update timer changed */
1275 ASSERT_EQ(record->notification->GetAutoDeletedTimer(), NotificationConstant::INVALID_TIMER_ID);
1276 }
1277
1278 /**
1279 * @tc.number : GetDeviceRemindType_3000
1280 * @tc.name : GetDeviceRemindType_3000
1281 * @tc.desc : Test GetDeviceRemindType function return ERR_ANS_INVALID_PARAM.
1282 * @tc.require : #I6P8UI
1283 */
1284 HWTEST_F(AnsBranchTest, GetDeviceRemindType_3000, Function | SmallTest | Level1)
1285 {
1286 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1287 MockIsVerfyPermisson(true);
1288 MockIsVerfyPermisson(true);
1289 AdvancedNotificationService ans;
1290 ans.notificationSvrQueue_ = nullptr;
1291 int32_t remindType = -1;
1292 ASSERT_EQ(ans.GetDeviceRemindType(remindType), ERR_ANS_INVALID_PARAM);
1293 }
1294
1295 /**
1296 * @tc.number : AnsBranchTest_285000
1297 * @tc.name : IsNeedSilentInDoNotDisturbMode_1000
1298 * @tc.desc : Test IsNeedSilentInDoNotDisturbMode function return ERR_ANS_NON_SYSTEM_APP.
1299 */
1300 HWTEST_F(AnsBranchTest, AnsBranchTest_285000, Function | SmallTest | Level1)
1301 {
1302 MockIsSystemApp(false);
1303 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1304
1305 std::string phoneNumber = "11111111111";
1306 int32_t callerType = 0;
1307 ASSERT_EQ(advancedNotificationService_->IsNeedSilentInDoNotDisturbMode(
1308 phoneNumber, callerType), -1);
1309 }
1310
1311 /**
1312 * @tc.number : AnsBranchTest_286000
1313 * @tc.name : IsNeedSilentInDoNotDisturbMode_2000
1314 * @tc.desc : Test IsNeedSilentInDoNotDisturbMode function return ERR_ANS_PERMISSION_DENIED.
1315 */
1316 HWTEST_F(AnsBranchTest, AnsBranchTest_286000, Function | SmallTest | Level1)
1317 {
1318 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
1319 MockIsVerfyPermisson(false);
1320
1321 std::string phoneNumber = "11111111111";
1322 int32_t callerType = 0;
1323 ASSERT_EQ(advancedNotificationService_->IsNeedSilentInDoNotDisturbMode(
1324 phoneNumber, callerType), ERR_ANS_PERMISSION_DENIED);
1325 }
1326 } // namespace Notification
1327 } // namespace OHOS
1328