1 /*
2 * Copyright (c) 2021 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 <gtest/gtest.h>
17 #include <thread>
18
19 #include "db_common.h"
20 #include "db_constant.h"
21 #include "db_errno.h"
22 #include "distributeddb_data_generate_unit_test.h"
23 #include "kv_store_nb_delegate_impl.h"
24 #include "platform_specific.h"
25 #include "process_system_api_adapter_impl.h"
26 #include "sqlite_single_ver_natural_store.h"
27 #include "sqlite_single_ver_natural_store_connection.h"
28 #include "storage_engine_manager.h"
29
30 using namespace testing::ext;
31 using namespace DistributedDB;
32 using namespace DistributedDBUnitTest;
33 using namespace std;
34
35 namespace {
36 string g_testDir;
37 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
38 KvStoreConfig g_config;
39 KvDBProperties g_property;
40 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
41 DBStatus g_kvDelegateStatus = INVALID_ARGS;
42 SQLiteSingleVerNaturalStore *g_store = nullptr;
43 DistributedDB::SQLiteSingleVerNaturalStoreConnection *g_connection = nullptr;
44 const string STORE_ID = STORE_ID_SYNC;
45 const int TIME_LAG = 100;
46 const std::string DEVICE_ID_1 = "ABC";
47 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
48 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
49 }
50
51 class DistributedDBDeviceIdentifierTest : public testing::Test {
52 public:
53 static void SetUpTestCase(void);
54 static void TearDownTestCase(void);
55 void SetUp();
56 void TearDown();
57 };
58
SetUpTestCase(void)59 void DistributedDBDeviceIdentifierTest::SetUpTestCase(void)
60 {
61 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
62 g_config.dataDir = g_testDir;
63 g_mgr.SetKvStoreConfig(g_config);
64
65 string dir = g_testDir + STORE_ID + "/" + DBConstant::SINGLE_SUB_DIR;
66 DIR *dirTmp = opendir(dir.c_str());
67 if (dirTmp == nullptr) {
68 OS::MakeDBDirectory(dir);
69 } else {
70 closedir(dirTmp);
71 }
72 g_property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
73 g_property.SetStringProp(KvDBProperties::STORE_ID, STORE_ID);
74 g_property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
75 }
76
TearDownTestCase(void)77 void DistributedDBDeviceIdentifierTest::TearDownTestCase(void)
78 {
79 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + STORE_ID + "/" + DBConstant::SINGLE_SUB_DIR) != 0) {
80 LOGE("rm test db files error!");
81 }
82 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
83 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_LAG));
84 }
85
SetUp(void)86 void DistributedDBDeviceIdentifierTest::SetUp(void)
87 {
88 DistributedDBToolsUnitTest::PrintTestCaseInfo();
89 KvStoreNbDelegate::Option option = {true, false, false};
90 g_mgr.GetKvStore(STORE_ID, option, g_kvNbDelegateCallback);
91 EXPECT_TRUE(g_kvDelegateStatus == OK);
92 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
93
94 g_store = new (std::nothrow) SQLiteSingleVerNaturalStore;
95 ASSERT_NE(g_store, nullptr);
96 ASSERT_EQ(g_store->Open(g_property), E_OK);
97
98 int erroCode = E_OK;
99 g_connection = static_cast<SQLiteSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(erroCode));
100 ASSERT_NE(g_connection, nullptr);
101 g_store->DecObjRef(g_store);
102 EXPECT_EQ(erroCode, E_OK);
103 }
104
TearDown(void)105 void DistributedDBDeviceIdentifierTest::TearDown(void)
106 {
107 if (g_connection != nullptr) {
108 g_connection->Close();
109 }
110
111 g_store = nullptr;
112
113 if (g_kvNbDelegatePtr != nullptr) {
114 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
115 g_kvNbDelegatePtr = nullptr;
116 EXPECT_TRUE(g_mgr.DeleteKvStore(STORE_ID) == OK);
117 }
118 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_LAG));
119 }
120
121 /**
122 * @tc.name: DeviceIdentifier001
123 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY,
124 * set Key to be null and origDevice to be false, expect return INVALID_ARGS.
125 * @tc.type: FUNC
126 * @tc.require: AR000D08KV
127 * @tc.author: maokeheng
128 */
129 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier001, TestSize.Level1)
130 {
131 /**
132 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
133 * @tc.expected: step1. Expect return true.
134 */
135 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
136
137 /**
138 * @tc.steps:step2. Set PragmaCmd to be GET_DEVICE_IDENTIFIER_OF_ENTRY, and set input key to be null
139 * @tc.expected: step2. Expect return INVALID_ARGS.
140 */
141 Key keyNull;
142 PragmaEntryDeviceIdentifier param;
143 param.key = keyNull;
144 param.origDevice = false;
145 PragmaData input = static_cast<void *>(¶m);
146 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), INVALID_ARGS);
147 }
148
149 /**
150 * @tc.name: DeviceIdentifier002
151 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY,
152 * set Key to be null and origDevice to be true, expect return INVALID_ARGS.
153 * @tc.type: FUNC
154 * @tc.require: AR000D08KV
155 * @tc.author: maokeheng
156 */
157 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier002, TestSize.Level1)
158 {
159 /**
160 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
161 * @tc.expected: step1. Expect return true.
162 */
163 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
164
165 /**
166 * @tc.steps:step2. Set PragmaCmd to be GET_DEVICE_IDENTIFIER_OF_ENTRY, and set input key to be null
167 * @tc.expected: step2. Expect return INVALID_ARGS.
168 */
169 Key keyNull;
170 PragmaEntryDeviceIdentifier param;
171 param.key = keyNull;
172 param.origDevice = true;
173 PragmaData input = static_cast<void *>(¶m);
174 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), INVALID_ARGS);
175 }
176
177 /**
178 * @tc.name: DeviceIdentifier003
179 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY and origDevice to be false.
180 * Check if a non-existing key will return NOT_FOUND.
181 * @tc.type: FUNC
182 * @tc.require: AR000D08KV
183 * @tc.author: maokeheng
184 */
185 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier003, TestSize.Level1)
186 {
187 /**
188 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
189 * @tc.expected: step1. Expect return true.
190 */
191 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
192 /**
193 * @tc.steps:step2. Set PragmaCmd to be GET_DEVICE_IDENTIFIER_OF_ENTRY, and set Key= Key_2
194 * @tc.expected: step2. Expect return NOT_FOUND.
195 */
196 PragmaEntryDeviceIdentifier param;
197 param.key = KEY_2;
198 param.origDevice = false;
199 PragmaData input = static_cast<void *>(¶m);
200 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), NOT_FOUND);
201 }
202
203 /**
204 * @tc.name: DeviceIdentifier004
205 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY and origDevice to be true.
206 * Check if a non-existing key will return NOT_FOUND.
207 * @tc.type: FUNC
208 * @tc.require: AR000D08KV
209 * @tc.author: maokeheng
210 */
211 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier004, TestSize.Level1)
212 {
213 /**
214 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
215 * @tc.expected: step1. Expect return true.
216 */
217 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
218 /**
219 * @tc.steps:step2. Set PragmaCmd to be GET_DEVICE_IDENTIFIER_OF_ENTRY, and set Key= Key_2
220 * @tc.expected: step2. Expect return NOT_FOUND.
221 */
222 PragmaEntryDeviceIdentifier param;
223 param.key = KEY_2;
224 param.origDevice = true;
225 PragmaData input = static_cast<void *>(¶m);
226 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), NOT_FOUND);
227 }
228
229 /**
230 * @tc.name: DeviceIdentifier005
231 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY and origDevice to be false. check if returns OK.
232 * @tc.type: FUNC
233 * @tc.require: AR000D08KV
234 * @tc.author: maokeheng
235 */
236 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier005, TestSize.Level1)
237 {
238 /**
239 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
240 * @tc.expected: step1. Expect return true.
241 */
242 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
243 /**
244 * @tc.steps:step2. Set PragmaCmd = GET_DEVICE_IDENTIFIER_OF_ENTRY, Key= Key_1, origDevice = false.
245 * @tc.expected: step2. Expect return deviceIdentifier is the same as deviceIdentifier of DEVICE_B.
246 */
247 PragmaEntryDeviceIdentifier param;
248 param.key = KEY_1;
249 param.origDevice = false;
250 PragmaData input = static_cast<void *>(¶m);
251 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), OK);
252 }
253
254 /**
255 * @tc.name: DeviceIdentifier006
256 * @tc.desc: Set pragma to be GET_DEVICE_IDENTIFIER_OF_ENTRY and origDevice to be true. check if returns OK.
257 * @tc.type: FUNC
258 * @tc.require: AR000D08KV
259 * @tc.author: maokeheng
260 */
261 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier006, TestSize.Level1)
262 {
263 /**
264 * @tc.steps:step1. Sync 1K data from DEVICE_B into database, with Key= KEY_1, and Value = VALUE_1.
265 * @tc.expected: step1. Expect return true.
266 */
267 g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
268 /**
269 * @tc.steps:step2. Set PragmaCmd = GET_DEVICE_IDENTIFIER_OF_ENTRY, Key= Key_1, origDevice = false.
270 * @tc.expected: step2. Expect return deviceIdentifier is the same as deviceIdentifier of DEVICE_B.
271 */
272 PragmaEntryDeviceIdentifier param;
273 param.key = KEY_1;
274 param.origDevice = true;
275 PragmaData input = static_cast<void *>(¶m);
276 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_DEVICE_IDENTIFIER_OF_ENTRY, input), OK);
277 }
278
279 /**
280 * @tc.name: DeviceIdentifier007
281 * @tc.desc: Set pragma to be GET_IDENTIFIER_OF_DEVICE. check if empty deviceID returns INVALID_ARGS.
282 * @tc.type: FUNC
283 * @tc.require: AR000D08KV
284 * @tc.author: maokeheng
285 */
286 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier007, TestSize.Level1)
287 {
288 /**
289 * @tc.steps:step1. Set PragmaCmd = GET_IDENTIFIER_OF_DEVICE, deviceID= NULL.
290 * @tc.expected: step1. Expect return INVALID_ARGS.
291 */
292 PragmaDeviceIdentifier param;
293 param.deviceID = "";
294 PragmaData input = static_cast<void *>(¶m);
295 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_IDENTIFIER_OF_DEVICE, input), INVALID_ARGS);
296 }
297
298 /**
299 * @tc.name: DeviceIdentifier008
300 * @tc.desc: Set pragma to be GET_IDENTIFIER_OF_DEVICE. check if deviceIdentifier matches deviceID.
301 * @tc.type: FUNC
302 * @tc.require: AR000D08KV
303 * @tc.author: maokeheng
304 */
305 HWTEST_F(DistributedDBDeviceIdentifierTest, DeviceIdentifier008, TestSize.Level1)
306 {
307 /**
308 * @tc.steps:step1. Set PragmaCmd = GET_IDENTIFIER_OF_DEVICE, deviceID = DEVICE_ID_1
309 * @tc.expected: step1. Expect return deviceIdentifier is the same as deviceIdentifier of DEVICE_ID_1.
310 */
311 PragmaDeviceIdentifier param;
312 param.deviceID = DEVICE_ID_1;
313 PragmaData input = static_cast<void *>(¶m);
314 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(GET_IDENTIFIER_OF_DEVICE, input), OK);
315 EXPECT_EQ(param.deviceIdentifier, DBCommon::TransferHashString(DEVICE_ID_1));
316 }
317
318 /**
319 * @tc.name: ErrDbTest001
320 * @tc.desc: Initialize check when the database is not opened
321 * @tc.type: FUNC
322 * @tc.require:
323 * @tc.author: bty
324 */
325 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest001, TestSize.Level1)
326 {
327 /**
328 * @tc.steps: step1. Initialize an empty db and register nullptr callback
329 * @tc.expected: step1. Expect return not nullptr
330 */
331 SQLiteSingleVerNaturalStore *errStore = new (std::nothrow) SQLiteSingleVerNaturalStore;
332 ASSERT_NE(errStore, nullptr);
333
334 /**
335 * @tc.steps: step2. Get db handle
336 * @tc.expected: step2. Expect return nullptr
337 */
338 int errCode;
339 EXPECT_EQ(errStore->GetHandle(false, errCode), nullptr);
340
341 /**
342 * @tc.steps: step3. Check db integrity
343 * @tc.expected: step3. Expect return -E_INVALID_DB
344 */
345 EXPECT_EQ(errStore->CheckIntegrity(), -E_INVALID_DB);
346
347 /**
348 * @tc.steps: step4. Check cache db mode
349 * @tc.expected: step4. Expect return false
350 */
351 EXPECT_FALSE(errStore->IsCacheDBMode());
352 EXPECT_FALSE(errStore->IsExtendedCacheDBMode());
353
354 errStore->KillAndDecObjRef(errStore);
355 }
356
357 /**
358 * @tc.name: ErrDbTest002
359 * @tc.desc: Register check when the database is not opened
360 * @tc.type: FUNC
361 * @tc.require:
362 * @tc.author: bty
363 */
364 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest002, TestSize.Level1)
365 {
366 /**
367 * @tc.steps: step1. Initialize an empty db
368 * @tc.expected: step1. Expect return not nullptr
369 */
370 std::unique_ptr<SQLiteSingleVerNaturalStore> errStore = std::make_unique<SQLiteSingleVerNaturalStore>();
371 ASSERT_NE(errStore, nullptr);
372
373 /**
374 * @tc.steps: step2. Check register nullptr callback
375 * @tc.expected: step2. Expect return E_OK
376 */
377 EXPECT_EQ(errStore->RegisterLifeCycleCallback(nullptr), E_OK);
378
379 /**
380 * @tc.steps: step3. Check register Conflict type
381 * @tc.expected: step3. Expect return -E_NOT_SUPPORT
382 */
383 RegisterFuncType funcType = OBSERVER_SINGLE_VERSION_NS_PUT_EVENT;
384 EXPECT_EQ(errStore->TransConflictTypeToRegisterFunctionType(0, funcType), -E_NOT_SUPPORT);
385
386 /**
387 * @tc.steps: step4. Check register Observer type
388 * @tc.expected: step4. Expect return -E_NOT_SUPPORT
389 */
390 EXPECT_EQ(errStore->TransObserverTypeToRegisterFunctionType(0, funcType), -E_NOT_SUPPORT);
391 }
392
393 /**
394 * @tc.name: ErrDbTest003
395 * @tc.desc: Export and Import check when the database is not opened
396 * @tc.type: FUNC
397 * @tc.require:
398 * @tc.author: bty
399 */
400 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest003, TestSize.Level1)
401 {
402 /**
403 * @tc.steps: step1. Initialize an empty db
404 * @tc.expected: step1. Expect return not nullptr
405 */
406 std::unique_ptr<SQLiteSingleVerNaturalStore> errStore = std::make_unique<SQLiteSingleVerNaturalStore>();
407 ASSERT_NE(errStore, nullptr);
408
409 /**
410 * @tc.steps: step2. Export
411 * @tc.expected: step2. Expect return -E_INVALID_DB
412 */
413 CipherPassword passwd;
414 EXPECT_EQ(errStore->Export(g_testDir, passwd), -E_INVALID_DB);
415
416 /**
417 * @tc.steps: step3. Import
418 * @tc.expected: step3. Expect return -E_INVALID_DB
419 */
420 EXPECT_EQ(errStore->Import(g_testDir, passwd), -E_INVALID_DB);
421
422 /**
423 * @tc.steps: step4. Reset key
424 * @tc.expected: step4. Expect return -E_INVALID_DB
425 */
426 EXPECT_EQ(errStore->Rekey(passwd), -E_INVALID_DB);
427 }
428
429 /**
430 * @tc.name: ErrDbTest004
431 * @tc.desc: Check the interface of operation data when the database is not opened
432 * @tc.type: FUNC
433 * @tc.require:
434 * @tc.author: bty
435 */
436 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest004, TestSize.Level1)
437 {
438 /**
439 * @tc.steps: step1. Initialize an empty db
440 * @tc.expected: step1. Expect return not nullptr
441 */
442 std::unique_ptr<SQLiteSingleVerNaturalStore> errStore = std::make_unique<SQLiteSingleVerNaturalStore>();
443 ASSERT_NE(errStore, nullptr);
444
445 /**
446 * @tc.steps: step2. Get all meta keys
447 * @tc.expected: step2. Expect return -E_INVALID_DB
448 */
449 std::vector<Key> keys;
450 EXPECT_EQ(errStore->GetAllMetaKeys(keys), -E_INVALID_DB);
451
452 /**
453 * @tc.steps: step3. Delete meta data if the Key is empty
454 * @tc.expected: step3. Expect return -E_INVALID_ARGS
455 */
456 Key key;
457 keys.push_back(key);
458 EXPECT_EQ(errStore->DeleteMetaData(keys), -E_INVALID_ARGS);
459
460 /**
461 * @tc.steps: step4. Delete meta data if the Key is not empty
462 * @tc.expected: step4. Expect return -E_INVALID_DB
463 */
464 keys.front().push_back('A');
465 EXPECT_EQ(errStore->DeleteMetaData(keys), -E_INVALID_DB);
466
467 /**
468 * @tc.steps: step5. put meta data
469 * @tc.expected: step5. Expect return -E_INVALID_DB
470 */
471 Value values;
472 EXPECT_EQ(errStore->PutMetaData(keys.front(), values), -E_INVALID_DB);
473 }
474
475 /**
476 * @tc.name: ErrDbTest005
477 * @tc.desc: Test sync data when the database is not opened
478 * @tc.type: FUNC
479 * @tc.require:
480 * @tc.author: bty
481 */
482 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest005, TestSize.Level1)
483 {
484 std::unique_ptr<SQLiteSingleVerNaturalStore> errStore = std::make_unique<SQLiteSingleVerNaturalStore>();
485 ASSERT_NE(errStore, nullptr);
486 ContinueToken token = nullptr;
487 std::vector<DataItem> dataItems;
488 DataSizeSpecInfo info = {DBConstant::MAX_SYNC_BLOCK_SIZE + 1, 0};
489 EXPECT_EQ(errStore->GetSyncDataNext(dataItems, token, info), -E_INVALID_ARGS);
490 info.blockSize = 0;
491 EXPECT_EQ(errStore->GetSyncDataNext(dataItems, token, info), -E_INVALID_ARGS);
492 errStore->ReleaseContinueToken(token);
493 }
494
495 /**
496 * @tc.name: StorageEngineTest001
497 * @tc.desc: Test cache db remove data
498 * @tc.type: FUNC
499 * @tc.require:
500 * @tc.author: bty
501 */
502 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest001, TestSize.Level1)
503 {
504 int errCode = E_OK;
505 SQLiteSingleVerStorageEngine *storageEngine =
506 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
507 ASSERT_EQ(errCode, E_OK);
508 ASSERT_NE(storageEngine, nullptr);
509 storageEngine->SetEngineState(CACHEDB);
510 EXPECT_EQ(g_store->RemoveDeviceData("device1", false), -1);
511 storageEngine->Release();
512 }
513
514 /**
515 * @tc.name: StorageEngineTest002
516 * @tc.desc: Test the interface of Dump
517 * @tc.type: FUNC
518 * @tc.require:
519 * @tc.author: bty
520 */
521 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest002, TestSize.Level1)
522 {
523 std::string exportFileName = g_testDir + "/" + STORE_ID + ".dump";
524 OS::FileHandle fd;
525 EXPECT_EQ(OS::OpenFile(exportFileName, fd), E_OK);
526 g_store->Dump(fd.handle);
527 OS::CloseFile(fd);
528 OS::RemoveDBDirectory(exportFileName);
529 }
530
531 /**
532 * @tc.name: StorageEngineTest003
533 * @tc.desc: Test the accuracy of CacheRecordVersion
534 * @tc.type: FUNC
535 * @tc.require:
536 * @tc.author: bty
537 */
538 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest003, TestSize.Level1)
539 {
540 uint64_t curVersion = g_store->GetCacheRecordVersion();
541 g_store->IncreaseCacheRecordVersion();
542 EXPECT_EQ(g_store->GetCacheRecordVersion(), curVersion + 1);
543 EXPECT_EQ(g_store->GetAndIncreaseCacheRecordVersion(), curVersion + 1);
544 EXPECT_EQ(g_store->GetCacheRecordVersion(), curVersion + 2);
545
546 curVersion = 0;
547 std::unique_ptr<SQLiteSingleVerNaturalStore> store2 = std::make_unique<SQLiteSingleVerNaturalStore>();
548 ASSERT_NE(store2, nullptr);
549 EXPECT_EQ(store2->GetCacheRecordVersion(), curVersion);
550 store2->IncreaseCacheRecordVersion();
551 EXPECT_EQ(store2->GetCacheRecordVersion(), curVersion);
552 }
553
554 /**
555 * @tc.name: StorageEngineTest004
556 * @tc.desc: Modify parameter initialization StorageEngine
557 * @tc.type: FUNC
558 * @tc.require:
559 * @tc.author: bty
560 */
561 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest004, TestSize.Level1)
562 {
563 /**
564 * @tc.steps: step1. Get storageEngine
565 * @tc.expected: step1. Expect return E_OK
566 */
567 int errCode = E_OK;
568 SQLiteSingleVerStorageEngine *storageEngine =
569 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
570 ASSERT_EQ(errCode, E_OK);
571 ASSERT_NE(storageEngine, nullptr);
572
573 /**
574 * @tc.steps: step2. Set the wrong min write num
575 * @tc.expected: step2. Expect -E_INVALID_ARGS
576 */
577 StorageEngineAttr poolSize = {17, 1, 1, 1}; // 17 means the maximum value is exceeded, 1 is the normal value
578 OpenDbProperties option = storageEngine->GetOpenOption();
579 EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), -E_INVALID_ARGS);
580
581 /**
582 * @tc.steps: step3. Set the correct min write num
583 * @tc.expected: step3. Expect E_OK
584 */
585 poolSize.minWriteNum = 1;
586 EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
587
588 /**
589 * @tc.steps: step4. Change the Engine State
590 * @tc.expected: step4. Expect E_OK
591 */
592 EngineState engineState = {CACHEDB};
593 storageEngine->SetEngineState(engineState);
594 EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
595
596 storageEngine->Release();
597 }
598
599 /**
600 * @tc.name: StorageEngineTest005
601 * @tc.desc: Set adapter to check engine
602 * @tc.type: FUNC
603 * @tc.require:
604 * @tc.author: bty
605 */
606 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest005, TestSize.Level1)
607 {
608 std::shared_ptr<ProcessSystemApiAdapterImpl> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
609 EXPECT_TRUE(adapter != nullptr);
610 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
611 int errCode = E_OK;
612 SQLiteSingleVerStorageEngine *storageEngine =
613 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
614 ASSERT_NE(storageEngine, nullptr);
615 EXPECT_EQ(storageEngine->CheckEngineOption(g_property), E_OK);
616 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
617 }
618
619 /**
620 * @tc.name: StorageEngineTest006
621 * @tc.desc: Check the engine Option after setting the schema
622 * @tc.type: FUNC
623 * @tc.require:
624 * @tc.author: bty
625 */
626 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest006, TestSize.Level1)
627 {
628 /**
629 * @tc.steps: step1. First parse the schema, then set the KvDBProperties
630 * @tc.expected: step1. Expect return -E_SCHEMA_MISMATCH
631 */
632 int errCode = E_OK;
633 SQLiteSingleVerStorageEngine *storageEngine =
634 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
635 ASSERT_NE(storageEngine, nullptr);
636 KvDBProperties prop;
637 prop.SetStringProp(KvDBProperties::DATA_DIR, g_property.GetStringProp(KvDBProperties::DATA_DIR, ""));
638 prop.SetStringProp(KvDBProperties::STORE_ID, g_property.GetStringProp(KvDBProperties::STORE_ID, ""));
639 prop.SetIntProp(KvDBProperties::DATABASE_TYPE, g_property.GetIntProp(KvDBProperties::DATABASE_TYPE, 0));
640 string schemaString =
641 "{\"SCHEMA_VERSION\":\"1.0\","
642 "\"SCHEMA_MODE\":\"STRICT\","
643 "\"SCHEMA_DEFINE\":{"
644 "\"field_name1\":\"BOOL\"},"
645 "\"SCHEMA_INDEXES\":[\"$.field_name1\"]}";
646 SchemaObject schema;
647 schema.ParseFromSchemaString(schemaString);
648 prop.SetSchema(schema);
649 EXPECT_EQ(storageEngine->CheckEngineOption(prop), -E_SCHEMA_MISMATCH);
650
651 /**
652 * @tc.steps: step2. Set the error Schema for the option
653 * @tc.expected: step2. Expect return -E_SCHEMA_MISMATCH
654 */
655 OpenDbProperties option = storageEngine->GetOpenOption();
656 option.schema = "errorSchema";
657 StorageEngineAttr poolSize = {1, 1, 1, 1}; // 1 is the valid size
658 EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
659 EXPECT_EQ(storageEngine->CheckEngineOption(prop), -E_SCHEMA_MISMATCH);
660
661 /**
662 * @tc.steps: step3. Set the correct Schema for the option
663 * @tc.expected: step3. Expect return E_OK
664 */
665 option.schema = schemaString;
666 EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
667 EXPECT_EQ(storageEngine->CheckEngineOption(prop), E_OK);
668
669 storageEngine->Release();
670 }
671
672 /**
673 * @tc.name: StorageEngineTest007
674 * @tc.desc: Export and import after changing engine state
675 * @tc.type: FUNC
676 * @tc.require:
677 * @tc.author: bty
678 */
679 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest007, TestSize.Level1)
680 {
681 /**
682 * @tc.steps: step1. Get storage engine
683 * @tc.expected: step1. Expect E_OK
684 */
685 int errCode = E_OK;
686 SQLiteSingleVerStorageEngine *storageEngine =
687 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
688 ASSERT_EQ(errCode, E_OK);
689 ASSERT_NE(storageEngine, nullptr);
690
691 /**
692 * @tc.steps: step2. Change engine state to cache db
693 * @tc.expected: step2. Expect -E_NOT_SUPPORT
694 */
695 storageEngine->SetEngineState(CACHEDB);
696 CipherPassword passwd;
697 string filePath = g_testDir + "/store_export.db";
698 EXPECT_EQ(g_store->Export(filePath, passwd), -E_NOT_SUPPORT);
699 EXPECT_EQ(g_store->Import(filePath, passwd), -E_NOT_SUPPORT);
700 EXPECT_EQ(g_store->Rekey(passwd), -E_NOT_SUPPORT);
701
702 /**
703 * @tc.steps: step3. Change engine state to INVALID
704 * @tc.expected: step3. Expect -E_BUSY
705 */
706 storageEngine->SetEngineState(INVALID);
707 EXPECT_EQ(g_store->Export(filePath, passwd), -E_BUSY);
708 EXPECT_EQ(g_store->Import(filePath, passwd), -E_BUSY);
709 EXPECT_EQ(g_store->Rekey(passwd), -E_BUSY);
710 storageEngine->Release();
711 }
712
713 /**
714 * @tc.name: StorageEngineTest008
715 * @tc.desc: add and remove sub after changing engine state
716 * @tc.type: FUNC
717 * @tc.require:
718 * @tc.author: bty
719 */
720 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest008, TestSize.Level1)
721 {
722 /**
723 * @tc.steps: step1. Get storage engine
724 * @tc.expected: step1. Expect E_OK
725 */
726 int errCode = E_OK;
727 SQLiteSingleVerStorageEngine *storageEngine =
728 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
729 ASSERT_EQ(errCode, E_OK);
730 ASSERT_NE(storageEngine, nullptr);
731
732 /**
733 * @tc.steps: step2. Change engine state to cache db
734 * @tc.expected: step2. Expect E_OK
735 */
736 storageEngine->SetEngineState(CACHEDB);
737 Query query = Query::Select();
738 QueryObject queryObj(query);
739 std::string sub = "123";
740 EXPECT_EQ(g_store->AddSubscribe(sub, queryObj, true), E_OK);
741
742 /**
743 * @tc.steps: step3. Remove subscribe from cache db
744 * @tc.expected: step3. Expect -1
745 */
746 std::vector<std::string> subs;
747 subs.push_back(sub);
748 EXPECT_EQ(g_store->RemoveSubscribe(subs), -1);
749 storageEngine->Release();
750 }