1 /*
2 * Copyright (c) 2021-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 "gtest/gtest.h"
17
18 #include <thread>
19 #include "advanced_notification_flow_control_service.h"
20 #include "ans_const_define.h"
21 #include "ans_inner_errors.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Notification {
27 class FlowControlServiceTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void FlowControlServiceTest::SetUpTestCase() {}
36
TearDownTestCase()37 void FlowControlServiceTest::TearDownTestCase() {}
38
SetUp()39 void FlowControlServiceTest::SetUp() {}
40
TearDown()41 void FlowControlServiceTest::TearDown() {}
42
GetCommonNotificationRecord()43 std::shared_ptr<NotificationRecord> GetCommonNotificationRecord()
44 {
45 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
46 sptr<Notification> notification = new (std::nothrow) Notification(request);
47 auto record = std::make_shared<NotificationRecord>();
48 record->request = request;
49 record->notification = notification;
50 record->isNeedFlowCtrl = true;
51 return record;
52 }
53
GetLiveviewNotificationRecord()54 std::shared_ptr<NotificationRecord> GetLiveviewNotificationRecord()
55 {
56 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
57 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
58 request->SetSlotType(slotType);
59 auto liveContent = std::make_shared<NotificationLiveViewContent>();
60 auto content = std::make_shared<NotificationContent>(liveContent);
61 request->SetContent(content);
62 sptr<Notification> notification = new (std::nothrow) Notification(request);
63 auto record = std::make_shared<NotificationRecord>();
64 record->request = request;
65 record->notification = notification;
66 record->isNeedFlowCtrl = true;
67 return record;
68 }
69
70 /**
71 * @tc.name: FlowControl_100
72 * @tc.desc: Test FlowControl when no need to flow control
73 * @tc.type: FUNC
74 * @tc.require: issue
75 */
76 HWTEST_F(FlowControlServiceTest, FlowControl_100, Function | SmallTest | Level1)
77 {
78 std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
79 record->isNeedFlowCtrl = false;
80 auto result = FlowControlService::GetInstance().FlowControl(record, DEFAULT_UID, false);
81 ASSERT_EQ(result, ERR_OK);
82 }
83
84 /**
85 * @tc.name: FlowControl_200
86 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_SYSTEM_NORMAL_CREATE
87 * @tc.type: FUNC
88 * @tc.require: issue
89 */
90 HWTEST_F(FlowControlServiceTest, FlowControl_200, Function | SmallTest | Level1)
91 {
92 auto record = GetCommonNotificationRecord();
93 record->isThirdparty = false;
94 int32_t uid = 1000;
95 int32_t index = 1;
96 ErrCode result = ERR_OK;
97 uint32_t totalCreate = 0;
98 while (totalCreate + MAX_CREATE_NUM_PERSECOND_PERAPP < MAX_CREATE_NUM_PERSECOND) {
99 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
100 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
101 }
102 totalCreate += MAX_CREATE_NUM_PERSECOND_PERAPP;
103 index++;
104 }
105
106 int gap = MAX_CREATE_NUM_PERSECOND - totalCreate;
107 for (int i = 0; i < gap; i++) {
108 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
109 }
110 ASSERT_EQ(result, ERR_OK);
111
112 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
113 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
114 std::this_thread::sleep_for(std::chrono::seconds(1));
115 }
116
117 /**
118 * @tc.name: FlowControl_300
119 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_SYSTEM_NORMAL_UPDATE
120 * @tc.type: FUNC
121 * @tc.require: issue
122 */
123 HWTEST_F(FlowControlServiceTest, FlowControl_300, Function | SmallTest | Level1)
124 {
125 auto record = GetCommonNotificationRecord();
126 record->isThirdparty = false;
127 int32_t uid = 1000;
128 int32_t index = 1;
129 ErrCode result = ERR_OK;
130 uint32_t totalCreate = 0;
131 while (totalCreate + MAX_UPDATE_NUM_PERSECOND_PERAPP < MAX_UPDATE_NUM_PERSECOND) {
132 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
133 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
134 }
135 totalCreate += MAX_UPDATE_NUM_PERSECOND_PERAPP;
136 index++;
137 }
138
139 int gap = MAX_UPDATE_NUM_PERSECOND - totalCreate;
140 for (int i = 0; i < gap; i++) {
141 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
142 }
143 ASSERT_EQ(result, ERR_OK);
144
145 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
146 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
147 std::this_thread::sleep_for(std::chrono::seconds(1));
148 }
149
150 /**
151 * @tc.name: FlowControl_400
152 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_SYSTEM_LIVEVIEW_CREATE
153 * @tc.type: FUNC
154 * @tc.require: issue
155 */
156 HWTEST_F(FlowControlServiceTest, FlowControl_400, Function | SmallTest | Level1)
157 {
158 auto record = GetLiveviewNotificationRecord();
159 record->isThirdparty = false;
160 int32_t uid = 1000;
161 int32_t index = 1;
162 ErrCode result = ERR_OK;
163 uint32_t totalCreate = 0;
164 while (totalCreate + MAX_CREATE_NUM_PERSECOND_PERAPP < MAX_CREATE_NUM_PERSECOND) {
165 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
166 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
167 }
168 totalCreate += MAX_CREATE_NUM_PERSECOND_PERAPP;
169 index++;
170 }
171
172 int gap = MAX_CREATE_NUM_PERSECOND - totalCreate;
173 for (int i = 0; i < gap; i++) {
174 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
175 }
176 ASSERT_EQ(result, ERR_OK);
177
178 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
179 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
180 std::this_thread::sleep_for(std::chrono::seconds(1));
181 }
182
183 /**
184 * @tc.name: FlowControl_500
185 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_SYSTEM_LIVEVIEW_UPDATE
186 * @tc.type: FUNC
187 * @tc.require: issue
188 */
189 HWTEST_F(FlowControlServiceTest, FlowControl_500, Function | SmallTest | Level1)
190 {
191 auto record = GetLiveviewNotificationRecord();
192 record->isThirdparty = false;
193 int32_t uid = 1000;
194 int32_t index = 1;
195 ErrCode result = ERR_OK;
196 uint32_t totalCreate = 0;
197 while (totalCreate + MAX_UPDATE_NUM_PERSECOND_PERAPP < MAX_UPDATE_NUM_PERSECOND) {
198 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
199 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
200 }
201 totalCreate += MAX_UPDATE_NUM_PERSECOND_PERAPP;
202 index++;
203 }
204
205 int gap = MAX_UPDATE_NUM_PERSECOND - totalCreate;
206 for (int i = 0; i < gap; i++) {
207 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
208 }
209 ASSERT_EQ(result, ERR_OK);
210
211 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
212 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
213 std::this_thread::sleep_for(std::chrono::seconds(1));
214 }
215
216 /**
217 * @tc.name: FlowControl_600
218 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_THIRD_PART_NORMAL_CREATE
219 * @tc.type: FUNC
220 * @tc.require: issue
221 */
222 HWTEST_F(FlowControlServiceTest, FlowControl_600, Function | SmallTest | Level1)
223 {
224 auto record = GetCommonNotificationRecord();
225 record->isThirdparty = true;
226 int32_t uid = 1000;
227 int32_t index = 1;
228 ErrCode result = ERR_OK;
229 uint32_t totalCreate = 0;
230 while (totalCreate + MAX_CREATE_NUM_PERSECOND_PERAPP < MAX_CREATE_NUM_PERSECOND) {
231 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
232 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
233 }
234 totalCreate += MAX_CREATE_NUM_PERSECOND_PERAPP;
235 index++;
236 }
237
238 int gap = MAX_CREATE_NUM_PERSECOND - totalCreate;
239 for (int i = 0; i < gap; i++) {
240 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
241 }
242 ASSERT_EQ(result, ERR_OK);
243
244 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
245 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
246 std::this_thread::sleep_for(std::chrono::seconds(1));
247 }
248
249 /**
250 * @tc.name: FlowControl_700
251 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_THIRD_PART_NORMAL_UPDATE
252 * @tc.type: FUNC
253 * @tc.require: issue
254 */
255 HWTEST_F(FlowControlServiceTest, FlowControl_700, Function | SmallTest | Level1)
256 {
257 auto record = GetCommonNotificationRecord();
258 record->isThirdparty = true;
259 int32_t uid = 1000;
260 int32_t index = 1;
261 ErrCode result = ERR_OK;
262 uint32_t totalCreate = 0;
263 while (totalCreate + MAX_UPDATE_NUM_PERSECOND_PERAPP < MAX_UPDATE_NUM_PERSECOND) {
264 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
265 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
266 }
267 totalCreate += MAX_UPDATE_NUM_PERSECOND_PERAPP;
268 index++;
269 }
270
271 int gap = MAX_UPDATE_NUM_PERSECOND - totalCreate;
272 for (int i = 0; i < gap; i++) {
273 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
274 }
275 ASSERT_EQ(result, ERR_OK);
276
277 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
278 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
279 std::this_thread::sleep_for(std::chrono::seconds(1));
280 }
281
282 /**
283 * @tc.name: FlowControl_800
284 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_THIRD_PART_LIVEVIEW_CREATE
285 * @tc.type: FUNC
286 * @tc.require: issue
287 */
288 HWTEST_F(FlowControlServiceTest, FlowControl_800, Function | SmallTest | Level1)
289 {
290 auto record = GetLiveviewNotificationRecord();
291 record->isThirdparty = true;
292 int32_t uid = 1000;
293 int32_t index = 1;
294 ErrCode result = ERR_OK;
295 uint32_t totalCreate = 0;
296 while (totalCreate + MAX_CREATE_NUM_PERSECOND_PERAPP < MAX_CREATE_NUM_PERSECOND) {
297 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
298 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
299 }
300 totalCreate += MAX_CREATE_NUM_PERSECOND_PERAPP;
301 index++;
302 }
303
304 int gap = MAX_CREATE_NUM_PERSECOND - totalCreate;
305 for (int i = 0; i < gap; i++) {
306 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
307 }
308 ASSERT_EQ(result, ERR_OK);
309
310 result = FlowControlService::GetInstance().FlowControl(record, uid + index, false);
311 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
312 std::this_thread::sleep_for(std::chrono::seconds(1));
313 }
314
315 /**
316 * @tc.name: FlowControl_900
317 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::GLOBAL_THIRD_PART_LIVEVIEW_UPDATE
318 * @tc.type: FUNC
319 * @tc.require: issue
320 */
321 HWTEST_F(FlowControlServiceTest, FlowControl_900, Function | SmallTest | Level1)
322 {
323 auto record = GetLiveviewNotificationRecord();
324 record->isThirdparty = true;
325 int32_t uid = 1000;
326 int32_t index = 1;
327 ErrCode result = ERR_OK;
328 uint32_t totalCreate = 0;
329 while (totalCreate + MAX_UPDATE_NUM_PERSECOND_PERAPP < MAX_UPDATE_NUM_PERSECOND) {
330 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
331 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
332 }
333 totalCreate += MAX_UPDATE_NUM_PERSECOND_PERAPP;
334 index++;
335 }
336
337 int gap = MAX_UPDATE_NUM_PERSECOND - totalCreate;
338 for (int i = 0; i < gap; i++) {
339 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
340 }
341 ASSERT_EQ(result, ERR_OK);
342
343 result = FlowControlService::GetInstance().FlowControl(record, uid + index, true);
344 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
345 std::this_thread::sleep_for(std::chrono::seconds(1));
346 }
347
348 /**
349 * @tc.name: FlowControl_1000
350 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_SYSTEM_NORMAL_CREATE
351 * @tc.type: FUNC
352 * @tc.require: issue
353 */
354 HWTEST_F(FlowControlServiceTest, FlowControl_1000, Function | SmallTest | Level1)
355 {
356 auto record = GetCommonNotificationRecord();
357 record->isThirdparty = false;
358 int32_t uid = 1000;
359 ErrCode result = ERR_OK;
360
361 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
362 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
363 }
364 ASSERT_EQ(result, ERR_OK);
365
366 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
367 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
368 std::this_thread::sleep_for(std::chrono::seconds(1));
369 }
370
371 /**
372 * @tc.name: FlowControl_1100
373 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_SYSTEM_NORMAL_UPDATE
374 * @tc.type: FUNC
375 * @tc.require: issue
376 */
377 HWTEST_F(FlowControlServiceTest, FlowControl_1100, Function | SmallTest | Level1)
378 {
379 auto record = GetCommonNotificationRecord();
380 record->isThirdparty = false;
381 int32_t uid = 1000;
382 ErrCode result = ERR_OK;
383
384 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
385 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
386 }
387 ASSERT_EQ(result, ERR_OK);
388
389 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
390 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
391 std::this_thread::sleep_for(std::chrono::seconds(1));
392 }
393
394 /**
395 * @tc.name: FlowControl_1200
396 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_SYSTEM_LIVEVIEW_CREATE
397 * @tc.type: FUNC
398 * @tc.require: issue
399 */
400 HWTEST_F(FlowControlServiceTest, FlowControl_1200, Function | SmallTest | Level1)
401 {
402 auto record = GetLiveviewNotificationRecord();
403 record->isThirdparty = false;
404 int32_t uid = 1000;
405 ErrCode result = ERR_OK;
406
407 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
408 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
409 }
410 ASSERT_EQ(result, ERR_OK);
411
412 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
413 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
414 std::this_thread::sleep_for(std::chrono::seconds(1));
415 }
416
417 /**
418 * @tc.name: FlowControl_1300
419 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_SYSTEM_LIVEVIEW_UPDATE
420 * @tc.type: FUNC
421 * @tc.require: issue
422 */
423 HWTEST_F(FlowControlServiceTest, FlowControl_1300, Function | SmallTest | Level1)
424 {
425 auto record = GetLiveviewNotificationRecord();
426 record->isThirdparty = false;
427 int32_t uid = 1000;
428 ErrCode result = ERR_OK;
429
430 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
431 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
432 }
433 ASSERT_EQ(result, ERR_OK);
434
435 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
436 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
437 std::this_thread::sleep_for(std::chrono::seconds(1));
438 }
439
440 /**
441 * @tc.name: FlowControl_1400
442 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_THIRD_PART_NORMAL_CREATE
443 * @tc.type: FUNC
444 * @tc.require: issue
445 */
446 HWTEST_F(FlowControlServiceTest, FlowControl_1400, Function | SmallTest | Level1)
447 {
448 auto record = GetCommonNotificationRecord();
449 record->isThirdparty = true;
450 int32_t uid = 1000;
451 ErrCode result = ERR_OK;
452
453 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
454 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
455 }
456 ASSERT_EQ(result, ERR_OK);
457
458 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
459 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
460 std::this_thread::sleep_for(std::chrono::seconds(1));
461 }
462
463 /**
464 * @tc.name: FlowControl_1500
465 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_THIRD_PART_NORMAL_UPDATE
466 * @tc.type: FUNC
467 * @tc.require: issue
468 */
469 HWTEST_F(FlowControlServiceTest, FlowControl_1500, Function | SmallTest | Level1)
470 {
471 auto record = GetCommonNotificationRecord();
472 record->isThirdparty = true;
473 int32_t uid = 1000;
474 ErrCode result = ERR_OK;
475
476 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
477 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
478 }
479 ASSERT_EQ(result, ERR_OK);
480
481 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
482 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
483 std::this_thread::sleep_for(std::chrono::seconds(1));
484 }
485
486 /**
487 * @tc.name: FlowControl_1600
488 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_THIRD_PART_LIVEVIEW_CREATE
489 * @tc.type: FUNC
490 * @tc.require: issue
491 */
492 HWTEST_F(FlowControlServiceTest, FlowControl_1600, Function | SmallTest | Level1)
493 {
494 auto record = GetLiveviewNotificationRecord();
495 record->isThirdparty = true;
496 int32_t uid = 1000;
497 ErrCode result = ERR_OK;
498
499 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
500 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
501 }
502 ASSERT_EQ(result, ERR_OK);
503
504 result = FlowControlService::GetInstance().FlowControl(record, uid, false);
505 ASSERT_EQ(result, ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
506 std::this_thread::sleep_for(std::chrono::seconds(1));
507 }
508
509 /**
510 * @tc.name: FlowControl_1700
511 * @tc.desc: Test FlowControl when scene type is FlowControlSceneType::CALLER_THIRD_PART_LIVEVIEW_UPDATE
512 * @tc.type: FUNC
513 * @tc.require: issue
514 */
515 HWTEST_F(FlowControlServiceTest, FlowControl_1700, Function | SmallTest | Level1)
516 {
517 auto record = GetLiveviewNotificationRecord();
518 record->isThirdparty = true;
519 int32_t uid = 1000;
520 ErrCode result = ERR_OK;
521
522 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
523 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
524 }
525 ASSERT_EQ(result, ERR_OK);
526
527 result = FlowControlService::GetInstance().FlowControl(record, uid, true);
528 ASSERT_EQ(result, ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
529 std::this_thread::sleep_for(std::chrono::seconds(1));
530 }
531 } // namespace Notification
532 } // namespace OHOS