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 "security_guard_data_collect_test.h"
17
18 #include "directory_ex.h"
19 #include "file_ex.h"
20 #include "gmock/gmock.h"
21
22 #include "security_guard_define.h"
23 #include "security_guard_log.h"
24 #include "security_guard_utils.h"
25 #include "store_define.h"
26 #include "rdb_helper.h"
27 #define private public
28 #define protected public
29 #include "config_data_manager.h"
30 #include "database.h"
31 #include "database_manager.h"
32 #include "device_manager.h"
33 #include "os_account_manager.h"
34 #include "preferences_helper.h"
35 #include "risk_event_rdb_helper.h"
36 #undef private
37 #undef protected
38
39 using namespace testing;
40 using namespace testing::ext;
41 using namespace OHOS::Security::SecurityGuard;
42 using namespace OHOS::Security::SecurityGuardTest;
43
44 namespace OHOS {
45 std::shared_ptr<NativeRdb::MockRdbHelperInterface> NativeRdb::RdbHelper::instance_ = nullptr;
46 std::shared_ptr<AccountSA::MockOsAccountManagerInterface> AccountSA::OsAccountManager::instance_ = nullptr;
47 std::shared_ptr<NativePreferences::MockPreferenceHelperInterface>
48 NativePreferences::PreferencesHelper::instance_ = nullptr;
49 std::mutex NativeRdb::RdbHelper::mutex_ {};
50 std::mutex AccountSA::OsAccountManager::mutex_ {};
51 std::mutex NativePreferences::PreferencesHelper::mutex_ {};
52 }
53
54 namespace OHOS::Security::SecurityGuardTest {
55 namespace {
56 constexpr int SUCCESS = 0;
57 constexpr int FAILED = -1;
58 }
59
SetUpTestCase()60 void SecurityGuardDataCollectTest::SetUpTestCase()
61 {
62 }
63
TearDownTestCase()64 void SecurityGuardDataCollectTest::TearDownTestCase()
65 {
66 NativeRdb::RdbHelper::DelInterface();
67 }
68
SetUp()69 void SecurityGuardDataCollectTest::SetUp()
70 {
71 }
72
TearDown()73 void SecurityGuardDataCollectTest::TearDown()
74 {
75 }
76
77 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock001, TestSize.Level1)
78 {
79 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
80 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
81 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
82 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60202( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 83 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
84 errCode = SUCCESS;
85 return rdbStoreMock;
86 });
87 EXPECT_CALL(*rdbStoreMock, Query(_, _))
88 .WillRepeatedly(
__anon0f1ec7c60302(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 89 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
90 return resultSetMock;
91 });
92 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
93 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(0));
94 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
95 EXPECT_CALL(*resultSetMock, GoToNextRow).WillRepeatedly(Return(-1));
96 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
97 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
98 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
99 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
100 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
101 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
102 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
103 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
104 EXPECT_CALL(*rdbStoreMock, Insert).WillOnce(Return(FAILED)).WillOnce(Return(SUCCESS));
105 RiskEventRdbHelper helper;
106 int32_t ret = helper.Init();
107 EXPECT_EQ(ret, SUCCESS);
108 SecEvent event;
109 ret = helper.InsertEvent(event);
110 EXPECT_EQ(ret, DB_OPT_ERR);
111 ret = helper.InsertEvent(event);
112 EXPECT_EQ(ret, SUCCESS);
113 }
114
115 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock002, TestSize.Level1)
116 {
117 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
118 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
119 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
120 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60402( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 121 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
122 errCode = SUCCESS;
123 return rdbStoreMock;
124 });
125 EXPECT_CALL(*rdbStoreMock, Query(_, _)).WillOnce(Return(nullptr))
126 .WillRepeatedly(
__anon0f1ec7c60502(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 127 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
128 return resultSetMock;
129 });
130 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
131 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(0));
132 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
133 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
134 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
135 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
136 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
137 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
138 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
139 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
140 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
141 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
142 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
143 RiskEventRdbHelper helper;
144 int32_t ret = helper.Init();
145 EXPECT_EQ(ret, SUCCESS);
146 int64_t eventId = 0;
147 SecEvent event;
148 ret = helper.QueryRecentEventByEventId(eventId, event);
149 EXPECT_EQ(ret, DB_OPT_ERR);
150 ret = helper.QueryRecentEventByEventId(eventId, event);
151 EXPECT_EQ(ret, SUCCESS);
152 ret = helper.QueryRecentEventByEventId(eventId, event);
153 EXPECT_EQ(ret, SUCCESS);
154 }
155
156 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock003, TestSize.Level1)
157 {
158 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
159 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
160 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
161 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60602( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 162 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
163 errCode = SUCCESS;
164 return rdbStoreMock;
165 });
166 EXPECT_CALL(*rdbStoreMock, Query(_, _)).WillOnce(Return(nullptr))
167 .WillRepeatedly(
__anon0f1ec7c60702(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 168 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
169 return resultSetMock;
170 });
171 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
172 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
173 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
174 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
175 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
176 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
177 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
178 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
179 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
180 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
181 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
182 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
183 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
184 RiskEventRdbHelper helper;
185 int32_t ret = helper.Init();
186 EXPECT_EQ(ret, SUCCESS);
187 std::vector<int64_t> eventIds;
188 std::vector<SecEvent> events;
189 ret = helper.QueryRecentEventByEventId(eventIds, events);
190 EXPECT_EQ(ret, BAD_PARAM);
191 eventIds.emplace_back(0);
192 ret = helper.QueryRecentEventByEventId(eventIds, events);
193 EXPECT_EQ(ret, DB_OPT_ERR);
194 ret = helper.QueryRecentEventByEventId(eventIds, events);
195 EXPECT_EQ(ret, SUCCESS);
196 }
197
198 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock004, TestSize.Level1)
199 {
200 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
201 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
202 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
203 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60802( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 204 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
205 errCode = SUCCESS;
206 return rdbStoreMock;
207 });
208 EXPECT_CALL(*rdbStoreMock, Query(_, _))
209 .WillRepeatedly(
__anon0f1ec7c60902(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 210 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
211 return resultSetMock;
212 });
213 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
214 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
215 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
216 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
217 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
218 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
219 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
220 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
221 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
222 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
223 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
224 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
225 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
226 RiskEventRdbHelper helper;
227 int32_t ret = helper.Init();
228 EXPECT_EQ(ret, SUCCESS);
229 std::vector<int64_t> eventIds;
230 std::vector<SecEvent> events;
231 ret = helper.QueryEventByEventId(eventIds, events);
232 EXPECT_EQ(ret, BAD_PARAM);
233 eventIds.emplace_back(0);
234 ret = helper.QueryEventByEventId(eventIds, events);
235 EXPECT_EQ(ret, SUCCESS);
236 }
237
238 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock005, TestSize.Level1)
239 {
240 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
241 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
242 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
243 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60a02( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 244 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
245 errCode = SUCCESS;
246 return rdbStoreMock;
247 });
248 EXPECT_CALL(*rdbStoreMock, Query(_, _))
249 .WillRepeatedly(
__anon0f1ec7c60b02(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 250 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
251 return resultSetMock;
252 });
253 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
254 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
255 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
256 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
257 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
258 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
259 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
260 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
261 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
262 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
263 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
264 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
265 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
266 RiskEventRdbHelper helper;
267 int32_t ret = helper.Init();
268 EXPECT_EQ(ret, SUCCESS);
269 std::vector<int64_t> eventIds;
270 std::vector<SecEvent> events;
271 std::string data = "202301011200";
272 ret = helper.QueryEventByEventIdAndDate(eventIds, events, data, data);
273 EXPECT_EQ(ret, BAD_PARAM);
274 eventIds.emplace_back(0);
275 ret = helper.QueryEventByEventIdAndDate(eventIds, events, data, data);
276 EXPECT_EQ(ret, SUCCESS);
277 }
278
279 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock006, TestSize.Level1)
280 {
281 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
282 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
283 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
284 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60c02( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 285 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
286 errCode = SUCCESS;
287 return rdbStoreMock;
288 });
289 EXPECT_CALL(*rdbStoreMock, Query(_, _))
290 .WillRepeatedly(
__anon0f1ec7c60d02(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 291 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
292 return resultSetMock;
293 });
294 EXPECT_CALL(*rdbStoreMock, Delete).WillRepeatedly(Return(SUCCESS));
295 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
296 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
297 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
298 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
299 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
300 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
301 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
302 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
303 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
304 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
305 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
306 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
307 RiskEventRdbHelper helper;
308 int32_t ret = helper.Init();
309 EXPECT_EQ(ret, SUCCESS);
310 std::vector<int64_t> eventIds;
311 std::vector<SecEvent> events;
312 std::string data = "202301011200";
313 ret = helper.QueryEventByEventIdAndDate(eventIds, events, data, data);
314 EXPECT_EQ(ret, BAD_PARAM);
315 eventIds.emplace_back(0);
316 ret = helper.QueryEventByEventIdAndDate(eventIds, events, data, data);
317 EXPECT_EQ(ret, SUCCESS);
318 }
319
320 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock007, TestSize.Level1)
321 {
322 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
323 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
324 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60e02( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 325 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
326 errCode = SUCCESS;
327 return rdbStoreMock;
328 });
329 EXPECT_CALL(*rdbStoreMock, Count).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
330 RiskEventRdbHelper helper;
331 int32_t ret = helper.Init();
332 EXPECT_EQ(ret, SUCCESS);
333 int64_t count = helper.CountAllEvent();
334 EXPECT_EQ(count, 0);
335 count = helper.CountAllEvent();
336 EXPECT_EQ(count, 0);
337 }
338
339 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock008, TestSize.Level1)
340 {
341 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
342 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
343 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c60f02( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 344 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
345 errCode = SUCCESS;
346 return rdbStoreMock;
347 });
348 EXPECT_CALL(*rdbStoreMock, Count).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
349 RiskEventRdbHelper helper;
350 int32_t ret = helper.Init();
351 EXPECT_EQ(ret, SUCCESS);
352 int64_t count = helper.CountEventByEventId(0);
353 EXPECT_EQ(count, 0);
354 count = helper.CountEventByEventId(0);
355 EXPECT_EQ(count, 0);
356 }
357
358 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock009, TestSize.Level1)
359 {
360 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
361 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
362 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
363 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c61002( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 364 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
365 errCode = SUCCESS;
366 return rdbStoreMock;
367 });
368 EXPECT_CALL(*rdbStoreMock, Query(_, _)).WillOnce(Return(nullptr))
369 .WillRepeatedly(
__anon0f1ec7c61102(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 370 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
371 return resultSetMock;
372 });
373 EXPECT_CALL(*rdbStoreMock, Delete).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
374 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
375 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
376 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
377 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
378 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
379 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
380 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
381 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
382 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
383 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
384 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
385 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
386 RiskEventRdbHelper helper;
387 int32_t ret = helper.Init();
388 EXPECT_EQ(ret, SUCCESS);
389 int64_t eventId = 0;
390 int64_t count = 0;
391 ret = helper.DeleteOldEventByEventId(eventId, count);
392 EXPECT_EQ(ret, DB_OPT_ERR);
393 ret = helper.DeleteOldEventByEventId(eventId, count);
394 EXPECT_EQ(ret, DB_OPT_ERR);
395 ret = helper.DeleteOldEventByEventId(eventId, count);
396 EXPECT_EQ(ret, SUCCESS);
397 }
398
399 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock010, TestSize.Level1)
400 {
401 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
402 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
403 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c61202( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 404 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
405 errCode = SUCCESS;
406 return rdbStoreMock;
407 });
408 EXPECT_CALL(*rdbStoreMock, Delete).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
409 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
410 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
411 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
412 RiskEventRdbHelper helper;
413 int32_t ret = helper.Init();
414 EXPECT_EQ(ret, SUCCESS);
415 int64_t eventId = 0;
416 ret = helper.DeleteAllEventByEventId(eventId);
417 EXPECT_EQ(ret, DB_OPT_ERR);
418 ret = helper.DeleteAllEventByEventId(eventId);
419 EXPECT_EQ(ret, SUCCESS);
420 }
421
422 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock011, TestSize.Level1)
423 {
424 auto rdbStoreMock = std::make_shared<NativeRdb::RdbStore>();
425 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
426 EXPECT_CALL(*(NativeRdb::RdbHelper::GetInterface()), GetRdbStore)
427 .WillRepeatedly([&rdbStoreMock] (
__anon0f1ec7c61302( const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) 428 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode) {
429 errCode = SUCCESS;
430 return rdbStoreMock;
431 });
432 EXPECT_CALL(*rdbStoreMock, Query(_, _)).WillOnce(Return(nullptr))
433 .WillRepeatedly(
__anon0f1ec7c61402(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) 434 [&resultSetMock] (const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns) {
435 return resultSetMock;
436 });
437 EXPECT_CALL(*rdbStoreMock, Attach).WillRepeatedly(Return(SUCCESS));
438 EXPECT_CALL(*rdbStoreMock, BatchInsert).WillRepeatedly(Return(SUCCESS));
439 EXPECT_CALL(*resultSetMock, GoToNextRow).WillOnce(Return(SUCCESS)).WillRepeatedly(Return(FAILED));
440 EXPECT_CALL(*resultSetMock, GetString).WillRepeatedly(Return(0));
441 EXPECT_CALL(*resultSetMock, GetLong).WillRepeatedly(Return(0));
442 EXPECT_CALL(*resultSetMock, GetInt).WillRepeatedly(Return(0));
443 EXPECT_CALL(*resultSetMock, GoToRow).WillRepeatedly(Return(0));
444 EXPECT_CALL(*resultSetMock, GetRowCount).WillRepeatedly(Return(0));
445 EXPECT_CALL(*resultSetMock, GetColumnCount).WillRepeatedly(Return(0));
446 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillRepeatedly(Return(0));
447 EXPECT_CALL(*resultSetMock, Close).WillRepeatedly(Return(0));
448 EXPECT_CALL(*rdbStoreMock, Insert).WillRepeatedly(Return(SUCCESS));
449 RiskEventRdbHelper helper;
450 int32_t ret = helper.Init();
451 EXPECT_EQ(ret, SUCCESS);
452 NativeRdb::RdbPredicates predicates("");
453 std::vector<SecEvent> events;
454 ret = helper.QueryEventBase(predicates, events);
455 EXPECT_EQ(ret, DB_OPT_ERR);
456 ret = helper.QueryEventBase(predicates, events);
457 EXPECT_EQ(ret, SUCCESS);
458 }
459
460 HWTEST_F(SecurityGuardDataCollectTest, TestRiskEventRdbHelperMock012, TestSize.Level1)
461 {
462 auto resultSetMock = std::make_shared<NativeRdb::ResultSet>();
463 EXPECT_CALL(*resultSetMock, GetRowCount).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
464 EXPECT_CALL(*resultSetMock, GetColumnCount).WillOnce(Return(FAILED)).WillRepeatedly(Return(SUCCESS));
465 EXPECT_CALL(*resultSetMock, GetAllColumnNames).WillOnce(Return(FAILED)).WillRepeatedly(
__anon0f1ec7c61502(std::vector<std::string> &columnNames) 466 [] (std::vector<std::string> &columnNames) {
467 columnNames.emplace_back(ID);
468 columnNames.emplace_back(EVENT_ID);
469 columnNames.emplace_back(VERSION);
470 columnNames.emplace_back(DATE);
471 columnNames.emplace_back(CONTENT);
472 columnNames.emplace_back(USER_ID);
473 columnNames.emplace_back(DEVICE_ID);
474 return SUCCESS;
475 });
476 RiskEventRdbHelper helper;
477 SecEventTableInfo table;
478 int32_t ret = helper.GetResultSetTableInfo(resultSetMock, table);
479 EXPECT_EQ(ret, DB_LOAD_ERR);
480 ret = helper.GetResultSetTableInfo(resultSetMock, table);
481 EXPECT_EQ(ret, DB_LOAD_ERR);
482 ret = helper.GetResultSetTableInfo(resultSetMock, table);
483 EXPECT_EQ(ret, DB_LOAD_ERR);
484 ret = helper.GetResultSetTableInfo(resultSetMock, table);
485 EXPECT_EQ(ret, SUCCESS);
486 }
487 }