• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define LOG_TAG "DistributedKvDataManagerTest"
16 #include "distributed_kv_data_manager.h"
17 #include <gtest/gtest.h>
18 #include <vector>
19 
20 #include "kvstore_death_recipient.h"
21 #include "log_print.h"
22 #include "types.h"
23 using namespace testing::ext;
24 using namespace OHOS::DistributedKv;
25 namespace OHOS::Test {
26 class DistributedKvDataManagerTest : public testing::Test {
27 public:
28     static DistributedKvDataManager manager;
29     static Options create;
30     static Options noCreate;
31 
32     static UserId userId;
33 
34     static AppId appId;
35     static StoreId storeId64;
36     static StoreId storeId65;
37     static StoreId storeIdTest;
38     static StoreId storeIdEmpty;
39 
40     static Entry entryA;
41     static Entry entryB;
42 
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45 
46     static void RemoveAllStore(DistributedKvDataManager manager);
47 
48     void SetUp();
49     void TearDown();
50     DistributedKvDataManagerTest();
51 };
52 
53 class MyDeathRecipient : public KvStoreDeathRecipient {
54 public:
MyDeathRecipient()55     MyDeathRecipient() {}
~MyDeathRecipient()56     virtual ~MyDeathRecipient() {}
OnRemoteDied()57     void OnRemoteDied() override {}
58 };
59 
60 DistributedKvDataManager DistributedKvDataManagerTest::manager;
61 Options DistributedKvDataManagerTest::create;
62 Options DistributedKvDataManagerTest::noCreate;
63 
64 UserId DistributedKvDataManagerTest::userId;
65 
66 AppId DistributedKvDataManagerTest::appId;
67 StoreId DistributedKvDataManagerTest::storeId64;
68 StoreId DistributedKvDataManagerTest::storeId65;
69 StoreId DistributedKvDataManagerTest::storeIdTest;
70 StoreId DistributedKvDataManagerTest::storeIdEmpty;
71 
72 Entry DistributedKvDataManagerTest::entryA;
73 Entry DistributedKvDataManagerTest::entryB;
74 
RemoveAllStore(DistributedKvDataManager manager)75 void DistributedKvDataManagerTest::RemoveAllStore(DistributedKvDataManager manager)
76 {
77     manager.CloseAllKvStore(appId);
78     manager.DeleteAllKvStore(appId, create.baseDir);
79 }
SetUpTestCase(void)80 void DistributedKvDataManagerTest::SetUpTestCase(void)
81 {
82     userId.userId = "account0";
83     appId.appId = "ohos.kvdatamanager.test";
84     create.createIfMissing = true;
85     create.encrypt = false;
86     create.autoSync = true;
87     create.kvStoreType = SINGLE_VERSION;
88     create.area = EL1;
89     create.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
90     mkdir(create.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
91 
92     noCreate.createIfMissing = false;
93     noCreate.encrypt = false;
94     noCreate.autoSync = true;
95     noCreate.kvStoreType = SINGLE_VERSION;
96     noCreate.area = EL1;
97     noCreate.baseDir = create.baseDir;
98 
99     storeId64.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000";
100     storeId65.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000"
101                         "a000000000b000000000c000000000d000000000e000000000f000000000g0000";
102     storeIdTest.storeId = "test";
103     storeIdEmpty.storeId = "";
104 
105     entryA.key = "a";
106     entryA.value = "valueA";
107     entryB.key = "b";
108     entryB.value = "valueB";
109     RemoveAllStore(manager);
110 }
111 
TearDownTestCase(void)112 void DistributedKvDataManagerTest::TearDownTestCase(void)
113 {
114     RemoveAllStore(manager);
115     (void)remove((create.baseDir + "/kvdb").c_str());
116     (void)remove(create.baseDir.c_str());
117 }
118 
SetUp(void)119 void DistributedKvDataManagerTest::SetUp(void)
120 {}
121 
DistributedKvDataManagerTest(void)122 DistributedKvDataManagerTest::DistributedKvDataManagerTest(void)
123 {}
124 
TearDown(void)125 void DistributedKvDataManagerTest::TearDown(void)
126 {
127     RemoveAllStore(manager);
128 }
129 
130 /**
131 * @tc.name: GetKvStore001
132 * @tc.desc: Get an exist SingleKvStore
133 * @tc.type: FUNC
134 * @tc.require: SR000CQDU0 AR000BVTDM
135 * @tc.author: liqiao
136 */
137 HWTEST_F(DistributedKvDataManagerTest, GetKvStore001, TestSize.Level1)
138 {
139     ZLOGI("GetKvStore001 begin.");
140     std::shared_ptr<SingleKvStore> notExistKvStore;
141     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
142     ASSERT_EQ(status, Status::SUCCESS);
143     EXPECT_NE(notExistKvStore, nullptr);
144 
145     std::shared_ptr<SingleKvStore> existKvStore;
146     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore);
147     ASSERT_EQ(status, Status::SUCCESS);
148     EXPECT_NE(existKvStore, nullptr);
149 }
150 
151 /**
152 * @tc.name: GetKvStore002
153 * @tc.desc: Create and get a new SingleKvStore
154 * @tc.type: FUNC
155 * @tc.require: SR000CQDU0 AR000BVTDM
156 * @tc.author: liqiao
157 */
158 HWTEST_F(DistributedKvDataManagerTest, GetKvStore002, TestSize.Level1)
159 {
160     ZLOGI("GetKvStore002 begin.");
161     std::shared_ptr<SingleKvStore> notExistKvStore;
162     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
163     ASSERT_EQ(status, Status::SUCCESS);
164     EXPECT_NE(notExistKvStore, nullptr);
165     manager.CloseKvStore(appId, storeId64);
166     manager.DeleteKvStore(appId, storeId64);
167 }
168 
169 /**
170 * @tc.name: GetKvStore003
171 * @tc.desc: Get a non-existing SingleKvStore, and the callback function should receive STORE_NOT_FOUND and
172 * get a nullptr.
173 * @tc.type: FUNC
174 * @tc.require: SR000CQDU0 AR000BVTDM
175 * @tc.author: liqiao
176 */
177 HWTEST_F(DistributedKvDataManagerTest, GetKvStore003, TestSize.Level1)
178 {
179     ZLOGI("GetKvStore003 begin.");
180     std::shared_ptr<SingleKvStore> notExistKvStore;
181     (void)manager.GetSingleKvStore(noCreate, appId, storeId64, notExistKvStore);
182     EXPECT_EQ(notExistKvStore, nullptr);
183 }
184 
185 /**
186 * @tc.name: GetKvStore004
187 * @tc.desc: Create a SingleKvStore with an empty storeId, and the callback function should receive
188 * @tc.type: FUNC
189 * @tc.require: SR000CQDU0 AR000BVTDM
190 * @tc.author: liqiao
191 */
192 HWTEST_F(DistributedKvDataManagerTest, GetKvStore004, TestSize.Level1)
193 {
194     ZLOGI("GetKvStore004 begin.");
195     std::shared_ptr<SingleKvStore> notExistKvStore;
196     Status status = manager.GetSingleKvStore(create, appId, storeIdEmpty, notExistKvStore);
197     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
198     EXPECT_EQ(notExistKvStore, nullptr);
199 }
200 
201 /**
202 * @tc.name: GetKvStore005
203 * @tc.desc: Get a SingleKvStore with an empty storeId, and the callback function should receive INVALID_ARGUMENT
204 * @tc.type: FUNC
205 * @tc.require: SR000CQDU0 AR000BVTDM
206 * @tc.author: liqiao
207 */
208 HWTEST_F(DistributedKvDataManagerTest, GetKvStore005, TestSize.Level1)
209 {
210     ZLOGI("GetKvStore005 begin.");
211     std::shared_ptr<SingleKvStore> notExistKvStore;
212     Status status = manager.GetSingleKvStore(noCreate, appId, storeIdEmpty, notExistKvStore);
213     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
214     EXPECT_EQ(notExistKvStore, nullptr);
215 }
216 
217 /**
218 * @tc.name: GetKvStore006
219 * @tc.desc: Create a SingleKvStore with a 65-byte storeId, and the callback function should receive INVALID_ARGUMENT
220 * @tc.type: FUNC
221 * @tc.require: SR000CQDU0 AR000BVTDM
222 * @tc.author: liqiao
223 */
224 HWTEST_F(DistributedKvDataManagerTest, GetKvStore006, TestSize.Level1)
225 {
226     ZLOGI("GetKvStore006 begin.");
227     std::shared_ptr<SingleKvStore> notExistKvStore;
228     Status status = manager.GetSingleKvStore(create, appId, storeId65, notExistKvStore);
229     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
230     EXPECT_EQ(notExistKvStore, nullptr);
231 }
232 
233 /**
234 * @tc.name: GetKvStore007
235 * @tc.desc: Get a SingleKvStore with a 65-byte storeId, the callback function should receive INVALID_ARGUMENT
236 * @tc.type: FUNC
237 * @tc.require: SR000CQDU0 AR000BVTDM
238 * @tc.author: liqiao
239 */
240 HWTEST_F(DistributedKvDataManagerTest, GetKvStore007, TestSize.Level1)
241 {
242     ZLOGI("GetKvStore007 begin.");
243     std::shared_ptr<SingleKvStore> notExistKvStore;
244     Status status = manager.GetSingleKvStore(noCreate, appId, storeId65, notExistKvStore);
245     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
246     EXPECT_EQ(notExistKvStore, nullptr);
247 }
248 
249 /**
250 * @tc.name: GetAllKvStore001
251 * @tc.desc: Get all KvStore IDs when no KvStore exists, and the callback function should receive a 0-length vector.
252 * @tc.type: FUNC
253 * @tc.require: SR000CQDU0 AR000BVTDM
254 * @tc.author: liqiao
255 */
256 HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore001, TestSize.Level1)
257 {
258     ZLOGI("GetAllKvStore001 begin.");
259     std::vector<StoreId> storeIds;
260     Status status = manager.GetAllKvStoreId(appId, storeIds);
261     EXPECT_EQ(status, Status::SUCCESS);
262     EXPECT_EQ(storeIds.size(), static_cast<size_t>(0));
263 }
264 
265 /**
266 * @tc.name: GetAllKvStore002
267 * @tc.desc: Get all SingleKvStore IDs when no KvStore exists, and the callback function should receive a empty vector.
268 * @tc.type: FUNC
269 * @tc.require: SR000CQDU0 AR000BVTDM
270 * @tc.author: liqiao
271 */
272 HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore002, TestSize.Level1)
273 {
274     ZLOGI("GetAllKvStore002 begin.");
275     StoreId id1;
276     id1.storeId = "id1";
277     StoreId id2;
278     id2.storeId = "id2";
279     StoreId id3;
280     id3.storeId = "id3";
281     std::shared_ptr<SingleKvStore> kvStore;
282     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
283     ASSERT_NE(kvStore, nullptr);
284     ASSERT_EQ(status, Status::SUCCESS);
285     status = manager.GetSingleKvStore(create, appId, id2, kvStore);
286     ASSERT_NE(kvStore, nullptr);
287     ASSERT_EQ(status, Status::SUCCESS);
288     status = manager.GetSingleKvStore(create, appId, id3, kvStore);
289     ASSERT_NE(kvStore, nullptr);
290     ASSERT_EQ(status, Status::SUCCESS);
291     std::vector<StoreId> storeIds;
292     status = manager.GetAllKvStoreId(appId, storeIds);
293     EXPECT_EQ(status, Status::SUCCESS);
294     bool haveId1 = false;
295     bool haveId2 = false;
296     bool haveId3 = false;
297     for (StoreId &id : storeIds) {
298         if (id.storeId == "id1") {
299             haveId1 = true;
300         } else if (id.storeId == "id2") {
301             haveId2 = true;
302         } else if (id.storeId == "id3") {
303             haveId3 = true;
304         } else {
305             ZLOGI("got an unknown storeId.");
306             EXPECT_TRUE(false);
307         }
308     }
309     EXPECT_TRUE(haveId1);
310     EXPECT_TRUE(haveId2);
311     EXPECT_TRUE(haveId3);
312     EXPECT_EQ(storeIds.size(), static_cast<size_t>(3));
313 }
314 
315 /**
316 * @tc.name: CloseKvStore001
317 * @tc.desc: Close an opened KVStore, and the callback function should return SUCCESS.
318 * @tc.type: FUNC
319 * @tc.require: SR000CQDU0 AR000BVTDM
320 * @tc.author: liqiao
321 */
322 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore001, TestSize.Level1)
323 {
324     ZLOGI("CloseKvStore001 begin.");
325     std::shared_ptr<SingleKvStore> kvStore;
326     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
327     ASSERT_EQ(status, Status::SUCCESS);
328     ASSERT_NE(kvStore, nullptr);
329 
330     Status stat = manager.CloseKvStore(appId, storeId64);
331     EXPECT_EQ(stat, Status::SUCCESS);
332 }
333 
334 /**
335 * @tc.name: CloseKvStore002
336 * @tc.desc: Close a closed SingleKvStore, and the callback function should return SUCCESS.
337 * @tc.type: FUNC
338 * @tc.require: SR000CQDU0 AR000BVTDM
339 * @tc.author: liqiao
340 */
341 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore002, TestSize.Level1)
342 {
343     ZLOGI("CloseKvStore002 begin.");
344     std::shared_ptr<SingleKvStore> kvStore;
345     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
346     ASSERT_EQ(status, Status::SUCCESS);
347     ASSERT_NE(kvStore, nullptr);
348 
349     manager.CloseKvStore(appId, storeId64);
350     Status stat = manager.CloseKvStore(appId, storeId64);
351     EXPECT_EQ(stat, Status::STORE_NOT_OPEN);
352 }
353 
354 /**
355 * @tc.name: CloseKvStore003
356 * @tc.desc: Close a SingleKvStore with an empty storeId, and the callback function should return INVALID_ARGUMENT.
357 * @tc.type: FUNC
358 * @tc.require: SR000CQDU0 AR000BVTDM
359 * @tc.author: liqiao
360 */
361 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore003, TestSize.Level1)
362 {
363     ZLOGI("CloseKvStore003 begin.");
364     Status stat = manager.CloseKvStore(appId, storeIdEmpty);
365     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
366 }
367 
368 /**
369 * @tc.name: CloseKvStore004
370 * @tc.desc: Close a SingleKvStore with a 65-byte storeId, and the callback function should return INVALID_ARGUMENT.
371 * @tc.type: FUNC
372 * @tc.require: SR000CQDU0 AR000BVTDM
373 * @tc.author: liqiao
374 */
375 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore004, TestSize.Level1)
376 {
377     ZLOGI("CloseKvStore004 begin.");
378     Status stat = manager.CloseKvStore(appId, storeId65);
379     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
380 }
381 
382 /**
383 * @tc.name: CloseKvStore005
384 * @tc.desc: Close a non-existing SingleKvStore, and the callback function should return STORE_NOT_OPEN.
385 * @tc.type: FUNC
386 * @tc.require: SR000CQDU0 AR000BVTDM
387 * @tc.author: liqiao
388 */
389 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore005, TestSize.Level1)
390 {
391     ZLOGI("CloseKvStore005 begin.");
392     Status stat = manager.CloseKvStore(appId, storeId64);
393     EXPECT_EQ(stat, Status::STORE_NOT_OPEN);
394 }
395 
396 /**
397 * @tc.name: CloseKvStoreMulti001
398 * @tc.desc: Open a SingleKvStore several times and close them one by one.
399 * @tc.type: FUNC
400 * @tc.require: SR000CQDU0 AR000CSKRU
401 * @tc.author: liqiao
402 */
403 HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti001, TestSize.Level1)
404 {
405     ZLOGI("CloseKvStoreMulti001 begin.");
406     std::shared_ptr<SingleKvStore> notExistKvStore;
407     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
408     ASSERT_EQ(status, Status::SUCCESS);
409     EXPECT_NE(notExistKvStore, nullptr);
410 
411     std::shared_ptr<SingleKvStore> existKvStore;
412     manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore);
413     ASSERT_EQ(status, Status::SUCCESS);
414     EXPECT_NE(existKvStore, nullptr);
415 
416     Status stat = manager.CloseKvStore(appId, storeId64);
417     EXPECT_EQ(stat, Status::SUCCESS);
418 
419     stat = manager.CloseKvStore(appId, storeId64);
420     EXPECT_EQ(stat, Status::SUCCESS);
421 }
422 
423 /**
424 * @tc.name: CloseKvStoreMulti002
425 * @tc.desc: Open a SingleKvStore several times and close them one by one.
426 * @tc.type: FUNC
427 * @tc.require: SR000CQDU0 AR000CSKRU
428 * @tc.author: liqiao
429 */
430 HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti002, TestSize.Level1)
431 {
432     ZLOGI("CloseKvStoreMulti002 begin.");
433     std::shared_ptr<SingleKvStore> notExistKvStore;
434     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
435     ASSERT_EQ(status, Status::SUCCESS);
436     EXPECT_NE(notExistKvStore, nullptr);
437 
438     std::shared_ptr<SingleKvStore> existKvStore1;
439     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore1);
440     ASSERT_EQ(status, Status::SUCCESS);
441     EXPECT_NE(existKvStore1, nullptr);
442 
443     std::shared_ptr<SingleKvStore> existKvStore2;
444     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore2);
445     ASSERT_EQ(status, Status::SUCCESS);
446     EXPECT_NE(existKvStore2, nullptr);
447 
448     Status stat = manager.CloseKvStore(appId, storeId64);
449     EXPECT_EQ(stat, Status::SUCCESS);
450 
451     stat = manager.CloseKvStore(appId, storeId64);
452     EXPECT_EQ(stat, Status::SUCCESS);
453 
454     stat = manager.CloseKvStore(appId, storeId64);
455     EXPECT_EQ(stat, Status::SUCCESS);
456 
457     stat = manager.CloseKvStore(appId, storeId64);
458     EXPECT_NE(stat, Status::SUCCESS);
459 }
460 
461 /**
462 * @tc.name: CloseAllKvStore001
463 * @tc.desc: Close all opened KvStores, and the callback function should return SUCCESS.
464 * @tc.type: FUNC
465 * @tc.require: SR000CQDU0 AR000BVTDM
466 * @tc.author: liqiao
467 */
468 HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore001, TestSize.Level1)
469 {
470     ZLOGI("CloseAllKvStore001 begin.");
471     std::shared_ptr<SingleKvStore> kvStore1;
472     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
473     ASSERT_EQ(status, Status::SUCCESS);
474     ASSERT_NE(kvStore1, nullptr);
475 
476     std::shared_ptr<SingleKvStore> kvStore2;
477     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
478     ASSERT_EQ(status, Status::SUCCESS);
479     ASSERT_NE(kvStore2, nullptr);
480 
481     Status stat = manager.CloseAllKvStore(appId);
482     EXPECT_EQ(stat, Status::SUCCESS);
483 }
484 
485 /**
486 * @tc.name: CloseAllKvStore002
487 * @tc.desc: Close all KvStores which exist but are not opened, and the callback function should return STORE_NOT_OPEN.
488 * @tc.type: FUNC
489 * @tc.require: SR000CQDU0 AR000BVTDM
490 * @tc.author: liqiao
491 */
492 HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore002, TestSize.Level1)
493 {
494     ZLOGI("CloseAllKvStore002 begin.");
495     std::shared_ptr<SingleKvStore> kvStore;
496     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
497     ASSERT_EQ(status, Status::SUCCESS);
498     ASSERT_NE(kvStore, nullptr);
499 
500     std::shared_ptr<SingleKvStore> kvStore2;
501     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
502     ASSERT_EQ(status, Status::SUCCESS);
503     ASSERT_NE(kvStore2, nullptr);
504 
505     Status stat = manager.CloseKvStore(appId, storeId64);
506     EXPECT_EQ(stat, Status::SUCCESS);
507 
508     stat = manager.CloseAllKvStore(appId);
509     EXPECT_EQ(stat, Status::SUCCESS);
510 }
511 
512 /**
513 * @tc.name: DeleteKvStore001
514 * @tc.desc: Delete a closed KvStore, and the callback function should return SUCCESS.
515 * @tc.type: FUNC
516 * @tc.require: SR000CQDU0 AR000BVTDM
517 * @tc.author: liqiao
518 */
519 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore001, TestSize.Level1)
520 {
521     ZLOGI("DeleteKvStore001 begin.");
522     std::shared_ptr<SingleKvStore> kvStore;
523     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
524     ASSERT_EQ(status, Status::SUCCESS);
525     ASSERT_NE(kvStore, nullptr);
526 
527     Status stat = manager.CloseKvStore(appId, storeId64);
528     ASSERT_EQ(stat, Status::SUCCESS);
529 
530     stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
531     EXPECT_EQ(stat, Status::SUCCESS);
532 }
533 
534 /**
535 * @tc.name: DeleteKvStore002
536 * @tc.desc: Delete an opened SingleKvStore, and the callback function should return SUCCESS.
537 * @tc.type: FUNC
538 * @tc.require: SR000CQDU0 AR000BVTDM
539 * @tc.author: liqiao
540 */
541 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore002, TestSize.Level1)
542 {
543     ZLOGI("DeleteKvStore002 begin.");
544     std::shared_ptr<SingleKvStore> kvStore;
545     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
546     ASSERT_EQ(status, Status::SUCCESS);
547     ASSERT_NE(kvStore, nullptr);
548 
549     // first close it if opened, and then delete it.
550     Status stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
551     EXPECT_EQ(stat, Status::SUCCESS);
552 }
553 
554 /**
555 * @tc.name: DeleteKvStore003
556 * @tc.desc: Delete a non-existing KvStore, and the callback function should return DB_ERROR.
557 * @tc.type: FUNC
558 * @tc.require: SR000CQDU0 AR000BVTDM
559 * @tc.author: liqiao
560 */
561 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore003, TestSize.Level1)
562 {
563     ZLOGI("DeleteKvStore003 begin.");
564     Status stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
565     EXPECT_EQ(stat, Status::STORE_NOT_FOUND);
566 }
567 
568 /**
569 * @tc.name: DeleteKvStore004
570 * @tc.desc: Delete a KvStore with an empty storeId, and the callback function should return INVALID_ARGUMENT.
571 * @tc.type: FUNC
572 * @tc.require: SR000CQDU0 AR000BVTDM
573 * @tc.author: liqiao
574 */
575 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore004, TestSize.Level1)
576 {
577     ZLOGI("DeleteKvStore004 begin.");
578     Status stat = manager.DeleteKvStore(appId, storeIdEmpty);
579     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
580 }
581 
582 /**
583 * @tc.name: DeleteKvStore005
584 * @tc.desc: Delete a KvStore with 65 bytes long storeId (which exceed storeId length limit). Should
585 * return INVALID_ARGUMENT.
586 * @tc.type: FUNC
587 * @tc.require: SR000CQDU0 AR000BVTDM
588 * @tc.author: liqiao
589 */
590 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore005, TestSize.Level1)
591 {
592     ZLOGI("DeleteKvStore005 begin.");
593     Status stat = manager.DeleteKvStore(appId, storeId65);
594     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
595 }
596 
597 /**
598 * @tc.name: DeleteAllKvStore001
599 * @tc.desc: Delete all KvStores after closing all of them, and the callback function should return SUCCESS.
600 * @tc.type: FUNC
601 * @tc.require: SR000CQDU0 AR000BVTDM
602 * @tc.author: liqiao
603 */
604 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore001, TestSize.Level1)
605 {
606     ZLOGI("DeleteAllKvStore001 begin.");
607     std::shared_ptr<SingleKvStore> kvStore1;
608     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
609     ASSERT_EQ(status, Status::SUCCESS);
610     ASSERT_NE(kvStore1, nullptr);
611     std::shared_ptr<SingleKvStore> kvStore2;
612     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
613     ASSERT_EQ(status, Status::SUCCESS);
614     ASSERT_NE(kvStore2, nullptr);
615     Status stat = manager.CloseKvStore(appId, storeId64);
616     EXPECT_EQ(stat, Status::SUCCESS);
617     stat = manager.CloseKvStore(appId, storeIdTest);
618     EXPECT_EQ(stat, Status::SUCCESS);
619 
620     stat = manager.DeleteAllKvStore({""}, create.baseDir);
621     EXPECT_NE(stat, Status::SUCCESS);
622     stat = manager.DeleteAllKvStore(appId, "");
623     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
624 
625     stat = manager.DeleteAllKvStore(appId, create.baseDir);
626     EXPECT_EQ(stat, Status::SUCCESS);
627 }
628 
629 /**
630 * @tc.name: DeleteAllKvStore002
631 * @tc.desc: Delete all kvstore fail when any kvstore in the appId is not closed
632 * @tc.type: FUNC
633 * @tc.require: SR000CQDU0 AR000BVTDM
634 * @tc.author: liqiao
635 */
636 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore002, TestSize.Level1)
637 {
638     ZLOGI("DeleteAllKvStore002 begin.");
639     std::shared_ptr<SingleKvStore> kvStore1;
640     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
641     ASSERT_EQ(status, Status::SUCCESS);
642     ASSERT_NE(kvStore1, nullptr);
643     std::shared_ptr<SingleKvStore> kvStore2;
644     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
645     ASSERT_EQ(status, Status::SUCCESS);
646     ASSERT_NE(kvStore2, nullptr);
647     Status stat = manager.CloseKvStore(appId, storeId64);
648     EXPECT_EQ(stat, Status::SUCCESS);
649 
650     stat = manager.DeleteAllKvStore(appId, create.baseDir);
651     EXPECT_EQ(stat, Status::SUCCESS);
652 }
653 
654 /**
655 * @tc.name: DeleteAllKvStore003
656 * @tc.desc: Delete all KvStores even if no KvStore exists in the appId.
657 * @tc.type: FUNC
658 * @tc.require: SR000CQDU0 AR000BVTDM
659 * @tc.author: liqiao
660 */
661 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore003, TestSize.Level1)
662 {
663     ZLOGI("DeleteAllKvStore003 begin.");
664     Status stat = manager.DeleteAllKvStore(appId, create.baseDir);
665     EXPECT_EQ(stat, Status::SUCCESS);
666 }
667 
668 /**
669 * @tc.name: DeleteAllKvStore004
670 * @tc.desc: when delete the last active kvstore, the system will remove the app manager scene
671 * @tc.type: FUNC
672 * @tc.require: bugs
673 * @tc.author: Sven Wang
674 */
675 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore004, TestSize.Level1)
676 {
677     ZLOGI("DeleteAllKvStore004 begin.");
678     std::shared_ptr<SingleKvStore> kvStore1;
679     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
680     ASSERT_EQ(status, Status::SUCCESS);
681     ASSERT_NE(kvStore1, nullptr);
682     std::shared_ptr<SingleKvStore> kvStore2;
683     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
684     ASSERT_EQ(status, Status::SUCCESS);
685     ASSERT_NE(kvStore2, nullptr);
686     Status stat = manager.CloseKvStore(appId, storeId64);
687     EXPECT_EQ(stat, Status::SUCCESS);
688     stat = manager.CloseKvStore(appId, storeIdTest);
689     EXPECT_EQ(stat, Status::SUCCESS);
690     stat = manager.DeleteKvStore(appId, storeIdTest, create.baseDir);
691     EXPECT_EQ(stat, Status::SUCCESS);
692     stat = manager.DeleteAllKvStore(appId, create.baseDir);
693     EXPECT_EQ(stat, Status::SUCCESS);
694 }
695 
696 /**
697 * @tc.name: RegisterKvStoreServiceDeathRecipient001
698 * @tc.desc: Register a callback called when the service dies.
699 * @tc.type: FUNC
700 * @tc.require: SR000CQDU0 AR000CQDU1
701 * @tc.author: liqiao
702 */
703 HWTEST_F(DistributedKvDataManagerTest, RegisterKvStoreServiceDeathRecipient001, TestSize.Level1)
704 {
705     ZLOGI("RegisterKvStoreServiceDeathRecipient001 begin.");
706     std::shared_ptr<KvStoreDeathRecipient> kvStoreDeathRecipient = nullptr;
707     manager.RegisterKvStoreServiceDeathRecipient(kvStoreDeathRecipient);
708     manager.UnRegisterKvStoreServiceDeathRecipient(kvStoreDeathRecipient);
709 
710     kvStoreDeathRecipient = std::make_shared<MyDeathRecipient>();
711     manager.RegisterKvStoreServiceDeathRecipient(kvStoreDeathRecipient);
712     kvStoreDeathRecipient->OnRemoteDied();
713 }
714 } // namespace OHOS::Test