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