1 /*
2 * Copyright (c) 2022-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
16 #include "hiappevent_cache_test.h"
17
18 #include "app_event_cache_common.h"
19 #include "app_event_db_cleaner.h"
20 #include "app_event_log_cleaner.h"
21 #include "app_event_stat.h"
22 #include "app_event_store.h"
23 #include "file_util.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_clean.h"
26 #include "hiappevent_config.h"
27 #include "hiappevent_write.h"
28 #include "time_util.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::HiviewDFX;
32 using namespace OHOS::HiviewDFX::AppEventCacheCommon;
33 namespace {
34 const std::string TEST_DIR = "/data/test/hiappevent/";
35 const std::string TEST_DB_PATH = "/data/test/hiappevent/databases/appevent.db";
36 const std::string TEST_OBSERVER_NAME = "test_observer";
37 const std::string TEST_EVENT_DOMAIN = "test_domain";
38 const std::string TEST_EVENT_NAME = "test_name";
39 constexpr int TEST_EVENT_TYPE = 1;
40 const std::string TEST_PACKAGE = "{\"domain_\":\"hiappevent\", \"name_\":\"testEvent\"}";
41 const std::string TEST_RUNNING_ID = "running_test";
42
CreateAppEventPack()43 std::shared_ptr<AppEventPack> CreateAppEventPack()
44 {
45 return std::make_shared<AppEventPack>(TEST_EVENT_DOMAIN, TEST_EVENT_NAME, TEST_EVENT_TYPE);
46 }
47 }
48
SetUp()49 void HiAppEventCacheTest::SetUp()
50 {
51 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
52 }
53
54 /**
55 * @tc.name: HiAppEventDBTest001
56 * @tc.desc: check the query result of DB operation.
57 * @tc.type: FUNC
58 * @tc.require: issueI5K0X6
59 */
60 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest001, TestSize.Level0)
61 {
62 /**
63 * @tc.steps: step1. open the db.
64 * @tc.steps: step2. insert record to the tables.
65 * @tc.steps: step3. query records from tables.
66 */
67 int result = AppEventStore::GetInstance().InitDbStore();;
68 ASSERT_EQ(result, 0);
69
70 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
71 ASSERT_GT(eventSeq, 0);
72 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(AppEventCacheCommon::Observer(TEST_OBSERVER_NAME,
73 0, ""));
74 ASSERT_GT(observerSeq, 0);
75 result = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
76 ASSERT_EQ(result, 0);
77
78 std::vector<std::shared_ptr<AppEventPack>> events;
79 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
80 ASSERT_EQ(result, 0);
81 ASSERT_GT(events.size(), 0);
82 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
83 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
84 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
85
86 std::vector<int64_t> observerSeqs;
87 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
88 ASSERT_EQ(result, 0);
89 ASSERT_GT(observerSeqs.size(), 0);
90 ASSERT_EQ(observerSeqs[0], observerSeq);
91
92 result = AppEventStore::GetInstance().DestroyDbStore();;
93 ASSERT_EQ(result, 0);
94 }
95
96 /**
97 * @tc.name: HiAppEventDBTest002
98 * @tc.desc: check the take result of DB operation.
99 * @tc.type: FUNC
100 * @tc.require: issueI5K0X6
101 */
102 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest002, TestSize.Level0)
103 {
104 /**
105 * @tc.steps: step1. open the db.
106 * @tc.steps: step2. insert record to the tables.
107 * @tc.steps: step3. take records from tables.
108 */
109 int result = AppEventStore::GetInstance().InitDbStore();;
110 ASSERT_EQ(result, 0);
111
112 auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
113 ASSERT_GT(eventSeq, 0);
114 auto observerSeq = AppEventStore::GetInstance().InsertObserver(AppEventCacheCommon::Observer(TEST_OBSERVER_NAME,
115 0, ""));
116 ASSERT_GT(observerSeq, 0);
117 result = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
118 ASSERT_EQ(result, 0);
119
120 std::vector<std::shared_ptr<AppEventPack>> events;
121 result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
122 ASSERT_EQ(result, 0);
123 ASSERT_GT(events.size(), 0);
124 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
125 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
126 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
127
128 events.clear();
129 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
130 ASSERT_EQ(result, 0);
131 ASSERT_EQ(events.size(), 0);
132
133 result = AppEventStore::GetInstance().DestroyDbStore();;
134 ASSERT_EQ(result, 0);
135 }
136
137 /**
138 * @tc.name: HiAppEventDBTest003
139 * @tc.desc: check the delete result of DB operation.
140 * @tc.type: FUNC
141 * @tc.require: issueI5NTOD
142 */
143 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest003, TestSize.Level0)
144 {
145 /**
146 * @tc.steps: step1. open the db.
147 * @tc.steps: step2. insert record to the tables.
148 * @tc.steps: step3. delete records from tables.
149 */
150 int result = AppEventStore::GetInstance().InitDbStore();;
151 ASSERT_EQ(result, 0);
152
153 auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
154 ASSERT_GT(eventSeq, 0);
155 auto observerSeq = AppEventStore::GetInstance().InsertObserver(AppEventCacheCommon::Observer(TEST_OBSERVER_NAME,
156 0, ""));
157 ASSERT_GT(observerSeq, 0);
158 result = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
159 ASSERT_EQ(result, 0);
160
161 result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
162 ASSERT_EQ(result, 0);
163 std::vector<int64_t> observerSeqs;
164 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
165 ASSERT_EQ(result, 0);
166 ASSERT_EQ(observerSeqs.size(), 0);
167
168 result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
169 ASSERT_EQ(result, 0);
170 std::vector<std::shared_ptr<AppEventPack>> events;
171 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
172 ASSERT_EQ(result, 0);
173 ASSERT_EQ(events.size(), 0);
174 }
175
176 /**
177 * @tc.name: HiAppEventDBTest004
178 * @tc.desc: revisit the DB after destroying it.
179 * @tc.type: FUNC
180 * @tc.require: issueI5NTOS
181 */
182 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest004, TestSize.Level1)
183 {
184 /**
185 * @tc.steps: step1. open the db.
186 * @tc.steps: step2. create block table.
187 * @tc.steps: step3. add record to the block table.
188 * @tc.steps: step4. config the max storage size.
189 * @tc.steps: step5. trigger cleanup.
190 * @tc.steps: step6. close the db.
191 */
192 int result = AppEventStore::GetInstance().DestroyDbStore();;
193 ASSERT_EQ(result, 0);
194
195 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
196 ASSERT_GT(eventSeq, 0);
197 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(AppEventCacheCommon::Observer(TEST_OBSERVER_NAME,
198 0, ""));
199 ASSERT_GT(observerSeq, 0);
200 result = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
201 ASSERT_EQ(result, 0);
202
203 std::vector<std::shared_ptr<AppEventPack>> events;
204 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
205 ASSERT_EQ(result, 0);
206 std::vector<int64_t> observerSeqs;
207 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
208 ASSERT_EQ(result, 0);
209 result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
210 ASSERT_EQ(result, 0);
211
212 result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
213 ASSERT_EQ(result, 0);
214 result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
215 ASSERT_EQ(result, 0);
216 }
217
218 /**
219 * @tc.name: HiAppEventDBTest005
220 * @tc.desc: check the result of clear data.
221 * @tc.type: FUNC
222 * @tc.require: issueI5NTOS
223 */
224 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest005, TestSize.Level1)
225 {
226 /**
227 * @tc.steps: step1. create log file.
228 * @tc.steps: step2. create db file.
229 * @tc.steps: step3. clear the data.
230 */
231 WriteEvent(std::make_shared<AppEventPack>("name", 1));
232 std::vector<std::string> files;
233 FileUtil::GetDirFiles(TEST_DIR, files);
234 ASSERT_FALSE(files.empty());
235 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
236 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
237
238 HiAppEventClean::ClearData(TEST_DIR);
239 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
240 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
241 for (const auto& file : files) {
242 ASSERT_FALSE(FileUtil::IsFileExists(file));
243 }
244 }
245
246 /**
247 * @tc.name: HiAppEventDBTest006
248 * @tc.desc: check the query result of DB operation.
249 * @tc.type: FUNC
250 * @tc.require: issueI5K0X6
251 */
252 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest006, TestSize.Level0)
253 {
254 /**
255 * @tc.steps: step1. open the db.
256 * @tc.steps: step2. insert record to the tables, insert custom param.
257 * @tc.steps: step3. query records from tables.
258 * @tc.steps: step3. delete custom param, query records from tables.
259 */
260 int result = AppEventStore::GetInstance().InitDbStore();;
261 ASSERT_EQ(result, 0);
262
263 auto event = CreateAppEventPack();
264 event->SetRunningId(TEST_RUNNING_ID);
265 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(event);
266 ASSERT_GT(eventSeq, 0);
267 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(AppEventCacheCommon::Observer(TEST_OBSERVER_NAME,
268 0, ""));
269 ASSERT_GT(observerSeq, 0);
270 result = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
271 ASSERT_EQ(result, 0);
272 auto eventParams = CreateAppEventPack();
273 eventParams->SetRunningId(TEST_RUNNING_ID);
274 eventParams->AddParam("custom_data", "value_str");
275 result = AppEventStore::GetInstance().InsertCustomEventParams(eventParams);
276 ASSERT_EQ(result, 0);
277
278 std::vector<std::shared_ptr<AppEventPack>> events;
279 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
280 ASSERT_EQ(result, 0);
281 ASSERT_GT(events.size(), 0);
282 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
283 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
284 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
285 ASSERT_EQ(events[0]->GetRunningId(), TEST_RUNNING_ID);
286 ASSERT_EQ(events[0]->GetParamStr(), "{\"custom_data\":\"value_str\"}\n");
287
288 // delete custom params
289 AppEventStore::GetInstance().DeleteCustomEventParams();
290 std::vector<std::shared_ptr<AppEventPack>> events1;
291 result = AppEventStore::GetInstance().QueryEvents(events1, observerSeq, 1);
292 ASSERT_EQ(result, 0);
293 ASSERT_EQ(events1.size(), 1);
294 ASSERT_EQ(events1[0]->GetDomain(), TEST_EVENT_DOMAIN);
295 ASSERT_EQ(events1[0]->GetName(), TEST_EVENT_NAME);
296 ASSERT_EQ(events1[0]->GetType(), TEST_EVENT_TYPE);
297 ASSERT_EQ(events1[0]->GetRunningId(), TEST_RUNNING_ID);
298 ASSERT_EQ(events1[0]->GetParamStr(), "{}\n");
299
300 result = AppEventStore::GetInstance().DestroyDbStore();;
301 ASSERT_EQ(result, 0);
302 }
303
304 /**
305 * @tc.name: HiAppEventCleanTest001
306 * @tc.desc: test the DB cleaner operation.
307 * @tc.type: FUNC
308 * @tc.require: issueI5NTOS
309 */
310 HWTEST_F(HiAppEventCacheTest, HiAppEventCleanTest001, TestSize.Level1)
311 {
312 /**
313 * @tc.steps: step1. init the db and insert event to the table.
314 * @tc.steps: step2. clear event db.
315 */
316 int result = AppEventStore::GetInstance().InitDbStore();
317 EXPECT_EQ(result, 0);
318 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
319 EXPECT_GT(eventSeq, 0);
320
321 AppEventDbCleaner dbCleaner(TEST_DIR);
322 uint64_t curSize = dbCleaner.GetFilesSize();
323 EXPECT_GT(curSize, 0);
324 uint64_t clearResult = dbCleaner.ClearSpace(curSize, curSize);
325 EXPECT_EQ(clearResult, curSize);
326 uint64_t clearHistoryResult = dbCleaner.ClearSpace(curSize, 0);
327 EXPECT_EQ(clearHistoryResult, 0);
328
329 result = AppEventStore::GetInstance().DestroyDbStore();
330 EXPECT_EQ(result, 0);
331 }
332
333 /**
334 * @tc.name: HiAppEventCleanTest002
335 * @tc.desc: test the log cleaner operation.
336 * @tc.type: FUNC
337 * @tc.require: issueI5NTOS
338 */
339 HWTEST_F(HiAppEventCacheTest, HiAppEventCleanTest002, TestSize.Level1)
340 {
341 /**
342 * @tc.steps: step1. create a example log.
343 * @tc.steps: step2. clear log space.
344 */
345 std::string filePath = TEST_DIR + "test.txt";
346 bool makeFileResult = FileUtil::CreateFile(filePath, S_IRUSR | S_IWUSR);
347 EXPECT_TRUE(makeFileResult);
348 bool writeFileResult = FileUtil::SaveStringToFile(filePath, filePath, false);
349 EXPECT_TRUE(writeFileResult);
350
351 AppEventLogCleaner logCleaner(TEST_DIR);
352 uint64_t curSize = logCleaner.GetFilesSize();
353 EXPECT_GT(curSize, 0);
354 uint64_t clearResult = logCleaner.ClearSpace(curSize, curSize);
355 EXPECT_EQ(clearResult, curSize);
356 uint64_t clearHistoryResult = logCleaner.ClearSpace(curSize, 0);
357 EXPECT_EQ(clearHistoryResult, 0);
358
359 bool removeResult = FileUtil::RemoveFile(filePath);
360 EXPECT_TRUE(removeResult);
361 }
362
363 /**
364 * @tc.name: HiAppEventStat001
365 * @tc.desc: test the WriteApiEndEventAsync func of app event stat.
366 * @tc.type: FUNC
367 * @tc.require: issueI5NTOS
368 */
369 HWTEST_F(HiAppEventCacheTest, HiAppEventStat001, TestSize.Level1)
370 {
371 std::string apiName = "testApi";
372 uint64_t beginTime = TimeUtil::GetMilliSecondsTimestamp(CLOCK_REALTIME);
373 AppEventStat::WriteApiEndEventAsync(apiName, beginTime, AppEventStat::SUCCESS, AppEventStat::SUCCESS);
374 AppEventStat::WriteApiEndEventAsync(apiName, -beginTime, AppEventStat::SUCCESS, AppEventStat::SUCCESS);
375 EXPECT_GT(beginTime, 0);
376 }