• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *>(&param);
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 *>(&param);
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 *>(&param);
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 *>(&param);
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 *>(&param);
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 *>(&param);
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 *>(&param);
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 *>(&param);
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      * @tc.steps: step6. Test remove device data
476      * @tc.expected: step6. Expect return -E_INVALID_ARGS
477      */
478     std::string deviceName;
479     EXPECT_EQ(errStore->RemoveDeviceData(deviceName, false), -E_INVALID_ARGS);
480 }
481 
482 /**
483   * @tc.name: ErrDbTest005
484   * @tc.desc: Test sync data when the database is not opened
485   * @tc.type: FUNC
486   * @tc.require:
487   * @tc.author: bty
488   */
489 HWTEST_F(DistributedDBDeviceIdentifierTest, ErrDbTest005, TestSize.Level1)
490 {
491     std::unique_ptr<SQLiteSingleVerNaturalStore> errStore = std::make_unique<SQLiteSingleVerNaturalStore>();
492     ASSERT_NE(errStore, nullptr);
493     ContinueToken token = nullptr;
494     std::vector<DataItem> dataItems;
495     DataSizeSpecInfo info = {DBConstant::MAX_SYNC_BLOCK_SIZE + 1, 0};
496     EXPECT_EQ(errStore->GetSyncDataNext(dataItems, token, info), -E_INVALID_ARGS);
497     info.blockSize = 0;
498     EXPECT_EQ(errStore->GetSyncDataNext(dataItems, token, info), -E_INVALID_ARGS);
499     errStore->ReleaseContinueToken(token);
500 }
501 
502 /**
503   * @tc.name: StorageEngineTest001
504   * @tc.desc: Test cache db remove data
505   * @tc.type: FUNC
506   * @tc.require:
507   * @tc.author: bty
508   */
509 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest001, TestSize.Level1)
510 {
511     int errCode = E_OK;
512     SQLiteSingleVerStorageEngine *storageEngine =
513         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
514     ASSERT_EQ(errCode, E_OK);
515     ASSERT_NE(storageEngine, nullptr);
516     storageEngine->SetEngineState(CACHEDB);
517     EXPECT_EQ(g_store->RemoveDeviceData("device1", false), -1);
518     storageEngine->Release();
519 }
520 
521 /**
522   * @tc.name: StorageEngineTest002
523   * @tc.desc: Test the interface of Dump
524   * @tc.type: FUNC
525   * @tc.require:
526   * @tc.author: bty
527   */
528 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest002, TestSize.Level1)
529 {
530     std::string exportFileName = g_testDir + "/" + STORE_ID + ".dump";
531     OS::FileHandle fd;
532     EXPECT_EQ(OS::OpenFile(exportFileName, fd), E_OK);
533     g_store->Dump(fd.handle);
534     OS::CloseFile(fd);
535     OS::RemoveDBDirectory(exportFileName);
536 }
537 
538 /**
539   * @tc.name: StorageEngineTest003
540   * @tc.desc: Test the accuracy of CacheRecordVersion
541   * @tc.type: FUNC
542   * @tc.require:
543   * @tc.author: bty
544   */
545 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest003, TestSize.Level1)
546 {
547     uint64_t curVersion = g_store->GetCacheRecordVersion();
548     g_store->IncreaseCacheRecordVersion();
549     EXPECT_EQ(g_store->GetCacheRecordVersion(), curVersion + 1);
550     EXPECT_EQ(g_store->GetAndIncreaseCacheRecordVersion(), curVersion + 1);
551     EXPECT_EQ(g_store->GetCacheRecordVersion(), curVersion + 2);
552 
553     curVersion = 0;
554     std::unique_ptr<SQLiteSingleVerNaturalStore> store2 = std::make_unique<SQLiteSingleVerNaturalStore>();
555     ASSERT_NE(store2, nullptr);
556     EXPECT_EQ(store2->GetCacheRecordVersion(), curVersion);
557     store2->IncreaseCacheRecordVersion();
558     EXPECT_EQ(store2->GetCacheRecordVersion(), curVersion);
559 }
560 
561 /**
562   * @tc.name: StorageEngineTest004
563   * @tc.desc: Modify parameter initialization StorageEngine
564   * @tc.type: FUNC
565   * @tc.require:
566   * @tc.author: bty
567   */
568 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest004, TestSize.Level1)
569 {
570     /**
571      * @tc.steps: step1. Get storageEngine
572      * @tc.expected: step1. Expect return E_OK
573      */
574     int errCode = E_OK;
575     SQLiteSingleVerStorageEngine *storageEngine =
576         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
577     ASSERT_EQ(errCode, E_OK);
578     ASSERT_NE(storageEngine, nullptr);
579 
580     /**
581      * @tc.steps: step2. Set the wrong min write num
582      * @tc.expected: step2. Expect -E_INVALID_ARGS
583      */
584     StorageEngineAttr poolSize = {17, 1, 1, 1};  // 17 means the maximum value is exceeded, 1 is the normal value
585     OpenDbProperties option = storageEngine->GetOpenOption();
586     EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), -E_INVALID_ARGS);
587 
588     /**
589      * @tc.steps: step3. Set the correct min write num
590      * @tc.expected: step3. Expect E_OK
591      */
592     poolSize.minWriteNum = 1;
593     EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
594 
595     /**
596      * @tc.steps: step4. Change the Engine State
597      * @tc.expected: step4. Expect E_OK
598      */
599     EngineState engineState = {CACHEDB};
600     storageEngine->SetEngineState(engineState);
601     EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
602 
603     storageEngine->Release();
604 }
605 
606 /**
607   * @tc.name: StorageEngineTest005
608   * @tc.desc: Set adapter to check engine
609   * @tc.type: FUNC
610   * @tc.require:
611   * @tc.author: bty
612   */
613 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest005, TestSize.Level1)
614 {
615     std::shared_ptr<ProcessSystemApiAdapterImpl> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
616     EXPECT_TRUE(adapter != nullptr);
617     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
618     int errCode = E_OK;
619     SQLiteSingleVerStorageEngine *storageEngine =
620         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
621     ASSERT_NE(storageEngine, nullptr);
622     EXPECT_EQ(storageEngine->CheckEngineOption(g_property), E_OK);
623     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
624 }
625 
626 /**
627   * @tc.name: StorageEngineTest006
628   * @tc.desc: Check the engine Option after setting the schema
629   * @tc.type: FUNC
630   * @tc.require:
631   * @tc.author: bty
632   */
633 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest006, TestSize.Level1)
634 {
635     /**
636      * @tc.steps: step1. First parse the schema, then set the KvDBProperties
637      * @tc.expected: step1. Expect return -E_SCHEMA_MISMATCH
638      */
639     int errCode = E_OK;
640     SQLiteSingleVerStorageEngine *storageEngine =
641         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
642     ASSERT_NE(storageEngine, nullptr);
643     KvDBProperties prop;
644     prop.SetStringProp(KvDBProperties::DATA_DIR, g_property.GetStringProp(KvDBProperties::DATA_DIR, ""));
645     prop.SetStringProp(KvDBProperties::STORE_ID, g_property.GetStringProp(KvDBProperties::STORE_ID, ""));
646     prop.SetIntProp(KvDBProperties::DATABASE_TYPE, g_property.GetIntProp(KvDBProperties::DATABASE_TYPE, 0));
647     string schemaString =
648         "{\"SCHEMA_VERSION\":\"1.0\","
649         "\"SCHEMA_MODE\":\"STRICT\","
650         "\"SCHEMA_DEFINE\":{"
651         "\"field_name1\":\"BOOL\"},"
652         "\"SCHEMA_INDEXES\":[\"$.field_name1\"]}";
653     SchemaObject schema;
654     schema.ParseFromSchemaString(schemaString);
655     prop.SetSchema(schema);
656     EXPECT_EQ(storageEngine->CheckEngineOption(prop), -E_SCHEMA_MISMATCH);
657 
658     /**
659      * @tc.steps: step2. Set the error Schema for the option
660      * @tc.expected: step2. Expect return -E_SCHEMA_MISMATCH
661      */
662     OpenDbProperties option = storageEngine->GetOpenOption();
663     option.schema = "errorSchema";
664     StorageEngineAttr poolSize = {1, 1, 1, 1};  // 1 is the valid size
665     EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
666     EXPECT_EQ(storageEngine->CheckEngineOption(prop), -E_SCHEMA_MISMATCH);
667 
668     /**
669      * @tc.steps: step3. Set the correct Schema for the option
670      * @tc.expected: step3. Expect return E_OK
671      */
672     option.schema = schemaString;
673     EXPECT_EQ(storageEngine->InitSQLiteStorageEngine(poolSize, option), E_OK);
674     EXPECT_EQ(storageEngine->CheckEngineOption(prop), E_OK);
675 
676     storageEngine->Release();
677 }
678 
679 /**
680   * @tc.name: StorageEngineTest007
681   * @tc.desc: Export and import after changing engine state
682   * @tc.type: FUNC
683   * @tc.require:
684   * @tc.author: bty
685   */
686 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest007, TestSize.Level1)
687 {
688     /**
689      * @tc.steps: step1. Get storage engine
690      * @tc.expected: step1. Expect E_OK
691      */
692     int errCode = E_OK;
693     SQLiteSingleVerStorageEngine *storageEngine =
694         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
695     ASSERT_EQ(errCode, E_OK);
696     ASSERT_NE(storageEngine, nullptr);
697 
698     /**
699      * @tc.steps: step2. Change engine state to cache db
700      * @tc.expected: step2. Expect -E_NOT_SUPPORT
701      */
702     storageEngine->SetEngineState(CACHEDB);
703     CipherPassword passwd;
704     string filePath = g_testDir + "/store_export.db";
705     EXPECT_EQ(g_store->Export(filePath, passwd), -E_NOT_SUPPORT);
706     EXPECT_EQ(g_store->Import(filePath, passwd), -E_NOT_SUPPORT);
707     EXPECT_EQ(g_store->Rekey(passwd), -E_NOT_SUPPORT);
708 
709     /**
710      * @tc.steps: step3. Change engine state to INVALID
711      * @tc.expected: step3. Expect -E_BUSY
712      */
713     storageEngine->SetEngineState(INVALID);
714     EXPECT_EQ(g_store->Export(filePath, passwd), -E_BUSY);
715     EXPECT_EQ(g_store->Import(filePath, passwd), -E_BUSY);
716     EXPECT_EQ(g_store->Rekey(passwd), -E_BUSY);
717     storageEngine->Release();
718 }
719 
720 /**
721   * @tc.name: StorageEngineTest008
722   * @tc.desc: add and remove sub after changing engine state
723   * @tc.type: FUNC
724   * @tc.require:
725   * @tc.author: bty
726   */
727 HWTEST_F(DistributedDBDeviceIdentifierTest, StorageEngineTest008, TestSize.Level1)
728 {
729     /**
730      * @tc.steps: step1. Get storage engine
731      * @tc.expected: step1. Expect E_OK
732      */
733     int errCode = E_OK;
734     SQLiteSingleVerStorageEngine *storageEngine =
735         static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
736     ASSERT_EQ(errCode, E_OK);
737     ASSERT_NE(storageEngine, nullptr);
738 
739     /**
740      * @tc.steps: step2. Change engine state to cache db
741      * @tc.expected: step2. Expect E_OK
742      */
743     storageEngine->SetEngineState(CACHEDB);
744     Query query = Query::Select();
745     QueryObject queryObj(query);
746     std::string sub = "123";
747     EXPECT_EQ(g_store->AddSubscribe(sub, queryObj, true), E_OK);
748 
749     /**
750      * @tc.steps: step3. Remove subscribe from cache db
751      * @tc.expected: step3. Expect -1
752      */
753     std::vector<std::string> subs;
754     subs.push_back(sub);
755     EXPECT_EQ(g_store->RemoveSubscribe(subs), -1);
756     storageEngine->Release();
757 }