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