1 /*
2 * Copyright (c) 2023 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 #ifdef USE_RD_KERNEL
16 #include <gtest/gtest.h>
17 #include <thread>
18
19 #include "db_common.h"
20 #include "db_constant.h"
21 #include "db_errno.h"
22 #include "distributeddb_data_generate_unit_test.h"
23 #include "distributeddb_tools_unit_test.h"
24 #include "log_print.h"
25 #include "platform_specific.h"
26 #include "process_system_api_adapter_impl.h"
27 #include "runtime_context.h"
28 #include "system_time.h"
29 #include "virtual_communicator_aggregator.h"
30
31 using namespace testing::ext;
32 using namespace DistributedDB;
33 using namespace DistributedDBUnitTest;
34 using namespace std;
35
36 namespace {
37 // define some variables to init a KvStoreDelegateManager object.
38 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
39 string g_testDir;
40 KvStoreConfig g_config;
41 KvStoreNbDelegate::Option g_option;
42 const int RESULT_SET_COUNT = 9;
43 const int RESULT_SET_INIT_POS = -1;
44 Key g_keyPrefix = {'A', 'B', 'C'};
45 uint8_t g_testDict[RESULT_SET_COUNT] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
46
47 // define the g_kvNbDelegateCallback, used to get some information when open a kv store.
48 DBStatus g_kvDelegateStatus = INVALID_ARGS;
49 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
50 const int OBSERVER_SLEEP_TIME = 100;
51 const int BATCH_PRESET_SIZE_TEST = 10;
52 const int DIVIDE_BATCH_PRESET_SIZE = 5;
53 const int VALUE_OFFSET = 5;
54
55 const int DEFAULT_KEY_VALUE_SIZE = 10;
56 VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
57 // the type of g_kvNbDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
58 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
59 placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
60
InitResultSet()61 void InitResultSet()
62 {
63 Key testKey;
64 Value testValue;
65 for (int i = 0; i < RESULT_SET_COUNT; i++) {
66 testKey.clear();
67 testValue.clear();
68 // set key
69 testKey = g_keyPrefix;
70 testKey.push_back(g_testDict[i]);
71 // set value
72 testValue.push_back(g_testDict[i]);
73 // insert entry
74 EXPECT_EQ(g_kvNbDelegatePtr->Put(testKey, testValue), OK);
75 }
76 }
77
ReadResultSet(KvStoreResultSet * readResultSet)78 void ReadResultSet(KvStoreResultSet *readResultSet)
79 {
80 if (readResultSet == nullptr) {
81 return;
82 }
83 // index from 0 to 8(first to last)
84 for (int i = 0; i < RESULT_SET_COUNT; i++) {
85 Entry entry;
86 std::vector<uint8_t> cursorKey = g_keyPrefix;
87 cursorKey.push_back(g_testDict[i]);
88 std::vector<uint8_t> cursorValue;
89 cursorValue.push_back(g_testDict[i]);
90 EXPECT_TRUE(readResultSet->MoveToNext());
91 EXPECT_EQ(readResultSet->GetEntry(entry), OK);
92 EXPECT_EQ(entry.key, cursorKey);
93 EXPECT_EQ(entry.value, cursorValue);
94 EXPECT_TRUE(!readResultSet->IsBeforeFirst());
95 EXPECT_TRUE(!readResultSet->IsAfterLast());
96 }
97 // change index to 8(last)
98 EXPECT_EQ(readResultSet->GetPosition(), RESULT_SET_COUNT - 1);
99 EXPECT_TRUE(!readResultSet->IsFirst());
100 EXPECT_TRUE(readResultSet->IsLast());
101 EXPECT_TRUE(!readResultSet->IsBeforeFirst());
102 EXPECT_TRUE(!readResultSet->IsAfterLast());
103 }
104
CheckResultSetValue(KvStoreResultSet * readResultSet,DBStatus errCode,int position)105 void CheckResultSetValue(KvStoreResultSet *readResultSet, DBStatus errCode, int position)
106 {
107 if (readResultSet == nullptr) {
108 return;
109 }
110 Entry entry;
111 EXPECT_EQ(readResultSet->GetPosition(), position);
112 EXPECT_EQ(readResultSet->GetEntry(entry), errCode);
113 if (errCode == OK) {
114 std::vector<uint8_t> cursorKey;
115 std::vector<uint8_t> cursorValue;
116 if (position > RESULT_SET_INIT_POS && position < RESULT_SET_COUNT) {
117 uint8_t keyPostfix = g_testDict[position];
118 // set key
119 cursorKey = g_keyPrefix;
120 cursorKey.push_back(keyPostfix);
121 // set value
122 cursorValue.push_back(keyPostfix);
123 }
124 // check key and value
125 EXPECT_EQ(entry.key, cursorKey);
126 EXPECT_EQ(entry.value, cursorValue);
127 }
128 }
129
130 class DistributedDBInterfacesNBDelegateRdTest : public testing::Test {
131 public:
132 static void SetUpTestCase(void);
133 static void TearDownTestCase(void);
134 void SetUp();
135 void TearDown();
136 };
137
SetUpTestCase(void)138 void DistributedDBInterfacesNBDelegateRdTest::SetUpTestCase(void)
139 {
140 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
141 g_config.dataDir = g_testDir;
142 g_option.storageEngineType = GAUSSDB_RD;
143 g_mgr.SetKvStoreConfig(g_config);
144 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
145 LOGE("rm test db files error!");
146 }
147
148 g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
149 ASSERT_TRUE(g_communicatorAggregator != nullptr);
150 RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
151
152 std::shared_ptr<ProcessSystemApiAdapterImpl> g_adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
153 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
154 }
155
TearDownTestCase(void)156 void DistributedDBInterfacesNBDelegateRdTest::TearDownTestCase(void)
157 {
158 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
159
160 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
161 }
162
SetUp(void)163 void DistributedDBInterfacesNBDelegateRdTest::SetUp(void)
164 {
165 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
166 LOGE("rm test db files error.");
167 }
168 DistributedDBToolsUnitTest::PrintTestCaseInfo();
169 g_kvDelegateStatus = INVALID_ARGS;
170 g_kvNbDelegatePtr = nullptr;
171 }
172
TearDown(void)173 void DistributedDBInterfacesNBDelegateRdTest::TearDown(void)
174 {
175 if (g_kvNbDelegatePtr != nullptr) {
176 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
177 g_kvNbDelegatePtr = nullptr;
178 }
179 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
180 }
181
182 /**
183 * @tc.name:
184 * @tc.desc:
185 * @tc.type: FUNC
186 * @tc.require:
187 * @tc.author:
188 */
189 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, CombineTest001, TestSize.Level1)
190 {
191 /**
192 * @tc.steps:step1. Get the nb delegate.
193 * @tc.expected: step1. Get results OK and non-null delegate.
194 */
195 g_mgr.GetKvStore("distributed_nb_delegate_test_rd", g_option, g_kvNbDelegateCallback);
196 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
197 EXPECT_TRUE(g_kvDelegateStatus == OK);
198 std::string keyStr("acd");
199 Key key(keyStr.begin(), keyStr.end());
200 std::string valueStr("acd");
201 Value value(valueStr.begin(), valueStr.end());
202 Value valueRead;
203 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
204 ASSERT_TRUE(observer != nullptr);
205 /**
206 * @tc.steps:step2. Register a non-null but unsupported observer for the special key.
207 * @tc.expected: step2. Register results DB_ERROR.
208 */
209 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), DB_ERROR);
210 /**
211 * @tc.steps:step3. Try to get the data before putting
212 * @tc.expected: step3. Put returns OK.
213 */
214 EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
215 /**
216 * @tc.steps:step3. Put the local data.
217 * @tc.expected: step3. Put returns OK.
218 *
219 */
220 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
221 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
222 /**
223 * @tc.steps:step4. Check the local data.
224 * @tc.expected: step4. The get data is equal to the put data.
225 */
226 EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), OK);
227 /**
228 * @tc.steps:step5. Delete the local data.
229 * @tc.expected: step5. Delete returns OK.
230 */
231 EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), OK);
232 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
233 /**
234 * @tc.steps:step6. Check the local data.
235 * @tc.expected: step6. Couldn't find the deleted data.
236 */
237 LOGD("step6 =========== Get local data again");
238 EXPECT_EQ(g_kvNbDelegatePtr->Get(key, valueRead), NOT_FOUND);
239 /**
240 * @tc.steps:step7. UnRegister the observer.
241 * @tc.expected: step7. Returns NOT_FOUND.
242 */
243 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), NOT_FOUND);
244 delete observer;
245 observer = nullptr;
246 /**
247 * @tc.steps:step8. Close the kv store.
248 * @tc.expected: step8. Results OK and delete successfully.
249 */
250 LOGD("step8 =========== Close store");
251 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
252 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_test_rd"), OK);
253 g_kvNbDelegatePtr = nullptr;
254 }
255
256 /**
257 * @tc.name: SingleVerGetLocalEntries001
258 * @tc.desc: Test GetEntries interface for the single ver database.
259 * @tc.type: FUNC
260 * @tc.require: AR000DPTTA
261 * @tc.author: wangbingquan
262 */
263 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerGetLocalEntries001, TestSize.Level1)
264 {
265 /**
266 * @tc.steps:step1. Get the nb delegate.
267 * @tc.expected: step1. Get results OK and non-null delegate.
268 */
269 g_mgr.GetKvStore("concurrentPutTest", g_option, g_kvNbDelegateCallback);
270 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
271 EXPECT_TRUE(g_kvDelegateStatus == OK);
272
273 /**
274 * @tc.steps:step2. Put one data whose key has prefix 'p' into the local zone.
275 */
276 Entry entry1 = {{'p'}, {'q'}};
277 EXPECT_EQ(g_kvNbDelegatePtr->Put(entry1.key, entry1.value), OK);
278
279 /**
280 * @tc.steps:step3. Get batch data whose key has prefix 'k' from the local zone.
281 * @tc.expected: step3. Get results NOT_FOUND.
282 */
283 std::vector<Entry> entries;
284 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries({'k'}, entries), NOT_FOUND);
285
286 /**
287 * @tc.steps:step4. Put two data whose key have prefix 'k' into the local zone.
288 */
289 Entry entry2 = {{'k', '1'}, {'d'}};
290 Entry entry3 = {{'k', '2'}, {'d'}};
291 EXPECT_EQ(g_kvNbDelegatePtr->Put(entry2.key, entry2.value), OK);
292 EXPECT_EQ(g_kvNbDelegatePtr->Put(entry3.key, entry3.value), OK);
293
294 /**
295 * @tc.steps:step5. Get batch data whose key has prefix 'k' from the local zone.
296 * @tc.expected: step5. Get results OK, and the entries size is 2.
297 */
298 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries({'k'}, entries), OK);
299 EXPECT_EQ(entries.size(), 2UL);
300
301 /**
302 * @tc.steps:step6. Get batch data whose key has empty prefix from the local zone.
303 * @tc.expected: step6. Get results OK, and the entries size is 3.
304 */
305 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries((Key){}, entries), OK);
306 EXPECT_EQ(entries.size(), 3UL);
307
308 /**
309 * @tc.steps:step7. Delete one data whose key has prefix 'k' from the local zone.
310 */
311 EXPECT_EQ(g_kvNbDelegatePtr->Delete(entry3.key), OK);
312
313 /**
314 * @tc.steps:step8. Get batch data whose key has prefix 'k' from the local zone.
315 * @tc.expected: step8. Get results OK, and the entries size is 1.
316 */
317 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries({'k'}, entries), OK);
318 EXPECT_EQ(entries.size(), 1UL);
319
320 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
321 EXPECT_EQ(g_mgr.DeleteKvStore("concurrentPutTest"), OK);
322 g_kvNbDelegatePtr = nullptr;
323 }
324
325 /**
326 * @tc.name: ResultSetTest001
327 * @tc.desc: Test the NbDelegate for result set function.
328 * @tc.type: FUNC
329 * @tc.require: AR000D08KT
330 * @tc.author: wumin
331 */
332 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, ResultSetTest001, TestSize.Level1)
333 {
334 /**
335 * @tc.steps: step1. initialize result set.
336 * @tc.expected: step1. Success.
337 */
338 g_mgr.GetKvStore("distributed_nb_delegate_result_set_test", g_option, g_kvNbDelegateCallback);
339 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
340 EXPECT_TRUE(g_kvDelegateStatus == OK);
341 InitResultSet();
342
343 /**
344 * @tc.steps: step2. get entries using result set.
345 * @tc.expected: step2. Success.
346 */
347 KvStoreResultSet *readResultSet = nullptr;
348 Key keyPrefix = { g_keyPrefix };
349 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(keyPrefix, readResultSet), OK);
350 ASSERT_TRUE(readResultSet != nullptr);
351 EXPECT_EQ(readResultSet->GetCount(), RESULT_SET_COUNT);
352
353 /**
354 * @tc.steps: step3. result function check.
355 * @tc.expected: step3. Success.
356 */
357 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
358 // index from 0 to 8(first to last)
359 ReadResultSet(readResultSet);
360 // change index to 9(after last)
361 EXPECT_TRUE(!readResultSet->MoveToNext());
362 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
363 // change index to 8(last)
364 EXPECT_TRUE(readResultSet->MoveToPrevious());
365 CheckResultSetValue(readResultSet, OK, RESULT_SET_COUNT - 1);
366 // change index to 0(first)
367 EXPECT_TRUE(readResultSet->MoveToFirst());
368 CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 1);
369 // change index to 8(last)
370 EXPECT_TRUE(readResultSet->MoveToLast());
371 CheckResultSetValue(readResultSet, OK, RESULT_SET_COUNT - 1);
372 // move to -4: change index to -1
373 EXPECT_TRUE(!readResultSet->MoveToPosition(RESULT_SET_INIT_POS - 3));
374 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
375 // move to 10: change index to 9
376 EXPECT_TRUE(!readResultSet->MoveToPosition(RESULT_SET_COUNT + 1));
377 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
378 // change index to 2
379 EXPECT_TRUE(readResultSet->MoveToPosition(RESULT_SET_INIT_POS + 3));
380 CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 3);
381 // move 0: change index to 2
382 EXPECT_TRUE(readResultSet->Move(0));
383 CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 3);
384 // change index to 6
385 EXPECT_TRUE(readResultSet->Move(RESULT_SET_INIT_POS + 5));
386 CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 7);
387 // change index to 3
388 EXPECT_TRUE(readResultSet->Move(RESULT_SET_INIT_POS - 2));
389 CheckResultSetValue(readResultSet, OK, RESULT_SET_INIT_POS + 4);
390 // move -5: change index to -1
391 EXPECT_TRUE(!readResultSet->Move(-5));
392 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
393
394 // move INT_MIN: change index to -1
395 EXPECT_TRUE(!readResultSet->Move(INT_MIN));
396 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_INIT_POS);
397
398 EXPECT_TRUE(readResultSet->Move(5));
399 EXPECT_TRUE(!readResultSet->Move(INT_MAX));
400 CheckResultSetValue(readResultSet, NOT_FOUND, RESULT_SET_COUNT);
401
402 /**
403 * @tc.steps: step4. clear the result set resource.
404 * @tc.expected: step4. Success.
405 */
406 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(readResultSet), OK);
407 EXPECT_TRUE(readResultSet == nullptr);
408
409 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
410 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_nb_delegate_result_set_test"), OK);
411 g_kvNbDelegatePtr = nullptr;
412 }
413
414 /**
415 * @tc.name: PutBatchVerify001
416 * @tc.desc: This test case use to verify the putBatch interface function
417 * @tc.type: FUNC
418 * @tc.require: AR000CCPOM
419 * @tc.author: wumin
420 */
421 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, PutBatchVerify001, TestSize.Level1)
422 {
423 /**
424 * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
425 * @tc.expected: step1. Get database success.
426 */
427 g_mgr.GetKvStore("distributed_PutBatchVerify_001", g_option, g_kvNbDelegateCallback);
428 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
429 EXPECT_TRUE(g_kvDelegateStatus == OK);
430
431 /**
432 * @tc.steps: step2. Insert 10 records into database.
433 * @tc.expected: step2. Insert successfully.
434 */
435 vector<Entry> entries;
436 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
437 Entry entry;
438 entry.key.push_back(i);
439 entry.value.push_back(i);
440 entries.push_back(entry);
441 }
442
443 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
444
445 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
446 Key key;
447 key.push_back(i);
448 Value value;
449 g_kvNbDelegatePtr->Get(key, value);
450 EXPECT_EQ(key, value);
451 }
452
453 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
454 g_kvNbDelegatePtr = nullptr;
455 }
456
457 /**
458 * @tc.name: SingleVerPutBatch001
459 * @tc.desc: Check for illegal parameters
460 * @tc.type: FUNC
461 * @tc.require: AR000DPTQ8
462 * @tc.author: sunpeng
463 */
464 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatch001, TestSize.Level1)
465 {
466 /**
467 * @tc.steps: step1.
468 * Create and construct three sets of vector <Entry>, each set of three data contains records:
469 * (K1, V1) It is illegal for K1 to be greater than 1K, and V1 is 1K in size
470 * (K2, V2) K2 is legal, V2 is greater than 4M
471 * (K3, V3) are not legal.
472 */
473 Key illegalKey;
474 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
475 Value illegalValue;
476 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalValue, DBConstant::MAX_VALUE_SIZE + 1); // 4M + 1
477 vector<Entry> entrysKeyIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, VALUE_3}};
478 vector<Entry> entrysValueIllegal = {KV_ENTRY_1, KV_ENTRY_2, {KEY_3, illegalValue}};
479 vector<Entry> entrysIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, illegalValue}};
480
481 g_mgr.GetKvStore("distributed_SingleVerPutBatch_001", g_option, g_kvNbDelegateCallback);
482 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
483 EXPECT_TRUE(g_kvDelegateStatus == OK);
484 /**
485 * @tc.steps: step2. PutBatch operates on three sets of data.
486 * @tc.expected: step2. All three operations return INVALID_ARGS.
487 */
488 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysKeyIllegal), INVALID_ARGS);
489 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysValueIllegal), INVALID_ARGS);
490 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysIllegal), INVALID_ARGS);
491
492 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
493 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_001"), OK);
494 g_kvNbDelegatePtr = nullptr;
495 }
496
497 /**
498 * @tc.name: SingleVerPutBatch002
499 * @tc.desc: PutBatch normal insert function test.
500 * @tc.type: FUNC
501 * @tc.require: AR000DPTQ8
502 * @tc.author: sunpeng
503 */
504 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatch002, TestSize.Level1)
505 {
506 g_mgr.GetKvStore("distributed_SingleVerPutBatch_002", g_option, g_kvNbDelegateCallback);
507 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
508 EXPECT_TRUE(g_kvDelegateStatus == OK);
509 /**
510 * @tc.steps: step1.
511 * Create and build 4 groups of vector <Entry>, which are:
512 * Vect of empty objects;
513 * Vect1 of a legal Entry record;
514 * 128 legal Entry records Vect2;
515 * 129 legal Entry records Vect3;
516 */
517 vector<Entry> entrysMaxNumber;
518 for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
519 Entry entry;
520 entry.key.push_back(i);
521 entry.value.push_back(i);
522 entrysMaxNumber.push_back(entry);
523 }
524 Key keyTemp = {'1', '1'};
525 Value valueTemp;
526 Entry entryTemp = {keyTemp, VALUE_1};
527 vector<Entry> entrysOneRecord = {entryTemp};
528 vector<Entry> entrysOverSize = entrysMaxNumber;
529 entrysOverSize.push_back(entryTemp);
530
531 /**
532 * @tc.steps: step2. PutBatch operates on four sets of data. and use get check the result of Vect3.
533 * @tc.expected: step2. Returns INVALID_ARGS for 129 records, and returns OK for the rest. all get return NOT_FOUND.
534 */
535 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysOverSize), INVALID_ARGS);
536 for (size_t i = 0; i < entrysOverSize.size(); i++) {
537 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysOverSize[i].key, valueTemp), NOT_FOUND);
538 }
539
540 /**
541 * @tc.steps: step3. Use get check the result of Vect2.
542 * @tc.expected: step3. Return OK and get the correct value.
543 */
544 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysOneRecord), OK);
545 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueTemp), OK);
546 EXPECT_EQ(valueTemp, VALUE_1);
547 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMaxNumber), OK);
548 /**
549 * @tc.steps: step4. Use get check the result of Vect3.
550 * @tc.expected: step4. Return OK and get the correct value.
551 */
552 for (size_t i = 0; i < entrysMaxNumber.size(); i++) {
553 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[i].key, valueTemp), OK);
554 EXPECT_EQ(valueTemp, entrysMaxNumber[i].value);
555 }
556
557 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
558 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_002"), OK);
559 g_kvNbDelegatePtr = nullptr;
560 }
561
562 /**
563 * @tc.name: SingleVerPutBatch003
564 * @tc.desc: Check interface atomicity
565 * @tc.type: FUNC
566 * @tc.require: AR000DPTQ8
567 * @tc.author: sunpeng
568 */
569 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatch003, TestSize.Level1)
570 {
571 g_mgr.GetKvStore("distributed_SingleVerPutBatch_003", g_option, g_kvNbDelegateCallback);
572 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
573 EXPECT_TRUE(g_kvDelegateStatus == OK);
574 /**
575 * @tc.steps: step1. Create and construct a set of vector <Entry> with a total of 128 data,
576 * including one illegal data. And call PutBatch interface to insert.
577 */
578 vector<Entry> entrysMaxNumber;
579 for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
580 Entry entry;
581 entry.key.push_back(i);
582 entry.value.push_back(i);
583 entrysMaxNumber.push_back(entry);
584 }
585 Key illegalKey;
586 Value valueTemp;
587 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
588 entrysMaxNumber[0].key = illegalKey;
589
590 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysMaxNumber), INVALID_ARGS);
591 /**
592 * @tc.steps: step2. Use Get interface to query 128 corresponding key values.
593 * @tc.expected: step2. All Get interface return NOT_FOUND.
594 */
595 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[0].key, valueTemp), INVALID_ARGS);
596 for (size_t i = 1; i < entrysMaxNumber.size(); i++) {
597 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysMaxNumber[i].key, valueTemp), NOT_FOUND);
598 }
599 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
600 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_003"), OK);
601 g_kvNbDelegatePtr = nullptr;
602 }
603
PreparePutBatch004(vector<Entry> & entrys1,vector<Entry> & entrys2,vector<Entry> & entrys3)604 static void PreparePutBatch004(vector<Entry> &entrys1, vector<Entry> &entrys2, vector<Entry> &entrys3)
605 {
606 g_mgr.GetKvStore("distributed_SingleVerPutBatch_004", g_option, g_kvNbDelegateCallback);
607 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
608 EXPECT_TRUE(g_kvDelegateStatus == OK);
609
610 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
611 Entry entry;
612 entry.key.push_back(i);
613 entry.value.push_back(i);
614 entrys1.push_back(entry);
615 }
616
617 for (int i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
618 Entry entry;
619 entry.key.push_back(i);
620 entry.value.push_back(i + VALUE_OFFSET);
621 entrys2.push_back(entry);
622 }
623
624 for (int i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
625 Entry entry;
626 entry.key.push_back(i);
627 entry.value.push_back(i - VALUE_OFFSET);
628 entrys3.push_back(entry);
629 }
630 }
631
632 /**
633 * @tc.name: SingleVerPutBatch004
634 * @tc.desc: Check interface data insertion and update functions.
635 * @tc.type: FUNC
636 * @tc.require: AR000DPTQ8
637 * @tc.author: sunpeng
638 */
639 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatch004, TestSize.Level1)
640 {
641 /**
642 * @tc.steps: step1.
643 * Construct three groups of three vector <Entry>:
644 * (1) entrys1: key1 ~ 10, corresponding to Value1 ~ 10;
645 * (2) entrys2: key1 ~ 5, corresponding to Value6 ~ 10;
646 * (3) entrys3: key6 ~ 10, corresponding to Value1 ~ 5;
647 */
648 vector<Entry> entrys1;
649 vector<Entry> entrys2;
650 vector<Entry> entrys3;
651 PreparePutBatch004(entrys1, entrys2, entrys3);
652 /**
653 * @tc.steps: step2. PutBatch entrys2.
654 * @tc.expected: step2. PutBatch return OK.
655 */
656 Value valueRead;
657 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys2), OK);
658 /**
659 * @tc.steps: step3. Check PutBatch result.
660 * @tc.expected: step3. Get correct value of key1~5. Key6~10 return NOT_FOUND.
661 */
662 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
663 Key keyTemp;
664 keyTemp.push_back(i);
665 if (i < DIVIDE_BATCH_PRESET_SIZE) {
666 Value valueTemp;
667 valueTemp.push_back(i + VALUE_OFFSET);
668 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
669 EXPECT_EQ(valueRead, valueTemp);
670 continue;
671 }
672 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), NOT_FOUND);
673 }
674 /**
675 * @tc.steps: step4. PutBatch entrys1.
676 * @tc.expected: step4. PutBatch return OK.
677 */
678 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys1), OK);
679 /**
680 * @tc.steps: step5. Check PutBatch result.
681 * @tc.expected: step5. Update and insert value of key1~10 to value1~10.
682 */
683 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
684 Key keyTemp;
685 keyTemp.push_back(i);
686 if (i < DIVIDE_BATCH_PRESET_SIZE) {
687 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
688 EXPECT_EQ(valueRead, keyTemp);
689 continue;
690 }
691 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
692 EXPECT_EQ(valueRead, keyTemp);
693 }
694 /**
695 * @tc.steps: step6. PutBatch entrys3.
696 * @tc.expected: step6. PutBatch return OK.
697 */
698 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
699 /**
700 * @tc.steps: step7. Check PutBatch result of key1~10.
701 * @tc.expected: step7. Update value of key5~10 to value1~5.
702 */
703 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
704 Key keyTemp;
705 keyTemp.push_back(i);
706 if (i < DIVIDE_BATCH_PRESET_SIZE) {
707 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
708 EXPECT_EQ(valueRead, keyTemp);
709 continue;
710 }
711 Value valueTemp;
712 valueTemp.push_back(i - VALUE_OFFSET);
713 EXPECT_EQ(g_kvNbDelegatePtr->Get(keyTemp, valueRead), OK);
714 EXPECT_EQ(valueRead, valueTemp);
715 }
716
717 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
718 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_004"), OK);
719 g_kvNbDelegatePtr = nullptr;
720 }
721
CreatEntrys(int recordSize,vector<Key> & keys,vector<Value> & values,vector<Entry> & entries)722 static void CreatEntrys(int recordSize, vector<Key> &keys, vector<Value> &values, vector<Entry> &entries)
723 {
724 keys.clear();
725 values.clear();
726 entries.clear();
727 for (int i = 0; i < recordSize; i++) {
728 string temp = to_string(i);
729 Entry entry;
730 Key keyTemp;
731 Value valueTemp;
732 for (auto &iter : temp) {
733 entry.key.push_back(iter);
734 entry.value.push_back(iter);
735 keyTemp.push_back(iter);
736 valueTemp.push_back(iter);
737 }
738 keys.push_back(keyTemp);
739 values.push_back(valueTemp);
740 entries.push_back(entry);
741 }
742 }
743
744 /**
745 * @tc.name: SingleVerDeleteBatch001
746 * @tc.desc: Check for illegal parameters.
747 * @tc.type: FUNC
748 * @tc.require: AR000DPTQ8
749 * @tc.author: sunpeng
750 */
751 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerDeleteBatch001, TestSize.Level1)
752 {
753 g_mgr.GetKvStore("distributed_SingleVerDeleteBatch_001", g_option, g_kvNbDelegateCallback);
754 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
755 EXPECT_TRUE(g_kvDelegateStatus == OK);
756 /**
757 * @tc.steps: step1. Create and construct a set of vector <Entry>, containing a total of 10 data keys1 ~ 10,
758 * Value1 ~ 10, and call Putbatch interface to insert data.
759 * @tc.expected: step1. PutBatch successfully.
760 */
761 vector<Entry> entries;
762 vector<Key> keys;
763 vector<Value> values;
764 Value valueRead;
765 CreatEntrys(BATCH_PRESET_SIZE_TEST, keys, values, entries);
766 vector<Entry> entrysBase = entries;
767 vector<Key> keysBase = keys;
768 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysBase), OK);
769 /**
770 * @tc.steps: step2. Use Get to check data in database.
771 * @tc.expected: step2. Get value1~10 by key1~10 successfully.
772 */
773 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
774 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
775 }
776 /**
777 * @tc.steps: step3. Use DeleteBatch interface to transfer 10 + 119 extra keys (total 129).
778 * @tc.expected: step3. Return INVALID_ARGS.
779 */
780 CreatEntrys(DBConstant::MAX_BATCH_SIZE + 1, keys, values, entries);
781 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), INVALID_ARGS);
782 /**
783 * @tc.steps: step4. Use Get to check data in database.
784 * @tc.expected: step4. Key1~10 still in database.
785 */
786 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
787 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
788 }
789 /**
790 * @tc.steps: step5. Use the DeleteBatch interface to pass in 10 included
791 * keys6 ~ 10 + 123 additional key values (128 in total).
792 * @tc.expected: step5. DeleteBatch OK.
793 */
794 CreatEntrys(DBConstant::MAX_BATCH_SIZE + DIVIDE_BATCH_PRESET_SIZE, keys, values, entries);
795 keys.erase(keys.begin(), keys.begin() + DIVIDE_BATCH_PRESET_SIZE);
796 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
797 /**
798 * @tc.steps: step6. Use Get to check key1~10 in database.
799 * @tc.expected: step6. Key1~5 in database, key6~10 have been deleted.
800 */
801 for (size_t i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
802 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
803 }
804 for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
805 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), NOT_FOUND);
806 }
807 /**
808 * @tc.steps: step7. Repeat Putbatch key1~10, value1~10.
809 * @tc.expected: step7. Return OK.
810 */
811 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysBase), OK);
812
813 Key illegalKey;
814 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
815 keysBase.push_back(illegalKey);
816 /**
817 * @tc.steps: step8. Use DeleteBatch interface to pass in 10 + 1(larger than 1K) keys.
818 * @tc.expected: step8. Return INVALID_ARGS.
819 */
820 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), INVALID_ARGS);
821 /**
822 * @tc.steps: step9. Use Get to check key1~10 in database.
823 * @tc.expected: step9. Delete those data failed.
824 */
825 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
826 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), OK);
827 }
828 /**
829 * @tc.steps: step10. Use DeleteBatch interface to pass in 10(in database) + 1 valid keys.
830 * @tc.expected: step10. Delete those data successfully.
831 */
832 keysBase.back().erase(keysBase.back().begin(), keysBase.back().begin() + 1);
833 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
834 /**
835 * @tc.steps: step11. Check data.
836 * @tc.expected: step11. DeleteBatch successfully.
837 */
838 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
839 EXPECT_EQ(g_kvNbDelegatePtr->Get(entrysBase[i].key, valueRead), NOT_FOUND);
840 }
841
842 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
843 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteBatch_001"), OK);
844 g_kvNbDelegatePtr = nullptr;
845 }
846
847 /**
848 * @tc.name: SingleVerDeleteBatch002
849 * @tc.desc: Check normal delete batch ability.
850 * @tc.type: FUNC
851 * @tc.require: AR000DPTQ8
852 * @tc.author: sunpeng
853 */
854 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerDeleteBatch002, TestSize.Level1)
855 {
856 g_mgr.GetKvStore("distributed_SingleVerPutBatch_002", g_option, g_kvNbDelegateCallback);
857 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
858 EXPECT_TRUE(g_kvDelegateStatus == OK);
859 /**
860 * @tc.steps: step1. Create a group of vector <Entry>, containing a total of 10 data keys1 ~ 10, Value1 ~ 10,
861 * call the Putbatch interface to insert data.
862 * @tc.expected: step1. Insert to database successfully.
863 */
864 vector<Entry> entries;
865 vector<Key> keysBase;
866 vector<Value> values;
867 CreatEntrys(BATCH_PRESET_SIZE_TEST, keysBase, values, entries);
868
869 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
870 /**
871 * @tc.steps: step2. Check data.
872 * @tc.expected: step2. Get key1~10 successfully.
873 */
874 Value valueRead;
875 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
876 EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), OK);
877 }
878 /**
879 * @tc.steps: step3. DeleteBatch key1~5.
880 * @tc.expected: step3. Return OK.
881 */
882 vector<Key> keys(keysBase.begin(), keysBase.begin() + DIVIDE_BATCH_PRESET_SIZE);
883 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
884 /**
885 * @tc.steps: step4. Check key1~10.
886 * @tc.expected: step4. Key1~5 deleted, key6~10 existed.
887 */
888 for (size_t i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
889 EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), NOT_FOUND);
890 }
891 for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
892 EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), OK);
893 }
894 /**
895 * @tc.steps: step5. DeleteBatch key1~10.
896 * @tc.expected: step5. Return OK.
897 */
898 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
899 /**
900 * @tc.steps: step6. Check key1~10.
901 * @tc.expected: step6. Key1~10 deleted successfully.
902 */
903 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
904 EXPECT_EQ(g_kvNbDelegatePtr->Get(keysBase[i], valueRead), NOT_FOUND);
905 }
906 /**
907 * @tc.steps: step7. DeleteBatch key1~10 once again.
908 * @tc.expected: step7. Return OK.
909 */
910 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keysBase), OK);
911
912 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
913 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatch_002"), OK);
914 g_kvNbDelegatePtr = nullptr;
915 }
916
917 /**
918 * @tc.name: SingleVerPutBatchObserver001
919 * @tc.desc: Test the observer function of PutBatch() interface.
920 * @tc.type: FUNC
921 * @tc.require: AR000DPTTA
922 * @tc.author: wumin
923 */
924 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatchObserver001, TestSize.Level1)
925 {
926 /**
927 * @tc.steps:step1. Get the nb delegate.
928 * @tc.expected: step1. Get results OK and non-null delegate.
929 */
930 g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_001", g_option, g_kvNbDelegateCallback);
931 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
932 EXPECT_TRUE(g_kvDelegateStatus == OK);
933
934 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
935 ASSERT_TRUE(observer != nullptr);
936 /**
937 * @tc.steps:step2. Register the non-null observer for the special key.
938 * @tc.expected: step2. Register results OK.
939 */
940 Key key;
941 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
942 // register same observer twice will return already_set
943 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), ALREADY_SET);
944 /**
945 * @tc.steps:step3. Put batch data.
946 * @tc.expected: step3. Returns OK.
947 */
948 vector<Entry> entrysBase;
949 vector<Key> keysBase;
950 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST + 1, entrysBase, keysBase);
951
952 vector<Entry> entries(entrysBase.begin(), entrysBase.end() - 1);
953 EXPECT_EQ(entries.size(), 10UL);
954 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
955 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
956 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
957 /**
958 * @tc.steps:step4. Delete the batch data.
959 * @tc.expected: step4. Returns OK.
960 */
961 vector<Key> keys(keysBase.begin() + 5, keysBase.end());
962 EXPECT_EQ(keys.size(), 6UL);
963 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
964 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
965 vector<Entry> entrysDel(entrysBase.begin() + 5, entrysBase.end() - 1);
966 EXPECT_EQ(entrysDel.size(), 5UL);
967 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysDel, observer->GetEntriesDeleted()));
968 /**
969 * @tc.steps:step5. UnRegister the observer.
970 * @tc.expected: step5. Returns OK.
971 */
972 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
973 delete observer;
974 observer = nullptr;
975 /**
976 * @tc.steps:step6. Close the kv store.
977 * @tc.expected: step6. Results OK and delete successfully.
978 */
979 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
980 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_001"), OK);
981 g_kvNbDelegatePtr = nullptr;
982 }
983
984 /**
985 * @tc.name: SingleVerPutBatchObserver002
986 * @tc.desc: Test the observer function of PutBatch() for invalid input.
987 * @tc.type: FUNC
988 * @tc.require: AR000DPTTA
989 * @tc.author: wumin
990 */
991 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatchObserver002, TestSize.Level4)
992 {
993 /**
994 * @tc.steps:step1. Get the nb delegate.
995 * @tc.expected: step1. Get results OK and non-null delegate.
996 */
997 g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_002", g_option, g_kvNbDelegateCallback);
998 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
999 EXPECT_TRUE(g_kvDelegateStatus == OK);
1000
1001 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1002 ASSERT_TRUE(observer != nullptr);
1003 /**
1004 * @tc.steps:step2. Register the non-null observer for the special key.
1005 * @tc.expected: step2. Register results OK.
1006 */
1007 Key key;
1008 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1009 /**
1010 * @tc.steps:step3. Put 129 batch data.
1011 * @tc.expected: step3. Returns INVALID_ARGS.
1012 */
1013 vector<Entry> entrys1;
1014 vector<Key> keys1;
1015 DistributedDBUnitTest::GenerateRecords(DBConstant::MAX_BATCH_SIZE + 1, entrys1, keys1);
1016
1017 EXPECT_EQ(entrys1.size(), 129UL);
1018 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys1), INVALID_ARGS);
1019 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1020 EXPECT_TRUE(observer->GetEntriesInserted().empty());
1021 /**
1022 * @tc.steps:step4. Put invalid batch data.
1023 * @tc.expected: step4. Returns INVALID_ARGS.
1024 */
1025 vector<Entry> entrys2;
1026 vector<Key> keys2;
1027 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2);
1028 EXPECT_EQ(entrys2.size(), 10UL);
1029
1030 vector<Entry> entrysInvalid;
1031 vector<Key> keysInvalid;
1032 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysInvalid, keysInvalid,
1033 DBConstant::MAX_KEY_SIZE + 10);
1034 EXPECT_EQ(entrysInvalid.size(), 10UL);
1035 entrys2[0].key = entrysInvalid[0].key;
1036
1037 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys2), INVALID_ARGS);
1038 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1039 EXPECT_TRUE(observer->GetEntriesInserted().empty());
1040 /**
1041 * @tc.steps:step5. Put MAX valid value batch data.
1042 * @tc.expected: step5. Returns OK.
1043 */
1044 vector<Entry> entrys3;
1045 vector<Key> keys3;
1046
1047 DistributedDBUnitTest::GenerateRecords(DBConstant::MAX_BATCH_SIZE, entrys3, keys3);
1048 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
1049 LOGD("sleep begin");
1050 // sleep 20 seconds
1051 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME * 10));
1052 LOGD("sleep end");
1053 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys3, observer->GetEntriesInserted()));
1054 /**
1055 * @tc.steps:step6. UnRegister the observer.
1056 * @tc.expected: step6. Returns OK.
1057 */
1058 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1059 delete observer;
1060 observer = nullptr;
1061 /**
1062 * @tc.steps:step7. Close the kv store.
1063 * @tc.expected: step7. Results OK and delete successfully.
1064 */
1065 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1066 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_002"), OK);
1067 g_kvNbDelegatePtr = nullptr;
1068 }
1069
1070 /**
1071 * @tc.name: SingleVerPutBatchObserver003
1072 * @tc.desc: Test the observer function of PutBatch() update function.
1073 * @tc.type: FUNC
1074 * @tc.require: AR000DPTTA
1075 * @tc.author: wumin
1076 */
1077 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatchObserver003, TestSize.Level1)
1078 {
1079 /**
1080 * @tc.steps:step1. Get the nb delegate.
1081 * @tc.expected: step1. Get results OK and non-null delegate.
1082 */
1083 g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_003", g_option, g_kvNbDelegateCallback);
1084 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1085 EXPECT_TRUE(g_kvDelegateStatus == OK);
1086
1087 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1088 ASSERT_TRUE(observer != nullptr);
1089 /**
1090 * @tc.steps:step2. Register the non-null observer for the special key.
1091 * @tc.expected: step2. Register results OK.
1092 */
1093 Key key;
1094 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1095 /**
1096 * @tc.steps:step3. Put batch data.
1097 * @tc.expected: step3. Returns OK.
1098 */
1099 vector<Entry> entrysAdd;
1100 vector<Key> keysAdd;
1101 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysAdd, keysAdd);
1102
1103 EXPECT_EQ(entrysAdd.size(), 10UL);
1104 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysAdd), OK);
1105 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1106 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysAdd, observer->GetEntriesInserted()));
1107 /**
1108 * @tc.steps:step4. Update the batch data.
1109 * @tc.expected: step4. Returns OK.
1110 */
1111 vector<Entry> entrysUpdate;
1112 vector<Key> keysUpdate;
1113 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysUpdate, keysUpdate, DEFAULT_KEY_VALUE_SIZE,
1114 DEFAULT_KEY_VALUE_SIZE + 10);
1115
1116 EXPECT_EQ(entrysUpdate.size(), 10UL);
1117 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrysUpdate), OK);
1118 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1119 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysUpdate, observer->GetEntriesUpdated()));
1120 /**
1121 * @tc.steps:step5. UnRegister the observer.
1122 * @tc.expected: step5. Returns OK.
1123 */
1124 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1125 delete observer;
1126 observer = nullptr;
1127 /**
1128 * @tc.steps:step6. Close the kv store.
1129 * @tc.expected: step6. Results OK and delete successfully.
1130 */
1131 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1132 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_003"), OK);
1133 g_kvNbDelegatePtr = nullptr;
1134 }
1135
1136 /**
1137 * @tc.name: SingleVerPutBatchObserver004
1138 * @tc.desc: Test the observer function of PutBatch(), same keys handle.
1139 * @tc.type: FUNC
1140 * @tc.require: AR000DPTTA
1141 * @tc.author: wumin
1142 */
1143 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerPutBatchObserver004, TestSize.Level1)
1144 {
1145 /**
1146 * @tc.steps:step1. Get the nb delegate.
1147 * @tc.expected: step1. Get results OK and non-null delegate.
1148 */
1149 g_mgr.GetKvStore("distributed_SingleVerPutBatchObserver_004", g_option, g_kvNbDelegateCallback);
1150 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1151 EXPECT_TRUE(g_kvDelegateStatus == OK);
1152
1153 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1154 ASSERT_TRUE(observer != nullptr);
1155 /**
1156 * @tc.steps:step2. Register the non-null observer for the special key.
1157 * @tc.expected: step2. Register results OK.
1158 */
1159 Key key;
1160 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1161 /**
1162 * @tc.steps:step3. Put batch data.
1163 * @tc.expected: step3. Returns OK.
1164 */
1165 vector<Entry> entrys1;
1166 vector<Key> keys1;
1167 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys1, keys1);
1168 vector<Entry> entrys2;
1169 vector<Key> keys2;
1170 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2, DEFAULT_KEY_VALUE_SIZE,
1171 DEFAULT_KEY_VALUE_SIZE + 10);
1172 entrys1.insert(entrys1.end(), entrys2.begin(), entrys2.end());
1173
1174 EXPECT_EQ(entrys1.size(), 20UL);
1175 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys1), OK);
1176 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1177 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys2, observer->GetEntriesInserted()));
1178 EXPECT_EQ(observer->GetEntriesUpdated().size(), 0UL);
1179
1180 vector<Entry> entrys3;
1181 vector<Key> keys3;
1182 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys3, keys3, DEFAULT_KEY_VALUE_SIZE,
1183 DEFAULT_KEY_VALUE_SIZE + 20);
1184 vector<Entry> entrys4;
1185 vector<Key> keys4;
1186 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys4, keys4, DEFAULT_KEY_VALUE_SIZE,
1187 DEFAULT_KEY_VALUE_SIZE + 30);
1188 entrys3.insert(entrys3.end(), entrys4.begin(), entrys4.end());
1189 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entrys3), OK);
1190 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1191 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys4, observer->GetEntriesUpdated()));
1192 EXPECT_EQ(observer->GetEntriesInserted().size(), 0UL);
1193
1194 /**
1195 * @tc.steps:step4. UnRegister the observer.
1196 * @tc.expected: step4. Returns OK.
1197 */
1198 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1199 delete observer;
1200 observer = nullptr;
1201 /**
1202 * @tc.steps:step5. Close the kv store.
1203 * @tc.expected: step5. Results OK and delete successfully.
1204 */
1205 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1206 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutBatchObserver_004"), OK);
1207 g_kvNbDelegatePtr = nullptr;
1208 }
1209
1210 /**
1211 * @tc.name: SingleVerDeleteBatchObserver001
1212 * @tc.desc: Test the observer function of DeleteBatch() interface.
1213 * @tc.type: FUNC
1214 * @tc.require: AR000DPTTA
1215 * @tc.author: wumin
1216 */
1217 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerDeleteBatchObserver001, TestSize.Level1)
1218 {
1219 /**
1220 * @tc.steps:step1. Get the nb delegate.
1221 * @tc.expected: step1. Get results OK and non-null delegate.
1222 */
1223 g_mgr.GetKvStore("distributed_SingleVerDeleteBatchObserver_001", g_option, g_kvNbDelegateCallback);
1224 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1225 EXPECT_TRUE(g_kvDelegateStatus == OK);
1226
1227 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
1228 ASSERT_TRUE(observer != nullptr);
1229 /**
1230 * @tc.steps:step2. Register the non-null observer for the special key.
1231 * @tc.expected: step2. Register results OK.
1232 */
1233 Key key;
1234 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_NATIVE, observer), OK);
1235 /**
1236 * @tc.steps:step3. Put batch data.
1237 * @tc.expected: step3. Returns OK.
1238 */
1239 vector<Entry> entries;
1240 vector<Key> keys;
1241 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entries, keys);
1242 EXPECT_EQ(entries.size(), 10UL);
1243
1244 EXPECT_EQ(g_kvNbDelegatePtr->PutBatch(entries), OK);
1245 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1246 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
1247 /**
1248 * @tc.steps:step4. Delete the batch data.
1249 * @tc.expected: step4. Returns OK.
1250 */
1251 EXPECT_EQ(g_kvNbDelegatePtr->DeleteBatch(keys), OK);
1252 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
1253 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesDeleted()));
1254 /**
1255 * @tc.steps:step5. UnRegister the observer.
1256 * @tc.expected: step5. Returns OK.
1257 */
1258 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
1259 delete observer;
1260 observer = nullptr;
1261 /**
1262 * @tc.steps:step6. Close the kv store.
1263 * @tc.expected: step6. Results OK and delete successfully.
1264 */
1265 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1266 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteBatchObserver_001"), OK);
1267 g_kvNbDelegatePtr = nullptr;
1268 }
1269
1270 /**
1271 * @tc.name: SingleVerGetSecurityOption001
1272 * @tc.desc: Test GetSecurityOption interface for the single ver database.
1273 * @tc.type: FUNC
1274 * @tc.require: AR000EV1G2
1275 * @tc.author: liuwenkai
1276 */
1277 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerGetSecurityOption001, TestSize.Level1)
1278 {
1279 SecurityOption savedOption;
1280 std::shared_ptr<IProcessSystemApiAdapter> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
1281 EXPECT_TRUE(adapter);
1282 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
1283 KvStoreNbDelegate::Option option;
1284 option.storageEngineType = GAUSSDB_RD;
1285
1286 /**
1287 * @tc.steps:step1. Create databases without securityOption.
1288 * @tc.expected: step2. Returns a non-null kvstore but can not get SecurityOption.
1289 */
1290 g_mgr.GetKvStore("SingleVerGetSecurityOption001", option, g_kvNbDelegateCallback);
1291 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1292 EXPECT_TRUE(g_kvDelegateStatus == OK);
1293 EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
1294 EXPECT_TRUE(savedOption.securityLabel == 0);
1295 EXPECT_TRUE(savedOption.securityFlag == 0);
1296 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
1297
1298 /**
1299 * @tc.steps:step2. Create databases with new securityOption(Check ignore the new option).
1300 * @tc.expected: step2. Returns non-null kvstore.
1301 */
1302 option.secOption.securityLabel = S3;
1303 option.secOption.securityFlag = 1;
1304 g_mgr.GetKvStore("SingleVerGetSecurityOption001", option, g_kvNbDelegateCallback);
1305 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1306 EXPECT_TRUE(g_kvDelegateStatus == OK);
1307 EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
1308 EXPECT_TRUE(savedOption.securityLabel == 0);
1309 EXPECT_TRUE(savedOption.securityFlag == 0);
1310
1311 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
1312 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1313 g_kvNbDelegatePtr = nullptr;
1314 EXPECT_TRUE(g_mgr.DeleteKvStore("SingleVerGetSecurityOption001") == OK);
1315 }
1316
1317 /**
1318 * @tc.name: SingleVerGetSecurityOption002
1319 * @tc.desc: Test GetSecurityOption interface for the single ver database.
1320 * @tc.type: FUNC
1321 * @tc.require: AR000EV1G2
1322 * @tc.author: liuwenkai
1323 */
1324 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, SingleVerGetSecurityOption002, TestSize.Level1)
1325 {
1326 SecurityOption savedOption;
1327 std::shared_ptr<IProcessSystemApiAdapter> adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
1328 EXPECT_TRUE(adapter != nullptr);
1329 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter);
1330 KvStoreNbDelegate::Option option;
1331 option.storageEngineType = GAUSSDB_RD;
1332
1333 /**
1334 * @tc.steps:step1. Create databases with securityOption.
1335 * @tc.expected: step2. Returns a non-null kvstore and get right SecurityOption.
1336 */
1337 option.secOption.securityLabel = S3;
1338 option.secOption.securityFlag = 1;
1339 g_mgr.GetKvStore("SingleVerGetSecurityOption002", option, g_kvNbDelegateCallback);
1340 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1341 EXPECT_TRUE(g_kvDelegateStatus == OK);
1342 EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
1343 EXPECT_TRUE(savedOption.securityLabel == S3);
1344 EXPECT_TRUE(savedOption.securityFlag == 1);
1345 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
1346
1347 /**
1348 * @tc.steps:step2. Create databases without securityOption.
1349 * @tc.expected: step2. Returns a non-null kvstore and get right SecurityOption.
1350 */
1351 option.secOption.securityLabel = 0;
1352 option.secOption.securityFlag = 0;
1353 g_mgr.GetKvStore("SingleVerGetSecurityOption002", option, g_kvNbDelegateCallback);
1354 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1355 EXPECT_TRUE(g_kvDelegateStatus == OK);
1356 EXPECT_TRUE(g_kvNbDelegatePtr->GetSecurityOption(savedOption) == OK);
1357 EXPECT_TRUE(savedOption.securityLabel == S3);
1358 EXPECT_TRUE(savedOption.securityFlag == 1);
1359
1360 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
1361 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1362 g_kvNbDelegatePtr = nullptr;
1363 EXPECT_TRUE(g_mgr.DeleteKvStore("SingleVerGetSecurityOption002") == OK);
1364 }
1365
1366 /**
1367 * @tc.name: MaxLogSize001
1368 * @tc.desc: Test the pragma cmd of the max log size limit.
1369 * @tc.type: FUNC
1370 * @tc.require:
1371 * @tc.author: wangbingquan
1372 */
1373 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, MaxLogSize001, TestSize.Level2)
1374 {
1375 /**
1376 * @tc.steps:step1. Create database.
1377 * @tc.expected: step1. Returns a non-null kvstore.
1378 */
1379 g_mgr.GetKvStore("MaxLogSize001", g_option, g_kvNbDelegateCallback);
1380 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1381 EXPECT_TRUE(g_kvDelegateStatus == OK);
1382
1383 /**
1384 * @tc.steps:step2. Setting the max log limit for the valid value.
1385 * @tc.expected: step2. Returns NOT_SUPPORT.
1386 */
1387 uint64_t logSize = DBConstant::MAX_LOG_SIZE_HIGH;
1388 PragmaData pragLimit = static_cast<PragmaData>(&logSize);
1389 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1390
1391 logSize = DBConstant::MAX_LOG_SIZE_LOW;
1392 pragLimit = static_cast<PragmaData>(&logSize);
1393 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1394
1395 logSize = 10 * 1024 * 1024; // 10M
1396 pragLimit = static_cast<PragmaData>(&logSize);
1397 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1398
1399 /**
1400 * @tc.steps:step3. Setting the max log limit for the invalid value.
1401 * @tc.expected: step3. Returns INLIVAD_ARGS.
1402 */
1403 logSize = DBConstant::MAX_LOG_SIZE_HIGH + 1;
1404 pragLimit = static_cast<PragmaData>(&logSize);
1405 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1406
1407 logSize = DBConstant::MAX_LOG_SIZE_LOW - 1;
1408 pragLimit = static_cast<PragmaData>(&logSize);
1409 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1410 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1411 g_kvNbDelegatePtr = nullptr;
1412 EXPECT_TRUE(g_mgr.DeleteKvStore("MaxLogSize001") == OK);
1413 }
1414
1415 /**
1416 * @tc.name: ForceCheckpoint002
1417 * @tc.desc: Test the checkpoint of the database.
1418 * @tc.type: FUNC
1419 * @tc.require:
1420 * @tc.author: wangbingquan
1421 */
1422 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, MaxLogSize002, TestSize.Level2)
1423 {
1424 /**
1425 * @tc.steps:step1. Create database.
1426 * @tc.expected: step1. Returns a non-null kvstore.
1427 */
1428 g_mgr.GetKvStore("MaxLogSize002", g_option, g_kvNbDelegateCallback);
1429 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1430 EXPECT_TRUE(g_kvDelegateStatus == OK);
1431
1432 /**
1433 * @tc.steps:step2. Put the random entry into the database.
1434 * @tc.expected: step2. Returns OK.
1435 */
1436 Key key;
1437 Value value;
1438 DistributedDBToolsUnitTest::GetRandomKeyValue(key, 30); // for 30B random key
1439 DistributedDBToolsUnitTest::GetRandomKeyValue(value, 1024); // 1M value
1440 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
1441 DistributedDBToolsUnitTest::GetRandomKeyValue(key, 40); // for 40B random key
1442 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
1443
1444 DistributedDBToolsUnitTest::GetRandomKeyValue(key, 20); // for 20B random key
1445 DistributedDBToolsUnitTest::GetRandomKeyValue(value, 30); // 30B
1446 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
1447
1448 /**
1449 * @tc.steps:step3. Get the resultset.
1450 * @tc.expected: step3. Returns OK.
1451 */
1452 KvStoreResultSet *resultSet = nullptr;
1453 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(Key{}, resultSet), OK);
1454 ASSERT_NE(resultSet, nullptr);
1455 EXPECT_EQ(resultSet->GetCount(), 3); // size of all the entries is 3
1456 EXPECT_EQ(resultSet->MoveToFirst(), true);
1457
1458 /**
1459 * @tc.steps:step4. Put more data into the database.
1460 * @tc.expected: step4. Returns OK.
1461 */
1462 uint64_t logSize = 6 * 1024 * 1024; // 6M for initial test.
1463 PragmaData pragLimit = static_cast<PragmaData>(&logSize);
1464 // rd pragma only support checkpoint only
1465 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(SET_MAX_LOG_LIMIT, pragLimit), NOT_SUPPORT);
1466
1467 g_kvNbDelegatePtr->CloseResultSet(resultSet);
1468 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1469 EXPECT_EQ(g_mgr.DeleteKvStore("MaxLogSize002"), OK);
1470 g_kvNbDelegatePtr = nullptr;
1471 }
1472
1473 /**
1474 * @tc.name: MaxLogCheckPoint001
1475 * @tc.desc: Pragma the checkpoint command.
1476 * @tc.type: FUNC
1477 * @tc.require:
1478 * @tc.author: wangbingquan
1479 */
1480 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, MaxLogCheckPoint001, TestSize.Level2)
1481 {
1482 /**
1483 * @tc.steps:step1. Create database.
1484 * @tc.expected: step1. Returns a non-null kvstore.
1485 */
1486 g_mgr.GetKvStore("MaxLogCheckPoint001", g_option, g_kvNbDelegateCallback);
1487 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1488 EXPECT_TRUE(g_kvDelegateStatus == OK);
1489
1490 /**
1491 * @tc.steps:step2. Put the random entry into the database.
1492 * @tc.expected: step2. Returns OK.
1493 */
1494 Key key;
1495 Value value;
1496 DistributedDBToolsUnitTest::GetRandomKeyValue(key, 30); // for 30B random key(different size)
1497 DistributedDBToolsUnitTest::GetRandomKeyValue(value, 1 * 1024); // 1M
1498 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, value), OK);
1499 EXPECT_EQ(g_kvNbDelegatePtr->Delete(key), OK);
1500
1501 /**
1502 * @tc.steps:step3. Get the disk file size, execute the checkpoint and get the disk file size.
1503 * @tc.expected: step3. Returns DB_ERROR because of Getting file size is not currently supported
1504 */
1505 int param = 0;
1506 PragmaData paraData = static_cast<PragmaData>(¶m);
1507 EXPECT_EQ(g_kvNbDelegatePtr->Pragma(EXEC_CHECKPOINT, paraData), OK);
1508 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1509 EXPECT_EQ(g_mgr.DeleteKvStore("MaxLogCheckPoint001"), OK);
1510 g_kvNbDelegatePtr = nullptr;
1511 }
1512
1513 /**
1514 * @tc.name: OpenStorePathCheckTest001
1515 * @tc.desc: Test open store with same label but different path.
1516 * @tc.type: FUNC
1517 * @tc.require: AR000GK58F
1518 * @tc.author: lianhuix
1519 */
1520 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, OpenStorePathCheckTest001, TestSize.Level1)
1521 {
1522 std::string dir1 = g_testDir + "/dbDir1";
1523 EXPECT_EQ(OS::MakeDBDirectory(dir1), E_OK);
1524 std::string dir2 = g_testDir + "/dbDir2";
1525 EXPECT_EQ(OS::MakeDBDirectory(dir2), E_OK);
1526
1527 KvStoreDelegateManager mgr1(APP_ID, USER_ID);
1528 mgr1.SetKvStoreConfig({dir1});
1529
1530 KvStoreNbDelegate *delegate1 = nullptr;
1531 auto callback1 = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
1532 placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(delegate1));
1533
1534 mgr1.GetKvStore(STORE_ID_1, g_option, callback1);
1535 EXPECT_EQ(g_kvDelegateStatus, OK);
1536 ASSERT_NE(delegate1, nullptr);
1537
1538 KvStoreNbDelegate *delegate2 = nullptr;
1539 auto callback2 = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
1540 placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(delegate2));
1541 KvStoreDelegateManager mgr2(APP_ID, USER_ID);
1542 mgr2.SetKvStoreConfig({dir2});
1543 mgr2.GetKvStore(STORE_ID_1, g_option, callback2);
1544 EXPECT_EQ(g_kvDelegateStatus, INVALID_ARGS);
1545 ASSERT_EQ(delegate2, nullptr);
1546
1547 mgr1.CloseKvStore(delegate1);
1548 mgr1.DeleteKvStore(STORE_ID_1);
1549 mgr2.CloseKvStore(delegate2);
1550 mgr2.DeleteKvStore(STORE_ID_1);
1551 }
1552
1553 /**
1554 * @tc.name: GetKeys001
1555 * @tc.desc: Test get keys from the database.
1556 * @tc.type: FUNC
1557 * @tc.require:
1558 * @tc.author: zhangqiquan
1559 */
1560 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, GetKeys001, TestSize.Level1)
1561 {
1562 /**
1563 * @tc.steps:step1. Create database.
1564 * @tc.expected: step1. Returns a non-null kvstore.
1565 */
1566 g_mgr.GetKvStore("GetKeys001", g_option, g_kvNbDelegateCallback);
1567 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1568 EXPECT_TRUE(g_kvDelegateStatus == OK);
1569
1570 /**
1571 * @tc.steps:step2. Put the all keys into the database.
1572 * @tc.expected: step2. Returns OK.
1573 */
1574 std::vector<Key> expectKeys = {
1575 {'k', '1', '1'},
1576 {'k', '2'},
1577 {'k', '3'},
1578 {'k', '4'}
1579 };
1580 for (const auto &key : expectKeys) {
1581 EXPECT_EQ(g_kvNbDelegatePtr->Put(key, key), OK);
1582 }
1583 EXPECT_EQ(g_kvNbDelegatePtr->Put({'k', '2'}, {'k', '2'}), OK);
1584 EXPECT_EQ(g_kvNbDelegatePtr->Delete({'k', '4'}), OK);
1585
1586 /**
1587 * @tc.steps:step3. Get the all keys.
1588 * @tc.expected: step3. Returns OK.
1589 */
1590 Key keyPrefix = {'k', '1'};
1591 std::vector<Key> actualKeys;
1592 EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), NOT_SUPPORT);
1593 EXPECT_EQ(actualKeys.size(), 0u); // get the k11, but now not support, so it is 0 for now
1594 for (const auto &key : actualKeys) {
1595 EXPECT_EQ(key, expectKeys[0]);
1596 }
1597 keyPrefix.clear();
1598 EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), NOT_SUPPORT);
1599 EXPECT_EQ(actualKeys.size(), 0u); // size of all the key is 3, but now not support, so it is 0 for now
1600
1601 keyPrefix = {'k', '4'};
1602 EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), NOT_SUPPORT);
1603 EXPECT_EQ(actualKeys.size(), 0u); // not found key and size is 0
1604
1605 DistributedDBToolsUnitTest::GetRandomKeyValue(keyPrefix, 2048); // for 2048B random key
1606 EXPECT_EQ(g_kvNbDelegatePtr->GetKeys(keyPrefix, actualKeys), NOT_SUPPORT);
1607 EXPECT_EQ(actualKeys.size(), 0u); // invalid prefix key and size is 0
1608
1609 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1610 EXPECT_EQ(g_mgr.DeleteKvStore("GetKeys001"), OK);
1611 g_kvNbDelegatePtr = nullptr;
1612 }
1613
1614 /**
1615 * @tc.name: TimeChangeWithCloseStoreTest001
1616 * @tc.desc: Test close store with time changed
1617 * @tc.type: FUNC
1618 * @tc.require:
1619 * @tc.author: lianhuix
1620 */
1621 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, TimeChangeWithCloseStoreTest001, TestSize.Level3)
1622 {
1623 KvStoreDelegateManager mgr(APP_ID, USER_ID);
1624 mgr.SetKvStoreConfig(g_config);
1625
1626 std::atomic<bool> isFinished(false);
1627
1628 std::vector<std::thread> slowThreads;
1629 for (int i = 0; i < 10; i++) { // 10: thread to slow donw system
__anonb17bbd990202() 1630 std::thread th([&isFinished]() {
1631 while (!isFinished) {
1632 // pass
1633 }
1634 });
1635 slowThreads.emplace_back(std::move(th));
1636 }
1637
__anonb17bbd990302() 1638 std::thread th([&isFinished]() {
1639 int timeChangedCnt = 0;
1640 while (!isFinished.load()) {
1641 OS::SetOffsetBySecond(100 - timeChangedCnt++ * 2); // 100 2 : fake system time change
1642 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 100: wait for a while
1643 }
1644 });
1645
1646 for (int i = 0; i < 100; i++) { // run 100 times
1647 mgr.GetKvStore(STORE_ID_1, g_option, g_kvNbDelegateCallback);
1648 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1649 EXPECT_EQ(g_kvDelegateStatus, OK);
1650 EXPECT_EQ(mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1651 g_kvNbDelegatePtr = nullptr;
1652 }
1653
1654 std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // 1000: wait for a while
1655 isFinished.store(true);
1656 th.join();
1657 for (auto &it : slowThreads) {
1658 it.join();
1659 }
1660 EXPECT_EQ(mgr.DeleteKvStore(STORE_ID_1), OK);
1661 }
1662
1663 /**
1664 * @tc.name: TimeChangeWithCloseStoreTest002
1665 * @tc.desc: Test close store with time changed
1666 * @tc.type: FUNC
1667 * @tc.require:
1668 * @tc.author: zhangqiquan
1669 */
1670 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, TimeChangeWithCloseStoreTest002, TestSize.Level3)
1671 {
1672 KvStoreDelegateManager mgr(APP_ID, USER_ID);
1673 mgr.SetKvStoreConfig(g_config);
1674
1675 mgr.GetKvStore(STORE_ID_1, g_option, g_kvNbDelegateCallback);
1676 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1677 EXPECT_EQ(g_kvDelegateStatus, OK);
1678 const int threadPoolMax = 10;
1679 for (int i = 0; i < threadPoolMax; ++i) {
__anonb17bbd990402() 1680 (void) RuntimeContext::GetInstance()->ScheduleTask([]() {
1681 std::this_thread::sleep_for(std::chrono::seconds(10)); // sleep 10s for block thread pool
1682 });
1683 }
1684 OS::SetOffsetBySecond(100); // 100 2 : fake system time change
1685 std::this_thread::sleep_for(std::chrono::seconds(1)); // sleep 1s for time tick
1686
1687 EXPECT_EQ(mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1688 g_kvNbDelegatePtr = nullptr;
1689
1690 EXPECT_EQ(mgr.DeleteKvStore(STORE_ID_1), OK);
1691 RuntimeContext::GetInstance()->StopTaskPool(); // stop all async task
1692 }
1693
1694 /**
1695 * @tc.name: TimeChangeWithCloseStoreTest003
1696 * @tc.desc: Test store close with timechange listener
1697 * @tc.type: FUNC
1698 * @tc.require:
1699 * @tc.author: zhangqiquan
1700 */
1701 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, TimeChangeWithCloseStoreTest003, TestSize.Level3)
1702 {
1703 /**
1704 * @tc.steps:step1. Create database.
1705 * @tc.expected: step1. Returns a non-null kvstore.
1706 */
1707 g_mgr.GetKvStore("TimeChangeWithCloseStoreTest003", g_option, g_kvNbDelegateCallback);
1708 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1709 EXPECT_TRUE(g_kvDelegateStatus == OK);
1710 std::shared_ptr<bool> timeChange = std::make_shared<bool>(false);
1711 int errCode = E_OK;
__anonb17bbd990502(void *) 1712 auto *listener = RuntimeContext::GetInstance()->RegisterTimeChangedLister([timeChange](void *) {
1713 std::this_thread::sleep_for(std::chrono::seconds(10)); // block close store 10s
1714 *timeChange = true;
1715 }, nullptr, errCode);
1716 /**
1717 * @tc.steps:step2. Block time change 10s and trigger time change.
1718 * @tc.expected: step2. close store cost time > 5s.
1719 */
1720 ASSERT_EQ(errCode, E_OK);
1721 OS::SetOffsetBySecond(100); // 100 : fake system time change
1722 std::this_thread::sleep_for(std::chrono::seconds(1)); // wait 1s for time change
1723 Timestamp beginTime;
1724 (void)OS::GetCurrentSysTimeInMicrosecond(beginTime);
1725 ASSERT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1726 Timestamp endTime;
1727 (void)OS::GetCurrentSysTimeInMicrosecond(endTime);
1728 if (*timeChange) {
1729 EXPECT_GE(static_cast<int>(endTime - beginTime), 5 * 1000 * 1000); // 5 * 1000 * 1000 = 5s
1730 }
1731 listener->Drop(true);
1732 OS::SetOffsetBySecond(-100); // -100 : fake system time change
1733 g_kvNbDelegatePtr = nullptr;
1734 EXPECT_EQ(g_mgr.DeleteKvStore("TimeChangeWithCloseStoreTest003"), OK);
1735 }
1736
1737 /**
1738 * @tc.name: ResultSetLimitTest001
1739 * @tc.desc: Get result set over limit
1740 * @tc.type: FUNC
1741 * @tc.require:
1742 * @tc.author: lianhuix
1743 */
1744 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, ResultSetLimitTest001, TestSize.Level0)
1745 {
1746 /**
1747 * @tc.steps:step1. Create database.
1748 * @tc.expected: step1. Returns a non-null kvstore.
1749 */
1750 g_mgr.GetKvStore("ResultSetLimitTest001", g_option, g_kvNbDelegateCallback);
1751 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1752 EXPECT_TRUE(g_kvDelegateStatus == OK);
1753
1754 /**
1755 * @tc.steps:step2. Put the random entry into the database.
1756 * @tc.expected: step2. Returns OK.
1757 */
1758 EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
1759 EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_2, VALUE_2), OK);
1760 EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_3, VALUE_3), OK);
1761
1762 /**
1763 * @tc.steps:step3. Get the resultset overlimit.
1764 * @tc.expected: step3. In limit returns OK, else return OVER_MAX_LIMITS.
1765 */
1766 std::vector<KvStoreResultSet *> dataResultSet;
1767 for (int i = 0; i < 8; i++) { // 8: max result set count
1768 KvStoreResultSet *resultSet = nullptr;
1769 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(Key{}, resultSet), OK);
1770 dataResultSet.push_back(resultSet);
1771 EXPECT_NE(resultSet, nullptr);
1772 }
1773
1774 KvStoreResultSet *resultSet = nullptr;
1775 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(Key{}, resultSet), OVER_MAX_LIMITS);
1776 EXPECT_EQ(resultSet, nullptr);
1777 if (resultSet != nullptr) {
1778 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
1779 }
1780
1781 /**
1782 * @tc.steps:step4. Close result set and store.
1783 * @tc.expected: step4. Returns OK.
1784 */
1785 for (auto it : dataResultSet) {
1786 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(it), OK);
1787 }
1788
1789 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1790 EXPECT_EQ(g_mgr.DeleteKvStore("ResultSetLimitTest001"), OK);
1791 g_kvNbDelegatePtr = nullptr;
1792 }
1793
GetDataFromDatabase()1794 void GetDataFromDatabase()
1795 {
1796 int successTime = 0;
1797 for (size_t i = 0; i < 1000; i++) { // cycle 1000 times.
1798 Value value;
1799 EXPECT_EQ(g_kvNbDelegatePtr->Get(KEY_1, value), OK);
1800 EXPECT_EQ(value, VALUE_1);
1801 successTime += 1;
1802 }
1803 LOGD("Succeed times is %d", successTime);
1804 }
1805
FreqGet001()1806 void FreqGet001()
1807 {
1808 /**
1809 * @tc.steps:step1. Get KV store connection as well as putting an entry (KEY_1, VALUE_1)
1810 * @tc.expected: step1. Returns OK.
1811 */
1812 g_mgr.GetKvStore("FreqGet001", g_option, g_kvNbDelegateCallback);
1813 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1814 EXPECT_TRUE(g_kvDelegateStatus == OK);
1815 EXPECT_EQ(g_kvNbDelegatePtr->Put(KEY_1, VALUE_1), OK);
1816 /**
1817 * @tc.steps:step2. multiple threads tring to get the entry
1818 * @tc.expected: step2. Returns OK.
1819 */
1820 std::thread t1(GetDataFromDatabase);
1821 std::thread t2(GetDataFromDatabase);
1822 std::thread t3(GetDataFromDatabase);
1823 std::thread t4(GetDataFromDatabase);
1824 t1.join();
1825 t2.join();
1826 t3.join();
1827 t4.join();
1828 /**
1829 * @tc.steps:step3. Close and delete KV store
1830 * @tc.expected: step3. Returns OK.
1831 */
1832 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1833 EXPECT_EQ(g_mgr.DeleteKvStore("FreqGet001"), OK);
1834 g_kvNbDelegatePtr = nullptr;
1835 }
1836
1837 /**
1838 * @tc.name: FreqGet001
1839 * @tc.desc: Open and close the kv store concurrently.
1840 * @tc.type: FUNC
1841 * @tc.require:
1842 * @tc.author: wanyi
1843 */
1844 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, FreqGet001, TestSize.Level2)
1845 {
1846 ASSERT_NO_FATAL_FAILURE(FreqGet001());
1847 }
1848
PutRangeDataIntoDB()1849 void PutRangeDataIntoDB()
1850 {
1851 for (int i = 0; i < 6; i++) { // 6 is the number of data inserted
1852 std::vector<uint8_t> keyTemp;
1853 std::vector<uint8_t> valTemp;
1854 keyTemp.push_back('0' + i);
1855 valTemp.push_back('0' + i);
1856 Entry entryTmp;
1857 entryTmp.key = keyTemp;
1858 entryTmp.value = valTemp;
1859 EXPECT_EQ(g_kvNbDelegatePtr->Put(entryTmp.key, entryTmp.value), OK);
1860 }
1861 }
1862
1863 /**
1864 * @tc.name: RdRangeQuery001
1865 * @tc.desc: Test GetEntries and the out of the parameter is entries.
1866 * @tc.type: FUNC
1867 * @tc.require: AR.SR.IR20230714002092.017.001
1868 * @tc.author: mazhao
1869 */
1870 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdRangeQuery001, TestSize.Level1)
1871 {
1872 /**
1873 * @tc.steps:step1. Get the nb delegate, and put key {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'} into db.
1874 * @tc.expected: step1. Get results OK and non-null delegate, put data into db successfully.
1875 */
1876 g_mgr.GetKvStore("RdRangeQuery001", g_option, g_kvNbDelegateCallback);
1877 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
1878 EXPECT_EQ(g_kvDelegateStatus, OK);
1879
1880 PutRangeDataIntoDB();
1881
1882 /**
1883 * @tc.steps: step2. Use range query conditions to obtain the dataset and check the dataset
1884 * @tc.expected: step2. The expected data are 2, 3, 4.
1885 */
1886 Query query1 = Query::Select().Range({'2'}, {'4'});
1887 std::vector<Entry> entries;
1888 int ret = g_kvNbDelegatePtr->GetEntries(query1, entries);
1889 EXPECT_EQ(entries.size(), 3u);
1890 int targetKey = 2; // 2 is the initial key that is expected to be found
1891 for (auto item : entries) {
1892 std::string keyStr(item.key.begin(), item.key.end());
1893 EXPECT_EQ(to_string(targetKey), keyStr);
1894 targetKey++;
1895 }
1896
1897 /**
1898 * @tc.steps: step3. Use range query conditions to obtain the dataset and check the dataset
1899 * @tc.expected: step3. The expected data are 0, 1, 2, 3, 4.
1900 */
1901 Query query2 = Query::Select().Range({}, {'4'});
1902 ret = g_kvNbDelegatePtr->GetEntries(query2, entries);
1903 EXPECT_EQ(entries.size(), 5u);
1904 targetKey = 0; // 0 is the initial key that is expected to be found
1905 for (auto item : entries) {
1906 std::string keyStr(item.key.begin(), item.key.end());
1907 EXPECT_EQ(to_string(targetKey), keyStr);
1908 targetKey++;
1909 }
1910
1911 /**
1912 * @tc.steps: step4. Use range query conditions to obtain the dataset and check the dataset
1913 * @tc.expected: step4. The expected data are 2, 3, 4, 5.
1914 */
1915 Query query3 = Query::Select().Range({'2'}, {});
1916 ret = g_kvNbDelegatePtr->GetEntries(query3, entries);
1917 EXPECT_EQ(entries.size(), 4u);
1918 targetKey = 2; // 2 is the initial key that is expected to be found
1919 for (auto item : entries) {
1920 std::string keyStr(item.key.begin(), item.key.end());
1921 EXPECT_EQ(to_string(targetKey), keyStr);
1922 targetKey++;
1923 }
1924
1925 /**
1926 * @tc.steps:step5. Close and delete KV store
1927 * @tc.expected: step5. Returns OK.
1928 */
1929 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
1930 EXPECT_EQ(g_mgr.DeleteKvStore("RdRangeQuery001"), OK);
1931 g_kvNbDelegatePtr = nullptr;
1932 }
1933
ChkRangeResultSet(KvStoreResultSet * resultSet,int beginNum,int EndNum)1934 void ChkRangeResultSet(KvStoreResultSet *resultSet, int beginNum, int EndNum)
1935 {
1936 while (resultSet->MoveToNext()) {
1937 Entry entryValue;
1938 EXPECT_EQ(resultSet->GetEntry(entryValue), OK);
1939 std::string keyStr(entryValue.value.begin(), entryValue.value.end());
1940 EXPECT_EQ(to_string(beginNum), keyStr);
1941 beginNum++;
1942 }
1943 EXPECT_EQ(beginNum, EndNum + 1);
1944 EXPECT_EQ(resultSet->MoveToNext(), false);
1945 }
1946
1947 /**
1948 * @tc.name: RdRangeQuery002
1949 * @tc.desc:Test GetEntries and the out of the parameter is resultSet.
1950 * @tc.type: FUNC
1951 * @tc.require: AR.SR.IR20230714002092.017.001
1952 * @tc.author: mazhao
1953 */
1954 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdRangeQuery002, TestSize.Level1)
1955 {
1956 /**
1957 * @tc.steps:step1. Get the nb delegate, and put key {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'} into db.
1958 * @tc.expected: step1. Get results OK and non-null delegate.
1959 */
1960 g_mgr.GetKvStore("RdRangeQuery002", g_option, g_kvNbDelegateCallback);
1961 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
1962 EXPECT_EQ(g_kvDelegateStatus, OK);
1963 PutRangeDataIntoDB();
1964
1965 /**
1966 * @tc.steps: step2. Use range query conditions to obtain the resultset and check the resultset
1967 * @tc.expected: step2. The expected data are 2, 3, 4.
1968 */
1969 Query fullQuery = Query::Select().Range({'2'}, {'4'});
1970 KvStoreResultSet *resultSet = nullptr;
1971 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery, resultSet), OK);
1972 EXPECT_NE(resultSet, nullptr);
1973 int beginTargetKey = 2; // 2 is the initial key that is expected to be found
1974 int endTargetKey = 4; // 4 is the end key that is expected to be found
1975 ChkRangeResultSet(resultSet, beginTargetKey, endTargetKey);
1976 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
1977 EXPECT_EQ(resultSet, nullptr);
1978
1979 /**
1980 * @tc.steps: step3. Use range query conditions to obtain the resultset and check the resultset
1981 * @tc.expected: step3. The expected data are 0, 1, 2, 3, 4.
1982 */
1983 Query fullQuery2 = Query::Select().Range({}, {'4'});
1984 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery2, resultSet), OK);
1985 EXPECT_NE(resultSet, nullptr);
1986 beginTargetKey = 0; // 0 is the initial key that is expected to be found
1987 endTargetKey = 4; // 4 is the end key that is expected to be found
1988 ChkRangeResultSet(resultSet, beginTargetKey, endTargetKey);
1989 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
1990 EXPECT_EQ(resultSet, nullptr);
1991
1992 /**
1993 * @tc.steps: step4. Use range query conditions to obtain the resultset and check the resultset
1994 * @tc.expected: step4. The expected data are 2, 3, 4, 5.
1995 */
1996 Query fullQuery3 = Query::Select().Range({'2'}, {});
1997 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery3, resultSet), OK);
1998 EXPECT_NE(resultSet, nullptr);
1999 beginTargetKey = 2; // 2 is the initial key that is expected to be found
2000 endTargetKey = 5; // 5 is the end key that is expected to be found
2001 ChkRangeResultSet(resultSet, beginTargetKey, endTargetKey);
2002 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2003 EXPECT_EQ(resultSet, nullptr);
2004
2005 /**
2006 * @tc.steps: step5. Use range query conditions to obtain the resultset and check the resultset
2007 * @tc.expected: step5. The expected data are 0, 1, 2, 3, 4, 5.
2008 */
2009 Query fullQuery4 = Query::Select().Range({}, {});
2010 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery4, resultSet), OK);
2011 EXPECT_NE(resultSet, nullptr);
2012 beginTargetKey = 0; // 0 is the initial key that is expected to be found
2013 endTargetKey = 5; // 5 is the end key that is expected to be found
2014 ChkRangeResultSet(resultSet, beginTargetKey, endTargetKey);
2015 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2016 EXPECT_EQ(resultSet, nullptr);
2017
2018 /**
2019 * @tc.steps:step5. Close and delete KV store
2020 * @tc.expected: step5. Returns OK.
2021 */
2022 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2023 EXPECT_EQ(g_mgr.DeleteKvStore("RdRangeQuery002"), OK);
2024 g_kvNbDelegatePtr = nullptr;
2025 }
2026
2027 /**
2028 * @tc.name: RdRangeQuery003
2029 * @tc.desc: Test GetEntries and the in put paramter is invalid.
2030 * @tc.type: FUNC
2031 * @tc.require: AR.SR.IR20230714002092.017.001
2032 * @tc.author: mazhao
2033 */
2034 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdRangeQuery003, TestSize.Level1)
2035 {
2036 /**
2037 * @tc.steps:step1. Get the nb delegate.
2038 * @tc.expected: step1. Get results OK and non-null delegate.
2039 */
2040 std::vector<Entry> entries;
2041 g_mgr.GetKvStore("RdRangeQuery003", g_option, g_kvNbDelegateCallback);
2042 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
2043 EXPECT_EQ(g_kvDelegateStatus, OK);
2044
2045 /**
2046 * @tc.steps: step2. Use invalid range query conditions to obtain the resultset.
2047 * @tc.expected: step2. return INVALID_ARGS.
2048 */
2049 KvStoreResultSet *resultSet = nullptr;
2050 Query inValidQuery = Query::Select().Range({'2'}, {'4'}).Range({'1'}, {'6'});
2051 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery, resultSet), INVALID_ARGS);
2052 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery, entries), INVALID_ARGS);
2053
2054 /**
2055 * @tc.steps: step3. Use invalid range query conditions to obtain the resultset.
2056 * @tc.expected: step3. return INVALID_ARGS.
2057 */
2058 Query inValidQuery2 = Query::Select().Range({'2'}, {'4'}).And();
2059 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery2, resultSet), INVALID_ARGS);
2060 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery2, entries), INVALID_ARGS);
2061
2062 /**
2063 * @tc.steps: step4. Use invalid range query key is longer than limit conditions to obtain the resultset.
2064 * @tc.expected: step4. return INVALID_ARGS.
2065 */
2066 Key keyCan(1024, 'a');
2067 Query ValidQuery3 = Query::Select().Range(keyCan, {'4'});
2068 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(ValidQuery3, resultSet), OK);
2069 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(ValidQuery3, entries), NOT_FOUND);
2070
2071 Key keyInvalid(1025, 'a');
2072 Query inValidQuery4 = Query::Select().Range(keyInvalid, {'4'});
2073 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery4, resultSet), INVALID_ARGS);
2074 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(inValidQuery4, entries), INVALID_ARGS);
2075
2076 /**
2077 * @tc.steps:step5. Close and delete KV store
2078 * @tc.expected: step5. Returns OK.
2079 */
2080 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2081 EXPECT_EQ(resultSet, nullptr);
2082 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2083 EXPECT_EQ(g_mgr.DeleteKvStore("RdRangeQuery003"), OK);
2084 g_kvNbDelegatePtr = nullptr;
2085 }
2086
ChkRangeResultSetMoveFuc(KvStoreResultSet * resultSet,int beginNum,int endNum)2087 void ChkRangeResultSetMoveFuc(KvStoreResultSet *resultSet, int beginNum, int endNum)
2088 {
2089 EXPECT_EQ(resultSet->MoveToLast(), true);
2090 Entry entryValue;
2091 EXPECT_EQ(resultSet->GetEntry(entryValue), OK);
2092 std::string keyStr(entryValue.value.begin(), entryValue.value.end());
2093 EXPECT_EQ(to_string(endNum), keyStr);
2094
2095 while (resultSet->MoveToPrevious()) {
2096 endNum--;
2097 Entry entryValue2;
2098 EXPECT_EQ(resultSet->GetEntry(entryValue2), OK);
2099 std::string keyStr2(entryValue2.value.begin(), entryValue2.value.end());
2100 EXPECT_EQ(to_string(endNum), keyStr2);
2101 }
2102 EXPECT_EQ(endNum, beginNum);
2103 EXPECT_EQ(resultSet->MoveToPrevious(), false);
2104 }
2105
2106 /**
2107 * @tc.name: RdRangeQuery004
2108 * @tc.desc: Test resultSet fuction.
2109 * @tc.type: FUNC
2110 * @tc.require: AR.SR.IR20230714002092.017.001
2111 * @tc.author: mazhao
2112 */
2113 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdRangeQuery004, TestSize.Level1)
2114 {
2115 /**
2116 * @tc.steps:step1. Get the nb delegate, and put key {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'} into db.
2117 * @tc.expected: step1. Get results OK and non-null delegate.
2118 */
2119 g_mgr.GetKvStore("RdRangeQuery004", g_option, g_kvNbDelegateCallback);
2120 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
2121 EXPECT_EQ(g_kvDelegateStatus, OK);
2122
2123 PutRangeDataIntoDB();
2124 /**
2125 * @tc.steps: step2. Use range query conditions to obtain the resultset and check the resultset move fuction.
2126 * @tc.expected: step2. move founction is OK and the expected data are 2, 3, 4.
2127 */
2128 Query fullQuery = Query::Select().Range({'2'}, {'4'});
2129 KvStoreResultSet *resultSet = nullptr;
2130 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery, resultSet), OK);
2131 EXPECT_NE(resultSet, nullptr);
2132 int beginTargetKey = 2; // 2 is the initial key that is expected to be found
2133 int endTargetKey = 4; // 4 is the end key that is expected to be found
2134 ChkRangeResultSetMoveFuc(resultSet, beginTargetKey, endTargetKey);
2135 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2136 EXPECT_EQ(resultSet, nullptr);
2137
2138 /**
2139 * @tc.steps: step3. Use range query conditions to obtain the resultset and check the resultset move fuction.
2140 * @tc.expected: step3. move founction is OK and the expected data are 0, 1, 2, 3, 4.
2141 */
2142 Query fullQuery2 = Query::Select().Range({}, {'4'});
2143 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery2, resultSet), OK);
2144 EXPECT_NE(resultSet, nullptr);
2145
2146 beginTargetKey = 0; // 0 is the initial key that is expected to be found
2147 endTargetKey = 4; // 4 is the end key that is expected to be found
2148 ChkRangeResultSetMoveFuc(resultSet, beginTargetKey, endTargetKey);
2149 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2150 EXPECT_EQ(resultSet, nullptr);
2151
2152 /**
2153 * @tc.steps: step3. Use range query conditions to obtain the resultset and check the resultset move fuction.
2154 * @tc.expected: step3. move founction is OK and the expected data are 2, 3, 4, 5.
2155 */
2156 Query fullQuery3 = Query::Select().Range({'2'}, {});
2157 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery3, resultSet), OK);
2158 EXPECT_NE(resultSet, nullptr);
2159 beginTargetKey = 2; // 2 is the initial key that is expected to be found
2160 endTargetKey = 5; // 5 is the end key that is expected to be found
2161 ChkRangeResultSetMoveFuc(resultSet, beginTargetKey, endTargetKey);
2162 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2163 EXPECT_EQ(resultSet, nullptr);
2164
2165 /**
2166 * @tc.steps:step5. Close and delete KV store
2167 * @tc.expected: step5. Returns OK.
2168 */
2169 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2170 EXPECT_EQ(g_mgr.DeleteKvStore("RdRangeQuery004"), OK);
2171 g_kvNbDelegatePtr = nullptr;
2172 }
2173
2174 /**
2175 * @tc.name: RdRangeQuery005
2176 * @tc.desc:Test resultSet fuction, end key is bigger or equal than the biggest data in DB.
2177 * @tc.type: FUNC
2178 * @tc.require:
2179 * @tc.author: mazhao
2180 */
2181 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdRangeQuery005, TestSize.Level0)
2182 {
2183 /**
2184 * @tc.steps:step1. Get the nb delegate, and put key {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'} into db.
2185 * @tc.expected: step1. Get results OK and non-null delegate.
2186 */
2187 g_mgr.GetKvStore("RdRangeQuery005", g_option, g_kvNbDelegateCallback);
2188 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
2189 EXPECT_EQ(g_kvDelegateStatus, OK);
2190
2191 PutRangeDataIntoDB();
2192
2193 /**
2194 * @tc.steps: step2. Use range query conditions that end key equal with data in db, then move to next.
2195 * @tc.expected: step2. move to next four times successfully.
2196 */
2197 Query fullQuery = Query::Select().Range({'2'}, {'5'});
2198 KvStoreResultSet *resultSet = nullptr;
2199 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery, resultSet), OK);
2200 EXPECT_NE(resultSet, nullptr);
2201 int beginTargetKey = 0; // 0 is the initial key that is expected to be found
2202 while (resultSet->MoveToNext()) {
2203 beginTargetKey++;
2204 }
2205 EXPECT_EQ(beginTargetKey, 4); // 4 is the end key that is expected to be found
2206 EXPECT_EQ(resultSet->MoveToNext(), false);
2207 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2208 EXPECT_EQ(resultSet, nullptr);
2209
2210 /**
2211 * @tc.steps: step2. Use range query conditions that end key equal with data in db, then move to next.
2212 * @tc.expected: step2. move to next four times successfully.
2213 */
2214 Query fullQuery2 = Query::Select().Range({'2'}, {'8'});
2215 EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(fullQuery2, resultSet), OK);
2216 EXPECT_NE(resultSet, nullptr);
2217 beginTargetKey = 0; // 0 is the initial key that is expected to be found
2218 while (resultSet->MoveToNext()) {
2219 beginTargetKey++;
2220 }
2221 EXPECT_EQ(beginTargetKey, 4); // 4 is the end key that is expected to be found
2222 EXPECT_EQ(resultSet->MoveToNext(), false);
2223 EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(resultSet), OK);
2224 EXPECT_EQ(resultSet, nullptr);
2225
2226 /**
2227 * @tc.steps:step5. Close and delete KV store
2228 * @tc.expected: step5. Returns OK.
2229 */
2230 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2231 EXPECT_EQ(g_mgr.DeleteKvStore("RdRangeQuery005"), OK);
2232 g_kvNbDelegatePtr = nullptr;
2233 }
2234
2235 /**
2236 * @tc.name: RdSync001
2237 * @tc.desc:Test sync func with rd.
2238 * @tc.type: FUNC
2239 * @tc.require:
2240 * @tc.author: caihaoting
2241 */
2242 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdSync001, TestSize.Level0)
2243 {
2244 /**
2245 * @tc.steps:step1. Get the nb delegate.
2246 * @tc.expected: step1. Get results OK and non-null delegate.
2247 */
2248 g_mgr.GetKvStore("RdSync001", g_option, g_kvNbDelegateCallback);
2249 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
2250 EXPECT_EQ(g_kvDelegateStatus, OK);
2251 /**
2252 * @tc.steps:step2. Sync
2253 * @tc.expected: step2. Return NOT_SUPPORT.
2254 */
2255 std::mutex dataMutex;
2256 std::condition_variable cv;
2257 bool finish = false;
__anonb17bbd990602(const std::map<std::string, SyncProcess> &process) 2258 auto callback = [&cv, &dataMutex, &finish](const std::map<std::string, SyncProcess> &process) {
2259 for (const auto &item: process) {
2260 if (item.second.process == DistributedDB::FINISHED) {
2261 {
2262 std::lock_guard<std::mutex> autoLock(dataMutex);
2263 finish = true;
2264 }
2265 cv.notify_one();
2266 }
2267 }
2268 };
2269 CloudSyncOption syncOption = {
2270 .devices = {"deviceName"},
2271 .mode = SYNC_MODE_CLOUD_MERGE,
2272 .query = {},
2273 .waitTime = 0,
2274 .priorityTask = false,
2275 .compensatedSyncOnly = false,
2276 .users = {USER_ID}
2277 };
2278 auto res = g_kvNbDelegatePtr->Sync(syncOption, callback);
2279 EXPECT_EQ(res, NOT_SUPPORT);
2280 /**
2281 * @tc.steps:step3. Close and delete KV store
2282 * @tc.expected: step3. Returns OK.
2283 */
2284 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2285 EXPECT_EQ(g_mgr.DeleteKvStore("RdSync001"), OK);
2286 g_kvNbDelegatePtr = nullptr;
2287 }
2288
2289 /**
2290 * @tc.name: RdSetCloudDB001
2291 * @tc.desc:Test set cloudDB func with rd.
2292 * @tc.type: FUNC
2293 * @tc.require:
2294 * @tc.author: caihaoting
2295 */
2296 HWTEST_F(DistributedDBInterfacesNBDelegateRdTest, RdSetCloudDB001, TestSize.Level0)
2297 {
2298 /**
2299 * @tc.steps:step1. Get the nb delegate.
2300 * @tc.expected: step1. Get results OK and non-null delegate.
2301 */
2302 g_mgr.GetKvStore("RdSetCloudDB001", g_option, g_kvNbDelegateCallback);
2303 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
2304 EXPECT_EQ(g_kvDelegateStatus, OK);
2305 /**
2306 * @tc.steps:step2. Set cloudDB
2307 * @tc.expected: step2. Return NOT_SUPPORT.
2308 */
2309 std::shared_ptr<VirtualCloudDb> virtualCloudDb = std::make_shared<VirtualCloudDb>();
2310 std::map<std::string, std::shared_ptr<ICloudDb>> cloudDbs;
2311 cloudDbs[USER_ID] = virtualCloudDb;
2312 auto res = g_kvNbDelegatePtr->SetCloudDB(cloudDbs);
2313 EXPECT_EQ(res, NOT_SUPPORT);
2314 /**
2315 * @tc.steps:step3. Close and delete KV store
2316 * @tc.expected: step3. Returns OK.
2317 */
2318 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
2319 EXPECT_EQ(g_mgr.DeleteKvStore("RdSetCloudDB001"), OK);
2320 g_kvNbDelegatePtr = nullptr;
2321 }
2322 }
2323 #endif // USE_RD_KERNEL