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