1 /*
2 * Copyright (c) 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 "hiappevent_cache_test.h"
17
18 #include "app_event_cache_common.h"
19 #include "app_event_store.h"
20 #include "file_util.h"
21 #include "hiappevent_base.h"
22 #include "hiappevent_clean.h"
23 #include "hiappevent_config.h"
24 #include "hiappevent_write.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::HiviewDFX::AppEventCacheCommon;
29 namespace {
30 const std::string TEST_DIR = "/data/test/hiappevent/";
31 const std::string TEST_DB_PATH = "/data/test/hiappevent/databases/appevent.db";
32 const std::string TEST_OBSERVER_NAME = "test_observer";
33 const std::string TEST_EVENT_DOMAIN = "test_domain";
34 const std::string TEST_EVENT_NAME = "test_name";
35 constexpr int TEST_EVENT_TYPE = 1;
36 const std::string TEST_PACKAGE = "{\"domain_\":\"hiappevent\", \"name_\":\"testEvent\"}";
37 const std::string TEST_RUNNING_ID = "running_test";
38
CreateAppEventPack()39 std::shared_ptr<AppEventPack> CreateAppEventPack()
40 {
41 return std::make_shared<AppEventPack>(TEST_EVENT_DOMAIN, TEST_EVENT_NAME, TEST_EVENT_TYPE);
42 }
43 }
44
SetUp()45 void HiAppEventCacheTest::SetUp()
46 {
47 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
48 }
49
50 /**
51 * @tc.name: HiAppEventDBTest001
52 * @tc.desc: check the query result of DB operation.
53 * @tc.type: FUNC
54 * @tc.require: issueI5K0X6
55 */
56 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest001, TestSize.Level0)
57 {
58 /**
59 * @tc.steps: step1. open the db.
60 * @tc.steps: step2. insert record to the tables.
61 * @tc.steps: step3. query records from tables.
62 */
63 int result = AppEventStore::GetInstance().InitDbStore();;
64 ASSERT_EQ(result, 0);
65
66 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
67 ASSERT_GT(eventSeq, 0);
68 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
69 ASSERT_GT(observerSeq, 0);
70 int64_t mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
71 ASSERT_GT(mappingSeq, 0);
72
73 std::vector<std::shared_ptr<AppEventPack>> events;
74 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
75 ASSERT_EQ(result, 0);
76 ASSERT_GT(events.size(), 0);
77 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
78 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
79 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
80
81 std::vector<int64_t> observerSeqs;
82 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
83 ASSERT_EQ(result, 0);
84 ASSERT_GT(observerSeqs.size(), 0);
85 ASSERT_EQ(observerSeqs[0], observerSeq);
86
87 result = AppEventStore::GetInstance().DestroyDbStore();;
88 ASSERT_EQ(result, 0);
89 }
90
91 /**
92 * @tc.name: HiAppEventDBTest002
93 * @tc.desc: check the take result of DB operation.
94 * @tc.type: FUNC
95 * @tc.require: issueI5K0X6
96 */
97 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest002, TestSize.Level0)
98 {
99 /**
100 * @tc.steps: step1. open the db.
101 * @tc.steps: step2. insert record to the tables.
102 * @tc.steps: step3. take records from tables.
103 */
104 int result = AppEventStore::GetInstance().InitDbStore();;
105 ASSERT_EQ(result, 0);
106
107 auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
108 ASSERT_GT(eventSeq, 0);
109 auto observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
110 ASSERT_GT(observerSeq, 0);
111 auto mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
112 ASSERT_GT(mappingSeq, 0);
113
114 std::vector<std::shared_ptr<AppEventPack>> events;
115 result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
116 ASSERT_EQ(result, 0);
117 ASSERT_GT(events.size(), 0);
118 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
119 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
120 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
121
122 events.clear();
123 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
124 ASSERT_EQ(result, 0);
125 ASSERT_EQ(events.size(), 0);
126
127 result = AppEventStore::GetInstance().DestroyDbStore();;
128 ASSERT_EQ(result, 0);
129 }
130
131 /**
132 * @tc.name: HiAppEventDBTest003
133 * @tc.desc: check the delete result of DB operation.
134 * @tc.type: FUNC
135 * @tc.require: issueI5NTOD
136 */
137 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest003, TestSize.Level0)
138 {
139 /**
140 * @tc.steps: step1. open the db.
141 * @tc.steps: step2. insert record to the tables.
142 * @tc.steps: step3. delete records from tables.
143 */
144 int result = AppEventStore::GetInstance().InitDbStore();;
145 ASSERT_EQ(result, 0);
146
147 auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
148 ASSERT_GT(eventSeq, 0);
149 auto observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
150 ASSERT_GT(observerSeq, 0);
151 auto mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
152 ASSERT_GT(mappingSeq, 0);
153
154 result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
155 ASSERT_EQ(result, 1); // 1 recored
156 std::vector<int64_t> observerSeqs;
157 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
158 ASSERT_EQ(result, 0);
159 ASSERT_EQ(observerSeqs.size(), 0);
160
161 result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
162 ASSERT_EQ(result, 0); // 0 recored
163 std::vector<std::shared_ptr<AppEventPack>> events;
164 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
165 ASSERT_EQ(result, 0);
166 ASSERT_EQ(events.size(), 0);
167 }
168
169 /**
170 * @tc.name: HiAppEventDBTest004
171 * @tc.desc: revisit the DB after destroying it.
172 * @tc.type: FUNC
173 * @tc.require: issueI5NTOS
174 */
175 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest004, TestSize.Level1)
176 {
177 /**
178 * @tc.steps: step1. open the db.
179 * @tc.steps: step2. create block table.
180 * @tc.steps: step3. add record to the block table.
181 * @tc.steps: step4. config the max storage size.
182 * @tc.steps: step5. trigger cleanup.
183 * @tc.steps: step6. close the db.
184 */
185 int result = AppEventStore::GetInstance().DestroyDbStore();;
186 ASSERT_EQ(result, 0);
187
188 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
189 ASSERT_GT(eventSeq, 0);
190 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
191 ASSERT_GT(observerSeq, 0);
192 int64_t mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
193 ASSERT_GT(mappingSeq, 0);
194
195 std::vector<std::shared_ptr<AppEventPack>> events;
196 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
197 ASSERT_EQ(result, 0);
198 std::vector<int64_t> observerSeqs;
199 result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
200 ASSERT_EQ(result, 0);
201 result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
202 ASSERT_EQ(result, 0);
203
204 result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
205 ASSERT_EQ(result, 1); // 1 recored
206 result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
207 ASSERT_EQ(result, 0); // 0 recored
208 }
209
210 /**
211 * @tc.name: HiAppEventDBTest005
212 * @tc.desc: check the result of clear data.
213 * @tc.type: FUNC
214 * @tc.require: issueI5NTOS
215 */
216 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest005, TestSize.Level1)
217 {
218 /**
219 * @tc.steps: step1. create log file.
220 * @tc.steps: step2. create db file.
221 * @tc.steps: step3. clear the data.
222 */
223 WriteEvent(std::make_shared<AppEventPack>("name", 1));
224 std::vector<std::string> files;
225 FileUtil::GetDirFiles(TEST_DIR, files);
226 ASSERT_FALSE(files.empty());
227 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
228 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
229
230 HiAppEventClean::ClearData(TEST_DIR);
231 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
232 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
233 for (const auto& file : files) {
234 ASSERT_FALSE(FileUtil::IsFileExists(file));
235 }
236 }
237
238 /**
239 * @tc.name: HiAppEventDBTest006
240 * @tc.desc: check the query result of DB operation.
241 * @tc.type: FUNC
242 * @tc.require: issueI5K0X6
243 */
244 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest006, TestSize.Level0)
245 {
246 /**
247 * @tc.steps: step1. open the db.
248 * @tc.steps: step2. insert record to the tables, insert custom param.
249 * @tc.steps: step3. query records from tables.
250 * @tc.steps: step3. delete custom param, query records from tables.
251 */
252 int result = AppEventStore::GetInstance().InitDbStore();;
253 ASSERT_EQ(result, 0);
254
255 auto event = CreateAppEventPack();
256 event->SetRunningId(TEST_RUNNING_ID);
257 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(event);
258 ASSERT_GT(eventSeq, 0);
259 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
260 ASSERT_GT(observerSeq, 0);
261 int64_t mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
262 ASSERT_GT(mappingSeq, 0);
263 auto eventParams = CreateAppEventPack();
264 eventParams->SetRunningId(TEST_RUNNING_ID);
265 eventParams->AddParam("custom_data", "value_str");
266 int64_t ret = AppEventStore::GetInstance().InsertCustomEventParams(eventParams);
267 ASSERT_EQ(ret, 0);
268
269 std::vector<std::shared_ptr<AppEventPack>> events;
270 result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
271 ASSERT_EQ(result, 0);
272 ASSERT_GT(events.size(), 0);
273 ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
274 ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
275 ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
276 ASSERT_EQ(events[0]->GetRunningId(), TEST_RUNNING_ID);
277 ASSERT_EQ(events[0]->GetParamStr(), "{\"custom_data\":\"value_str\"}\n");
278
279 // delete custom params
280 AppEventStore::GetInstance().DeleteCustomEventParams();
281 std::vector<std::shared_ptr<AppEventPack>> events1;
282 result = AppEventStore::GetInstance().QueryEvents(events1, observerSeq, 1);
283 ASSERT_EQ(result, 0);
284 ASSERT_EQ(events1.size(), 1);
285 ASSERT_EQ(events1[0]->GetDomain(), TEST_EVENT_DOMAIN);
286 ASSERT_EQ(events1[0]->GetName(), TEST_EVENT_NAME);
287 ASSERT_EQ(events1[0]->GetType(), TEST_EVENT_TYPE);
288 ASSERT_EQ(events1[0]->GetRunningId(), TEST_RUNNING_ID);
289 ASSERT_EQ(events1[0]->GetParamStr(), "{}\n");
290
291 result = AppEventStore::GetInstance().DestroyDbStore();;
292 ASSERT_EQ(result, 0);
293 }
294