• 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 
16 #include <gtest/gtest.h>
17 #include <thread>
18 
19 #include "db_common.h"
20 #include "db_constant.h"
21 #include "db_errno.h"
22 #include "distributeddb_data_generate_unit_test.h"
23 #include "distributeddb_tools_unit_test.h"
24 #include "log_print.h"
25 #include "platform_specific.h"
26 #include "process_system_api_adapter_impl.h"
27 #include "runtime_context.h"
28 #include "sqlite_single_ver_natural_store.h"
29 #include "storage_engine_manager.h"
30 #ifdef DB_DEBUG_ENV
31 #include "system_time.h"
32 #endif // DB_DEBUG_ENV
33 #include "kv_store_result_set_impl.h"
34 #include "kv_store_nb_delegate_impl.h"
35 #include "kv_virtual_device.h"
36 #include "virtual_communicator_aggregator.h"
37 
38 using namespace testing::ext;
39 using namespace DistributedDB;
40 using namespace DistributedDBUnitTest;
41 using namespace std;
42 
43 namespace {
44     // define some variables to init a KvStoreDelegateManager object.
45     KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
46     string g_testDir;
47     KvStoreConfig g_config;
48     Key g_keyPrefix = {'A', 'B', 'C'};
49     const int RESULT_SET_COUNT = 9;
50     const int RESULT_SET_INIT_POS = -1;
51     uint8_t g_testDict[RESULT_SET_COUNT] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
52 
53     // define the g_kvNbDelegateCallback, used to get some information when open a kv store.
54     DBStatus g_kvDelegateStatus = INVALID_ARGS;
55     KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
56 #ifndef OMIT_MULTI_VER
57     KvStoreDelegate *g_kvDelegatePtr = nullptr;
58 
59     // the type of g_kvDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
60     auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreDelegateCallback, placeholders::_1,
61         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr));
62 #endif // OMIT_MULTI_VER
63     const int OBSERVER_SLEEP_TIME = 100;
64     const int BATCH_PRESET_SIZE_TEST = 10;
65     const int DIVIDE_BATCH_PRESET_SIZE = 5;
66     const int VALUE_OFFSET = 5;
67 
68     const int DEFAULT_KEY_VALUE_SIZE = 10;
69 
70     const int CON_PUT_THREAD_NUM = 4;
71     const int PER_THREAD_PUT_NUM = 100;
72 
73     const std::string DEVICE_B = "deviceB";
74     const std::string DEVICE_C = "deviceC";
75     const std::string DEVICE_D = "deviceD";
76     VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
77     KvVirtualDevice *g_deviceB = nullptr;
78     KvVirtualDevice *g_deviceC = nullptr;
79     KvVirtualDevice *g_deviceD = nullptr;
80     VirtualSingleVerSyncDBInterface *g_syncInterfaceB = nullptr;
81     VirtualSingleVerSyncDBInterface *g_syncInterfaceC = nullptr;
82     VirtualSingleVerSyncDBInterface *g_syncInterfaceD = nullptr;
83 
84     // the type of g_kvNbDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
85     auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
86         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
87 
88     enum LockState {
89         UNLOCKED = 0,
90         LOCKED
91     };
92 
InitResultSet()93     void InitResultSet()
94     {
95         Key testKey;
96         Value testValue;
97         for (int i = 0; i < RESULT_SET_COUNT; i++) {
98             testKey.clear();
99             testValue.clear();
100             // set key
101             testKey = g_keyPrefix;
102             testKey.push_back(g_testDict[i]);
103             // set value
104             testValue.push_back(g_testDict[i]);
105             // insert entry
106             EXPECT_EQ(g_kvNbDelegatePtr->Put(testKey, testValue), OK);
107         }
108     }
109 
ReadResultSet(KvStoreResultSet * readResultSet)110     void ReadResultSet(KvStoreResultSet *readResultSet)
111     {
112         if (readResultSet == nullptr) {
113             return;
114         }
115         // index from 0 to 8(first to last)
116         for (int i = 0; i < RESULT_SET_COUNT; i++) {
117             Entry entry;
118             std::vector<uint8_t> cursorKey = g_keyPrefix;
119             cursorKey.push_back(g_testDict[i]);
120             std::vector<uint8_t> cursorValue;
121             cursorValue.push_back(g_testDict[i]);
122             EXPECT_TRUE(readResultSet->MoveToNext());
123             EXPECT_EQ(readResultSet->GetEntry(entry), OK);
124             EXPECT_EQ(entry.key, cursorKey);
125             EXPECT_EQ(entry.value, cursorValue);
126             EXPECT_TRUE(!readResultSet->IsBeforeFirst());
127             EXPECT_TRUE(!readResultSet->IsAfterLast());
128         }
129         // change index to 8(last)
130         EXPECT_EQ(readResultSet->GetPosition(), RESULT_SET_COUNT - 1);
131         EXPECT_TRUE(!readResultSet->IsFirst());
132         EXPECT_TRUE(readResultSet->IsLast());
133         EXPECT_TRUE(!readResultSet->IsBeforeFirst());
134         EXPECT_TRUE(!readResultSet->IsAfterLast());
135     }
136 
CheckResultSetValue(KvStoreResultSet * readResultSet,DBStatus errCode,int position)137     void CheckResultSetValue(KvStoreResultSet *readResultSet, DBStatus errCode, int position)
138     {
139         if (readResultSet == nullptr) {
140             return;
141         }
142         Entry entry;
143         EXPECT_EQ(readResultSet->GetPosition(), position);
144         EXPECT_EQ(readResultSet->GetEntry(entry), errCode);
145         if (errCode == OK) {
146             std::vector<uint8_t> cursorKey;
147             std::vector<uint8_t> cursorValue;
148             if (position > RESULT_SET_INIT_POS && position < RESULT_SET_COUNT) {
149                 uint8_t keyPostfix = g_testDict[position];
150                 // set key
151                 cursorKey = g_keyPrefix;
152                 cursorKey.push_back(keyPostfix);
153                 // set value
154                 cursorValue.push_back(keyPostfix);
155             }
156             // check key and value
157             EXPECT_EQ(entry.key, cursorKey);
158             EXPECT_EQ(entry.value, cursorValue);
159         }
160     }
161 
162     std::vector<Entry> g_entriesForConcurrency;
PutData(KvStoreNbDelegate * kvStore,int flag)163     void PutData(KvStoreNbDelegate *kvStore, int flag)
164     {
165         for (int i = 0; i < PER_THREAD_PUT_NUM; i++) {
166             int index = flag * PER_THREAD_PUT_NUM + i;
167             kvStore->Put(g_entriesForConcurrency[index].key, g_entriesForConcurrency[index].value);
168         }
169         LOGD("%dth put has been finished", flag);
170     }
171 
CheckDataTimestamp(const std::string & storeId)172     bool CheckDataTimestamp(const std::string &storeId)
173     {
174         std::string identifier = USER_ID + "-" + APP_ID + "-" + storeId;
175         std::string hashIdentifier = DBCommon::TransferHashString(identifier);
176         std::string identifierName = DBCommon::TransferStringToHex(hashIdentifier);
177         std::string storeDir = g_testDir + "/" + identifierName + "/" + DBConstant::SINGLE_SUB_DIR + "/" +
178             DBConstant::MAINDB_DIR + "/" + DBConstant::SINGLE_VER_DATA_STORE + DBConstant::DB_EXTENSION;
179         sqlite3 *db = nullptr;
180         EXPECT_EQ(sqlite3_open_v2(storeDir.c_str(), &db, SQLITE_OPEN_READWRITE, nullptr), SQLITE_OK);
181         if (db == nullptr) {
182             return false;
183         }
184 
185         std::string selectSQL = "select timestamp from sync_data order by rowid;";
186         sqlite3_stmt *statement = nullptr;
187         EXPECT_EQ(sqlite3_prepare(db, selectSQL.c_str(), -1, &statement, NULL), SQLITE_OK);
188         std::vector<int64_t> timeVect;
189         while (sqlite3_step(statement) == SQLITE_ROW) {
190             timeVect.push_back(sqlite3_column_int64(statement, 0));
191         }
192 
193         sqlite3_finalize(statement);
194         statement = nullptr;
195         (void)sqlite3_close_v2(db);
196         db = nullptr;
197         EXPECT_EQ(timeVect.size(), g_entriesForConcurrency.size());
198         bool resultCheck = true;
199         if (g_entriesForConcurrency.size() > 1) {
200             for (size_t i = 1; i < timeVect.size(); i++) {
201                 if (timeVect[i] <= timeVect[i - 1]) {
202                     resultCheck = false;
203                     break;
204                 }
205             }
206         }
207 
208         return resultCheck;
209     }
210 
211 class DistributedDBInterfacesNBDelegateTest : public testing::Test {
212 public:
213     static void SetUpTestCase(void);
214     static void TearDownTestCase(void);
215     void SetUp();
216     void TearDown();
217 };
218 
SetUpTestCase(void)219 void DistributedDBInterfacesNBDelegateTest::SetUpTestCase(void)
220 {
221     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
222     g_config.dataDir = g_testDir;
223     g_mgr.SetKvStoreConfig(g_config);
224     if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
225         LOGE("rm test db files error!");
226     }
227 
228     g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
229     ASSERT_TRUE(g_communicatorAggregator != nullptr);
230     RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
231 }
232 
TearDownTestCase(void)233 void DistributedDBInterfacesNBDelegateTest::TearDownTestCase(void)
234 {
235     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
236 
237     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
238 }
239 
SetUp(void)240 void DistributedDBInterfacesNBDelegateTest::SetUp(void)
241 {
242     DistributedDBToolsUnitTest::PrintTestCaseInfo();
243     g_kvDelegateStatus = INVALID_ARGS;
244     g_kvNbDelegatePtr = nullptr;
245 #ifndef OMIT_MULTI_VER
246     g_kvDelegatePtr = nullptr;
247 #endif // OMIT_MULTI_VER
248 }
249 
TearDown(void)250 void DistributedDBInterfacesNBDelegateTest::TearDown(void)
251 {
252     if (g_kvNbDelegatePtr != nullptr) {
253         g_mgr.CloseKvStore(g_kvNbDelegatePtr);
254         g_kvNbDelegatePtr = nullptr;
255     }
256     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
257 }
258 
259 /**
260   * @tc.name: CombineTest001
261   * @tc.desc: Test the NbDelegate for combined operation.
262   * @tc.type: FUNC
263   * @tc.require:
264   * @tc.author: huangnaigu
265   */
266 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CombineTest001, TestSize.Level1)
267 {
268     /**
269      * @tc.steps:step1. Get the nb delegate.
270      * @tc.expected: step1. Get results OK and non-null delegate.
271      */
272     KvStoreNbDelegate::Option option = {true, false, false};
273     g_mgr.GetKvStore("distributed_nb_delegate_test", option, g_kvNbDelegateCallback);
274     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
275     EXPECT_TRUE(g_kvDelegateStatus == OK);
276     Key key;
277     key = {'A', 'C', 'Q'};
278     Value value;
279     value = {'G', 'D', 'O'};
280     Value valueRead;
281     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
282     ASSERT_TRUE(observer != nullptr);
283     /**
284      * @tc.steps:step2. Register the non-null observer for the special key.
285      * @tc.expected: step2. Register results OK.
286      */
287     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
288     /**
289      * @tc.steps:step3. Put the local data.
290      * @tc.expected: step3. Put returns OK.
291      */
292     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(key, value), OK);
293     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
294     /**
295      * @tc.steps:step4. Check the local data.
296      * @tc.expected: step4. The get data is equal to the put data.
297      */
298     EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, valueRead), OK);
299     /**
300      * @tc.steps:step5. Delete the local data.
301      * @tc.expected: step5. Delete returns OK.
302      */
303     EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocal(key), OK);
304     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
305     /**
306      * @tc.steps:step6. Check the local data.
307      * @tc.expected: step6. Couldn't find the deleted data.
308      */
309     EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, valueRead), NOT_FOUND);
310     /**
311      * @tc.steps:step7. UnRegister the observer.
312      * @tc.expected: step7. Returns OK.
313      */
314     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
315     delete observer;
316     observer = nullptr;
317     Key key1;
318     key1 = {'D', 'B', 'N'};
319     Value value1;
320     value1 = {'P', 'D', 'G'};
321 
322     Key key2 = key1;
323     Value value2;
324     key2.push_back('U');
325     value2 = {'C'};
326     /**
327      * @tc.steps:step8. Put the data.
328      * @tc.expected: step8. Put returns OK.
329      */
330     EXPECT_EQ(g_kvNbDelegatePtr->Put(key1, value1), OK);
331     Value valueRead2;
332     /**
333      * @tc.steps:step9. Check the data.
334      * @tc.expected: step9. Getting the put data returns OK.
335      */
336     EXPECT_EQ(g_kvNbDelegatePtr->Get(key1, valueRead2), OK);
337     /**
338      * @tc.steps:step10. Put another data.
339      * @tc.expected: step10. Returns OK.
340      */
341     EXPECT_EQ(g_kvNbDelegatePtr->Put(key2, value2), OK);
342     std::vector<Entry> vect;
343     /**
344      * @tc.steps:step10. Get the batch data using the prefix key.
345      * @tc.expected: step10. Results OK and the batch data size is equal to the put data size.
346      */
347     EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(key1, vect), OK);
348     EXPECT_EQ(vect.size(), 2UL);
349     /**
350      * @tc.steps:step11. Delete one data.
351      * @tc.expected: step11. Results OK and couldn't get the deleted data.
352      */
353     EXPECT_EQ(g_kvNbDelegatePtr->Delete(key1), OK);
354     EXPECT_EQ(g_kvNbDelegatePtr->Get(key1, valueRead2), NOT_FOUND);
355 
356     LOGD("Close store");
357     /**
358      * @tc.steps:step12. Close the kv store.
359      * @tc.expected: step12. Results OK and delete successfully.
360      */
361     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
362     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_test"), OK);
363     g_kvNbDelegatePtr = nullptr;
364 }
365 
366 /**
367   * @tc.name: CombineTest002
368   * @tc.desc: Test the NbDelegate for combined operation, try to use GAUSSDB_RD.
369   * @tc.type: FUNC
370   * @tc.require:
371   * @tc.author: zhujinlin
372   */
373 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CombineTest002, TestSize.Level1)
374 {
375     /**
376      * @tc.steps:step1. Get the nb delegate.
377      * @tc.expected: step1. Get results OK and non-null delegate.
378      */
379     KvStoreNbDelegate::Option option = {true, false, false};
380     option.storageEngineType = GAUSSDB_RD;
381     g_mgr.GetKvStore("distributed_nb_delegate_test_rd", option, g_kvNbDelegateCallback);
382     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
383     EXPECT_TRUE(g_kvDelegateStatus == OK);
384     std::string keyStr("key");
385     Key key(keyStr.begin(), keyStr.end());
386     std::string valueStr("acd");
387     Value value(valueStr.begin(), valueStr.end());
388     Value valueRead;
389     /**
390      * @tc.steps:step2. Try to get the data before put.
391      * @tc.expected: step2. Get returns NOT_FOUND.
392      */
393     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
394     /**
395      * @tc.steps:step3. Put the local data.
396      * @tc.expected: step3. Returns OK.
397      */
398     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
399     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
400     /**
401      * @tc.steps:step4. Check the local data.
402      * @tc.expected: step4. The get data is equal to the put data.
403      */
404     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), OK);
405     /**
406      * @tc.steps:step5. Delete the local data.
407      * @tc.expected: step5. Delete return OK.
408      */
409     EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), OK);
410     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
411     /**
412      * @tc.steps:step6. Check the local data.
413      * @tc.expected: step6. Couldn't find the deleted data.
414      */
415     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
416     /**
417      * @tc.steps:step7. Close the kv store.
418      * @tc.expected: step7. Results OK and delete successfully.
419      */
420     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
421     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_test_rd"), OK);
422     g_kvNbDelegatePtr = nullptr;
423 }
424 
425 /**
426   * @tc.name: CombineTest003
427   * @tc.desc: Test the NbDelegate for combined operation, try to use GAUSSDB_RD and index type with hash.
428   * @tc.type: FUNC
429   * @tc.require:
430   * @tc.author: zhujinlin
431   */
432 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CombineTest003, TestSize.Level1)
433 {
434     /**
435      * @tc.steps:step1. Get the nb delegate.
436      * @tc.expected: step1. Get results OK and non-null delegate.
437      */
438     KvStoreNbDelegate::Option option = {true, false, false};
439     option.storageEngineType = GAUSSDB_RD;
440     option.rdconfig.type = HASH;
441     g_mgr.GetKvStore("distributed_nb_delegate_test_rd_combine_003", option, g_kvNbDelegateCallback);
442     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
443     EXPECT_TRUE(g_kvDelegateStatus == OK);
444     std::string keyStr("acd");
445     Key key(keyStr.begin(), keyStr.end());
446     std::string valueStr("acd");
447     Value value(valueStr.begin(), valueStr.end());
448     Value valueRead;
449     /**
450      * @tc.steps:step2. Try to get the data before put.
451      * @tc.expected: step2. Get returns NOT_FOUND.
452      */
453     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
454     /**
455      * @tc.steps:step3. Put the local data.
456      * @tc.expected: step3. Put returns OK.
457      */
458     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
459     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
460     /**
461      * @tc.steps:step4. Check the local data.
462      * @tc.expected: step4. The get data is equal to the put data.
463      */
464     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), OK);
465     /**
466      * @tc.steps:step5. Delete the local data.
467      * @tc.expected: step5. Delete returns OK.
468      */
469     EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), OK);
470     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
471     /**
472      * @tc.steps:step6. Check the local data.
473      * @tc.expected: step6. Couldn't find the deleted data.
474      */
475     EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
476     /**
477      * @tc.steps:step7. Close the kv store.
478      * @tc.expected: step7. Results OK and delete successfully.
479      */
480     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
481     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_test_rd_combine_003"), OK);
482     g_kvNbDelegatePtr = nullptr;
483 }
484 
485 /**
486   * @tc.name: CreateMemoryDb001
487   * @tc.desc: Create memory database after.
488   * @tc.type: FUNC
489   * @tc.require:
490   * @tc.author: sunpeng
491   */
492 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CreateMemoryDb001, TestSize.Level1)
493 {
494     /**
495      * @tc.steps: step1. Create Memory database by GetKvStore.
496      * @tc.expected: step1. Create successfully.
497      */
498     const KvStoreNbDelegate::Option option = {true, true};
499     g_mgr.SetKvStoreConfig(g_config);
500     g_mgr.GetKvStore("distributed_Memorykvstore_001", option, g_kvNbDelegateCallback);
501     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
502     EXPECT_TRUE(g_kvDelegateStatus == OK);
503     KvStoreNbDelegate *kvNbDelegatePtr001 = g_kvNbDelegatePtr;
504 
505     /**
506      * @tc.steps: step2. Duplicate create Memory database by GetKvStore.
507      * @tc.expected: step2. Duplicate create successfully.
508      */
509     g_mgr.GetKvStore("distributed_Memorykvstore_001", option, g_kvNbDelegateCallback);
510     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
511     EXPECT_TRUE(g_kvDelegateStatus == OK);
512     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
513 
514     /**
515      * @tc.steps: step3. Duplicate create Memory database by GetKvStore.
516      * @tc.expected: step3. Duplicate create successfully.
517      */
518     g_mgr.GetKvStore("distributed_Memorykvstore_002", option, g_kvNbDelegateCallback);
519     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
520     EXPECT_TRUE(g_kvDelegateStatus == OK);
521     KvStoreNbDelegate *kvNbDelegatePtr002 = g_kvNbDelegatePtr;
522 
523     g_mgr.CloseKvStore(kvNbDelegatePtr001);
524     g_mgr.CloseKvStore(kvNbDelegatePtr002);
525     g_kvNbDelegatePtr = nullptr;
526 }
527 
528 /**
529   * @tc.name: CreateMemoryDb002
530   * @tc.desc: The MemoryDB cannot be created or open, when the physical database has been opened
531   * @tc.type: FUNC
532   * @tc.require:
533   * @tc.author: sunpeng
534   */
535 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CreateMemoryDb002, TestSize.Level1)
536 {
537     KvStoreNbDelegate::Option option = {true, true};
538     /**
539      * @tc.steps: step1. Create SingleVer database by GetKvStore.
540      * @tc.expected: step1. Create database success.
541      */
542     g_mgr.GetKvStore("distributed_Memorykvstore_002", option, g_kvNbDelegateCallback);
543     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
544     EXPECT_TRUE(g_kvDelegateStatus == OK);
545     KvStoreNbDelegate *delegate1 = g_kvNbDelegatePtr;
546     g_kvNbDelegatePtr = nullptr;
547 
548     /**
549      * @tc.steps: step2. Create Memory database by GetKvStore.
550      * @tc.expected: step2. Create Memory database fail.
551      */
552     option.isMemoryDb = false;
553     g_mgr.GetKvStore("distributed_Memorykvstore_002", option, g_kvNbDelegateCallback);
554     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
555     EXPECT_TRUE(g_kvDelegateStatus != OK);
556     g_mgr.CloseKvStore(delegate1);
557     delegate1 = nullptr;
558 }
559 
560 #ifndef OMIT_MULTI_VER
561 /**
562   * @tc.name: CreateMemoryDb003
563   * @tc.desc: The physical database cannot be created or open, when the MemoryDB has been opened.
564   * @tc.type: FUNC
565   * @tc.require:
566   * @tc.author: sunpeng
567   */
568 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CreateMemoryDb003, TestSize.Level1)
569 {
570     /**
571      * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
572      * @tc.expected: step1. Get database success.
573      */
574     KvStoreDelegate::Option option;
575     g_mgr.GetKvStore("distributed_Memorykvstore_003", option, g_kvDelegateCallback);
576     ASSERT_TRUE(g_kvDelegatePtr != nullptr);
577     EXPECT_TRUE(g_kvDelegateStatus == OK);
578 
579     /**
580      * @tc.steps: step2. Create Memory database by GetKvStore.
581      * @tc.expected: step2. Create Memory database fail.
582      */
583     KvStoreNbDelegate::Option nbOption = {true, true};
584     g_mgr.GetKvStore("distributed_Memorykvstore_003", nbOption, g_kvNbDelegateCallback);
585     ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
586     EXPECT_TRUE(g_kvDelegateStatus != OK);
587     g_mgr.CloseKvStore(g_kvDelegatePtr);
588     g_kvDelegatePtr = nullptr;
589 }
590 #endif // OMIT_MULTI_VER
591 
592 /**
593   * @tc.name: OperMemoryDbData001
594   * @tc.desc: Operate memory database
595   * @tc.type: FUNC
596   * @tc.require:
597   * @tc.author: sunpeng
598   */
599 HWTEST_F(DistributedDBInterfacesNBDelegateTest, OperMemoryDbData001, TestSize.Level1)
600 {
601     /**
602      * @tc.steps: step1. Create Memory database by GetKvStore.
603      */
604     const KvStoreNbDelegate::Option option = {true, true};
605     g_mgr.GetKvStore("distributed_OperMemorykvstore_001", option, g_kvNbDelegateCallback);
606     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
607     EXPECT_TRUE(g_kvDelegateStatus == OK);
608 
609     /**
610      * @tc.steps: step2. Put (KEY_1,VALUE_1)(KEY_2,VALUE_2) to Memory database.
611      * @tc.expected: step2. Success.
612      */
613     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
614     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_2, VALUE_2), OK);
615 
616     /**
617      * @tc.steps: step3. Get (KEY_1,VALUE_1)(KEY_2,VALUE_2) to Memory database.
618      * @tc.expected: step3. Success.
619      */
620     Value readValueKey1;
621     Value readValueKey2;
622     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, readValueKey1), OK);
623     EXPECT_EQ(readValueKey1, VALUE_1);
624 
625     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_2, readValueKey2), OK);
626     EXPECT_EQ(readValueKey2, VALUE_2);
627 
628     /**
629      * @tc.steps: step4. Delete K1 from Memory database.
630      * @tc.expected: step4. Success.
631      */
632     EXPECT_EQ(g_kvNbDelegatePtr->Delete(KEY_1), OK);
633 
634     /**
635      * @tc.steps: step5. Get K1 from Memory database.
636      * @tc.expected: step5. NOT_FOUND.
637      */
638     readValueKey1.clear();
639     readValueKey2.clear();
640     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, readValueKey1), NOT_FOUND);
641 
642     /**
643      * @tc.steps: step6. Update K2 value from Memory database.
644      * @tc.expected: step6. Get the right value after the update.
645      */
646     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_2, VALUE_3), OK);
647     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_2, readValueKey2), OK);
648     EXPECT_EQ(readValueKey2, VALUE_3);
649     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
650     g_kvNbDelegatePtr = nullptr;
651 }
652 
653 /**
654   * @tc.name: CloseMemoryDb001
655   * @tc.desc: Operate memory database after reopen memory database
656   * @tc.type: FUNC
657   * @tc.require:
658   * @tc.author: sunpeng
659   */
660 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CloseMemoryDb001, TestSize.Level1)
661 {
662     /**
663      * @tc.steps: step1. Create Memory database by GetKvStore.
664      */
665     const KvStoreNbDelegate::Option option = {true, true};
666     g_mgr.SetKvStoreConfig(g_config);
667     g_mgr.GetKvStore("distributed_CloseMemorykvstore_001", option, g_kvNbDelegateCallback);
668     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
669     EXPECT_TRUE(g_kvDelegateStatus == OK);
670 
671     /**
672      * @tc.steps: step2/3. Put and get to Memory database.
673      * @tc.expected: step2/3. Success and the value is right.
674      */
675     Value readValue;
676     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
677     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, readValue), OK);
678     EXPECT_EQ(readValue, VALUE_1);
679 
680     /**
681      * @tc.steps: step4. Close the Memory database.
682      * @tc.expected: step4. Success.
683      */
684     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
685 
686     /**
687      * @tc.steps: step5. Reopen the Memory database.
688      * @tc.expected: step5. Success.
689      */
690     g_mgr.GetKvStore("distributed_CloseMemorykvstore_001", option, g_kvNbDelegateCallback);
691     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
692     EXPECT_TRUE(g_kvDelegateStatus == OK);
693 
694     /**
695      * @tc.steps: step6. Get the key1 which has been put into the Memory database.
696      * @tc.expected: step6. Return NOT_FOUND.
697      */
698     readValue.clear();
699     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, readValue), NOT_FOUND);
700     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
701     g_kvNbDelegatePtr = nullptr;
702 }
703 
704 /**
705   * @tc.name: ResultSetTest001
706   * @tc.desc: Test the NbDelegate for result set function.
707   * @tc.type: FUNC
708   * @tc.require:
709   * @tc.author: wumin
710   */
711 HWTEST_F(DistributedDBInterfacesNBDelegateTest, ResultSetTest001, TestSize.Level1)
712 {
713     /**
714      * @tc.steps: step1. initialize result set.
715      * @tc.expected: step1. Success.
716      */
717     KvStoreNbDelegate::Option option = {true, false, false};
718     g_mgr.GetKvStore("distributed_nb_delegate_result_set_test", option, g_kvNbDelegateCallback);
719     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
720     EXPECT_TRUE(g_kvDelegateStatus == OK);
721     InitResultSet();
722 
723     /**
724      * @tc.steps: step2. get entries using result set.
725      * @tc.expected: step2. Success.
726      */
727     KvStoreResultSet *readResultSet = nullptr;
728     EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(g_keyPrefix, readResultSet), OK);
729     ASSERT_TRUE(readResultSet != nullptr);
730     EXPECT_EQ(readResultSet->GetCount(), RESULT_SET_COUNT);
731 
732     /**
733      * @tc.steps: step3. result function check.
734      * @tc.expected: step3. Success.
735      */
736     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
737     // index from 0 to 8(first to last)
738     ReadResultSet(readResultSet);
739     // change index to 9(after last)
740     EXPECT_TRUE(!readResultSet->MoveToNext());
741     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
742     // change index to 8(last)
743     EXPECT_TRUE(readResultSet->MoveToPrevious());
744     CheckResultSetValue(readResultSet, OK, RESULT_SET_COUNT - 1);
745     // change index to 0(first)
746     EXPECT_TRUE(readResultSet->MoveToFirst());
747     CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 1);
748     // change index to 8(last)
749     EXPECT_TRUE(readResultSet->MoveToLast());
750     CheckResultSetValue(readResultSet, OK, RESULT_SET_COUNT - 1);
751     // move to -4: change index to -1
752     EXPECT_TRUE(!readResultSet->MoveToPosition(RESULT_SET_INIT_POS - 3));
753     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
754     // move to 10: change index to 9
755     EXPECT_TRUE(!readResultSet->MoveToPosition(RESULT_SET_COUNT + 1));
756     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
757     // change index to 2
758     EXPECT_TRUE(readResultSet->MoveToPosition(RESULT_SET_INIT_POS + 3));
759     CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 3);
760     // move 0: change index to 2
761     EXPECT_TRUE(readResultSet->Move(0));
762     CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 3);
763     // change index to 6
764     EXPECT_TRUE(readResultSet->Move(RESULT_SET_INIT_POS + 5));
765     CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 7);
766     // change index to 3
767     EXPECT_TRUE(readResultSet->Move(RESULT_SET_INIT_POS - 2));
768     CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 4);
769     // move -5: change index to -1
770     EXPECT_TRUE(!readResultSet->Move(-5));
771     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
772 
773     // move INT_MIN: change index to -1
774     EXPECT_TRUE(!readResultSet->Move(INT_MIN));
775     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
776 
777     EXPECT_TRUE(readResultSet->Move(5));
778     EXPECT_TRUE(!readResultSet->Move(INT_MAX));
779     CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
780 
781     /**
782      * @tc.steps: step4. clear the result set resource.
783      * @tc.expected: step4. Success.
784      */
785     EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(readResultSet), OK);
786     EXPECT_TRUE(readResultSet == nullptr);
787     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
788     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_result_set_test"), OK);
789     g_kvNbDelegatePtr = nullptr;
790 }
791 
792 /**
793   * @tc.name: PutBatchVerify001
794   * @tc.desc: This test case use to verify the putBatch interface function
795   * @tc.type: FUNC
796   * @tc.require:
797   * @tc.author: wumin
798   */
799 HWTEST_F(DistributedDBInterfacesNBDelegateTest, PutBatchVerify001, TestSize.Level1)
800 {
801     /**
802      * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
803      * @tc.expected: step1. Get database success.
804      */
805     const KvStoreNbDelegate::Option option = {true, true};
806     g_mgr.SetKvStoreConfig(g_config);
807     g_mgr.GetKvStore("distributed_PutBatchVerify_001", option, g_kvNbDelegateCallback);
808     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
809     EXPECT_TRUE(g_kvDelegateStatus == OK);
810 
811     /**
812      * @tc.steps: step2. Insert 10 records into database.
813      * @tc.expected: step2. Insert successfully.
814      */
815     vector<Entry> entries;
816     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
817         Entry entry;
818         entry.key.push_back(i);
819         entry.value.push_back(i);
820         entries.push_back(entry);
821     }
822 
823     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
824 
825     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
826         Key key;
827         key.push_back(i);
828         Value value;
829         g_kvNbDelegatePtr->Get(key, value);
830         EXPECT_EQ(key, value);
831     }
832 
833     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
834     g_kvNbDelegatePtr = nullptr;
835 }
836 
837 /**
838   * @tc.name: PutBatchVerify002
839   * @tc.desc: This test case use to verify the putBatch interface function while conn is nullptr
840   * @tc.type: FUNC
841   * @tc.require:
842   * @tc.author: caihaoting
843   */
844 HWTEST_F(DistributedDBInterfacesNBDelegateTest, PutBatchVerify002, TestSize.Level1)
845 {
846     /**
847      * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
848      * @tc.expected: step1. Get database success.
849      */
850     const KvStoreNbDelegate::Option option = {true, true};
851     g_mgr.SetKvStoreConfig(g_config);
852     g_mgr.GetKvStore("PutBatchVerify002", option, g_kvNbDelegateCallback);
853     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
854     EXPECT_TRUE(g_kvDelegateStatus == OK);
855 
856     /**
857      * @tc.steps: step2. Insert 10 records into database while conn is nullptr.
858      * @tc.expected: step2. DB_ERROR.
859      */
860     vector<Entry> entries;
861     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
862         Entry entry;
863         entry.key.push_back(i);
864         entry.value.push_back(i);
865         entries.push_back(entry);
866     }
867 
868     auto kvStoreImpl = static_cast<KvStoreNbDelegateImpl *>(g_kvNbDelegatePtr);
869     EXPECT_EQ(kvStoreImpl->Close(), OK);
870     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), DB_ERROR);
871 
872     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
873         Key key;
874         key.push_back(i);
875         Value value;
876         g_kvNbDelegatePtr->Get(key, value);
877         EXPECT_NE(key, value);
878     }
879 
880     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
881     g_kvNbDelegatePtr = nullptr;
882 }
883 
884 /**
885   * @tc.name: SingleVerPutBatch001
886   * @tc.desc: Check for illegal parameters
887   * @tc.type: FUNC
888   * @tc.require:
889   * @tc.author: sunpeng
890   */
891 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch001, TestSize.Level1)
892 {
893     /**
894      * @tc.steps: step1.
895      *  Create and construct three sets of vector <Entry>, each set of three data contains records:
896      *  (K1, V1) It is illegal for K1 to be greater than 1K, and V1 is 1K in size
897      *  (K2, V2) K2 is legal, V2 is greater than 4M
898      *  (K3, V3) are not legal.
899      */
900     Key illegalKey;
901     DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
902     Value illegalValue;
903     DistributedDBToolsUnitTest::GetRandomKeyValue(illegalValue, DBConstant::MAX_VALUE_SIZE + 1); // 4M + 1
904     vector<Entry> entrysKeyIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, VALUE_3}};
905     vector<Entry> entrysValueIllegal = {KV_ENTRY_1, KV_ENTRY_2, {KEY_3, illegalValue}};
906     vector<Entry> entrysIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, illegalValue}};
907 
908     const KvStoreNbDelegate::Option option = {true, false};
909     g_mgr.SetKvStoreConfig(g_config);
910     g_mgr.GetKvStore("distributed_SingleVerPutBatch_001", option, g_kvNbDelegateCallback);
911     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
912     EXPECT_TRUE(g_kvDelegateStatus == OK);
913     /**
914      * @tc.steps: step2. PutBatch operates on three sets of data.
915      * @tc.expected: step2. All three operations return INVALID_ARGS.
916      */
917     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysKeyIllegal), INVALID_ARGS);
918     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysValueIllegal), INVALID_ARGS);
919     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysIllegal), INVALID_ARGS);
920 
921     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
922     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_001"), OK);
923     g_kvNbDelegatePtr = nullptr;
924 }
925 
926 /**
927   * @tc.name: SingleVerPutBatch002
928   * @tc.desc: PutBatch normal insert function test.
929   * @tc.type: FUNC
930   * @tc.require:
931   * @tc.author: sunpeng
932   */
933 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch002, TestSize.Level1)
934 {
935     const KvStoreNbDelegate::Option option = {true, false};
936     g_mgr.SetKvStoreConfig(g_config);
937     g_mgr.GetKvStore("distributed_SingleVerPutBatch_002", option, g_kvNbDelegateCallback);
938     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
939     EXPECT_TRUE(g_kvDelegateStatus == OK);
940     /**
941      * @tc.steps: step1.
942      *  Create and build 4 groups of vector <Entry>, which are:
943      *  Vect of empty objects;
944      *  Vect1 of a legal Entry record;
945      *  128 legal Entry records Vect2;
946      *  129 legal Entry records Vect3;
947      */
948     vector<Entry> entrysMaxNumber;
949     for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
950         Entry entry;
951         entry.key.push_back(i);
952         entry.value.push_back(i);
953         entrysMaxNumber.push_back(entry);
954     }
955     Key keyTemp = {'1', '1'};
956     Value valueTemp;
957     Entry entryTemp = {keyTemp, VALUE_1};
958     vector<Entry> entrysOneRecord = {entryTemp};
959     vector<Entry> entrysOverSize = entrysMaxNumber;
960     entrysOverSize.push_back(entryTemp);
961     /**
962      * @tc.steps: step2. PutBatch operates on four sets of data. and use get check the result of Vect3.
963      * @tc.expected: step2. Returns OK for 129 records, and returns OK for the rest. all get return OK.
964      */
965     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysOverSize), OK); // 128 restrictions have been lifted
966     for (size_t i = 0; i < entrysOverSize.size(); i++) {
967         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysOverSize[i].key, valueTemp), OK);
968     }
969     /**
970      * @tc.steps: step3. Use get check the result of Vect2.
971      * @tc.expected: step3. Return OK and get the correct value.
972      */
973     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysOneRecord), OK);
974     EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueTemp), OK);
975     EXPECT_EQ(valueTemp, VALUE_1);
976     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMaxNumber), OK);
977      /**
978      * @tc.steps: step4. Use get check the result of Vect3.
979      * @tc.expected: step4. Return OK and get the correct value.
980      */
981     for (size_t i = 0; i < entrysMaxNumber.size(); i++) {
982         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[i].key, valueTemp), OK);
983         EXPECT_EQ(valueTemp, entrysMaxNumber[i].value);
984     }
985 
986     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
987     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_002"), OK);
988     g_kvNbDelegatePtr = nullptr;
989 }
990 
991 /**
992   * @tc.name: SingleVerPutBatch003
993   * @tc.desc: Check interface atomicity
994   * @tc.type: FUNC
995   * @tc.require:
996   * @tc.author: sunpeng
997   */
998 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch003, TestSize.Level1)
999 {
1000     const KvStoreNbDelegate::Option option = {true, false};
1001     g_mgr.SetKvStoreConfig(g_config);
1002     g_mgr.GetKvStore("distributed_SingleVerPutBatch_003", option, g_kvNbDelegateCallback);
1003     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1004     EXPECT_TRUE(g_kvDelegateStatus == OK);
1005     /**
1006      * @tc.steps: step1. Create and construct a set of vector <Entry> with a total of 128 data,
1007      * including one illegal data. And call PutBatch interface to insert.
1008      */
1009     vector<Entry> entrysMaxNumber;
1010     for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
1011         Entry entry;
1012         entry.key.push_back(i);
1013         entry.value.push_back(i);
1014         entrysMaxNumber.push_back(entry);
1015     }
1016     Key illegalKey;
1017     Value valueTemp;
1018     DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
1019     entrysMaxNumber[0].key = illegalKey;
1020 
1021     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMaxNumber), INVALID_ARGS);
1022     /**
1023      * @tc.steps: step2. Use Get interface to query 128 corresponding key values.
1024      * @tc.expected: step2. All Get interface return NOT_FOUND.
1025      */
1026     EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[0].key, valueTemp), INVALID_ARGS);
1027     for (size_t i = 1; i < entrysMaxNumber.size(); i++) {
1028         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[i].key, valueTemp), NOT_FOUND);
1029     }
1030     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1031     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_003"), OK);
1032     g_kvNbDelegatePtr = nullptr;
1033 }
1034 
PreparePutBatch004(vector<Entry> & entrys1,vector<Entry> & entrys2,vector<Entry> & entrys3)1035 static void PreparePutBatch004(vector<Entry> &entrys1, vector<Entry> &entrys2, vector<Entry> &entrys3)
1036 {
1037     const KvStoreNbDelegate::Option option = {true, false};
1038     g_mgr.SetKvStoreConfig(g_config);
1039     g_mgr.GetKvStore("distributed_SingleVerPutBatch_004", option, g_kvNbDelegateCallback);
1040     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1041     EXPECT_TRUE(g_kvDelegateStatus == OK);
1042 
1043     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1044         Entry entry;
1045         entry.key.push_back(i);
1046         entry.value.push_back(i);
1047         entrys1.push_back(entry);
1048     }
1049 
1050     for (int i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
1051         Entry entry;
1052         entry.key.push_back(i);
1053         entry.value.push_back(i + VALUE_OFFSET);
1054         entrys2.push_back(entry);
1055     }
1056 
1057     for (int i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
1058         Entry entry;
1059         entry.key.push_back(i);
1060         entry.value.push_back(i - VALUE_OFFSET);
1061         entrys3.push_back(entry);
1062     }
1063 }
1064 
1065 /**
1066   * @tc.name: SingleVerPutBatch004
1067   * @tc.desc: Check interface data insertion and update functions.
1068   * @tc.type: FUNC
1069   * @tc.require:
1070   * @tc.author: sunpeng
1071   */
1072 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch004, TestSize.Level1)
1073 {
1074     /**
1075      * @tc.steps: step1.
1076      *  Construct three groups of three vector <Entry>:
1077      *  (1) entrys1: key1 ~ 10, corresponding to Value1 ~ 10;
1078      *  (2) entrys2: key1 ~ 5, corresponding to Value6 ~ 10;
1079      *  (3) entrys3: key6 ~ 10, corresponding to Value1 ~ 5;
1080      */
1081     vector<Entry> entrys1;
1082     vector<Entry> entrys2;
1083     vector<Entry> entrys3;
1084     PreparePutBatch004(entrys1, entrys2, entrys3);
1085     /**
1086      * @tc.steps: step2. PutBatch entrys2.
1087      * @tc.expected: step2. PutBatch return OK.
1088      */
1089     Value valueRead;
1090     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys2), OK);
1091     /**
1092      * @tc.steps: step3. Check PutBatch result.
1093      * @tc.expected: step3. Get correct value of key1~5. Key6~10 return NOT_FOUND.
1094      */
1095     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1096         Key keyTemp;
1097         keyTemp.push_back(i);
1098         if (i < DIVIDE_BATCH_PRESET_SIZE) {
1099             Value valueTemp;
1100             valueTemp.push_back(i + VALUE_OFFSET);
1101             EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
1102             EXPECT_EQ(valueRead, valueTemp);
1103             continue;
1104         }
1105         EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), NOT_FOUND);
1106     }
1107     /**
1108      * @tc.steps: step4. PutBatch entrys1.
1109      * @tc.expected: step4. PutBatch return OK.
1110      */
1111     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys1), OK);
1112     /**
1113      * @tc.steps: step5. Check PutBatch result.
1114      * @tc.expected: step5. Update and insert value of key1~10 to value1~10.
1115      */
1116     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1117         Key keyTemp;
1118         keyTemp.push_back(i);
1119         if (i < DIVIDE_BATCH_PRESET_SIZE) {
1120             EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
1121             EXPECT_EQ(valueRead, keyTemp);
1122             continue;
1123         }
1124         EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
1125         EXPECT_EQ(valueRead, keyTemp);
1126     }
1127     /**
1128      * @tc.steps: step6. PutBatch entrys3.
1129      * @tc.expected: step6. PutBatch return OK.
1130      */
1131     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
1132     /**
1133      * @tc.steps: step7. Check PutBatch result of key1~10.
1134      * @tc.expected: step7. Update value of key5~10 to value1~5.
1135      */
1136     for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1137         Key keyTemp;
1138         keyTemp.push_back(i);
1139         if (i < DIVIDE_BATCH_PRESET_SIZE) {
1140             EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
1141             EXPECT_EQ(valueRead, keyTemp);
1142             continue;
1143         }
1144         Value valueTemp;
1145         valueTemp.push_back(i - VALUE_OFFSET);
1146         EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
1147         EXPECT_EQ(valueRead, valueTemp);
1148     }
1149 
1150     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1151     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_004"), OK);
1152     g_kvNbDelegatePtr = nullptr;
1153 }
1154 
CreatEntrys(int recordSize,vector<Key> & keys,vector<Value> & values,vector<Entry> & entries)1155 static void CreatEntrys(int recordSize, vector<Key> &keys, vector<Value> &values, vector<Entry> &entries)
1156 {
1157     keys.clear();
1158     values.clear();
1159     entries.clear();
1160     for (int i = 0; i < recordSize; i++) {
1161         string temp = to_string(i);
1162         Entry entry;
1163         Key keyTemp;
1164         Value valueTemp;
1165         for (auto &iter : temp) {
1166             entry.key.push_back(iter);
1167             entry.value.push_back(iter);
1168             keyTemp.push_back(iter);
1169             valueTemp.push_back(iter);
1170         }
1171         keys.push_back(keyTemp);
1172         values.push_back(valueTemp);
1173         entries.push_back(entry);
1174     }
1175 }
1176 
1177 #ifndef LOW_LEVEL_MEM_DEV
1178 /**
1179   * @tc.name: SingleVerPutBatch005
1180   * @tc.desc: Check for legal parameters that the sum size of all entries is smaller than 512M.
1181   * @tc.type: FUNC
1182   * @tc.require:
1183   * @tc.author: mazhao
1184   */
1185 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch005, TestSize.Level1)
1186 {
1187     /**
1188      * @tc.steps: step1.
1189      *  Create and construct two sets of vector <Entry>, each set of two data contains records:
1190      */
1191     Key legalKey;
1192     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1193     Value legalValue;
1194     DistributedDBToolsUnitTest::GetRandomKeyValue(legalValue, DBConstant::MAX_VALUE_SIZE); // 4M
1195     Value emptyValue; // 0k
1196     vector<Entry> entrysKeyLegal; // size is 512M - 1kB
1197     for (int i = 0; i < 524287; i++) { // 524287 * legalKey is equal to 512M - 1KB.
1198         entrysKeyLegal.push_back({legalKey, emptyValue});
1199     }
1200 
1201     vector<Entry> entrysMixLegal; // size is 511M + 511KB < 512M
1202     for (int i = 0; i < 127; i++) { // 127 * (legalValue + legalKey) is equal to 508M + 127KB < 512M.
1203         entrysMixLegal.push_back({legalKey, legalValue});
1204     }
1205 
1206     const KvStoreNbDelegate::Option option = {true, false};
1207     g_mgr.SetKvStoreConfig(g_config);
1208     g_mgr.GetKvStore("distributed_SingleVerPutBatch_005", option, g_kvNbDelegateCallback);
1209     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1210     EXPECT_TRUE(g_kvDelegateStatus == OK);
1211     /**
1212      * @tc.steps: step2. PutBatch operates on two sets of data.
1213      * @tc.expected: step2. two operations return OK.
1214      */
1215     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysKeyLegal), OK);
1216     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMixLegal), OK);
1217 
1218     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1219     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_005"), OK);
1220     g_kvNbDelegatePtr = nullptr;
1221 }
1222 
1223 /**
1224   * @tc.name: SingleVerPutBatch006
1225   * @tc.desc: Check for legal parameters that the sum size of all entries is equal to 512M.
1226   * @tc.type: FUNC
1227   * @tc.require:
1228   * @tc.author: mazhao
1229   */
1230 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch006, TestSize.Level1)
1231 {
1232     /**
1233      * @tc.steps: step1.
1234      *  Create and construct two sets of vector <Entry>, each set of two data contains records:
1235      */
1236     Key legalKey;
1237     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1238     Value legalValue;
1239     DistributedDBToolsUnitTest::GetRandomKeyValue(legalValue, DBConstant::MAX_VALUE_SIZE); // 4M
1240     Value emptyValue; // 0k
1241 
1242     vector<Entry> entrysKeyLegal; // size is 512M
1243     for (int i = 0; i < 524288; i++) { // 524288 * legalKey is equal to 512M.
1244         entrysKeyLegal.push_back({legalKey, emptyValue});
1245     }
1246 
1247     vector<Entry> entrysMixLegal; // size is 512M
1248     for (int i = 0; i < 127; i++) { // 127 * (legalValue + legalKey) is equal to 508M + 127KB < 512M.
1249         entrysMixLegal.push_back({legalKey, legalValue});
1250     }
1251     for (int i = 0; i < 3969; i++) { // 3969 * legalKey is equal to 3969KB.
1252         entrysMixLegal.push_back({legalKey, emptyValue});
1253     }
1254 
1255     const KvStoreNbDelegate::Option option = {true, false};
1256     g_mgr.SetKvStoreConfig(g_config);
1257     g_mgr.GetKvStore("distributed_SingleVerPutBatch_006", option, g_kvNbDelegateCallback);
1258     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1259     EXPECT_TRUE(g_kvDelegateStatus == OK);
1260     /**
1261      * @tc.steps: step2. PutBatch operates on two sets of data.
1262      * @tc.expected: step2. two operations return OK.
1263      */
1264     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysKeyLegal), OK);
1265     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMixLegal), OK);
1266 
1267     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1268     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_006"), OK);
1269     g_kvNbDelegatePtr = nullptr;
1270 }
1271 
1272 /**
1273   * @tc.name: SingleVerPutBatch007
1274   * @tc.desc: Check for illegal parameters that the sum size of all entries is larger to 512M.
1275   * @tc.type: FUNC
1276   * @tc.require:
1277   * @tc.author: mazhao
1278   */
1279 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch007, TestSize.Level1)
1280 {
1281     /**
1282      * @tc.steps: step1.
1283      *  Create and construct two sets of vector <Entry>, each set of two data contains records:
1284      */
1285     Key legalKey;
1286     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1287     Value legalValue;
1288     DistributedDBToolsUnitTest::GetRandomKeyValue(legalValue, DBConstant::MAX_VALUE_SIZE); // 4M
1289     Value emptyValue; // 0k
1290 
1291     vector<Entry> entrysKeyIllegal; // size is 512M + 1KB
1292     for (int i = 0; i < 524289; i++) { // 524289 * legalKey is equal to 512M + 1KB.
1293         entrysKeyIllegal.push_back({legalKey, emptyValue});
1294     }
1295 
1296     vector<Entry> entrysMixIllegal; // size is 512M + 1KB
1297     for (int i = 0; i < 127; i++) { // 127 * (legalValue + legalKey) is equal to 508M + 127KB < 512M.
1298         entrysMixIllegal.push_back({legalKey, legalValue});
1299     }
1300     for (int i = 0; i < 3970; i++) { // 3970 * legalKey is equal to 3970KB.
1301         entrysMixIllegal.push_back({legalKey, emptyValue});
1302     }
1303 
1304     const KvStoreNbDelegate::Option option = {true, false};
1305     g_mgr.SetKvStoreConfig(g_config);
1306     g_mgr.GetKvStore("distributed_SingleVerPutBatch_007", option, g_kvNbDelegateCallback);
1307     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1308     EXPECT_TRUE(g_kvDelegateStatus == OK);
1309     /**
1310      * @tc.steps: step2. PutBatch operates on two sets of data.
1311      * @tc.expected: step2. two operations return INVALID_ARGS.
1312      */
1313     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysKeyIllegal), INVALID_ARGS);
1314     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMixIllegal), INVALID_ARGS);
1315 
1316     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1317     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_007"), OK);
1318     g_kvNbDelegatePtr = nullptr;
1319 }
1320 
1321 /**
1322   * @tc.name: SingleVerPutBatch008
1323   * @tc.desc: Check for illegal parameters that the sum size of all entries excced uint32_t limit.
1324   * @tc.type: FUNC
1325   * @tc.require:
1326   * @tc.author: mazhao
1327   */
1328 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch008, TestSize.Level1)
1329 {
1330     /**
1331      * @tc.steps: step1.
1332      *  Create and construct two sets of vector <Entry>, each set of two data contains records:
1333      */
1334     Key legalKey;
1335     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1336     Value emptyValue; // 0k
1337 
1338     vector<Entry> entrysIllegal; // size excced to the limit of uint32_t
1339     for (int i = 0; i < 4194305; i++) { // 4194305 * legalKey is excced to the limit of uint32_t.
1340         entrysIllegal.push_back({legalKey, emptyValue});
1341     }
1342 
1343     const KvStoreNbDelegate::Option option = {true, false};
1344     g_mgr.SetKvStoreConfig(g_config);
1345     g_mgr.GetKvStore("SingleVerPutBatch008", option, g_kvNbDelegateCallback);
1346     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1347     EXPECT_TRUE(g_kvDelegateStatus == OK);
1348     /**
1349      * @tc.steps: step2. PutBatch operates on two sets of data.
1350      * @tc.expected: step2. two operations return INVALID_ARGS.
1351      */
1352     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysIllegal), INVALID_ARGS);
1353 
1354     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1355     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerPutBatch008"), OK);
1356     g_kvNbDelegatePtr = nullptr;
1357 }
1358 
1359 /**
1360   * @tc.name: SingleVerPutBatch009
1361   * @tc.desc: Check for illegal parameters
1362   * @tc.type: FUNC
1363   * @tc.require:
1364   * @tc.author: wangxiangdong
1365   */
1366 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatch009, TestSize.Level1)
1367 {
1368     /**
1369      * @tc.steps: step1.
1370      *  Create and construct three sets of vector <Entry>, each set of three data contains records:
1371      *  (K4, V4) are legal.
1372      *  (K5, V5) are not legal.
1373      */
1374     uint32_t maxValueSize = 64 * 1024 * 1024;
1375     Key legalKey;
1376     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1377     Value legalValue;
1378     DistributedDBToolsUnitTest::GetRandomKeyValue(legalValue, maxValueSize); // 64M
1379     Value illegalValue;
1380     DistributedDBToolsUnitTest::GetRandomKeyValue(illegalValue, maxValueSize + 1); // 64M + 1
1381     vector<Entry> entrysl = {KV_ENTRY_1, KV_ENTRY_2, {KEY_3, VALUE_3}};
1382     vector<Entry> entrys2 = {{KEY_4, legalValue}};
1383     vector<Entry> entrysIllegal = {{KEY_5, illegalValue}};
1384     /**
1385      * @tc.steps: step2.
1386      *  pragrma SET_MAX_VALUE_SIZE of legal and illegal value
1387      */
1388     const KvStoreNbDelegate::Option option = {true, false};
1389     g_mgr.SetKvStoreConfig(g_config);
1390     g_mgr.GetKvStore("distributed_SingleVerPutBatch_001", option, g_kvNbDelegateCallback);
1391     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1392     EXPECT_TRUE(g_kvDelegateStatus == OK);
1393     EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys2), INVALID_ARGS);
1394     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(KEY_4, legalValue), INVALID_ARGS);
1395     uint32_t illegalValueSize = 64 * 1024 * 1024 + 1;
1396     PragmaData input = static_cast<PragmaData>(&illegalValueSize);
1397     DBStatus status = g_kvNbDelegatePtr->Pragma(SET_MAX_VALUE_SIZE, input);
1398     EXPECT_TRUE(status == INVALID_ARGS);
1399     input = static_cast<PragmaData>(&maxValueSize);
1400     status = g_kvNbDelegatePtr->Pragma(SET_MAX_VALUE_SIZE, input);
1401     EXPECT_TRUE(status == OK);
1402     /**
1403      * @tc.steps: step3. PutBatch/PutLocalBatch/PublishLocal operates on three sets of data.
1404      * @tc.expected: step3. Three operations return OK or INVALID_ARGS.
1405      */
1406     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysl), OK);
1407     EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys2), OK);
1408     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(KEY_4, legalValue), OK);
1409     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys2), OK);
1410     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysIllegal), INVALID_ARGS);
1411     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysIllegal), INVALID_ARGS);
1412     EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysIllegal), INVALID_ARGS);
1413     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(KEY_5, illegalValue), INVALID_ARGS);
1414     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(KEY_6, legalValue), OK);
1415     EXPECT_EQ(g_kvNbDelegatePtr->PublishLocal(KEY_6, true, false, nullptr), OK);
1416 
1417     /**
1418      * @tc.steps: step4. Use Get to check data in database.
1419      * @tc.expected: step4. Get value by key successfully.
1420      */
1421     Value valueReadLocal;
1422     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_4, valueReadLocal), OK);
1423     Value valueRead;
1424     EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(KEY_4, valueRead), OK);
1425     EXPECT_EQ(valueRead, valueReadLocal);
1426     EXPECT_EQ(valueRead, legalValue);
1427 
1428     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1429     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_001"), OK);
1430     g_kvNbDelegatePtr = nullptr;
1431 }
1432 #endif // LOW_LEVEL_MEM_DEV
1433 
1434 /**
1435   * @tc.name: SingleVerDeleteBatch001
1436   * @tc.desc: Check for illegal parameters.
1437   * @tc.type: FUNC
1438   * @tc.require:
1439   * @tc.author: sunpeng
1440   */
1441 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch001, TestSize.Level1)
1442 {
1443     const KvStoreNbDelegate::Option option = {true, false};
1444     g_mgr.SetKvStoreConfig(g_config);
1445     g_mgr.GetKvStore("distributed_SingleVerPutBatch_001", option, g_kvNbDelegateCallback);
1446     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1447     EXPECT_TRUE(g_kvDelegateStatus == OK);
1448     /**
1449      * @tc.steps: step1. Create and construct a set of vector <Entry>, containing a total of 10 data keys1 ~ 10,
1450      *  Value1 ~ 10, and call Putbatch interface to insert data.
1451      * @tc.expected: step1. PutBatch successfully.
1452      */
1453     vector<Entry> entries;
1454     vector<Key> keys;
1455     vector<Value> values;
1456     Value valueRead;
1457     CreatEntrys(BATCH_PRESET_SIZE_TEST, keys, values, entries);
1458     vector<Entry> entrysBase = entries;
1459     vector<Key> keysBase = keys;
1460     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysBase), OK);
1461     /**
1462      * @tc.steps: step2. Use Get to check data in database.
1463      * @tc.expected: step2. Get value1~10 by key1~10 successfully.
1464      */
1465     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1466         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
1467     }
1468     /**
1469      * @tc.steps: step3. Use DeleteBatch interface to transfer 10 + 119 extra keys (total 129).
1470      * @tc.expected: step3. Return OK.
1471      */
1472     CreatEntrys(DBConstant::MAX_BATCH_SIZE + 1, keys, values, entries);
1473     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK); // 128 restrictions have been lifted
1474     /**
1475      * @tc.steps: step4. Use Get to check data in database.
1476      * @tc.expected: step4. Key1~10 still in database.
1477      */
1478     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1479         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), NOT_FOUND);
1480     }
1481     /**
1482      * @tc.steps: step5. Use the DeleteBatch interface to pass in 10 included
1483      *  keys6 ~ 10 + 123 additional key values ​​(128 in total).
1484      * @tc.expected: step5. DeleteBatch OK.
1485      */
1486     CreatEntrys(DBConstant::MAX_BATCH_SIZE + DIVIDE_BATCH_PRESET_SIZE, keys, values, entries);
1487     keys.erase(keys.begin(), keys.begin() + DIVIDE_BATCH_PRESET_SIZE);
1488     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
1489     /**
1490      * @tc.steps: step6. Use Get to check key1~10 in database.
1491      * @tc.expected: step6. Key1~5 in database, key6~10 have been deleted.
1492      */
1493     for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
1494         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), NOT_FOUND);
1495     }
1496     /**
1497      * @tc.steps: step7. Repeat Putbatch key1~10, value1~10.
1498      * @tc.expected: step7. Return OK.
1499      */
1500     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysBase), OK);
1501 
1502     Key illegalKey;
1503     DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
1504     keysBase.push_back(illegalKey);
1505     /**
1506      * @tc.steps: step8. Use DeleteBatch interface to pass in 10 + 1(larger than 1K) keys.
1507      * @tc.expected: step8. Return INVALID_ARGS.
1508      */
1509     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), INVALID_ARGS);
1510     /**
1511      * @tc.steps: step9. Use Get to check key1~10 in database.
1512      * @tc.expected: step9. Delete those data failed.
1513      */
1514     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1515         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
1516     }
1517     /**
1518      * @tc.steps: step10. Use DeleteBatch interface to pass in 10(in database) + 1 valid keys.
1519      * @tc.expected: step10. Delete those data successfully.
1520      */
1521     keysBase.back().erase(keysBase.back().begin(), keysBase.back().begin() + 1);
1522     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
1523     /**
1524      * @tc.steps: step11. Check data.
1525      * @tc.expected: step11. DeleteBatch successfully.
1526      */
1527     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1528         EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), NOT_FOUND);
1529     }
1530 
1531     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1532     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_001"), OK);
1533     g_kvNbDelegatePtr = nullptr;
1534 }
1535 
1536 /**
1537   * @tc.name: SingleVerDeleteBatch002
1538   * @tc.desc: Check normal delete batch ability.
1539   * @tc.type: FUNC
1540   * @tc.require:
1541   * @tc.author: sunpeng
1542   */
1543 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch002, TestSize.Level1)
1544 {
1545     const KvStoreNbDelegate::Option option = {true, false};
1546     g_mgr.SetKvStoreConfig(g_config);
1547     g_mgr.GetKvStore("distributed_SingleVerPutBatch_002", option, g_kvNbDelegateCallback);
1548     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1549     EXPECT_TRUE(g_kvDelegateStatus == OK);
1550     /**
1551      * @tc.steps: step1. Create a group of vector <Entry>, containing a total of 10 data keys1 ~ 10, Value1 ~ 10,
1552      *  call the Putbatch interface to insert data.
1553      * @tc.expected: step1. Insert to database successfully.
1554      */
1555     vector<Entry> entries;
1556     vector<Key> keysBase;
1557     vector<Value> values;
1558     CreatEntrys(BATCH_PRESET_SIZE_TEST, keysBase, values, entries);
1559 
1560     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
1561     /**
1562      * @tc.steps: step2. Check data.
1563      * @tc.expected: step2. Get key1~10 successfully.
1564      */
1565     Value valueRead;
1566     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1567         EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), OK);
1568     }
1569     /**
1570      * @tc.steps: step3. DeleteBatch key1~5.
1571      * @tc.expected: step3. Return OK.
1572      */
1573     vector<Key> keys(keysBase.begin(), keysBase.begin() + DIVIDE_BATCH_PRESET_SIZE);
1574     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
1575     /**
1576      * @tc.steps: step4. Check key1~10.
1577      * @tc.expected: step4. Key1~5 deleted, key6~10 existed.
1578      */
1579     for (size_t i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
1580         EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), NOT_FOUND);
1581     }
1582     for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
1583         EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), OK);
1584     }
1585     /**
1586      * @tc.steps: step5. DeleteBatch key1~10.
1587      * @tc.expected: step5. Return OK.
1588      */
1589     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
1590     /**
1591      * @tc.steps: step6. Check key1~10.
1592      * @tc.expected: step6. Key1~10 deleted successfully.
1593      */
1594     for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
1595         EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), NOT_FOUND);
1596     }
1597     /**
1598      * @tc.steps: step7. DeleteBatch key1~10 once again.
1599      * @tc.expected: step7. Return OK.
1600      */
1601     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
1602 
1603     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1604     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_002"), OK);
1605     g_kvNbDelegatePtr = nullptr;
1606 }
1607 
1608 #ifndef LOW_LEVEL_MEM_DEV
1609 /**
1610   * @tc.name: SingleVerDeleteBatch003
1611   * @tc.desc: Check for legal parameters that the sum size of all Keys is smaller than 512M.
1612   * @tc.type: FUNC
1613   * @tc.require:
1614   * @tc.author: mazhao
1615   */
1616 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch003, TestSize.Level1)
1617 {
1618     /**
1619      * @tc.steps: step1.
1620      *  Create and construct one sets of vector <Key>:
1621      */
1622     Key legalKey;
1623     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1624     vector<Key> keysLegal; // size is 512M - 1kB
1625     for (int i = 0; i < 524287; i++) { // 524287 * legalKey is equal to 512M - 1KB.
1626         keysLegal.push_back(legalKey);
1627     }
1628 
1629     const KvStoreNbDelegate::Option option = {true, false};
1630     g_mgr.SetKvStoreConfig(g_config);
1631     g_mgr.GetKvStore("SingleVerDeleteBatch003", option, g_kvNbDelegateCallback);
1632     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1633     EXPECT_TRUE(g_kvDelegateStatus == OK);
1634     /**
1635      * @tc.steps: step2. DeleteBatch operates on sets of data.
1636      * @tc.expected: step2. return OK.
1637      */
1638     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysLegal), OK);
1639 
1640     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1641     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerDeleteBatch003"), OK);
1642     g_kvNbDelegatePtr = nullptr;
1643 }
1644 
1645 /**
1646   * @tc.name: SingleVerDeleteBatch004
1647   * @tc.desc: Check for legal parameters that the sum size of all entries is equal to 512M.
1648   * @tc.type: FUNC
1649   * @tc.require:
1650   * @tc.author: mazhao
1651   */
1652 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch004, TestSize.Level1)
1653 {
1654     /**
1655      * @tc.steps: step1.
1656      *  Create and construct one sets of vector <Key>:
1657      */
1658     Key legalKey;
1659     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1660     vector<Key> keysLegal; // size is 512M
1661     for (int i = 0; i < 524288; i++) { // 524288 * legalKey is equal to 512M.
1662         keysLegal.push_back(legalKey);
1663     }
1664 
1665     const KvStoreNbDelegate::Option option = {true, false};
1666     g_mgr.SetKvStoreConfig(g_config);
1667     g_mgr.GetKvStore("SingleVerDeleteBatch004", option, g_kvNbDelegateCallback);
1668     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1669     EXPECT_TRUE(g_kvDelegateStatus == OK);
1670     /**
1671      * @tc.steps: step2. DeleteBatch operates on sets of data.
1672      * @tc.expected: step2. return OK.
1673      */
1674     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysLegal), OK);
1675 
1676     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1677     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerDeleteBatch004"), OK);
1678     g_kvNbDelegatePtr = nullptr;
1679 }
1680 
1681 /**
1682   * @tc.name: SingleVerDeleteBatch005
1683   * @tc.desc: Check for illegal parameters that the sum size of all entries is larger to 512M.
1684   * @tc.type: FUNC
1685   * @tc.require:
1686   * @tc.author: mazhao
1687   */
1688 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch005, TestSize.Level1)
1689 {
1690     /**
1691      * @tc.steps: step1.
1692      *  Create and construct one sets of vector <Key>:
1693      */
1694     Key legalKey;
1695     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1696     vector<Key> keysIllLegal; // size is 512M + 1kB
1697     for (int i = 0; i < 524289; i++) { // 524289 * legalKey is equal to 512M + 1KB.
1698         keysIllLegal.push_back(legalKey);
1699     }
1700 
1701     const KvStoreNbDelegate::Option option = {true, false};
1702     g_mgr.SetKvStoreConfig(g_config);
1703     g_mgr.GetKvStore("SingleVerDeleteBatch005", option, g_kvNbDelegateCallback);
1704     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1705     EXPECT_TRUE(g_kvDelegateStatus == OK);
1706     /**
1707      * @tc.steps: step2. DeleteBatch operates on sets of data.
1708      * @tc.expected: step2. return INVALID_ARGS.
1709      */
1710     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysIllLegal), INVALID_ARGS);
1711 
1712     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1713     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerDeleteBatch005"), OK);
1714     g_kvNbDelegatePtr = nullptr;
1715 }
1716 
1717 /**
1718   * @tc.name: SingleVerDeleteBatch006
1719   * @tc.desc: Check for illegal parameters that the sum size of all entries excced uint32_t limit.
1720   * @tc.type: FUNC
1721   * @tc.require:
1722   * @tc.author: mazhao
1723   */
1724 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch006, TestSize.Level1)
1725 {
1726     /**
1727      * @tc.steps: step1.
1728      *  Create and construct one sets of vector <Key>:
1729      */
1730     Key legalKey;
1731     DistributedDBToolsUnitTest::GetRandomKeyValue(legalKey, DBConstant::MAX_KEY_SIZE); // 1K
1732     vector<Key> keysIllLegal; // size excced to the limit of uint32_t
1733     for (int i = 0; i < 4194305; i++) { // 4194305 * legalKey is excced to the limit of uint32_t.
1734         keysIllLegal.push_back(legalKey);
1735     }
1736 
1737     const KvStoreNbDelegate::Option option = {true, false};
1738     g_mgr.SetKvStoreConfig(g_config);
1739     g_mgr.GetKvStore("SingleVerDeleteBatch006", option, g_kvNbDelegateCallback);
1740     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1741     EXPECT_TRUE(g_kvDelegateStatus == OK);
1742     /**
1743      * @tc.steps: step2. DeleteLocalBatch operates on sets of data.
1744      * @tc.expected: step2. return INVALID_ARGS.
1745      */
1746     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysIllLegal), INVALID_ARGS);
1747 
1748     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1749     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerDeleteBatch006"), OK);
1750     g_kvNbDelegatePtr = nullptr;
1751 }
1752 #endif // LOW_LEVEL_MEM_DEV
1753 
1754 /**
1755   * @tc.name: SingleVerDeleteBatch007
1756   * @tc.desc: Check normal delete batch while conn is nullptr.
1757   * @tc.type: FUNC
1758   * @tc.require:
1759   * @tc.author: caihaoting
1760   */
1761 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatch007, TestSize.Level1)
1762 {
1763     const KvStoreNbDelegate::Option option = {true, false};
1764     g_mgr.SetKvStoreConfig(g_config);
1765     g_mgr.GetKvStore("SingleVerDeleteBatch007", option, g_kvNbDelegateCallback);
1766     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1767     EXPECT_TRUE(g_kvDelegateStatus == OK);
1768     /**
1769      * @tc.steps: step1. Create a group of vector <Entry>, containing a total of 10 data keys1 ~ 10, Value1 ~ 10,
1770      *  call the Putbatch interface to insert data.
1771      * @tc.expected: step1. Insert to database successfully.
1772      */
1773     vector<Entry> entries;
1774     vector<Key> keysBase;
1775     vector<Value> values;
1776     CreatEntrys(BATCH_PRESET_SIZE_TEST, keysBase, values, entries);
1777 
1778     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
1779     /**
1780      * @tc.steps: step2. DeleteBatch operates on sets of data while conn is nullptr.
1781      * @tc.expected: step2. return DB_ERROR.
1782      */
1783     auto kvStoreImpl = static_cast<KvStoreNbDelegateImpl *>(g_kvNbDelegatePtr);
1784     EXPECT_EQ(kvStoreImpl->Close(), OK);
1785     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), DB_ERROR);
1786 
1787     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1788     EXPECT_EQ(g_mgr.DeleteKvStore("SingleVerDeleteBatch007"), OK);
1789     g_kvNbDelegatePtr = nullptr;
1790 }
1791 
1792 /**
1793   * @tc.name: SingleVerPutBatchObserver001
1794   * @tc.desc: Test the observer function of PutBatch() interface.
1795   * @tc.type: FUNC
1796   * @tc.require:
1797   * @tc.author: wumin
1798   */
1799 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatchObserver001, TestSize.Level1)
1800 {
1801     /**
1802      * @tc.steps:step1. Get the nb delegate.
1803      * @tc.expected: step1. Get results OK and non-null delegate.
1804      */
1805     KvStoreNbDelegate::Option option = {true, false, false};
1806     g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_001", option, g_kvNbDelegateCallback);
1807     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1808     EXPECT_TRUE(g_kvDelegateStatus == OK);
1809 
1810     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1811     ASSERT_TRUE(observer != nullptr);
1812     /**
1813      * @tc.steps:step2. Register the non-null observer for the special key.
1814      * @tc.expected: step2. Register results OK.
1815      */
1816     Key key;
1817     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1818     // register same observer twice will return already_set
1819     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), ALREADY_SET);
1820     /**
1821      * @tc.steps:step3. Put batch data.
1822      * @tc.expected: step3. Returns OK.
1823      */
1824     vector<Entry> entrysBase;
1825     vector<Key> keysBase;
1826     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST + 1, entrysBase, keysBase);
1827 
1828     vector<Entry> entries(entrysBase.begin(), entrysBase.end() - 1);
1829     EXPECT_EQ(entries.size(), 10UL);
1830     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
1831     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1832     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
1833     /**
1834      * @tc.steps:step4. Delete the batch data.
1835      * @tc.expected: step4. Returns OK.
1836      */
1837     vector<Key> keys(keysBase.begin() + 5, keysBase.end());
1838     EXPECT_EQ(keys.size(), 6UL);
1839     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
1840     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1841     vector<Entry> entrysDel(entrysBase.begin() + 5, entrysBase.end() - 1);
1842     EXPECT_EQ(entrysDel.size(), 5UL);
1843     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysDel, observer->GetEntriesDeleted()));
1844     /**
1845      * @tc.steps:step5. UnRegister the observer.
1846      * @tc.expected: step5. Returns OK.
1847      */
1848     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1849     delete observer;
1850     observer = nullptr;
1851     /**
1852      * @tc.steps:step6. Close the kv store.
1853      * @tc.expected: step6. Results OK and delete successfully.
1854      */
1855     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1856     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_001"), OK);
1857     g_kvNbDelegatePtr = nullptr;
1858 }
1859 
1860 /**
1861   * @tc.name: SingleVerPutBatchObserver002
1862   * @tc.desc: Test the observer function of PutBatch() for invalid input.
1863   * @tc.type: FUNC
1864   * @tc.require:
1865   * @tc.author: wumin
1866   */
1867 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatchObserver002, TestSize.Level4)
1868 {
1869     /**
1870      * @tc.steps:step1. Get the nb delegate.
1871      * @tc.expected: step1. Get results OK and non-null delegate.
1872      */
1873     KvStoreNbDelegate::Option option = {true, false, false};
1874     g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_002", option, g_kvNbDelegateCallback);
1875     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1876     EXPECT_TRUE(g_kvDelegateStatus == OK);
1877 
1878     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1879     ASSERT_TRUE(observer != nullptr);
1880     /**
1881      * @tc.steps:step2. Register the non-null observer for the special key.
1882      * @tc.expected: step2. Register results OK.
1883      */
1884     Key key;
1885     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1886     /**
1887      * @tc.steps:step3. Put invalid batch data.
1888      * @tc.expected: step3. Returns INVALID_ARGS.
1889      */
1890     vector<Entry> entrys2;
1891     vector<Key> keys2;
1892     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2);
1893     EXPECT_EQ(entrys2.size(), 10UL);
1894 
1895     vector<Entry> entrysInvalid;
1896     vector<Key> keysInvalid;
1897     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysInvalid, keysInvalid,
1898         DBConstant::MAX_KEY_SIZE + 10);
1899     EXPECT_EQ(entrysInvalid.size(), 10UL);
1900     entrys2[0].key = entrysInvalid[0].key;
1901 
1902     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys2), INVALID_ARGS);
1903     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1904     EXPECT_TRUE(observer->GetEntriesInserted().empty());
1905     /**
1906      * @tc.steps:step4. Put MAX valid value batch data.
1907      * @tc.expected: step4. Returns OK.
1908      */
1909     vector<Entry> entrys3;
1910     vector<Key> keys3;
1911 
1912     DistributedDBUnitTest::GenerateRecords(DBConstant::MAX_BATCH_SIZE, entrys3, keys3);
1913     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
1914     LOGD("sleep begin");
1915     // sleep 20 seconds
1916     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME * 10));
1917     LOGD("sleep end");
1918     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys3, observer->GetEntriesInserted()));
1919     /**
1920      * @tc.steps:step5. UnRegister the observer.
1921      * @tc.expected: step5. Returns OK.
1922      */
1923     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1924     delete observer;
1925     observer = nullptr;
1926     /**
1927      * @tc.steps:step6. Close the kv store.
1928      * @tc.expected: step6. Results OK and delete successfully.
1929      */
1930     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1931     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_002"), OK);
1932     g_kvNbDelegatePtr = nullptr;
1933 }
1934 
1935 /**
1936   * @tc.name: SingleVerPutBatchObserver003
1937   * @tc.desc: Test the observer function of PutBatch() update function.
1938   * @tc.type: FUNC
1939   * @tc.require:
1940   * @tc.author: wumin
1941   */
1942 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatchObserver003, TestSize.Level1)
1943 {
1944     /**
1945      * @tc.steps:step1. Get the nb delegate.
1946      * @tc.expected: step1. Get results OK and non-null delegate.
1947      */
1948     KvStoreNbDelegate::Option option = {true, false, false};
1949     g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_003", option, g_kvNbDelegateCallback);
1950     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1951     EXPECT_TRUE(g_kvDelegateStatus == OK);
1952 
1953     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1954     ASSERT_TRUE(observer != nullptr);
1955     /**
1956      * @tc.steps:step2. Register the non-null observer for the special key.
1957      * @tc.expected: step2. Register results OK.
1958      */
1959     Key key;
1960     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1961     /**
1962      * @tc.steps:step3. Put batch data.
1963      * @tc.expected: step3. Returns OK.
1964      */
1965     vector<Entry> entrysAdd;
1966     vector<Key> keysAdd;
1967     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysAdd, keysAdd);
1968 
1969     EXPECT_EQ(entrysAdd.size(), 10UL);
1970     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysAdd), OK);
1971     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1972     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysAdd, observer->GetEntriesInserted()));
1973     /**
1974      * @tc.steps:step4. Update the batch data.
1975      * @tc.expected: step4. Returns OK.
1976      */
1977     vector<Entry> entrysUpdate;
1978     vector<Key> keysUpdate;
1979     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysUpdate, keysUpdate, DEFAULT_KEY_VALUE_SIZE,
1980         DEFAULT_KEY_VALUE_SIZE + 10);
1981 
1982     EXPECT_EQ(entrysUpdate.size(), 10UL);
1983     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysUpdate), OK);
1984     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1985     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysUpdate, observer->GetEntriesUpdated()));
1986     /**
1987      * @tc.steps:step5. UnRegister the observer.
1988      * @tc.expected: step5. Returns OK.
1989      */
1990     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1991     delete observer;
1992     observer = nullptr;
1993     /**
1994      * @tc.steps:step6. Close the kv store.
1995      * @tc.expected: step6. Results OK and delete successfully.
1996      */
1997     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1998     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_003"), OK);
1999     g_kvNbDelegatePtr = nullptr;
2000 }
2001 
2002 /**
2003   * @tc.name: SingleVerPutBatchObserver004
2004   * @tc.desc: Test the observer function of PutBatch(), same keys handle.
2005   * @tc.type: FUNC
2006   * @tc.require:
2007   * @tc.author: wumin
2008   */
2009 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerPutBatchObserver004, TestSize.Level1)
2010 {
2011     /**
2012      * @tc.steps:step1. Get the nb delegate.
2013      * @tc.expected: step1. Get results OK and non-null delegate.
2014      */
2015     KvStoreNbDelegate::Option option = {true, false, false};
2016     g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_004", option, g_kvNbDelegateCallback);
2017     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2018     EXPECT_TRUE(g_kvDelegateStatus == OK);
2019 
2020     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
2021     ASSERT_TRUE(observer != nullptr);
2022     /**
2023      * @tc.steps:step2. Register the non-null observer for the special key.
2024      * @tc.expected: step2. Register results OK.
2025      */
2026     Key key;
2027     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
2028     /**
2029      * @tc.steps:step3. Put batch data.
2030      * @tc.expected: step3. Returns OK.
2031      */
2032     vector<Entry> entrys1;
2033     vector<Key> keys1;
2034     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys1, keys1);
2035     vector<Entry> entrys2;
2036     vector<Key> keys2;
2037     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2, DEFAULT_KEY_VALUE_SIZE,
2038         DEFAULT_KEY_VALUE_SIZE + 10);
2039     entrys1.insert(entrys1.end(), entrys2.begin(), entrys2.end());
2040 
2041     EXPECT_EQ(entrys1.size(), 20UL);
2042     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys1), OK);
2043     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
2044     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys2, observer->GetEntriesInserted()));
2045     EXPECT_EQ(observer->GetEntriesUpdated().size(), 0UL);
2046 
2047     vector<Entry> entrys3;
2048     vector<Key> keys3;
2049     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys3, keys3, DEFAULT_KEY_VALUE_SIZE,
2050         DEFAULT_KEY_VALUE_SIZE + 20);
2051     vector<Entry> entrys4;
2052     vector<Key> keys4;
2053     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys4, keys4, DEFAULT_KEY_VALUE_SIZE,
2054         DEFAULT_KEY_VALUE_SIZE + 30);
2055     entrys3.insert(entrys3.end(), entrys4.begin(), entrys4.end());
2056     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
2057     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
2058     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys4, observer->GetEntriesUpdated()));
2059     EXPECT_EQ(observer->GetEntriesInserted().size(), 0UL);
2060 
2061     /**
2062      * @tc.steps:step4. UnRegister the observer.
2063      * @tc.expected: step4. Returns OK.
2064      */
2065     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
2066     delete observer;
2067     observer = nullptr;
2068     /**
2069      * @tc.steps:step5. Close the kv store.
2070      * @tc.expected: step5. Results OK and delete successfully.
2071      */
2072     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2073     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_004"), OK);
2074     g_kvNbDelegatePtr = nullptr;
2075 }
2076 
2077 /**
2078   * @tc.name: SingleVerDeleteBatchObserver001
2079   * @tc.desc: Test the observer function of DeleteBatch() interface.
2080   * @tc.type: FUNC
2081   * @tc.require:
2082   * @tc.author: wumin
2083   */
2084 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerDeleteBatchObserver001, TestSize.Level1)
2085 {
2086     /**
2087      * @tc.steps:step1. Get the nb delegate.
2088      * @tc.expected: step1. Get results OK and non-null delegate.
2089      */
2090     KvStoreNbDelegate::Option option = {true, false, false};
2091     g_mgr.GetKvStore("distributed_SingleVerDeleteBatchObserver_001", option, g_kvNbDelegateCallback);
2092     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2093     EXPECT_TRUE(g_kvDelegateStatus == OK);
2094 
2095     KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
2096     ASSERT_TRUE(observer != nullptr);
2097     /**
2098      * @tc.steps:step2. Register the non-null observer for the special key.
2099      * @tc.expected: step2. Register results OK.
2100      */
2101     Key key;
2102     EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
2103     /**
2104      * @tc.steps:step3. Put batch data.
2105      * @tc.expected: step3. Returns OK.
2106      */
2107     vector<Entry> entries;
2108     vector<Key> keys;
2109     DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entries, keys);
2110     EXPECT_EQ(entries.size(), 10UL);
2111 
2112     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
2113     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
2114     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
2115     /**
2116      * @tc.steps:step4. Delete the batch data.
2117      * @tc.expected: step4. Returns OK.
2118      */
2119     EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
2120     std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
2121     EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesDeleted()));
2122     /**
2123      * @tc.steps:step5. UnRegister the observer.
2124      * @tc.expected: step5. Returns OK.
2125      */
2126     EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
2127     delete observer;
2128     observer = nullptr;
2129     /**
2130      * @tc.steps:step6. Close the kv store.
2131      * @tc.expected: step6. Results OK and delete successfully.
2132      */
2133     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2134     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteBatchObserver_001"), OK);
2135     g_kvNbDelegatePtr = nullptr;
2136 }
2137 
2138 /**
2139   * @tc.name: SingleVerConcurrentPut001
2140   * @tc.desc: Test put the data concurrently, and check the timestamp.
2141   * @tc.type: FUNC
2142   * @tc.require:
2143   * @tc.author: wangbingquan
2144   */
2145 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerConcurrentPut001, TestSize.Level4)
2146 {
2147     /**
2148      * @tc.steps:step1. Get the nb delegate.
2149      * @tc.expected: step1. Get results OK and non-null delegate.
2150      */
2151     KvStoreNbDelegate::Option option = {true, false, false};
2152     g_mgr.GetKvStore("concurrentPutTest", option, g_kvNbDelegateCallback);
2153     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2154     EXPECT_TRUE(g_kvDelegateStatus == OK);
2155 
2156     for (size_t i = 0; i < CON_PUT_THREAD_NUM * PER_THREAD_PUT_NUM; i++) {
2157         Entry entry;
2158         DistributedDBToolsUnitTest::GetRandomKeyValue(entry.key, DEFAULT_KEY_VALUE_SIZE);
2159         DistributedDBToolsUnitTest::GetRandomKeyValue(entry.value);
2160         g_entriesForConcurrency.push_back(std::move(entry));
2161     }
2162 
2163     /**
2164      * @tc.steps:step2. Put data concurrently in 4 threads.
2165      * @tc.expected: step2. Put OK, and the timestamp order is same with the rowid.
2166      */
2167     std::thread thread1(std::bind(PutData, g_kvNbDelegatePtr, 0)); // 0th thread.
2168     std::thread thread2(std::bind(PutData, g_kvNbDelegatePtr, 1)); // 1th thread.
2169     std::thread thread3(std::bind(PutData, g_kvNbDelegatePtr, 2)); // 2th thread.
2170     std::thread thread4(std::bind(PutData, g_kvNbDelegatePtr, 3)); // 3th thread.
2171 
2172     thread1.join();
2173     thread2.join();
2174     thread3.join();
2175     thread4.join();
2176 
2177     EXPECT_EQ(CheckDataTimestamp("concurrentPutTest"), true);
2178 
2179     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2180     EXPECT_EQ(g_mgr.DeleteKvStore("concurrentPutTest"), OK);
2181     g_kvNbDelegatePtr = nullptr;
2182 }
2183 
2184 /**
2185   * @tc.name: SingleVerGetLocalEntries001
2186   * @tc.desc: Test GetLocalEntries interface for the single ver database.
2187   * @tc.type: FUNC
2188   * @tc.require:
2189   * @tc.author: wangbingquan
2190   */
2191 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerGetLocalEntries001, TestSize.Level1)
2192 {
2193     /**
2194      * @tc.steps:step1. Get the nb delegate.
2195      * @tc.expected: step1. Get results OK and non-null delegate.
2196      */
2197     KvStoreNbDelegate::Option option = {true, false, false};
2198     g_mgr.GetKvStore("concurrentPutTest", option, g_kvNbDelegateCallback);
2199     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2200     EXPECT_TRUE(g_kvDelegateStatus == OK);
2201 
2202     /**
2203      * @tc.steps:step2. Put one data whose key has prefix 'p' into the local zone.
2204      */
2205     Entry entry1 = {{'p'}, {'q'}};
2206     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(entry1.key, entry1.value), OK);
2207 
2208     /**
2209      * @tc.steps:step3. Get batch data whose key has prefix 'k' from the local zone.
2210      * @tc.expected: step3. Get results NOT_FOUND.
2211      */
2212     std::vector<Entry> entries;
2213     EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries({'k'}, entries), NOT_FOUND);
2214 
2215     /**
2216      * @tc.steps:step4. Put two data whose key have prefix 'k' into the local zone.
2217      */
2218     Entry entry2 = {{'k', '1'}, {'d'}};
2219     Entry entry3 = {{'k', '2'}, {'d'}};
2220     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(entry2.key, entry2.value), OK);
2221     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(entry3.key, entry3.value), OK);
2222 
2223     /**
2224      * @tc.steps:step5. Get batch data whose key has prefix 'k' from the local zone.
2225      * @tc.expected: step5. Get results OK, and the entries size is 2.
2226      */
2227     EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries({'k'}, entries), OK);
2228     EXPECT_EQ(entries.size(), 2UL);
2229 
2230     /**
2231      * @tc.steps:step6. Get batch data whose key has empty prefix from the local zone.
2232      * @tc.expected: step6. Get results OK, and the entries size is 3.
2233      */
2234     EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries({}, entries), OK);
2235     EXPECT_EQ(entries.size(), 3UL);
2236 
2237     /**
2238      * @tc.steps:step7. Delete one data whose key has prefix 'k' from the local zone.
2239      */
2240     EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocal(entry3.key), OK);
2241 
2242     /**
2243      * @tc.steps:step8. Get batch data whose key has prefix 'k' from the local zone.
2244      * @tc.expected: step8. Get results OK, and the entries size is 1.
2245      */
2246     EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries({'k'}, entries), OK);
2247     EXPECT_EQ(entries.size(), 1UL);
2248 
2249     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2250     EXPECT_EQ(g_mgr.DeleteKvStore("concurrentPutTest"), OK);
2251     g_kvNbDelegatePtr = nullptr;
2252 }
2253 
PreDataForQueryByPreFixKey()2254 static vector<Entry> PreDataForQueryByPreFixKey()
2255 {
2256     vector<Entry> res;
2257     for (int i = 0; i < 5; i++) { // Random 5 for test
2258         Key key = DistributedDBToolsUnitTest::GetRandPrefixKey({'a', 'b'}, 1024);
2259         std::string validData = "{\"field_name1\":null, \"field_name2\":" + std::to_string(rand()) + "}";
2260         Value value(validData.begin(), validData.end());
2261         res.push_back({key, value});
2262     }
2263 
2264     for (int i = 0; i < 5; i++) { // Random 5 for test
2265         Key key = DistributedDBToolsUnitTest::GetRandPrefixKey({'a', 'c'}, 1024);
2266         std::string validData = "{\"field_name1\":null, \"field_name2\":" + std::to_string(rand()) + "}";
2267         Value value(validData.begin(), validData.end());
2268         res.push_back({key, value});
2269     }
2270     return res;
2271 }
2272 
2273 /**
2274   * @tc.name: QueryPreFixKey002
2275   * @tc.desc: The query method without filtering the field can query non-schma databases
2276   * @tc.type: FUNC
2277   * @tc.require:
2278   * @tc.author: sunpeng
2279   */
2280 HWTEST_F(DistributedDBInterfacesNBDelegateTest, QueryPreFixKey002, TestSize.Level1)
2281 {
2282     /**
2283      * @tc.steps:step1. Create non-schma databases
2284      */
2285     KvStoreNbDelegate::Option option = {true, false, false};
2286     g_mgr.GetKvStore("QueryPreFixKey002", option, g_kvNbDelegateCallback);
2287     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2288     EXPECT_TRUE(g_kvDelegateStatus == OK);
2289 
2290     vector<Entry> entries = PreDataForQueryByPreFixKey();
2291     EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
2292 
2293     /**
2294      * @tc.steps:step2. Get query object with prefixkey limit combination.
2295      * @tc.expected: step2. Get results OK, and the entries size right.
2296      */
2297     Query query = Query::Select().PrefixKey({'a', 'c'});
2298     std::vector<Entry> entriesRes;
2299     int errCode = g_kvNbDelegatePtr->GetEntries(query, entriesRes);
2300     EXPECT_EQ(errCode, OK);
2301     EXPECT_EQ(entriesRes.size(), 5ul);
2302     for (size_t i = 0; i < entriesRes.size(); i++) {
2303         EXPECT_EQ(entriesRes[i].key.front(), 'a');
2304         EXPECT_EQ(entriesRes[i].key[1], 'c');
2305     }
2306     int count = -1;
2307     g_kvNbDelegatePtr->GetCount(query, count);
2308     EXPECT_EQ(count, 5);
2309 
2310     Query query1 = Query::Select().PrefixKey({}).Limit(4, 0);
2311     errCode = g_kvNbDelegatePtr->GetEntries(query1, entriesRes);
2312     EXPECT_EQ(errCode, OK);
2313     EXPECT_EQ(entriesRes.size(), 4ul);
2314 
2315     Query query2 = Query::Select().PrefixKey(Key(1025, 'a'));
2316     errCode = g_kvNbDelegatePtr->GetEntries(query2, entriesRes);
2317     EXPECT_EQ(errCode, INVALID_ARGS);
2318 
2319     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2320     EXPECT_TRUE(g_mgr.DeleteKvStore("QueryPreFixKey002") == OK);
2321     g_kvNbDelegatePtr = nullptr;
2322 }
2323 
2324 /**
2325   * @tc.name: SingleVerGetSecurityOption001
2326   * @tc.desc: Test GetSecurityOption interface for the single ver database.
2327   * @tc.type: FUNC
2328   * @tc.require:
2329   * @tc.author: liuwenkai
2330   */
2331 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerGetSecurityOption001, TestSize.Level1)
2332 {
2333     SecurityOption savedOption;
2334     std::shared_ptr<IProcessSystemApiAdapter> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
2335     EXPECT_TRUE(adapter);
2336     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
2337     KvStoreNbDelegate::Option option = {true, false, false};
2338 
2339     /**
2340      * @tc.steps:step1. Create databases without securityOption.
2341      * @tc.expected: step2. Returns a non-null kvstore but can not get SecurityOption.
2342      */
2343     g_mgr.GetKvStore("SingleVerGetSecurityOption001", option, g_kvNbDelegateCallback);
2344     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2345     EXPECT_TRUE(g_kvDelegateStatus == OK);
2346     EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
2347     EXPECT_TRUE(savedOption.securityLabel == 0);
2348     EXPECT_TRUE(savedOption.securityFlag == 0);
2349     KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
2350 
2351     /**
2352      * @tc.steps:step2. Create databases with new securityOption(Check ignore the new option).
2353      * @tc.expected: step2. Returns non-null kvstore.
2354      */
2355     option.secOption.securityLabel = S3;
2356     option.secOption.securityFlag = 1;
2357     g_mgr.GetKvStore("SingleVerGetSecurityOption001", option, g_kvNbDelegateCallback);
2358     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2359     EXPECT_TRUE(g_kvDelegateStatus == OK);
2360     EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
2361     SecurityOption secOption = {option.secOption.securityLabel, option.secOption.securityFlag};
2362     EXPECT_TRUE(savedOption != secOption);
2363     EXPECT_TRUE(savedOption.securityLabel == 0);
2364     EXPECT_TRUE(savedOption.securityFlag == 0);
2365 
2366     EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
2367     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2368     g_kvNbDelegatePtr = nullptr;
2369     EXPECT_TRUE(g_mgr.DeleteKvStore("SingleVerGetSecurityOption001") == OK);
2370 }
2371 
2372 /**
2373   * @tc.name: SingleVerGetSecurityOption002
2374   * @tc.desc: Test GetSecurityOption interface for the single ver database.
2375   * @tc.type: FUNC
2376   * @tc.require:
2377   * @tc.author: liuwenkai
2378   */
2379 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SingleVerGetSecurityOption002, TestSize.Level1)
2380 {
2381     SecurityOption savedOption;
2382     std::shared_ptr<IProcessSystemApiAdapter> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
2383     EXPECT_TRUE(adapter != nullptr);
2384     RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
2385     KvStoreNbDelegate::Option option = {true, false, false};
2386 
2387     /**
2388      * @tc.steps:step1. Create databases with securityOption.
2389      * @tc.expected: step2. Returns a non-null kvstore and get right SecurityOption.
2390      */
2391     option.secOption.securityLabel = S3;
2392     option.secOption.securityFlag = 1;
2393     g_mgr.GetKvStore("SingleVerGetSecurityOption002", option, g_kvNbDelegateCallback);
2394     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2395     EXPECT_TRUE(g_kvDelegateStatus == OK);
2396     EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
2397     EXPECT_TRUE(savedOption.securityLabel == S3);
2398     EXPECT_TRUE(savedOption.securityFlag == 1);
2399     KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
2400 
2401     /**
2402      * @tc.steps:step2. Create databases without securityOption.
2403      * @tc.expected: step2. Returns a non-null kvstore and get right SecurityOption.
2404      */
2405     option.secOption.securityLabel = 0;
2406     option.secOption.securityFlag = 0;
2407     g_mgr.GetKvStore("SingleVerGetSecurityOption002", option, g_kvNbDelegateCallback);
2408     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2409     EXPECT_TRUE(g_kvDelegateStatus == OK);
2410     EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
2411     EXPECT_TRUE(savedOption.securityLabel == S3);
2412     EXPECT_TRUE(savedOption.securityFlag == 1);
2413 
2414     EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
2415     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2416     g_kvNbDelegatePtr = nullptr;
2417     EXPECT_TRUE(g_mgr.DeleteKvStore("SingleVerGetSecurityOption002") == OK);
2418 }
2419 
2420 /**
2421  * @tc.name: MaxLogSize001
2422  * @tc.desc: Test the pragma cmd of the max log size limit.
2423  * @tc.type: FUNC
2424  * @tc.require:
2425  * @tc.author: wangbingquan
2426  */
2427 HWTEST_F(DistributedDBInterfacesNBDelegateTest, MaxLogSize001, TestSize.Level2)
2428 {
2429     /**
2430      * @tc.steps:step1. Create database.
2431      * @tc.expected: step1. Returns a non-null kvstore.
2432      */
2433     KvStoreNbDelegate::Option option;
2434     g_mgr.GetKvStore("MaxLogSize001", option, g_kvNbDelegateCallback);
2435     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2436     EXPECT_TRUE(g_kvDelegateStatus == OK);
2437 
2438     /**
2439      * @tc.steps:step2. Setting the max log limit for the valid value.
2440      * @tc.expected: step2. Returns OK.
2441      */
2442     uint64_t logSize = DBConstant::MAX_LOG_SIZE_HIGH;
2443     PragmaData pragLimit = static_cast<PragmaData>(&logSize);
2444     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), OK);
2445 
2446     logSize = DBConstant::MAX_LOG_SIZE_LOW;
2447     pragLimit = static_cast<PragmaData>(&logSize);
2448     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), OK);
2449 
2450     logSize = 10 * 1024 * 1024; // 10M
2451     pragLimit = static_cast<PragmaData>(&logSize);
2452     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), OK);
2453 
2454     /**
2455      * @tc.steps:step3. Setting the max log limit for the invalid value.
2456      * @tc.expected: step3. Returns INLIVAD_ARGS.
2457      */
2458     logSize = DBConstant::MAX_LOG_SIZE_HIGH + 1;
2459     pragLimit = static_cast<PragmaData>(&logSize);
2460     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), INVALID_ARGS);
2461 
2462     logSize = DBConstant::MAX_LOG_SIZE_LOW - 1;
2463     pragLimit = static_cast<PragmaData>(&logSize);
2464     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), INVALID_ARGS);
2465     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2466     g_kvNbDelegatePtr = nullptr;
2467     EXPECT_TRUE(g_mgr.DeleteKvStore("MaxLogSize001") == OK);
2468 }
2469 
2470 /**
2471  * @tc.name: ForceCheckpoint002
2472  * @tc.desc: Test the checkpoint of the database.
2473  * @tc.type: FUNC
2474  * @tc.require:
2475  * @tc.author: wangbingquan
2476  */
2477 HWTEST_F(DistributedDBInterfacesNBDelegateTest, MaxLogSize002, TestSize.Level2)
2478 {
2479     /**
2480      * @tc.steps:step1. Create database.
2481      * @tc.expected: step1. Returns a non-null kvstore.
2482      */
2483     KvStoreNbDelegate::Option option;
2484     g_mgr.GetKvStore("MaxLogSize002", option, g_kvNbDelegateCallback);
2485     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2486     EXPECT_TRUE(g_kvDelegateStatus == OK);
2487 
2488     /**
2489      * @tc.steps:step2. Put the random entry into the database.
2490      * @tc.expected: step2. Returns OK.
2491      */
2492     Key key;
2493     Value value;
2494     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 30); // for 30B random key
2495     DistributedDBToolsUnitTest::GetRandomKeyValue(value, 3 * 1024 * 1024); // 3M value
2496     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2497     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 40); // for 40B random key
2498     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2499 
2500     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 20); // for 20B random key
2501     DistributedDBToolsUnitTest::GetRandomKeyValue(value, 1 * 1024 * 1024); // 1M value
2502     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2503 
2504     /**
2505      * @tc.steps:step3. Get the resultset.
2506      * @tc.expected: step3. Returns OK.
2507      */
2508     KvStoreResultSet *resultSet = nullptr;
2509     EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(Key{}, resultSet), OK);
2510     ASSERT_NE(resultSet, nullptr);
2511     EXPECT_EQ(resultSet->GetCount(), 3); // size of all the entries is 3
2512     EXPECT_EQ(resultSet->MoveToFirst(), true);
2513 
2514     /**
2515      * @tc.steps:step4. Put more data into the database.
2516      * @tc.expected: step4. Returns OK.
2517      */
2518     uint64_t logSize = 6 * 1024 * 1024; // 6M for initial test.
2519     PragmaData pragLimit = static_cast<PragmaData>(&logSize);
2520     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), OK);
2521     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 10); // for 10B random key(different size)
2522     DistributedDBToolsUnitTest::GetRandomKeyValue(value, 3 * 1024 * 1024); // 3MB
2523     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2524     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 15); // for 15B random key(different size)
2525     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2526 
2527     /**
2528      * @tc.steps:step4. Put more data into the database while the log size is over the limit.
2529      * @tc.expected: step4. Returns LOG_OVER_LIMITS.
2530      */
2531     DistributedDBToolsUnitTest::GetRandomKeyValue(value, 25); // for 25B random key(different size)
2532     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), LOG_OVER_LIMITS);
2533     EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), LOG_OVER_LIMITS);
2534     EXPECT_EQ(g_kvNbDelegatePtr->StartTransaction(), LOG_OVER_LIMITS);
2535     EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(key, value), LOG_OVER_LIMITS);
2536     EXPECT_EQ(g_kvNbDelegatePtr->RemoveDeviceData("deviceA"), LOG_OVER_LIMITS);
2537     EXPECT_EQ(g_kvNbDelegatePtr->RemoveDeviceData(), LOG_OVER_LIMITS);
2538     /**
2539      * @tc.steps:step4. Change the max log size limit, and put the data.
2540      * @tc.expected: step4. Returns OK.
2541      */
2542     logSize *= 10; // 10 multiple size
2543     pragLimit = static_cast<PragmaData>(&logSize);
2544     EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), OK);
2545     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2546     g_kvNbDelegatePtr->CloseResultSet(resultSet);
2547 
2548     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2549     EXPECT_EQ(g_mgr.DeleteKvStore("MaxLogSize002"), OK);
2550     g_kvNbDelegatePtr = nullptr;
2551 }
2552 
2553 /**
2554  * @tc.name: MaxLogCheckPoint001
2555  * @tc.desc: Pragma the checkpoint command.
2556  * @tc.type: FUNC
2557  * @tc.require:
2558  * @tc.author: wangbingquan
2559  */
2560 HWTEST_F(DistributedDBInterfacesNBDelegateTest, MaxLogCheckPoint001, TestSize.Level2)
2561 {
2562     /**
2563      * @tc.steps:step1. Create database.
2564      * @tc.expected: step1. Returns a non-null kvstore.
2565      */
2566     KvStoreNbDelegate::Option option;
2567     g_mgr.GetKvStore("MaxLogCheckPoint001", option, g_kvNbDelegateCallback);
2568     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2569     EXPECT_TRUE(g_kvDelegateStatus == OK);
2570 
2571     /**
2572      * @tc.steps:step2. Put the random entry into the database.
2573      * @tc.expected: step2. Returns OK.
2574      */
2575     Key key;
2576     Value value;
2577     DistributedDBToolsUnitTest::GetRandomKeyValue(key, 30); // for 30B random key(different size)
2578     DistributedDBToolsUnitTest::GetRandomKeyValue(value, 1 * 1024 * 1024); // 1M
2579     EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
2580     EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), OK);
2581 
2582     /**
2583      * @tc.steps:step3. Get the disk file size, execute the checkpoint and get the disk file size.
2584      * @tc.expected: step3. Returns OK and the file size is less than the size before checkpoint.
2585      */
2586     uint64_t sizeBeforeChk = 0;
2587     g_mgr.GetKvStoreDiskSize("MaxLogCheckPoint001", sizeBeforeChk);
2588     EXPECT_GT(sizeBeforeChk, 1 * 1024 * 1024ULL); // more than 1M
2589     int param = 0;
2590     PragmaData paraData = static_cast<PragmaData>(&param);
2591     g_kvNbDelegatePtr->Pragma(EXEC_CHECKPOINT, paraData);
2592     uint64_t sizeAfterChk = 0;
2593     g_mgr.GetKvStoreDiskSize("MaxLogCheckPoint001", sizeAfterChk);
2594     EXPECT_LT(sizeAfterChk, 100 * 1024ULL); // less than 100K
2595     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2596     EXPECT_EQ(g_mgr.DeleteKvStore("MaxLogCheckPoint001"), OK);
2597     g_kvNbDelegatePtr = nullptr;
2598 }
2599 
2600 /**
2601   * @tc.name: CreateMemoryDbWithoutPath
2602   * @tc.desc: Create memory database without path.
2603   * @tc.type: FUNC
2604   * @tc.require:
2605   * @tc.author: sunpeng
2606   */
2607 HWTEST_F(DistributedDBInterfacesNBDelegateTest, CreateMemoryDbWithoutPath, TestSize.Level1)
2608 {
2609     /**
2610      * @tc.steps: step1. Create Memory database by GetKvStore without path.
2611      * @tc.expected: step1. Create successfully.
2612      */
2613     KvStoreDelegateManager mgr(APP_ID, USER_ID);
2614     const KvStoreNbDelegate::Option option = {true, true};
2615     mgr.GetKvStore("memory_without_path", option, g_kvNbDelegateCallback);
2616     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2617     EXPECT_TRUE(g_kvDelegateStatus == OK);
2618     EXPECT_EQ(mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2619     g_kvNbDelegatePtr = nullptr;
2620 }
2621 
2622 /**
2623   * @tc.name: OpenStorePathCheckTest001
2624   * @tc.desc: Test open store with same label but different path.
2625   * @tc.type: FUNC
2626   * @tc.require:
2627   * @tc.author: lianhuix
2628   */
2629 HWTEST_F(DistributedDBInterfacesNBDelegateTest, OpenStorePathCheckTest001, TestSize.Level1)
2630 {
2631     std::string dir1 = g_testDir + "/dbDir1";
2632     EXPECT_EQ(OS::MakeDBDirectory(dir1), E_OK);
2633     std::string dir2 = g_testDir + "/dbDir2";
2634     EXPECT_EQ(OS::MakeDBDirectory(dir2), E_OK);
2635 
2636     KvStoreDelegateManager mgr1(APP_ID, USER_ID);
2637     mgr1.SetKvStoreConfig({dir1});
2638 
2639     KvStoreNbDelegate *delegate1 = nullptr;
2640     auto callback1 = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
2641         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(delegate1));
2642 
2643     KvStoreNbDelegate::Option option;
2644     mgr1.GetKvStore(STORE_ID_1, option, callback1);
2645     EXPECT_EQ(g_kvDelegateStatus, OK);
2646     ASSERT_NE(delegate1, nullptr);
2647 
2648     KvStoreNbDelegate *delegate2 = nullptr;
2649     auto callback2 = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
2650         placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(delegate2));
2651     KvStoreDelegateManager mgr2(APP_ID, USER_ID);
2652     mgr2.SetKvStoreConfig({dir2});
2653     mgr2.GetKvStore(STORE_ID_1, option, callback2);
2654     EXPECT_EQ(g_kvDelegateStatus, INVALID_ARGS);
2655     ASSERT_EQ(delegate2, nullptr);
2656 
2657     mgr1.CloseKvStore(delegate1);
2658     mgr1.DeleteKvStore(STORE_ID_1);
2659     mgr2.CloseKvStore(delegate2);
2660     mgr2.DeleteKvStore(STORE_ID_1);
2661 }
2662 
2663 namespace {
GetRealFileUrl(const std::string & dbPath,const std::string & appId,const std::string & userId,const std::string & storeId)2664 std::string GetRealFileUrl(const std::string &dbPath, const std::string &appId, const std::string &userId,
2665     const std::string &storeId)
2666 {
2667     std::string hashIdentifier = DBCommon::TransferHashString(
2668         DBCommon::GenerateIdentifierId(storeId, appId, userId));
2669     return dbPath + "/" + DBCommon::TransferStringToHex(hashIdentifier) + "/single_ver/main/gen_natural_store.db";
2670 }
2671 }
2672 
2673 /**
2674   * @tc.name: BusyTest001
2675   * @tc.desc: Test put kv data while another thread holds the transaction for one second
2676   * @tc.type: FUNC
2677   * @tc.require:
2678   * @tc.author: lianhuix
2679   */
2680 HWTEST_F(DistributedDBInterfacesNBDelegateTest, BusyTest001, TestSize.Level1)
2681 {
2682     KvStoreDelegateManager mgr(APP_ID, USER_ID);
2683     mgr.SetKvStoreConfig(g_config);
2684 
2685     const KvStoreNbDelegate::Option option = {true, false, false};
2686     mgr.GetKvStore(STORE_ID_1, option, g_kvNbDelegateCallback);
2687     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2688     EXPECT_TRUE(g_kvDelegateStatus == OK);
2689 
2690     std::string dbPath = GetRealFileUrl(g_config.dataDir, APP_ID, USER_ID, STORE_ID_1);
2691     sqlite3 *db = RelationalTestUtils::CreateDataBase(dbPath);
2692     RelationalTestUtils::ExecSql(db, "BEGIN IMMEDIATE;");
2693 
__anon00286aae0302() 2694     std::thread th([db]() {
2695         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
2696         RelationalTestUtils::ExecSql(db, "COMMIT");
2697     });
2698 
2699     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
2700 
2701     th.join();
2702     sqlite3_close_v2(db);
2703     EXPECT_EQ(mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2704     EXPECT_EQ(mgr.DeleteKvStore(STORE_ID_1), OK);
2705     g_kvNbDelegatePtr = nullptr;
2706 }
2707 
2708 /**
2709  * @tc.name: GetKeys001
2710  * @tc.desc: Test get keys from the database.
2711  * @tc.type: FUNC
2712  * @tc.require:
2713  * @tc.author: zhangqiquan
2714  */
2715 HWTEST_F(DistributedDBInterfacesNBDelegateTest, GetKeys001, TestSize.Level1)
2716 {
2717     /**
2718      * @tc.steps:step1. Create database.
2719      * @tc.expected: step1. Returns a non-null kvstore.
2720      */
2721     KvStoreNbDelegate::Option option;
2722     g_mgr.GetKvStore("GetKeys001", option, g_kvNbDelegateCallback);
2723     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2724     EXPECT_TRUE(g_kvDelegateStatus == OK);
2725 
2726     /**
2727      * @tc.steps:step2. Put the all keys into the database.
2728      * @tc.expected: step2. Returns OK.
2729      */
2730     std::vector<Key> expectKeys = {
2731         {'k', '1', '1'},
2732         {'k', '2'},
2733         {'k', '3'},
2734         {'k', '4'}
2735     };
2736     for (const auto &key : expectKeys) {
2737         EXPECT_EQ(g_kvNbDelegatePtr->Put(key, {}), OK);
2738     }
2739     EXPECT_EQ(g_kvNbDelegatePtr->Put({'k', '2'}, {}), OK);
2740     EXPECT_EQ(g_kvNbDelegatePtr->Delete({'k', '4'}), OK);
2741 
2742     /**
2743      * @tc.steps:step3. Get the all keys.
2744      * @tc.expected: step3. Returns OK.
2745      */
2746     Key keyPrefix = {'k', '1'};
2747     std::vector<Key> actualKeys;
2748     EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), OK);
2749     EXPECT_EQ(actualKeys.size(), 1u); // get the k11
2750     for (const auto &key : actualKeys) {
2751         EXPECT_EQ(key, expectKeys[0]);
2752     }
2753     keyPrefix.clear();
2754     EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), OK);
2755     EXPECT_EQ(actualKeys.size(), 3u); // size of all the key is 3
2756 
2757     keyPrefix = {'k', '4'};
2758     EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), NOT_FOUND);
2759     EXPECT_EQ(actualKeys.size(), 0u); // not found key and size is 0
2760 
2761     DistributedDBToolsUnitTest::GetRandomKeyValue(keyPrefix, 2048); // for 2048B random key
2762     EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), INVALID_ARGS);
2763     EXPECT_EQ(actualKeys.size(), 0u); // invalid prefix key and size is 0
2764 
2765     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2766     EXPECT_EQ(g_mgr.DeleteKvStore("GetKeys001"), OK);
2767     g_kvNbDelegatePtr = nullptr;
2768 }
2769 
2770 namespace {
InitVirtualDevice(const std::string & devId,KvVirtualDevice * & devices,VirtualSingleVerSyncDBInterface * & syncInterface)2771 void InitVirtualDevice(const std::string &devId, KvVirtualDevice *&devices,
2772     VirtualSingleVerSyncDBInterface *&syncInterface)
2773 {
2774     devices = new (std::nothrow) KvVirtualDevice(devId);
2775     ASSERT_TRUE(devices != nullptr);
2776     syncInterface = new (std::nothrow) VirtualSingleVerSyncDBInterface();
2777     ASSERT_TRUE(syncInterface != nullptr);
2778     ASSERT_EQ(devices->Initialize(g_communicatorAggregator, syncInterface), E_OK);
2779 }
2780 
FreeVirtualDevice(KvVirtualDevice * & devices)2781 void FreeVirtualDevice(KvVirtualDevice *&devices)
2782 {
2783     if (devices != nullptr) {
2784         delete devices;
2785         devices = nullptr;
2786     }
2787 }
2788 }
2789 
2790 /**
2791   * @tc.name: RemoveDeviceDataTest001
2792   * @tc.desc: remove device data with devId unspecified
2793   * @tc.type: FUNC
2794   * @tc.require:
2795   * @tc.author: lianhuix
2796   */
2797 HWTEST_F(DistributedDBInterfacesNBDelegateTest, RemoveDeviceDataTest001, TestSize.Level1)
2798 {
2799     InitVirtualDevice(DEVICE_B, g_deviceB, g_syncInterfaceB);
2800     InitVirtualDevice(DEVICE_C, g_deviceC, g_syncInterfaceC);
2801     InitVirtualDevice(DEVICE_D, g_deviceD, g_syncInterfaceD);
2802 
2803     KvStoreDelegateManager mgr(APP_ID, USER_ID);
2804     mgr.SetKvStoreConfig(g_config);
2805 
2806     const KvStoreNbDelegate::Option option = {true, false, false};
2807     mgr.GetKvStore(STORE_ID_1, option, g_kvNbDelegateCallback);
2808     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2809     EXPECT_EQ(g_kvDelegateStatus, OK);
2810 
2811     EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
2812     g_deviceB->PutData(KEY_2, VALUE_2, 0, 0);
2813     g_deviceC->PutData(KEY_3, VALUE_3, 0, 0);
2814     g_deviceD->PutData(KEY_4, VALUE_4, 0, 0);
2815 
2816     std::vector<std::string> devices;
2817     devices.push_back(DEVICE_B);
2818     devices.push_back(DEVICE_C);
2819     devices.push_back(DEVICE_D);
2820     DBStatus status = g_kvNbDelegatePtr->Sync(devices, SYNC_MODE_PULL_ONLY,
__anon00286aae0502(const std::map<std::string, DBStatus>& statusMap) 2821         [devices, this](const std::map<std::string, DBStatus>& statusMap) {
2822             ASSERT_EQ(statusMap.size(), devices.size());
2823             for (const auto &pair : statusMap) {
2824                 EXPECT_EQ(pair.second, OK);
2825             }
2826         }, true);
2827     EXPECT_EQ(status, OK);
2828 
2829     EXPECT_EQ(g_kvNbDelegatePtr->RemoveDeviceData(), OK);
2830 
2831     Value val;
2832     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, val), OK);
2833     EXPECT_EQ(val, VALUE_1);
2834     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_2, val), NOT_FOUND);
2835     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_3, val), NOT_FOUND);
2836     EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_4, val), NOT_FOUND);
2837 
2838     EXPECT_EQ(mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2839     g_kvNbDelegatePtr = nullptr;
2840     EXPECT_EQ(mgr.DeleteKvStore(STORE_ID_1), OK);
2841     FreeVirtualDevice(g_deviceB);
2842     FreeVirtualDevice(g_deviceC);
2843     FreeVirtualDevice(g_deviceD);
2844 }
2845 
2846 /**
2847   * @tc.name: RekeyTest001
2848   * @tc.desc: Test rekey with multi db handles.
2849   * @tc.type: FUNC
2850   * @tc.require:
2851   * @tc.author: liaoyonnghuang
2852   */
2853 HWTEST_F(DistributedDBInterfacesNBDelegateTest, RekeyTest001, TestSize.Level1)
2854 {
2855     /**
2856      * @tc.steps:step1. Create database.
2857      * @tc.expected: step1. Returns a non-null kvstore.
2858      */
2859     CipherPassword passwd;
2860     const std::vector<uint8_t> PASSWD_VECTOR_1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '1', '2', '3'};
2861     const std::vector<uint8_t> PASSWD_VECTOR_2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '0', '0', '0'};
2862     const std::vector<uint8_t> PASSWD_VECTOR_3 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '0', '0', '1'};
2863     KvStoreNbDelegate::Option option = {true, false, true};
2864     (void)passwd.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
2865     option.passwd = passwd;
2866     g_mgr.SetKvStoreConfig(g_config);
2867     g_mgr.GetKvStore("rekeyTest001", option, g_kvNbDelegateCallback);
2868     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2869     EXPECT_TRUE(g_kvDelegateStatus == OK);
2870     KvStoreNbDelegate *kvNbDelegatePtr001 = g_kvNbDelegatePtr;
2871 
2872     /**
2873      * @tc.steps:step2. Rekey.
2874      * @tc.expected: step2. Returns OK.
2875      */
2876     (void)passwd.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
2877     kvNbDelegatePtr001->Rekey(passwd);
2878 
2879     /**
2880      * @tc.steps:step3. Open DB and rekey.
2881      * @tc.expected: step3. Returns not OK.
2882      */
2883     sqlite3 *db = nullptr;
2884     int flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
2885     const auto &dbPath = g_testDir +
2886         "/cf479925e0691d2df83f095db294ee671a7a8f38e7527fef0dd1b40f8e3cb476/single_ver/main/gen_natural_store.db";
2887     int rc = sqlite3_open_v2(dbPath.c_str(), &db, flag, nullptr);
2888     EXPECT_TRUE(rc == SQLITE_OK);
2889     (void)passwd.SetValue(PASSWD_VECTOR_3.data(), PASSWD_VECTOR_3.size());
2890     EXPECT_FALSE(kvNbDelegatePtr001->Rekey(passwd) == OK);
2891     g_mgr.CloseKvStore(kvNbDelegatePtr001);
2892     sqlite3_close(db);
2893     g_kvNbDelegatePtr = nullptr;
2894 }
2895 
2896 /**
2897   * @tc.name: RekeyTest002
2898   * @tc.desc: Test normal rekey.
2899   * @tc.type: FUNC
2900   * @tc.require:
2901   * @tc.author: liaoyonghuang
2902   */
2903 HWTEST_F(DistributedDBInterfacesNBDelegateTest, RekeyTest002, TestSize.Level0)
2904 {
2905     /**
2906      * @tc.steps:step1. Create database.
2907      * @tc.expected: step1. Returns a non-null kvstore.
2908      */
2909     CipherPassword passwd;
2910     const std::vector<uint8_t> PASSWD_VECTOR = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '1', '2', '3'};
2911     KvStoreNbDelegate::Option option = {true, false, true};
2912     (void)passwd.SetValue(PASSWD_VECTOR.data(), PASSWD_VECTOR.size());
2913     option.passwd = passwd;
2914     g_mgr.SetKvStoreConfig(g_config);
2915     g_mgr.GetKvStore("rekeyTest002", option, g_kvNbDelegateCallback);
2916     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2917     EXPECT_TRUE(g_kvDelegateStatus == OK);
2918 
2919     /**
2920      * @tc.steps:step2. Rekey.
2921      * @tc.expected: step2. Returns OK.
2922      */
2923     const std::vector<uint8_t> NEW_PASSWD_VECTOR = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '1', '1', '1'};
2924     (void)passwd.SetValue(NEW_PASSWD_VECTOR.data(), NEW_PASSWD_VECTOR.size());
2925     EXPECT_TRUE(g_kvNbDelegatePtr->Rekey(passwd) == OK);
2926     g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2927     g_kvNbDelegatePtr = nullptr;
2928     option.passwd = passwd;
2929     g_mgr.GetKvStore("rekeyTest002", option, g_kvNbDelegateCallback);
2930     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2931     EXPECT_TRUE(g_kvDelegateStatus == OK);
2932     g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2933     g_kvNbDelegatePtr = nullptr;
2934 }
2935 
2936 /**
2937   * @tc.name: SetAndGetHandleTest001
2938   * @tc.desc: Test SetHandle and GetHandle.
2939   * @tc.type: FUNC
2940   * @tc.require:
2941   * @tc.author: lideshi
2942   */
2943 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SetAndGetHandleTest001, TestSize.Level0)
2944 {
2945     /**
2946      * @tc.steps:step1. Get the nb delegate.
2947      * @tc.expected: step1. Get results OK and non-null delegate.
2948      */
2949     KvStoreNbDelegate::Option option = {true, false, false};
2950     option.storageEngineType = GAUSSDB_RD;
2951     g_mgr.GetKvStore("distributed_nb_delegate_test_rd", option, g_kvNbDelegateCallback);
2952     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
2953     EXPECT_TRUE(g_kvDelegateStatus == OK);
2954 
2955     auto kvStoreImpl = static_cast<KvStoreNbDelegateImpl *>(g_kvNbDelegatePtr);
2956     void *handle = DBCommon::LoadGrdLib();
2957     kvStoreImpl->SetHandle(handle);
2958     DBCommon::UnLoadGrdLib(nullptr);
2959     DBCommon::UnLoadGrdLib(handle);
2960     handle = nullptr;
2961     /**
2962      * @tc.steps:step2. Close the kv store.
2963      * @tc.expected: step2. Results OK and delete successfully.
2964      */
2965     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
2966     EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_test_rd"), OK);
2967     g_kvNbDelegatePtr = nullptr;
2968 }
2969 
2970 /**
2971   * @tc.name: SetAndGetHandleTest002
2972   * @tc.desc: Test multi get delegate.
2973   * @tc.type: FUNC
2974   * @tc.require:
2975   * @tc.author: lideshi
2976   */
2977 HWTEST_F(DistributedDBInterfacesNBDelegateTest, SetAndGetHandleTest002, TestSize.Level0)
2978 {
2979     const std::string dbName = "storeId";
2980 
2981     KvStoreNbDelegate::Option option = {true, false, false};
2982     option.storageEngineType = GAUSSDB_RD;
2983     static int openCount = 2; // test open count 2
2984 
2985     std::vector<KvStoreNbDelegate *> delegates;
2986     KvStoreDelegateManager g_mgrTest("app1", "user1");
2987     g_mgrTest.SetKvStoreConfig(g_config);
2988     for (int i = 0; i < openCount; i++) {
2989         KvStoreNbDelegate *delegate = nullptr;
2990         DBStatus kvDelegateStatus = INVALID_ARGS;
2991         auto callback1 = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
2992             placeholders::_2, std::ref(kvDelegateStatus), std::ref(delegate));
2993         g_mgrTest.GetKvStore(dbName + std::to_string(i), option, callback1);
2994         ASSERT_TRUE(delegate != nullptr);
2995         EXPECT_TRUE(kvDelegateStatus == OK);
2996         delegates.push_back(delegate);
2997     }
2998 
2999     for (int i = 0; i < openCount; i++) {
3000         EXPECT_EQ(g_mgrTest.CloseKvStore(delegates[i]), OK);
3001         EXPECT_EQ(g_mgrTest.DeleteKvStore(dbName + std::to_string(i)), OK);
3002         delegates[i] = nullptr;
3003     }
3004 }
3005 }
3006