• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CreateAppEventPack()38 std::shared_ptr<AppEventPack> CreateAppEventPack()
39 {
40     return std::make_shared<AppEventPack>(TEST_EVENT_DOMAIN, TEST_EVENT_NAME, TEST_EVENT_TYPE);
41 }
42 }
43 
SetUp()44 void HiAppEventCacheTest::SetUp()
45 {
46     HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
47 }
48 
49 /**
50  * @tc.name: HiAppEventDBTest001
51  * @tc.desc: check the query result of DB operation.
52  * @tc.type: FUNC
53  * @tc.require: issueI5K0X6
54  */
55 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest001, TestSize.Level0)
56 {
57     /**
58      * @tc.steps: step1. open the db.
59      * @tc.steps: step2. insert record to the tables.
60      * @tc.steps: step3. query records from tables.
61      */
62     int result = AppEventStore::GetInstance().InitDbStore();;
63     ASSERT_EQ(result, 0);
64 
65     int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
66     ASSERT_GT(eventSeq, 0);
67     int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
68     ASSERT_GT(observerSeq, 0);
69     int64_t mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
70     ASSERT_GT(mappingSeq, 0);
71 
72     std::vector<std::shared_ptr<AppEventPack>> events;
73     result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
74     ASSERT_EQ(result, 0);
75     ASSERT_GT(events.size(), 0);
76     ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
77     ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
78     ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
79 
80     std::vector<int64_t> observerSeqs;
81     result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
82     ASSERT_EQ(result, 0);
83     ASSERT_GT(observerSeqs.size(), 0);
84     ASSERT_EQ(observerSeqs[0], observerSeq);
85 
86     result = AppEventStore::GetInstance().DestroyDbStore();;
87     ASSERT_EQ(result, 0);
88 }
89 
90 /**
91  * @tc.name: HiAppEventDBTest002
92  * @tc.desc: check the take result of DB operation.
93  * @tc.type: FUNC
94  * @tc.require: issueI5K0X6
95  */
96 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest002, TestSize.Level0)
97 {
98     /**
99      * @tc.steps: step1. open the db.
100      * @tc.steps: step2. insert record to the tables.
101      * @tc.steps: step3. take records from tables.
102      */
103     int result = AppEventStore::GetInstance().InitDbStore();;
104     ASSERT_EQ(result, 0);
105 
106     auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
107     ASSERT_GT(eventSeq, 0);
108     auto observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
109     ASSERT_GT(observerSeq, 0);
110     auto mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
111     ASSERT_GT(mappingSeq, 0);
112 
113     std::vector<std::shared_ptr<AppEventPack>> events;
114     result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
115     ASSERT_EQ(result, 0);
116     ASSERT_GT(events.size(), 0);
117     ASSERT_EQ(events[0]->GetDomain(), TEST_EVENT_DOMAIN);
118     ASSERT_EQ(events[0]->GetName(), TEST_EVENT_NAME);
119     ASSERT_EQ(events[0]->GetType(), TEST_EVENT_TYPE);
120 
121     events.clear();
122     result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
123     ASSERT_EQ(result, 0);
124     ASSERT_EQ(events.size(), 0);
125 
126     result = AppEventStore::GetInstance().DestroyDbStore();;
127     ASSERT_EQ(result, 0);
128 }
129 
130 /**
131  * @tc.name: HiAppEventDBTest003
132  * @tc.desc: check the delete result of DB operation.
133  * @tc.type: FUNC
134  * @tc.require: issueI5NTOD
135  */
136 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest003, TestSize.Level0)
137 {
138    /**
139      * @tc.steps: step1. open the db.
140      * @tc.steps: step2. insert record to the tables.
141      * @tc.steps: step3. delete records from tables.
142      */
143     int result = AppEventStore::GetInstance().InitDbStore();;
144     ASSERT_EQ(result, 0);
145 
146     auto eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
147     ASSERT_GT(eventSeq, 0);
148     auto observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
149     ASSERT_GT(observerSeq, 0);
150     auto mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
151     ASSERT_GT(mappingSeq, 0);
152 
153     result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
154     ASSERT_EQ(result, 1); // 1 recored
155     std::vector<int64_t> observerSeqs;
156     result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
157     ASSERT_EQ(result, 0);
158     ASSERT_EQ(observerSeqs.size(), 0);
159 
160     result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
161     ASSERT_EQ(result, 0); // 0 recored
162     std::vector<std::shared_ptr<AppEventPack>> events;
163     result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
164     ASSERT_EQ(result, 0);
165     ASSERT_EQ(events.size(), 0);
166 }
167 
168 /**
169  * @tc.name: HiAppEventDBTest004
170  * @tc.desc: revisit the DB after destroying it.
171  * @tc.type: FUNC
172  * @tc.require: issueI5NTOS
173  */
174 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest004, TestSize.Level1)
175 {
176     /**
177      * @tc.steps: step1. open the db.
178      * @tc.steps: step2. create block table.
179      * @tc.steps: step3. add record to the block table.
180      * @tc.steps: step4. config the max storage size.
181      * @tc.steps: step5. trigger cleanup.
182      * @tc.steps: step6. close the db.
183      */
184     int result = AppEventStore::GetInstance().DestroyDbStore();;
185     ASSERT_EQ(result, 0);
186 
187     int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(CreateAppEventPack());
188     ASSERT_GT(eventSeq, 0);
189     int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(TEST_OBSERVER_NAME);
190     ASSERT_GT(observerSeq, 0);
191     int64_t mappingSeq = AppEventStore::GetInstance().InsertEventMapping(eventSeq, observerSeq);
192     ASSERT_GT(mappingSeq, 0);
193 
194     std::vector<std::shared_ptr<AppEventPack>> events;
195     result = AppEventStore::GetInstance().QueryEvents(events, observerSeq);
196     ASSERT_EQ(result, 0);
197     std::vector<int64_t> observerSeqs;
198     result = AppEventStore::GetInstance().QueryObserverSeqs(TEST_OBSERVER_NAME, observerSeqs);
199     ASSERT_EQ(result, 0);
200     result = AppEventStore::GetInstance().TakeEvents(events, observerSeq);
201     ASSERT_EQ(result, 0);
202 
203     result = AppEventStore::GetInstance().DeleteObserver(observerSeq);
204     ASSERT_EQ(result, 1); // 1 recored
205     result = AppEventStore::GetInstance().DeleteEventMapping(observerSeq, {eventSeq});
206     ASSERT_EQ(result, 0); // 0 recored
207 }
208 
209 /**
210  * @tc.name: HiAppEventDBTest005
211  * @tc.desc: check the result of clear data.
212  * @tc.type: FUNC
213  * @tc.require: issueI5NTOS
214  */
215 HWTEST_F(HiAppEventCacheTest, HiAppEventDBTest005, TestSize.Level1)
216 {
217     /**
218      * @tc.steps: step1. create log file.
219      * @tc.steps: step2. create db file.
220      * @tc.steps: step3. clear the data.
221      */
222     WriteEvent(std::make_shared<AppEventPack>("name", 1));
223     std::vector<std::string> files;
224     FileUtil::GetDirFiles(TEST_DIR, files);
225     ASSERT_FALSE(files.empty());
226     ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
227     ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
228 
229     HiAppEventClean::ClearData(TEST_DIR);
230     ASSERT_TRUE(FileUtil::IsFileExists(TEST_DB_PATH));
231     ASSERT_TRUE(FileUtil::IsFileExists(TEST_DIR));
232     for (const auto& file : files) {
233         ASSERT_FALSE(FileUtil::IsFileExists(file));
234     }
235 }
236 
237