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