• 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, key2, key3, key4, key5, key6, key7, key8;
1414     key1 = {'a', 'b', 'c'};
1415     key2 = {'a', 'b', 'c', 'd', 'e'};
1416     key3 = {'a', 'b', 'c'};
1417     key4 = {'a', 'b', 'c', 'd', 'a', 's', 'd'};
1418     key5 = {'a', 'b', 'c', 'd', 's'};
1419     key6 = {'b', 'c'};
1420     key7 = {'d', 'a', 'b'};
1421     key8 = {'d', 'a', 'b', 'c'};
1422     KeyCrud = {
1423         key1, key2, key3,
1424         key4, key5, key6,
1425         key7, key8
1426     };
1427     DistributedDB::Value value1, value2, value3, value4, value5, value6, value7, value8;
1428     value1 = {'l', 'o', 'c', 'a', 'l', 'V', '1'};
1429     value2 = {'l', 'o', 'c', 'a', 'l', 'V', '2'};
1430     value3 = {'s', 'y', 'n', 'V', '1'};
1431     value4 = {'s', 'y', 'n', 'V', '2'};
1432     value5 = {'s', 'y', 'n', 'V', '3'};
1433     value6 = {'s', 'y', 'n', 'V', '4'};
1434     value7 = {'s', 'y', 'n', 'V', '5'};
1435     value8 = {'s', 'y', 'n', 'V', '6'};
1436     vector<Value> ValueCrud = {
1437         value1, value2, value3,
1438         value4, value5, value6,
1439         value7, value8
1440     };
1441     return ValueCrud;
1442 }
1443 
PutData(vector<Key> & KeyCrud,vector<Value> & ValueCrud)1444 void PutData(vector<Key> &KeyCrud, vector<Value> &ValueCrud)
1445 {
1446     ASSERT_TRUE(g_kvStoreNbDelegate != nullptr);
1447     DBStatus status;
1448     int index;
1449     for (index = 0; index < LOCAL_OPER_CNT; ++index) {
1450         status = g_kvStoreNbDelegate->PutLocal(KeyCrud[index], ValueCrud[index]);
1451         EXPECT_TRUE(status == OK);
1452     }
1453     for (index = LOCAL_OPER_CNT; index < LOCAL_OPER_CNT + NATIVE_OPER_CNT; ++index) {
1454         status = g_kvStoreNbDelegate->Put(KeyCrud[index], ValueCrud[index]);
1455         EXPECT_TRUE(status == OK);
1456     }
1457 }
1458 
1459 /*
1460  * @tc.name: Pressure 007
1461  * @tc.desc: verify that can get batch data use keyprefix from db.
1462  * @tc.type: FUNC
1463  * @tc.require: SR000CCPOI
1464  * @tc.author: luqianfu
1465  */
1466 HWTEST_F(DistributeddbNbCrudTest, Pressure007, TestSize.Level1)
1467 {
1468     vector<Key> KeyCrud;
1469     vector<Value> ValueCrud = GenerateDataForBatch(KeyCrud);
1470     PutData(KeyCrud, ValueCrud);
1471     DistributedDB::Key key9, key10;
1472     key9 = { 'a', 'b' };
1473     key10 = { 'a', 'b', 'c', 'd', 'e' };
1474     std::vector<Entry> entries;
1475     /**
1476      * @tc.steps: step1. get entries from db use keyprefix key9 = "ab".
1477      * @tc.expected: step1. get entries succeed and entries contains ({abcde}, {localV2}),
1478      *  "abc, syncV1"; "abcdasd, syncV2".
1479      */
1480     DBStatus status = g_kvStoreNbDelegate->GetEntries(key9, entries);
1481     EXPECT_TRUE(status == OK);
1482     int iCount = 3;
1483     DistributedDB::Key rKey[iCount];
1484     rKey[0] = KeyCrud[2];
1485     rKey[1] = KeyCrud[3];
1486     rKey[2] = KeyCrud[4];
1487     DistributedDB::Value rValue[iCount];
1488     rValue[0] = ValueCrud[2];
1489     rValue[1] = ValueCrud[3];
1490     rValue[2] = ValueCrud[4];
1491 
1492     for (unsigned long index = 0; index < entries.size(); index++) {
1493         EXPECT_EQ(entries[index].key, rKey[index]);
1494         EXPECT_EQ(entries[index].value, rValue[index]);
1495     }
1496     /**
1497      * @tc.steps: step2. get entries from db use keyprefix key10 = "abcde".
1498      * @tc.expected: step2. get entries failed and return NOT_FOUND
1499      */
1500     status = g_kvStoreNbDelegate->GetEntries(key10, entries);
1501     EXPECT_TRUE(status == NOT_FOUND);
1502     g_kvStoreNbDelegate->DeleteLocal(KeyCrud[0]);
1503     g_kvStoreNbDelegate->DeleteLocal(KeyCrud[1]);
1504     g_kvStoreNbDelegate->Delete(KeyCrud[2]);
1505     g_kvStoreNbDelegate->Delete(KeyCrud[3]);
1506     g_kvStoreNbDelegate->Delete(KeyCrud[4]);
1507     g_kvStoreNbDelegate->Delete(KeyCrud[5]);
1508     g_kvStoreNbDelegate->Delete(KeyCrud[6]);
1509     g_kvStoreNbDelegate->Delete(KeyCrud[7]);
1510 }
1511 
1512 /*
1513  * @tc.name: Pressure 008
1514  * @tc.desc: verify that key effectiveness of getEntries interface.
1515  * @tc.type: FUNC
1516  * @tc.require: SR000CCPOI
1517  * @tc.author: luqianfu
1518  */
1519 HWTEST_F(DistributeddbNbCrudTest, Pressure008, TestSize.Level1)
1520 {
1521     DistributedDB::Key longKey1, longKey2, longKey3;
1522     longKey1.assign(ONE_K_LONG_STRING, (uint8_t)'a');
1523     longKey2.assign(ONE_K_LONG_STRING + 1, (uint8_t)'b');
1524     longKey3 = { 'a', 'b', 'c', 'D', 'E', 'F', '2', '4', '5', 199, 255, 0 };
1525     Value value1, value3;
1526     DBStatus status = g_kvStoreNbDelegate->PutLocal(longKey1, value1);
1527     EXPECT_TRUE(status == OK);
1528     status = g_kvStoreNbDelegate->Put(longKey3, value3);
1529     EXPECT_TRUE(status == OK);
1530     DistributedDB::Key key9, key10;
1531     key9 = { 'a', 'b' };
1532     key10 = { 'a', 'b', 'c', 'd', 'e' };
1533     std::vector<Entry> entries;
1534     /**
1535      * @tc.steps: step1. get entries from db use keyPrefix = longKey1 size of which is 1K.
1536      * @tc.expected: step1. get entries failed and return NOT_FOUND
1537      */
1538     status = g_kvStoreNbDelegate->GetEntries(longKey1, entries);
1539     EXPECT_TRUE(status == NOT_FOUND);
1540     /**
1541      * @tc.steps: step2. get entries from db use keyPrefix = longKey2 size of which is 1k + 1.
1542      * @tc.expected: step2. get entries failed and return INVALID_ARGS
1543      */
1544     status = g_kvStoreNbDelegate->GetEntries(longKey2, entries);
1545     EXPECT_TRUE(status == INVALID_ARGS);
1546     /**
1547      * @tc.steps: step3. get entries from db use keyPrefix = longKey3
1548      *     which contains [a-zA-Z0-9], [\0-\255], chinese, latins.
1549      * @tc.expected: step3. get entries success and return the entries has longKey3 keyPrefix.
1550      */
1551     status = g_kvStoreNbDelegate->GetEntries(longKey3, entries);
1552     EXPECT_TRUE(status == OK);
1553     EXPECT_TRUE(entries.size() == 1);
1554     g_kvStoreNbDelegate->DeleteLocal(longKey1);
1555     g_kvStoreNbDelegate->Delete(longKey3);
1556 }
1557 
1558 /*
1559  * @tc.name: Pressure 011
1560  * @tc.desc: verify can't crud after db is deleted.
1561  * @tc.type: Fault injection
1562  * @tc.require: SR000CCPOI
1563  * @tc.author: luqianfu
1564  */
1565 HWTEST_F(DistributeddbNbCrudTest, Pressure011, TestSize.Level3)
1566 {
1567     std::this_thread::sleep_for(std::chrono::seconds(UNIQUE_SECOND * 10));
1568     /**
1569      * @tc.steps: step1. put (KEY_1, VALUE_1) to local db.
1570      * @tc.expected: step1. put failed and return ERROR.
1571      */
1572     Value valueResult;
1573     DBStatus status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_1);
1574     EXPECT_TRUE(status == OK);
1575     /**
1576      * @tc.steps: step2. put (KEY_1, VALUE_1) to sync db.
1577      * @tc.expected: step2. put failed and return ERROR.
1578      */
1579     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_2);
1580     EXPECT_TRUE(status == OK);
1581     /**
1582      * @tc.steps: step3. delete data from local db where key = KEY_1.
1583      * @tc.expected: step3. delete failed and return ERROR.
1584      */
1585     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
1586     EXPECT_TRUE(status == OK);
1587     /**
1588      * @tc.steps: step4. delete data from sync db where key = KEY_2.
1589      * @tc.expected: step4. delete failed and return ERROR.
1590      */
1591     status = g_kvStoreNbDelegate->Delete(KEY_2);
1592     EXPECT_TRUE(status == OK);
1593     /**
1594      * @tc.steps: step5. update (KEY_1, VALUE_2) to local db.
1595      * @tc.expected: step5. update failed and return ERROR.
1596      */
1597     status = g_kvStoreNbDelegate->PutLocal(KEY_1, VALUE_2);
1598     EXPECT_TRUE(status == OK);
1599     /**
1600      * @tc.steps: step6. update (KEY_2, VALUE_1) to sync db.
1601      * @tc.expected: step6. update failed and return ERROR.
1602      */
1603     status = g_kvStoreNbDelegate->Put(KEY_2, VALUE_1);
1604     EXPECT_TRUE(status == OK);
1605     /**
1606      * @tc.steps: step7. get data from local db where key = KEY_1.
1607      * @tc.expected: step7. get failed and return ERROR.
1608      */
1609     status = g_kvStoreNbDelegate->GetLocal(KEY_1, valueResult);
1610     EXPECT_EQ(status, OK);
1611     /**
1612      * @tc.steps: step8. get data from sync db where key = KEY_2.
1613      * @tc.expected: step8. get failed and return ERROR.
1614      */
1615     status = g_kvStoreNbDelegate->Get(KEY_2, valueResult);
1616     EXPECT_EQ(status, OK);
1617     DistributedDB::Key key9 = { 'a', 'b' };
1618     /**
1619      * @tc.steps: step9. get batch data from db with keyPrefix = 'ab'.
1620      * @tc.expected: step9. get failed and return ERROR.
1621      */
1622     std::vector<Entry> entries;
1623     status = g_kvStoreNbDelegate->GetEntries(key9, entries);
1624     EXPECT_EQ(status, NOT_FOUND);
1625     status = g_kvStoreNbDelegate->DeleteLocal(KEY_1);
1626     EXPECT_TRUE(status == OK);
1627     status = g_kvStoreNbDelegate->Delete(KEY_2);
1628     EXPECT_TRUE(status == OK);
1629 }
1630 
StartThreadForLongReadRead()1631 void StartThreadForLongReadRead()
1632 {
1633     std::random_device randDevReadKeyNo;
1634     std::mt19937 genRandReadKeyNo(randDevReadKeyNo());
1635     std::uniform_int_distribution<unsigned long> disRandReadKeyNo(READ_RECORDS_NUM_START, READ_RECORDS_NUM_END);
1636 
1637     unsigned long randKeyNo;
1638     unsigned int thrCurId = 0;
1639 
1640     SysTime start;
1641     SysDurTime dur;
1642     double operInterval = 0.0;
1643     start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1644 
1645     /**
1646      * @tc.steps: step1. start 6 thread read the db randly at the same time for a long time.
1647      * @tc.expected: step1. can read and read rightly and has no mistakes.
1648      */
1649     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1650         auto threadParam = new (std::nothrow) ConcurParam;
1651         ASSERT_NE(threadParam, nullptr);
1652         threadParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1653         ASSERT_NE(threadParam->entryPtr_, nullptr);
1654         threadParam->threadId_ = thrCurId++;
1655         threadParam->tag_ = READ;
1656         randKeyNo = disRandReadKeyNo(genRandReadKeyNo);
1657         Entry entryCurrent;
1658         GenerateRecord(randKeyNo, entryCurrent);
1659         operInterval = NbCalculateTime(threadParam, entryCurrent, start, dur);
1660     }
1661 }
1662 
1663 /*
1664  * @tc.name: Concurrency 002
1665  * @tc.desc: verify can read and read Concurrency for a long time and has no mistakes.
1666  * @tc.type: LongTime Test
1667  * @tc.require: SR000CCPOI
1668  * @tc.author: luqianfu
1669  */
1670 HWTEST_F(DistributeddbNbCrudTest, Concurrency002, TestSize.Level3)
1671 {
1672     KvStoreNbDelegate *kvStoreNbDelegate = nullptr;
1673     KvStoreDelegateManager *manager = nullptr;
1674     kvStoreNbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1675     ASSERT_TRUE(manager != nullptr && kvStoreNbDelegate != nullptr);
1676 
1677     vector<Entry> entriesBatch;
1678     vector<Key> allKeys;
1679     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entriesBatch);
1680 
1681     DBStatus status;
1682     for (unsigned int index = 0; index < RECORDS_NUM_THOUSAND; index++)
1683     {
1684         status = kvStoreNbDelegate->Put(entriesBatch[index].key, entriesBatch[index].value);
1685         EXPECT_EQ(status, OK);
1686     }
1687 
1688     vector<Entry> valueResult;
1689     kvStoreNbDelegate->GetEntries(KEY_EMPTY, valueResult);
1690     EXPECT_EQ(valueResult.size(), RECORDS_NUM_THOUSAND);
1691 
1692     StartThreadForLongReadRead();
1693 
1694     while (entriesBatch.size() > 0) {
1695         status = DistributedDBNbTestTools::Delete(*kvStoreNbDelegate, entriesBatch[0].key);
1696         EXPECT_EQ(status, OK);
1697         entriesBatch.erase(entriesBatch.begin());
1698     }
1699     MST_LOG("entriesBatch.size() = %zu", entriesBatch.size());
1700     EXPECT_TRUE(EndCaseDeleteDB(manager, kvStoreNbDelegate, STORE_ID_2, g_option.isMemoryDb));
1701 }
1702 
StartThreadForLongReadWrite(vector<Entry> & entriesBatch)1703 void StartThreadForLongReadWrite(vector<Entry> &entriesBatch)
1704 {
1705     std::vector<std::thread> threads;
1706     std::random_device randDevReadKeyNo, randDevWriteKeyNo, randDevTag;
1707     std::mt19937 genRandReadKeyNo(randDevReadKeyNo()), genRandWriteKeyNo(randDevWriteKeyNo()),
1708         genRandTag(randDevTag());
1709     std::uniform_int_distribution<unsigned long> disRandReadKeyNo(READ_RECORDS_NUM_START, READ_RECORDS_NUM_END);
1710     std::uniform_int_distribution<unsigned long> disRandWriteKeyNo(WRITE_RECORDS_NUM_START, WRITE_RECORDS_NUM_END);
1711     std::uniform_int_distribution<unsigned long> disRandTag(READ, WRITE);
1712     unsigned long randKeyNo;
1713     unsigned int threadCurId = 0;
1714     int randTag;
1715     SysTime start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1716     /**
1717      * @tc.steps: step1. start 6 thread read and write the db randly at the same time for a long time.
1718      * @tc.expected: step1. can read and write rightly and has no mistakes.
1719      */
1720     SysDurTime dur;
1721     double operInterval = 0.0;
1722     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1723         auto threadParam = new (std::nothrow) ConcurParam;
1724         ASSERT_NE(threadParam, nullptr);
1725         threadParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1726         ASSERT_NE(threadParam->entryPtr_, nullptr);
1727         threadParam->threadId_ = threadCurId++;
1728         randTag = disRandTag(genRandTag);
1729         threadParam->tag_ = static_cast<ReadOrWriteTag>(randTag);
1730         if (randTag == READ) {
1731             randKeyNo = disRandReadKeyNo(genRandReadKeyNo);
1732         } else {
1733             randKeyNo = disRandWriteKeyNo(genRandWriteKeyNo);
1734         }
1735         Entry entryCurrent;
1736         GenerateRecord(randKeyNo, entryCurrent);
1737         if ((randTag == WRITE) && (randKeyNo > READ_RECORDS_NUM_END)) {
1738             entriesBatch.push_back(entryCurrent);
1739         }
1740         operInterval = NbCalculateTime(threadParam, entryCurrent, start, dur);
1741     }
1742 }
1743 
1744 /*
1745  * @tc.name: Concurrency 004
1746  * @tc.desc: verify can read and write Concurrency for a long time.
1747  * @tc.type: LONGTIME TEST
1748  * @tc.require: SR000CCPOI
1749  * @tc.author: luqianfu
1750  */
1751 HWTEST_F(DistributeddbNbCrudTest, Concurrency004, TestSize.Level3)
1752 {
1753     KvStoreNbDelegate *kvStoreNbDelegate = nullptr;
1754     KvStoreDelegateManager *manager = nullptr;
1755     kvStoreNbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1756     ASSERT_TRUE(manager != nullptr && kvStoreNbDelegate != nullptr);
1757 
1758     vector<Entry> entriesBatch;
1759     vector<Key> allKeys;
1760     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entriesBatch);
1761 
1762     DBStatus status = DistributedDBNbTestTools::PutBatch(*kvStoreNbDelegate, entriesBatch);
1763     EXPECT_EQ(status, DBStatus::OK);
1764     vector<Entry> valueResult;
1765     status = DistributedDBNbTestTools::GetEntries(*kvStoreNbDelegate, KEY_SEARCH_4, valueResult);
1766     EXPECT_EQ(status, DBStatus::OK);
1767     MST_LOG("value size %zu", valueResult.size());
1768 
1769     StartThreadForLongReadWrite(entriesBatch);
1770 
1771     for (unsigned int index = 0; index < RECORDS_NUM_THOUSAND; index++) {
1772         status = DistributedDBNbTestTools::Delete(*kvStoreNbDelegate, entriesBatch[index].key);
1773         EXPECT_EQ(status, OK);
1774     }
1775     MST_LOG("entriesBatch.size() = %zu", entriesBatch.size());
1776     EXPECT_TRUE(EndCaseDeleteDB(manager, kvStoreNbDelegate, STORE_ID_2, g_option.isMemoryDb));
1777 }
1778 
JudgeInLongWriteWrite(vector<Entry> & entriesBatch,ConcurParam * threadParam,Entry & entryCurrent)1779 void JudgeInLongWriteWrite(vector<Entry> &entriesBatch, ConcurParam *threadParam, Entry &entryCurrent)
1780 {
1781     bool isExist = false;
1782     if (threadParam->tag_ == WRITE) {
1783         for (auto &entry : entriesBatch) {
1784             if (CompareVector(entry.key, entryCurrent.key)) {
1785                 isExist = true;
1786             }
1787         }
1788         if (!isExist) {
1789             entriesBatch.push_back(entryCurrent);
1790         }
1791     } else {
1792         for (unsigned long index = 0; index < entriesBatch.size(); ++index) {
1793             if (CompareVector(entriesBatch[index].key, entryCurrent.key)) {
1794                 entriesBatch.erase(entriesBatch.begin() + index);
1795             }
1796         }
1797     }
1798 }
1799 
StartThreadForLongWriteWrite(vector<Entry> & entriesBatch)1800 void StartThreadForLongWriteWrite(vector<Entry> &entriesBatch)
1801 {
1802     std::vector<std::thread> threads;
1803     std::random_device randDevWriteKeyNo, randDevDeleteKeyNo, randDevTag;
1804     std::mt19937 genRandWriteKeyNo(randDevWriteKeyNo()), genRandDeleteKeyNo(randDevDeleteKeyNo()),
1805         genRandTag(randDevTag());
1806     std::uniform_int_distribution<unsigned long> disRandWriteKeyNo(WRITE_RECORDS_NUM_START, WRITE_RECORDS_NUM_END);
1807     std::uniform_int_distribution<unsigned long> disRandDeleteKeyNo(DELETE_RECORDS_NUM_START, DELETE_RECORDS_NUM_END);
1808     std::uniform_int_distribution<unsigned long> disRandTag(WRITE, DELETE);
1809     unsigned long randKeyNo, randRandTag;
1810     unsigned int threadCurId = 0;
1811 
1812     SysTime start;
1813     SysDurTime dur;
1814     double operInterval = 0.0;
1815 
1816     /**
1817      * @tc.steps: step1. start 6 thread write the db randly at the same time for a long time.
1818      * @tc.expected: step1. can write rightly and has no mistake.
1819      */
1820     start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
1821     while (operInterval < static_cast<double>(LONG_TIME_TEST_SECONDS)) {
1822         auto thParam = new (std::nothrow) ConcurParam;
1823         ASSERT_NE(thParam, nullptr);
1824         thParam->entryPtr_ = new (std::nothrow) DistributedDB::Entry;
1825         ASSERT_NE(thParam->entryPtr_, nullptr);
1826         thParam->threadId_ = threadCurId++;
1827         randRandTag = disRandTag(genRandTag);
1828         thParam->tag_ = static_cast<ReadOrWriteTag>(randRandTag);
1829         if (randRandTag == WRITE) {
1830             randKeyNo = disRandWriteKeyNo(genRandWriteKeyNo);
1831         } else {
1832             randKeyNo = disRandDeleteKeyNo(genRandDeleteKeyNo);
1833         }
1834         Entry entryCurrent;
1835         GenerateRecord(randKeyNo, entryCurrent);
1836         JudgeInLongWriteWrite(entriesBatch, thParam, entryCurrent);
1837         operInterval = NbCalculateTime(thParam, entryCurrent, start, dur);
1838     }
1839 }
1840 
1841 /*
1842  * @tc.name: Concurrency 006
1843  * @tc.desc: verify can write and write Concurrency for a long time.
1844  * @tc.type: LONGTIME TEST
1845  * @tc.require: SR000CCPOI
1846  * @tc.author: luqianfu
1847  */
1848 #ifdef DB_RUNNIG
1849 HWTEST_F(DistributeddbNbCrudTest, Concurrency006, TestSize.Level3)
1850 {
1851     KvStoreNbDelegate *delegate = nullptr;
1852     KvStoreDelegateManager *manager = nullptr;
1853     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, g_option);
1854     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1855 
1856     vector<Entry> entryBatches;
1857     vector<Key> allKeys;
1858     GenerateRecords(RECORDS_NUM_THOUSAND, DEFAULT_START, allKeys, entryBatches);
1859 
1860     DBStatus status = DistributedDBNbTestTools::PutBatch(*delegate, entryBatches);
1861     EXPECT_EQ(status, DBStatus::OK);
1862     vector<Entry> valueResult;
1863     status = DistributedDBNbTestTools::GetEntries(*delegate, KEY_SEARCH_4, valueResult);
1864     EXPECT_EQ(status, DBStatus::OK);
1865     MST_LOG("value size %zu", valueResult.size());
1866 
1867     StartThreadForLongWriteWrite(entryBatches);
1868 
1869     for (unsigned int index = 0; index < entryBatches.size(); index++) {
1870         status = DistributedDBNbTestTools::Delete(*delegate, entryBatches[index].key);
1871         EXPECT_TRUE(status == OK || status == NOT_FOUND);
1872     }
1873     MST_LOG("entryBatches.size() = %zu", entryBatches.size());
1874     EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, g_option.isMemoryDb));
1875 }
1876 #endif
1877 }
1878