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 #ifndef OMIT_MULTI_VER
16 #include <gtest/gtest.h>
17 #include <ctime>
18 #include <cmath>
19 #include <thread>
20 #include <condition_variable>
21 #include <mutex>
22 #include <string>
23
24 #include "delegate_kv_mgr_callback.h"
25 #include "distributeddb_data_generator.h"
26 #include "distributed_test_tools.h"
27 #include "kv_store_delegate.h"
28 #include "kv_store_delegate_manager.h"
29 #include "distributed_test_sysinfo.h"
30
31 using namespace std;
32 using namespace testing;
33 #if defined TESTCASES_USING_GTEST_EXT
34 using namespace testing::ext;
35 #endif
36 using namespace DistributedDB;
37 using namespace DistributedDBDataGenerator;
38
39 namespace DistributeddbKvCreate {
40 const int OPER_CNT = 4;
41 DistributedDB::CipherPassword g_passwd1;
42 DistributedDB::CipherPassword g_passwd2;
43 class DistributeddbKvCreateTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase(void)51 void DistributeddbKvCreateTest::SetUpTestCase(void)
52 {
53 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
54 (void)g_passwd2.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
55 }
56
TearDownTestCase(void)57 void DistributeddbKvCreateTest::TearDownTestCase(void)
58 {
59 }
60
SetUp(void)61 void DistributeddbKvCreateTest::SetUp(void)
62 {
63 UnitTest *test = UnitTest::GetInstance();
64 ASSERT_NE(test, nullptr);
65 const TestInfo *testinfo = test->current_test_info();
66 ASSERT_NE(testinfo, nullptr);
67 string testCaseName = string(testinfo->name());
68 MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
69 }
70
TearDown(void)71 void DistributeddbKvCreateTest::TearDown(void)
72 {
73 }
74
75 /*
76 * @tc.name: Open 001
77 * @tc.desc: Verify that the kv db was created successfully.
78 * @tc.type: FUNC
79 * @tc.require: SR000CQS3O
80 * @tc.author: luqianfu
81 */
82 HWTEST_F(DistributeddbKvCreateTest, Open001, TestSize.Level1)
83 {
84 KvStoreDelegateManager *manager1 = nullptr;
85 KvStoreDelegateManager *manager2 = nullptr;
86 KvStoreDelegateManager *manager3 = nullptr;
87 KvStoreDelegateManager *manager4 = nullptr;
88
89 /**
90 * @tc.steps: step1. create kv db with params storeId1, appId1, userId1.
91 * @tc.expected: step1. the kv db was created successfully.
92 */
93 KvStoreDelegate *result = nullptr;
94 result = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter1, g_kvOption);
95 ASSERT_TRUE(manager1 != nullptr && result != nullptr);
96 EXPECT_TRUE(manager1->CloseKvStore(result) == OK);
97 result = nullptr;
98 /**
99 * @tc.steps: step2. create kv db with params storeId2, appId1, userId1.
100 * @tc.expected: step2. the kv db was created successfully.
101 */
102 result = DistributedTestTools::GetDelegateSuccess(manager2, g_kvdbParameter2_1_1, g_kvOption);
103 ASSERT_TRUE(manager2 != nullptr && result != nullptr);
104 EXPECT_TRUE(manager2->CloseKvStore(result) == OK);
105 result = nullptr;
106 /**
107 * @tc.steps: step3. create kv db with params storeId1, appId2, userId1.
108 * @tc.expected: step3. the kv db was created successfully.
109 */
110 result = DistributedTestTools::GetDelegateSuccess(manager3, g_kvdbParameter1_2_1, g_kvOption);
111 ASSERT_TRUE(manager3 != nullptr && result != nullptr);
112 EXPECT_TRUE(manager3->CloseKvStore(result) == OK);
113 result = nullptr;
114 /**
115 * @tc.steps: step4. create kv db with params storeId1, appId1, userId2.
116 * @tc.expected: step4. the kv db was created successfully.
117 */
118 result = DistributedTestTools::GetDelegateSuccess(manager4, g_kvdbParameter1_1_2, g_kvOption);
119 ASSERT_TRUE(manager4 != nullptr && result != nullptr);
120 EXPECT_EQ(manager4->CloseKvStore(result), OK);
121 result = nullptr;
122
123 EXPECT_EQ(manager1->DeleteKvStore(STORE_ID_1), OK);
124 delete manager1;
125 manager1 = nullptr;
126 EXPECT_EQ(manager2->DeleteKvStore(STORE_ID_2), OK);
127 delete manager2;
128 manager2 = nullptr;
129 EXPECT_EQ(manager3->DeleteKvStore(STORE_ID_1), OK);
130 delete manager3;
131 manager3 = nullptr;
132 EXPECT_EQ(manager4->DeleteKvStore(STORE_ID_1), OK);
133 delete manager4;
134 manager4 = nullptr;
135 }
136
137 /*
138 * @tc.name: Open 002
139 * @tc.desc: Verify that the kv db can be reopened successfully
140 * @tc.type: FUNC
141 * @tc.require: SR000CQS3O
142 * @tc.author: luqianfu
143 */
144 HWTEST_F(DistributeddbKvCreateTest, Open002, TestSize.Level1)
145 {
146 KvStoreDelegate *result = nullptr;
147 KvStoreDelegateManager *manager = nullptr;
148 /**
149 * @tc.steps: step1. create kv db.
150 * @tc.expected: step1. the kv db was created successfully.
151 */
152 KvOption option = g_kvOption;
153 option.localOnly = IS_LOCAL_ONLY;
154 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
155 ASSERT_TRUE(manager != nullptr && result != nullptr);
156 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
157 delete manager;
158 manager = nullptr;
159 /**
160 * @tc.steps: step2. reopen kv db.
161 * @tc.expected: step2. the kv db was reopened successfully.
162 */
163 option.createIfNecessary = IS_NOT_NEED_CREATE;
164 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
165 ASSERT_TRUE(manager != nullptr && result != nullptr);
166
167 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
168 result = nullptr;
169 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
170 delete manager;
171 manager = nullptr;
172 }
173
174 /*
175 * @tc.name: Open 003
176 * @tc.desc: Verify that can not reopen an absent db.
177 * @tc.type: FUNC
178 * @tc.require: SR000CQS3O
179 * @tc.author: luqianfu
180 */
181 HWTEST_F(DistributeddbKvCreateTest, Open003, TestSize.Level1)
182 {
183 /**
184 * @tc.steps: step1. delete kv db if exist.
185 * @tc.expected: step1. Construct that no database exist.
186 */
187 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
188 ASSERT_NE(manager, nullptr);
189 SetDir(DIRECTOR);
190 DBStatus status = manager->SetKvStoreConfig(KV_CONFIG);
191 EXPECT_EQ(status, DBStatus::OK);
192 status = manager->DeleteKvStore(STORE_ID_1);
193 EXPECT_EQ(status, DBStatus::NOT_FOUND);
194 delete manager;
195 manager = nullptr;
196
197 KvStoreDelegate *delegate = nullptr;
198 /**
199 * @tc.steps: step2. reopen an absent db.
200 * @tc.expected: step2. reopen failed and return error.
201 */
202 KvOption option = g_kvOption;
203 option.createIfNecessary = IS_NOT_NEED_CREATE;
204 option.localOnly = IS_LOCAL_ONLY;
205 status = DistributedTestTools::GetDelegateNotGood(manager, delegate, STORE_ID_1, option);
206 ASSERT_NE(status, DBStatus::OK);
207 EXPECT_EQ(delegate, nullptr);
208 EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
209
210 delete manager;
211 manager = nullptr;
212 }
213
214 /*
215 * @tc.name: Open 004
216 * @tc.desc: Verify that transferring param IS_NOT_NEED_CREATE can not create kv db.
217 * @tc.type: FUNC
218 * @tc.require: SR000BUH3J
219 * @tc.author: luqianfu
220 */
221 HWTEST_F(DistributeddbKvCreateTest, Open004, TestSize.Level1)
222 {
223 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_2);
224 ASSERT_NE(manager, nullptr);
225 SetDir(DIRECTOR);
226 EXPECT_EQ(manager->SetKvStoreConfig(KV_CONFIG), DBStatus::OK);
227 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), DBStatus::NOT_FOUND);
228
229 KvStoreDelegateManager *manager1 = nullptr;
230 KvStoreDelegateManager *manager2 = nullptr;
231 /**
232 * @tc.steps: step1. create kv db with param IS_NOT_NEED_CREATE.
233 * @tc.expected: step1. create kv db and close it failed.
234 */
235 KvOption option = g_kvOption;
236 option.createIfNecessary = IS_NOT_NEED_CREATE;
237 KvStoreDelegate *result1 = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter1_1_2, option);
238 EXPECT_TRUE(manager1 == nullptr && result1 == nullptr);
239 EXPECT_TRUE(manager->CloseKvStore(result1) != OK);
240 delete manager;
241 manager = nullptr;
242
243 /**
244 * @tc.steps: step2. create kv db with param IS_NEED_CREATE.
245 * @tc.expected: step2. create kv db and close it successfully.
246 */
247 KvStoreDelegate *result2 = DistributedTestTools::GetDelegateSuccess(manager2, g_kvdbParameter1_1_2,
248 g_kvOption);
249 ASSERT_TRUE(manager2 != nullptr && result2 != nullptr);
250 EXPECT_TRUE(manager2->CloseKvStore(result2) == OK);
251 result2 = nullptr;
252 EXPECT_TRUE(manager2->DeleteKvStore(STORE_ID_1) == OK);
253 delete manager2;
254 manager2 = nullptr;
255 }
256
257 /*
258 * @tc.name: Open 005
259 * @tc.desc: Verify that can not create or open kv db with exceptional params.
260 * @tc.type: EXCEPTION
261 * @tc.require: SR000BUH3J
262 * @tc.author: luqianfu
263 */
264 HWTEST_F(DistributeddbKvCreateTest, Open005, TestSize.Level1)
265 {
266 KvStoreDelegateManager *manager1 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
267 ASSERT_NE(manager1, nullptr);
268 KvStoreDelegateManager *manager2 = new (std::nothrow) KvStoreDelegateManager("", USER_ID_1);
269 ASSERT_NE(manager2, nullptr);
270 KvStoreDelegateManager *manager3 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, "");
271 ASSERT_NE(manager3, nullptr);
272 KvStoreDelegateManager *manager4 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
273 ASSERT_NE(manager4, nullptr);
274 KvStoreDelegate *delegate1 = nullptr;
275 KvStoreDelegate *delegate2 = nullptr;
276 KvStoreDelegate *delegate3 = nullptr;
277 KvStoreDelegate *delegate4 = nullptr;
278
279 /**
280 * @tc.steps: step1. create kv db with param storeId is null.
281 * @tc.expected: step1. create kv db failed.
282 */
283 KvOption option1 = g_kvOption;
284 option1.createIfNecessary = IS_NEED_CREATE;
285 option1.localOnly = IS_LOCAL_ONLY;
286 DBStatus status = DistributedTestTools::GetDelegateNotGood(manager1, delegate1, "", option1);
287 ASSERT_EQ(status, DBStatus::INVALID_ARGS);
288 EXPECT_TRUE(delegate1 == nullptr);
289 EXPECT_TRUE(manager1->CloseKvStore(delegate1) == INVALID_ARGS);
290
291 /**
292 * @tc.steps: step2. create kv db with param appId is null.
293 * @tc.expected: step2. create kv db failed.
294 */
295 KvOption option2 = g_kvOption;
296 option2.createIfNecessary = IS_NEED_CREATE;
297 option2.localOnly = IS_LOCAL_ONLY;
298 status = DistributedTestTools::GetDelegateNotGood(manager2, delegate2, STORE_ID_1, option2);
299 ASSERT_EQ(status, DBStatus::INVALID_ARGS);
300 EXPECT_TRUE(delegate2 == nullptr);
301 EXPECT_TRUE(manager2->CloseKvStore(delegate2) == INVALID_ARGS);
302
303 /**
304 * @tc.steps: step3. create kv db with param userId is null.
305 * @tc.expected: step3. create kv db failed.
306 */
307 KvOption option3 = g_kvOption;
308 option3.createIfNecessary = IS_NEED_CREATE;
309 option3.localOnly = IS_LOCAL_ONLY;
310 status = DistributedTestTools::GetDelegateNotGood(manager3, delegate3, STORE_ID_1, option3);
311 ASSERT_EQ(status, DBStatus::INVALID_ARGS);
312 EXPECT_TRUE(delegate3 == nullptr);
313 EXPECT_TRUE(manager3->CloseKvStore(delegate3) == INVALID_ARGS);
314
315 /**
316 * @tc.steps: step3. create kv db with param userId is null.
317 * @tc.expected: step3. create kv db failed.
318 */
319 KvOption option4 = g_kvOption;
320 option4.createIfNecessary = IS_NOT_NEED_CREATE;
321 option4.localOnly = IS_LOCAL_ONLY;
322 status = DistributedTestTools::GetDelegateNotGood(manager4, delegate4, STORE_ID_1, option4);
323 ASSERT_NE(status, DBStatus::OK);
324 EXPECT_TRUE(delegate4 == nullptr);
325 EXPECT_TRUE(manager4->CloseKvStore(delegate4) == INVALID_ARGS);
326
327 delete manager1;
328 manager1 = nullptr;
329 delete manager2;
330 manager2 = nullptr;
331 delete manager3;
332 manager3 = nullptr;
333 delete manager4;
334 manager4 = nullptr;
335 }
336
337 /*
338 * @tc.name: Close 001
339 * @tc.desc: Verify that can close kv db successfully.
340 * @tc.type: FUNC
341 * @tc.require: SR000BUH3J
342 * @tc.author: luqianfu
343 */
344 HWTEST_F(DistributeddbKvCreateTest, Close001, TestSize.Level1)
345 {
346 KvStoreDelegate *result = nullptr;
347 KvStoreDelegateManager *manager = nullptr;
348
349 /**
350 * @tc.steps: step1. create and open kv db.
351 * @tc.expected: step1. create and open kv db successfully.
352 */
353 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
354 ASSERT_TRUE(manager != nullptr && result != nullptr);
355
356 /**
357 * @tc.steps: step2. close kv db that exist.
358 * @tc.expected: step2. close kv db successfully.
359 */
360 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
361 result = nullptr;
362 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
363 delete manager;
364 manager = nullptr;
365 }
366
367 /*
368 * @tc.name: Close 002
369 * @tc.desc: Verify that can not close absent db successfully.
370 * @tc.type: FUNC
371 * @tc.require: SR000BUH3J
372 * @tc.author: luqianfu
373 */
374 HWTEST_F(DistributeddbKvCreateTest, Close002, TestSize.Level1)
375 {
376 KvStoreDelegate *result = nullptr;
377 KvStoreDelegateManager *manager = nullptr;
378
379 /**
380 * @tc.steps: step1. create and delete kv db.
381 * @tc.expected: step1. Construct that no database exist.
382 */
383 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
384 ASSERT_TRUE(manager != nullptr && result != nullptr);
385 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
386 result = nullptr;
387
388 /**
389 * @tc.steps: step2. close nonexistent db.
390 * @tc.expected: step2. close db failed.
391 */
392 EXPECT_EQ(manager->CloseKvStore(result), INVALID_ARGS);
393
394 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
395 delete manager;
396 manager = nullptr;
397 }
398
399 /*
400 * @tc.name: Delete 001
401 * @tc.desc: Verify that can delete db successfully.
402 * @tc.type: FUNC
403 * @tc.require: SR000BUH3J
404 * @tc.author: luqianfu
405 */
406 HWTEST_F(DistributeddbKvCreateTest, Delete001, TestSize.Level1)
407 {
408 KvStoreDelegate *result = nullptr;
409 KvStoreDelegateManager *manager = nullptr;
410
411 /**
412 * @tc.steps: step1. create and close kv db.
413 * @tc.expected: step1. Construct that database exist.
414 */
415 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
416 ASSERT_TRUE(manager != nullptr && result != nullptr);
417 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
418 result = nullptr;
419 /**
420 * @tc.steps: step2. delete db.
421 * @tc.expected: step2. delete db successfully.
422 */
423 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
424
425 delete manager;
426 manager = nullptr;
427 }
428
429 /*
430 * @tc.name: Delete 002
431 * @tc.desc: Verify that can not delete absent db.
432 * @tc.type: FUNC
433 * @tc.require: SR000BUH3J
434 * @tc.author: luqianfu
435 */
436 HWTEST_F(DistributeddbKvCreateTest, Delete002, TestSize.Level1)
437 {
438 KvStoreDelegate *result = nullptr;
439 KvStoreDelegateManager *manager = nullptr;
440
441 /**
442 * @tc.steps: step1. create and delete kv db.
443 * @tc.expected: step1. Construct that no database exist.
444 */
445 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
446 ASSERT_TRUE(manager != nullptr && result != nullptr);
447 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
448 result = nullptr;
449 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
450
451 /**
452 * @tc.steps: step2. delete absent db.
453 * @tc.expected: step2. delete absent db failed.
454 */
455 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == NOT_FOUND);
456
457 delete manager;
458 manager = nullptr;
459 }
460
461 /*
462 * @tc.name: Config 001
463 * @tc.desc: Verify that can set global config of db.
464 * @tc.type: FUNC
465 * @tc.require: SR000BUH3J
466 * @tc.author: luqianfu
467 */
468 HWTEST_F(DistributeddbKvCreateTest, Config001, TestSize.Level1)
469 {
470 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
471 ASSERT_NE(manager, nullptr);
472 /**
473 * @tc.steps: step1. set rightly exist global config of db.
474 * @tc.expected: step1. set successfully.
475 */
476 SetDir(DIRECTOR);
477 KvStoreConfig config;
478 config.dataDir = DIRECTOR;
479 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::OK);
480 /**
481 * @tc.steps: step2. set exceptionally not exist global config of db.
482 * @tc.expected: step2. set failed.
483 */
484 config.dataDir = "/dataDED/";
485 EXPECT_EQ(manager->SetKvStoreConfig(config), INVALID_ARGS);
486 delete manager;
487 manager = nullptr;
488 }
489
490 /*
491 * @tc.name: Config 002
492 * @tc.desc: Verify that can get the set storeid and check it.
493 * @tc.type: FUNC
494 * @tc.require: SR000BUH3J
495 * @tc.author: luqianfu
496 */
497 HWTEST_F(DistributeddbKvCreateTest, Config002, TestSize.Level1)
498 {
499 KvStoreDelegate *result = nullptr;
500 KvStoreDelegateManager *manager = nullptr;
501 /**
502 * @tc.steps: step1. create kv db with STORE_ID_1.
503 * @tc.expected: step1. create db successfully.
504 */
505 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
506 ASSERT_TRUE(manager != nullptr && result != nullptr);
507
508 /**
509 * @tc.steps: step2. get the set storeid of db.
510 * @tc.expected: step2. get the set storeid successfully and it equals STORE_ID_1.
511 */
512 string storeid = result->GetStoreId();
513 ASSERT_STREQ(STORE_ID_1.c_str(), storeid.c_str());
514
515 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
516 result = nullptr;
517 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
518 delete manager;
519 manager = nullptr;
520 }
521
522 /*
523 * @tc.name: Performance 001
524 * @tc.desc: calculate the time of creating db.
525 * @tc.type: Performance
526 * @tc.require: SR000BUH3J
527 * @tc.author: luqianfu
528 */
529 HWTEST_F(DistributeddbKvCreateTest, Performance001, TestSize.Level2)
530 {
531 std::chrono::steady_clock::time_point tick, tock;
532 /**
533 * @tc.steps: step1. get the time1 and then create kv db.
534 * @tc.expected: step1. create kv db successfully.
535 */
536 SetDir(DIRECTOR);
537 DelegateKvMgrCallback delegateKvMgrCallback;
538 function<void(DBStatus, KvStoreDelegate*)> function
539 = bind(&DelegateKvMgrCallback::Callback, &delegateKvMgrCallback, std::placeholders::_1, std::placeholders::_2);
540
541 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
542 ASSERT_NE(manager, nullptr);
543 EXPECT_EQ(manager->SetKvStoreConfig(KV_CONFIG), DBStatus::OK);
544 KvStoreDelegate::Option option = DistributedTestTools::TransferKvOptionType(g_kvOption);
545
546 tick = std::chrono::steady_clock::now();
547 manager->GetKvStore(STORE_ID_1, option, function);
548 tock = std::chrono::steady_clock::now();
549
550 KvStoreDelegate* delegate = const_cast<KvStoreDelegate*>(delegateKvMgrCallback.GetKvStore());
551 EXPECT_NE(delegate, nullptr);
552 /**
553 * @tc.steps: step2. get the time2 after creating kv db.
554 * @tc.expected: step2. time2-time1<100s.
555 */
556 std::chrono::duration<uint64_t, std::milli> dur;
557 dur = std::chrono::duration_cast<std::chrono::milliseconds>(tock - tick);
558 double duration = static_cast<double>(dur.count());
559 MST_LOG("the time used for create DB is %f ms", duration);
560
561 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
562 delegate = nullptr;
563 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
564 delete manager;
565 manager = nullptr;
566 }
567
568 /*
569 * @tc.name: Performance 002
570 * @tc.desc: check the system or cpu storage and power when creating db.
571 * @tc.type: Performance
572 * @tc.require: SR000BUH3J
573 * @tc.author: luqianfu
574 */
575 HWTEST_F(DistributeddbKvCreateTest, Performance002, TestSize.Level2)
576 {
577 /**
578 * @tc.steps: step1. get the system info before creating db.
579 * @tc.expected: step1. the system or cpu storage and power is normal.
580 */
581 DistributedTestSysInfo si;
582 MST_LOG("System info before opening db");
583 si.GetSysMemOccpy(FIRST);
584 si.GetSysCpuUsage(FIRST, DEFAULT_INTEVAL);
585 si.GetSysCurrentPower(FIRST, DEFAULT_COUNT, DEFAULT_INTEVAL);
586
587 /**
588 * @tc.steps: step2. construct the params for interface and create db
589 * @tc.expected: step2. create db successfully.
590 */
591 KvStoreDelegate *result = nullptr;
592 KvStoreDelegateManager *manager = nullptr;
593 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
594 ASSERT_TRUE(manager != nullptr && result != nullptr);
595
596 /**
597 * @tc.steps: step3. get the system info after creating db.
598 * @tc.expected: step3. the system or cpu storage and power is normal.
599 */
600 MST_LOG("System info after opening db");
601 si.GetSysMemOccpy(SECOND);
602 si.GetSysCpuUsage(SECOND, DEFAULT_INTEVAL);
603 si.GetSysCurrentPower(SECOND, DEFAULT_COUNT, DEFAULT_INTEVAL);
604
605 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
606 result = nullptr;
607 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == DBStatus::OK);
608 delete manager;
609 manager = nullptr;
610 }
611
612 /*
613 * @tc.name: Performance 003
614 * @tc.desc: calculate the time of reopening db.
615 * @tc.type: Performance
616 * @tc.require: SR000BUH3J
617 * @tc.author: luqianfu
618 */
619 HWTEST_F(DistributeddbKvCreateTest, Performance003, TestSize.Level2)
620 {
621 KvStoreDelegate *delegate = nullptr;
622 KvStoreDelegateManager *manager = nullptr;
623 /**
624 * @tc.steps: step1. create and close kv db.
625 * @tc.expected: step1. Construct that database exist.
626 */
627 KvOption option = g_kvOption;
628 option.localOnly = IS_LOCAL_ONLY;
629 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
630 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
631 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
632 delegate = nullptr;
633
634 DelegateKvMgrCallback delegateKvMgrCallback;
635 function<void(DBStatus, KvStoreDelegate*)> function
636 = bind(&DelegateKvMgrCallback::Callback, &delegateKvMgrCallback, std::placeholders::_1, std::placeholders::_2);
637 std::chrono::steady_clock::time_point tick, tock;
638 option.createIfNecessary = IS_NOT_NEED_CREATE;
639 /**
640 * @tc.steps: step2. get the tick and tock value of the system before and after reopen the db.
641 * @tc.expected: step2. the system time of tick and tock obtained successfully.
642 */
643 KvStoreDelegate::Option optionUsedForGetStore = DistributedTestTools::TransferKvOptionType(option);
644 tick = std::chrono::steady_clock::now();
645 manager->GetKvStore(STORE_ID_1, optionUsedForGetStore, function);
646 tock = std::chrono::steady_clock::now();
647
648 /**
649 * @tc.steps: step3. tock - tick and translate it to milliseconds.
650 * @tc.expected: step3. tock - tick < 150ms.
651 */
652 std::chrono::duration<uint64_t, std::milli> dur;
653 dur = std::chrono::duration_cast<std::chrono::milliseconds>(tock - tick);
654 double duration = static_cast<double>(dur.count());
655 MST_LOG("the time used for reopen DB is %f ms", duration);
656
657 delegate = const_cast<KvStoreDelegate*>(delegateKvMgrCallback.GetKvStore());
658 EXPECT_NE(delegate, nullptr);
659
660 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
661 delegate = nullptr;
662 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == DBStatus::OK);
663 delete manager;
664 manager = nullptr;
665 }
666
667 /*
668 * @tc.name: Performance 004
669 * @tc.desc: check the system or cpu storage and power when reopening db.
670 * @tc.type: System overhead
671 * @tc.require: SR000BUH3J
672 * @tc.author: luqianfu
673 */
674 HWTEST_F(DistributeddbKvCreateTest, Performance004, TestSize.Level3)
675 {
676 KvStoreDelegate *delegate = nullptr;
677 KvStoreDelegateManager *manager = nullptr;
678 /**
679 * @tc.steps: step1. create and close kv db.
680 * @tc.expected: step1. Construct that database exist.
681 */
682 KvOption option04 = g_kvOption;
683 option04.localOnly = IS_LOCAL_ONLY;
684 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option04);
685 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
686 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
687 delegate = nullptr;
688 delete manager;
689 manager = nullptr;
690
691 /**
692 * @tc.steps: step2. get the system info before reopening db and then reopen db.
693 * @tc.expected: step2. the system or cpu storage and power is normal and reopen db successful .
694 */
695 MST_LOG("System info before reopening db");
696 DistributedTestSysInfo sysInfo04;
697 sysInfo04.GetSysMemOccpy(FIRST);
698 sysInfo04.GetSysCpuUsage(FIRST, DEFAULT_INTEVAL);
699 sysInfo04.GetSysCurrentPower(FIRST, DEFAULT_COUNT, DEFAULT_INTEVAL);
700 option04.createIfNecessary = IS_NOT_NEED_CREATE;
701 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option04);
702 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
703
704 /**
705 * @tc.steps: step3. get the system info after reopening db.
706 * @tc.expected: step3. the system or cpu storage and power is normal and reopen db successful.
707 */
708 MST_LOG("System info after reopening db");
709 sysInfo04.GetSysMemOccpy(SECOND);
710 sysInfo04.GetSysCpuUsage(SECOND, DEFAULT_INTEVAL);
711 sysInfo04.GetSysCurrentPower(SECOND, DEFAULT_COUNT, DEFAULT_INTEVAL);
712
713 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
714 delegate = nullptr;
715 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == DBStatus::OK);
716 delete manager;
717 manager = nullptr;
718 }
719
720 /*
721 * @tc.name: Performance 005
722 * @tc.desc: check the system storage when reopening by reopening db.
723 * @tc.type: System overhead
724 * @tc.require: SR000BUH3J
725 * @tc.author: luqianfu
726 */
727 HWTEST_F(DistributeddbKvCreateTest, Performance005, TestSize.Level3)
728 {
729 KvStoreDelegate *delegate = nullptr;
730 KvStoreDelegateManager *manager = nullptr;
731 /**
732 * @tc.steps: step1. create and close kv db.
733 * @tc.expected: step1. Construct that database exist.
734 */
735 KvOption option05 = g_kvOption;
736 option05.localOnly = IS_LOCAL_ONLY;
737 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option05);
738 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
739 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
740 delegate = nullptr;
741 delete manager;
742 manager = nullptr;
743
744 /**
745 * @tc.steps: step2. reopen db.
746 * @tc.expected: step2. reopen db successful.
747 */
748 option05.createIfNecessary = IS_NOT_NEED_CREATE;
749 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option05);
750 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
751
752 /**
753 * @tc.steps: step3. get the system info before close db.
754 * @tc.expected: step3. reopen db successful.
755 */
756 MST_LOG("System info before close db again and again");
757 DistributedTestSysInfo sysInfo05;
758 sysInfo05.GetSysMemOccpy(FIRST);
759 sysInfo05.GetSysCpuUsage(FIRST, DEFAULT_INTEVAL);
760 sysInfo05.GetSysCurrentPower(FIRST, DEFAULT_COUNT, DEFAULT_INTEVAL);
761 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
762 delegate = nullptr;
763 delete manager;
764 manager = nullptr;
765
766 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option05);
767 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
768
769 /**
770 * @tc.steps: step4. get the system info after reopening db.
771 * @tc.expected: step4. the system has no memory leak.
772 */
773 MST_LOG("System info after opening db again and again");
774 sysInfo05.GetSysMemOccpy(SECOND);
775 sysInfo05.GetSysCpuUsage(SECOND, DEFAULT_INTEVAL);
776 sysInfo05.GetSysCurrentPower(SECOND, DEFAULT_COUNT, DEFAULT_INTEVAL);
777
778 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
779 delegate = nullptr;
780 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == DBStatus::OK);
781 delete manager;
782 manager = nullptr;
783 }
784
785 /*
786 * @tc.name: Performance 006
787 * @tc.desc: check the handler using when reopening by reopening db.
788 * @tc.type: System overhead
789 * @tc.require: SR000BUH3J
790 * @tc.author: luqianfu
791 */
792 HWTEST_F(DistributeddbKvCreateTest, Performance006, TestSize.Level2)
793 {
794 KvStoreDelegate *delegate = nullptr;
795 KvStoreDelegateManager *manager = nullptr;
796 /**
797 * @tc.steps: step1. create and close kv db.
798 * @tc.expected: step1. Construct that database exist.
799 */
800 KvOption option06 = g_kvOption;
801 option06.localOnly = IS_LOCAL_ONLY;
802 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option06);
803 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
804 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
805 delegate = nullptr;
806 delete manager;
807 manager = nullptr;
808 /**
809 * @tc.steps: step2. reopen by reopen db three times.
810 * @tc.expected: step2. reopen db successful and the system has no handle overflow.
811 */
812 option06.createIfNecessary = IS_NOT_NEED_CREATE;
813 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option06);
814 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
815 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
816 delegate = nullptr;
817 delete manager;
818 manager = nullptr;
819
820 option06.createIfNecessary = IS_NEED_CREATE;
821 option06.localOnly = IS_NOT_LOCAL_ONLY;
822 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option06);
823 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
824 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
825 delegate = nullptr;
826 delete manager;
827 manager = nullptr;
828
829 delegate = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option06);
830 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
831 EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
832 delegate = nullptr;
833
834 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == DBStatus::OK);
835 delete manager;
836 manager = nullptr;
837 }
838
839 /*
840 * @tc.name: PathException 001
841 * @tc.desc: check it that can't create kv db without setting path with manager->SetKvStoreConfig(KV_CONFIG) interface.
842 * @tc.type: EXCEPTION
843 * @tc.require: SR000BUH3J
844 * @tc.author: luqianfu
845 */
846 HWTEST_F(DistributeddbKvCreateTest, PathException001, TestSize.Level2)
847 {
848 KvStoreDelegateManager *manager = nullptr;
849 KvStoreDelegate *delegate = nullptr;
850
851 SetDir(DIRECTOR);
852 DelegateKvMgrCallback delegateKvMgrCallback;
853 function<void(DBStatus, KvStoreDelegate*)> function
854 = bind(&DelegateKvMgrCallback::Callback, &delegateKvMgrCallback, std::placeholders::_1, std::placeholders::_2);
855
856 manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
857 ASSERT_NE(manager, nullptr);
858 /**
859 * @tc.steps: step1. create kv db without setting path with manager->SetKvStoreConfig(KV_CONFIG) interface.
860 * @tc.expected: step1. create and close db failed.
861 */
862 KvStoreDelegate::Option option = DistributedTestTools::TransferKvOptionType(g_kvOption);
863 manager->GetKvStore(STORE_ID_1, option, function);
864 EXPECT_EQ(delegateKvMgrCallback.GetStatus(), DBStatus::INVALID_ARGS);
865
866 delegate = const_cast<KvStoreDelegate*>(delegateKvMgrCallback.GetKvStore());
867 EXPECT_EQ(delegate, nullptr);
868 EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
869 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), DBStatus::INVALID_ARGS);
870
871 delete manager;
872 manager = nullptr;
873 }
874
875 /*
876 * @tc.name: PathException 002
877 * @tc.desc: create kv db after setting no existing path.
878 * @tc.type: EXCEPTION
879 * @tc.require: SR000BUH3J
880 * @tc.author: luqianfu
881 */
882 HWTEST_F(DistributeddbKvCreateTest, PathException002, TestSize.Level2)
883 {
884 /**
885 * @tc.steps: step1. create kv db when setting no existing path.
886 * @tc.expected: step1. create db failed.
887 */
888 DelegateKvMgrCallback delegateKvMgrCallback;
889 function<void(DBStatus, KvStoreDelegate*)> function
890 = bind(&DelegateKvMgrCallback::Callback, &delegateKvMgrCallback, std::placeholders::_1, std::placeholders::_2);
891
892 KvStoreConfig config;
893 config.dataDir = "/NOTFOUUD/PATH";
894
895 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
896 ASSERT_NE(manager, nullptr);
897 KvStoreDelegate::Option option = {true, true};
898
899 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::INVALID_ARGS);
900
901 manager->GetKvStore(STORE_ID_1, option, function);
902 EXPECT_EQ(delegateKvMgrCallback.GetStatus(), INVALID_ARGS);
903
904 KvStoreDelegate* delegate = const_cast<KvStoreDelegate*>(delegateKvMgrCallback.GetKvStore());
905 EXPECT_EQ(delegate, nullptr);
906 EXPECT_EQ(manager->CloseKvStore(delegate), INVALID_ARGS);
907 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), DBStatus::INVALID_ARGS);
908 delete manager;
909 manager = nullptr;
910 }
911
912 /*
913 * @tc.name: PathException 003
914 * @tc.desc: create kv db with invalid-char param.
915 * @tc.type: EXCEPTION
916 * @tc.require: SR000BUH3J
917 * @tc.author: luqianfu
918 */
919 HWTEST_F(DistributeddbKvCreateTest, PathException003, TestSize.Level2)
920 {
921 char chinese[5] = { static_cast<char>(0xD6), static_cast<char>(0xD0),
922 static_cast<char>(0xCE), static_cast<char>(0xC4) }; // not good ascii letters:<D6><D0><CE><C4>
923 std::string chStr = chinese;
924 MST_LOG("%s", chStr.c_str());
925
926 DelegateKvMgrCallback exceptionCallback;
927 function<void(DBStatus, KvStoreDelegate*)> function
928 = bind(&DelegateKvMgrCallback::Callback, &exceptionCallback, std::placeholders::_1, std::placeholders::_2);
929 KvStoreConfig config;
930 config.dataDir = "";
931 /**
932 * @tc.steps: step1. create kv db with the path is null.
933 * @tc.expected: step1. create db failed.
934 */
935 KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
936 ASSERT_NE(manager, nullptr);
937 KvStoreDelegate::Option option = {true, true};
938 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::INVALID_ARGS);
939 manager->GetKvStore(STORE_ID_1, option, function);
940 EXPECT_EQ(exceptionCallback.GetStatus(), INVALID_ARGS);
941 delete manager;
942 manager = nullptr;
943 /**
944 * @tc.steps: step1. create kv db with storeId="中文".
945 * @tc.expected: step1. create db failed.
946 */
947 SetDir(DIRECTOR);
948 config.dataDir = DIRECTOR;
949 manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
950 ASSERT_NE(manager, nullptr);
951 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::OK);
952 manager->GetKvStore(chStr, option, function);
953 EXPECT_EQ(exceptionCallback.GetStatus(), INVALID_ARGS);
954 delete manager;
955 manager = nullptr;
956
957 /**
958 * @tc.steps: step2. create kv db with appId="中文".
959 * @tc.expected: step2. create db failed.
960 */
961 manager = new (std::nothrow) KvStoreDelegateManager(chStr, USER_ID_1);
962 ASSERT_NE(manager, nullptr);
963 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::OK);
964 manager->GetKvStore(chStr, option, function);
965 EXPECT_EQ(exceptionCallback.GetStatus(), INVALID_ARGS);
966 delete manager;
967 manager = nullptr;
968 /**
969 * @tc.steps: step3. create kv db with userId="中文".
970 * @tc.expected: step3. create db failed.
971 */
972 manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, chStr);
973 ASSERT_NE(manager, nullptr);
974 EXPECT_EQ(manager->SetKvStoreConfig(config), DBStatus::OK);
975 manager->GetKvStore(chStr, option, function);
976 EXPECT_EQ(exceptionCallback.GetStatus(), INVALID_ARGS);
977 delete manager;
978 manager = nullptr;
979 }
980
981 /*
982 * @tc.name: PathException 004
983 * @tc.desc: create kv db with too long param.
984 * @tc.type: EXCEPTION
985 * @tc.require: SR000BUH3J
986 * @tc.author: luqianfu
987 */
988 HWTEST_F(DistributeddbKvCreateTest, PathException004, TestSize.Level2)
989 {
990 std::string storeTooLongID, appTooLongID, userTooLongID;
991 storeTooLongID.append(TWO_M_LONG_STRING, 'a');
992 appTooLongID.append(TWO_M_LONG_STRING, 'b');
993 userTooLongID.append(TWO_M_LONG_STRING, 'c');
994
995 KvStoreDelegateManager *manager1 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
996 ASSERT_NE(manager1, nullptr);
997 KvStoreDelegateManager *manager2 = new (std::nothrow) KvStoreDelegateManager(appTooLongID, USER_ID_1);
998 ASSERT_NE(manager2, nullptr);
999 KvStoreDelegateManager *manager3 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, userTooLongID);
1000 ASSERT_NE(manager3, nullptr);
1001 KvStoreDelegate *delegate1 = nullptr;
1002 KvStoreDelegate *delegate2 = nullptr;
1003 KvStoreDelegate *delegate3 = nullptr;
1004 /**
1005 * @tc.steps: step1. create kv db with storeId=storeTooLongID.
1006 * @tc.expected: step1. create db failed.
1007 */
1008 DBStatus status1 = DistributedTestTools::GetDelegateNotGood(manager1, delegate1, storeTooLongID, g_kvOption);
1009 EXPECT_EQ(delegate1, nullptr);
1010 EXPECT_EQ(manager1->CloseKvStore(delegate1), INVALID_ARGS);
1011 /**
1012 * @tc.steps: step2. create kv db with appId=appTooLongID.
1013 * @tc.expected: step2. create db failed.
1014 */
1015 DBStatus status2 = DistributedTestTools::GetDelegateNotGood(manager2, delegate2, USER_ID_1, g_kvOption);
1016 EXPECT_EQ(delegate2, nullptr);
1017 EXPECT_EQ(manager2->CloseKvStore(delegate2), INVALID_ARGS);
1018 /**
1019 * @tc.steps: step3. create kv db with userId=userTooLongID.
1020 * @tc.expected: step3. create db failed.
1021 */
1022 DBStatus status3 = DistributedTestTools::GetDelegateNotGood(manager3, delegate3, USER_ID_1, g_kvOption);
1023 EXPECT_EQ(delegate3, nullptr);
1024 EXPECT_EQ(manager3->CloseKvStore(delegate3), INVALID_ARGS);
1025
1026 ASSERT_EQ(status1, DBStatus::INVALID_ARGS);
1027 ASSERT_EQ(status2, DBStatus::INVALID_ARGS);
1028 ASSERT_EQ(status3, DBStatus::INVALID_ARGS);
1029
1030 delete manager1;
1031 manager1 = nullptr;
1032 delete manager2;
1033 manager2 = nullptr;
1034 delete manager3;
1035 manager3 = nullptr;
1036 }
1037
1038 /*
1039 * @tc.name: PathException 005
1040 * @tc.desc: verify that the same DB can be reopen many times.
1041 * @tc.type: Stability
1042 * @tc.require: SR000BUH3J
1043 * @tc.author: luqianfu
1044 */
1045 HWTEST_F(DistributeddbKvCreateTest, PathException005, TestSize.Level2)
1046 {
1047 KvStoreDelegate *result = nullptr;
1048 KvStoreDelegateManager *manager = nullptr;
1049 /**
1050 * @tc.steps: step1. create and close kv db.
1051 * @tc.expected: step1. Construct that database exist.
1052 */
1053 KvOption option = g_kvOption;
1054 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1_1_2, option);
1055 ASSERT_TRUE(manager != nullptr && result != nullptr);
1056 EXPECT_EQ(manager->CloseKvStore(result), OK);
1057 result = nullptr;
1058
1059 /**
1060 * @tc.steps: step2. reopen kv db five times.
1061 * @tc.expected: step2. reopen successfully and no memory exception when closing db.
1062 */
1063 option.createIfNecessary = IS_NEED_CREATE;
1064 KvStoreDelegate::Option optionUsedForGetStore = DistributedTestTools::TransferKvOptionType(option);
1065
1066 DelegateKvMgrCallback delegateCallback;
1067 function<void(DBStatus, KvStoreDelegate*)> function
1068 = bind(&DelegateKvMgrCallback::Callback, &delegateCallback, std::placeholders::_1, std::placeholders::_2);
1069 KvStoreDelegate* delegate = nullptr;
1070 for (int operCnt = 1; operCnt <= OPER_CNT; ++operCnt) {
1071 manager->GetKvStore(STORE_ID_1, optionUsedForGetStore, function);
1072 EXPECT_EQ(delegateCallback.GetStatus(), OK);
1073 delegate = const_cast<KvStoreDelegate*>(delegateCallback.GetKvStore());
1074 EXPECT_NE(delegate, nullptr);
1075
1076 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1077 delegate = nullptr;
1078 }
1079
1080 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), DBStatus::OK);
1081 delete manager;
1082 manager = nullptr;
1083 }
1084
1085 /*
1086 * @tc.name: PathException 006
1087 * @tc.desc: verify that one delegate can be closed only one time.
1088 * @tc.type: EXCEPTION
1089 * @tc.require: SR000BUH3J
1090 * @tc.author: luqianfu
1091 */
1092 HWTEST_F(DistributeddbKvCreateTest, PathException006, TestSize.Level2)
1093 {
1094 KvStoreDelegate *result = nullptr;
1095 KvStoreDelegateManager *manager = nullptr;
1096 /**
1097 * @tc.steps: step1. create and close kv db.
1098 * @tc.expected: step1. Construct that database exist.
1099 */
1100 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1_1_2, g_kvOption);
1101 ASSERT_TRUE(manager != nullptr && result != nullptr);
1102 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
1103 result = nullptr;
1104
1105 /**
1106 * @tc.steps: step2. create and close kv db more four times.
1107 * @tc.expected: step2. close successfully and no memory leak.
1108 */
1109 for (int operCnt = 1; operCnt <= OPER_CNT; ++operCnt) {
1110 EXPECT_EQ(manager->CloseKvStore(result), INVALID_ARGS);
1111 }
1112
1113 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1114 delete manager;
1115 manager = nullptr;
1116 }
1117
1118 /*
1119 * @tc.name: PathException 007
1120 * @tc.desc: open deleted kv db.
1121 * @tc.type: EXCEPTION
1122 * @tc.require: SR000BUH3J
1123 * @tc.author: luqianfu
1124 */
1125 HWTEST_F(DistributeddbKvCreateTest, PathException007, TestSize.Level2)
1126 {
1127 KvStoreDelegateManager *manager1 = nullptr;
1128 KvStoreDelegateManager *manager2 = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_2);
1129 ASSERT_NE(manager2, nullptr);
1130 KvStoreDelegate *delegate1 = nullptr;
1131 KvStoreDelegate *delegate2 = nullptr;
1132 /**
1133 * @tc.steps: step1. create and close kv db.
1134 * @tc.expected: step1. Construct that database exist.
1135 */
1136 delegate1 = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter1_1_2, g_kvOption);
1137 ASSERT_TRUE(manager1 != nullptr && delegate1 != nullptr);
1138 EXPECT_TRUE(manager1->CloseKvStore(delegate1) == OK);
1139 delegate1 = nullptr;
1140
1141 /**
1142 * @tc.steps: step2. delete the DB.
1143 * @tc.expected: step2. delete successfully.
1144 */
1145 EXPECT_EQ(manager1->DeleteKvStore(STORE_ID_1), OK);
1146 delete manager1;
1147 manager1 = nullptr;
1148 /**
1149 * @tc.steps: step3. open the deleted DB with the mode createIfNecessary = IS_NOT_NEED_CREATE.
1150 * @tc.expected: step3. open failed and return error.
1151 */
1152 KvOption option = g_kvOption;
1153 option.createIfNecessary = IS_NOT_NEED_CREATE;
1154 option.localOnly = IS_LOCAL_ONLY;
1155 DBStatus status = DistributedTestTools::GetDelegateNotGood(manager2, delegate2, STORE_ID_1, option);
1156 EXPECT_TRUE(delegate2 == nullptr);
1157 EXPECT_EQ(manager2->CloseKvStore(delegate2), INVALID_ARGS);
1158 ASSERT_NE(status, OK);
1159
1160 delete manager2;
1161 manager2 = nullptr;
1162 }
1163
1164 /*
1165 * @tc.name: ConflictConfig 001
1166 * @tc.desc: check SetConflictResolutionPolicy interface with AUTO_LAST_WIN mode
1167 * @tc.type: LONGTIME TEST
1168 * @tc.require: SR000CCPOI
1169 * @tc.author: luqianfu
1170 */
1171 HWTEST_F(DistributeddbKvCreateTest, ConflictConfig001, TestSize.Level1)
1172 {
1173 KvStoreDelegate *conflictDelegate = nullptr;
1174 KvStoreDelegateManager *manager = nullptr;
1175 conflictDelegate = DistributedTestTools::GetDelegateSuccess(manager,
1176 g_kvdbParameter1_1_2, g_kvOption);
1177 ASSERT_TRUE(manager != nullptr && conflictDelegate != nullptr);
1178
1179 /**
1180 * @tc.steps: step1. check SetConflictResolutionPolicy interface with AUTO_LAST_WIN mode.
1181 * @tc.expected: step1. return ok.
1182 */
1183 EXPECT_EQ(conflictDelegate->SetConflictResolutionPolicy(AUTO_LAST_WIN, nullptr), OK);
1184
1185 EXPECT_TRUE(manager->CloseKvStore(conflictDelegate) == OK);
1186 conflictDelegate = nullptr;
1187 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
1188 delete manager;
1189 manager = nullptr;
1190 }
1191
1192 /*
1193 * @tc.name: ConflictConfig 002
1194 * @tc.desc: check SetConflictResolutionPolicy interface with CUSTOMER_RESOLUTION mode
1195 * @tc.type: LONGTIME TEST
1196 * @tc.require: SR000CCPOI
1197 * @tc.author: luqianfu
1198 */
1199 HWTEST_F(DistributeddbKvCreateTest, ConflictConfig002, TestSize.Level1)
1200 {
1201 KvStoreDelegate *conflictDelegate = nullptr;
1202 KvStoreDelegateManager *manager = nullptr;
1203 conflictDelegate = DistributedTestTools::GetDelegateSuccess(manager,
1204 g_kvdbParameter1_1_2, g_kvOption);
1205 ASSERT_TRUE(manager != nullptr && conflictDelegate != nullptr);
1206
1207 /**
1208 * @tc.steps: step1. check SetConflictResolutionPolicy interface with CUSTOMER_RESOLUTION mode.
1209 * @tc.expected: step1. return error.
1210 */
1211 EXPECT_NE(conflictDelegate->SetConflictResolutionPolicy(CUSTOMER_RESOLUTION, nullptr), OK);
1212
1213 EXPECT_TRUE(manager->CloseKvStore(conflictDelegate) == OK);
1214 conflictDelegate = nullptr;
1215 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
1216 delete manager;
1217 manager = nullptr;
1218 }
1219
1220 /*
1221 * @tc.name: ConflictConfig 003
1222 * @tc.desc: check SetConflictResolutionPolicy interface with invalid mode
1223 * @tc.type: LONGTIME TEST
1224 * @tc.require: SR000CCPOI
1225 * @tc.author: luqianfu
1226 */
1227 HWTEST_F(DistributeddbKvCreateTest, ConflictConfig003, TestSize.Level1)
1228 {
1229 KvStoreDelegate *conflictDelegate = nullptr;
1230 KvStoreDelegateManager *manager = nullptr;
1231 conflictDelegate = DistributedTestTools::GetDelegateSuccess(manager,
1232 g_kvdbParameter1_1_2, g_kvOption);
1233 ASSERT_TRUE(manager != nullptr && conflictDelegate != nullptr);
1234
1235 /**
1236 * @tc.steps: step1. check SetConflictResolutionPolicy interface with invalid mode.
1237 * @tc.expected: step1. return ok.
1238 */
1239 EXPECT_EQ(conflictDelegate->SetConflictResolutionPolicy(static_cast<ResolutionPolicyType>(2), nullptr), DB_ERROR);
1240 EXPECT_EQ(conflictDelegate->SetConflictResolutionPolicy(static_cast<ResolutionPolicyType>(-1), nullptr), DB_ERROR);
1241
1242 EXPECT_TRUE(manager->CloseKvStore(conflictDelegate) == OK);
1243 conflictDelegate = nullptr;
1244 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
1245 delete manager;
1246 manager = nullptr;
1247 }
1248
1249 /*
1250 * @tc.name: OptionParam 001
1251 * @tc.desc: verify that will check the option parameter when create encrypted DB.
1252 * @tc.type: FUNC
1253 * @tc.require: SR000CQDT4
1254 * @tc.author: fengxiaoyun
1255 */
1256 HWTEST_F(DistributeddbKvCreateTest, OptionParam001, TestSize.Level1)
1257 {
1258 vector<KvStoreDelegateManager *> manager = {nullptr, nullptr, nullptr};
1259 vector<KvStoreDelegate*> result = {nullptr, nullptr, nullptr, nullptr, nullptr};
1260 KvOption option;
1261 vector<uint8_t> password;
1262
1263 /**
1264 * @tc.steps: step1. isEncryptedDb=true, passwd=NULL, cipher=DEFAULT when create db.
1265 * @tc.expected: step1. create failed and return INVALID_ARGS.
1266 */
1267 option.isEncryptedDb = true;
1268 option.cipher = CipherType::DEFAULT;
1269 option.passwd = NULL_PASSWD_VECTOR;
1270 DBStatus status;
1271 result[INDEX_ZEROTH] = DistributedTestTools::GetDelegateStatus(manager[INDEX_ZEROTH],
1272 status, g_kvdbParameter1, option);
1273 EXPECT_TRUE(manager[INDEX_ZEROTH] == nullptr && result[INDEX_ZEROTH] == nullptr);
1274 EXPECT_TRUE(status == INVALID_ARGS);
1275
1276 /**
1277 * @tc.steps: step2. isEncryptedDb=true, passwd=a......(100B) when create db.
1278 * @tc.expected: step2. create successfully and return OK.
1279 */
1280 password.assign(VALUE_ONE_HUNDRED_BYTE, 'a');
1281 option.passwd = password;
1282 result[INDEX_FIRST] = DistributedTestTools::GetDelegateSuccess(manager[INDEX_ZEROTH], g_kvdbParameter1, option);
1283 ASSERT_TRUE(manager[INDEX_ZEROTH] != nullptr && result[INDEX_FIRST] != nullptr);
1284 EXPECT_EQ(manager[INDEX_ZEROTH]->CloseKvStore(result[INDEX_FIRST]), OK);
1285 result[INDEX_FIRST] = nullptr;
1286
1287 /**
1288 * @tc.steps: step3. isEncryptedDb=true, passwd=a......(128B), cipher=AES_256_GCM when create db.
1289 * @tc.expected: step3. create successfully and return OK.
1290 */
1291 password.clear();
1292 password.assign(BATCH_RECORDS, 'a');
1293 option.cipher = CipherType::AES_256_GCM;
1294 option.passwd = password;
1295 result[INDEX_SECOND] = DistributedTestTools::GetDelegateSuccess(manager[INDEX_FIRST], g_kvdbParameter2, option);
1296 ASSERT_TRUE(manager[INDEX_FIRST] != nullptr && result[INDEX_SECOND] != nullptr);
1297 EXPECT_EQ(manager[INDEX_FIRST]->CloseKvStore(result[INDEX_SECOND]), OK);
1298 result[INDEX_SECOND] = nullptr;
1299
1300 /**
1301 * @tc.steps: step4. isEncryptedDb=true, passwd=a......(129B), cipher=DEFAULT when create db.
1302 * @tc.expected: step4. create failed and return INVALID_ARGS.
1303 */
1304 password.clear();
1305 password.assign(PASSWD_BYTE, 'a');
1306 option.cipher = CipherType::DEFAULT;
1307 option.passwd = password;
1308 result[INDEX_THIRD] = DistributedTestTools::GetDelegateStatus(manager[INDEX_SECOND],
1309 status, g_kvdbParameter3, option);
1310 EXPECT_TRUE(manager[INDEX_SECOND] == nullptr && result[INDEX_THIRD] == nullptr);
1311 EXPECT_TRUE(status == INVALID_ARGS);
1312
1313 /**
1314 * @tc.steps: step5. isEncryptedDb=false, passwd=a......(129B), cipher=DEFAULT when create db.
1315 * @tc.expected: step5. create successfully and return OK.
1316 */
1317 option.isEncryptedDb = false;
1318 result[INDEX_FORTH] = DistributedTestTools::GetDelegateSuccess(manager[INDEX_SECOND], g_kvdbParameter3, option);
1319 ASSERT_TRUE(manager[INDEX_SECOND] != nullptr && result[INDEX_FORTH] != nullptr);
1320 EXPECT_EQ(manager[INDEX_SECOND]->CloseKvStore(result[INDEX_FORTH]), OK);
1321 result[INDEX_FORTH] = nullptr;
1322
1323 EXPECT_EQ(manager[INDEX_ZEROTH]->DeleteKvStore(STORE_ID_1), OK);
1324 EXPECT_EQ(manager[INDEX_FIRST]->DeleteKvStore(STORE_ID_2), OK);
1325 EXPECT_EQ(manager[INDEX_SECOND]->DeleteKvStore(STORE_ID_3), OK);
1326 for (auto &item : manager) {
1327 if (item != nullptr) {
1328 delete item;
1329 }
1330 }
1331 }
1332
1333 /*
1334 * @tc.name: OptionParam 002
1335 * @tc.desc: verify that isEncryptedDb and passwd are consistent with the creation time can open an existing DB.
1336 * @tc.type: FUNC
1337 * @tc.require: SR000CQDT4
1338 * @tc.author: fengxiaoyun
1339 */
1340 HWTEST_F(DistributeddbKvCreateTest, OptionParam002, TestSize.Level1)
1341 {
1342 vector<KvStoreDelegateManager *> manager = {nullptr, nullptr, nullptr, nullptr};
1343 KvStoreDelegate *result = nullptr;
1344 KvOption option;
1345
1346 /**
1347 * @tc.steps: step1. isEncryptedDb=true, passwd=p1, cipher=DEFAULT when create db1.
1348 * @tc.expected: step1. create successfully and return OK.
1349 */
1350 option.isEncryptedDb = true;
1351 option.passwd = PASSWD_VECTOR_1;
1352 result = DistributedTestTools::GetDelegateSuccess(manager[INDEX_ZEROTH], g_kvdbParameter1, option);
1353 ASSERT_TRUE(manager[INDEX_ZEROTH] != nullptr && result != nullptr);
1354 EXPECT_TRUE(manager[INDEX_ZEROTH]->CloseKvStore(result) == OK);
1355 result = nullptr;
1356 /**
1357 * @tc.steps: step2. isEncryptedDb=false, passwd=p1 when open db1.
1358 * @tc.expected: step2. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1359 */
1360 option.isEncryptedDb = false;
1361 DBStatus status;
1362 result = DistributedTestTools::GetDelegateStatus(manager[INDEX_FIRST], status, g_kvdbParameter1, option);
1363 EXPECT_TRUE(manager[INDEX_FIRST] == nullptr && result == nullptr);
1364 EXPECT_TRUE(status == INVALID_PASSWD_OR_CORRUPTED_DB);
1365
1366 /**
1367 * @tc.steps: step3. isEncryptedDb=true, passwd=p2 when open db1.
1368 * @tc.expected: step3. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1369 */
1370 option.isEncryptedDb = true;
1371 option.passwd = PASSWD_VECTOR_2;
1372 result = DistributedTestTools::GetDelegateStatus(manager[INDEX_FIRST], status, g_kvdbParameter1, option);
1373 EXPECT_TRUE(manager[INDEX_FIRST] == nullptr && result == nullptr);
1374 EXPECT_TRUE(status == INVALID_PASSWD_OR_CORRUPTED_DB);
1375
1376 /**
1377 * @tc.steps: step4. isEncryptedDb=true, passwd=p1 when open db1.
1378 * @tc.expected: step4. open successfully and return OK.
1379 */
1380 option.passwd = PASSWD_VECTOR_1;
1381 result = DistributedTestTools::GetDelegateSuccess(manager[INDEX_FIRST], g_kvdbParameter1, option);
1382 ASSERT_TRUE(manager[INDEX_FIRST] != nullptr && result != nullptr);
1383 EXPECT_TRUE(manager[INDEX_FIRST]->CloseKvStore(result) == OK);
1384 result = nullptr;
1385
1386 /**
1387 * @tc.steps: step5. isEncryptedDb=false, passwd=p1, cipher=DEFAULT when create db2.
1388 * @tc.expected: step5. create successfully and return OK.
1389 */
1390 option.isEncryptedDb = false;
1391 result = DistributedTestTools::GetDelegateSuccess(manager[INDEX_SECOND], g_kvdbParameter2, option);
1392 ASSERT_TRUE(manager[INDEX_SECOND] != nullptr && result != nullptr);
1393 EXPECT_TRUE(manager[INDEX_SECOND]->CloseKvStore(result) == OK);
1394 result = nullptr;
1395
1396 /**
1397 * @tc.steps: step6. isEncryptedDb=true, passwd=p1 when open db2.
1398 * @tc.expected: step6. open failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1399 */
1400 option.isEncryptedDb = true;
1401 result = DistributedTestTools::GetDelegateStatus(manager[INDEX_THIRD], status, g_kvdbParameter2, option);
1402 EXPECT_TRUE(manager[INDEX_THIRD] == nullptr && result == nullptr);
1403 EXPECT_TRUE(status == INVALID_PASSWD_OR_CORRUPTED_DB);
1404
1405 /**
1406 * @tc.steps: step7. isEncryptedDb=false, passwd=p2 when open db2.
1407 * @tc.expected: step7. open successfully and return OK.
1408 */
1409 option.isEncryptedDb = false;
1410 option.passwd = PASSWD_VECTOR_2;
1411 result = DistributedTestTools::GetDelegateSuccess(manager[INDEX_THIRD], g_kvdbParameter2, option);
1412 ASSERT_TRUE(manager[INDEX_THIRD] != nullptr && result != nullptr);
1413 EXPECT_TRUE(manager[INDEX_THIRD]->CloseKvStore(result) == OK);
1414 result = nullptr;
1415 EXPECT_TRUE(manager[INDEX_FIRST]->DeleteKvStore(STORE_ID_1) == OK);
1416 EXPECT_TRUE(manager[INDEX_THIRD]->DeleteKvStore(STORE_ID_2) == OK);
1417 for (auto &item : manager) {
1418 if (item != nullptr) {
1419 delete item;
1420 item = nullptr;
1421 }
1422 }
1423 }
1424
ReleaseManager(KvStoreDelegateManager * & manager)1425 void ReleaseManager(KvStoreDelegateManager *&manager)
1426 {
1427 if (manager != nullptr) {
1428 delete manager;
1429 manager = nullptr;
1430 }
1431 return;
1432 }
1433 /*
1434 * @tc.name: RekeyDb 001
1435 * @tc.desc: verify that can switching a non-encrypted database to an encrypted database by Rekey.
1436 * @tc.type: FUNC
1437 * @tc.require: SR000CQDT4
1438 * @tc.author: fengxiaoyun
1439 */
1440 HWTEST_F(DistributeddbKvCreateTest, RekeyDb001, TestSize.Level1)
1441 {
1442 KvStoreDelegateManager *manager = nullptr;
1443 KvStoreDelegate *result = nullptr;
1444 KvOption option;
1445
1446 /**
1447 * @tc.steps: step1. create unencrypted db, use Rekey to update its passwd to NULL,
1448 * then close and open without passwd.
1449 * @tc.expected: step1. operate successfully and can open db again without passwd.
1450 */
1451 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1452 ASSERT_TRUE(manager != nullptr && result != nullptr);
1453 EXPECT_TRUE(result->Rekey(NULL_PASSWD) == OK);
1454 DistributedTestTools::CloseAndRelease(manager, result);
1455 option.createIfNecessary = false;
1456 option.isEncryptedDb = false;
1457 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1458 ASSERT_TRUE(manager != nullptr && result != nullptr);
1459 ASSERT_TRUE(DistributedTestTools::Put(*result, KEY_1, VALUE_1) == DBStatus::OK);
1460
1461 /**
1462 * @tc.steps: step2. use Rekey to update db's passwd to p1=a......(100B), then close
1463 * and open again without passwd.
1464 * @tc.expected: step2. Rekey is ok, open db failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1465 */
1466 vector<uint8_t> passwordVector(VALUE_ONE_HUNDRED_BYTE, 'a');
1467 CipherPassword password;
1468 EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OK);
1469 EXPECT_TRUE(result->Rekey(password) == OK);
1470 DistributedTestTools::CloseAndRelease(manager, result);
1471 DBStatus status;
1472 ASSERT_TRUE(DistributedTestTools::GetDelegateStatus(manager, status, g_kvdbParameter1, option) == nullptr);
1473 EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1474
1475 /**
1476 * @tc.steps: step3. use p1 to open db and Get(k1), GetLocal(k1).
1477 * @tc.expected: step3. open successfully and Get(k1)=v1.
1478 */
1479 option.isEncryptedDb = true;
1480 option.passwd = passwordVector;
1481 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1482 ASSERT_TRUE(manager != nullptr && result != nullptr);
1483 Value valueResult = DistributedTestTools::Get(*result, KEY_1);
1484 EXPECT_TRUE(valueResult.size() != 0);
1485 EXPECT_TRUE(DistributedTestTools::IsValueEquals(valueResult, VALUE_1));
1486
1487 /**
1488 * @tc.steps: step4. use Rekey to update db's passwd to p2=passwordVector whose size is 128B, then close
1489 * and open again with p1.
1490 * @tc.expected: step4. Rekey is ok, open db failed and return INVALID_PASSWD_OR_CORRUPTED_DB.
1491 */
1492 passwordVector.assign(BATCH_RECORDS, 'a');
1493 EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OK);
1494 EXPECT_TRUE(result->Rekey(password) == OK);
1495 DistributedTestTools::CloseAndRelease(manager, result);
1496 ASSERT_TRUE(DistributedTestTools::GetDelegateStatus(manager, status, g_kvdbParameter1, option) == nullptr);
1497 EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1498
1499 /**
1500 * @tc.steps: step5. use p2 to open db and delete(k1).
1501 * @tc.expected: step5. operate successfully.
1502 */
1503 option.passwd = passwordVector;
1504 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1505 ASSERT_TRUE(manager != nullptr && result != nullptr);
1506 ASSERT_TRUE(DistributedTestTools::Delete(*result, KEY_1) == OK);
1507
1508 /**
1509 * @tc.steps: step6. use Rekey to update db's passwd to p2=passwordVector whose size is 129B.
1510 * @tc.expected: step6. Rekey failed and return INVALID_ARGS.
1511 */
1512 passwordVector.assign(PASSWD_BYTE, 'a');
1513 EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OVERSIZE);
1514
1515 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
1516 result = nullptr;
1517 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
1518 ReleaseManager(manager);
1519 }
1520
1521 /*
1522 * @tc.name: RekeyDb 002
1523 * @tc.desc: verify that can change passwd of an encrypted database by Rekey.
1524 * @tc.type: FUNC
1525 * @tc.require: SR000CQDT4
1526 * @tc.author: fengxiaoyun
1527 */
1528 HWTEST_F(DistributeddbKvCreateTest, RekeyDb002, TestSize.Level1)
1529 {
1530 KvStoreDelegateManager *manager = nullptr;
1531 KvStoreDelegate *result = nullptr;
1532 KvOption option;
1533
1534 /**
1535 * @tc.steps: step1. create encrypted db with p1=PASSWD_VECTOR_1.
1536 * @tc.expected: step1. create successfully.
1537 */
1538 option.isEncryptedDb = true;
1539 option.passwd = PASSWD_VECTOR_1;
1540 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1541 ASSERT_TRUE(manager != nullptr && result != nullptr);
1542
1543 /**
1544 * @tc.steps: step2. use Rekey to update passwd to p1 and close db then open db again with unencrypted way.
1545 * @tc.expected: step2. the Rekey return OK, but open db failed.
1546 */
1547 EXPECT_TRUE(result->Rekey(g_passwd1) == OK);
1548 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
1549 result = nullptr;
1550 ReleaseManager(manager);
1551 option.createIfNecessary = false;
1552 option.isEncryptedDb = false;
1553 DBStatus status;
1554 result = DistributedTestTools::GetDelegateStatus(manager, status, g_kvdbParameter1, option);
1555 ASSERT_TRUE(result == nullptr);
1556 EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1557
1558 /**
1559 * @tc.steps: step3. open db with passwd=p1 and putbatch(k1,v1)(k2,v2).
1560 * @tc.expected: step3. operate successfully.
1561 */
1562 option.isEncryptedDb = true;
1563 option.passwd = PASSWD_VECTOR_1;
1564 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1565 ASSERT_TRUE(manager != nullptr && result != nullptr);
1566 vector<Entry> entries1;
1567 entries1.push_back(ENTRY_1);
1568 entries1.push_back(ENTRY_2);
1569 EXPECT_TRUE(DistributedTestTools::PutBatch(*result, entries1) == OK);
1570
1571 /**
1572 * @tc.steps: step4. use Rekey to update passwd to NULL and close db then open db with passwd=p1.
1573 * @tc.expected: step4. the Rekey return OK, but open db failed.
1574 */
1575 EXPECT_TRUE(result->Rekey(NULL_PASSWD) == OK);
1576 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
1577 result = nullptr;
1578 ReleaseManager(manager);
1579 result = DistributedTestTools::GetDelegateStatus(manager, status, g_kvdbParameter1, option);
1580 ASSERT_TRUE(result == nullptr);
1581 EXPECT_EQ(status, INVALID_PASSWD_OR_CORRUPTED_DB);
1582
1583 /**
1584 * @tc.steps: step5. open db again with unencrypted way and Get(k1), GetLocal(k2).
1585 * @tc.expected: step5. open successfully and Get(k1)=v1, GetLocal(k2)=v2.
1586 */
1587 option.isEncryptedDb = false;
1588 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1589 ASSERT_TRUE(manager != nullptr && result != nullptr);
1590 vector<Entry> valueResult = DistributedTestTools::GetEntries(*result, KEY_EMPTY);
1591 ASSERT_TRUE(valueResult.size() == entries1.size());
1592
1593 /**
1594 * @tc.steps: step6. use Rekey to update db's passwd to p2=a......(129B).
1595 * @tc.expected: step6. Reksy failed and return INVALID_ARGS.
1596 */
1597 vector<uint8_t> passwordVector(PASSWD_BYTE, 'a');
1598 CipherPassword password;
1599 EXPECT_EQ(password.SetValue(passwordVector.data(), passwordVector.size()), CipherPassword::ErrorCode::OVERSIZE);
1600
1601 EXPECT_TRUE(manager->CloseKvStore(result) == OK);
1602 result = nullptr;
1603 EXPECT_TRUE(manager->DeleteKvStore(STORE_ID_1) == OK);
1604 ReleaseManager(manager);
1605 }
1606
1607 #ifndef LOW_LEVEL_MEM_DEV
1608 /*
1609 * @tc.name: RekeyDb 003
1610 * @tc.desc: verify that do other operations during Rekey execution, the operation returns busy.
1611 * @tc.type: FUNC
1612 * @tc.require: SR000CQDT4
1613 * @tc.author: fengxiaoyun
1614 */
1615 HWTEST_F(DistributeddbKvCreateTest, RekeyDb003, TestSize.Level3)
1616 {
1617 KvStoreDelegateManager *manager = nullptr;
1618 KvStoreDelegate *result1 = nullptr;
1619 KvStoreDelegate *result2 = nullptr;
1620 KvOption option;
1621
1622 /**
1623 * @tc.steps: step1. use Rekey to update passwd to p1=PASSWD_VECTOR_1.
1624 * @tc.expected: step1. create successfully.
1625 */
1626 result1 = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1627 ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1628 std::vector<DistributedDB::Entry> entriesBatch;
1629 std::vector<DistributedDB::Key> allKeys;
1630 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, ONE_K_LONG_STRING, ONE_M_LONG_STRING);
1631 EXPECT_EQ(DistributedTestTools::PutBatch(*result1, entriesBatch), OK);
1632 bool rekeyFlag1 = false;
__anon3af778870102() 1633 thread subThread1([&result1, &rekeyFlag1]() {
1634 DBStatus status = result1->Rekey(g_passwd1);
1635 EXPECT_TRUE(status == OK || status == BUSY);
1636 rekeyFlag1 = true;
1637 g_conditionKvVar.notify_one();
1638 });
1639 subThread1.detach();
1640
1641 /**
1642 * @tc.steps: step2. Call the GetKvstore interface when Rekey.
1643 * @tc.expected: step2. the GetKvstore return BUSY.
1644 */
1645 option.createIfNecessary = false;
1646 KvStoreDelegateManager *managerRes = nullptr;
1647 DBStatus status;
1648 result2 = DistributedTestTools::GetDelegateStatus(managerRes, status, g_kvdbParameter1, option);
1649 EXPECT_TRUE(status == BUSY || status == OK);
1650 if (result2 != nullptr) {
1651 EXPECT_EQ(managerRes->CloseKvStore(result2), OK);
1652 ReleaseManager(managerRes);
1653 }
1654 std::mutex count;
1655 {
1656 std::unique_lock<std::mutex> lck(count);
__anon3af778870202null1657 g_conditionKvVar.wait(lck, [&]{return rekeyFlag1;});
1658 }
1659
1660 /**
1661 * @tc.steps: step3. put data to db when Rekey.
1662 * @tc.expected: step3. the put return BUSY.
1663 */
1664 bool rekeyFlag2 = false;
__anon3af778870302() 1665 thread subThread2([&result1, &rekeyFlag2]() {
1666 DBStatus rekeyStatus = result1->Rekey(g_passwd2);
1667 EXPECT_TRUE(rekeyStatus == OK || rekeyStatus == BUSY);
1668 rekeyFlag2 = true;
1669 g_conditionKvVar.notify_all();
1670 });
1671 subThread2.detach();
1672 status = DistributedTestTools::Put(*result1, KEY_1, VALUE_1);
1673 EXPECT_TRUE(status == BUSY || status == OK);
1674 std::unique_lock<std::mutex> lck(count);
__anon3af778870402null1675 g_conditionKvVar.wait(lck, [&]{return rekeyFlag2;});
1676 EXPECT_EQ(manager->CloseKvStore(result1), OK);
1677 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1678 ReleaseManager(manager);
1679 }
1680 #endif
1681
1682 /*
1683 * @tc.name: RekeyDb 004
1684 * @tc.desc: verify that Rekey will return busy when there are multiple instances of the same KvStore.
1685 * @tc.type: FUNC
1686 * @tc.require: SR000CQDT4
1687 * @tc.author: fengxiaoyun
1688 */
1689 HWTEST_F(DistributeddbKvCreateTest, RekeyDb004, TestSize.Level1)
1690 {
1691 KvStoreDelegateManager *manager = nullptr;
1692 KvStoreDelegate *result1 = nullptr;
1693 KvStoreDelegate *result2 = nullptr;
1694 KvOption option;
1695
1696 result1 = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1697 ASSERT_TRUE(manager != nullptr && result1 != nullptr);
1698 ReleaseManager(manager);
1699
1700 /**
1701 * @tc.steps: step1. use GetKvstore to open another instances of the same KvStore.
1702 * @tc.expected: step1. open successfully.
1703 */
1704 option.createIfNecessary = false;
1705 result2 = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1706 ASSERT_TRUE(manager != nullptr && result2 != nullptr);
1707
1708 /**
1709 * @tc.steps: step2. call Rekey.
1710 * @tc.expected: step2. Rekey returns BUSY.
1711 */
1712 EXPECT_EQ(result2->Rekey(g_passwd1), BUSY);
1713 EXPECT_EQ(manager->CloseKvStore(result1), OK);
1714 result1 = nullptr;
1715 EXPECT_EQ(manager->CloseKvStore(result2), OK);
1716 result2 = nullptr;
1717 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1718 ReleaseManager(manager);
1719 }
1720
RunDbRekeyOne()1721 void RunDbRekeyOne()
1722 {
1723 KvStoreDelegateManager *manager1 = nullptr;
1724 KvStoreDelegate *result1 = nullptr;
1725 KvOption option;
1726 option.isEncryptedDb = true;
1727 option.passwd = PASSWD_VECTOR_1;
1728 result1 = DistributedTestTools::GetDelegateSuccess(manager1, g_kvdbParameter1, option);
1729 ASSERT_TRUE(manager1 != nullptr && result1 != nullptr);
1730 EXPECT_TRUE(result1->Rekey(g_passwd2) == OK);
1731 EXPECT_TRUE(manager1->CloseKvStore(result1) == OK);
1732 result1 = nullptr;
1733 EXPECT_TRUE(manager1->DeleteKvStore(STORE_ID_1) == OK);
1734 delete manager1;
1735 manager1 = nullptr;
1736 }
1737
RunDbRekeyTwo()1738 void RunDbRekeyTwo()
1739 {
1740 KvStoreDelegateManager *manager2 = nullptr;
1741 KvStoreDelegate *result2 = nullptr;
1742 KvOption option;
1743 option.isEncryptedDb = true;
1744 option.passwd = PASSWD_VECTOR_2;
1745 result2 = DistributedTestTools::GetDelegateSuccess(manager2, g_kvdbParameter2, option);
1746 ASSERT_TRUE(manager2 != nullptr && result2 != nullptr);
1747 EXPECT_TRUE(result2->Rekey(g_passwd1) == OK);
1748 EXPECT_TRUE(manager2->CloseKvStore(result2) == OK);
1749 result2 = nullptr;
1750 EXPECT_TRUE(manager2->DeleteKvStore(STORE_ID_2) == OK);
1751 delete manager2;
1752 manager2 = nullptr;
1753 }
1754
RunDbRekeyThree()1755 void RunDbRekeyThree()
1756 {
1757 KvStoreDelegateManager *manager3 = nullptr;
1758 KvStoreDelegate *result3 = nullptr;
1759 KvOption option;
1760 option.isEncryptedDb = true;
1761 option.passwd = PASSWD_VECTOR_1;
1762 result3 = DistributedTestTools::GetDelegateSuccess(manager3, g_kvdbParameter3, option);
1763 ASSERT_TRUE(manager3 != nullptr && result3 != nullptr);
1764 EXPECT_TRUE(result3->Rekey(NULL_PASSWD) == OK);
1765 EXPECT_TRUE(manager3->CloseKvStore(result3) == OK);
1766 result3 = nullptr;
1767 EXPECT_TRUE(manager3->DeleteKvStore(STORE_ID_3) == OK);
1768 delete manager3;
1769 manager3 = nullptr;
1770 }
1771
RunDbRekeyFour()1772 void RunDbRekeyFour()
1773 {
1774 KvStoreDelegateManager *manager4 = nullptr;
1775 KvStoreDelegate *result4 = nullptr;
1776 KvOption option;
1777 result4 = DistributedTestTools::GetDelegateSuccess(manager4, g_kvdbParameter4, option);
1778 ASSERT_TRUE(manager4 != nullptr && result4 != nullptr);
1779 EXPECT_TRUE(result4->Rekey(g_passwd1) == OK);
1780 EXPECT_TRUE(manager4->CloseKvStore(result4) == OK);
1781 result4 = nullptr;
1782 EXPECT_TRUE(manager4->DeleteKvStore(STORE_ID_4) == OK);
1783 delete manager4;
1784 manager4 = nullptr;
1785 }
1786
RunDbRekeyFive()1787 void RunDbRekeyFive()
1788 {
1789 KvStoreDelegateManager *manager5 = nullptr;
1790 KvStoreDelegate *result5 = nullptr;
1791 KvOption option;
1792 result5 = DistributedTestTools::GetDelegateSuccess(manager5, g_kvdbParameter5, option);
1793 ASSERT_TRUE(manager5 != nullptr && result5 != nullptr);
1794 vector<Entry> entries1;
1795 vector<Key> allKey1;
1796 vector<Key> allKey2;
1797 GenerateRecords(BATCH_RECORDS, DEFAULT_START, allKey1, entries1, K_SEARCH_3);
1798 DBStatus status = DistributedTestTools::PutBatch(*result5, entries1);
1799 ASSERT_TRUE(status == DBStatus::OK);
1800 DBStatus statusDelete = DistributedTestTools::DeleteBatch(*result5, allKey1);
1801 ASSERT_TRUE(statusDelete == DBStatus::OK);
1802 status = DistributedTestTools::Put(*result5, KEY_1, VALUE_1);
1803 ASSERT_TRUE(status == DBStatus::OK);
1804 Value valueResult = DistributedTestTools::Get(*result5, KEY_1);
1805 EXPECT_TRUE(valueResult.size() != 0);
1806 EXPECT_TRUE(DistributedTestTools::IsValueEquals(valueResult, VALUE_1));
1807 EXPECT_TRUE(manager5->CloseKvStore(result5) == OK);
1808 result5 = nullptr;
1809 EXPECT_TRUE(manager5->DeleteKvStore(STORE_ID_5) == OK);
1810 delete manager5;
1811 manager5 = nullptr;
1812 }
1813 /*
1814 * @tc.name: RekeyDb 005
1815 * @tc.desc: verify that calling Rekey interfaces on different DBs does not affect each other..
1816 * @tc.type: FUNC
1817 * @tc.require: SR000CQDT4
1818 * @tc.author: fengxiaoyun
1819 */
1820 HWTEST_F(DistributeddbKvCreateTest, RekeyDb005, TestSize.Level1)
1821 {
1822 /**
1823 * @tc.steps: step1. create thread1 to create db1 with passwd=p1, call Rekey to update passwd to p2=PASSSWD_2.
1824 * @tc.expected: step1. operate successfully.
1825 */
1826 thread subThread1(RunDbRekeyOne);
1827
1828 /**
1829 * @tc.steps: step2. create thread2 to create db2 with passwd=p2, call Rekey to update passwd to p1=PASSSWD_1.
1830 * @tc.expected: step2. operate successfully.
1831 */
1832 thread subThread2(RunDbRekeyTwo);
1833
1834 /**
1835 * @tc.steps: step3. create thread3 to create db3 with passwd=p1, call Rekey to update passwd to NULL_PASSWD.
1836 * @tc.expected: step3. operate successfully.
1837 */
1838 thread subThread3(RunDbRekeyThree);
1839
1840 /**
1841 * @tc.steps: step4. create thread4 to create db4 without passwd, call Rekey to make db to be encrypted.
1842 * @tc.expected: step4. operate successfully.
1843 */
1844 thread subThread4(RunDbRekeyFour);
1845
1846 /**
1847 * @tc.steps: step5. create thread5 to create db5 without passwd, then CRUD data to db5.
1848 * @tc.expected: step5. operate successfully.
1849 */
1850 thread subThread5(RunDbRekeyFive);
1851 subThread1.join();
1852 subThread2.join();
1853 subThread3.join();
1854 subThread5.join();
1855 subThread4.join();
1856 }
1857
1858 /*
1859 * @tc.name: SpaceManger 001
1860 * @tc.desc: verify that can calculate the space size normally with the existing databaseID.
1861 * @tc.type: FUNC
1862 * @tc.require: SR000CQDT4
1863 * @tc.author: fengxiaoyun
1864 */
1865 #ifdef KV_CREATE
1866 HWTEST_F(DistributeddbKvCreateTest, SpaceManger001, TestSize.Level1)
1867 {
1868 KvStoreDelegateManager *manager = nullptr;
1869 KvStoreDelegate *result = nullptr;
1870 KvOption option;
1871 option.isEncryptedDb = true;
1872 option.passwd = PASSWD_VECTOR_1;
1873 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1874 ASSERT_TRUE(manager != nullptr && result != nullptr);
1875 EXPECT_EQ(manager->CloseKvStore(result), OK);
1876 result = nullptr;
1877
1878 /**
1879 * @tc.steps: step1. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1880 * @tc.expected: step1. call successfully and return dbSize1.
1881 */
1882 uint64_t dbSize1, dbSize2, dbSize3;
1883 dbSize1 = dbSize2 = dbSize3 = 0ul;
1884 EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize1), OK);
1885
1886 /**
1887 * @tc.steps: step2. put 100 (keys,values) to db that every item's size = 1K.
1888 * @tc.expected: step2. operate successfully.
1889 */
1890 option.createIfNecessary = false;
1891 ReleaseManager(manager);
1892 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, option);
1893 ASSERT_TRUE(manager != nullptr && result != nullptr);
1894 vector<Entry> entriesBatch;
1895 vector<Key> allKeys;
1896 GenerateTenThousandRecords(NB_OPERATION_NUM, DEFAULT_START, allKeys, entriesBatch);
1897 EXPECT_EQ(DistributedTestTools::PutBatch(*result, entriesBatch), OK);
1898
1899 /**
1900 * @tc.steps: step3. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1901 * @tc.expected: step3. call successfully and return dbSize2, dbSize2>dbSize1.
1902 */
1903 EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize2), OK);
1904 EXPECT_GT(dbSize2, dbSize1);
1905
1906 /**
1907 * @tc.steps: step4. delete the 100 (keys,values) that inserted in step2.
1908 * @tc.expected: step4. operate successfully.
1909 */
1910 EXPECT_EQ(DistributedTestTools::DeleteBatch(*result, allKeys), OK);
1911
1912 /**
1913 * @tc.steps: step5. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1914 * @tc.expected: step5. call successfully and return dbSize3, dbSize3>dbSize2 and dbSize3 != dbSize2.
1915 */
1916 EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize3), OK);
1917 EXPECT_GT(dbSize3, dbSize2);
1918 EXPECT_EQ(manager->CloseKvStore(result), OK);
1919 result = nullptr;
1920 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1921 ReleaseManager(manager);
1922 }
1923
1924 /*
1925 * @tc.name: MergeRepeat 001
1926 * @tc.desc: verify that delete 9 items of (keys,v) that have the same value, can query remaining data's value is v.
1927 * @tc.type: FUNC
1928 * @tc.require: SR000CQDT4
1929 * @tc.author: fengxiaoyun
1930 */
1931 HWTEST_F(DistributeddbKvCreateTest, MergeRepeat001, TestSize.Level2)
1932 {
1933 KvStoreDelegateManager *manager = nullptr;
1934 KvStoreDelegate *result = nullptr;
1935 result = DistributedTestTools::GetDelegateSuccess(manager, g_kvdbParameter1, g_kvOption);
1936 ASSERT_TRUE(manager != nullptr && result != nullptr);
1937
1938 /**
1939 * @tc.steps: step1. put 10 items of (keys,v) to db and GetKvStoreDiskSize() with storeId=store_Id_1.
1940 * @tc.expected: step1. put successfully and the GetKvStoreDiskSize() returns dbSize1.
1941 */
1942 uint64_t dbSize1, dbSize2;
1943 dbSize1 = dbSize2 = 0ul;
1944 EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize1), OK);
1945 vector<Entry> entriesBatch;
1946 vector<Key> allKeys;
1947 DistributedDB::Entry entry;
1948 int putCount = 0;
1949 entry.value.assign(TWO_M_LONG_STRING, 'v');
1950 GenerateTenThousandRecords(OPER_CNT_END, DEFAULT_START, allKeys, entriesBatch);
1951 for (vector<Entry>::iterator iter = entriesBatch.begin(); iter != entriesBatch.end(); iter++) {
1952 EXPECT_EQ(DistributedTestTools::Put(*result, iter->key, entry.value), OK);
1953 putCount++;
1954 }
1955
1956 /**
1957 * @tc.steps: step2. call the GetKvStoreDiskSize() with storeId=store_Id_1.
1958 * @tc.expected: step2. call successfully and return dbSize2, dbSize2 > dbSize1.
1959 */
1960 EXPECT_EQ(manager->GetKvStoreDiskSize(STORE_ID_1, dbSize2), OK);
1961 EXPECT_TRUE(dbSize2 > dbSize1);
1962
1963 /**
1964 * @tc.steps: step3. delete the 10 items of (keys,v) except the sixth item.
1965 * @tc.expected: step3. operate successfully.
1966 */
1967 allKeys.erase(allKeys.begin() + FIVE_SECONDS);
1968 DBStatus statusDelete = DistributedTestTools::DeleteBatch(*result, allKeys);
1969 EXPECT_EQ(statusDelete, DBStatus::OK);
1970
1971 /**
1972 * @tc.steps: step4. Get(k6).
1973 * @tc.expected: step4. Get(k6)=v.
1974 */
1975 Value valueResult = DistributedTestTools::Get(*result, KEY_SEARCH_6);
1976 EXPECT_TRUE(valueResult.size() != 0);
1977 EXPECT_TRUE(DistributedTestTools::IsValueEquals(valueResult, entry.value));
1978 EXPECT_EQ(manager->CloseKvStore(result), OK);
1979 result = nullptr;
1980 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
1981 delete manager;
1982 manager = nullptr;
1983 }
1984 #endif
1985 }
1986 #endif // OMIT_MULTI_VER
1987