• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 #include <ctime>
17 #include <cmath>
18 #include <random>
19 #include <chrono>
20 #include <thread>
21 #include <string>
22 
23 #include "kv_store_delegate.h"
24 #include "kv_store_nb_delegate.h"
25 #include "kv_store_delegate_manager.h"
26 #include "distributed_test_tools.h"
27 #include "distributeddb_nb_test_tools.h"
28 #include "distributeddb_data_generator.h"
29 
30 using namespace std;
31 using namespace chrono;
32 using namespace testing;
33 #if defined TESTCASES_USING_GTEST_EXT
34 using namespace testing::ext;
35 #endif
36 using namespace std::placeholders;
37 using namespace DistributedDB;
38 using namespace DistributedDBDataGenerator;
39 
40 namespace DistributeddbNbCrud {
41 const unsigned int READ_RECORDS_NUM_START = 1;
42 const unsigned int READ_RECORDS_NUM_END = 1000;
43 const unsigned int WRITE_RECORDS_NUM_START = 1;
44 const unsigned int WRITE_RECORDS_NUM_END = 9999;
45 const unsigned int DELETE_RECORDS_NUM_START = 1;
46 const unsigned int DELETE_RECORDS_NUM_END = 1000;
47 
48 const unsigned long LONG_TIME_TEST_SECONDS = 10;
49 const unsigned long LONG_TIME_INTERVAL_MILLSECONDS = 5;
50 
51 const unsigned int RECORDS_NUM_THOUSAND = 1000;
52 
53 KvStoreNbDelegate *g_kvStoreNbDelegate = nullptr;
54 KvStoreDelegateManager *g_manager = nullptr;
55 
56 class DistributeddbNbCrudTest : public testing::Test {
57 public:
58     static void SetUpTestCase(void);
59     static void TearDownTestCase(void);
60     void SetUp();
61     void TearDown();
62 private:
63 };
64 
SetUpTestCase(void)65 void DistributeddbNbCrudTest::SetUpTestCase(void)
66 {
67 }
68 
TearDownTestCase(void)69 void DistributeddbNbCrudTest::TearDownTestCase(void)
70 {
71 }
72 
SetUp(void)73 void DistributeddbNbCrudTest::SetUp(void)
74 {
75     RemoveDir(DistributedDBConstant::NB_DIRECTOR);
76 
77     UnitTest *test = UnitTest::GetInstance();
78     ASSERT_NE(test, nullptr);
79     const TestInfo *testinfo = test->current_test_info();
80     ASSERT_NE(testinfo, nullptr);
81     string testCaseName = string(testinfo->name());
82     MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
83 
84     g_kvStoreNbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(g_manager, g_dbParameter1, g_option);
85     ASSERT_TRUE(g_manager != nullptr && g_kvStoreNbDelegate != nullptr);
86 }
87 
TearDown(void)88 void DistributeddbNbCrudTest::TearDown(void)
89 {
90     MST_LOG("TearDownTestCase after case.");
91     ASSERT_NE(g_manager, nullptr);
92     EXPECT_TRUE(EndCaseDeleteDB(g_manager, g_kvStoreNbDelegate, STORE_ID_1, g_option.isMemoryDb));
93     RemoveDir(DistributedDBConstant::NB_DIRECTOR);
94 }
95 
96 enum ReadOrWriteTag {
97     READ = 0,
98     WRITE = 1,
99     DELETE = 2
100 };
101 
102 struct ConcurParam {
103     unsigned int threadId_;
104     ReadOrWriteTag tag_;
105     Entry* entryPtr_;
106 };
107 static std::mutex g_concurOperMutex;
108 // the thread runnnig methods
ConcurOperThread(ConcurParam * args)109 void ConcurOperThread(ConcurParam* args)
110 {
111     auto paramsPtr = static_cast<ConcurParam *>(args);
112     DBStatus status;
113     Value valueResult;
114 
115     if (paramsPtr->tag_ == READ) {
116         status = DistributedDBNbTestTools::Get(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key, valueResult);
117         if (valueResult.size() != 0) {
118             EXPECT_EQ(status, OK);
119             EXPECT_TRUE(DistributedDBNbTestTools::isValueEquals(valueResult, paramsPtr->entryPtr_->value));
120         }
121     } else if (paramsPtr->tag_ == WRITE) {
122         status = DistributedDBNbTestTools::Put(*g_kvStoreNbDelegate,
123             paramsPtr->entryPtr_->key, paramsPtr->entryPtr_->value);
124         ASSERT_EQ(status, DBStatus::OK);
125 
126         status = DistributedDBNbTestTools::Get(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key, valueResult);
127         EXPECT_EQ(status, DBStatus::OK);
128         EXPECT_TRUE(DistributedDBNbTestTools::isValueEquals(valueResult, paramsPtr->entryPtr_->value));
129     } else {
130         std::lock_guard<std::mutex> lock(g_concurOperMutex);
131         status = DistributedDBNbTestTools::Get(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key, valueResult);
132         if (valueResult.size() != 0) {
133             status = DistributedDBNbTestTools::Delete(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key);
134             ASSERT_EQ(status, DBStatus::OK);
135 
136             valueResult = {};
137             status = DistributedDBNbTestTools::Get(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key, valueResult);
138             EXPECT_EQ(status, DBStatus::NOT_FOUND);
139             EXPECT_EQ(valueResult.size(), (unsigned long)0);
140         } else {
141             status = DistributedDBNbTestTools::Delete(*g_kvStoreNbDelegate, paramsPtr->entryPtr_->key);
142             ASSERT_EQ(status, DBStatus::OK);
143         }
144     }
145 }
146 
NbCalculateTime(ConcurParam * & nbThreadParam,const Entry entryCurrent,const SysTime & start,SysDurTime dur)147 double NbCalculateTime(ConcurParam *&nbThreadParam, const Entry entryCurrent, const SysTime &start, SysDurTime dur)
148 {
149     SysTime end;
150     nbThreadParam->entryPtr_->key = entryCurrent.key;
151     nbThreadParam->entryPtr_->value = entryCurrent.value;
152     std::thread thread = std::thread(ConcurOperThread, reinterpret_cast<ConcurParam *>(nbThreadParam));
153     thread.join();
154 
155     std::this_thread::sleep_for(std::chrono::duration<float, std::milli>(LONG_TIME_INTERVAL_MILLSECONDS));
156     end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
157     dur = std::chrono::duration_cast<chrono::microseconds>(end - start);
158     double operInterval = static_cast<double>(dur.count()) * chrono::microseconds::period::num
159         / chrono::microseconds::period::den;
160     delete nbThreadParam->entryPtr_;
161     nbThreadParam->entryPtr_ = nullptr;
162     delete nbThreadParam;
163     nbThreadParam = nullptr;
164     return operInterval;
165 }
166 
167 /*
168  * @tc.name: GetStoreid 001
169  * @tc.desc: Verify that can obtain storeid normally.
170  * @tc.type: FUNC
171  * @tc.require: SR000CCPOI
172  * @tc.author: luqianfu
173  */
174 HWTEST_F(DistributeddbNbCrudTest, GetStoreid001, TestSize.Level1)
175 {
176     /**
177      * @tc.steps: step1. create db and get storeId normally.
178      * @tc.expected: step1. success.
179      */
180     EXPECT_EQ(g_kvStoreNbDelegate->GetStoreId(), STORE_ID_1);
181 }
182 
183 /*
184  * @tc.name: SimpleAction 001
185  * @tc.desc: test that get data from local and sync db and they can't affect each other.
186  * @tc.type: FUNC
187  * @tc.require: SR000CCPOI
188  * @tc.author: luqianfu
189  */
190 HWTEST_F(DistributeddbNbCrudTest, SimpleAction001, TestSize.Level1)
191 {
192     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
193     EXPECT_TRUE(status == OK);
194     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2);
195     EXPECT_TRUE(status == OK);
196     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
197     EXPECT_TRUE(status == OK);
198     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
199     EXPECT_TRUE(status == OK);
200     status = g_kvStoreNbDelegate->Put(KEY_3, VALUE_3);
201     EXPECT_TRUE(status == OK);
202 
203     /**
204      * @tc.steps: step1. get value from local db when key = KEY_1.
205      * @tc.expected: step1. return value is VALUE_1.
206      */
207     Value valueResult;
208     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
209     EXPECT_EQ(status, OK);
210     EXPECT_EQ(valueResult, VALUE_1);
211     /**
212      * @tc.steps: step2. get value from local db when key = KEY_2.
213      * @tc.expected: step2. return value is VALUE_2.
214      */
215     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
216     EXPECT_EQ(status, OK);
217     EXPECT_EQ(valueResult, VALUE_2);
218     /**
219      * @tc.steps: step3. get value from local db when key = KEY_3.
220      * @tc.expected: step3. return value is NOT_FOUND.
221      */
222     status = g_kvStoreNbDelegate->GetLocal(KEY_3, valueResult);
223     EXPECT_EQ(status, NOT_FOUND);
224     /**
225      * @tc.steps: step4. get value from sync db when key = KEY_1.
226      * @tc.expected: step4. return value is VALUE_2.
227      */
228     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
229     EXPECT_EQ(status, OK);
230     EXPECT_EQ(valueResult, VALUE_2);
231     /**
232      * @tc.steps: step5. get value from sync db when key = KEY_2.
233      * @tc.expected: step5. return value is VALUE_1.
234      */
235     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
236     EXPECT_EQ(status, OK);
237     EXPECT_EQ(valueResult, VALUE_1);
238     /**
239      * @tc.steps: step6. get value from sync db when key = KEY_3.
240      * @tc.expected: step6. return value is VALUE_3.
241      */
242     status = g_kvStoreNbDelegate->Get(KEY_3, valueResult);
243     EXPECT_EQ(status, OK);
244     EXPECT_EQ(valueResult, VALUE_3);
245     g_kvStoreNbDelegate->Delete(KEY_1);
246     g_kvStoreNbDelegate->Delete(KEY_2);
247     g_kvStoreNbDelegate->Delete(KEY_3);
248     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
249     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
250 }
251 
252 /*
253  * @tc.name: SimpleAction 002
254  * @tc.desc: test put data from local and sync db and they can't affect each other.
255  * @tc.type: FUNC
256  * @tc.require: SR000CCPOI
257  * @tc.author: luqianfu
258  */
259 HWTEST_F(DistributeddbNbCrudTest, SimpleAction002, TestSize.Level1)
260 {
261     /**
262      * @tc.steps: step1. put (KEY_1, VALUE_1) to local db.
263      * @tc.expected: step1. put success.
264      */
265     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
266     EXPECT_TRUE(status == OK);
267     /**
268      * @tc.steps: step2. put (KEY_2, VALUE_2) to local db.
269      * @tc.expected: step2. put success.
270      */
271     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2);
272     EXPECT_TRUE(status == OK);
273     /**
274      * @tc.steps: step3. put (KEY_1, VALUE_2) to sync db.
275      * @tc.expected: step3. put success.
276      */
277     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
278     EXPECT_TRUE(status == OK);
279     /**
280      * @tc.steps: step4. put (KEY_2, VALUE_1) to sync db.
281      * @tc.expected: step4. put success.
282      */
283     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
284     EXPECT_TRUE(status == OK);
285 
286     /**
287      * @tc.steps: step6. get data from local db where key = KEY_1.
288      * @tc.expected: step6. return value is VALUE_1.
289      */
290     Value valueResult;
291     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
292     EXPECT_EQ(status, OK);
293     EXPECT_EQ(valueResult, VALUE_1);
294     /**
295      * @tc.steps: step7. get data from local db where key = KEY_2.
296      * @tc.expected: step7. return value is VALUE_2.
297      */
298     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
299     EXPECT_EQ(status, OK);
300     EXPECT_EQ(valueResult, VALUE_2);
301     /**
302      * @tc.steps: step8. get data from sync db where key = KEY_1.
303      * @tc.expected: step8. return value is VALUE_2.
304      */
305     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
306     EXPECT_EQ(status, OK);
307     EXPECT_EQ(valueResult, VALUE_2);
308     /**
309      * @tc.steps: step9. get data from sync db where key = KEY_2.
310      * @tc.expected: step9. return value is VALUE_1.
311      */
312     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
313     EXPECT_EQ(status, OK);
314     EXPECT_EQ(valueResult, VALUE_1);
315 
316     g_kvStoreNbDelegate->Delete(KEY_1);
317     g_kvStoreNbDelegate->Delete(KEY_2);
318     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
319     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
320 }
321 
322 /*
323  * @tc.name: SimpleAction 003
324  * @tc.desc: test that update data to db, and local and sync db can't affect each other.
325  * @tc.type: FUNC
326  * @tc.require: SR000CCPOI
327  * @tc.author: luqianfu
328  */
329 HWTEST_F(DistributeddbNbCrudTest, SimpleAction003, TestSize.Level1)
330 {
331     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
332     EXPECT_TRUE(status == OK);
333     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2);
334     EXPECT_TRUE(status == OK);
335     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
336     EXPECT_TRUE(status == OK);
337     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
338     EXPECT_TRUE(status == OK);
339 
340     /**
341      * @tc.steps: step1. update data (KEY_1, VALUE_2) to local db.
342      * @tc.expected: step1.update success.
343      */
344     Value valueResult;
345     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
346     EXPECT_TRUE(status == OK);
347     /**
348      * @tc.steps: step2. update data (KEY_2, VALUE_1) to local db.
349      * @tc.expected: step2.update success.
350      */
351     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_1);
352     EXPECT_TRUE(status == OK);
353     /**
354      * @tc.steps: step3. update data (KEY_1, VALUE_1) to sync db.
355      * @tc.expected: step3.update success.
356      */
357     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_1);
358     EXPECT_TRUE(status == OK);
359     /**
360      * @tc.steps: step4. update data (KEY_2, VALUE_2) to sync db.
361      * @tc.expected: step4.update success.
362      */
363     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_2);
364     EXPECT_TRUE(status == OK);
365     /**
366      * @tc.steps: step5. get data from local db where key = KEY_1.
367      * @tc.expected: step5. return value is VALUE_2.
368      */
369     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
370     EXPECT_EQ(status, OK);
371     EXPECT_EQ(valueResult, VALUE_2);
372     /**
373      * @tc.steps: step6. get data from local db where key = KEY_2.
374      * @tc.expected: step6. return value is VALUE_1.
375      */
376     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
377     EXPECT_EQ(status, OK);
378     EXPECT_EQ(valueResult, VALUE_1);
379     /**
380      * @tc.steps: step7. get data from sync db where key = KEY_1.
381      * @tc.expected: step7. return value is VALUE_1.
382      */
383     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
384     EXPECT_EQ(status, OK);
385     EXPECT_EQ(valueResult, VALUE_1);
386     /**
387      * @tc.steps: step8. get data from sync db where key = KEY_2.
388      * @tc.expected: step8. return value is VALUE_2.
389      */
390     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
391     EXPECT_EQ(status, OK);
392     EXPECT_EQ(valueResult, VALUE_2);
393 
394     g_kvStoreNbDelegate->Delete(KEY_1);
395     g_kvStoreNbDelegate->Delete(KEY_2);
396     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
397     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
398 }
399 
400 /*
401  * @tc.name: SimpleAction 004
402  * @tc.desc: test that can delete entries of db, and local and sync db can't affect each other.
403  * @tc.type: FUNC
404  * @tc.require: SR000CCPOI
405  * @tc.author: luqianfu
406  */
407 HWTEST_F(DistributeddbNbCrudTest, SimpleAction004, TestSize.Level1)
408 {
409     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
410     EXPECT_TRUE(status == OK);
411     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2);
412     EXPECT_TRUE(status == OK);
413     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
414     EXPECT_TRUE(status == OK);
415     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
416     EXPECT_TRUE(status == OK);
417     /**
418      * @tc.steps: step1. delete entries from local db where key = KEY_1.
419      * @tc.expected: step1. delete success.
420      */
421     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
422     EXPECT_TRUE(status == OK);
423     /**
424      * @tc.steps: step2. delete entries from sync db where key = KEY_2.
425      * @tc.expected: step2. delete success.
426      */
427     status = g_kvStoreNbDelegate->Delete(KEY_2);
428     EXPECT_TRUE(status == OK);
429     /**
430      * @tc.steps: step3. get data from local where key = KEY_1.
431      * @tc.expected: step3. return value is NOT_FOUND.
432      */
433     Value valueResult;
434     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
435     EXPECT_EQ(status, NOT_FOUND);
436     /**
437      * @tc.steps: step4. get data from local where key = KEY_2.
438      * @tc.expected: step4. return value is VALUE_2.
439      */
440     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
441     EXPECT_EQ(status, OK);
442     EXPECT_EQ(valueResult, VALUE_2);
443     /**
444      * @tc.steps: step5. get data from sync where key = KEY_1.
445      * @tc.expected: step5. return value is VALUE_2.
446      */
447     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
448     EXPECT_EQ(status, OK);
449     EXPECT_EQ(valueResult, VALUE_2);
450     /**
451      * @tc.steps: step6. get data from sync where key = KEY_2.
452      * @tc.expected: step6. return value is NOT_FOUND.
453      */
454     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
455     EXPECT_EQ(status, NOT_FOUND);
456 
457     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
458     g_kvStoreNbDelegate->Delete(KEY_1);
459 }
460 
461 /*
462  * @tc.name: SimpleAction 005
463  * @tc.desc: test that can query entries that don't exist.
464  * @tc.type: FUNC
465  * @tc.require: SR000CCPOI
466  * @tc.author: luqianfu
467  */
468 HWTEST_F(DistributeddbNbCrudTest, SimpleAction005, TestSize.Level1)
469 {
470     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
471     EXPECT_TRUE(status == OK);
472     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
473     EXPECT_TRUE(status == OK);
474 
475     /**
476      * @tc.steps: step1. get data from local where key = KEY_2.
477      * @tc.expected: step1. return value is NOT_FOUND.
478      */
479     Value valueResult;
480     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
481     EXPECT_EQ(status, NOT_FOUND);
482     /**
483      * @tc.steps: step2. get data from sync where key = KEY_1.
484      * @tc.expected: step2. return value is NOT_FOUND.
485      */
486     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
487     EXPECT_EQ(status, NOT_FOUND);
488     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
489     g_kvStoreNbDelegate->Delete(KEY_2);
490 }
491 
492 /*
493  * @tc.name: RepeatAction 001
494  * @tc.desc: test that query the same entries repeatedly.
495  * @tc.type: FUNC
496  * @tc.require: SR000CCPOI
497  * @tc.author: luqianfu
498  */
499 HWTEST_F(DistributeddbNbCrudTest, RepeatAction001, TestSize.Level1)
500 {
501     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
502     EXPECT_TRUE(status == OK);
503     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
504     EXPECT_TRUE(status == OK);
505 
506     /**
507      * @tc.steps: step1. get data from local data where key = KEY_1.
508      * @tc.expected: step1. return value is VALUE_1.
509      */
510     Value valueResult;
511     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
512     EXPECT_EQ(status, OK);
513     EXPECT_EQ(valueResult, VALUE_1);
514     /**
515      * @tc.steps: step2. get data from local data where key = KEY_1.
516      * @tc.expected: step2. return value is VALUE_1.
517      */
518     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
519     EXPECT_EQ(status, OK);
520     EXPECT_EQ(valueResult, VALUE_1);
521     /**
522      * @tc.steps: step3. get data from sync data where key = KEY_1.
523      * @tc.expected: step3. return value is VALUE_2.
524      */
525     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
526     EXPECT_EQ(status, OK);
527     EXPECT_EQ(valueResult, VALUE_2);
528     /**
529      * @tc.steps: step4. get data from sync data where key = KEY_1.
530      * @tc.expected: step4. return value is VALUE_2.
531      */
532     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
533     EXPECT_EQ(status, OK);
534     EXPECT_EQ(valueResult, VALUE_2);
535     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
536     g_kvStoreNbDelegate->Delete(KEY_1);
537 }
538 
539 /*
540  * @tc.name: RepeatAction 002
541  * @tc.desc: test that put the same entries repeatedly.
542  * @tc.type: FUNC
543  * @tc.require: SR000CCPOI
544  * @tc.author: luqianfu
545  */
546 HWTEST_F(DistributeddbNbCrudTest, RepeatAction002, TestSize.Level1)
547 {
548     /**
549      * @tc.steps: step1. put (KEY_1, VALUE_1) to local db.
550      * @tc.expected: step1. put succeed.
551      */
552     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
553     EXPECT_TRUE(status == OK);
554     /**
555      * @tc.steps: step2. put (KEY_1, VALUE_1) to local db again.
556      * @tc.expected: step2. put succeed.
557      */
558     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
559     EXPECT_TRUE(status == OK);
560     /**
561      * @tc.steps: step3. put (KEY_1, VALUE_2) to sync db.
562      * @tc.expected: step3. put succeed.
563      */
564     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
565     EXPECT_TRUE(status == OK);
566     /**
567      * @tc.steps: step4. put (KEY_1, VALUE_2) to sync db.
568      * @tc.expected: step4. put succeed.
569      */
570     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
571     EXPECT_TRUE(status == OK);
572 
573     /**
574      * @tc.steps: step5. get data from local db where key = KEY_1.
575      * @tc.expected: step5. return value is VALUE_1.
576      */
577     Value valueResult;
578     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
579     EXPECT_EQ(status, OK);
580     EXPECT_EQ(valueResult, VALUE_1);
581     /**
582      * @tc.steps: step6. get data from sync db where key = KEY_1.
583      * @tc.expected: step6. return value is VALUE_2.
584      */
585     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
586     EXPECT_EQ(status, OK);
587     EXPECT_EQ(valueResult, VALUE_2);
588 
589     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
590     g_kvStoreNbDelegate->Delete(KEY_1);
591 }
592 
593 /*
594  * @tc.name: RepeatAction 003
595  * @tc.desc: test that can update the same entries repeatedly.
596  * @tc.type: FUNC
597  * @tc.require: SR000CCPOI
598  * @tc.author: luqianfu
599  */
600 HWTEST_F(DistributeddbNbCrudTest, RepeatAction003, TestSize.Level1)
601 {
602     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
603     EXPECT_TRUE(status == OK);
604     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
605     EXPECT_TRUE(status == OK);
606     /**
607      * @tc.steps: step1. update (KEY_1, VALUE_2) to local db.
608      * @tc.expected: step1. update success.
609      */
610     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
611     EXPECT_TRUE(status == OK);
612     /**
613      * @tc.steps: step2. update (KEY_1, VALUE_2) to local db again.
614      * @tc.expected: step2. update success.
615      */
616     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
617     EXPECT_TRUE(status == OK);
618     /**
619      * @tc.steps: step3. update (KEY_1, VALUE_1) to sync db.
620      * @tc.expected: step3. update success.
621      */
622     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_1);
623     EXPECT_TRUE(status == OK);
624     /**
625      * @tc.steps: step4. update (KEY_1, VALUE_1) to sync db again.
626      * @tc.expected: step4. update success.
627      */
628     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_1);
629     EXPECT_TRUE(status == OK);
630 
631     /**
632      * @tc.steps: step5. get data from local where key KEY_1.
633      * @tc.expected: step5. return value is VALUE_2.
634      */
635     Value valueResult;
636     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
637     EXPECT_EQ(status, OK);
638     EXPECT_EQ(valueResult, VALUE_2);
639     /**
640      * @tc.steps: step6. get data from sync where key KEY_1.
641      * @tc.expected: step6. return value is VALUE_1.
642      */
643     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
644     EXPECT_EQ(status, OK);
645     EXPECT_EQ(valueResult, VALUE_1);
646 
647     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
648     g_kvStoreNbDelegate->Delete(KEY_1);
649 }
650 
651 /*
652  * @tc.name: RepeatAction 004
653  * @tc.desc: verify that it can delete the same entries repeatedly.
654  * @tc.type: FUNC
655  * @tc.require: SR000CCPOI
656  * @tc.author: luqianfu
657  */
658 HWTEST_F(DistributeddbNbCrudTest, RepeatAction004, TestSize.Level1)
659 {
660     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
661     EXPECT_TRUE(status == OK);
662     status = g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2);
663     EXPECT_TRUE(status == OK);
664     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
665     EXPECT_TRUE(status == OK);
666     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
667     EXPECT_TRUE(status == OK);
668 
669     /**
670      * @tc.steps: step1. delete KEY_1 from local db.
671      * @tc.expected: step1. delete succeed.
672      */
673     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
674     EXPECT_EQ(status, OK);
675     /**
676      * @tc.steps: step2. delete KEY_2 from sync db.
677      * @tc.expected: step2. delete succeed.
678      */
679     status = g_kvStoreNbDelegate->Delete(KEY_2);
680     EXPECT_EQ(status, OK);
681     /**
682      * @tc.steps: step3. delete KEY_1 from local db again.
683      * @tc.expected: step3. delete failed but return OK.
684      */
685     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
686     EXPECT_EQ(status, OK);
687     /**
688      * @tc.steps: step4. delete KEY_2 from sync db again.
689      * @tc.expected: step4. delete failed but return OK.
690      */
691     status = g_kvStoreNbDelegate->Delete(KEY_2);
692     EXPECT_EQ(status, OK);
693     /**
694      * @tc.steps: step5. get data from local db where key = KEY_1.
695      * @tc.expected: step5. return value is NOT_FOUND.
696      */
697     Value realValue;
698     status = g_kvStoreNbDelegate->GetLocal(KEY_1, realValue);
699     EXPECT_EQ(status, NOT_FOUND);
700     /**
701      * @tc.steps: step6. get data from local db where key = KEY_2.
702      * @tc.expected: step6. return value is VALUE_2.
703      */
704     status = g_kvStoreNbDelegate->GetLocal(KEY_2, realValue);
705     EXPECT_EQ(status, OK);
706     EXPECT_EQ(realValue, VALUE_2);
707     /**
708      * @tc.steps: step7. get data from sync db where key = KEY_1.
709      * @tc.expected: step7. return value is VALUE_2.
710      */
711     status = g_kvStoreNbDelegate->Get(KEY_1, realValue);
712     EXPECT_EQ(status, OK);
713     EXPECT_EQ(realValue, VALUE_2);
714     /**
715      * @tc.steps: step8. get data from sync db where key = KEY_2.
716      * @tc.expected: step8. return value is NOT_FOUND.
717      */
718     status = g_kvStoreNbDelegate->Get(KEY_2, realValue);
719     EXPECT_EQ(status, NOT_FOUND);
720 
721     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
722     g_kvStoreNbDelegate->Delete(KEY_1);
723 }
724 
725 /*
726  * @tc.name: RepeatAction 005
727  * @tc.desc: test that delete the entries do not exist will return OK.
728  * @tc.type: FUNC
729  * @tc.require: SR000CCPOI
730  * @tc.author: luqianfu
731  */
732 HWTEST_F(DistributeddbNbCrudTest, RepeatAction005, TestSize.Level1)
733 {
734     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
735     EXPECT_TRUE(status == OK);
736     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
737     EXPECT_TRUE(status == OK);
738     /**
739      * @tc.steps: step1. delete KEY_2 from local db.
740      * @tc.expected: step1. delete failed but return OK.
741      */
742     status = g_kvStoreNbDelegate->DeleteLocal(KEY_2);
743     EXPECT_EQ(status, OK);
744     /**
745      * @tc.steps: step2. delete KEY_1 from sync db.
746      * @tc.expected: step2. delete failed but return OK.
747      */
748     status = g_kvStoreNbDelegate->Delete(KEY_1);
749     EXPECT_EQ(status, OK);
750 
751     /**
752      * @tc.steps: step3. get data from local db where key = KEY_1.
753      * @tc.expected: step3. return value is VALUE_1.
754      */
755     Value valueResult;
756     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
757     EXPECT_EQ(status, OK);
758     EXPECT_EQ(valueResult, VALUE_1);
759     /**
760      * @tc.steps: step4. get data from local db where key = KEY_2.
761      * @tc.expected: step4. return value is NOT_FOUND.
762      */
763     status = g_kvStoreNbDelegate->GetLocal(KEY_2, valueResult);
764     EXPECT_EQ(status, NOT_FOUND);
765     /**
766      * @tc.steps: step5. get data from sync db where key = KEY_1.
767      * @tc.expected: step5. return value is NOT_FOUND.
768      */
769     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
770     EXPECT_EQ(status, NOT_FOUND);
771     /**
772      * @tc.steps: step6. get data from sync db where key = KEY_2.
773      * @tc.expected: step6. return value is VALUE_1.
774      */
775     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
776     EXPECT_EQ(status, OK);
777     EXPECT_EQ(valueResult, VALUE_1);
778     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
779     g_kvStoreNbDelegate->Delete(KEY_2);
780 }
781 
782 /*
783  * @tc.name: RepeatAction 006
784  * @tc.desc: test that can put entries which was deleted.
785  * @tc.type: FUNC
786  * @tc.require: SR000CCPOI
787  * @tc.author: luqianfu
788  */
789 HWTEST_F(DistributeddbNbCrudTest, RepeatAction006, TestSize.Level1)
790 {
791     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
792     EXPECT_TRUE(status == OK);
793     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
794     EXPECT_TRUE(status == OK);
795     /**
796      * @tc.steps: step1. delete KEY_1 from local db.
797      * @tc.expected: step1. delete succeed.
798      */
799     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
800     EXPECT_EQ(status, OK);
801     /**
802      * @tc.steps: step2. delete KEY_1 from sync db.
803      * @tc.expected: step2. delete succeed.
804      */
805     g_kvStoreNbDelegate->Delete(KEY_1);
806     EXPECT_EQ(status, OK);
807     /**
808      * @tc.steps: step3. update (KEY_1, VALUE_1) to local db.
809      * @tc.expected: step3. update succeed.
810      */
811     Value valueResult;
812     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
813     EXPECT_TRUE(status == OK);
814     /**
815      * @tc.steps: step4. update (KEY_1, VALUE_2) to sync db.
816      * @tc.expected: step4. update succeed.
817      */
818     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_2);
819     EXPECT_TRUE(status == OK);
820     /**
821      * @tc.steps: step5. get data from local db where key = KEY_1.
822      * @tc.expected: step5. return value is VALUE_1.
823      */
824     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
825     EXPECT_EQ(status, OK);
826     EXPECT_EQ(valueResult, VALUE_1);
827     /**
828      * @tc.steps: step6. get data from sync db where key = KEY_1.
829      * @tc.expected: step6. return value is VALUE_2.
830      */
831     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
832     EXPECT_EQ(status, OK);
833     EXPECT_EQ(valueResult, VALUE_2);
834     /**
835      * @tc.steps: step7. get data from local db where key = KEY_1.
836      * @tc.expected: step7. return value is VALUE_2.
837      */
838     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
839     EXPECT_TRUE(status == OK);
840     /**
841      * @tc.steps: step8. get data from sync db where key = KEY_1.
842      * @tc.expected: step8. return value is VALUE_1.
843      */
844     status = g_kvStoreNbDelegate->Put(KEY_1, VALUE_1);
845     EXPECT_TRUE(status == OK);
846     /**
847      * @tc.steps: step9. get data from local db where key = KEY_1.
848      * @tc.expected: step9. return value is VALUE_2.
849      */
850     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
851     EXPECT_EQ(status, OK);
852     EXPECT_EQ(valueResult, VALUE_2);
853     /**
854      * @tc.steps: step10. get data from sync db where key = KEY_1.
855      * @tc.expected: step10. return value is VALUE_1.
856      */
857     status = g_kvStoreNbDelegate->Get(KEY_1, valueResult);
858     EXPECT_EQ(status, OK);
859     EXPECT_EQ(valueResult, VALUE_1);
860     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
861     g_kvStoreNbDelegate->Delete(KEY_1);
862 }
863 
864 /*
865  * @tc.name: Pressure 001
866  * @tc.desc: verify that the get interface can check the key params' effectiveness.
867  * @tc.type: FUNC
868  * @tc.require: SR000CCPOI
869  * @tc.author: luqianfu
870  */
871 HWTEST_F(DistributeddbNbCrudTest, Pressure001, TestSize.Level1)
872 {
873     DistributedDB::Key eKey1, eKey2, eKey3, eKey4;
874     eKey1 = {};
875     eKey2.assign(ONE_K_LONG_STRING, (uint8_t)'a');
876     eKey3.assign(ONE_K_LONG_STRING + 1, (uint8_t)'b');
877     eKey4 = { 'a', 'b', 'c', 'D', 'E', 'F', '2', '4', '5', 199, 1, 255, 0 };
878 
879     /**
880      * @tc.steps: step1. get data from local db where key = eKey1.
881      * @tc.expected: step1. get failed and return INVALID_ARGS.
882      */
883     Value valueResult;
884     DBStatus status = g_kvStoreNbDelegate->GetLocal(eKey1, valueResult);
885     EXPECT_EQ(status, INVALID_ARGS);
886     /**
887      * @tc.steps: step2. get data from local db where key = eKey2.
888      * @tc.expected: step2. get failed and return NOT_FOUND.
889      */
890     status = g_kvStoreNbDelegate->GetLocal(eKey2, valueResult);
891     EXPECT_EQ(status, NOT_FOUND);
892     /**
893      * @tc.steps: step3. get data from local db where key = eKey3.
894      * @tc.expected: step3. get failed and return INVALID_ARGS.
895      */
896     status = g_kvStoreNbDelegate->GetLocal(eKey3, valueResult);
897     EXPECT_EQ(status, INVALID_ARGS);
898     /**
899      * @tc.steps: step4. get data from local db where key = eKey4.
900      * @tc.expected: step4. get failed and return NOT_FOUND.
901      */
902     status = g_kvStoreNbDelegate->GetLocal(eKey4, valueResult);
903     EXPECT_EQ(status, NOT_FOUND);
904     /**
905      * @tc.steps: step5. get data from local db where key = eKey1.
906      * @tc.expected: step5. get failed and return INVALID_ARGS.
907      */
908     status = g_kvStoreNbDelegate->Get(eKey1, valueResult);
909     EXPECT_EQ(status, INVALID_ARGS);
910     /**
911      * @tc.steps: step6. get data from local db where key = eKey2.
912      * @tc.expected: step6. get failed and return NOT_FOUND.
913      */
914     status = g_kvStoreNbDelegate->Get(eKey2, valueResult);
915     EXPECT_EQ(status, NOT_FOUND);
916     /**
917      * @tc.steps: step7. get data from local db where key = eKey.
918      * @tc.expected: step7. get failed and return INVALID_ARGS.
919      */
920     status = g_kvStoreNbDelegate->Get(eKey3, valueResult);
921     EXPECT_EQ(status, INVALID_ARGS);
922     /**
923      * @tc.steps: step8. get data from local db where key = eKey4.
924      * @tc.expected: step8. get failed and return NOT_FOUND.
925      */
926     status = g_kvStoreNbDelegate->Get(eKey4, valueResult);
927     EXPECT_EQ(status, NOT_FOUND);
928 }
929 /*
930  * @tc.name: Pressure 002
931  * @tc.desc: verify that the put interface can check key params 'effectiveness and 4M value.
932  * @tc.type: FUNC
933  * @tc.require: SR000CCPOI
934  * @tc.author: luqianfu
935  */
936 HWTEST_F(DistributeddbNbCrudTest, Pressure002, TestSize.Level1)
937 {
938     DistributedDB::Value longValue;
939     longValue.assign(FOUR_M_LONG_STRING, (uint8_t)'a');
940 
941     DistributedDB::Key eKey1, eKey2, eKey3, eKey4;
942     eKey1 = {};
943     eKey2.assign(ONE_K_LONG_STRING, (uint8_t)'a');
944     eKey3.assign(ONE_K_LONG_STRING + 1, (uint8_t)'b');
945     eKey4 = { 'a', 'b', 'c', 'D', 'E', 'F', '2', '4', '5', 199, 1, 255, 0 };
946     /**
947      * @tc.steps: step1. put (eKey1, longValue) to local db, among which ekey1 = {}.
948      * @tc.expected: step1. put failed, and return INVALID_ARGS.
949      */
950     DBStatus status = g_kvStoreNbDelegate->PutLocal(eKey1, longValue);
951     EXPECT_TRUE(status == INVALID_ARGS);
952     /**
953      * @tc.steps: step2. put (eKey2, longValue) to local db, among which size of ekey2 is 1K.
954      * @tc.expected: step2. put succeed.
955      */
956     status = g_kvStoreNbDelegate->PutLocal(eKey2, longValue);
957     EXPECT_TRUE(status == OK);
958     /**
959      * @tc.steps: step3. put (eKey3, longValue) to local db, among which size of ekey3 is 1k + 1.
960      * @tc.expected: step3. put failed, and return INVALID_ARGS.
961      */
962     status = g_kvStoreNbDelegate->PutLocal(eKey3, longValue);
963     EXPECT_TRUE(status == INVALID_ARGS);
964     /**
965      * @tc.steps: step4. put (eKey4, longValue) to local db, among which ekey4 contains [a-zA-Z0-9], [\0-\255],
966      *    chinese and latins, and size of ekey4 is big than 0 and less or equal than 1K.
967      * @tc.expected: step4. put succeed.
968      */
969     status = g_kvStoreNbDelegate->PutLocal(eKey4, longValue);
970     EXPECT_TRUE(status == OK);
971     /**
972      * @tc.steps: step5. put (eKey1, longValue) to sync db, among which ekey1 = {}.
973      * @tc.expected: step5. put failed, and return INVALID_ARGS.
974      */
975     status = g_kvStoreNbDelegate->Put(eKey1, longValue);
976     EXPECT_TRUE(status == INVALID_ARGS);
977     /**
978      * @tc.steps: step6. put (eKey2, longValue) to sync db, among which size of ekey2 is 1K.
979      * @tc.expected: step6. put succeed.
980      */
981     status = g_kvStoreNbDelegate->Put(eKey2, longValue);
982     EXPECT_TRUE(status == OK);
983     /**
984      * @tc.steps: step7. put (eKey3, longValue) to sync db, among which size of ekey3 is 1k + 1.
985      * @tc.expected: step7. put failed, and return INVALID_ARGS.
986      */
987     status = g_kvStoreNbDelegate->Put(eKey3, longValue);
988     EXPECT_TRUE(status == INVALID_ARGS);
989     /**
990      * @tc.steps: step8. put (eKey4, longValue) to local db, among which ekey4 contains [a-zA-Z0-9], [\0-\255],
991      *    chinese and latins, and size of ekey4 is big than 0 and less or equal than 1K.
992      * @tc.expected: step8. put succeed.
993      */
994     status = g_kvStoreNbDelegate->Put(eKey4, longValue);
995     EXPECT_TRUE(status == OK);
996     g_kvStoreNbDelegate->DeleteLocal(eKey1);
997     g_kvStoreNbDelegate->DeleteLocal(eKey2);
998     g_kvStoreNbDelegate->DeleteLocal(eKey3);
999     g_kvStoreNbDelegate->DeleteLocal(eKey4);
1000     g_kvStoreNbDelegate->Delete(eKey1);
1001     g_kvStoreNbDelegate->Delete(eKey2);
1002     g_kvStoreNbDelegate->Delete(eKey3);
1003     g_kvStoreNbDelegate->Delete(eKey4);
1004 }
1005 /*
1006  * @tc.name: Pressure 003
1007  * @tc.desc: verify that the update interface can check key params' effectiveness and value params' effectiveness.
1008  * @tc.type: FUNC
1009  * @tc.require: SR000CCPOI
1010  * @tc.author: luqianfu
1011  */
1012 HWTEST_F(DistributeddbNbCrudTest, Pressure003, TestSize.Level1)
1013 {
1014     DistributedDB::Value alphanumValue1, alphanumValue2, alphanumValue3, alphanumValue4;
1015     alphanumValue1 = {};
1016     alphanumValue2.assign(FOUR_M_LONG_STRING, (uint8_t)'a');
1017     alphanumValue3.assign(FOUR_M_LONG_STRING + 1, (uint8_t)'b');
1018     int chr;
1019     for (chr = 'a'; chr <= 'z'; ++chr) {
1020         alphanumValue4.push_back(chr);
1021     }
1022     for (chr = 'A'; chr <= 'Z'; ++chr) {
1023         alphanumValue4.push_back(chr);
1024     }
1025     for (chr = '0'; chr <= '9'; ++chr) {
1026         alphanumValue4.push_back(chr);
1027     }
1028     alphanumValue4.push_back(1);
1029     alphanumValue4.push_back(0);
1030     alphanumValue4.push_back(255);
1031     alphanumValue4.push_back(9);
1032     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1)) == OK);
1033     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_2, VALUE_2)) == OK);
1034     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_3, VALUE_3)) == OK);
1035     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_4, VALUE_4)) == OK);
1036     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_1, VALUE_1)) == OK);
1037     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_2, VALUE_2)) == OK);
1038     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_3, VALUE_3)) == OK);
1039     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_4, VALUE_4)) == OK);
1040     /**
1041      * @tc.steps: step1. put (KEY_1, alphanumValue1) to local db, among which size of alphanumValue1 0.
1042      * @tc.expected: step1. put succeed and return OK.
1043      */
1044     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_1, alphanumValue1)) == OK);
1045     /**
1046      * @tc.steps: step2. put (KEY_2, alphanumValue2) to local db, among which size of alphanumValue2 4M.
1047      * @tc.expected: step2. put succeed and return OK.
1048      */
1049     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_2, alphanumValue2)) == OK);
1050     /**
1051      * @tc.steps: step3. put (KEY_3, alphanumValue3) to local db, among which size of alphanumValue3 (4M + 1).
1052      * @tc.expected: step3. put failed and return INVALID_ARGS.
1053      */
1054     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_3, alphanumValue3)) == INVALID_ARGS);
1055     /**
1056      * @tc.steps: step4. put (KEY_4, alphanumValue4) to local db, among which alphanumValue4
1057      *    contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1058      * @tc.expected: step4. put succeed and return OK.
1059      */
1060     EXPECT_TRUE((g_kvStoreNbDelegate->PutLocal(KEY_4, alphanumValue4)) == OK);
1061     /**
1062      * @tc.steps: step5. put (KEY_1, alphanumValue1) to sync db, among which size of alphanumValue1 0.
1063      * @tc.expected: step5. put succeed and return OK.
1064      */
1065     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_1, alphanumValue1)) == OK);
1066     /**
1067      * @tc.steps: step6. put (KEY_2, alphanumValue2) to sync db, among which size of alphanumValue2 4M.
1068      * @tc.expected: step6. put succeed and return OK.
1069      */
1070     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_2, alphanumValue2)) == OK);
1071     /**
1072      * @tc.steps: step7. put (KEY_3, alphanumValue3) to local db, among which size of alphanumValue3 (4M + 1).
1073      * @tc.expected: step7. put failed and return INVALID_ARGS.
1074      */
1075     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_3, alphanumValue3)) == INVALID_ARGS);
1076     /**
1077      * @tc.steps: step8. put (KEY_4, alphanumValue4) to local db, among which alphanumValue4
1078      *    contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1079      * @tc.expected: step8. put succeed and return OK.
1080      */
1081     EXPECT_TRUE((g_kvStoreNbDelegate->Put(KEY_4, alphanumValue4)) == OK);
1082     g_kvStoreNbDelegate->DeleteLocal(KEY_1);
1083     g_kvStoreNbDelegate->DeleteLocal(KEY_2);
1084     g_kvStoreNbDelegate->DeleteLocal(KEY_3);
1085     g_kvStoreNbDelegate->DeleteLocal(KEY_4);
1086     g_kvStoreNbDelegate->Delete(KEY_1);
1087     g_kvStoreNbDelegate->Delete(KEY_2);
1088     g_kvStoreNbDelegate->Delete(KEY_3);
1089     g_kvStoreNbDelegate->Delete(KEY_4);
1090 }
1091 
1092 /*
1093  * @tc.name: Pressure 004
1094  * @tc.desc: verify that check value params' effectiveness of delete interface.
1095  * @tc.type: FUNC
1096  * @tc.require: SR000CCPOI
1097  * @tc.author: luqianfu
1098  */
1099 HWTEST_F(DistributeddbNbCrudTest, Pressure004, TestSize.Level1)
1100 {
1101     DistributedDB::Key eKey1, eKey2, eKey3, eKey4;
1102     eKey1 = {};
1103     eKey2.assign(ONE_K_LONG_STRING, (uint8_t)'a');
1104     eKey3.assign(ONE_K_LONG_STRING + 1, (uint8_t)'b');
1105     eKey4 = { 'a', 'b', 'c', 'D', 'E', 'F', '2', '4', '5', 199, 255, 0, 1 };
1106     DBStatus status = g_kvStoreNbDelegate->PutLocal(eKey1, VALUE_1);
1107     EXPECT_TRUE(status == INVALID_ARGS);
1108     status = g_kvStoreNbDelegate->PutLocal(eKey2, VALUE_1);
1109     EXPECT_TRUE(status == OK);
1110     status = g_kvStoreNbDelegate->PutLocal(eKey3, VALUE_1);
1111     EXPECT_TRUE(status == INVALID_ARGS);
1112     status = g_kvStoreNbDelegate->PutLocal(eKey4, VALUE_1);
1113     EXPECT_TRUE(status == OK);
1114     status = g_kvStoreNbDelegate->Put(eKey1, VALUE_2);
1115     EXPECT_TRUE(status == INVALID_ARGS);
1116     status = g_kvStoreNbDelegate->Put(eKey2, VALUE_2);
1117     EXPECT_TRUE(status == OK);
1118     status = g_kvStoreNbDelegate->Put(eKey3, VALUE_2);
1119     EXPECT_TRUE(status == INVALID_ARGS);
1120     status = g_kvStoreNbDelegate->Put(eKey4, VALUE_2);
1121     EXPECT_TRUE(status == OK);
1122     /**
1123      * @tc.steps: step1. delete eKey1 from local db, where key = eKey1, which eKey1 is null.
1124      * @tc.expected: step1. delete failed and return INVALID_ARGS.
1125      */
1126     status = g_kvStoreNbDelegate->DeleteLocal(eKey1);
1127     EXPECT_TRUE(status == INVALID_ARGS);
1128     /**
1129      * @tc.steps: step2. delete eKey2 from local db, where key = eKey2, which the size of eKey2 is 1K.
1130      * @tc.expected: step2. delete succeed and return OK.
1131      */
1132     status = g_kvStoreNbDelegate->DeleteLocal(eKey2);
1133     EXPECT_TRUE(status == OK);
1134     /**
1135      * @tc.steps: step3. delete eKey3 from local db, where key = eKey3, which the size of eKey3 is 1k + 1.
1136      * @tc.expected: step3. delete failed and return INVALID_ARGS.
1137      */
1138     status = g_kvStoreNbDelegate->DeleteLocal(eKey3);
1139     EXPECT_TRUE(status == INVALID_ARGS);
1140     /**
1141      * @tc.steps: step4. delete eKey4 from local db, where key = eKey4, which eKey2
1142      *    contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1143      * @tc.expected: step4. delete succeed and return OK.
1144      */
1145     status = g_kvStoreNbDelegate->DeleteLocal(eKey4);
1146     EXPECT_TRUE(status == OK);
1147     /**
1148      * @tc.steps: step5. delete eKey1 from sync db, where key = eKey1, which eKey1 is null.
1149      * @tc.expected: step5. delete failed and return INVALID_ARGS.
1150      */
1151     status = g_kvStoreNbDelegate->Delete(eKey1);
1152     EXPECT_TRUE(status == INVALID_ARGS);
1153     /**
1154      * @tc.steps: step6. delete eKey2 from sync db, where key = eKey2, which the size of eKey2 is 1K.
1155      * @tc.expected: step6. delete succeed and return OK.
1156      */
1157     status = g_kvStoreNbDelegate->Delete(eKey2);
1158     EXPECT_TRUE(status == OK);
1159     /**
1160      * @tc.steps: step7. delete eKey3 from sync db, where key = eKey3, which the size of eKey3 is 1k + 1.
1161      * @tc.expected: step7. delete failed and return INVALID_ARGS.
1162      */
1163     status = g_kvStoreNbDelegate->Delete(eKey3);
1164     EXPECT_TRUE(status == INVALID_ARGS);
1165     /**
1166      * @tc.steps: step8. delete eKey4 from sync db, where key = eKey4, which eKey2
1167      *    contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1168      * @tc.expected: step8. delete succeed and return OK.
1169      */
1170     status = g_kvStoreNbDelegate->Delete(eKey4);
1171     EXPECT_TRUE(status == OK);
1172 }
1173 /*
1174  * @tc.name: Pressure 005
1175  * @tc.desc: verify that can operate a super huge key and a super huge value.
1176  * @tc.type: FUNC
1177  * @tc.require: SR000CCPOI
1178  * @tc.author: luqianfu
1179  */
1180 HWTEST_F(DistributeddbNbCrudTest, Pressure005, TestSize.Level2)
1181 {
1182     DistributedDB::Key longKey;
1183     DistributedDB::Value longValue1, longValue2;
1184     longKey.assign(ONE_K_LONG_STRING, (uint8_t)'a');
1185     longValue1.assign(FOUR_M_LONG_STRING, (uint8_t)'b');
1186     longValue2.assign(FOUR_M_LONG_STRING, (uint8_t)'c');
1187     /**
1188      * @tc.steps: step1. put a longkey and longvalue (longKey, longValue1) to local db.
1189      * @tc.expected: step1. put succeed and return OK.
1190      */
1191     DBStatus status = g_kvStoreNbDelegate->PutLocal(longKey, longValue1);
1192     EXPECT_TRUE(status == OK);
1193     /**
1194      * @tc.steps: step2. put a longkey and longvalue (longKey, longValue2) to sync db.
1195      * @tc.expected: step2. put succeed and return OK.
1196      */
1197     status = g_kvStoreNbDelegate->Put(longKey, longValue2);
1198     EXPECT_TRUE(status == OK);
1199     /**
1200      * @tc.steps: step3. get data from local db where key = longKey.
1201      * @tc.expected: step3. succeed get value is longValue1.
1202      */
1203     Value valueResult;
1204     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1205     EXPECT_EQ(status, OK);
1206     EXPECT_EQ(valueResult, longValue1);
1207     /**
1208      * @tc.steps: step4. get data from sync db where key = longKey.
1209      * @tc.expected: step4. succeed get value is longValue2.
1210      */
1211     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1212     EXPECT_EQ(status, OK);
1213     EXPECT_EQ(valueResult, longValue2);
1214     /**
1215      * @tc.steps: step5. put a longkey and longvalue (longKey, longValue2) to local db.
1216      * @tc.expected: step5. put succeed and return OK.
1217      */
1218     status = g_kvStoreNbDelegate->PutLocal(longKey, longValue2);
1219     EXPECT_TRUE(status == OK);
1220     /**
1221      * @tc.steps: step6. put a longkey and longvalue (longKey, longValue1) to sync db.
1222      * @tc.expected: step6. put succeed and return OK.
1223      */
1224     status = g_kvStoreNbDelegate->Put(longKey, longValue1);
1225     EXPECT_TRUE(status == OK);
1226     /**
1227      * @tc.steps: step7. get data from local db where key = longKey.
1228      * @tc.expected: step7. succeed get value is longValue2.
1229      */
1230     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1231     EXPECT_EQ(status, OK);
1232     EXPECT_EQ(valueResult, longValue2);
1233     /**
1234      * @tc.steps: step8. get data from sync db where key = longKey.
1235      * @tc.expected: step8. succeed get value is longValue1.
1236      */
1237     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1238     EXPECT_EQ(status, OK);
1239     EXPECT_EQ(valueResult, longValue1);
1240     /**
1241      * @tc.steps: step9. delete data from local db where key = longKey.
1242      * @tc.expected: step9. delete succeed and return ok.
1243      */
1244     status = g_kvStoreNbDelegate->DeleteLocal(longKey);
1245     EXPECT_TRUE(status == OK);
1246     /**
1247      * @tc.steps: step10. delete data from sync db where key = longKey.
1248      * @tc.expected: step10. delete succeed and return ok.
1249      */
1250     status = g_kvStoreNbDelegate->Delete(longKey);
1251     EXPECT_TRUE(status == OK);
1252     /**
1253      * @tc.steps: step11. get data from local db where key = longKey.
1254      * @tc.expected: step11. get failed and return NOT_FOUND.
1255      */
1256     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1257     EXPECT_TRUE(status == NOT_FOUND);
1258     /**
1259      * @tc.steps: step12. get data from sync db where key = longKey.
1260      * @tc.expected: step12. get failed and return NOT_FOUND.
1261      */
1262     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1263     EXPECT_TRUE(status == NOT_FOUND);
1264 }
1265 
PressureForBasicCrud(Key & longKey,Value & valueResult)1266 void PressureForBasicCrud(Key &longKey, Value &valueResult)
1267 {
1268     /**
1269      * @tc.steps: step1. put (longKey, VALUE_1) to local db.
1270      * @tc.expected: step1. put succeed and return OK.
1271      */
1272     DBStatus status = g_kvStoreNbDelegate->PutLocal(longKey, VALUE_1);
1273     EXPECT_TRUE(status == OK);
1274     /**
1275      * @tc.steps: step2. put (longKey, VALUE_2) to sync db.
1276      * @tc.expected: step2. put succeed and return OK.
1277      */
1278     status = g_kvStoreNbDelegate->Put(longKey, VALUE_2);
1279     EXPECT_TRUE(status == OK);
1280     /**
1281      * @tc.steps: step3. get data from local db where key = longKey.
1282      * @tc.expected: step3. get success and value = VALUE_1.
1283      */
1284     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1285     EXPECT_EQ(status, OK);
1286     EXPECT_EQ(valueResult, VALUE_1);
1287     /**
1288      * @tc.steps: step4. get data from sync db where key = longKey.
1289      * @tc.expected: step4. get success and value = VALUE_2.
1290      */
1291     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1292     EXPECT_EQ(status, OK);
1293     EXPECT_EQ(valueResult, VALUE_2);
1294     /**
1295      * @tc.steps: step5. delete data from local db where key = longKey.
1296      * @tc.expected: step5. delete succeed and return ok.
1297      */
1298     status = g_kvStoreNbDelegate->DeleteLocal(longKey);
1299     EXPECT_TRUE(status == OK);
1300     /**
1301      * @tc.steps: step6. delete data from sync db where key = longKey.
1302      * @tc.expected: step6. delete succeed and return ok.
1303      */
1304     status = g_kvStoreNbDelegate->Delete(longKey);
1305     EXPECT_TRUE(status == OK);
1306     /**
1307      * @tc.steps: step7. get data from local db where key = longKey.
1308      * @tc.expected: step7. get failed and return NOT_FOUND.
1309      */
1310     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1311     EXPECT_EQ(status, NOT_FOUND);
1312     /**
1313      * @tc.steps: step8. get data from sync db where key = longKey.
1314      * @tc.expected: step8. get failed and return NOT_FOUND.
1315      */
1316     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1317     EXPECT_EQ(status, NOT_FOUND);
1318     /**
1319      * @tc.steps: step9. put (longKey, VALUE_1) to local db.
1320      * @tc.expected: step9. put succeed and return OK.
1321      */
1322     status = g_kvStoreNbDelegate->PutLocal(longKey, VALUE_1);
1323     EXPECT_TRUE(status == OK);
1324     /**
1325      * @tc.steps: step10. put (longKey, VALUE_2) to sync db.
1326      * @tc.expected: step10. put succeed and return OK.
1327      */
1328     status = g_kvStoreNbDelegate->Put(longKey, VALUE_2);
1329     EXPECT_TRUE(status == OK);
1330 }
1331 
1332 /*
1333  * @tc.name: Pressure 006
1334  * @tc.desc: verify that can crud one key-value repeately.
1335  * @tc.type: FUNC
1336  * @tc.require: SR000CCPOI
1337  * @tc.author: luqianfu
1338  */
1339 HWTEST_F(DistributeddbNbCrudTest, Pressure006, TestSize.Level1)
1340 {
1341     DistributedDB::Key longKey;
1342     longKey.assign(ONE_K_LONG_STRING, (uint8_t)'a');
1343     Value valueResult;
1344     PressureForBasicCrud(longKey, valueResult);
1345     /**
1346      * @tc.steps: step1. get data from local db where key = longKey.
1347      * @tc.expected: step1. get success and value = VALUE_1.
1348      */
1349     DBStatus status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1350     EXPECT_EQ(status, OK);
1351     EXPECT_EQ(valueResult, VALUE_1);
1352     /**
1353      * @tc.steps: step2. get data from sync db where key = longKey.
1354      * @tc.expected: step2. get success and value = VALUE_2.
1355      */
1356     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1357     EXPECT_EQ(status, OK);
1358     EXPECT_EQ(valueResult, VALUE_2);
1359     /**
1360      * @tc.steps: step3. put (longKey, VALUE_2) to local db.
1361      * @tc.expected: step3. put succeed and return OK.
1362      */
1363     status = g_kvStoreNbDelegate->PutLocal(longKey, VALUE_2);
1364     EXPECT_TRUE(status == OK);
1365     /**
1366      * @tc.steps: step4. put (longKey, VALUE_1) to sync db.
1367      * @tc.expected: step4. put succeed and return OK.
1368      */
1369     status = g_kvStoreNbDelegate->Put(longKey, VALUE_1);
1370     EXPECT_TRUE(status == OK);
1371     /**
1372      * @tc.steps: step5. get data from local db where key = longKey.
1373      * @tc.expected: step5. get success and value = VALUE_2.
1374      */
1375     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1376     EXPECT_EQ(status, OK);
1377     EXPECT_EQ(valueResult, VALUE_2);
1378     /**
1379      * @tc.steps: step6. get data from sync db where key = longKey.
1380      * @tc.expected: step6. get success and value = VALUE_1.
1381      */
1382     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1383     EXPECT_EQ(status, OK);
1384     EXPECT_EQ(valueResult, VALUE_1);
1385     /**
1386      * @tc.steps: step7. delete data from local db where key = longKey.
1387      * @tc.expected: step7. delete succeed and return ok.
1388      */
1389     status = g_kvStoreNbDelegate->DeleteLocal(longKey);
1390     EXPECT_TRUE(status == OK);
1391     /**
1392      * @tc.steps: step8. delete data from sync db where key = longKey.
1393      * @tc.expected: step8. delete succeed and return ok.
1394      */
1395     status = g_kvStoreNbDelegate->Delete(longKey);
1396     EXPECT_TRUE(status == OK);
1397     /**
1398      * @tc.steps: step9. get data from local db where key = longKey.
1399      * @tc.expected: step9. get failed and return NOT_FOUND.
1400      */
1401     status = g_kvStoreNbDelegate->GetLocal(longKey, valueResult);
1402     EXPECT_EQ(status, NOT_FOUND);
1403     /**
1404      * @tc.steps: step10. get data from sync db where key = longKey.
1405      * @tc.expected: step10. get failed and return NOT_FOUND.
1406      */
1407     status = g_kvStoreNbDelegate->Get(longKey, valueResult);
1408     EXPECT_EQ(status, NOT_FOUND);
1409 }
1410 
GenerateDataForBatch(vector<Key> & KeyCrud)1411 vector<Value> GenerateDataForBatch(vector<Key> &KeyCrud)
1412 {
1413     DistributedDB::Key key1 = {'a', 'b', 'c'};
1414     DistributedDB::Key key2 = {'a', 'b', 'c', 'd', 'e'};
1415     DistributedDB::Key key3 = {'a', 'b', 'c'};
1416     DistributedDB::Key key4 = {'a', 'b', 'c', 'd', 'a', 's', 'd'};
1417     DistributedDB::Key key5 = {'a', 'b', 'c', 'd', 's'};
1418     DistributedDB::Key key6 = {'b', 'c'};
1419     DistributedDB::Key key7 = {'d', 'a', 'b'};
1420     DistributedDB::Key key8 = {'d', 'a', 'b', 'c'};
1421     KeyCrud = {
1422         key1, key2, key3,
1423         key4, key5, key6,
1424         key7, key8
1425     };
1426     DistributedDB::Value value1 = {'l', 'o', 'c', 'a', 'l', 'V', '1'};
1427     DistributedDB::Value value2 = {'l', 'o', 'c', 'a', 'l', 'V', '2'};
1428     DistributedDB::Value value3 = {'s', 'y', 'n', 'V', '1'};
1429     DistributedDB::Value value4 = {'s', 'y', 'n', 'V', '2'};
1430     DistributedDB::Value value5 = {'s', 'y', 'n', 'V', '3'};
1431     DistributedDB::Value value6 = {'s', 'y', 'n', 'V', '4'};
1432     DistributedDB::Value value7 = {'s', 'y', 'n', 'V', '5'};
1433     DistributedDB::Value value8 = {'s', 'y', 'n', 'V', '6'};
1434     vector<Value> ValueCrud = {
1435         value1, value2, value3,
1436         value4, value5, value6,
1437         value7, value8
1438     };
1439     return ValueCrud;
1440 }
1441 
PutData(vector<Key> & KeyCrud,vector<Value> & ValueCrud)1442 void PutData(vector<Key> &KeyCrud, vector<Value> &ValueCrud)
1443 {
1444     ASSERT_TRUE(g_kvStoreNbDelegate != nullptr);
1445     DBStatus status;
1446     int index;
1447     for (index = 0; index < LOCAL_OPER_CNT; ++index) {
1448         status = g_kvStoreNbDelegate->PutLocal(KeyCrud[index], ValueCrud[index]);
1449         EXPECT_TRUE(status == OK);
1450     }
1451     for (index = LOCAL_OPER_CNT; index < LOCAL_OPER_CNT + NATIVE_OPER_CNT; ++index) {
1452         status = g_kvStoreNbDelegate->Put(KeyCrud[index], ValueCrud[index]);
1453         EXPECT_TRUE(status == OK);
1454     }
1455 }
1456 
1457 /*
1458  * @tc.name: Pressure 007
1459  * @tc.desc: verify that can get batch data use keyprefix from db.
1460  * @tc.type: FUNC
1461  * @tc.require: SR000CCPOI
1462  * @tc.author: luqianfu
1463  */
1464 HWTEST_F(DistributeddbNbCrudTest, Pressure007, TestSize.Level1)
1465 {
1466     vector<Key> KeyCrud;
1467     vector<Value> ValueCrud = GenerateDataForBatch(KeyCrud);
1468     PutData(KeyCrud, ValueCrud);
1469     DistributedDB::Key key9, key10;
1470     key9 = { 'a', 'b' };
1471     key10 = { 'a', 'b', 'c', 'd', 'e' };
1472     std::vector<Entry> entries;
1473     /**
1474      * @tc.steps: step1. get entries from db use keyprefix key9 = "ab".
1475      * @tc.expected: step1. get entries succeed and entries contains ({abcde}, {localV2}),
1476      *  "abc, syncV1"; "abcdasd, syncV2".
1477      */
1478     DBStatus status = g_kvStoreNbDelegate->GetEntries(key9, entries);
1479     EXPECT_TRUE(status == OK);
1480     int iCount = 3;
1481     DistributedDB::Key rKey[iCount];
1482     rKey[0] = KeyCrud[2];
1483     rKey[1] = KeyCrud[3];
1484     rKey[2] = KeyCrud[4];
1485     DistributedDB::Value rValue[iCount];
1486     rValue[0] = ValueCrud[2];
1487     rValue[1] = ValueCrud[3];
1488     rValue[2] = ValueCrud[4];
1489 
1490     for (unsigned long index = 0; index < entries.size(); index++) {
1491         EXPECT_EQ(entries[index].key, rKey[index]);
1492         EXPECT_EQ(entries[index].value, rValue[index]);
1493     }
1494     /**
1495      * @tc.steps: step2. get entries from db use keyprefix key10 = "abcde".
1496      * @tc.expected: step2. get entries failed and return NOT_FOUND
1497      */
1498     status = g_kvStoreNbDelegate->GetEntries(key10, entries);
1499     EXPECT_TRUE(status == NOT_FOUND);
1500     g_kvStoreNbDelegate->DeleteLocal(KeyCrud[0]);
1501     g_kvStoreNbDelegate->DeleteLocal(KeyCrud[1]);
1502     g_kvStoreNbDelegate->Delete(KeyCrud[2]);
1503     g_kvStoreNbDelegate->Delete(KeyCrud[3]);
1504     g_kvStoreNbDelegate->Delete(KeyCrud[4]);
1505     g_kvStoreNbDelegate->Delete(KeyCrud[5]);
1506     g_kvStoreNbDelegate->Delete(KeyCrud[6]);
1507     g_kvStoreNbDelegate->Delete(KeyCrud[7]);
1508 }
1509 
1510 /*
1511  * @tc.name: Pressure 008
1512  * @tc.desc: verify that key effectiveness of getEntries interface.
1513  * @tc.type: FUNC
1514  * @tc.require: SR000CCPOI
1515  * @tc.author: luqianfu
1516  */
1517 HWTEST_F(DistributeddbNbCrudTest, Pressure008, TestSize.Level1)
1518 {
1519     DistributedDB::Key longKey1, longKey2, longKey3;
1520     longKey1.assign(ONE_K_LONG_STRING, (uint8_t)'a');
1521     longKey2.assign(ONE_K_LONG_STRING + 1, (uint8_t)'b');
1522     longKey3 = { 'a', 'b', 'c', 'D', 'E', 'F', '2', '4', '5', 199, 255, 0 };
1523     Value value1, value3;
1524     DBStatus status = g_kvStoreNbDelegate->PutLocal(longKey1, value1);
1525     EXPECT_TRUE(status == OK);
1526     status = g_kvStoreNbDelegate->Put(longKey3, value3);
1527     EXPECT_TRUE(status == OK);
1528     DistributedDB::Key key9, key10;
1529     key9 = { 'a', 'b' };
1530     key10 = { 'a', 'b', 'c', 'd', 'e' };
1531     std::vector<Entry> entries;
1532     /**
1533      * @tc.steps: step1. get entries from db use keyPrefix = longKey1 size of which is 1K.
1534      * @tc.expected: step1. get entries failed and return NOT_FOUND
1535      */
1536     status = g_kvStoreNbDelegate->GetEntries(longKey1, entries);
1537     EXPECT_TRUE(status == NOT_FOUND);
1538     /**
1539      * @tc.steps: step2. get entries from db use keyPrefix = longKey2 size of which is 1k + 1.
1540      * @tc.expected: step2. get entries failed and return INVALID_ARGS
1541      */
1542     status = g_kvStoreNbDelegate->GetEntries(longKey2, entries);
1543     EXPECT_TRUE(status == INVALID_ARGS);
1544     /**
1545      * @tc.steps: step3. get entries from db use keyPrefix = longKey3
1546      *     which contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1547      * @tc.expected: step3. get entries success and return the entries has longKey3 keyPrefix.
1548      */
1549     status = g_kvStoreNbDelegate->GetEntries(longKey3, entries);
1550     EXPECT_TRUE(status == OK);
1551     EXPECT_TRUE(entries.size() == 1);
1552     g_kvStoreNbDelegate->DeleteLocal(longKey1);
1553     g_kvStoreNbDelegate->Delete(longKey3);
1554 }
1555 
1556 /*
1557  * @tc.name: Pressure 011
1558  * @tc.desc: verify can't crud after db is deleted.
1559  * @tc.type: Fault injection
1560  * @tc.require: SR000CCPOI
1561  * @tc.author: luqianfu
1562  */
1563 HWTEST_F(DistributeddbNbCrudTest, Pressure011, TestSize.Level3)
1564 {
1565     std::this_thread::sleep_for(std::chrono::seconds(UNIQUE_SECOND * 10));
1566     /**
1567      * @tc.steps: step1. put (KEY_1, VALUE_1) to local db.
1568      * @tc.expected: step1. put failed and return ERROR.
1569      */
1570     Value valueResult;
1571     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
1572     EXPECT_TRUE(status == OK);
1573     /**
1574      * @tc.steps: step2. put (KEY_1, VALUE_1) to sync db.
1575      * @tc.expected: step2. put failed and return ERROR.
1576      */
1577     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_2);
1578     EXPECT_TRUE(status == OK);
1579     /**
1580      * @tc.steps: step3. delete data from local db where key = KEY_1.
1581      * @tc.expected: step3. delete failed and return ERROR.
1582      */
1583     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
1584     EXPECT_TRUE(status == OK);
1585     /**
1586      * @tc.steps: step4. delete data from sync db where key = KEY_2.
1587      * @tc.expected: step4. delete failed and return ERROR.
1588      */
1589     status = g_kvStoreNbDelegate->Delete(KEY_2);
1590     EXPECT_TRUE(status == OK);
1591     /**
1592      * @tc.steps: step5. update (KEY_1, VALUE_2) to local db.
1593      * @tc.expected: step5. update failed and return ERROR.
1594      */
1595     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
1596     EXPECT_TRUE(status == OK);
1597     /**
1598      * @tc.steps: step6. update (KEY_2, VALUE_1) to sync db.
1599      * @tc.expected: step6. update failed and return ERROR.
1600      */
1601     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
1602     EXPECT_TRUE(status == OK);
1603     /**
1604      * @tc.steps: step7. get data from local db where key = KEY_1.
1605      * @tc.expected: step7. get failed and return ERROR.
1606      */
1607     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
1608     EXPECT_EQ(status, OK);
1609     /**
1610      * @tc.steps: step8. get data from sync db where key = KEY_2.
1611      * @tc.expected: step8. get failed and return ERROR.
1612      */
1613     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
1614     EXPECT_EQ(status, OK);
1615     DistributedDB::Key key9 = { 'a', 'b' };
1616     /**
1617      * @tc.steps: step9. get batch data from db with keyPrefix = 'ab'.
1618      * @tc.expected: step9. get failed and return ERROR.
1619      */
1620     std::vector<Entry> entries;
1621     status = g_kvStoreNbDelegate->GetEntries(key9, entries);
1622     EXPECT_EQ(status, NOT_FOUND);
1623     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
1624     EXPECT_TRUE(status == OK);
1625     status = g_kvStoreNbDelegate->Delete(KEY_2);
1626     EXPECT_TRUE(status == OK);
1627 }
1628 
StartThreadForLongReadRead()1629 void StartThreadForLongReadRead()
1630 {
1631     std::random_device randDevReadKeyNo;
1632     std::mt19937 genRandReadKeyNo(randDevReadKeyNo());
1633     std::uniform_int_distribution<unsigned long> disRandReadKeyNo(READ_RECORDS_NUM_START, READ_RECORDS_NUM_END);
1634 
1635     unsigned long randKeyNo;
1636     unsigned int thrCurId = 0;
1637 
1638     SysTime start;
1639     SysDurTime dur;
1640     double operInterval = 0.0;
1641     start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1642 
1643     /**
1644      * @tc.steps: step1. start 6 thread read the db randly at the same time for a long time.
1645      * @tc.expected: step1. can read and read rightly and has no mistakes.
1646      */
1647     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1648         auto threadParam = new (std::nothrow) ConcurParam;
1649         ASSERT_NE(threadParam, nullptr);
1650         threadParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1651         ASSERT_NE(threadParam->entryPtr_, nullptr);
1652         threadParam->threadId_ = thrCurId++;
1653         threadParam->tag_ = READ;
1654         randKeyNo = disRandReadKeyNo(genRandReadKeyNo);
1655         Entry entryCurrent;
1656         GenerateRecord(randKeyNo, entryCurrent);
1657         operInterval = NbCalculateTime(threadParam, entryCurrent, start, dur);
1658     }
1659 }
1660 
1661 /*
1662  * @tc.name: Concurrency 002
1663  * @tc.desc: verify can read and read Concurrency for a long time and has no mistakes.
1664  * @tc.type: LongTime Test
1665  * @tc.require: SR000CCPOI
1666  * @tc.author: luqianfu
1667  */
1668 HWTEST_F(DistributeddbNbCrudTest, Concurrency002, TestSize.Level3)
1669 {
1670     KvStoreNbDelegate *kvStoreNbDelegate = nullptr;
1671     KvStoreDelegateManager *manager = nullptr;
1672     kvStoreNbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1673     ASSERT_TRUE(manager != nullptr && kvStoreNbDelegate != nullptr);
1674 
1675     vector<Entry> entriesBatch;
1676     vector<Key> allKeys;
1677     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entriesBatch);
1678 
1679     DBStatus status;
1680     for (unsigned int index = 0; index < RECORDS_NUM_THOUSAND; index++)
1681     {
1682         status = kvStoreNbDelegate->Put(entriesBatch[index].key, entriesBatch[index].value);
1683         EXPECT_EQ(status, OK);
1684     }
1685 
1686     vector<Entry> valueResult;
1687     kvStoreNbDelegate->GetEntries(KEY_EMPTY, valueResult);
1688     EXPECT_EQ(valueResult.size(), RECORDS_NUM_THOUSAND);
1689 
1690     StartThreadForLongReadRead();
1691 
1692     while (entriesBatch.size() > 0) {
1693         status = DistributedDBNbTestTools::Delete(*kvStoreNbDelegate, entriesBatch[0].key);
1694         EXPECT_EQ(status, OK);
1695         entriesBatch.erase(entriesBatch.begin());
1696     }
1697     MST_LOG("entriesBatch.size() = %zu", entriesBatch.size());
1698     EXPECT_TRUE(EndCaseDeleteDB(manager, kvStoreNbDelegate, STORE_ID_2, g_option.isMemoryDb));
1699 }
1700 
StartThreadForLongReadWrite(vector<Entry> & entriesBatch)1701 void StartThreadForLongReadWrite(vector<Entry> &entriesBatch)
1702 {
1703     std::vector<std::thread> threads;
1704     std::random_device randDevReadKeyNo;
1705     std::random_device randDevWriteKeyNo;
1706     std::random_device randDevTag;
1707     std::mt19937 genRandReadKeyNo(randDevReadKeyNo());
1708     std::mt19937 genRandWriteKeyNo(randDevWriteKeyNo());
1709     std::mt19937 genRandTag(randDevTag());
1710     std::uniform_int_distribution<unsigned long> disRandReadKeyNo(READ_RECORDS_NUM_START, READ_RECORDS_NUM_END);
1711     std::uniform_int_distribution<unsigned long> disRandWriteKeyNo(WRITE_RECORDS_NUM_START, WRITE_RECORDS_NUM_END);
1712     std::uniform_int_distribution<unsigned long> disRandTag(READ, WRITE);
1713     unsigned long randKeyNo;
1714     unsigned int threadCurId = 0;
1715     int randTag;
1716     SysTime start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1717     /**
1718      * @tc.steps: step1. start 6 thread read and write the db randly at the same time for a long time.
1719      * @tc.expected: step1. can read and write rightly and has no mistakes.
1720      */
1721     SysDurTime dur;
1722     double operInterval = 0.0;
1723     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1724         auto threadParam = new (std::nothrow) ConcurParam;
1725         ASSERT_NE(threadParam, nullptr);
1726         threadParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1727         ASSERT_NE(threadParam->entryPtr_, nullptr);
1728         threadParam->threadId_ = threadCurId++;
1729         randTag = disRandTag(genRandTag);
1730         threadParam->tag_ = static_cast<ReadOrWriteTag>(randTag);
1731         if (randTag == READ) {
1732             randKeyNo = disRandReadKeyNo(genRandReadKeyNo);
1733         } else {
1734             randKeyNo = disRandWriteKeyNo(genRandWriteKeyNo);
1735         }
1736         Entry entryCurrent;
1737         GenerateRecord(randKeyNo, entryCurrent);
1738         if ((randTag == WRITE) && (randKeyNo > READ_RECORDS_NUM_END)) {
1739             entriesBatch.push_back(entryCurrent);
1740         }
1741         operInterval = NbCalculateTime(threadParam, entryCurrent, start, dur);
1742     }
1743 }
1744 
1745 /*
1746  * @tc.name: Concurrency 004
1747  * @tc.desc: verify can read and write Concurrency for a long time.
1748  * @tc.type: LONGTIME TEST
1749  * @tc.require: SR000CCPOI
1750  * @tc.author: luqianfu
1751  */
1752 HWTEST_F(DistributeddbNbCrudTest, Concurrency004, TestSize.Level3)
1753 {
1754     KvStoreNbDelegate *kvStoreNbDelegate = nullptr;
1755     KvStoreDelegateManager *manager = nullptr;
1756     kvStoreNbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1757     ASSERT_TRUE(manager != nullptr && kvStoreNbDelegate != nullptr);
1758 
1759     vector<Entry> entriesBatch;
1760     vector<Key> allKeys;
1761     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entriesBatch);
1762 
1763     DBStatus status = DistributedDBNbTestTools::PutBatch(*kvStoreNbDelegate, entriesBatch);
1764     EXPECT_EQ(status, DBStatus::OK);
1765     vector<Entry> valueResult;
1766     status = DistributedDBNbTestTools::GetEntries(*kvStoreNbDelegate, KEY_SEARCH_4, valueResult);
1767     EXPECT_EQ(status, DBStatus::OK);
1768     MST_LOG("value size %zu", valueResult.size());
1769 
1770     StartThreadForLongReadWrite(entriesBatch);
1771 
1772     for (unsigned int index = 0; index < RECORDS_NUM_THOUSAND; index++) {
1773         status = DistributedDBNbTestTools::Delete(*kvStoreNbDelegate, entriesBatch[index].key);
1774         EXPECT_EQ(status, OK);
1775     }
1776     MST_LOG("entriesBatch.size() = %zu", entriesBatch.size());
1777     EXPECT_TRUE(EndCaseDeleteDB(manager, kvStoreNbDelegate, STORE_ID_2, g_option.isMemoryDb));
1778 }
1779 
JudgeInLongWriteWrite(vector<Entry> & entriesBatch,ConcurParam * threadParam,Entry & entryCurrent)1780 void JudgeInLongWriteWrite(vector<Entry> &entriesBatch, ConcurParam *threadParam, Entry &entryCurrent)
1781 {
1782     bool isExist = false;
1783     if (threadParam->tag_ == WRITE) {
1784         for (auto &entry : entriesBatch) {
1785             if (CompareVector(entry.key, entryCurrent.key)) {
1786                 isExist = true;
1787             }
1788         }
1789         if (!isExist) {
1790             entriesBatch.push_back(entryCurrent);
1791         }
1792     } else {
1793         for (unsigned long index = 0; index < entriesBatch.size(); ++index) {
1794             if (CompareVector(entriesBatch[index].key, entryCurrent.key)) {
1795                 entriesBatch.erase(entriesBatch.begin() + index);
1796             }
1797         }
1798     }
1799 }
1800 
StartThreadForLongWriteWrite(vector<Entry> & entriesBatch)1801 void StartThreadForLongWriteWrite(vector<Entry> &entriesBatch)
1802 {
1803     std::vector<std::thread> threads;
1804     std::random_device randDevWriteKeyNo;
1805     std::random_device randDevDeleteKeyNo;
1806     std::random_device randDevTag;
1807     std::mt19937 genRandWriteKeyNo(randDevWriteKeyNo());
1808     std::mt19937 genRandDeleteKeyNo(randDevDeleteKeyNo());
1809     std::mt19937 genRandTag(randDevTag());
1810     std::uniform_int_distribution<unsigned long> disRandWriteKeyNo(WRITE_RECORDS_NUM_START, WRITE_RECORDS_NUM_END);
1811     std::uniform_int_distribution<unsigned long> disRandDeleteKeyNo(DELETE_RECORDS_NUM_START, DELETE_RECORDS_NUM_END);
1812     std::uniform_int_distribution<unsigned long> disRandTag(WRITE, DELETE);
1813     unsigned long randKeyNo = 0;
1814     unsigned long randRandTag = 0;
1815     unsigned int threadCurId = 0;
1816 
1817     SysTime start;
1818     SysDurTime dur;
1819     double operInterval = 0.0;
1820 
1821     /**
1822      * @tc.steps: step1. start 6 thread write the db randly at the same time for a long time.
1823      * @tc.expected: step1. can write rightly and has no mistake.
1824      */
1825     start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1826     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1827         auto thParam = new (std::nothrow) ConcurParam;
1828         ASSERT_NE(thParam, nullptr);
1829         thParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1830         ASSERT_NE(thParam->entryPtr_, nullptr);
1831         thParam->threadId_ = threadCurId++;
1832         randRandTag = disRandTag(genRandTag);
1833         thParam->tag_ = static_cast<ReadOrWriteTag>(randRandTag);
1834         if (randRandTag == WRITE) {
1835             randKeyNo = disRandWriteKeyNo(genRandWriteKeyNo);
1836         } else {
1837             randKeyNo = disRandDeleteKeyNo(genRandDeleteKeyNo);
1838         }
1839         Entry entryCurrent;
1840         GenerateRecord(randKeyNo, entryCurrent);
1841         JudgeInLongWriteWrite(entriesBatch, thParam, entryCurrent);
1842         operInterval = NbCalculateTime(thParam, entryCurrent, start, dur);
1843     }
1844 }
1845 
1846 /*
1847  * @tc.name: Concurrency 006
1848  * @tc.desc: verify can write and write Concurrency for a long time.
1849  * @tc.type: LONGTIME TEST
1850  * @tc.require: SR000CCPOI
1851  * @tc.author: luqianfu
1852  */
1853 #ifdef DB_RUNNIG
1854 HWTEST_F(DistributeddbNbCrudTest, Concurrency006, TestSize.Level3)
1855 {
1856     KvStoreNbDelegate *delegate = nullptr;
1857     KvStoreDelegateManager *manager = nullptr;
1858     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1859     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1860 
1861     vector<Entry> entryBatches;
1862     vector<Key> allKeys;
1863     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entryBatches);
1864 
1865     DBStatus status = DistributedDBNbTestTools::PutBatch(*delegate, entryBatches);
1866     EXPECT_EQ(status, DBStatus::OK);
1867     vector<Entry> valueResult;
1868     status = DistributedDBNbTestTools::GetEntries(*delegate, KEY_SEARCH_4, valueResult);
1869     EXPECT_EQ(status, DBStatus::OK);
1870     MST_LOG("value size %zu", valueResult.size());
1871 
1872     StartThreadForLongWriteWrite(entryBatches);
1873 
1874     for (unsigned int index = 0; index < entryBatches.size(); index++) {
1875         status = DistributedDBNbTestTools::Delete(*delegate, entryBatches[index].key);
1876         EXPECT_TRUE(status == OK || status == NOT_FOUND);
1877     }
1878     MST_LOG("entryBatches.size() = %zu", entryBatches.size());
1879     EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, g_option.isMemoryDb));
1880 }
1881 #endif
1882 }
1883