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