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_cache.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_DIR = "/data/test/hiappevent/databases/";
32 const std::string TEST_BLOCK = "testBlock";
33 const std::string TEST_PACKAGE = "{\"domain_\":\"hiappevent\", \"name_\":\"testEvent\"}";
34 }
35
TearDown()36 void HiAppEventCacheTest::TearDown()
37 {
38 AppEventCache::GetInstance()->Close();
39 }
40
41 /**
42 * @tc.name: HiAppEventDBTest001
43 * @tc.desc: check the successful result of DB operation.
44 * @tc.type: FUNC
45 * @tc.require: issueI5K0X6
46 */
47 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest001, TestSize.Level0)
48 {
49 /**
50 * @tc.steps: step1. open the db.
51 * @tc.steps: step2. create block table.
52 * @tc.steps: step3. add record to the block table.
53 */
54 int result = AppEventCache::GetInstance()->Open(TEST_DIR);
55 ASSERT_EQ(result, DB_SUCC);
56
57 result = AppEventCache::GetInstance()->CreateBlock(TEST_BLOCK);
58 ASSERT_EQ(result, DB_SUCC);
59
60 auto block = AppEventCache::GetInstance()->GetBlock(TEST_BLOCK);
61 ASSERT_NE(block, nullptr);
62
63 std::map<std::string, std::pair<int, int64_t>> blocksStat;
64 result = AppEventCache::GetInstance()->GetBlocksStat(blocksStat);
65 ASSERT_EQ(result, DB_SUCC);
66 ASSERT_EQ(blocksStat.size(), 1);
67
68 result = AppEventCache::GetInstance()->CreateBlock("testBlock2");
69 ASSERT_EQ(result, DB_SUCC);
70 std::map<std::string, std::pair<int, int64_t>> blocksStat2;
71 result = AppEventCache::GetInstance()->GetBlocksStat(blocksStat2);
72 ASSERT_EQ(result, DB_SUCC);
73 ASSERT_EQ(blocksStat2.size(), 2);
74
75 result = AppEventCache::GetInstance()->DestroyBlock(TEST_BLOCK);
76 ASSERT_EQ(result, DB_SUCC);
77 result = AppEventCache::GetInstance()->DestroyBlock("testBlock2");
78 ASSERT_EQ(result, DB_SUCC);
79
80 result = AppEventCache::GetInstance()->Close();
81 ASSERT_EQ(result, DB_SUCC);
82 }
83
84 /**
85 * @tc.name: HiAppEventDBTest002
86 * @tc.desc: check the failed result of DB operation.
87 * @tc.type: FUNC
88 * @tc.require: issueI5K0X6
89 */
90 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest002, TestSize.Level0)
91 {
92 /**
93 * @tc.steps: step1. open the db.
94 * @tc.steps: step2. create block table.
95 * @tc.steps: step3. destroy the block table.
96 * @tc.steps: step4. close the db.
97 */
98 int result = AppEventCache::GetInstance()->Open("");
99 ASSERT_EQ(result, DB_FAILED);
100
101 result = AppEventCache::GetInstance()->CreateBlock(TEST_BLOCK);
102 ASSERT_EQ(result, DB_FAILED);
103
104 auto block = AppEventCache::GetInstance()->GetBlock(TEST_BLOCK);
105 ASSERT_EQ(block, nullptr);
106
107 std::map<std::string, std::pair<int, int64_t>> blocksStat;
108 result = AppEventCache::GetInstance()->GetBlocksStat(blocksStat);
109 ASSERT_EQ(result, DB_FAILED);
110 ASSERT_TRUE(blocksStat.empty());
111
112 result = AppEventCache::GetInstance()->DestroyBlock(TEST_BLOCK);
113 ASSERT_EQ(result, DB_FAILED);
114
115 result = AppEventCache::GetInstance()->Close();
116 ASSERT_EQ(result, DB_SUCC);
117 }
118
119 /**
120 * @tc.name: HiAppEventBlockTest001
121 * @tc.desc: check the successful result of block operation.
122 * @tc.type: FUNC
123 * @tc.require: issueI5NTOD
124 */
125 HWTEST_F(HiAppEventCacheTest, HiAppEventBlockTest001, TestSize.Level0)
126 {
127 /**
128 * @tc.steps: step1. open the db.
129 * @tc.steps: step2. create block table.
130 * @tc.steps: step3. add record to the block table.
131 * @tc.steps: step4. delete record from the block table.
132 * @tc.steps: step5. close the db.
133 */
134 int result = AppEventCache::GetInstance()->Open(TEST_DIR);
135 ASSERT_EQ(result, DB_SUCC);
136 result = AppEventCache::GetInstance()->CreateBlock(TEST_BLOCK);
137 ASSERT_EQ(result, DB_SUCC);
138 auto block = AppEventCache::GetInstance()->GetBlock(TEST_BLOCK);
139 ASSERT_NE(block, nullptr);
140
141 // put 5 records to the block
142 const int ADD_NUM = 5;
143 for (int i = 0; i < ADD_NUM; ++i) {
144 result = block->Add(TEST_PACKAGE);
145 ASSERT_EQ(result, DB_SUCC);
146 }
147
148 std::map<std::string, std::pair<int, int64_t>> blocksStat;
149 result = AppEventCache::GetInstance()->GetBlocksStat(blocksStat);
150 ASSERT_EQ(result, DB_SUCC);
151 ASSERT_EQ(blocksStat.size(), 1);
152 ASSERT_EQ(blocksStat[TEST_BLOCK].first, ADD_NUM);
153 ASSERT_EQ(blocksStat[TEST_BLOCK].second, TEST_PACKAGE.size() * ADD_NUM);
154
155 // take one record from the block
156 std::vector<std::string> packages;
157 result = block->Take(TEST_PACKAGE.size(), packages);
158 ASSERT_EQ(result, TEST_PACKAGE.size());
159 ASSERT_EQ(packages.size(), 1);
160
161 // remove one record from the block
162 result = block->Remove(1);
163 ASSERT_EQ(result, DB_SUCC);
164 std::pair<int, int64_t> statPair;
165 result = block->GetStat(statPair);
166 ASSERT_EQ(result, DB_SUCC);
167 ASSERT_EQ(statPair.first, ADD_NUM - 2);
168 ASSERT_EQ(statPair.second, TEST_PACKAGE.size() * (ADD_NUM - 2));
169
170 result = AppEventCache::GetInstance()->DestroyBlock(TEST_BLOCK);
171 ASSERT_EQ(result, DB_SUCC);
172 result = AppEventCache::GetInstance()->Close();
173 ASSERT_EQ(result, DB_SUCC);
174 }
175
176 /**
177 * @tc.name: HiAppEventCleanTest001
178 * @tc.desc: check the cleaning function of DB.
179 * @tc.type: FUNC
180 * @tc.require: issueI5NTOS
181 */
182 HWTEST_F(HiAppEventCacheTest, HiAppEventCleanTest001, 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 = AppEventCache::GetInstance()->Open(TEST_DIR);
193 ASSERT_EQ(result, DB_SUCC);
194
195 std::string blockNames[] = { "testBlock1", "testBlock2", "testBlock3" };
196 const int ADD_NUM = 10;
197 for (auto& blockName : blockNames) {
198 result = AppEventCache::GetInstance()->CreateBlock(blockName);
199 ASSERT_EQ(result, DB_SUCC);
200 auto block = AppEventCache::GetInstance()->GetBlock(blockName);
201 ASSERT_NE(block, nullptr);
202 for (int i = 0; i < ADD_NUM; ++i) {
203 result = block->Add(TEST_PACKAGE);
204 ASSERT_EQ(result, DB_SUCC);
205 }
206 }
207
208 // trigger cleanup
209 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
210 HiAppEventConfig::GetInstance().SetConfigurationItem("max_storage", "1024b");
211 WriteEvent(std::make_shared<AppEventPack>("name", 1));
212
213 std::map<std::string, std::pair<int, int64_t>> blocksStat;
214 result = AppEventCache::GetInstance()->GetBlocksStat(blocksStat);
215 ASSERT_EQ(result, DB_SUCC);
216 ASSERT_EQ(blocksStat.size(), 3);
217 for (auto& blockStat : blocksStat) {
218 auto statPair = blockStat.second;
219 ASSERT_TRUE(statPair.first < ADD_NUM);
220 ASSERT_TRUE(statPair.second < (TEST_PACKAGE.size() * ADD_NUM));
221 }
222
223 result = AppEventCache::GetInstance()->Close();
224 ASSERT_EQ(result, DB_SUCC);
225 }
226
227 /**
228 * @tc.name: HiAppEventCleanTest002
229 * @tc.desc: check the result of clear data.
230 * @tc.type: FUNC
231 * @tc.require: issueI5NTOS
232 */
233 HWTEST_F(HiAppEventCacheTest, HiAppEventCleanTest002, TestSize.Level1)
234 {
235 /**
236 * @tc.steps: step1. create log file.
237 * @tc.steps: step2. create db file.
238 * @tc.steps: step3. clear the data.
239 */
240 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
241 HiAppEventConfig::GetInstance().SetConfigurationItem("max_storage", "10M");
242 WriteEvent(std::make_shared<AppEventPack>("name", 1));
243
244 int result = AppEventCache::GetInstance()->Open(TEST_DIR);
245 ASSERT_EQ(result, DB_SUCC);
246 result = AppEventCache::GetInstance()->CreateBlock(TEST_BLOCK);
247 ASSERT_EQ(result, DB_SUCC);
248 auto block = AppEventCache::GetInstance()->GetBlock(TEST_BLOCK);
249 ASSERT_NE(block, nullptr);
250 result = block->Add(TEST_PACKAGE);
251 ASSERT_EQ(result, DB_SUCC);
252
253 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
254 ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_DIR));
255
256 HiAppEventClean::ClearData(TEST_DIR);
257 ASSERT_FALSE(FileUtil::IsFileExists(TEST_DB_DIR));
258 ASSERT_FALSE(FileUtil::IsFileExists(TEST_DIR));
259 }
260
261