1 /*
2  * Copyright (c) 2021-2022 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 "publish_manager.h"
19 #include "system_time.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::EventFwk;
24 
25 namespace {
26 constexpr int32_t FLOOD_ATTACK_MAX = 20;
27 constexpr int32_t NOT_ATTACK_TIME = 10 + FLOOD_ATTACK_MAX;
28 constexpr int32_t TEST_TIMES = 100;
29 constexpr int32_t FLOOD_ATTACK_INTERVAL_MAX = 5;
30 constexpr int32_t SLEEP_TIME = 1000 * FLOOD_ATTACK_INTERVAL_MAX;
31 constexpr pid_t APPUID1 = 50;
32 constexpr pid_t APPUID2 = 51;
33 constexpr pid_t APPUID3 = 52;
34 
35 class CommonEventPublishManagerEventUnitTest : public testing::Test {
36 public:
CommonEventPublishManagerEventUnitTest()37     CommonEventPublishManagerEventUnitTest()
38     {}
~CommonEventPublishManagerEventUnitTest()39     ~CommonEventPublishManagerEventUnitTest()
40     {}
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp();
44     void TearDown();
45 };
46 
SetUpTestCase(void)47 void CommonEventPublishManagerEventUnitTest::SetUpTestCase(void)
48 {}
49 
TearDownTestCase(void)50 void CommonEventPublishManagerEventUnitTest::TearDownTestCase(void)
51 {}
52 
SetUp(void)53 void CommonEventPublishManagerEventUnitTest::SetUp(void)
54 {}
55 
TearDown(void)56 void CommonEventPublishManagerEventUnitTest::TearDown(void)
57 {}
58 
59 /*
60  * @tc.number: CommonEventPublishManagerEventUnitTest_0100
61  * @tc.name: test effectinve event floodAttack
62  */
63 HWTEST_F(CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0100,
64     Function | MediumTest | Level1)
65 {
66     GTEST_LOG_(INFO)
67         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0100, TestSize.Level1";
68 
69     bool result = false;
70 
71     for (int i = 1; i <= TEST_TIMES; ++i) {
72         result = DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(APPUID1);
73         if (result) {
74             EXPECT_GT(i, FLOOD_ATTACK_MAX);
75         }
76     }
77     GTEST_LOG_(INFO)
78         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0100, TestSize.Level1 end";
79 }
80 
81 /*
82  * @tc.number: CommonEventPublishManagerEventUnitTest_0200
83  * @tc.name: test not event floodAttack
84  */
85 HWTEST_F(CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0200,
86     Function | MediumTest | Level1)
87 {
88     GTEST_LOG_(INFO)
89         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0200, TestSize.Level1";
90 
91     for (int i = 1; i <= TEST_TIMES; ++i) {
92         usleep(SLEEP_TIME);
93         EXPECT_FALSE(DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(APPUID2));
94     }
95     GTEST_LOG_(INFO)
96         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0200, TestSize.Level1 end";
97 }
98 
99 /*
100  * @tc.number: CommonEventPublishManagerEventUnitTest_0300
101  * @tc.name: test first some times not event floodAttack then effectinve event floodAttack
102  */
103 HWTEST_F(CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0300,
104     Function | MediumTest | Level1)
105 {
106     GTEST_LOG_(INFO)
107         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0300, TestSize.Level1";
108 
109     bool result = false;
110 
111     for (int i = 1; i <= TEST_TIMES; ++i) {
112         if (i <= NOT_ATTACK_TIME) {
113             EXPECT_FALSE(DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(APPUID3));
114             usleep(SLEEP_TIME);
115         } else {
116             result = DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(APPUID3);
117             if (result) {
118                 EXPECT_GE(i, NOT_ATTACK_TIME + FLOOD_ATTACK_MAX);
119                 break;
120             }
121         }
122     }
123     GTEST_LOG_(INFO)
124         << "CommonEventPublishManagerEventUnitTest, CommonEventPublishManagerEventUnitTestt_0300, TestSize.Level1 end";
125 }
126 }  // namespace
127