• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Device Co., Ltd. 2024. All rights reserved.
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 #ifndef OMIT_ENCRYPT
16 #include <gtest/gtest.h>
17 #include <fcntl.h>
18 
19 #include "db_common.h"
20 #include "distributeddb_data_generate_unit_test.h"
21 #include "platform_specific.h"
22 #include "process_communicator_test_stub.h"
23 #include "process_system_api_adapter_impl.h"
24 
25 using namespace std;
26 using namespace testing::ext;
27 using namespace DistributedDB;
28 using namespace DistributedDBUnitTest;
29 
30 namespace {
31     // define some variables to init a KvStoreDelegateManager object.
32     KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
33     string g_testDir;
34     KvStoreConfig g_config;
35     std::string g_exportFileDir;
36     std::vector<std::string> g_junkFilesList;
37 
38     // define the g_kvNbDelegateCallback, used to get some information when open a kv store.
39     DBStatus g_kvDelegateStatus = INVALID_ARGS;
40     KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
41     KvStoreNbDelegate *g_kvNbDelegatePtrWithoutPasswd = nullptr;
42 
43 #ifndef OMIT_MULTI_VER
44     KvStoreDelegate *g_kvDelegatePtr = nullptr;
45     KvStoreDelegate *g_kvDelegatePtrWithoutPasswd = nullptr;
46     // the type of g_kvDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
47     auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreDelegateCallback, placeholders::_1,
48         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr));
49     KvStoreDelegate::Option g_option;
50 #endif // OMIT_MULTI_VER
51 
52     const size_t MAX_PASSWD_SIZE = 128;
53     // define the g_valueCallback, used to query a value object data from the kvdb.
54     DBStatus g_valueStatus = INVALID_ARGS;
55     Value g_value;
56 
57     CipherPassword g_passwd1;
58     CipherPassword g_passwd2;
59     CipherPassword g_passwd3;
60     CipherPassword g_passwd4;
61     // the type of g_valueCallback is function<void(DBStatus, Value)>
62     auto g_valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
63         placeholders::_1, placeholders::_2, std::ref(g_valueStatus), std::ref(g_value));
64 
65     // the type of g_kvNbDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
66     auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
67         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
68 
RemoveJunkFile(const std::vector<std::string> & fileList)69     void RemoveJunkFile(const std::vector<std::string> &fileList)
70     {
71         for (auto &junkFile : fileList) {
72             std::ifstream file(junkFile);
73             if (file) {
74                 file.close();
75                 int result = remove(junkFile.c_str());
76                 if (result < 0) {
77                     LOGE("failed to delete the db file:%d", errno);
78                 }
79             }
80         }
81         return;
82     }
83 }
84 
85 class DistributedDBInterfacesImportAndExportRdTest : public testing::Test {
86 public:
87     static void SetUpTestCase(void);
88     static void TearDownTestCase(void);
89     void SetUp();
90     void TearDown();
91     int ModifyDataInPage(int modifyPos, char newVal, const char *modifyFile);
92 };
93 
SetUpTestCase(void)94 void DistributedDBInterfacesImportAndExportRdTest::SetUpTestCase(void)
95 {
96     g_mgr.SetProcessLabel("6666", "8888");
97     g_mgr.SetProcessCommunicator(std::make_shared<ProcessCommunicatorTestStub>());
98     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
99     g_config.dataDir = g_testDir;
100     g_mgr.SetKvStoreConfig(g_config);
101 
102     g_exportFileDir = g_testDir + "/ExportDir";
103     OS::MakeDBDirectory(g_exportFileDir);
104     vector<uint8_t> passwdBuffer1(5, 1);  // 5 and 1 as random password.
105     int errCode = g_passwd1.SetValue(passwdBuffer1.data(), passwdBuffer1.size());
106     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
107     vector<uint8_t> passwdBuffer2(5, 2);  // 5 and 2 as random password.
108     errCode = g_passwd2.SetValue(passwdBuffer2.data(), passwdBuffer2.size());
109     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
110     vector<uint8_t> passwdBuffer3(5, 3);  // 5 and 3 as random password.
111     errCode = g_passwd3.SetValue(passwdBuffer3.data(), passwdBuffer3.size());
112     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
113     vector<uint8_t> passwdBuffer4(5, 4);  // 5 and 4 as random password.
114     errCode = g_passwd4.SetValue(passwdBuffer4.data(), passwdBuffer4.size());
115     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
116 }
117 
TearDownTestCase(void)118 void DistributedDBInterfacesImportAndExportRdTest::TearDownTestCase(void)
119 {
120     OS::RemoveDBDirectory(g_exportFileDir);
121     if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
122         LOGE("rm test db files error!");
123     }
124     RuntimeContext::GetInstance()->StopTaskPool(); // wait for all thread exit
125 }
126 
SetUp(void)127 void DistributedDBInterfacesImportAndExportRdTest::SetUp(void)
128 {
129     DistributedDBToolsUnitTest::PrintTestCaseInfo();
130     g_junkFilesList.clear();
131     g_kvDelegateStatus = INVALID_ARGS;
132     g_kvNbDelegatePtr = nullptr;
133 #ifndef OMIT_MULTI_VER
134     g_kvDelegatePtr = nullptr;
135 #endif // OMIT_MULTI_VER
136 }
137 
TearDown(void)138 void DistributedDBInterfacesImportAndExportRdTest::TearDown(void)
139 {
140     RemoveJunkFile(g_junkFilesList);
141 }
142 
143 /**
144   * @tc.name: NormalExport001
145   * @tc.desc: The data of the current version of the board is exported and the package file is single.
146   * @tc.type: FUNC
147   * @tc.author: chenguoliang
148   */
149 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, NormalExport001, TestSize.Level1)
150 {
151     /**
152      * @tc.steps: step1. Pre-create folder dir
153      */
154     std::string singleExportFileName = g_exportFileDir + "/singleNormalExport001.$$";
155     std::string singleStoreId = "distributed_SingleNormalExport_001";
156     KvStoreNbDelegate::Option option = {true, false, false};
157     option.storageEngineType = DistributedDB::GAUSSDB_RD;
158     option.rdconfig.type = HASH;
159     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
160     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
161     EXPECT_TRUE(g_kvDelegateStatus == OK);
162 
163     /**
164      * @tc.steps: step2. Specify the path to export the non-encrypted board database.
165      * @tc.expected: step2. Returns OK
166      */
167     CipherPassword passwd;
168     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
169 #ifndef OMIT_MULTI_VER
170     std::string mulitExportFileName = g_exportFileDir + "/mulitNormalExport001.$$";
171     std::string multiStoreId = "distributed_MultiNormalExport_001";
172     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
173     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
174     EXPECT_TRUE(g_kvDelegateStatus == OK);
175 
176     /**
177      * @tc.steps: step3. Specify the path to export the multi-version non-encrypted database.
178      * @tc.expected: step3. Returns OK
179      */
180     EXPECT_EQ(g_kvDelegatePtr->Export(mulitExportFileName, passwd), OK);
181 
182     // clear resource
183     g_junkFilesList.push_back(mulitExportFileName);
184     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
185     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
186 #endif // OMIT_MULTI_VER
187     g_junkFilesList.push_back(singleExportFileName);
188     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
189     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
190 }
191 
192 /**
193   * @tc.name: ImportTxtFile001
194   * @tc.desc: Check Txt file type when import
195   * @tc.type: FUNC
196   * @tc.author: chenguoliang
197   */
198 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ImportTxtFile001, TestSize.Level1)
199 {
200     /**
201      * @tc.steps: step1. Pre-create folder dir
202      */
203     std::string singleExportFileName = g_exportFileDir + "/importTxtFile001.$$";
204     std::string singleStoreId = "import_TxtFile_001";
205     KvStoreNbDelegate::Option option = {true, false, false};
206     option.storageEngineType = DistributedDB::GAUSSDB_RD;
207     option.rdconfig.type = HASH;
208     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
209     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
210     EXPECT_TRUE(g_kvDelegateStatus == OK);
211 
212     /**
213      * @tc.steps: step2. Import a.txt in the right path.
214      * @tc.expected: step2. Returns INVALID_FILE
215      */
216     CipherPassword passwd;
217     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
218 
219     std::string filePath = g_exportFileDir + "/a.txt";
220     ofstream createFile(filePath);
221     EXPECT_EQ(g_kvNbDelegatePtr->Import(filePath, passwd), INVALID_FILE);
222 
223     if (createFile) {
224         createFile << '1' << endl;
225         createFile.close();
226     }
227     EXPECT_EQ(g_kvNbDelegatePtr->Import(filePath, passwd), INVALID_FILE);
228 
229     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
230     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
231 }
232 
233 /**
234   * @tc.name: ImportDamagedFile001
235   * @tc.desc: Test import damaged file.
236   * @tc.type: FUNC
237   * @tc.author: chenguoliang
238   */
ModifyDataInPage(int modifyPos,char newVal,const char * modifyFile)239 int DistributedDBInterfacesImportAndExportRdTest::ModifyDataInPage(int modifyPos, char newVal, const char *modifyFile)
240 {
241     FILE *fp = fopen(modifyFile, "rb+");
242     if (fp == nullptr) {
243         printf("Failed to open file");
244         return 1;
245     }
246     if (fseek(fp, modifyPos, SEEK_SET) != 0) {
247         printf("Failed to seek to position");
248         fclose(fp);
249         return 1;
250     }
251     size_t ret = fwrite(&newVal, sizeof(char), 1, fp);
252     if (ret != 1) {
253         printf("Failed to write file");
254         return 1;
255     }
256     if (fclose(fp) == EOF) {
257         printf("Failed to close file");
258         return 1;
259     }
260     return 0;
261 }
262 
263 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ImportDamagedFile001, TestSize.Level1)
264 {
265     /**
266      * @tc.steps: step1. Pre-create folder dir
267      */
268     std::string singleExportFileName = g_exportFileDir + "/importDamagedFile001.$$";
269     std::string singleStoreId = "import_DamagedFile_001";
270     KvStoreNbDelegate::Option option = {true, false, false};
271     option.storageEngineType = DistributedDB::GAUSSDB_RD;
272     option.rdconfig.type = HASH;
273     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
274     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
275     EXPECT_TRUE(g_kvDelegateStatus == OK);
276 
277     /**
278      * @tc.steps: step2. Specify the path to export the non-encrypted board database.
279      * @tc.expected: step2. Returns OK
280      */
281     CipherPassword passwd;
282     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
283 
284     /**
285      * @tc.steps: step3. Import damaged file.
286      * @tc.expected: step3. Returns INVALID_FILE
287      */
288     ModifyDataInPage(50086, '0', singleExportFileName.c_str());
289     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), INVALID_FILE);
290 
291     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
292     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
293 }
294 
295 /**
296   * @tc.name: ReadOnlyNotExport001
297   * @tc.desc: Export is not supported when option.rdconfig.type is true.
298   * @tc.type: FUNC
299   * @tc.author: chenguoliang
300   */
301 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ReadOnlyNotExport001, TestSize.Level1)
302 {
303     /**
304      * @tc.steps: step1. Pre-create folder dir
305      */
306     std::string singleExportFileName = g_exportFileDir + "/readOnlyNotExport001.$$";
307     std::string singleStoreId = "distributed_readOnlyNotExport_001";
308     KvStoreNbDelegate::Option option = {true, false, false};
309     option.storageEngineType = DistributedDB::GAUSSDB_RD;
310     option.rdconfig.type = HASH;
311     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
312     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
313     EXPECT_TRUE(g_kvDelegateStatus == OK);
314     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
315 
316     /**
317      * @tc.steps: step2. Specify the path to export the non-encrypted board database.
318      * @tc.expected: step2. Returns OK
319      */
320 
321     option.rdconfig.readOnly = true;
322     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
323     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
324     EXPECT_TRUE(g_kvDelegateStatus == OK);
325     CipherPassword passwd;
326     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), READ_ONLY);
327     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
328     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
329 }
330 
331 /**
332   * @tc.name: UndisturbedlSingleExport001
333   * @tc.desc: Check that the export action is an independent transaction.
334   * @tc.type: FUNC
335   * @tc.author: chenguoliang
336   */
337 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, UndisturbedlSingleExport001, TestSize.Level1)
338 {
339     std::string singleStoreId = "undistributed_SingleExport_001";
340     KvStoreNbDelegate::Option option = {true, false, false};
341     option.storageEngineType = DistributedDB::GAUSSDB_RD;
342     option.rdconfig.type = HASH;
343     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
344     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
345     EXPECT_TRUE(g_kvDelegateStatus == OK);
346 
347     /**
348      * @tc.steps: step1. Three known data records are preset in the board database.
349      */
350     g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
351     g_kvNbDelegatePtr->Put(KEY_2, VALUE_2);
352     g_kvNbDelegatePtr->Put(KEY_3, VALUE_3);
353 
354     /**
355      * @tc.steps: step2. Execute the export action.
356      */
357     std::string singleExportFileName = g_exportFileDir + "/UndisturbedlSingleExport001.$$";
358     CipherPassword passwd;
359     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
360 
361     /**
362      * @tc.steps: step3. Insert multiple new data records into the database.
363      */
364     g_kvNbDelegatePtr->Put(KEY_4, VALUE_4);
365     g_kvNbDelegatePtr->Put(KEY_5, VALUE_5);
366 
367     /**
368      * @tc.steps: step4.  Import backup data.
369      * @tc.expected: step4. Returns OK.
370      */
371     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
372 
373     /**
374      * @tc.steps: step5. Check whether the imported data is the preset content in step 1.
375      * @tc.expected: step5. Three preset data records are found.
376      */
377     Value readValue;
378     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, readValue), OK);
379     EXPECT_EQ(readValue, VALUE_1);
380     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_2, readValue), OK);
381     EXPECT_EQ(readValue, VALUE_2);
382     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_3, readValue), OK);
383     EXPECT_EQ(readValue, VALUE_3);
384 
385     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_4, readValue), NOT_FOUND);
386     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_5, readValue), NOT_FOUND);
387 
388     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
389     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
390     g_junkFilesList.push_back(singleExportFileName);
391 }
392 
393 #ifndef OMIT_MULTI_VER
GetSnapshotUnitTest(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)394 static void GetSnapshotUnitTest(KvStoreDelegate *&kvDelegatePtr, KvStoreSnapshotDelegate *&snapshotDelegatePtr)
395 {
396     DBStatus snapshotDelegateStatus = INVALID_ARGS;
397     auto snapshotDelegateCallback = bind(&DistributedDBToolsUnitTest::SnapshotDelegateCallback,
398         placeholders::_1, placeholders::_2, std::ref(snapshotDelegateStatus), std::ref(snapshotDelegatePtr));
399 
400     kvDelegatePtr->GetKvStoreSnapshot(nullptr, snapshotDelegateCallback);
401     EXPECT_TRUE(snapshotDelegateStatus == OK);
402     ASSERT_TRUE(snapshotDelegatePtr != nullptr);
403 }
404 
405 /**
406   * @tc.name: UndisturbedlMultiExport001
407   * @tc.desc: Check that the export action is an independent transaction.
408   * @tc.type: FUNC
409   * @tc.author: chenguoliang
410   */
411 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, UndisturbedlMultiExport001, TestSize.Level1)
412 {
413     std::string multiStoreId = "undistributed_MultiExport_001";
414     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
415     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
416     EXPECT_TRUE(g_kvDelegateStatus == OK);
417 
418     /**
419      * @tc.steps: step1. Three known data records are preset in the board database.
420      */
421     g_kvDelegatePtr->Put(KEY_1, VALUE_1);
422     g_kvDelegatePtr->Put(KEY_2, VALUE_2);
423     g_kvDelegatePtr->Put(KEY_3, VALUE_3);
424 
425     /**
426      * @tc.steps: step2. Execute the export action.
427      */
428     std::string mulitExportFileName = g_exportFileDir + "/UndisturbedlMultiExport001.$$";
429     CipherPassword passwd;
430     EXPECT_EQ(g_kvDelegatePtr->Export(mulitExportFileName, passwd), OK);
431 
432     /**
433      * @tc.steps: step3. Insert multiple new data records into the database.
434      */
435     g_kvDelegatePtr->Put(KEY_4, VALUE_4);
436     g_kvDelegatePtr->Put(KEY_5, VALUE_5);
437 
438     /**
439      * @tc.steps: step4.  Import backup data.
440      * @tc.expected: step4. Returns OK.
441      */
442     EXPECT_EQ(g_kvDelegatePtr->Import(mulitExportFileName, passwd), OK);
443 
444     KvStoreSnapshotDelegate *snapshotDelegatePtr = nullptr;
445     GetSnapshotUnitTest(g_kvDelegatePtr, snapshotDelegatePtr);
446     ASSERT_TRUE(snapshotDelegatePtr != nullptr);
447 
448     /**
449      * @tc.steps: step5. Check whether the imported data is the preset content in step 1.
450      * @tc.expected: step5. Three preset data records are found.
451      */
452     snapshotDelegatePtr->Get(KEY_1, g_valueCallback);
453     EXPECT_EQ(g_valueStatus, OK);
454     EXPECT_EQ(g_value, VALUE_1);
455     snapshotDelegatePtr->Get(KEY_2, g_valueCallback);
456     EXPECT_EQ(g_valueStatus, OK);
457     EXPECT_EQ(g_value, VALUE_2);
458     snapshotDelegatePtr->Get(KEY_3, g_valueCallback);
459     EXPECT_EQ(g_valueStatus, OK);
460     EXPECT_EQ(g_value, VALUE_3);
461 
462     snapshotDelegatePtr->Get(KEY_4, g_valueCallback);
463     EXPECT_EQ(g_valueStatus, NOT_FOUND);
464     snapshotDelegatePtr->Get(KEY_5, g_valueCallback);
465     EXPECT_EQ(g_valueStatus, NOT_FOUND);
466 
467     EXPECT_TRUE(g_kvDelegatePtr->ReleaseKvStoreSnapshot(snapshotDelegatePtr) == OK);
468     snapshotDelegatePtr = nullptr;
469 
470     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
471     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
472     g_junkFilesList.push_back(mulitExportFileName);
473 }
474 #endif // OMIT_MULTI_VER
475 
476 /**
477   * @tc.name: ExportParameterCheck001
478   * @tc.desc: Check the verification of abnormal interface parameters.
479   * @tc.type: FUNC
480   * @tc.author: chenguoliang
481   */
482 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ExportParameterCheck001, TestSize.Level1)
483 {
484     std::string singleStoreId = "distributed_ParameterCheck_001";
485     KvStoreNbDelegate::Option option = {true, false, false};
486     option.storageEngineType = DistributedDB::GAUSSDB_RD;
487     option.rdconfig.type = HASH;
488     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
489     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
490     EXPECT_TRUE(g_kvDelegateStatus == OK);
491 
492     g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
493 
494     /**
495      * @tc.steps: step1. The filePath path does not exist.
496      * @tc.expected: step1. Return INVALID_ARGS.
497      */
498     std::string invalidFileName = g_exportFileDir + "/tempNotCreated/" + "/ExportParameterCheck001.$$";
499     CipherPassword passwd;
500     EXPECT_EQ(g_kvNbDelegatePtr->Export(invalidFileName, passwd), INVALID_ARGS);
501 
502     /**
503      * @tc.steps: step2. Password length MAX_PASSWD_SIZE + 1
504      * @tc.expected: step2. Return INVALID_ARGS.
505      */
506     vector<uint8_t> passwdBuffer(MAX_PASSWD_SIZE + 1, MAX_PASSWD_SIZE);
507     int errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
508     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OVERSIZE);
509     /**
510      * @tc.steps: step3. Password length MAX_PASSWD_SIZE
511      * @tc.expected: step3. Return OK.
512      */
513     passwdBuffer.resize(MAX_PASSWD_SIZE, MAX_PASSWD_SIZE);
514     errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
515     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
516     std::string singleExportFileName = g_exportFileDir + "/ExportParameterCheck001.$$";
517     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), NOT_SUPPORT);
518 
519     /**
520      * @tc.steps: step5. Use the password to import the file again,
521      * @tc.expected: step5. Return OK.
522      */
523     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
524     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
525 }
526 
527 #ifndef OMIT_MULTI_VER
528 /**
529   * @tc.name: ExportParameterCheck002
530   * @tc.desc: Check the verification of abnormal interface parameters.
531   * @tc.type: FUNC
532   * @tc.author: chenguoliang
533   */
534 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ExportParameterCheck002, TestSize.Level1)
535 {
536     std::string multiStoreId = "distributed_ParameterCheck_002";
537     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
538     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
539     EXPECT_TRUE(g_kvDelegateStatus == OK);
540 
541     g_kvDelegatePtr->Put(KEY_1, VALUE_1);
542 
543     /**
544      * @tc.steps: step1. The filePath path does not exist.
545      * @tc.expected: step1. Return INVALID_ARGS.
546      */
547     std::string invalidExportFileName = g_exportFileDir + "/tempNotCreated/" + "/ExportParameterCheck002.$$";
548     CipherPassword passwd;
549     EXPECT_EQ(g_kvDelegatePtr->Export(invalidExportFileName, passwd), INVALID_ARGS);
550 
551     /**
552      * @tc.steps: step2. Password length MAX_PASSWD_SIZE + 1
553      * @tc.expected: step2. Return INVALID_ARGS.
554      */
555     vector<uint8_t> passwdBuffer(MAX_PASSWD_SIZE + 1, MAX_PASSWD_SIZE);
556     int errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
557     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OVERSIZE);
558     /**
559      * @tc.steps: step3. Password length MAX_PASSWD_SIZE
560      * @tc.expected: step3. Return OK.
561      */
562     passwdBuffer.resize(MAX_PASSWD_SIZE, MAX_PASSWD_SIZE);
563     errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
564     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
565     std::string multiExportFileName = g_exportFileDir + "/ExportParameterCheck002.$$";
566     EXPECT_EQ(g_kvDelegatePtr->Export(multiExportFileName, passwd), NOT_SUPPORT);
567 
568     /**
569      * @tc.steps: step4. Use the password to import the file again,
570      * @tc.expected: step4. Return OK.
571      */
572     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
573     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
574 }
575 #endif // OMIT_MULTI_VER
576 
577 /**
578   * @tc.name: ReadOnlyNotImport001
579   * @tc.desc: Import is not supported when option.rdconfig.readOnly is true.
580   * @tc.type: FUNC
581   * @tc.author: chenguoliang
582   */
583 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ReadOnlyNotImport001, TestSize.Level1)
584 {
585     /**
586      * @tc.steps: step1. Pre-create folder dir
587      */
588     std::string singleExportFileName = g_exportFileDir + "/readOnlyNotImport001.$$";
589     std::string singleStoreId = "distributed_readOnlyNotImport_001";
590     KvStoreNbDelegate::Option option = {true, false, false};
591     option.storageEngineType = DistributedDB::GAUSSDB_RD;
592     option.rdconfig.type = HASH;
593     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
594     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
595     EXPECT_TRUE(g_kvDelegateStatus == OK);
596 
597     /**
598      * @tc.steps: step2. Import an authorized path with an incorrect password.
599      * @tc.expected: step2. Return INVALID_FILE.
600      */
601     CipherPassword passwd;
602     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
603     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
604 
605     /**
606      * @tc.steps: step3. Import a permission path without a password.
607      * @tc.expected: step3. Return OK.
608      */
609     option.rdconfig.readOnly = true;
610     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
611     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
612     EXPECT_TRUE(g_kvDelegateStatus == OK);
613     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), READ_ONLY);
614 
615     // clear resource
616     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
617     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
618 }
619 
620 /**
621   * @tc.name: NormalImport001
622   * @tc.desc: Normal import capability for single version, parameter verification capability
623   * @tc.type: FUNC
624   * @tc.author: chenguoliang
625   */
626 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, NormalImport001, TestSize.Level1)
627 {
628     std::string singleExportFileName = g_exportFileDir + "/NormalImport001.$$";
629     std::string singleStoreId = "distributed_Importmulti_001";
630     KvStoreNbDelegate::Option option = {true, false, false};
631     option.storageEngineType = DistributedDB::GAUSSDB_RD;
632     option.rdconfig.type = HASH;
633     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
634     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
635     EXPECT_TRUE(g_kvDelegateStatus == OK);
636     g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
637 
638     CipherPassword passwd;
639     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
640 
641     /**
642      * @tc.steps: step1. Import the invalid path.
643      * @tc.expected: step1. Return INVALID_ARGS.
644      */
645     std::string invalidPath = g_exportFileDir + "sdad" + "/NormalImport001.$$";
646     EXPECT_EQ(g_kvNbDelegatePtr->Import(invalidPath, passwd), INVALID_ARGS);
647 
648     /**
649      * @tc.steps: step2. Import an authorized path with an incorrect password.
650      * @tc.expected: step2. Return INVALID_FILE.
651      */
652     vector<uint8_t> passwdBuffer(MAX_PASSWD_SIZE, MAX_PASSWD_SIZE);
653     int errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
654     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
655     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), NOT_SUPPORT);
656 
657     /**
658      * @tc.steps: step3. Import a permission path without a password.
659      * @tc.expected: step3. Return OK.
660      */
661     errCode = passwd.Clear();
662     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
663     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
664 
665     /**
666      * @tc.steps: step4. Check whether the data is the same as the backup database.
667      * @tc.expected: step4. Same database data.
668      */
669     Value readValue;
670     g_kvNbDelegatePtr->Get(KEY_1, readValue);
671     EXPECT_EQ(readValue, VALUE_1);
672 
673     // clear resource
674     g_junkFilesList.push_back(singleExportFileName);
675     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
676     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
677 }
678 
679 #ifndef OMIT_MULTI_VER
680 /**
681   * @tc.name: NormalImport002
682   * @tc.desc: Normal import capability for multi version, parameter verification capability
683   * @tc.type: FUNC
684   * @tc.author: chenguoliang
685   */
686 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, NormalImport002, TestSize.Level1)
687 {
688     std::string multiExportFileName = g_exportFileDir + "/NormalImport002.$$";
689     std::string multiStoreId = "distributed_ImportSingle_002";
690     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
691     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
692     EXPECT_TRUE(g_kvDelegateStatus == OK);
693     g_kvDelegatePtr->Put(KEY_1, VALUE_1);
694 
695     CipherPassword passwd;
696     EXPECT_EQ(g_kvDelegatePtr->Export(multiExportFileName, passwd), OK);
697 
698     /**
699      * @tc.steps: step1. Import the invalid path.
700      * @tc.expected: step1. Return INVALID_ARGS.
701      */
702     std::string invalidPath = g_exportFileDir + "sdad" + "/NormalImport002.$$";
703     EXPECT_EQ(g_kvDelegatePtr->Import(invalidPath, passwd), INVALID_ARGS);
704 
705     /**
706      * @tc.steps: step2. Import an authorized path with an incorrect password.
707      * @tc.expected: step2. Return INVALID_FILE.
708      */
709     vector<uint8_t> passwdBuffer(MAX_PASSWD_SIZE, MAX_PASSWD_SIZE);
710     int errCode = passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
711     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
712     EXPECT_EQ(g_kvDelegatePtr->Import(multiExportFileName, passwd), INVALID_FILE);
713 
714     g_kvDelegatePtr->Delete(KEY_1);
715     /**
716      * @tc.steps: step3. Import a permission path without a password.
717      * @tc.expected: step3. Return OK.
718      */
719     errCode = passwd.Clear();
720     ASSERT_EQ(errCode, CipherPassword::ErrorCode::OK);
721     EXPECT_EQ(g_kvDelegatePtr->Import(multiExportFileName, passwd), OK);
722 
723     KvStoreSnapshotDelegate *snapshotDelegatePtr = nullptr;
724     GetSnapshotUnitTest(g_kvDelegatePtr, snapshotDelegatePtr);
725     ASSERT_TRUE(snapshotDelegatePtr != nullptr);
726 
727     /**
728      * @tc.steps: step4. Check whether the data is the same as the backup database.
729      * @tc.expected: step4. Same database data.
730      */
731     snapshotDelegatePtr->Get(KEY_1, g_valueCallback);
732     EXPECT_EQ(g_valueStatus, OK);
733     EXPECT_EQ(g_value, VALUE_1);
734 
735     EXPECT_TRUE(g_kvDelegatePtr->ReleaseKvStoreSnapshot(snapshotDelegatePtr) == OK);
736     snapshotDelegatePtr = nullptr;
737 
738     // clear resource
739     g_junkFilesList.push_back(multiExportFileName);
740     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
741     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
742 }
743 #endif // OMIT_MULTI_VER
744 
745 /**
746   * @tc.name: ExceptionFileImport001
747   * @tc.desc: Normal import capability for single version, parameter verification capability
748   * @tc.type: FUNC
749   * @tc.author: chenguoliang
750   */
751 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ExceptionFileImport001, TestSize.Level1)
752 {
753     std::string singleExportFileName = g_exportFileDir + "/ExceptionFileImport001.$$";
754     std::string singleStoreId = "distributed_ImportExceptionsigle_001";
755     KvStoreNbDelegate::Option option = {true, false, false};
756     option.storageEngineType = DistributedDB::GAUSSDB_RD;
757     option.rdconfig.type = HASH;
758     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
759     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
760     EXPECT_TRUE(g_kvDelegateStatus == OK);
761     g_kvNbDelegatePtr->Put(KEY_2, VALUE_2);
762 
763     CipherPassword passwd;
764     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
765 
766     /**
767      * @tc.steps: step1. Repeat import backup file to same database.
768      * @tc.expected: step1. Return OK.
769      */
770     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
771     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
772 
773     /**
774      * @tc.steps: step2. Change the name of file1 to file2.
775      */
776     std::string newSingleExportFileName = g_exportFileDir + "/newExceptionFileImport001.$$";
777     EXPECT_EQ(rename(singleExportFileName.c_str(), newSingleExportFileName.c_str()), 0);
778 
779     /**
780      * @tc.steps: step3. Import file1 into the database.
781      * @tc.expected: step3. Return INVALID_FILE.
782      */
783     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), INVALID_FILE);
784 
785     /**
786      * @tc.steps: step4. Import file2 into the database.
787      * @tc.expected: step4. Return INVALID_FILE.
788      */
789     EXPECT_EQ(g_kvNbDelegatePtr->Import(newSingleExportFileName, passwd), OK);
790 
791     // clear resource
792     g_junkFilesList.push_back(singleExportFileName);
793     g_junkFilesList.push_back(newSingleExportFileName);
794     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
795     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
796 }
797 
798 #ifndef OMIT_MULTI_VER
799 /**
800   * @tc.name: ExceptionFileImport002
801   * @tc.desc: Normal import capability for multi version, parameter verification capability
802   * @tc.type: FUNC
803   * @tc.author: chenguoliang
804   */
805 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ExceptionFileImport002, TestSize.Level1)
806 {
807     std::string multiExportFileName = g_exportFileDir + "/ExceptionFileImport002.$$";
808     std::string multiStoreId = "distributed_ImportExceptionMulti_001";
809     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
810     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
811     EXPECT_TRUE(g_kvDelegateStatus == OK);
812     g_kvDelegatePtr->Put(KEY_1, VALUE_1);
813 
814     CipherPassword passwd;
815     EXPECT_EQ(g_kvDelegatePtr->Export(multiExportFileName, passwd), OK);
816 
817     /**
818      * @tc.steps: step1. Import the backup file that has been tampered with to the multi-version database.
819      * @tc.expected: step1. Return INVALID_FILE.
820      */
821     EXPECT_EQ(DistributedDBToolsUnitTest::ModifyDatabaseFile(multiExportFileName), 0);
822     EXPECT_EQ(g_kvDelegatePtr->Import(multiExportFileName, passwd), INVALID_FILE);
823 
824     // clear resource
825     g_junkFilesList.push_back(multiExportFileName);
826     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
827     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
828 }
829 
830 /**
831   * @tc.name: ExceptionFileImport003
832   * @tc.desc: The data of the current version of the board is exported and the package file is single.
833   * @tc.type: FUNC
834   * @tc.author: chenguoliang
835   */
836 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ExceptionFileImport003, TestSize.Level1)
837 {
838     std::string singleExportFileName = g_exportFileDir + "/singleExceptionFileImport003.$$";
839     std::string singleStoreId = "distributed_ExportSingle_001";
840     KvStoreNbDelegate::Option option = {true, false, false};
841     option.storageEngineType = DistributedDB::GAUSSDB_RD;
842     option.rdconfig.type = HASH;
843     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
844     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
845     EXPECT_TRUE(g_kvDelegateStatus == OK);
846 
847     CipherPassword passwd;
848     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
849 
850     std::string mulitExportFileName = g_exportFileDir + "/mulitExceptionFileImport003.$$";
851     std::string multiStoreId = "distributed_ExportMulit_001";
852     g_mgr.GetKvStore(multiStoreId, g_option, g_kvDelegateCallback);
853     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
854     EXPECT_TRUE(g_kvDelegateStatus == OK);
855     EXPECT_EQ(g_kvDelegatePtr->Export(mulitExportFileName, passwd), OK);
856 
857     /**
858      * @tc.steps: step1. Use the single ver import interface. The file path is a multi-version backup file.
859      * @tc.expected: step1. Return INVALID_FILE.
860      */
861     EXPECT_EQ(g_kvNbDelegatePtr->Import(mulitExportFileName, passwd), INVALID_FILE);
862 
863     /**
864      * @tc.steps: step2.  Use the single ver import interface. The file path is a single-version backup file.
865      * @tc.expected: step2. Return OK.
866      */
867     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
868 
869     /**
870      * @tc.steps: step3.  Use the multi-version import interface. The file path is a single-version backup file.
871      * @tc.expected: step3. Return INVALID_FILE.
872      */
873     EXPECT_EQ(g_kvDelegatePtr->Import(singleExportFileName, passwd), INVALID_FILE);
874 
875     /**
876      * @tc.steps: step4.  Use the multi-version import interface. The file path is a multi-version backup file.
877      * @tc.expected: step4. Return INVALID_FILE.
878      */
879     EXPECT_EQ(g_kvDelegatePtr->Import(mulitExportFileName, passwd), OK);
880 
881     // clear resource
882     g_junkFilesList.push_back(singleExportFileName);
883     g_junkFilesList.push_back(mulitExportFileName);
884     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
885     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
886     EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
887     EXPECT_EQ(g_mgr.DeleteKvStore(multiStoreId), OK);
888 }
889 #endif // OMIT_MULTI_VER
890 
TryDbForPasswordIndependence001()891 static void TryDbForPasswordIndependence001()
892 {
893     std::string singleStoreIdNoPasswd = "distributed_DbForPasswordIndependence_001";
894     std::string singleStoreId = "distributed_DbForPasswordIndependence_001";
895 
896     /**
897      * @tc.steps: step4. Run the p3 command to open the database db1.
898      * @tc.expected: step4. Return ERROR.
899      */
900     KvStoreNbDelegate::Option option = {true, false, true, CipherType::DEFAULT, g_passwd3};
901     option.storageEngineType = DistributedDB::GAUSSDB_RD;
902     option.rdconfig.type = HASH;
903     g_mgr.GetKvStore(singleStoreIdNoPasswd, option, g_kvNbDelegateCallback);
904     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
905     EXPECT_NE(g_kvDelegateStatus, OK);
906 
907     /**
908      * @tc.steps: step5. Run the p4 command to open the database db2.
909      * @tc.expected: step5. Return ERROR.
910      */
911     option = {true, false, true, CipherType::DEFAULT, g_passwd4};
912     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
913     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
914     ASSERT_TRUE(g_kvDelegateStatus != OK);
915 
916     /**
917      * @tc.steps: step6. Open the db1 directly.
918      * @tc.expected: step6. Return OK.
919      */
920     option = {true, false, false, CipherType::DEFAULT, g_passwd3};
921     g_mgr.GetKvStore(singleStoreIdNoPasswd, option, g_kvNbDelegateCallback);
922     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
923     ASSERT_TRUE(g_kvDelegateStatus == OK);
924     g_kvNbDelegatePtrWithoutPasswd = g_kvNbDelegatePtr;
925 
926     /**
927      * @tc.steps: step7. Open the db1 directly
928      * @tc.expected: step7. Return ERROR.
929      */
930     option = {true, false, false, CipherType::DEFAULT, g_passwd3};
931     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
932     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
933     ASSERT_TRUE(g_kvDelegateStatus != OK);
934 
935     /**
936      * @tc.steps: step8. Run the p2 command to open the db2 file.
937      * @tc.expected: step8. Return ERROR.
938      */
939     option = {true, false, true, CipherType::DEFAULT, g_passwd2};
940     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
941     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
942     ASSERT_TRUE(g_kvDelegateStatus == OK);
943 }
944 
945 /**
946   * @tc.name: PasswordIndependence001
947   * @tc.desc: The data of the current version of the board is exported and the package file is single.
948   * @tc.type: FUNC
949   * @tc.author: chenguoliang
950   */
951 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, PasswordIndependence001, TestSize.Level1)
952 {
953     /**
954      * @tc.steps: step1. Back up a single database db1 No password backup password p3
955      */
956     std::string singleExportFileNameNoPasswd = g_exportFileDir + "/passwordIndependence001.$$";
957     std::string singleStoreIdNoPasswd = "distributed_PasswordIndependence_001";
958 
959     KvStoreNbDelegate::Option option = {true, false, false};
960     option.storageEngineType = DistributedDB::GAUSSDB_RD;
961     option.rdconfig.type = HASH;
962     g_mgr.GetKvStore(singleStoreIdNoPasswd, option, g_kvNbDelegateCallback);
963     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
964     EXPECT_TRUE(g_kvDelegateStatus == OK);
965     g_kvNbDelegatePtrWithoutPasswd = g_kvNbDelegatePtr;
966 
967     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileNameNoPasswd, g_passwd3), NOT_SUPPORT);
968 }
969 
970 #ifndef OMIT_MULTI_VER
TryDbForPasswordIndependence002()971 static void TryDbForPasswordIndependence002()
972 {
973     std::string multiStoreIdNoPasswd = "distributed_DbForPasswordIndependence_002";
974     std::string multiStoreId = "distributed_DbForPasswordIndependence_002";
975 
976     KvStoreDelegate::Option option = {true, false, true, CipherType::DEFAULT, g_passwd3};
977     g_mgr.GetKvStore(multiStoreIdNoPasswd, option, g_kvDelegateCallback);
978     ASSERT_TRUE(g_kvDelegatePtr == nullptr);
979     ASSERT_TRUE(g_kvDelegateStatus != OK);
980 
981     option = {true, false, true, CipherType::DEFAULT, g_passwd4};
982     g_mgr.GetKvStore(multiStoreId, option, g_kvDelegateCallback);
983     ASSERT_TRUE(g_kvDelegatePtr == nullptr);
984     ASSERT_TRUE(g_kvDelegateStatus != OK);
985 
986     option = {true, false, false, CipherType::DEFAULT, g_passwd3};
987     g_mgr.GetKvStore(multiStoreIdNoPasswd, option, g_kvDelegateCallback);
988     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
989     ASSERT_TRUE(g_kvDelegateStatus == OK);
990     g_kvDelegatePtrWithoutPasswd = g_kvDelegatePtr;
991 
992     option = {true, false, false, CipherType::DEFAULT, g_passwd3};
993     g_mgr.GetKvStore(multiStoreId, option, g_kvDelegateCallback);
994     ASSERT_TRUE(g_kvDelegatePtr == nullptr);
995     ASSERT_TRUE(g_kvDelegateStatus != OK);
996 
997     option = {true, false, true, CipherType::DEFAULT, g_passwd2};
998     g_mgr.GetKvStore(multiStoreId, option, g_kvDelegateCallback);
999     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
1000     ASSERT_TRUE(g_kvDelegateStatus == OK);
1001 }
1002 
1003 /**
1004   * @tc.name: PasswordIndependence002
1005   * @tc.desc: The data of the current version of the board is exported and the package file is single.
1006   * @tc.type: FUNC
1007   * @tc.author: chenguoliang
1008   */
1009 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, PasswordIndependence002, TestSize.Level1)
1010 {
1011     /**
1012      * @tc.steps: step1. Back up a single database db1 No password backup password p3
1013      */
1014     std::string multiExportFileNameNoPasswd = g_exportFileDir + "/passwordIndependence002.$$";
1015     std::string multiStoreIdNoPasswd = "distributed_PasswordIndependence_002";
1016     KvStoreDelegate::Option option;
1017     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1018     option.rdconfig.type = HASH;
1019     g_mgr.GetKvStore(multiStoreIdNoPasswd, option, g_kvDelegateCallback);
1020     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
1021     EXPECT_TRUE(g_kvDelegateStatus == OK);
1022     g_kvDelegatePtrWithoutPasswd = g_kvDelegatePtr;
1023 
1024     EXPECT_EQ(g_kvDelegatePtr->Export(multiExportFileNameNoPasswd, g_passwd3), NOT_SUPPORT);
1025 }
1026 #endif // OMIT_MULTI_VER
1027 
1028 /**
1029   * @tc.name: PasswordIndependence002
1030   * @tc.desc: The data of the current version of the board is exported and the package file is single.
1031   * @tc.type: FUNC
1032   * @tc.author: chenguoliang
1033   */
1034 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, PasswordIndependence003, TestSize.Level1)
1035 {
1036     /**
1037      * @tc.steps: step1. Back up the (passwd1) encryption single-version (passwd2) database.
1038      */
1039     std::string singleExportFileName = g_exportFileDir + "/passwordIndependence003.$$";
1040     std::string singleStoreId = "distributed_ExportSingle_009";
1041     KvStoreNbDelegate::Option option = {true, false, true, CipherType::DEFAULT, g_passwd2};
1042     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1043     option.rdconfig.type = HASH;
1044     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1045     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1046 }
1047 
1048 /**
1049   * @tc.name: SeparaDbExportAndImport
1050   * @tc.desc: Import and export after Separate database.
1051   * @tc.type: FUNC
1052   * @tc.author: chenguoliang
1053   */
1054 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, SeparaDbExportAndImport, TestSize.Level1)
1055 {
1056     std::shared_ptr<ProcessSystemApiAdapterImpl> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
1057     EXPECT_TRUE(adapter != nullptr);
1058     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
1059 
1060     std::string singleExportFileName = g_exportFileDir + "/SeparaDbExportAndImport.$$";
1061     std::string singleStoreId = "distributed_ExportSingle_010";
1062     KvStoreNbDelegate::Option option = {true, false, false};
1063     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1064     option.rdconfig.type = HASH;
1065     SecurityOption secOption{SecurityLabel::S3, SecurityFlag::SECE};
1066     option.secOption = secOption;
1067 
1068     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1069     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1070     EXPECT_EQ(g_kvDelegateStatus, OK);
1071 
1072     g_kvNbDelegatePtr->Put(KEY_1, VALUE_1);
1073 
1074     CipherPassword passwd;
1075     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
1076 
1077     g_kvNbDelegatePtr->Put(KEY_2, VALUE_2);
1078 
1079     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleExportFileName, passwd), OK);
1080     Value valueRead;
1081     g_kvNbDelegatePtr->Get(KEY_2, valueRead);
1082     EXPECT_EQ(valueRead, Value());
1083     g_kvNbDelegatePtr->Get(KEY_1, valueRead);
1084     EXPECT_EQ(valueRead, VALUE_1);
1085     g_kvNbDelegatePtr->Put(KEY_3, VALUE_3);
1086 
1087     EXPECT_EQ(g_kvNbDelegatePtr->Rekey(g_passwd1), NOT_SUPPORT);
1088     g_kvNbDelegatePtr->Get(KEY_3, valueRead);
1089     EXPECT_EQ(valueRead, VALUE_3);
1090 
1091     // clear resource
1092     g_junkFilesList.push_back(singleExportFileName);
1093 
1094     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1095     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1096     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1097 
1098     option.passwd = g_passwd1;
1099     option.isEncryptedDb = true;
1100     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1101     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1102 }
1103 
1104 /**
1105   * @tc.name: SeparaDbExportAndImport
1106   * @tc.desc: Import and export after Separate database.
1107   * @tc.type: FUNC
1108   * @tc.author: chenguoliang
1109   */
1110 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, SeparaDbNoPasswdRekey, TestSize.Level1)
1111 {
1112     std::shared_ptr<ProcessSystemApiAdapterImpl> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
1113     EXPECT_TRUE(adapter != nullptr);
1114     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
1115 
1116     KvStoreNbDelegate::Option option = {true, false, true};
1117     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1118     option.rdconfig.type = HASH;
1119     SecurityOption secOption{SecurityLabel::S3, SecurityFlag::SECE};
1120     option.secOption = secOption;
1121     option.passwd = g_passwd1;
1122     g_mgr.GetKvStore("SeparaDbNoPasswdRekey", option, g_kvNbDelegateCallback);
1123     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1124 }
1125 
1126 /**
1127   * @tc.name: ForceExportTest001
1128   * @tc.desc: Force export to an existing file.
1129   * @tc.type: FUNC
1130   * @tc.require:
1131   * @tc.author: lianhuix
1132   */
1133 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ForceExportTest001, TestSize.Level1)
1134 {
1135     KvStoreNbDelegate::Option option = {true, false, false};
1136     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1137     option.rdconfig.type = HASH;
1138     g_mgr.GetKvStore("ForceExportTest001", option, g_kvNbDelegateCallback);
1139     EXPECT_EQ(g_kvDelegateStatus, OK);
1140     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1141 
1142     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
1143 
1144     CipherPassword passwd;
1145     std::string exportFileName = g_exportFileDir + "ForceExportTest001.back";
1146 
1147     int fd = open(exportFileName.c_str(), (O_WRONLY | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP));
1148     ASSERT_TRUE(fd >= 0);
1149     std::string text = "Hello world.";
1150     write(fd, text.c_str(), text.length());
1151     close(fd);
1152 
1153     chmod(exportFileName.c_str(), S_IRWXU);
1154     EXPECT_EQ(g_kvNbDelegatePtr->Export(exportFileName, passwd, true), OK);
1155 
1156     uint32_t filePermission = 0;
1157     struct stat fileStat;
1158     EXPECT_EQ(stat(exportFileName.c_str(), &fileStat), 0);
1159     filePermission = fileStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
1160     EXPECT_EQ(filePermission, static_cast<uint32_t>(S_IRWXU));
1161 
1162     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1163     g_kvNbDelegatePtr = nullptr;
1164     EXPECT_EQ(g_mgr.DeleteKvStore("ForceExportTest001"), OK);
1165 
1166     g_mgr.GetKvStore(STORE_ID_1, option, g_kvNbDelegateCallback);
1167     EXPECT_EQ(g_kvDelegateStatus, OK);
1168     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1169 
1170     EXPECT_EQ(g_kvNbDelegatePtr->Import(exportFileName, passwd), OK);
1171     Value val;
1172     g_kvNbDelegatePtr->Get(KEY_1, val);
1173     EXPECT_EQ(val, VALUE_1);
1174 
1175     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1176     g_kvNbDelegatePtr = nullptr;
1177     EXPECT_EQ(g_mgr.DeleteKvStore(STORE_ID_1), OK);
1178 }
1179 
1180 /**
1181   * @tc.name: abortHandle001
1182   * @tc.desc: Intercept obtaining new write handles during Import.
1183   * @tc.type: FUNC
1184   * @tc.require:
1185   * @tc.author: bty
1186   */
1187 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, abortHandle001, TestSize.Level1)
1188 {
1189     std::string singleStoreId = "ExportAbortHandle_001";
1190     KvStoreNbDelegate::Option option = {true, false, false};
1191     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1192     option.rdconfig.type = HASH;
1193     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1194 
1195     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1196     EXPECT_TRUE(g_kvDelegateStatus == OK);
1197 
1198     /**
1199      * @tc.steps: step1. Init data for export.
1200      */
1201     std::string str(1024, 'k');
1202     Value value(str.begin(), str.end());
1203     for (int i = 0; i < 1000; ++i) {
1204         Key key;
1205         DBCommon::StringToVector(std::to_string(i), key);
1206         g_kvNbDelegatePtr->Put(key, value);
1207     }
1208 
1209     /**
1210      * @tc.steps: step2. Execute the export action.
1211      */
1212     std::string singleExportFileName = g_exportFileDir + "/UnExportAbortHandle001.$$";
1213     CipherPassword passwd;
1214     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleExportFileName, passwd), OK);
1215 
1216     /**
1217      * @tc.steps: step3. Multi threads to occupy write handles.
1218      */
1219     for (int i = 0; i < 10; ++i) { // 10 is run times
1220         vector<thread> threads;
__anon5f9368450202() 1221         threads.emplace_back(thread([&]() {
1222             g_kvNbDelegatePtr->CheckIntegrity();
1223         }));
1224         threads.emplace_back(&KvStoreNbDelegate::Import, g_kvNbDelegatePtr, singleExportFileName, passwd);
__anon5f9368450302() 1225         threads.emplace_back(thread([&i]() {
1226             std::this_thread::sleep_for(std::chrono::milliseconds(i));
1227             g_kvNbDelegatePtr->CheckIntegrity();
1228         }));
1229         for (auto &th: threads) {
1230             th.join();
1231         }
1232     }
1233     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1234     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
1235     g_junkFilesList.push_back(singleExportFileName);
1236 }
1237 
1238 /**
1239   * @tc.name: ImportTest001
1240   * @tc.desc: Verify that it will allow to import backup file again during it is importing.
1241   * @tc.type: FUNC
1242   * @tc.require:
1243   * @tc.author: chenguoliang
1244   */
1245 HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, ImportTest001, TestSize.Level1)
1246 {
1247     /**
1248      * @tc.steps: step1. Pre-create folder dir
1249      */
1250     std::string singleFileName = g_exportFileDir + "/ImportTest001.$$";
1251     std::string singleStoreId = "distributed_ImportSingle_001";
1252     KvStoreNbDelegate::Option option = {true, false, false};
1253     option.storageEngineType = DistributedDB::GAUSSDB_RD;
1254     option.rdconfig.type = HASH;
1255     g_mgr.GetKvStore(singleStoreId, option, g_kvNbDelegateCallback);
1256 
1257     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1258     EXPECT_TRUE(g_kvDelegateStatus == OK);
1259 
1260     /**
1261      * @tc.steps: step2. Specify the path to export the non-encrypted board database.
1262      * @tc.expected: step2. Returns OK
1263      */
1264     CipherPassword passwd;
1265     EXPECT_EQ(g_kvNbDelegatePtr->Export(singleFileName, passwd), OK);
1266 
1267     /**
1268      * @tc.steps: step3. start subthread to import the backup file.
1269      * @tc.expected: step3. start successfully.
1270      */
1271     std::atomic<bool> readyFlag(false);
1272     readyFlag.store(false);
1273     std::condition_variable backupVar;
__anon5f9368450402() 1274     thread subThread([&singleFileName, &passwd, &readyFlag, &backupVar]() {
1275         EXPECT_EQ(g_kvNbDelegatePtr->Import(singleFileName, passwd), OK);
1276         readyFlag.store(true);
1277         backupVar.notify_one();
1278     });
1279     subThread.detach();
1280 
1281     /**
1282      * @tc.steps: step4. import the backup file during the subthread is importing with empty password.
1283      * @tc.expected: step4. import successfully and return OK.
1284      */
1285     const static int millsecondsPerSecond = 1000;
1286     std::this_thread::sleep_for(std::chrono::microseconds(millsecondsPerSecond));
1287     EXPECT_EQ(g_kvNbDelegatePtr->Import(singleFileName, passwd), OK);
1288 
1289     std::mutex rekeyMtx;
1290     std::unique_lock<std::mutex> lck(rekeyMtx);
__anon5f9368450502null1291     backupVar.wait(lck, [&]{ return readyFlag.load(); });
1292 
1293     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1294     EXPECT_EQ(g_mgr.DeleteKvStore(singleStoreId), OK);
1295 }
1296 #endif // OMIT_ENCRYPT