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