1 /*
2 * Copyright (c) 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 "advanced_notification_flow_control_service.h"
17
18 #include "gtest/gtest.h"
19
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 namespace {
28 constexpr int32_t NON_SYSTEM_APP_UID = 1000;
29 }
30 class FlowControlServiceTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase()38 void FlowControlServiceTest::SetUpTestCase() {}
39
TearDownTestCase()40 void FlowControlServiceTest::TearDownTestCase() {}
41
SetUp()42 void FlowControlServiceTest::SetUp() {}
43
TearDown()44 void FlowControlServiceTest::TearDown() {}
45
46 /**
47 * @tc.number : FlowControl_00001
48 * @tc.name : Test FlowControl
49 * @tc.desc : Test FlowControl
50 */
51 HWTEST_F(FlowControlServiceTest, FlowControl_00001, Function | SmallTest | Level1)
52 {
53 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
54 sptr<Notification> notification = new (std::nothrow) Notification(request);
55 auto record = std::make_shared<NotificationRecord>();
56 record->request = request;
57 record->notification = notification;
58 record->isThirdparty = false;
59 record->isNeedFlowCtrl = true;
60 ErrCode result = ERR_OK;
61 int32_t callingUid = DEFAULT_UID;
62
63 // create flow control
64 // single app flow control test
65 for (int i = 0; i < MAX_CREATE_NUM_PERSECOND_PERAPP; i++) {
66 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, false);
67 }
68 ASSERT_EQ(result, (int)ERR_OK);
69 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, false);
70 ASSERT_EQ(result, (int)ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
71
72 // global flow control test
73 int gap = MAX_CREATE_NUM_PERSECOND - MAX_CREATE_NUM_PERSECOND_PERAPP;
74 callingUid = NON_SYSTEM_APP_UID;
75 for (int i = 0; i < gap; i++) {
76 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, false);
77 }
78 ASSERT_EQ(result, (int)ERR_OK);
79 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, false);
80 ASSERT_EQ(result, (int)ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
81
82 // update flow control
83 // single app flow control test
84 callingUid = DEFAULT_UID;
85 for (int i = 0; i < MAX_UPDATE_NUM_PERSECOND_PERAPP; i++) {
86 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, true);
87 }
88 ASSERT_EQ(result, (int)ERR_OK);
89 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, true);
90 ASSERT_EQ(result, (int)ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
91
92 // global flow control test
93 gap = MAX_UPDATE_NUM_PERSECOND - MAX_UPDATE_NUM_PERSECOND_PERAPP;
94 callingUid = NON_SYSTEM_APP_UID;
95 for (int i = 0; i < gap; i++) {
96 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, true);
97 }
98 ASSERT_EQ(result, (int)ERR_OK);
99 result = FlowControlService::GetInstance()->FlowControl(record, callingUid, true);
100 ASSERT_EQ(result, (int)ERR_ANS_OVER_MAX_UPDATE_PERSECOND);
101 }
102 } // namespace Notification
103 } // namespace OHOS