• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 #include <ctime>
17 #include <cmath>
18 #include <thread>
19 #include <fstream>
20 #include <condition_variable>
21 #include <mutex>
22 #include <string>
23 
24 #include "distributeddb_data_generator.h"
25 #include "distributeddb_nb_test_tools.h"
26 #include "kv_store_delegate_manager.h"
27 #include "distributed_test_tools.h"
28 
29 using namespace std;
30 using namespace testing;
31 #if defined TESTCASES_USING_GTEST_EXT
32 using namespace testing::ext;
33 #endif
34 using namespace DistributedDB;
35 using namespace DistributedDBDataGenerator;
36 using namespace std::placeholders;
37 static std::condition_variable g_conditionNbVar;
38 
39 namespace DistributedDBNbCreate {
40 DistributedDB::CipherPassword g_passwd1;
41 DistributedDB::CipherPassword g_passwd2;
42 class DistributeddbNbCreateTest : public testing::Test {
43 public:
44     static void SetUpTestCase(void);
45     static void TearDownTestCase(void);
46     void SetUp();
47     void TearDown();
48 };
49 
SetUpTestCase(void)50 void DistributeddbNbCreateTest::SetUpTestCase(void)
51 {
52     (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
53     (void)g_passwd2.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
54 }
55 
TearDownTestCase(void)56 void DistributeddbNbCreateTest::TearDownTestCase(void)
57 {
58 }
59 
SetUp(void)60 void DistributeddbNbCreateTest::SetUp(void)
61 {
62     UnitTest *test = UnitTest::GetInstance();
63     ASSERT_NE(test, nullptr);
64     const TestInfo *testinfo = test->current_test_info();
65     ASSERT_NE(testinfo, nullptr);
66     string testCaseName = string(testinfo->name());
67     MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
68 }
69 
TearDown(void)70 void DistributeddbNbCreateTest::TearDown(void)
71 {
72 }
73 
WaitingDeletingDB()74 void WaitingDeletingDB()
75 {
76     KvStoreDelegateManager *manager = nullptr;
77     KvStoreNbDelegate *result = nullptr;
78     /**
79      * @tc.steps: step2. delete db when kill the process manually.
80      * @tc.expected: step2. delete success.
81      */
82     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
83     ASSERT_TRUE(manager != nullptr && result != nullptr);
84     EXPECT_EQ(manager->CloseKvStore(result), OK);
85     result = nullptr;
86     if (!g_option.isMemoryDb) {
87         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
88     }
89     MST_LOG("please kill the testing process...");
90     std::this_thread::sleep_for(std::chrono::seconds(TWENTY_SECONDS));
91     delete manager;
92     manager = nullptr;
93 }
94 
95 /*
96  * @tc.name: ManagerDb 001
97  * @tc.desc: Verify that can create distributed db normally.
98  * @tc.type: FUNC
99  * @tc.require: SR000CQDV2
100  * @tc.author: luqianfu
101  */
102 HWTEST_F(DistributeddbNbCreateTest, ManagerDb001, TestSize.Level1)
103 {
104     const std::string storeId[ID_MIN_CNT] = { "STORE_ID_1", "STORE_ID_2" };
105     const std::string appId[ID_MIN_CNT] = { "APP_ID_1", "APP_ID_2" };
106     const std::string userId[ID_MIN_CNT] = { "USER_ID_1", "USER_ID_2" };
107 
108     KvStoreDelegateManager *manager = nullptr;
109     KvStoreNbDelegate *result = nullptr;
110 
111     /**
112      * @tc.steps: step1. create NBdelegate with different normal appId, userId and create db with storeId.
113      * @tc.expected: step1. each db can be created successfully.
114      */
115     for (unsigned int xCnt = ID_CNT_START; xCnt < ID_MIN_CNT; xCnt++) {
116         for (unsigned int yCnt = ID_CNT_START; yCnt < ID_MIN_CNT; yCnt++) {
117             for (unsigned int zCnt = ID_CNT_START; zCnt < ID_MIN_CNT; zCnt++) {
118                 const DBParameters dbParameters(storeId[xCnt], appId[yCnt], userId[zCnt]);
119                 result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, dbParameters, g_option);
120                 ASSERT_TRUE(manager != nullptr && result != nullptr);
121                 EXPECT_TRUE(EndCaseDeleteDB(manager, result, storeId[xCnt], g_option.isMemoryDb));
122             }
123         }
124     }
125 }
126 
127 /*
128  * @tc.name: ManagerDb 002
129  * @tc.desc: Verify that can create distributed with inexistence appid and userid.
130  * @tc.type: FUNC
131  * @tc.require: SR000CQDV2
132  * @tc.author: luqianfu
133  */
134 HWTEST_F(DistributeddbNbCreateTest, ManagerDb002, TestSize.Level0)
135 {
136     KvStoreDelegateManager *manager = nullptr;
137     KvStoreNbDelegate *result = nullptr;
138 
139     /**
140      * @tc.steps: step1. create NBdelegate with not exist appId, userId and create db with storeId.
141      * @tc.expected: step1. create db failed.
142      */
143     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, DB_PARAMETER_0_1, g_option);
144     ASSERT_EQ(result, nullptr);
145     EXPECT_EQ(manager->CloseKvStore(result), INVALID_ARGS);
146     EXPECT_EQ(manager->DeleteKvStore(STORE_ID), INVALID_ARGS);
147     delete manager;
148     manager = nullptr;
149     result = nullptr;
150 }
151 
152 /*
153  * @tc.name: ManagerDb 003
154  * @tc.desc: test and verify that Set normal db path and can create distributed.
155  * @tc.type: FUNC
156  * @tc.require: SR000CQDV2
157  * @tc.author: luqianfu
158  */
159 HWTEST_F(DistributeddbNbCreateTest, ManagerDb003, TestSize.Level0)
160 {
161     KvStoreDelegateManager *manager = nullptr;
162     KvStoreNbDelegate *result = nullptr;
163 
164     /**
165      * @tc.steps: step1. Set normal path create db.
166      * @tc.expected: step1. db can be successfully created.
167      */
168     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
169     ASSERT_TRUE(manager != nullptr && result != nullptr);
170     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, g_option.isMemoryDb));
171 }
172 
DefinePath(string & pathOk,string & pathError)173 void DefinePath(string &pathOk, string &pathError)
174 {
175 #if defined(RUNNING_ON_LINUX)
176     pathOk =
177         "/data/test/getstub/dddddddddddddddddddddddddddddddddddssssssssssssssfffffDddddddddssssssssssssssssssfffffd/"
178         "dddddddddddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/dddddddd"
179         "dddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/dddddddddddddddd"
180         "dddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/dddddddddddddddddddddddd"
181         "dssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/eserweiaa";
182     MST_LOG("pathOk.length() = %zd", pathOk.length());
183     pathError =
184         "/data/test/getstub/ddddddddddddddddddddsssssssssssssfffffDdddddddddddddddddddddddssssssssssssssssssfffffd/"
185         "qdddddddddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/qddddddd"
186         "dddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/qdddddddddddddd"
187         "ddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/qddddddddddddddddddddd"
188         "dddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/qserweiaaaww";
189     MST_LOG("pathError.length() = %zd", pathError.length());
190 #elif defined RUNNING_ON_WIN
191     pathOk =
192         "/data/test/getstub/dddddddddddddddddddddddddddddddddddssssssssssssssfffffDddddddddssssssssssssssssssfffffd/"
193         "dddddddddddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/dddddddd"
194         "dssssssssssssssssssfffffDdddddd";
195     MST_LOG("pathOk.length() = %zd", pathOk.length());
196     pathError =
197         "/data/test/getstub/ddddddddddddddddddddsssssssssssssfffffDdddddddddddddddddddddddssssssssssssssssssfffffd/"
198         "qdddddddddddddddddddddddssssssssssssssssssfffffDdddddddddddddddddddddddddssssssssssssssssssfffffd/qddddddd"
199         "dddssssssssssssssssssfffffDddddddd";
200     MST_LOG("pathError.length() = %zd", pathError.length());
201 #endif
202     return;
203 }
204 /*
205  * @tc.name: ManagerDb 004
206  * @tc.desc: test and verify that Set abnormal db path and can't create distributeddb.
207  * @tc.type: FUNC
208  * @tc.require: SR000CCPOI
209  * @tc.author: luqianfu
210  */
211 HWTEST_F(DistributeddbNbCreateTest, ManagerDb004, TestSize.Level1)
212 {
213     /**
214      * @tc.steps: step1. Construct different valid path, which contains, '\0', up letter, low letter, data,
215      *      '_', or '\\' '//' '&' '^' '%' '#' and length equals to 512 characters. invalid path include null,
216      *      longer than 512 characters and not exist path.
217      * @tc.expected: step1. Construct success.
218      */
219     string pathOk = "", pathError = "";
220     DefinePath(pathOk, pathError);
221     const string nbDirectory[DIR_MAX_CNT] = {
222         "", "/data/test/getstub/ddddddddddddddddddd/idata/nbstub\0fdfg/", pathOk, pathError,
223         "/data/test/getstub/ddddddddddddddddddd/idata/..nbstub/",
224         "/data/test/getstub/ddddddddddddddddddd/idata/…nbstub/",
225         "/data/test/getstub/ddddddddddddddddddd/idata/分布式数据库/",
226         "/data/test/getstub/ddddddddddddddddddd/idata/nbstubÄäÖöÜü/",
227         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub\\\\/",
228         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub///",
229         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub&/",
230         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub^/",
231         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub%/",
232         "/data/test/getstub/ddddddddddddddddddd/idata/nbstub#/" };
233     const string notExistPath = "/data/test/getstub/dddddddddrwddddddd/idqta/nbs";
234     DistributedDB::DBStatus resultStatus[DIR_MAX_CNT] = {
235         INVALID_ARGS, OK, OK, INVALID_ARGS, OK,
236         OK, OK, OK, OK, OK,
237         OK, OK, OK, OK };
238     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager("APP_ID_1", "USER_ID_1");
239     ASSERT_NE(manager, nullptr);
240     /**
241      * @tc.steps: step2. Set different path construct above, and create db.
242      * @tc.expected: step2. return INVALID_ARGS if path invalid, otherwise can return ok.
243      */
244     DBStatus status;
245     for (unsigned int index = DIR_CNT_START; index < DIR_MAX_CNT; index++) {
246         MST_LOG("index %d", index);
247         SetDir(nbDirectory[index]);
248         KvStoreConfig config = { .dataDir = nbDirectory[index] };
249         status = manager->SetKvStoreConfig(config);
250         MST_LOG("config.dataDir = %s", config.dataDir.c_str());
251         MST_LOG("index[%d], status[%d], expect[%d]", index, status, resultStatus[index]);
252         EXPECT_EQ(status, resultStatus[index]);
253         chdir("/");
254         MST_LOG("\n");
255     }
256     /**
257      * @tc.steps: step3. verify that path do not exist, and create db.
258      * @tc.expected: step3. return INVALID_ARGS.
259      */
260     KvStoreConfig config = { .dataDir = notExistPath };
261     status = manager->SetKvStoreConfig(config);
262     MST_LOG("config.dataDir = %s", config.dataDir.c_str());
263     EXPECT_EQ(status, INVALID_ARGS);
264 
265     delete manager;
266     manager = nullptr;
267 }
268 
269 /*
270  * @tc.name: ManagerDb 006
271  * @tc.desc: verify that db can created uses IS_NEED_CREATE mode.
272  * @tc.type: FUNC
273  * @tc.require: SR000CCPOI
274  * @tc.author: luqianfu
275  */
276 HWTEST_F(DistributeddbNbCreateTest, ManagerDb006, TestSize.Level0)
277 {
278     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
279     ASSERT_NE(manager, nullptr);
280     SetDir(DistributedDBConstant::NB_DIRECTOR);
281     ASSERT_EQ(manager->SetKvStoreConfig(DistributedDBConstant::CONFIG), DBStatus::OK);
282     ASSERT_EQ(manager->DeleteKvStore(STORE_ID_1), DBStatus::NOT_FOUND);
283     delete manager;
284     manager = nullptr;
285 
286     /**
287      * @tc.steps: step1. create db use IS_NEED_CREATE mode.
288      * @tc.expected: step1. Create success.
289      */
290     KvStoreNbDelegate *result = nullptr;
291     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
292     ASSERT_TRUE(manager != nullptr && result != nullptr);
293     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, g_option.isMemoryDb));
294 }
295 
296 /*
297  * @tc.name: ManagerDb 007
298  * @tc.desc: verify that db can't be created if it inexist and opened when it exist if it uses IS_NOT_NEED_CREATE mode.
299  * @tc.type: FUNC
300  * @tc.require: SR000CCPOI
301  * @tc.author: luqianfu
302  */
303 HWTEST_F(DistributeddbNbCreateTest, ManagerDb007, TestSize.Level0)
304 {
305     KvStoreDelegateManager *manager = nullptr;
306     KvStoreNbDelegate *result = nullptr;
307 
308     /**
309      * @tc.steps: step1. db inexist and create it use IS_NOT_NEED_CREATE mode.
310      * @tc.expected: step1. Create failed.
311      */
312     Option option = g_option;
313     option.createIfNecessary = IS_NOT_NEED_CREATE;
314     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1_2_1, option);
315     ASSERT_EQ(manager, nullptr);
316     ASSERT_EQ(result, nullptr);
317 }
318 
319 /*
320  * @tc.name: ManagerDb 008
321  * @tc.desc: verify that different storeids can create different dbs.
322  * @tc.type: FUNC
323  * @tc.require: SR000CCPOI
324  * @tc.author: luqianfu
325  */
326 HWTEST_F(DistributeddbNbCreateTest, ManagerDb008, TestSize.Level1)
327 {
328     const std::string storeId[ID_MEDIUM_CNT] = { "STORE_ID_1", "STORE_ID_2", "STORE_ID_3" };
329     const std::string appId[ID_MEDIUM_CNT] = { "APP_ID_1", "APP_ID_2", "APP_ID_3" };
330     const std::string userId[ID_MEDIUM_CNT] = { "USER_ID_1", "USER_ID_2", "USER_ID_3" };
331     /**
332      * @tc.steps: step1. there isn't db storeId1, storeId2, storeId3, create db with different appIds and userIds.
333      * @tc.expected: step1. Create db storeId1, storeId2, storeId3 success.
334      */
335     DelegateMgrNbCallback delegateMgrCallback;
336     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
337         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
338     SetDir(DistributedDBConstant::CONFIG.dataDir);
339     DBStatus status;
340     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
341     for (unsigned int index = 0; index < ID_MEDIUM_CNT; index++) {
342         KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(appId[index], userId[index]);
343         ASSERT_NE(manager, nullptr);
344         status = manager->SetKvStoreConfig(DistributedDBConstant::CONFIG);
345         EXPECT_EQ(status, OK);
346         manager->GetKvStore(storeId[index], option, Nbfunction);
347         EXPECT_EQ(delegateMgrCallback.GetStatus(), OK);
348         KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate*>(delegateMgrCallback.GetKvStore());
349         EXPECT_NE(delegate, nullptr);
350         EXPECT_EQ(manager->CloseKvStore(delegate), OK);
351         delegate = nullptr;
352         EXPECT_EQ(manager->DeleteKvStore(storeId[index]), OK);
353         delete manager;
354         manager = nullptr;
355     }
356 }
357 
358 /*
359  * @tc.name: ManagerDb 009
360  * @tc.desc: verify that don't set dir create db failed.
361  * @tc.type: FUNC
362  * @tc.require: SR000CCPOI
363  * @tc.author: luqianfu
364  */
365 HWTEST_F(DistributeddbNbCreateTest, ManagerDb009, TestSize.Level0)
366 {
367     KvStoreDelegateManager *manager = nullptr;
368     /**
369      * @tc.steps: step1. didn't set datadir, delegate can create success but db create failed.
370      * @tc.expected: step1. Create failed.
371      */
372     DelegateMgrNbCallback delegateMgrCallback;
373     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
374         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
375     manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
376     ASSERT_NE(manager, nullptr);
377     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
378     manager->GetKvStore(STORE_ID_1, option, Nbfunction);
379     EXPECT_NE(delegateMgrCallback.GetStatus(), OK);
380     KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate*>(delegateMgrCallback.GetKvStore());
381     EXPECT_EQ(delegate, nullptr);
382     EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
383     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), INVALID_ARGS);
384     delete manager;
385     manager = nullptr;
386 }
387 
388 /*
389  * @tc.name: ManagerDb 010
390  * @tc.desc: verify that different delegate can create different storeid.
391  * @tc.type: FUNC
392  * @tc.require: SR000CCPOI
393  * @tc.author: luqianfu
394  */
395 HWTEST_F(DistributeddbNbCreateTest, ManagerDb010, TestSize.Level1)
396 {
397     const std::string storeId[STORE_CNT] = { "STORE_ID_1", "STORE_ID_2", "STORE_ID_3", "STORE_ID_4" };
398     KvStoreDelegateManager *manager[STORE_CNT] = { nullptr };
399     KvStoreNbDelegate *callbackKvStore[STORE_CNT] = { nullptr };
400     DelegateMgrNbCallback delegateMgrCallback[STORE_CNT];
401 
402     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction[STORE_CNT] = {
403         bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback[INDEX_ZEROTH], _1, _2),
404         bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback[INDEX_FIRST], _1, _2),
405         bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback[INDEX_SECOND], _1, _2),
406         bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback[INDEX_THIRD], _1, _2)
407     };
408     SetDir(DistributedDBConstant::NB_DIRECTOR);
409     /**
410      * @tc.steps: step1. create NBdelegate(APP_ID_1, USER_ID_1) and set dataDir.
411      * @tc.expected: step1. Create success.
412      */
413     manager[INDEX_ZEROTH] = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
414     ASSERT_NE(manager[INDEX_ZEROTH], nullptr);
415     manager[INDEX_ZEROTH]->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR });
416 
417     /**
418      * @tc.steps: step2. create NBdelegate(APP_ID_1, USER_ID_2) and set dataDir.
419      * @tc.expected: step2. Create success.
420      */
421     manager[INDEX_FIRST] = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_2);
422     ASSERT_NE(manager[INDEX_FIRST], nullptr);
423     manager[INDEX_FIRST]->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR });
424 
425     /**
426      * @tc.steps: step3. create NBdelegate(APP_ID_2, USER_ID_1) and set dataDir.
427      * @tc.expected: step3. Create success.
428      */
429     manager[INDEX_SECOND] = new (std::nothrow) KvStoreDelegateManager(APP_ID_2, USER_ID_1);
430     ASSERT_NE(manager[INDEX_SECOND], nullptr);
431     manager[INDEX_SECOND]->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR });
432     /**
433      * @tc.steps: step4. create NBdelegate(APP_ID_2, USER_ID_2) and set dataDir.
434      * @tc.expected: step4. Create success.
435      */
436     manager[INDEX_THIRD] = new (std::nothrow) KvStoreDelegateManager(APP_ID_2, USER_ID_2);
437     ASSERT_NE(manager[INDEX_THIRD], nullptr);
438     manager[INDEX_THIRD]->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR });
439     /**
440      * @tc.steps: step5. use STORE_ID_1 create db.
441      * @tc.expected: step5. Create success.
442      */
443     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
444     manager[INDEX_ZEROTH]->GetKvStore(STORE_ID_1, option, Nbfunction[INDEX_ZEROTH]);
445     EXPECT_EQ(delegateMgrCallback[INDEX_ZEROTH].GetStatus(), OK);
446     callbackKvStore[INDEX_ZEROTH] = delegateMgrCallback[INDEX_ZEROTH].GetKvStore();
447     EXPECT_EQ(manager[INDEX_ZEROTH]->CloseKvStore(callbackKvStore[INDEX_ZEROTH]), OK);
448     /**
449      * @tc.steps: step6. use STORE_ID_2 create db.
450      * @tc.expected: step6. Create success.
451      */
452     manager[INDEX_FIRST]->GetKvStore(STORE_ID_2, option, Nbfunction[INDEX_FIRST]);
453     EXPECT_EQ(delegateMgrCallback[INDEX_FIRST].GetStatus(), OK);
454     callbackKvStore[INDEX_FIRST] = delegateMgrCallback[INDEX_FIRST].GetKvStore();
455     EXPECT_EQ(manager[INDEX_FIRST]->CloseKvStore(callbackKvStore[INDEX_FIRST]), OK);
456     /**
457      * @tc.steps: step7. use STORE_ID_3 create db.
458      * @tc.expected: step7. Create success.
459      */
460     manager[INDEX_SECOND]->GetKvStore(STORE_ID_3, option, Nbfunction[INDEX_SECOND]);
461     EXPECT_EQ(delegateMgrCallback[INDEX_SECOND].GetStatus(), OK);
462     callbackKvStore[INDEX_SECOND] = delegateMgrCallback[INDEX_SECOND].GetKvStore();
463     EXPECT_EQ(manager[INDEX_SECOND]->CloseKvStore(callbackKvStore[INDEX_SECOND]), OK);
464     /**
465      * @tc.steps: step8. use STORE_ID_4 create db.
466      * @tc.expected: step8. Create success.
467      */
468     manager[INDEX_THIRD]->GetKvStore(STORE_ID_4, option, Nbfunction[INDEX_THIRD]);
469     EXPECT_EQ(delegateMgrCallback[INDEX_THIRD].GetStatus(), OK);
470     callbackKvStore[INDEX_THIRD] = delegateMgrCallback[INDEX_THIRD].GetKvStore();
471     EXPECT_EQ(manager[INDEX_THIRD]->CloseKvStore(callbackKvStore[INDEX_THIRD]), OK);
472 
473     for (unsigned int index = 0; index < STORE_CNT; index++) {
474         EXPECT_EQ(manager[index]->DeleteKvStore(storeId[index]), OK);
475         delete manager[index];
476         manager[index] = nullptr;
477     }
478 }
479 
480 /*
481  * @tc.name: ManagerDb 011
482  * @tc.desc: verify whether it will check if storeid is invalid.
483  * @tc.type: FUNC
484  * @tc.require: SR000CCPOI
485  * @tc.author: luqianfu
486  */
487 HWTEST_F(DistributeddbNbCreateTest, ManagerDb011, TestSize.Level1)
488 {
489     /**
490      * @tc.steps: step1. Construct valid storeId, length equals to 128 characters, and which contains '\0',
491      *    up letter and low letter, invalid storeId which include null, or contains '\\' '//' '&' '^' '%' '#',
492      *    '..', '……', chinese, latins or longer than 128 characters.
493      * @tc.expected: step1. Construct success.
494      */
495     KvStoreDelegateManager *manager = nullptr;
496     const std::string STORE_OK =
497         "STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE"
498         "_OK1_STORE_OK1_STORE_OK";
499     const std::string STORE_ERROR =
500         "STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_"
501         "ERR_STORE_ERR_STORE_ERR";
502     const std::string STORE_ID[ID_MAX_CNT] = { "", "STORE_ID_1\0", STORE_OK, STORE_ERROR, "Store_ID_2",
503         "STORE_ID_1\\", "STORE_ID_1//", "STORE_I&D_1", "STORE_ID_1^", "STORE_ID%_1",
504         "STORE#_ID_1", "STORE_I+D_1", "STORE_拜拜", "STOREÄäÖöÜü_ID_", " b", "store~Id" };
505     DistributedDB::DBStatus resultStatus[ID_MAX_CNT] = {
506         INVALID_ARGS, OK, OK, INVALID_ARGS, OK,
507         INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS,
508         INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS };
509     SetDir(DistributedDBConstant::CONFIG.dataDir);
510 
511     /**
512      * @tc.steps: step2. Use different storeid create db.
513      * @tc.expected: step2. return ok if storeid is valid, and return INVALID_ARGS if storeId is invalid.
514      */
515     DelegateMgrNbCallback delegateMgrNbCallback;
516     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
517         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrNbCallback, _1, _2);
518     manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
519     ASSERT_NE(manager, nullptr);
520     manager->SetKvStoreConfig(DistributedDBConstant::CONFIG);
521     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
522     for (unsigned int index = 0; index < ID_MAX_CNT; index++) {
523         MST_LOG("index: %d", index);
524         manager->GetKvStore(STORE_ID[index], option, Nbfunction);
525         EXPECT_EQ(delegateMgrNbCallback.GetStatus(), resultStatus[index]);
526         KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate*>(delegateMgrNbCallback.GetKvStore());
527         EXPECT_EQ(manager->CloseKvStore(delegate), resultStatus[index]);
528         delegate = nullptr;
529         EXPECT_EQ(manager->DeleteKvStore(STORE_ID[index]), resultStatus[index]);
530     }
531     delete manager;
532     manager = nullptr;
533 }
534 
535 /*
536  * @tc.name: ManagerDb 013
537  * @tc.desc: verify that open db with IS_NEED_CREATE and IS_NOT_NEED_CREATE mode.
538  * @tc.type: FUNC
539  * @tc.require: SR000CCPOI
540  * @tc.author: luqianfu
541  */
542 HWTEST_F(DistributeddbNbCreateTest, ManagerDb013, TestSize.Level0)
543 {
544     KvStoreDelegateManager *manager = nullptr;
545     KvStoreNbDelegate *result = nullptr;
546     /**
547      * @tc.steps: step1. open db with IS_NOT_NEED_CREATE mode .
548      * @tc.expected: step1. open failed.
549      */
550     Option option = g_option;
551     option.createIfNecessary = IS_NOT_NEED_CREATE;
552     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
553     ASSERT_EQ(manager, nullptr);
554     ASSERT_EQ(result, nullptr);
555 
556     /**
557      * @tc.steps: step2. open db with IS_NEED_CREATE mode .
558      * @tc.expected: step2. open success.
559      */
560     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
561     ASSERT_TRUE(manager != nullptr && result != nullptr);
562     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, g_option.isMemoryDb));
563 }
564 
GetAndCloseKvStore(KvStoreDelegateManager * & manager,const std::string & storeId,KvStoreNbDelegate::Option & option,function<void (DBStatus,KvStoreNbDelegate *)> & nbfunction,DelegateMgrNbCallback & delegateMgrCallback)565 void GetAndCloseKvStore(KvStoreDelegateManager *&manager, const std::string &storeId,
566     KvStoreNbDelegate::Option &option, function<void(DBStatus, KvStoreNbDelegate*)> &nbfunction,
567     DelegateMgrNbCallback &delegateMgrCallback)
568 {
569     manager->GetKvStore(storeId, option, nbfunction);
570     EXPECT_EQ(delegateMgrCallback.GetStatus(), OK);
571     KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate *>(delegateMgrCallback.GetKvStore());
572     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
573     delegate = nullptr;
574 }
575 /*
576  * @tc.name: ManagerDb 014
577  * @tc.desc: verify that open db reduplicatedly.
578  * @tc.type: FUNC
579  * @tc.require: SR000CCPOI
580  * @tc.author: luqianfu
581  */
582 HWTEST_F(DistributeddbNbCreateTest, ManagerDb014, TestSize.Level1)
583 {
584     KvStoreDelegateManager *manager = nullptr;
585     DelegateMgrNbCallback delegateMgrCallback;
586     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
587         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
588     manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
589     ASSERT_NE(manager, nullptr);
590     SetDir(DistributedDBConstant::CONFIG.dataDir);
591     EXPECT_EQ(manager->SetKvStoreConfig(DistributedDBConstant::CONFIG), OK);
592 
593     KvStoreNbDelegate::Option option;
594     for (int cnt = 0; cnt < MANYTINES; cnt++) {
595         /**
596          * @tc.steps: step1. open db with IS_NEED_CREATE mode.
597          * @tc.expected: step1. open success.
598          */
599         option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
600         GetAndCloseKvStore(manager, STORE_ID_1, option, Nbfunction, delegateMgrCallback);
601 
602         /**
603          * @tc.steps: step2. open db with IS_NOT_NEED_CREATE mode.
604          * @tc.expected: step2. open success.
605          */
606         option.createIfNecessary = IS_NOT_NEED_CREATE;
607         GetAndCloseKvStore(manager, STORE_ID_1, option, Nbfunction, delegateMgrCallback);
608         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
609     }
610 
611     delete manager;
612     manager = nullptr;
613 }
614 
615 /*
616  * @tc.name: ManagerDb 015
617  * @tc.desc: verify that check storeid when open db.
618  * @tc.type: FUNC
619  * @tc.require: SR000CCPOI
620  * @tc.author: luqianfu
621  */
622 HWTEST_F(DistributeddbNbCreateTest, ManagerDb015, TestSize.Level1)
623 {
624     /**
625      * @tc.steps: step1. Construct valid storeId, length equals to 128 characters, and which contains '\0',
626      *    up letter and low letter, invalid storeId which include null, or contains '\\' '//' '&' '^' '%' '#',
627      *    '..', '……', chinese, latins or longer than 128 characters.
628      * @tc.expected: step1. Construct success.
629      */
630     const std::string STORE_OK =
631         "STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1_STORE_OK1"
632         "_STORE_OK1_STORE_OK1_STORE_OK";
633     const std::string STORE_ERROR =
634         "STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR_STORE_ERR"
635         "_STORE_ERR_STORE_ERR_STORE_ERR";
636     const std::string STORE_ID[ID_MAX_CNT] = { "", "STORE_ID_1\0", STORE_OK, STORE_ERROR, "Store_ID_2",
637         "STORE_ID_1\\", "STORE_ID_1//", "STORE_ID_1&", "STORE_ID_1^", "STORE_%ID_1",
638         "STORE_ID_1#", "STORE_I-D_1", "STORE_编号", "STORE_ID_ÄäÖöÜü", " b", "STORE_ID_1" };
639     DistributedDB::DBStatus resultStatus[ID_MAX_CNT] = {
640         INVALID_ARGS, OK, OK, INVALID_ARGS, OK,
641         INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS,
642         INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, INVALID_ARGS, OK };
643     KvStoreDelegateManager *manager = nullptr;
644     KvStoreNbDelegate *delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
645     ASSERT_NE(delegate, nullptr);
646     ASSERT_NE(manager, nullptr);
647     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
648     delegate = nullptr;
649 
650     /**
651      * @tc.steps: step2. Use different storeid open and create db.
652      * @tc.expected: step2. return ok if storeid is valid, and return INVALID_ARGS if storeId is invalid.
653      */
654     DelegateMgrNbCallback delegateMgrCallback;
655     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
656         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
657     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
658     for (unsigned int index = 0; index < ID_MAX_CNT; index++) {
659         MST_LOG("ManagerDb015 open %d db.", index);
660         manager->GetKvStore(STORE_ID[index], option, Nbfunction);
661         EXPECT_EQ(delegateMgrCallback.GetStatus(), resultStatus[index]);
662         KvStoreNbDelegate *delegateRes = const_cast<KvStoreNbDelegate*>(delegateMgrCallback.GetKvStore());
663         EXPECT_EQ(manager->CloseKvStore(delegateRes), resultStatus[index]);
664         delegateRes = nullptr;
665         EXPECT_EQ(manager->DeleteKvStore(STORE_ID[index]), resultStatus[index]);
666     }
667     delete manager;
668     manager = nullptr;
669 }
670 
671 /*
672  * @tc.name: ManagerDb 018
673  * @tc.desc: verify that can close db normally.
674  * @tc.type: FUNC
675  * @tc.require: SR000CCPOI
676  * @tc.author: luqianfu
677  */
678 HWTEST_F(DistributeddbNbCreateTest, ManagerDb018, TestSize.Level0)
679 {
680     KvStoreDelegateManager *manager = nullptr;
681     KvStoreNbDelegate *result = nullptr;
682 
683     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
684     ASSERT_TRUE(manager != nullptr && result != nullptr);
685     /**
686      * @tc.steps: step1. close db normally.
687      * @tc.expected: step1. success.
688      */
689     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, g_option.isMemoryDb));
690 }
691 
692 /*
693  * @tc.name: ManagerDb 019
694  * @tc.desc: verify that can't close db that do not exist.
695  * @tc.type: FUNC
696  * @tc.require: SR000CCPOI
697  * @tc.author: luqianfu
698  */
699 HWTEST_F(DistributeddbNbCreateTest, ManagerDb019, TestSize.Level0)
700 {
701     KvStoreDelegateManager *manager = nullptr;
702     KvStoreNbDelegate *result = nullptr;
703 
704     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
705     ASSERT_TRUE(manager != nullptr && result != nullptr);
706     EXPECT_EQ(manager->CloseKvStore(result), OK);
707     result = nullptr;
708     if (!g_option.isMemoryDb) {
709         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
710     }
711     /**
712      * @tc.steps: step1. close the db that is not exist.
713      * @tc.expected: step1. close failed.
714      */
715     EXPECT_EQ(manager->CloseKvStore(result), INVALID_ARGS);
716 
717     delete manager;
718     manager = nullptr;
719 }
720 
721 /*
722  * @tc.name: ManagerDb 020
723  * @tc.desc: verify that can't close db that is closed.
724  * @tc.type: FUNC
725  * @tc.require: SR000CCPOI
726  * @tc.author: luqianfu
727  */
728 HWTEST_F(DistributeddbNbCreateTest, ManagerDb020, TestSize.Level0)
729 {
730     KvStoreDelegateManager *manager = nullptr;
731 
732     DelegateMgrNbCallback delegateMgrCallback;
733     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
734         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
735     manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
736     ASSERT_NE(manager, nullptr);
737     SetDir(DistributedDBConstant::NB_DIRECTOR);
738     manager->SetKvStoreConfig(DistributedDBConstant::CONFIG);
739     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
740     manager->GetKvStore(STORE_ID_1, option, Nbfunction);
741     KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate*>(delegateMgrCallback.GetKvStore());
742     EXPECT_NE(delegate, nullptr);
743     /**
744      * @tc.steps: step1. make sure that db is closed.
745      * @tc.expected: step1. close success.
746      */
747     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
748     delegate = nullptr;
749     /**
750      * @tc.steps: step2. close it again.
751      * @tc.expected: step2. close failed.
752      */
753     EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
754     if (!g_option.isMemoryDb) {
755         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
756     }
757     delete manager;
758     manager = nullptr;
759 }
760 
761 /*
762  * @tc.name: ManagerDb 023
763  * @tc.desc: verify that delete the handler of the db.
764  * @tc.type: FUNC
765  * @tc.require: SR000CCPOI
766  * @tc.author: luqianfu
767  */
768 HWTEST_F(DistributeddbNbCreateTest, ManagerDb023, TestSize.Level0)
769 {
770     KvStoreDelegateManager *manager = nullptr;
771     KvStoreNbDelegate *result = nullptr;
772     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
773     ASSERT_TRUE(manager != nullptr && result != nullptr);
774     /**
775      * @tc.steps: step1. close db and delete db, then check the db document manually.
776      * @tc.expected: step1. close and delete db successfully, and the document is not exist.
777      */
778     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, g_option.isMemoryDb));
779 }
780 
781 /*
782  * @tc.name: ManagerDb 024
783  * @tc.desc: verify that delete db that was not closed.
784  * @tc.type: FUNC
785  * @tc.require: SR000CCPOI
786  * @tc.author: luqianfu
787  */
788 HWTEST_F(DistributeddbNbCreateTest, ManagerDb024, TestSize.Level0)
789 {
790     KvStoreDelegateManager *manager = nullptr;
791     KvStoreNbDelegate *result = nullptr;
792     Option option = g_option;
793     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
794     ASSERT_TRUE(manager != nullptr && result != nullptr);
795     /**
796      * @tc.steps: step1. delete the db that was not closed and check db document manually.
797      * @tc.expected: step1. delete failed and db document is still exist
798      */
799     if (!option.isMemoryDb) {
800         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), BUSY);
801     }
802 
803     EXPECT_TRUE(EndCaseDeleteDB(manager, result, STORE_ID_1, option.isMemoryDb));
804 }
805 
806 /*
807  * @tc.name: ManagerDb 025
808  * @tc.desc: verify that can delete db that was not open.
809  * @tc.type: FUNC
810  * @tc.require: SR000CCPOI
811  * @tc.author: luqianfu
812  */
813 HWTEST_F(DistributeddbNbCreateTest, ManagerDb025, TestSize.Level0)
814 {
815     KvStoreDelegateManager *manager = nullptr;
816     KvStoreNbDelegate *result = nullptr;
817     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
818     ASSERT_TRUE(manager != nullptr && result != nullptr);
819     EXPECT_EQ(manager->CloseKvStore(result), OK);
820     result = nullptr;
821     /**
822      * @tc.steps: step1. delete the db that was not open and check db document manually.
823      * @tc.expected: step1. delete success and db document is deleted.
824      */
825     if (!g_option.isMemoryDb) {
826         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
827     }
828     delete manager;
829     manager = nullptr;
830 }
831 
832 /*
833  * @tc.name: ManagerDb 026
834  * @tc.desc: verify that can't delete db that was not exist.
835  * @tc.type: FUNC
836  * @tc.require: SR000CCPOI
837  * @tc.author: luqianfu
838  */
839 HWTEST_F(DistributeddbNbCreateTest, ManagerDb026, TestSize.Level0)
840 {
841     KvStoreDelegateManager *manager = nullptr;
842     KvStoreNbDelegate *result = nullptr;
843 
844     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
845     ASSERT_TRUE(manager != nullptr && result != nullptr);
846     /**
847      * @tc.steps: step1. delete the storeid that is not exist and check document manually.
848      * @tc.expected: step1. return failed and db document has not changes.
849      */
850     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), NOT_FOUND);
851     EXPECT_EQ(manager->CloseKvStore(result), OK);
852     result = nullptr;
853     if (!g_option.isMemoryDb) {
854         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
855     }
856     delete manager;
857     manager = nullptr;
858 }
859 
860 /*
861  * @tc.name: ManagerDb 027
862  * @tc.desc: verify that can't delete db reduplicatedly.
863  * @tc.type: FUNC
864  * @tc.require: SR000CCPOI
865  * @tc.author: luqianfu
866  */
867 HWTEST_F(DistributeddbNbCreateTest, ManagerDb027, TestSize.Level0)
868 {
869     KvStoreDelegateManager *manager = nullptr;
870     KvStoreNbDelegate *result = nullptr;
871     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
872     ASSERT_TRUE(manager != nullptr && result != nullptr);
873     /**
874      * @tc.steps: step1. close the db.
875      * @tc.expected: step1. close success.
876      */
877     EXPECT_EQ(manager->CloseKvStore(result), OK);
878     result = nullptr;
879     /**
880      * @tc.steps: step2. delete the db first time and check db document manually.
881      * @tc.expected: step2. delete success and db document was deleted.
882      */
883     if (!g_option.isMemoryDb) {
884         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
885     }
886     /**
887      * @tc.steps: step2. delete the db the second time.
888      * @tc.expected: step2. delete failed and db document has no changes.
889      */
890     if (!g_option.isMemoryDb) {
891         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), NOT_FOUND);
892     }
893     delete manager;
894     manager = nullptr;
895 }
896 
897 /*
898  * @tc.name: ManagerDb 028
899  * @tc.desc: verify that can't open db that was deleted.
900  * @tc.type: FUNC
901  * @tc.require: SR000CCPOI
902  * @tc.author: luqianfu
903  */
904 HWTEST_F(DistributeddbNbCreateTest, ManagerDb028, TestSize.Level0)
905 {
906     KvStoreDelegateManager *manager = nullptr;
907     KvStoreNbDelegate *result = nullptr;
908 
909     DelegateMgrNbCallback delegateMgrCallback;
910     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction1
911         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
912 
913     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
914     ASSERT_TRUE(manager != nullptr && result != nullptr);
915     /**
916      * @tc.steps: step1. close the db normally.
917      * @tc.expected: step1. close success.
918      */
919     EXPECT_EQ(manager->CloseKvStore(result), OK);
920     result = nullptr;
921     /**
922      * @tc.steps: step2. delete the db normally.
923      * @tc.expected: step2. delete success.
924      */
925     if (!g_option.isMemoryDb) {
926         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
927     }
928     /**
929      * @tc.steps: step3. open the db that was deleted.
930      * @tc.expected: step3. opened failed.
931      */
932     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(g_option);
933     option.createIfNecessary = IS_NOT_NEED_CREATE;
934     manager->GetKvStore(STORE_ID_1, option, Nbfunction1);
935     EXPECT_NE(delegateMgrCallback.GetStatus(), OK);
936     KvStoreNbDelegate *delegate = const_cast<KvStoreNbDelegate*>(delegateMgrCallback.GetKvStore());
937     EXPECT_EQ(delegate, nullptr);
938     EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
939     result = nullptr;
940     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), NOT_FOUND);
941     delete manager;
942     manager = nullptr;
943 }
944 
945 /*
946  * @tc.name: ManagerDb 029
947  * @tc.desc: verify that can create db again after it was deleted.
948  * @tc.type: FUNC
949  * @tc.require: SR000CCPOI
950  * @tc.author: luqianfu
951  */
952 HWTEST_F(DistributeddbNbCreateTest, ManagerDb029, TestSize.Level0)
953 {
954     KvStoreDelegateManager *manager = nullptr;
955     KvStoreNbDelegate *result = nullptr;
956     DelegateMgrNbCallback delegateMgrCallback;
957     function<void(DBStatus, KvStoreNbDelegate*)> Nbfunction
958         = bind(&DelegateMgrNbCallback::Callback, &delegateMgrCallback, _1, _2);
959     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
960     ASSERT_TRUE(manager != nullptr && result != nullptr);
961     /**
962      * @tc.steps: step1. close db.
963      * @tc.expected: step1. close success.
964      */
965     EXPECT_EQ(manager->CloseKvStore(result), OK);
966     result = nullptr;
967     /**
968      * @tc.steps: step2. delete db .
969      * @tc.expected: step2. delete success.
970      */
971     if (!g_option.isMemoryDb) {
972         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
973     }
974     delete manager;
975     manager = nullptr;
976     /**
977      * @tc.steps: step2. create db after it is delete.
978      * @tc.expected: step2. create success.
979      */
980     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
981     ASSERT_TRUE(manager != nullptr && result != nullptr);
982     EXPECT_EQ(manager->CloseKvStore(result), OK);
983     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
984 
985     delete manager;
986     manager = nullptr;
987 }
988 
989 /*
990  * @tc.name: ManagerDb 030
991  * @tc.desc: verify that can delete the db success even if the deleting process is killed when it is deleting the db.
992  * @tc.type: FUNC
993  * @tc.require: SR000CCPOI
994  * @tc.author: luqianfu
995  */
996 HWTEST_F(DistributeddbNbCreateTest, ManagerDb030, TestSize.Level2)
997 {
998     /**
999      * @tc.steps: step1. start thread.
1000      * @tc.expected: step1. success.
1001      */
1002     std::thread th(WaitingDeletingDB);
1003     th.detach();
1004     std::this_thread::sleep_for(std::chrono::seconds(UNIQUE_SECOND));
1005 }
1006 
1007 /*
1008  * @tc.name: MemoryDb 001
1009  * @tc.desc: verify that can create memory db successfully.
1010  * @tc.type: FUNC
1011  * @tc.require: SR000CRAD8
1012  * @tc.author: fengxiaoyun
1013  */
1014 HWTEST_F(DistributeddbNbCreateTest, MemoryDb001, TestSize.Level0)
1015 {
1016     KvStoreDelegateManager *manager = nullptr;
1017     KvStoreNbDelegate *result = nullptr;
1018 
1019     /**
1020      * @tc.steps: step1. create memory db with isMemoryDb=true in SetKvStoreConfig.
1021      * @tc.expected: step1. create successfully.
1022      */
1023     Option option;
1024     option.createIfNecessary = IS_NEED_CREATE;
1025     option.isMemoryDb = true;
1026     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1027     ASSERT_TRUE(manager != nullptr && result != nullptr);
1028     fstream dbFile;
1029     dbFile.open((DistributedDBConstant::NB_DIRECTOR + "single_ver/main/gen_natural_store.*"), ios::in);
1030     if (!dbFile) {
1031         MST_LOG("The db file is not exist!");
1032     } else {
1033         dbFile.close();
1034     }
1035 
1036     /**
1037      * @tc.steps: step2. PutLocal(k1,v1) to MemoryDb.
1038      * @tc.expected: step2. operate successfully and GetLocal(k1)=v1.
1039      */
1040     EXPECT_EQ(result->PutLocal(KEY_1, VALUE_1), OK);
1041     Value valueResult;
1042     EXPECT_EQ(result->GetLocal(KEY_1, valueResult), OK);
1043     EXPECT_EQ(valueResult, VALUE_1);
1044     EXPECT_EQ(result->DeleteLocal(KEY_1), OK);
1045     EXPECT_EQ(result->GetLocal(KEY_1, valueResult), NOT_FOUND);
1046 
1047     /**
1048      * @tc.steps: step3. Put(k1,v1) to MemoryDb.
1049      * @tc.expected: step3. operate successfully and Get(k1)=v1.
1050      */
1051     EXPECT_EQ(result->Put(KEY_1, VALUE_1), OK);
1052     EXPECT_EQ(result->Get(KEY_1, valueResult), OK);
1053     EXPECT_EQ(valueResult, VALUE_1);
1054     EXPECT_EQ(result->Delete(KEY_1), OK);
1055     EXPECT_EQ(result->Get(KEY_1, valueResult), NOT_FOUND);
1056 
1057     /**
1058      * @tc.steps: step4. delete MemoryDb.
1059      * @tc.expected: step4. delete failed.
1060      */
1061     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), BUSY);
1062 
1063     /**
1064      * @tc.steps: step5. close MemoryDb.
1065      * @tc.expected: step5. close successfully.
1066      */
1067     EXPECT_EQ(manager->CloseKvStore(result), OK);
1068     result = nullptr;
1069     delete manager;
1070     manager = nullptr;
1071 }
1072 
ReleaseManager(KvStoreDelegateManager * & manager)1073 void ReleaseManager(KvStoreDelegateManager *&manager)
1074 {
1075     if (manager != nullptr) {
1076         delete manager;
1077         manager = nullptr;
1078     }
1079     return;
1080 }
1081 /*
1082  * @tc.name: MemoryDb 002
1083  * @tc.desc: verify that the differently configured databases(e.g. diskdb,memdb) won't affect each other.
1084  * @tc.type: FUNC
1085  * @tc.require: SR000CRAD8
1086  * @tc.author: fengxiaoyun
1087  */
1088 HWTEST_F(DistributeddbNbCreateTest, MemoryDb002, TestSize.Level0)
1089 {
1090     KvStoreDelegateManager *manager = nullptr;
1091     KvStoreNbDelegate *result1 = nullptr, *result2 = nullptr;
1092 
1093      /**
1094      * @tc.steps: step1. Open diskdb and put(k1,v1),putlocal(k1,v2) to diskdb. Then create memorydb with the same 3-
1095      *    params of opened diskdb.
1096      * @tc.expected: step1. open the diskdb successfully and failed to open the corresponding memdb.
1097      */
1098     Option option;
1099     option.createIfNecessary = IS_NEED_CREATE;
1100     option.isMemoryDb = false;
1101     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1102     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1103     EXPECT_EQ(result1->PutLocal(KEY_1, VALUE_2), OK);
1104     EXPECT_EQ(result1->Put(KEY_1, VALUE_1), OK);
1105     option.isMemoryDb = true;
1106     KvStoreDelegateManager *managerRes = nullptr;
1107     ASSERT_EQ(DistributedDBNbTestTools::GetNbDelegateSuccess(managerRes, g_dbParameter1, option), nullptr);
1108     DistributedDBNbTestTools::CloseNbAndRelease(manager, result1);
1109 
1110     /**
1111      * @tc.steps: step2. device A put(k2,v3),putlocal(k2,v2) to memory db and open disk db then close memory db.
1112      * @tc.expected: step2. put successfully and open disk db failed.
1113      */
1114     option.isMemoryDb = true;
1115     result2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1116     ASSERT_TRUE(manager != nullptr && result2 != nullptr);
1117     EXPECT_EQ(result2->PutLocal(KEY_2, VALUE_2), OK);
1118     EXPECT_EQ(result2->Put(KEY_2, VALUE_3), OK);
1119     option.isMemoryDb = false;
1120     ASSERT_EQ(DistributedDBNbTestTools::GetNbDelegateSuccess(managerRes, g_dbParameter1, option), nullptr);
1121     DistributedDBNbTestTools::CloseNbAndRelease(manager, result2);
1122 
1123      /**
1124      * @tc.steps: step3. device A Get(k1),Get(k2),GetLocal(k1),GetLocal(k2) in disk db.
1125      * @tc.expected: step3. Get(k1)=v1, Get(k2)=NOT_FOUND, GetLocal(k1)=v2, GetLocal(k2)=NOT_FOUND.
1126      */
1127     option.isMemoryDb = false;
1128     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1129     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1130     Value valueResult;
1131     EXPECT_EQ(result1->GetLocal(KEY_1, valueResult), OK);
1132     EXPECT_EQ(valueResult, VALUE_2);
1133     EXPECT_EQ(result1->GetLocal(KEY_2, valueResult), NOT_FOUND);
1134     EXPECT_EQ(result1->Get(KEY_1, valueResult), OK);
1135     EXPECT_EQ(valueResult, VALUE_1);
1136     EXPECT_EQ(result1->Get(KEY_2, valueResult), NOT_FOUND);
1137     DistributedDBNbTestTools::CloseNbAndRelease(manager, result1);
1138     /**
1139      * @tc.steps: step4. device A Get(k1),Get(k2),GetLocal(k1),GetLocal(k2) in memory db.
1140      * @tc.expected: step4. Get(k1)=NOT_FOUND, Get(k2)=NOT_FOUND, GetLocal(k1)=NOT_FOUND, GetLocal(k2)=NOT_FOUND.
1141      */
1142     option.isMemoryDb = true;
1143     result2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1144     ASSERT_TRUE(manager != nullptr && result2 != nullptr);
1145     EXPECT_EQ(result2->GetLocal(KEY_1, valueResult), NOT_FOUND);
1146     EXPECT_EQ(result2->GetLocal(KEY_2, valueResult), NOT_FOUND);
1147     EXPECT_EQ(result2->Get(KEY_1, valueResult), NOT_FOUND);
1148     EXPECT_EQ(result2->Get(KEY_2, valueResult), NOT_FOUND);
1149     EXPECT_EQ(manager->CloseKvStore(result2), OK);
1150     result2 = nullptr;
1151 
1152     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1153     result1 = nullptr;
1154     ReleaseManager(manager);
1155 }
1156 
1157 /*
1158  * @tc.name: MemoryDb 003
1159  * @tc.desc: verify that the memory db do not need path and can create db successfully.
1160  * @tc.type: FUNC
1161  * @tc.require: SR000CRAD8
1162  * @tc.author: wangyulong
1163  */
1164 HWTEST_F(DistributeddbNbCreateTest, MemoryDb003, TestSize.Level0)
1165 {
1166     /**
1167      * @tc.steps: step1. create memory db without path.
1168      * @tc.expected: step1. create successfully.
1169      */
1170     DelegateMgrNbCallback delegateMgrCallback;
1171     function<void(DBStatus, KvStoreNbDelegate*)> function = bind(&DelegateMgrNbCallback::Callback,
1172         &delegateMgrCallback, _1, _2);
1173     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(
1174         DistributedDBDataGenerator::APP_ID_1, DistributedDBDataGenerator::USER_ID_1);
1175     ASSERT_TRUE(manager != nullptr);
1176     EXPECT_TRUE(manager->SetKvStoreConfig({.dataDir = ""}));
1177     Option optionParam;
1178     optionParam.isMemoryDb = true;
1179     KvStoreNbDelegate::Option option = DistributedDBNbTestTools::TransferNbOptionType(optionParam);
1180     manager->GetKvStore(DistributedDBDataGenerator::STORE_ID_1, option, function);
1181     KvStoreNbDelegate* delegate = const_cast<KvStoreNbDelegate *>(delegateMgrCallback.GetKvStore());
1182     ASSERT_TRUE(delegate != nullptr);
1183 
1184     /**
1185      * @tc.steps: step2. put (k1, v1) to the memory db.
1186      * @tc.expected: step2. the memory db can be insert data successfully.
1187      */
1188     EXPECT_EQ(delegate->PutLocal(KEY_1, VALUE_1), OK);
1189     EXPECT_EQ(delegate->Put(KEY_1, VALUE_2), OK);
1190     Value localValue, syncValue;
1191     EXPECT_EQ(delegate->GetLocal(KEY_1, localValue), OK);
1192     EXPECT_EQ(delegate->Get(KEY_1, syncValue), OK);
1193     EXPECT_EQ(localValue, VALUE_1);
1194     EXPECT_EQ(syncValue, VALUE_2);
1195 
1196     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1197     delegate = nullptr;
1198     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), INVALID_ARGS);
1199 
1200     ReleaseManager(manager);
1201 }
1202 
1203 /*
1204  * @tc.name: OptionParam 001
1205  * @tc.desc: verify that will check the option parameter when create encrypted DB.
1206  * @tc.type: FUNC
1207  * @tc.require: SR000CQDT4
1208  * @tc.author: fengxiaoyun
1209  */
1210 HWTEST_F(DistributeddbNbCreateTest, OptionParam001, TestSize.Level0)
1211 {
1212     vector<KvStoreDelegateManager*> manager = {nullptr, nullptr, nullptr};
1213     vector<KvStoreNbDelegate*> result = {nullptr, nullptr, nullptr, nullptr, nullptr};
1214     Option option;
1215     vector<uint8_t> password;
1216 
1217     /**
1218      * @tc.steps: step1. isEncryptedDb=true, passwd=NULL, cipher=DEFAULT when create db.
1219      * @tc.expected: step1. create failed and return INVALID_ARGS.
1220      */
1221     option.isEncryptedDb = true;
1222     option.cipher = CipherType::DEFAULT;
1223     option.passwd = NULL_PASSWD_VECTOR;
1224     DBStatus status;
1225     result[INDEX_ZEROTH] = DistributedDBNbTestTools::GetNbDelegateStatus(manager[INDEX_ZEROTH],
1226         status, g_dbParameter1, option);
1227     EXPECT_EQ(result[INDEX_ZEROTH], nullptr);
1228     EXPECT_EQ(status, INVALID_ARGS);
1229 
1230     /**
1231      * @tc.steps: step2. isEncryptedDb=true, passwd=a......(100B) when create db.
1232      * @tc.expected: step2. create successfully and return OK.
1233      */
1234     password.assign(VALUE_ONE_HUNDRED_BYTE, 'a');
1235     option.passwd = password;
1236     result[INDEX_FIRST] = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_ZEROTH], g_dbParameter1, option);
1237     ASSERT_TRUE(manager[INDEX_ZEROTH] != nullptr && result[INDEX_FIRST] != nullptr);
1238     EXPECT_EQ(manager[INDEX_ZEROTH]->CloseKvStore(result[INDEX_FIRST]), OK);
1239     result[INDEX_FIRST] = nullptr;
1240 
1241     /**
1242      * @tc.steps: step3. isEncryptedDb=true, passwd=a......(128B), cipher=AES_256_GCM when create db.
1243      * @tc.expected: step3. create successfully and return OK.
1244      */
1245     password.clear();
1246     password.assign(BATCH_RECORDS, 'a');
1247     option.cipher = CipherType::AES_256_GCM;
1248     option.passwd = password;
1249     result[INDEX_SECOND] = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_FIRST], g_dbParameter2, option);
1250     ASSERT_TRUE(manager[INDEX_FIRST] != nullptr && result[INDEX_SECOND] != nullptr);
1251     EXPECT_EQ(manager[INDEX_FIRST]->CloseKvStore(result[INDEX_SECOND]), OK);
1252     result[INDEX_SECOND] = nullptr;
1253 
1254     /**
1255      * @tc.steps: step4. isEncryptedDb=true, passwd=a......(129B), cipher=DEFAULT when create db.
1256      * @tc.expected: step4. create failed and return INVALID_ARGS.
1257      */
1258     password.clear();
1259     password.assign(PASSWD_BYTE, 'a');
1260     option.cipher = CipherType::DEFAULT;
1261     option.passwd = password;
1262     result[INDEX_THIRD] = DistributedDBNbTestTools::GetNbDelegateStatus(manager[INDEX_SECOND],
1263         status, g_dbParameter3, option);
1264     EXPECT_EQ(result[INDEX_THIRD], nullptr);
1265     EXPECT_EQ(status, INVALID_ARGS);
1266 
1267     /**
1268      * @tc.steps: step5. isEncryptedDb=false, passwd=a......(129B), cipher=DEFAULT when create db.
1269      * @tc.expected: step5. create successfully and return OK.
1270      */
1271     option.isEncryptedDb = false;
1272     result[INDEX_FORTH] = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_SECOND], g_dbParameter3, option);
1273     ASSERT_TRUE(manager[INDEX_SECOND] != nullptr && result[INDEX_FORTH] != nullptr);
1274     EXPECT_EQ(manager[INDEX_SECOND]->CloseKvStore(result[INDEX_FORTH]), OK);
1275     result[INDEX_FORTH] = nullptr;
1276 
1277     EXPECT_EQ(manager[INDEX_ZEROTH]->DeleteKvStore(STORE_ID_1), OK);
1278     EXPECT_EQ(manager[INDEX_FIRST]->DeleteKvStore(STORE_ID_2), OK);
1279     EXPECT_EQ(manager[INDEX_SECOND]->DeleteKvStore(STORE_ID_3), OK);
1280     for (auto &item : manager) {
1281         if (item != nullptr) {
1282             delete item;
1283         }
1284     }
1285 }
1286 
1287 /*
1288  * @tc.name: OptionParam 002
1289  * @tc.desc: verify that will memDb can't be encrypted.
1290  * @tc.type: FUNC
1291  * @tc.require: SR000CQDT4
1292  * @tc.author: fengxiaoyun
1293  */
1294 HWTEST_F(DistributeddbNbCreateTest, OptionParam002, TestSize.Level0)
1295 {
1296     KvStoreDelegateManager *manager = nullptr;
1297     KvStoreNbDelegate *result = nullptr;
1298     Option option;
1299     vector<uint8_t> password;
1300 
1301     /**
1302      * @tc.steps: step1. isEncryptedDb=true, passwd=a......(100B), cipher=DEFAULT when create memdb.
1303      * @tc.expected: step1. create failed and return NOT_SUPPORT.
1304      */
1305     option.isEncryptedDb = true;
1306     option.isMemoryDb = true;
1307     option.cipher = CipherType::DEFAULT;
1308     password.assign(VALUE_ONE_HUNDRED_BYTE, 'a');
1309     option.passwd = password;
1310     DBStatus status;
1311     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager, status, g_dbParameter1, option);
1312     EXPECT_EQ(result, nullptr);
1313     EXPECT_EQ(status, NOT_SUPPORT);
1314 
1315     /**
1316      * @tc.steps: step2. isEncryptedDb=false, passwd=a......(100B) when create memdb.
1317      * @tc.expected: step2. create successfully and return OK.
1318      */
1319     option.isEncryptedDb = false;
1320     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1321     ASSERT_TRUE(manager != nullptr && result != nullptr);
1322 
1323     EXPECT_EQ(manager->CloseKvStore(result), OK);
1324     result = nullptr;
1325     delete manager;
1326     manager = nullptr;
1327 }
1328 
1329 /*
1330  * @tc.name: OptionParam 003
1331  * @tc.desc: verify that isEncryptedDb and passwd are consistent with the creation time can open an existing DB.
1332  * @tc.type: FUNC
1333  * @tc.require: SR000CQDT4
1334  * @tc.author: fengxiaoyun
1335  */
1336 HWTEST_F(DistributeddbNbCreateTest, OptionParam003, TestSize.Level0)
1337 {
1338     vector<KvStoreDelegateManager *>manager = {nullptr, nullptr, nullptr, nullptr};
1339     KvStoreNbDelegate *result = nullptr;
1340 
1341     /**
1342      * @tc.steps: step1. isEncryptedDb=true, passwd=p1, cipher=DEFAULT when create db1.
1343      * @tc.expected: step1. create successfully and return OK.
1344      */
1345     Option option1 = {true, false, true, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_1};
1346     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_ZEROTH], g_dbParameter1, option1);
1347     ASSERT_TRUE(manager[INDEX_ZEROTH] != nullptr && result != nullptr);
1348     EXPECT_EQ(manager[INDEX_ZEROTH]->CloseKvStore(result), OK);
1349     result = nullptr;
1350     /**
1351      * @tc.steps: step2. isEncryptedDb=false, passwd=p1 when open db1.
1352      * @tc.expected: step2. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1353      */
1354     Option option2 = {false, false, false, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_1};
1355     DBStatus status;
1356     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager[INDEX_FIRST], status, g_dbParameter1, option2);
1357     EXPECT_EQ(result, nullptr);
1358     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1359 
1360     /**
1361      * @tc.steps: step3. isEncryptedDb=true, passwd=p2 when open db1.
1362      * @tc.expected: step3. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1363      */
1364     Option option3 = {false, false, true, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_2};
1365     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager[INDEX_FIRST], status, g_dbParameter1, option3);
1366     EXPECT_EQ(result, nullptr);
1367     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1368 
1369     /**
1370      * @tc.steps: step4. isEncryptedDb=true, passwd=p1 when open db1.
1371      * @tc.expected: step4. open successfully and return OK.
1372      */
1373     Option option4 = {false, false, true, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_1};
1374     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_FIRST], g_dbParameter1, option4);
1375     ASSERT_TRUE(manager[INDEX_FIRST] != nullptr && result != nullptr);
1376     EXPECT_EQ(manager[INDEX_FIRST]->CloseKvStore(result), OK);
1377     result = nullptr;
1378 
1379     /**
1380      * @tc.steps: step5. isEncryptedDb=false, passwd=p1, cipher=DEFAULT when create db2.
1381      * @tc.expected: step5. create successfully and return OK.
1382      */
1383     Option option5 = {true, false, false, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_1};
1384     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_SECOND], g_dbParameter2, option5);
1385     ASSERT_TRUE(manager[INDEX_SECOND] != nullptr && result != nullptr);
1386     EXPECT_EQ(manager[INDEX_SECOND]->CloseKvStore(result), OK);
1387     result = nullptr;
1388 
1389     /**
1390      * @tc.steps: step6. isEncryptedDb=true, passwd=p1 when open db2.
1391      * @tc.expected: step6. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1392      */
1393     Option option6 = {false, false, true, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_1};
1394     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager[INDEX_THIRD], status, g_dbParameter2, option6);
1395     EXPECT_EQ(result, nullptr);
1396     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1397 
1398     /**
1399      * @tc.steps: step7. isEncryptedDb=false, passwd=p2 when open db2.
1400      * @tc.expected: step7. open successfully and return OK.
1401      */
1402     Option option7 = {false, false, false, DistributedDB::CipherType::DEFAULT, PASSWD_VECTOR_2};
1403     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager[INDEX_THIRD], g_dbParameter2, option7);
1404     ASSERT_TRUE(manager[INDEX_THIRD] != nullptr && result != nullptr);
1405     EXPECT_EQ(manager[INDEX_THIRD]->CloseKvStore(result), OK);
1406     result = nullptr;
1407     EXPECT_EQ(manager[INDEX_FIRST]->DeleteKvStore(STORE_ID_1), OK);
1408     EXPECT_EQ(manager[INDEX_THIRD]->DeleteKvStore(STORE_ID_2), OK);
1409     for (auto &item : manager) {
1410         if (item != nullptr) {
1411             delete item;
1412             item = nullptr;
1413         }
1414     }
1415 }
1416 
RekeyAndClose(KvStoreDelegateManager * & manager,KvStoreNbDelegate * & delegate,const DistributedDB::CipherPassword & reKeyPasswd,DistributedDB::DBStatus rekeyStatus,bool isEnd)1417 void RekeyAndClose(KvStoreDelegateManager *&manager, KvStoreNbDelegate *&delegate,
1418     const DistributedDB::CipherPassword &reKeyPasswd, DistributedDB::DBStatus rekeyStatus, bool isEnd)
1419 {
1420     EXPECT_EQ(delegate->Rekey(reKeyPasswd), rekeyStatus);
1421     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1422     delegate = nullptr;
1423     if (!isEnd) {
1424         ReleaseManager(manager);
1425     }
1426 }
1427 
1428 /*
1429  * @tc.name: RekeyDb 001
1430  * @tc.desc: verify that can switching a non-encrypted database to an encrypted database by Rekey.
1431  * @tc.type: FUNC
1432  * @tc.require: SR000CQDT4
1433  * @tc.author: fengxiaoyun
1434  */
1435 HWTEST_F(DistributeddbNbCreateTest, RekeyDb001, TestSize.Level1)
1436 {
1437     KvStoreDelegateManager *manager = nullptr;
1438     KvStoreNbDelegate *result = nullptr;
1439     Option option;
1440 
1441     /**
1442      * @tc.steps: step1. use Rekey to update passwd to p1 after create memdb and then close memdb.
1443      * @tc.expected: step1. the Rekey return NOT_SUPPORT.
1444      */
1445     option.isMemoryDb = true;
1446     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1447     ASSERT_TRUE(manager != nullptr && result != nullptr);
1448     RekeyAndClose(manager, result, g_passwd1, NOT_SUPPORT, false);
1449     /**
1450      * @tc.steps: step2. create unencrypted db, use Rekey to update its passwd to NULL,
1451      * then close and open without passwd.
1452      * @tc.expected: step2. operate successfully and can open db again without passwd.
1453      */
1454     option.isMemoryDb = false;
1455     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1456     ASSERT_TRUE(manager != nullptr && result != nullptr);
1457     RekeyAndClose(manager, result, NULL_PASSWD, OK, false);
1458     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1459     ASSERT_TRUE(manager != nullptr && result != nullptr);
1460     EXPECT_EQ(result->PutLocal(KEY_1, VALUE_1), OK);
1461     EXPECT_EQ(result->Put(KEY_1, VALUE_1), OK);
1462 
1463     /**
1464      * @tc.steps: step3. use Rekey to update db's passwd to p1=a......(100B), then close
1465      * and open again without passwd.
1466      * @tc.expected: step3. Rekey is ok, open db failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1467      */
1468     vector<uint8_t> passwordVector(VALUE_ONE_HUNDRED_BYTE, 'a');
1469     CipherPassword password;
1470     EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OK);
1471     RekeyAndClose(manager, result, password, OK, false);
1472     DBStatus status;
1473     EXPECT_EQ(DistributedDBNbTestTools::GetNbDelegateStatus(manager, status, g_dbParameter1, option), nullptr);
1474     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1475 
1476     /**
1477      * @tc.steps: step4. use p1 to open db and Get(k1), GetLocal(k1).
1478      * @tc.expected: step4. open successfully and Get(k1)=v1, GetLocal(k1)=v1.
1479      */
1480     option.isEncryptedDb = true;
1481     option.passwd = passwordVector;
1482     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1483     ASSERT_TRUE(manager != nullptr && result != nullptr);
1484     Value valueResult;
1485     EXPECT_EQ(result->GetLocal(KEY_1, valueResult), OK);
1486     EXPECT_EQ(valueResult, VALUE_1);
1487     EXPECT_EQ(result->Get(KEY_1, valueResult), OK);
1488     EXPECT_EQ(valueResult, VALUE_1);
1489 
1490     /**
1491      * @tc.steps: step5. use Rekey to update db's passwd to p2=passwordVector whose size is 128B, then close
1492      * and open again with p1.
1493      * @tc.expected: step5. Rekey is ok, open db failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1494      */
1495     passwordVector.assign(BATCH_RECORDS, 'a');
1496     EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OK);
1497     RekeyAndClose(manager, result, password, OK, false);
1498     EXPECT_EQ(DistributedDBNbTestTools::GetNbDelegateStatus(manager, status, g_dbParameter1, option), nullptr);
1499     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1500 
1501     /**
1502      * @tc.steps: step6. use p2 to open db and delete(k1), DeleteLocal(k1).
1503      * @tc.expected: step6. operate successfully.
1504      */
1505     option.passwd = passwordVector;
1506     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1507     ASSERT_TRUE(manager != nullptr && result != nullptr);
1508     EXPECT_EQ(result->Delete(KEY_1), OK);
1509     EXPECT_EQ(result->DeleteLocal(KEY_1), OK);
1510 
1511     /**
1512      * @tc.steps: step7. use Rekey to update db's passwd to p2=passwordVector whose size is 129B.
1513      * @tc.expected: step7. Rekey failed and return INVALID_ARGS.
1514      */
1515     passwordVector.assign(PASSWD_BYTE, 'a');
1516     EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OVERSIZE);
1517     EXPECT_EQ(manager->CloseKvStore(result), OK);
1518     result = nullptr;
1519     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1520     ReleaseManager(manager);
1521 }
1522 
1523 /*
1524  * @tc.name: RekeyDb 002
1525  * @tc.desc: verify that can change passwd of an encrypted database by Rekey.
1526  * @tc.type: FUNC
1527  * @tc.require: SR000CQDT4
1528  * @tc.author: fengxiaoyun
1529  */
1530 HWTEST_F(DistributeddbNbCreateTest, RekeyDb002, TestSize.Level1)
1531 {
1532     KvStoreDelegateManager *manager = nullptr;
1533     KvStoreNbDelegate *result = nullptr;
1534     Option option;
1535 
1536     /**
1537      * @tc.steps: step1. create encrypted db with p1=password@123.
1538      * @tc.expected: step1. create successfully.
1539      */
1540     option.isEncryptedDb = true;
1541     option.passwd = PASSWD_VECTOR_1;
1542     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1543     ASSERT_TRUE(manager != nullptr && result != nullptr);
1544 
1545     /**
1546      * @tc.steps: step2. use Rekey to update passwd to p1 and close db then open db again with unencrypted way.
1547      * @tc.expected: step2. the Rekey return OK, but open db failed.
1548      */
1549     EXPECT_EQ(result->Rekey(g_passwd1), OK);
1550     EXPECT_EQ(manager->CloseKvStore(result), OK);
1551     result = nullptr;
1552     ReleaseManager(manager);
1553     option.createIfNecessary = false;
1554     option.isEncryptedDb = false;
1555     DBStatus status;
1556     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager, status, g_dbParameter1, option);
1557     EXPECT_EQ(result, nullptr);
1558     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1559 
1560     /**
1561      * @tc.steps: step3. open db with passwd=p1 and put(k1,v1), putLocal(k2,v2).
1562      * @tc.expected: step3. operate successfully.
1563      */
1564     option.isEncryptedDb = true;
1565     option.passwd = PASSWD_VECTOR_1;
1566     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1567     ASSERT_TRUE(manager != nullptr && result != nullptr);
1568     EXPECT_EQ(result->PutLocal(KEY_2, VALUE_2), OK);
1569     EXPECT_EQ(result->Put(KEY_1, VALUE_1), OK);
1570 
1571     /**
1572      * @tc.steps: step4. use Rekey to update passwd to NULL and close db then open db with passwd=p1.
1573      * @tc.expected: step4. the Rekey return OK, but open db failed.
1574      */
1575     EXPECT_EQ(result->Rekey(NULL_PASSWD), OK);
1576     EXPECT_EQ(manager->CloseKvStore(result), OK);
1577     result = nullptr;
1578     ReleaseManager(manager);
1579     result = DistributedDBNbTestTools::GetNbDelegateStatus(manager, status, g_dbParameter1, option);
1580     EXPECT_EQ(result, nullptr);
1581     EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1582 
1583     /**
1584      * @tc.steps: step5. open db again with unencrypted way and Get(k1), GetLocal(k2).
1585      * @tc.expected: step5. open successfully and Get(k1)=v1, GetLocal(k2)=v2.
1586      */
1587     option.isEncryptedDb = false;
1588     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1589     ASSERT_TRUE(manager != nullptr && result != nullptr);
1590     Value valueResult;
1591     EXPECT_EQ(result->Get(KEY_1, valueResult), OK);
1592     EXPECT_EQ(valueResult, VALUE_1);
1593     EXPECT_EQ(result->GetLocal(KEY_2, valueResult), OK);
1594     EXPECT_EQ(valueResult, VALUE_2);
1595 
1596     /**
1597      * @tc.steps: step6. use Rekey to update db's passwd to p2=a......(129B).
1598      * @tc.expected: step6. Reksy failed and return INVALID_ARGS.
1599      */
1600     vector<uint8_t> passwordVector(PASSWD_BYTE, 'a');
1601     CipherPassword password;
1602     EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OVERSIZE);
1603 
1604     EXPECT_EQ(manager->CloseKvStore(result), OK);
1605     result = nullptr;
1606     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1607     ReleaseManager(manager);
1608 }
1609 
1610 #ifndef LOW_LEVEL_MEM_DEV
1611 /*
1612  * @tc.name: RekeyDb 003
1613  * @tc.desc: verify that do other operations during Rekey execution, the operation returns busy.
1614  * @tc.type: FUNC
1615  * @tc.require: SR000CQDT4
1616  * @tc.author: fengxiaoyun
1617  */
1618 HWTEST_F(DistributeddbNbCreateTest, RekeyDb003, TestSize.Level3)
1619 {
1620     KvStoreDelegateManager *manager = nullptr;
1621     KvStoreNbDelegate *result1 = nullptr;
1622     KvStoreNbDelegate *result2 = nullptr;
1623     Option option;
1624 
1625     /**
1626      * @tc.steps: step1. use Rekey to update passwd to p1=PASSWD_VECTOR_1.
1627      * @tc.expected: step1. create successfully.
1628      */
1629     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1630     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1631     std::vector<DistributedDB::Entry> entriesBatch;
1632     std::vector<DistributedDB::Key> allKeys;
1633     GenerateFixedRecords(entriesBatch, allKeys, NB_OPERATION_NUM, ONE_K_LONG_STRING, ONE_M_LONG_STRING);
1634     for (vector<Entry>::iterator iter = entriesBatch.begin(); iter != entriesBatch.end(); iter++) {
1635         EXPECT_EQ(result1->Put(iter->key, iter->value), OK);
1636     }
1637     bool rekeyFlag = false;
__anon64c7d7d60102() 1638     thread subThread([&result1, &rekeyFlag]() {
1639         DBStatus rekeyStatus = result1->Rekey(g_passwd1);
1640         EXPECT_TRUE(rekeyStatus == OK || rekeyStatus == BUSY);
1641         rekeyFlag = true;
1642         g_conditionNbVar.notify_all();
1643     });
1644     subThread.detach();
1645 
1646     /**
1647      * @tc.steps: step2. Call the GetKvstore interface when Rekey.
1648      * @tc.expected: step2. the GetKvstore return BUSY.
1649      */
1650     option.createIfNecessary = false;
1651     DBStatus status;
1652     KvStoreDelegateManager *managerRes = nullptr;
1653     std::this_thread::sleep_for(std::chrono::microseconds(WAIT_FOR_LONG_TIME));
1654     result2 = DistributedDBNbTestTools::GetNbDelegateStatus(managerRes, status, g_dbParameter1, option);
1655     EXPECT_TRUE(status == BUSY || status == OK);
1656     if (status == OK) {
1657         EXPECT_EQ(managerRes->CloseKvStore(result2), OK);
1658         result2 = nullptr;
1659         ReleaseManager(managerRes);
1660     }
1661 
1662     /**
1663      * @tc.steps: step3. put data to db when Rekey.
1664      * @tc.expected: step3. the put return BUSY.
1665      */
1666     status = result1->Put(KEY_1, VALUE_1);
1667     EXPECT_TRUE(status == BUSY || status == OK);
1668     std::mutex count;
1669     std::unique_lock<std::mutex> lck(count);
__anon64c7d7d60202null1670     g_conditionNbVar.wait(lck, [&] { return rekeyFlag; });
1671     EXPECT_EQ(manager->CloseKvStore(result1), OK);
1672     result1 = nullptr;
1673     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1674     ReleaseManager(manager);
1675 }
1676 #endif
1677 
1678 /*
1679  * @tc.name: RekeyDb 004
1680  * @tc.desc: verify that Rekey will return busy when there are multiple instances of the same KvStore.
1681  * @tc.type: FUNC
1682  * @tc.require: SR000CQDT4
1683  * @tc.author: fengxiaoyun
1684  */
1685 HWTEST_F(DistributeddbNbCreateTest, RekeyDb004, TestSize.Level0)
1686 {
1687     KvStoreDelegateManager *manager = nullptr;
1688     KvStoreNbDelegate *result1 = nullptr;
1689     KvStoreNbDelegate *result2 = nullptr;
1690     Option option;
1691 
1692     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1693     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1694     ReleaseManager(manager);
1695     /**
1696      * @tc.steps: step1. use GetKvstore to open another instances of the same KvStore.
1697      * @tc.expected: step1. open successfully.
1698      */
1699     option.createIfNecessary = false;
1700     result2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1701     ASSERT_TRUE(manager != nullptr && result2 != nullptr);
1702 
1703     /**
1704      * @tc.steps: step2. call Rekey.
1705      * @tc.expected: step2. Rekey returns BUSY.
1706      */
1707     EXPECT_EQ(result1->Rekey(g_passwd1), BUSY);
1708     EXPECT_EQ(manager->CloseKvStore(result1), OK);
1709     result1 = nullptr;
1710     EXPECT_EQ(manager->CloseKvStore(result2), OK);
1711     result2 = nullptr;
1712     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1713     ReleaseManager(manager);
1714 }
1715 
RunDbRekeyOne()1716 void RunDbRekeyOne()
1717 {
1718     KvStoreDelegateManager *manager1 = nullptr;
1719     KvStoreNbDelegate *result1 = nullptr;
1720     Option option;
1721     option.isEncryptedDb = true;
1722     option.passwd = PASSWD_VECTOR_1;
1723     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager1, g_dbParameter1, option);
1724     ASSERT_TRUE(manager1 != nullptr && result1 != nullptr);
1725     EXPECT_EQ(result1->Rekey(g_passwd2), OK);
1726     EXPECT_EQ(manager1->CloseKvStore(result1), OK);
1727     result1 = nullptr;
1728     EXPECT_EQ(manager1->DeleteKvStore(STORE_ID_1), OK);
1729     delete manager1;
1730     manager1 = nullptr;
1731 }
1732 
RunDbRekeyTwo()1733 void RunDbRekeyTwo()
1734 {
1735     KvStoreDelegateManager *manager2 = nullptr;
1736     KvStoreNbDelegate *result2 = nullptr;
1737     Option option;
1738     option.isEncryptedDb = true;
1739     option.passwd = PASSWD_VECTOR_2;
1740     result2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option);
1741     ASSERT_TRUE(manager2 != nullptr && result2 != nullptr);
1742     EXPECT_EQ(result2->Rekey(g_passwd1), OK);
1743     EXPECT_EQ(manager2->CloseKvStore(result2), OK);
1744     result2 = nullptr;
1745     EXPECT_EQ(manager2->DeleteKvStore(STORE_ID_2), OK);
1746     delete manager2;
1747     manager2 = nullptr;
1748 }
1749 
RunDbRekeyThree()1750 void RunDbRekeyThree()
1751 {
1752     KvStoreDelegateManager *manager3 = nullptr;
1753     KvStoreNbDelegate *result3 = nullptr;
1754     Option option;
1755     option.isEncryptedDb = true;
1756     option.passwd = PASSWD_VECTOR_1;
1757     result3 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager3, g_dbParameter3, option);
1758     ASSERT_TRUE(manager3 != nullptr && result3 != nullptr);
1759     EXPECT_EQ(result3->Rekey(NULL_PASSWD), OK);
1760     EXPECT_EQ(manager3->CloseKvStore(result3), OK);
1761     result3 = nullptr;
1762     EXPECT_EQ(manager3->DeleteKvStore(STORE_ID_3), OK);
1763     delete manager3;
1764     manager3 = nullptr;
1765 }
1766 
RunDbRekeyFour()1767 void RunDbRekeyFour()
1768 {
1769     KvStoreDelegateManager *manager4 = nullptr;
1770     KvStoreNbDelegate *result4 = nullptr;
1771     Option option;
1772     result4 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager4, g_dbParameter4, option);
1773     ASSERT_TRUE(manager4 != nullptr && result4 != nullptr);
1774     EXPECT_EQ(result4->Rekey(g_passwd1), OK);
1775     EXPECT_EQ(manager4->CloseKvStore(result4), OK);
1776     result4 = nullptr;
1777     EXPECT_EQ(manager4->DeleteKvStore(STORE_ID_4), OK);
1778     delete manager4;
1779     manager4 = nullptr;
1780 }
1781 
RunDbRekeyFive()1782 void RunDbRekeyFive()
1783 {
1784     KvStoreDelegateManager *manager5 = nullptr;
1785     KvStoreNbDelegate *result5 = nullptr;
1786     Option option;
1787     result5 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager5, g_dbParameter5, option);
1788     ASSERT_TRUE(manager5 != nullptr && result5 != nullptr);
1789     EXPECT_EQ(result5->Put(KEY_1, VALUE_1), OK);
1790     EXPECT_EQ(result5->Delete(KEY_1), OK);
1791     EXPECT_EQ(result5->Put(KEY_1, VALUE_2), OK);
1792     Value valueResult;
1793     EXPECT_EQ(result5->Get(KEY_1, valueResult), OK);
1794     EXPECT_EQ(valueResult, VALUE_2);
1795     EXPECT_EQ(manager5->CloseKvStore(result5), OK);
1796     result5 = nullptr;
1797     EXPECT_EQ(manager5->DeleteKvStore(STORE_ID_5), OK);
1798     delete manager5;
1799     manager5 = nullptr;
1800 }
1801 /*
1802  * @tc.name: RekeyDb 005
1803  * @tc.desc: verify that calling Rekey interfaces on different DBs does not affect each other.
1804  * @tc.type: FUNC
1805  * @tc.require: SR000CQDT4
1806  * @tc.author: fengxiaoyun
1807  */
1808 HWTEST_F(DistributeddbNbCreateTest, RekeyDb005, TestSize.Level1)
1809 {
1810     /**
1811      * @tc.steps: step1. create thread1 to create db1 with passwd=p1, call Rekey to update passwd to p2=PASSSWD_2.
1812      * @tc.expected: step1. operate successfully.
1813      */
1814     thread subThread1(RunDbRekeyOne);
1815 
1816     /**
1817      * @tc.steps: step2. create thread2 to create db2 with passwd=p2, call Rekey to update passwd to p1=PASSSWD_1.
1818      * @tc.expected: step2. operate successfully.
1819      */
1820     thread subThread2(RunDbRekeyTwo);
1821 
1822     /**
1823      * @tc.steps: step3. create thread3 to create db3 with passwd=p1, call Rekey to update passwd to NULL_PASSWD.
1824      * @tc.expected: step3. operate successfully.
1825      */
1826     thread subThread3(RunDbRekeyThree);
1827 
1828     /**
1829      * @tc.steps: step4. create thread4 to create db4 without passwd, call Rekey to make db to be encrypted.
1830      * @tc.expected: step4. operate successfully.
1831      */
1832     thread subThread4(RunDbRekeyFour);
1833 
1834     /**
1835      * @tc.steps: step5. create thread5 to create db5 without passwd, then CRUD data to db5.
1836      * @tc.expected: step5. operate successfully.
1837      */
1838     thread subThread5(RunDbRekeyFive);
1839     subThread1.join();
1840     subThread2.join();
1841     subThread3.join();
1842     subThread4.join();
1843     subThread5.join();
1844 }
1845 
1846 /*
1847  * @tc.name: SpaceManger 001
1848  * @tc.desc: verify that StoreID is empty or does not exist, calling the GetKvStoreDiskSize() interface failed.
1849  * @tc.type: FUNC
1850  * @tc.require: SR000CQDT4
1851  * @tc.author: fengxiaoyun
1852  */
1853 #ifdef NB_CREATE
1854 HWTEST_F(DistributeddbNbCreateTest, SpaceManger001, TestSize.Level0)
1855 {
1856     /**
1857      * @tc.steps: step1. create DB1.
1858      * @tc.expected: step1. create successfully.
1859      */
1860     KvStoreDelegateManager *manager = nullptr;
1861     KvStoreNbDelegate *result = nullptr;
1862     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, g_option);
1863     ASSERT_TRUE(manager != nullptr && result != nullptr);
1864     /**
1865      * @tc.steps: step2. call the GetKvStoreDiskSize() with storeId=null.
1866      * @tc.expected: step2. call failed and return INVALID_ARGS.
1867      */
1868     uint64_t dbSize = 0ul;
1869     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID, dbSize), INVALID_ARGS);
1870 
1871     /**
1872      * @tc.steps: step3. call the GetKvStoreDiskSize() with storeId=store_Id_2.
1873      * @tc.expected: step3. call failed and return NOT_FOUND.
1874      */
1875     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_2, dbSize), NOT_FOUND);
1876     EXPECT_EQ(manager->CloseKvStore(result), OK);
1877     result = nullptr;
1878     if (!g_option.isMemoryDb) {
1879         EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1880     }
1881     ReleaseManager(manager);
1882 }
1883 
1884 /*
1885  * @tc.name: SpaceManger 002
1886  * @tc.desc: verify that can calculate the space size normally with the existing databaseID.
1887  * @tc.type: FUNC
1888  * @tc.require: SR000CQDT4
1889  * @tc.author: fengxiaoyun
1890  */
1891 HWTEST_F(DistributeddbNbCreateTest, SpaceManger002, TestSize.Level2)
1892 {
1893     KvStoreDelegateManager *manager = nullptr;
1894     KvStoreNbDelegate *result = nullptr;
1895     KvStoreNbDelegate *result1 = nullptr;
1896     Option option;
1897     result = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1898     ASSERT_TRUE(manager != nullptr && result != nullptr);
1899     EXPECT_EQ(manager->CloseKvStore(result), OK);
1900     result = nullptr;
1901 
1902     /**
1903      * @tc.steps: step1. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1904      * @tc.expected: step1. call successfully and return dbSize1.
1905      */
1906     uint64_t dbSize1, dbSize2, dbSize3;
1907     dbSize1 = dbSize2 = dbSize3 = 0ul;
1908     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize1), OK);
1909     ReleaseManager(manager);
1910 
1911     /**
1912      * @tc.steps: step2. put 100 (keys,values) to db that every item's size = 1K.
1913      * @tc.expected: step2. operate successfully.
1914      */
1915     option.createIfNecessary = false;
1916     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1917     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1918 
1919     std::vector<DistributedDB::Entry> entriesBatch;
1920     std::vector<DistributedDB::Key> allKeys;
1921     GenerateFixedRecords(entriesBatch, allKeys, OPER_CNT_END, ONE_K_LONG_STRING, FOUR_M_LONG_STRING);
1922     for (vector<Entry>::iterator iter = entriesBatch.begin(); iter != entriesBatch.end(); iter++) {
1923         EXPECT_EQ(result1->Put(iter->key, iter->value), OK);
1924     }
1925 
1926     /**
1927      * @tc.steps: step3. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1928      * @tc.expected: step3. call successfully and return dbSize2, dbSize2 > dbsize1.
1929      */
1930     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize2), OK);
1931     EXPECT_TRUE(dbSize2 > dbSize1);
1932 
1933     /**
1934      * @tc.steps: step4. delete the 100 (keys,values) that inserted in step2.
1935      * @tc.expected: step4. operate successfully.
1936      */
1937     for (vector<Entry>::iterator iter = entriesBatch.begin(); iter != entriesBatch.end(); iter++) {
1938         EXPECT_EQ(result1->Delete(iter->key), OK);
1939     }
1940 
1941     /**
1942      * @tc.steps: step5. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1943      * @tc.expected: step5. call successfully and return dbSize3, dbSize3 < dbsize2 and dbSize3 != dbsize2.
1944      */
1945     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize3), OK);
1946     EXPECT_TRUE(dbSize2 > dbSize3);
1947     EXPECT_NE(dbSize3, dbSize1);
1948     EXPECT_EQ(manager->CloseKvStore(result1), OK);
1949     result1 = nullptr;
1950     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1951     ReleaseManager(manager);
1952 
1953     /**
1954      * @tc.steps: step6. create memoryDb with storeId=store_Id_2 and call the GetKvStoreDiskSize().
1955      * @tc.expected: step6. call successfully and return dbSize1 and dbSize1=0.
1956      */
1957     option.createIfNecessary = true;
1958     option.isMemoryDb = true;
1959     result1 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
1960     ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1961     uint64_t dbSize4 = 0ul;
1962     EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize4), OK);
1963     EXPECT_EQ(dbSize4, size_t(0));
1964     EXPECT_EQ(manager->CloseKvStore(result1), OK);
1965     result1 = nullptr;
1966     ReleaseManager(manager);
1967 }
1968 #endif
1969 }