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 #include <iostream>
16 
17 #include <gtest/gtest.h>
18 
19 #include "daily_controller.h"
20 #include "file_util.h"
21 #include "hiview_platform.h"
22 #include "parameter_ex.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 
27 class DailyControllerTest : public testing::Test {
28 public:
29     void SetUp();
30     void TearDown();
31 
32 private:
33     HiviewPlatform platform;
34 };
35 
36 namespace {
37 const std::string WORK_PATH = "/data/test/hiview/daily_control/";
38 const std::string CONFIG_PATH = "/data/test/hiview/daily_control/event_threshold.json";
39 const std::string TEST_DOMAIN = "DEFAULT_DOMAIN";
40 const std::string TEST_NAME = "DEFAULT_NAME";
41 
CreateEvent(const std::string & domain,const std::string & name,SysEventCreator::EventType type=SysEventCreator::FAULT)42 std::shared_ptr<SysEvent> CreateEvent(const std::string& domain, const std::string& name,
43     SysEventCreator::EventType type = SysEventCreator::FAULT)
44 {
45     SysEventCreator sysEventCreator(domain, name, type);
46     return std::make_shared<SysEvent>("", nullptr, sysEventCreator);
47 }
48 
EventThresholdTest(DailyController & controller,std::shared_ptr<SysEvent> event,uint32_t threshold)49 void EventThresholdTest(DailyController& controller, std::shared_ptr<SysEvent> event, uint32_t threshold)
50 {
51     for (uint32_t i = 0; i < threshold; i++) {
52         ASSERT_TRUE(controller.CheckThreshold(event));
53     }
54 
55     // failed to check after the threshold is exceeded
56     ASSERT_FALSE(controller.CheckThreshold(event));
57 }
58 
EventWithoutThresholdTest(DailyController & controller,std::shared_ptr<SysEvent> event,uint32_t threshold)59 void EventWithoutThresholdTest(DailyController& controller, std::shared_ptr<SysEvent> event, uint32_t threshold)
60 {
61     for (uint32_t i = 0; i < threshold; i++) {
62         ASSERT_TRUE(controller.CheckThreshold(event));
63     }
64 }
65 }
66 
SetUp()67 void DailyControllerTest::SetUp()
68 {
69     platform.GetPluginMap();
70     const std::string dbDir = WORK_PATH + "sys_event_threshold/";
71     if (FileUtil::IsDirectory(dbDir) && !FileUtil::ForceRemoveDirectory(dbDir)) {
72         std::cout << "Failed to remove diretory=" << dbDir << std::endl;
73     }
74 }
75 
TearDown()76 void DailyControllerTest::TearDown()
77 {}
78 
79 /**
80  * @tc.name: DailyControllerTest001
81  * @tc.desc: FAULT event test.
82  * @tc.type: FUNC
83  * @tc.require: issueI9MZ5Z
84  */
85 HWTEST_F(DailyControllerTest, DailyControllerTest001, TestSize.Level0)
86 {
87     DailyController controller(WORK_PATH, CONFIG_PATH);
88     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::FAULT);
89     constexpr uint32_t thresholdOnBeta = 100;
90     constexpr uint32_t thresholdOnCommercial = 20;
91     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
92     EventThresholdTest(controller, event, threshold);
93 }
94 
95 /**
96  * @tc.name: DailyControllerTest002
97  * @tc.desc: STATISTIC event test.
98  * @tc.type: FUNC
99  * @tc.require: issueI9MZ5Z
100  */
101 HWTEST_F(DailyControllerTest, DailyControllerTest002, TestSize.Level0)
102 {
103     DailyController controller(WORK_PATH, CONFIG_PATH);
104     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::STATISTIC);
105     constexpr uint32_t thresholdOnBeta = 100;
106     constexpr uint32_t thresholdOnCommercial = 20;
107     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
108     EventThresholdTest(controller, event, threshold);
109 }
110 
111 /**
112  * @tc.name: DailyControllerTest003
113  * @tc.desc: SECURITY event test.
114  * @tc.type: FUNC
115  * @tc.require: issueI9MZ5Z
116  */
117 HWTEST_F(DailyControllerTest, DailyControllerTest003, TestSize.Level0)
118 {
119     DailyController controller(WORK_PATH, CONFIG_PATH);
120     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::SECURITY);
121     constexpr uint32_t thresholdOnBeta = 100;
122     constexpr uint32_t thresholdOnCommercial = 20;
123     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
124     EventThresholdTest(controller, event, threshold);
125 }
126 
127 /**
128  * @tc.name: DailyControllerTest004
129  * @tc.desc: BEHAVIOR event test.
130  * @tc.type: FUNC
131  * @tc.require: issueI9MZ5Z
132  */
133 HWTEST_F(DailyControllerTest, DailyControllerTest004, TestSize.Level0)
134 {
135     DailyController controller(WORK_PATH, CONFIG_PATH);
136     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::BEHAVIOR);
137     constexpr uint32_t thresholdOnBeta = 2000;
138     constexpr uint32_t thresholdOnCommercial = 100;
139     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
140     EventThresholdTest(controller, event, threshold);
141 }
142 
143 /**
144  * @tc.name: DailyControllerTest005
145  * @tc.desc: Custom FAULT event test.
146  * @tc.type: FUNC
147  * @tc.require: issueI9MZ5Z
148  */
149 HWTEST_F(DailyControllerTest, DailyControllerTest005, TestSize.Level0)
150 {
151     DailyController controller(WORK_PATH, CONFIG_PATH);
152     auto event = CreateEvent("TEST_DOMAIN1", "FAULT_EVENT", SysEventCreator::FAULT);
153     constexpr uint32_t thresholdOnBeta = 200;
154     constexpr uint32_t thresholdOnCommercial = 40;
155     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
156     EventThresholdTest(controller, event, threshold);
157 }
158 
159 /**
160  * @tc.name: DailyControllerTest006
161  * @tc.desc: Custom STATISTIC event test.
162  * @tc.type: FUNC
163  * @tc.require: issueI9MZ5Z
164  */
165 HWTEST_F(DailyControllerTest, DailyControllerTest006, TestSize.Level0)
166 {
167     DailyController controller(WORK_PATH, CONFIG_PATH);
168     auto event = CreateEvent("TEST_DOMAIN1", "STATISTIC_EVENT", SysEventCreator::STATISTIC);
169     constexpr uint32_t thresholdOnBeta = 200;
170     constexpr uint32_t thresholdOnCommercial = 40;
171     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
172     EventThresholdTest(controller, event, threshold);
173 }
174 
175 /**
176  * @tc.name: DailyControllerTest007
177  * @tc.desc: Custom SECURITY event test.
178  * @tc.type: FUNC
179  * @tc.require: issueI9MZ5Z
180  */
181 HWTEST_F(DailyControllerTest, DailyControllerTest007, TestSize.Level0)
182 {
183     DailyController controller(WORK_PATH, CONFIG_PATH);
184     auto event = CreateEvent("TEST_DOMAIN2", "SECURITY_EVENT", SysEventCreator::SECURITY);
185     constexpr uint32_t thresholdOnBeta = 200;
186     constexpr uint32_t thresholdOnCommercial = 40;
187     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
188     EventThresholdTest(controller, event, threshold);
189 }
190 
191 /**
192  * @tc.name: DailyControllerTest008
193  * @tc.desc: Custom BEHAVIOR event test.
194  * @tc.type: FUNC
195  * @tc.require: issueI9MZ5Z
196  */
197 HWTEST_F(DailyControllerTest, DailyControllerTest008, TestSize.Level0)
198 {
199     DailyController controller(WORK_PATH, CONFIG_PATH);
200     auto event = CreateEvent("TEST_DOMAIN2", "BEHAVIOR_EVENT", SysEventCreator::BEHAVIOR);
201     constexpr uint32_t thresholdOnBeta = 3000;
202     constexpr uint32_t thresholdOnCommercial = 200;
203     uint32_t threshold = Parameter::IsBetaVersion() ? thresholdOnBeta : thresholdOnCommercial;
204     EventThresholdTest(controller, event, threshold);
205 }
206 
207 /**
208  * @tc.name: DailyControllerTest009
209  * @tc.desc: FAULT event test without threshold.
210  * @tc.type: FUNC
211  * @tc.require: issueI9NVZ1
212  */
213 HWTEST_F(DailyControllerTest, DailyControllerTest009, TestSize.Level0)
214 {
215     DailyController controller(WORK_PATH, "");
216     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::FAULT);
217     constexpr uint32_t threshold = 1000;
218     EventWithoutThresholdTest(controller, event, threshold);
219 }
220 
221 /**
222  * @tc.name: DailyControllerTest010
223  * @tc.desc: STATISTIC event test without threshold.
224  * @tc.type: FUNC
225  * @tc.require: issueI9NVZ1
226  */
227 HWTEST_F(DailyControllerTest, DailyControllerTest010, TestSize.Level0)
228 {
229     DailyController controller(WORK_PATH, "");
230     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::STATISTIC);
231     constexpr uint32_t threshold = 1000;
232     EventWithoutThresholdTest(controller, event, threshold);
233 }
234 
235 /**
236  * @tc.name: DailyControllerTest011
237  * @tc.desc: SECURITY event test without threshold.
238  * @tc.type: FUNC
239  * @tc.require: issueI9NVZ1
240  */
241 HWTEST_F(DailyControllerTest, DailyControllerTest011, TestSize.Level0)
242 {
243     DailyController controller(WORK_PATH, "");
244     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::SECURITY);
245     constexpr uint32_t threshold = 1000;
246     EventWithoutThresholdTest(controller, event, threshold);
247 }
248 
249 /**
250  * @tc.name: DailyControllerTest012
251  * @tc.desc: BEHAVIOR event test without threshold.
252  * @tc.type: FUNC
253  * @tc.require: issueI9NVZ1
254  */
255 HWTEST_F(DailyControllerTest, DailyControllerTest012, TestSize.Level0)
256 {
257     DailyController controller(WORK_PATH, "");
258     auto event = CreateEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::BEHAVIOR);
259     constexpr uint32_t threshold = 10000;
260     EventWithoutThresholdTest(controller, event, threshold);
261 }
262