• 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 "block_data.h"
21 #include "dev_manager.h"
22 #include "kvstore_death_recipient.h"
23 #include "log_print.h"
24 #include "types.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 namespace OHOS::Test {
29 class DistributedKvDataManagerTest : public testing::Test {
30 public:
31     static constexpr size_t NUM_MIN = 5;
32     static constexpr size_t NUM_MAX = 12;
33     static std::shared_ptr<ExecutorPool> executors;
34 
35     static DistributedKvDataManager manager;
36     static Options create;
37     static Options noCreate;
38 
39     static UserId userId;
40 
41     static AppId appId;
42     static StoreId storeId64;
43     static StoreId storeId65;
44     static StoreId storeIdTest;
45     static StoreId storeIdEmpty;
46 
47     static Entry entryA;
48     static Entry entryB;
49 
50     static void SetUpTestCase(void);
51     static void TearDownTestCase(void);
52 
53     static void RemoveAllStore(DistributedKvDataManager &manager);
54 
55     void SetUp();
56     void TearDown();
57     DistributedKvDataManagerTest();
58 };
59 
60 class SwitchDataObserver : public KvStoreObserver {
61 public:
OnSwitchChange(const SwitchNotification & notification)62     void OnSwitchChange(const SwitchNotification &notification) override
63     {
64         blockData_.SetValue(notification);
65     }
66 
Get()67     SwitchNotification Get()
68     {
69         return blockData_.GetValue();
70     }
71 
72 private:
73     BlockData<SwitchNotification> blockData_ { 1, SwitchNotification() };
74 };
75 
76 class MyDeathRecipient : public KvStoreDeathRecipient {
77 public:
MyDeathRecipient()78     MyDeathRecipient() {}
~MyDeathRecipient()79     virtual ~MyDeathRecipient() {}
OnRemoteDied()80     void OnRemoteDied() override {}
81 };
82 std::shared_ptr<ExecutorPool> DistributedKvDataManagerTest::executors =
83     std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
84 
85 DistributedKvDataManager DistributedKvDataManagerTest::manager;
86 Options DistributedKvDataManagerTest::create;
87 Options DistributedKvDataManagerTest::noCreate;
88 
89 UserId DistributedKvDataManagerTest::userId;
90 
91 AppId DistributedKvDataManagerTest::appId;
92 StoreId DistributedKvDataManagerTest::storeId64;
93 StoreId DistributedKvDataManagerTest::storeId65;
94 StoreId DistributedKvDataManagerTest::storeIdTest;
95 StoreId DistributedKvDataManagerTest::storeIdEmpty;
96 
97 Entry DistributedKvDataManagerTest::entryA;
98 Entry DistributedKvDataManagerTest::entryB;
99 
RemoveAllStore(DistributedKvDataManager & manager)100 void DistributedKvDataManagerTest::RemoveAllStore(DistributedKvDataManager &manager)
101 {
102     manager.CloseAllKvStore(appId);
103     manager.DeleteAllKvStore(appId, create.baseDir);
104 }
SetUpTestCase(void)105 void DistributedKvDataManagerTest::SetUpTestCase(void)
106 {
107     manager.SetExecutors(executors);
108 
109     userId.userId = "account0";
110     appId.appId = "ohos.kvdatamanager.test";
111     create.createIfMissing = true;
112     create.encrypt = false;
113     create.securityLevel = S1;
114     create.autoSync = true;
115     create.kvStoreType = SINGLE_VERSION;
116     create.area = EL1;
117     create.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
118     mkdir(create.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
119 
120     noCreate.createIfMissing = false;
121     noCreate.encrypt = false;
122     noCreate.securityLevel = S1;
123     noCreate.autoSync = true;
124     noCreate.kvStoreType = SINGLE_VERSION;
125     noCreate.area = EL1;
126     noCreate.baseDir = create.baseDir;
127 
128     storeId64.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000";
129     storeId65.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000"
130                         "a000000000b000000000c000000000d000000000e000000000f000000000g0000";
131     storeIdTest.storeId = "test";
132     storeIdEmpty.storeId = "";
133 
134     entryA.key = "a";
135     entryA.value = "valueA";
136     entryB.key = "b";
137     entryB.value = "valueB";
138     RemoveAllStore(manager);
139 }
140 
TearDownTestCase(void)141 void DistributedKvDataManagerTest::TearDownTestCase(void)
142 {
143     RemoveAllStore(manager);
144     (void)remove((create.baseDir + "/kvdb").c_str());
145     (void)remove(create.baseDir.c_str());
146 }
147 
SetUp(void)148 void DistributedKvDataManagerTest::SetUp(void)
149 {}
150 
DistributedKvDataManagerTest(void)151 DistributedKvDataManagerTest::DistributedKvDataManagerTest(void)
152 {}
153 
TearDown(void)154 void DistributedKvDataManagerTest::TearDown(void)
155 {
156     RemoveAllStore(manager);
157 }
158 
159 /**
160 * @tc.name: GetKvStore001
161 * @tc.desc: Get an exist SingleKvStore
162 * @tc.type: FUNC
163 * @tc.require: SR000CQDU0 AR000BVTDM
164 * @tc.author: liqiao
165 */
166 HWTEST_F(DistributedKvDataManagerTest, GetKvStore001, TestSize.Level1)
167 {
168     ZLOGI("GetKvStore001 begin.");
169     std::shared_ptr<SingleKvStore> notExistKvStore;
170     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
171     ASSERT_EQ(status, Status::SUCCESS);
172     EXPECT_NE(notExistKvStore, nullptr);
173 
174     std::shared_ptr<SingleKvStore> existKvStore;
175     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore);
176     ASSERT_EQ(status, Status::SUCCESS);
177     EXPECT_NE(existKvStore, nullptr);
178 }
179 
180 /**
181 * @tc.name: GetKvStore002
182 * @tc.desc: Create and get a new SingleKvStore
183 * @tc.type: FUNC
184 * @tc.require: SR000CQDU0 AR000BVTDM
185 * @tc.author: liqiao
186 */
187 HWTEST_F(DistributedKvDataManagerTest, GetKvStore002, TestSize.Level1)
188 {
189     ZLOGI("GetKvStore002 begin.");
190     std::shared_ptr<SingleKvStore> notExistKvStore;
191     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
192     ASSERT_EQ(status, Status::SUCCESS);
193     EXPECT_NE(notExistKvStore, nullptr);
194     manager.CloseKvStore(appId, storeId64);
195     manager.DeleteKvStore(appId, storeId64);
196 }
197 
198 /**
199 * @tc.name: GetKvStore003
200 * @tc.desc: Get a non-existing SingleKvStore, and the callback function should receive STORE_NOT_FOUND and
201 * get a nullptr.
202 * @tc.type: FUNC
203 * @tc.require: SR000CQDU0 AR000BVTDM
204 * @tc.author: liqiao
205 */
206 HWTEST_F(DistributedKvDataManagerTest, GetKvStore003, TestSize.Level1)
207 {
208     ZLOGI("GetKvStore003 begin.");
209     std::shared_ptr<SingleKvStore> notExistKvStore;
210     (void)manager.GetSingleKvStore(noCreate, appId, storeId64, notExistKvStore);
211     EXPECT_EQ(notExistKvStore, nullptr);
212 }
213 
214 /**
215 * @tc.name: GetKvStore004
216 * @tc.desc: Create a SingleKvStore with an empty storeId, and the callback function should receive
217 * @tc.type: FUNC
218 * @tc.require: SR000CQDU0 AR000BVTDM
219 * @tc.author: liqiao
220 */
221 HWTEST_F(DistributedKvDataManagerTest, GetKvStore004, TestSize.Level1)
222 {
223     ZLOGI("GetKvStore004 begin.");
224     std::shared_ptr<SingleKvStore> notExistKvStore;
225     Status status = manager.GetSingleKvStore(create, appId, storeIdEmpty, notExistKvStore);
226     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
227     EXPECT_EQ(notExistKvStore, nullptr);
228 }
229 
230 /**
231 * @tc.name: GetKvStore005
232 * @tc.desc: Get a SingleKvStore with an empty storeId, and the callback function should receive INVALID_ARGUMENT
233 * @tc.type: FUNC
234 * @tc.require: SR000CQDU0 AR000BVTDM
235 * @tc.author: liqiao
236 */
237 HWTEST_F(DistributedKvDataManagerTest, GetKvStore005, TestSize.Level1)
238 {
239     ZLOGI("GetKvStore005 begin.");
240     std::shared_ptr<SingleKvStore> notExistKvStore;
241     Status status = manager.GetSingleKvStore(noCreate, appId, storeIdEmpty, notExistKvStore);
242     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
243     EXPECT_EQ(notExistKvStore, nullptr);
244 }
245 
246 /**
247 * @tc.name: GetKvStore006
248 * @tc.desc: Create a SingleKvStore with a 65-byte storeId, and the callback function should receive INVALID_ARGUMENT
249 * @tc.type: FUNC
250 * @tc.require: SR000CQDU0 AR000BVTDM
251 * @tc.author: liqiao
252 */
253 HWTEST_F(DistributedKvDataManagerTest, GetKvStore006, TestSize.Level1)
254 {
255     ZLOGI("GetKvStore006 begin.");
256     std::shared_ptr<SingleKvStore> notExistKvStore;
257     Status status = manager.GetSingleKvStore(create, appId, storeId65, notExistKvStore);
258     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
259     EXPECT_EQ(notExistKvStore, nullptr);
260 }
261 
262 /**
263 * @tc.name: GetKvStore007
264 * @tc.desc: Get a SingleKvStore with a 65-byte storeId, the callback function should receive INVALID_ARGUMENT
265 * @tc.type: FUNC
266 * @tc.require: SR000CQDU0 AR000BVTDM
267 * @tc.author: liqiao
268 */
269 HWTEST_F(DistributedKvDataManagerTest, GetKvStore007, TestSize.Level1)
270 {
271     ZLOGI("GetKvStore007 begin.");
272     std::shared_ptr<SingleKvStore> notExistKvStore;
273     Status status = manager.GetSingleKvStore(noCreate, appId, storeId65, notExistKvStore);
274     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
275     EXPECT_EQ(notExistKvStore, nullptr);
276 }
277 
278 /**
279  * @tc.name: GetKvStore008
280  * @tc.desc: Get a SingleKvStore which supports cloud sync.
281  * @tc.type: FUNC
282  * @tc.require:
283  * @tc.author: Hollokin
284  */
285 HWTEST_F(DistributedKvDataManagerTest, GetKvStore008, TestSize.Level1)
286 {
287     ZLOGI("GetKvStore008 begin.");
288     std::shared_ptr<SingleKvStore> cloudKvStore = nullptr;
289     Options options = create;
290     options.isPublic = true;
291     options.cloudConfig = {
292         .enableCloud = true,
293         .autoSync = true
294     };
295     Status status = manager.GetSingleKvStore(options, appId, storeId64, cloudKvStore);
296     ASSERT_EQ(status, Status::SUCCESS);
297     EXPECT_NE(cloudKvStore, nullptr);
298 }
299 
300 /**
301  * @tc.name: GetKvStore009
302  * @tc.desc: Get a SingleKvStore which security level upgrade.
303  * @tc.type: FUNC
304  * @tc.require:
305  * @tc.author: yanhui
306  */
307 HWTEST_F(DistributedKvDataManagerTest, GetKvStore009, TestSize.Level1)
308 {
309     ZLOGI("GetKvStore009 begin.");
310     std::shared_ptr<SingleKvStore> kvStore = nullptr;
311     Options options = create;
312     options.securityLevel = S1;
313     Status status = manager.GetSingleKvStore(options, appId, storeId64, kvStore);
314 
315     manager.CloseKvStore(appId, storeId64);
316     options.securityLevel = S2;
317     status = manager.GetSingleKvStore(options, appId, storeId64, kvStore);
318     ASSERT_EQ(status, Status::SUCCESS);
319     EXPECT_NE(kvStore, nullptr);
320 
321     manager.CloseKvStore(appId, storeId64);
322     options.securityLevel = S1;
323     status = manager.GetSingleKvStore(options, appId, storeId64, kvStore);
324     ASSERT_EQ(status, Status::STORE_META_CHANGED);
325     EXPECT_EQ(kvStore, nullptr);
326 }
327 
328 /**
329 * @tc.name: GetKvStoreInvalidSecurityLevel
330 * @tc.desc: Get a SingleKvStore with a 64
331  * -byte storeId, the callback function should receive INVALID_ARGUMENT
332 * @tc.type: FUNC
333 * @tc.require: SR000IIM2J AR000IIMLL
334 * @tc.author: wangkai
335 */
336 HWTEST_F(DistributedKvDataManagerTest, GetKvStoreInvalidSecurityLevel, TestSize.Level1)
337 {
338     ZLOGI("GetKvStoreInvalidSecurityLevel begin.");
339     std::shared_ptr<SingleKvStore> notExistKvStore;
340     Options invalidOption = create;
341     invalidOption.securityLevel = INVALID_LABEL;
342     Status status = manager.GetSingleKvStore(invalidOption, appId, storeId64, notExistKvStore);
343     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
344     EXPECT_EQ(notExistKvStore, nullptr);
345 }
346 
347 /**
348 * @tc.name: GetAllKvStore001
349 * @tc.desc: Get all KvStore IDs when no KvStore exists, and the callback function should receive a 0-length vector.
350 * @tc.type: FUNC
351 * @tc.require: SR000CQDU0 AR000BVTDM
352 * @tc.author: liqiao
353 */
354 HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore001, TestSize.Level1)
355 {
356     ZLOGI("GetAllKvStore001 begin.");
357     std::vector<StoreId> storeIds;
358     Status status = manager.GetAllKvStoreId(appId, storeIds);
359     EXPECT_EQ(status, Status::SUCCESS);
360     EXPECT_EQ(storeIds.size(), static_cast<size_t>(0));
361 }
362 
363 /**
364 * @tc.name: GetAllKvStore002
365 * @tc.desc: Get all SingleKvStore IDs when no KvStore exists, and the callback function should receive a empty vector.
366 * @tc.type: FUNC
367 * @tc.require: SR000CQDU0 AR000BVTDM
368 * @tc.author: liqiao
369 */
370 HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore002, TestSize.Level1)
371 {
372     ZLOGI("GetAllKvStore002 begin.");
373     StoreId id1;
374     id1.storeId = "id1";
375     StoreId id2;
376     id2.storeId = "id2";
377     StoreId id3;
378     id3.storeId = "id3";
379     std::shared_ptr<SingleKvStore> kvStore;
380     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
381     ASSERT_NE(kvStore, nullptr);
382     ASSERT_EQ(status, Status::SUCCESS);
383     status = manager.GetSingleKvStore(create, appId, id2, kvStore);
384     ASSERT_NE(kvStore, nullptr);
385     ASSERT_EQ(status, Status::SUCCESS);
386     status = manager.GetSingleKvStore(create, appId, id3, kvStore);
387     ASSERT_NE(kvStore, nullptr);
388     ASSERT_EQ(status, Status::SUCCESS);
389     std::vector<StoreId> storeIds;
390     status = manager.GetAllKvStoreId(appId, storeIds);
391     EXPECT_EQ(status, Status::SUCCESS);
392     bool haveId1 = false;
393     bool haveId2 = false;
394     bool haveId3 = false;
395     for (StoreId &id : storeIds) {
396         if (id.storeId == "id1") {
397             haveId1 = true;
398         } else if (id.storeId == "id2") {
399             haveId2 = true;
400         } else if (id.storeId == "id3") {
401             haveId3 = true;
402         } else {
403             ZLOGI("got an unknown storeId.");
404             EXPECT_TRUE(false);
405         }
406     }
407     EXPECT_TRUE(haveId1);
408     EXPECT_TRUE(haveId2);
409     EXPECT_TRUE(haveId3);
410     EXPECT_EQ(storeIds.size(), static_cast<size_t>(3));
411 }
412 
413 /**
414 * @tc.name: CloseKvStore001
415 * @tc.desc: Close an opened KVStore, and the callback function should return SUCCESS.
416 * @tc.type: FUNC
417 * @tc.require: SR000CQDU0 AR000BVTDM
418 * @tc.author: liqiao
419 */
420 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore001, TestSize.Level1)
421 {
422     ZLOGI("CloseKvStore001 begin.");
423     std::shared_ptr<SingleKvStore> kvStore;
424     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
425     ASSERT_EQ(status, Status::SUCCESS);
426     ASSERT_NE(kvStore, nullptr);
427 
428     Status stat = manager.CloseKvStore(appId, storeId64);
429     EXPECT_EQ(stat, Status::SUCCESS);
430 }
431 
432 /**
433 * @tc.name: CloseKvStore002
434 * @tc.desc: Close a closed SingleKvStore, and the callback function should return SUCCESS.
435 * @tc.type: FUNC
436 * @tc.require: SR000CQDU0 AR000BVTDM
437 * @tc.author: liqiao
438 */
439 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore002, TestSize.Level1)
440 {
441     ZLOGI("CloseKvStore002 begin.");
442     std::shared_ptr<SingleKvStore> kvStore;
443     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
444     ASSERT_EQ(status, Status::SUCCESS);
445     ASSERT_NE(kvStore, nullptr);
446 
447     manager.CloseKvStore(appId, storeId64);
448     Status stat = manager.CloseKvStore(appId, storeId64);
449     EXPECT_EQ(stat, Status::STORE_NOT_OPEN);
450 }
451 
452 /**
453 * @tc.name: CloseKvStore003
454 * @tc.desc: Close a SingleKvStore with an empty storeId, and the callback function should return INVALID_ARGUMENT.
455 * @tc.type: FUNC
456 * @tc.require: SR000CQDU0 AR000BVTDM
457 * @tc.author: liqiao
458 */
459 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore003, TestSize.Level1)
460 {
461     ZLOGI("CloseKvStore003 begin.");
462     Status stat = manager.CloseKvStore(appId, storeIdEmpty);
463     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
464 }
465 
466 /**
467 * @tc.name: CloseKvStore004
468 * @tc.desc: Close a SingleKvStore with a 65-byte storeId, and the callback function should return INVALID_ARGUMENT.
469 * @tc.type: FUNC
470 * @tc.require: SR000CQDU0 AR000BVTDM
471 * @tc.author: liqiao
472 */
473 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore004, TestSize.Level1)
474 {
475     ZLOGI("CloseKvStore004 begin.");
476     Status stat = manager.CloseKvStore(appId, storeId65);
477     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
478 }
479 
480 /**
481 * @tc.name: CloseKvStore005
482 * @tc.desc: Close a non-existing SingleKvStore, and the callback function should return STORE_NOT_OPEN.
483 * @tc.type: FUNC
484 * @tc.require: SR000CQDU0 AR000BVTDM
485 * @tc.author: liqiao
486 */
487 HWTEST_F(DistributedKvDataManagerTest, CloseKvStore005, TestSize.Level1)
488 {
489     ZLOGI("CloseKvStore005 begin.");
490     Status stat = manager.CloseKvStore(appId, storeId64);
491     EXPECT_EQ(stat, Status::STORE_NOT_OPEN);
492 }
493 
494 /**
495 * @tc.name: CloseKvStoreMulti001
496 * @tc.desc: Open a SingleKvStore several times and close them one by one.
497 * @tc.type: FUNC
498 * @tc.require: SR000CQDU0 AR000CSKRU
499 * @tc.author: liqiao
500 */
501 HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti001, TestSize.Level1)
502 {
503     ZLOGI("CloseKvStoreMulti001 begin.");
504     std::shared_ptr<SingleKvStore> notExistKvStore;
505     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
506     ASSERT_EQ(status, Status::SUCCESS);
507     EXPECT_NE(notExistKvStore, nullptr);
508 
509     std::shared_ptr<SingleKvStore> existKvStore;
510     manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore);
511     ASSERT_EQ(status, Status::SUCCESS);
512     EXPECT_NE(existKvStore, nullptr);
513 
514     Status stat = manager.CloseKvStore(appId, storeId64);
515     EXPECT_EQ(stat, Status::SUCCESS);
516 
517     stat = manager.CloseKvStore(appId, storeId64);
518     EXPECT_EQ(stat, Status::SUCCESS);
519 }
520 
521 /**
522 * @tc.name: CloseKvStoreMulti002
523 * @tc.desc: Open a SingleKvStore several times and close them one by one.
524 * @tc.type: FUNC
525 * @tc.require: SR000CQDU0 AR000CSKRU
526 * @tc.author: liqiao
527 */
528 HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti002, TestSize.Level1)
529 {
530     ZLOGI("CloseKvStoreMulti002 begin.");
531     std::shared_ptr<SingleKvStore> notExistKvStore;
532     Status status = manager.GetSingleKvStore(create, appId, storeId64, notExistKvStore);
533     ASSERT_EQ(status, Status::SUCCESS);
534     EXPECT_NE(notExistKvStore, nullptr);
535 
536     std::shared_ptr<SingleKvStore> existKvStore1;
537     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore1);
538     ASSERT_EQ(status, Status::SUCCESS);
539     EXPECT_NE(existKvStore1, nullptr);
540 
541     std::shared_ptr<SingleKvStore> existKvStore2;
542     status = manager.GetSingleKvStore(noCreate, appId, storeId64, existKvStore2);
543     ASSERT_EQ(status, Status::SUCCESS);
544     EXPECT_NE(existKvStore2, nullptr);
545 
546     Status stat = manager.CloseKvStore(appId, storeId64);
547     EXPECT_EQ(stat, Status::SUCCESS);
548 
549     stat = manager.CloseKvStore(appId, storeId64);
550     EXPECT_EQ(stat, Status::SUCCESS);
551 
552     stat = manager.CloseKvStore(appId, storeId64);
553     EXPECT_EQ(stat, Status::SUCCESS);
554 
555     stat = manager.CloseKvStore(appId, storeId64);
556     EXPECT_NE(stat, Status::SUCCESS);
557 }
558 
559 /**
560 * @tc.name: CloseAllKvStore001
561 * @tc.desc: Close all opened KvStores, and the callback function should return SUCCESS.
562 * @tc.type: FUNC
563 * @tc.require: SR000CQDU0 AR000BVTDM
564 * @tc.author: liqiao
565 */
566 HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore001, TestSize.Level1)
567 {
568     ZLOGI("CloseAllKvStore001 begin.");
569     std::shared_ptr<SingleKvStore> kvStore1;
570     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
571     ASSERT_EQ(status, Status::SUCCESS);
572     ASSERT_NE(kvStore1, nullptr);
573 
574     std::shared_ptr<SingleKvStore> kvStore2;
575     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
576     ASSERT_EQ(status, Status::SUCCESS);
577     ASSERT_NE(kvStore2, nullptr);
578 
579     Status stat = manager.CloseAllKvStore(appId);
580     EXPECT_EQ(stat, Status::SUCCESS);
581 }
582 
583 /**
584 * @tc.name: CloseAllKvStore002
585 * @tc.desc: Close all KvStores which exist but are not opened, and the callback function should return STORE_NOT_OPEN.
586 * @tc.type: FUNC
587 * @tc.require: SR000CQDU0 AR000BVTDM
588 * @tc.author: liqiao
589 */
590 HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore002, TestSize.Level1)
591 {
592     ZLOGI("CloseAllKvStore002 begin.");
593     std::shared_ptr<SingleKvStore> kvStore;
594     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
595     ASSERT_EQ(status, Status::SUCCESS);
596     ASSERT_NE(kvStore, nullptr);
597 
598     std::shared_ptr<SingleKvStore> kvStore2;
599     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
600     ASSERT_EQ(status, Status::SUCCESS);
601     ASSERT_NE(kvStore2, nullptr);
602 
603     Status stat = manager.CloseKvStore(appId, storeId64);
604     EXPECT_EQ(stat, Status::SUCCESS);
605 
606     stat = manager.CloseAllKvStore(appId);
607     EXPECT_EQ(stat, Status::SUCCESS);
608 }
609 
610 /**
611 * @tc.name: DeleteKvStore001
612 * @tc.desc: Delete a closed KvStore, and the callback function should return SUCCESS.
613 * @tc.type: FUNC
614 * @tc.require: SR000CQDU0 AR000BVTDM
615 * @tc.author: liqiao
616 */
617 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore001, TestSize.Level1)
618 {
619     ZLOGI("DeleteKvStore001 begin.");
620     std::shared_ptr<SingleKvStore> kvStore;
621     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
622     ASSERT_EQ(status, Status::SUCCESS);
623     ASSERT_NE(kvStore, nullptr);
624 
625     Status stat = manager.CloseKvStore(appId, storeId64);
626     ASSERT_EQ(stat, Status::SUCCESS);
627 
628     stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
629     EXPECT_EQ(stat, Status::SUCCESS);
630 }
631 
632 /**
633 * @tc.name: DeleteKvStore002
634 * @tc.desc: Delete an opened SingleKvStore, and the callback function should return SUCCESS.
635 * @tc.type: FUNC
636 * @tc.require: SR000CQDU0 AR000BVTDM
637 * @tc.author: liqiao
638 */
639 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore002, TestSize.Level1)
640 {
641     ZLOGI("DeleteKvStore002 begin.");
642     std::shared_ptr<SingleKvStore> kvStore;
643     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore);
644     ASSERT_EQ(status, Status::SUCCESS);
645     ASSERT_NE(kvStore, nullptr);
646 
647     // first close it if opened, and then delete it.
648     Status stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
649     EXPECT_EQ(stat, Status::SUCCESS);
650 }
651 
652 /**
653 * @tc.name: DeleteKvStore003
654 * @tc.desc: Delete a non-existing KvStore, and the callback function should return DB_ERROR.
655 * @tc.type: FUNC
656 * @tc.require: SR000CQDU0 AR000BVTDM
657 * @tc.author: liqiao
658 */
659 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore003, TestSize.Level1)
660 {
661     ZLOGI("DeleteKvStore003 begin.");
662     Status stat = manager.DeleteKvStore(appId, storeId64, create.baseDir);
663     EXPECT_EQ(stat, Status::STORE_NOT_FOUND);
664 }
665 
666 /**
667 * @tc.name: DeleteKvStore004
668 * @tc.desc: Delete a KvStore with an empty storeId, and the callback function should return INVALID_ARGUMENT.
669 * @tc.type: FUNC
670 * @tc.require: SR000CQDU0 AR000BVTDM
671 * @tc.author: liqiao
672 */
673 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore004, TestSize.Level1)
674 {
675     ZLOGI("DeleteKvStore004 begin.");
676     Status stat = manager.DeleteKvStore(appId, storeIdEmpty);
677     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
678 }
679 
680 /**
681 * @tc.name: DeleteKvStore005
682 * @tc.desc: Delete a KvStore with 65 bytes long storeId (which exceed storeId length limit). Should
683 * return INVALID_ARGUMENT.
684 * @tc.type: FUNC
685 * @tc.require: SR000CQDU0 AR000BVTDM
686 * @tc.author: liqiao
687 */
688 HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore005, TestSize.Level1)
689 {
690     ZLOGI("DeleteKvStore005 begin.");
691     Status stat = manager.DeleteKvStore(appId, storeId65);
692     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
693 }
694 
695 /**
696 * @tc.name: DeleteAllKvStore001
697 * @tc.desc: Delete all KvStores after closing all of them, and the callback function should return SUCCESS.
698 * @tc.type: FUNC
699 * @tc.require: SR000CQDU0 AR000BVTDM
700 * @tc.author: liqiao
701 */
702 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore001, TestSize.Level1)
703 {
704     ZLOGI("DeleteAllKvStore001 begin.");
705     std::shared_ptr<SingleKvStore> kvStore1;
706     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
707     ASSERT_EQ(status, Status::SUCCESS);
708     ASSERT_NE(kvStore1, nullptr);
709     std::shared_ptr<SingleKvStore> kvStore2;
710     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
711     ASSERT_EQ(status, Status::SUCCESS);
712     ASSERT_NE(kvStore2, nullptr);
713     Status stat = manager.CloseKvStore(appId, storeId64);
714     EXPECT_EQ(stat, Status::SUCCESS);
715     stat = manager.CloseKvStore(appId, storeIdTest);
716     EXPECT_EQ(stat, Status::SUCCESS);
717 
718     stat = manager.DeleteAllKvStore({""}, create.baseDir);
719     EXPECT_NE(stat, Status::SUCCESS);
720     stat = manager.DeleteAllKvStore(appId, "");
721     EXPECT_EQ(stat, Status::INVALID_ARGUMENT);
722 
723     stat = manager.DeleteAllKvStore(appId, create.baseDir);
724     EXPECT_EQ(stat, Status::SUCCESS);
725 }
726 
727 /**
728 * @tc.name: DeleteAllKvStore002
729 * @tc.desc: Delete all kvstore fail when any kvstore in the appId is not closed
730 * @tc.type: FUNC
731 * @tc.require: SR000CQDU0 AR000BVTDM
732 * @tc.author: liqiao
733 */
734 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore002, TestSize.Level1)
735 {
736     ZLOGI("DeleteAllKvStore002 begin.");
737     std::shared_ptr<SingleKvStore> kvStore1;
738     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
739     ASSERT_EQ(status, Status::SUCCESS);
740     ASSERT_NE(kvStore1, nullptr);
741     std::shared_ptr<SingleKvStore> kvStore2;
742     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
743     ASSERT_EQ(status, Status::SUCCESS);
744     ASSERT_NE(kvStore2, nullptr);
745     Status stat = manager.CloseKvStore(appId, storeId64);
746     EXPECT_EQ(stat, Status::SUCCESS);
747 
748     stat = manager.DeleteAllKvStore(appId, create.baseDir);
749     EXPECT_EQ(stat, Status::SUCCESS);
750 }
751 
752 /**
753 * @tc.name: DeleteAllKvStore003
754 * @tc.desc: Delete all KvStores even if no KvStore exists in the appId.
755 * @tc.type: FUNC
756 * @tc.require: SR000CQDU0 AR000BVTDM
757 * @tc.author: liqiao
758 */
759 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore003, TestSize.Level1)
760 {
761     ZLOGI("DeleteAllKvStore003 begin.");
762     Status stat = manager.DeleteAllKvStore(appId, create.baseDir);
763     EXPECT_EQ(stat, Status::SUCCESS);
764 }
765 
766 /**
767 * @tc.name: DeleteAllKvStore004
768 * @tc.desc: when delete the last active kvstore, the system will remove the app manager scene
769 * @tc.type: FUNC
770 * @tc.require: bugs
771 * @tc.author: Sven Wang
772 */
773 HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore004, TestSize.Level1)
774 {
775     ZLOGI("DeleteAllKvStore004 begin.");
776     std::shared_ptr<SingleKvStore> kvStore1;
777     Status status = manager.GetSingleKvStore(create, appId, storeId64, kvStore1);
778     ASSERT_EQ(status, Status::SUCCESS);
779     ASSERT_NE(kvStore1, nullptr);
780     std::shared_ptr<SingleKvStore> kvStore2;
781     status = manager.GetSingleKvStore(create, appId, storeIdTest, kvStore2);
782     ASSERT_EQ(status, Status::SUCCESS);
783     ASSERT_NE(kvStore2, nullptr);
784     Status stat = manager.CloseKvStore(appId, storeId64);
785     EXPECT_EQ(stat, Status::SUCCESS);
786     stat = manager.CloseKvStore(appId, storeIdTest);
787     EXPECT_EQ(stat, Status::SUCCESS);
788     stat = manager.DeleteKvStore(appId, storeIdTest, create.baseDir);
789     EXPECT_EQ(stat, Status::SUCCESS);
790     stat = manager.DeleteAllKvStore(appId, create.baseDir);
791     EXPECT_EQ(stat, Status::SUCCESS);
792 }
793 
794 /**
795 * @tc.name: PutSwitchWithEmptyAppId
796 * @tc.desc: put switch data, but appId is empty.
797 * @tc.type: FUNC
798 * @tc.require:
799 * @tc.author: zuojiangjiang
800 */
801 HWTEST_F(DistributedKvDataManagerTest, PutSwitchWithEmptyAppId, TestSize.Level1)
802 {
803     ZLOGI("PutSwitchWithEmptyAppId begin.");
804     SwitchData data;
805     Status status = manager.PutSwitch({ "" }, data);
806     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
807 }
808 
809 /**
810 * @tc.name: PutSwitchWithInvalidAppId
811 * @tc.desc: put switch data, but appId is not 'distributed_device_profile_service'.
812 * @tc.type: FUNC
813 * @tc.require:
814 * @tc.author: zuojiangjiang
815 */
816 HWTEST_F(DistributedKvDataManagerTest, PutSwitchWithInvalidAppId, TestSize.Level1)
817 {
818     ZLOGI("PutSwitchWithInvalidAppId begin.");
819     SwitchData data;
820     Status status = manager.PutSwitch({ "swicthes_test_appId" }, data);
821     ASSERT_EQ(status, Status::PERMISSION_DENIED);
822 }
823 
824 /**
825 * @tc.name: GetSwitchWithInvalidArg
826 * @tc.desc: get switch data, but appId is empty, networkId is invalid.
827 * @tc.type: FUNC
828 * @tc.require:
829 * @tc.author: zuojiangjiang
830 */
831 HWTEST_F(DistributedKvDataManagerTest, GetSwitchWithInvalidArg, TestSize.Level1)
832 {
833     ZLOGI("GetSwitchWithInvalidArg begin.");
834     auto [status1, data1] = manager.GetSwitch({ "" }, "networkId_test");
835     ASSERT_EQ(status1, Status::INVALID_ARGUMENT);
836     auto [status2, data2] = manager.GetSwitch({ "switches_test_appId" }, "");
837     ASSERT_EQ(status2, Status::INVALID_ARGUMENT);
838     auto [status3, data3] = manager.GetSwitch({ "switches_test_appId" }, "networkId_test");
839     ASSERT_EQ(status3, Status::INVALID_ARGUMENT);
840 }
841 
842 /**
843 * @tc.name: GetSwitchWithInvalidAppId
844 * @tc.desc: get switch data, but appId is not 'distributed_device_profile_service'.
845 * @tc.type: FUNC
846 * @tc.require:
847 * @tc.author: zuojiangjiang
848 */
849 HWTEST_F(DistributedKvDataManagerTest, GetSwitchWithInvalidAppId, TestSize.Level1)
850 {
851     ZLOGI("GetSwitchWithInvalidAppId begin.");
852     auto devInfo = DevManager::GetInstance().GetLocalDevice();
853     EXPECT_NE(devInfo.networkId, "");
854     auto [status, data] = manager.GetSwitch({ "switches_test_appId" }, devInfo.networkId);
855     ASSERT_EQ(status, Status::PERMISSION_DENIED);
856 }
857 
858 /**
859 * @tc.name: SubscribeSwitchesData
860 * @tc.desc: subscribe switch data.
861 * @tc.type: FUNC
862 * @tc.require:
863 * @tc.author: zuojiangjiang
864 */
865 HWTEST_F(DistributedKvDataManagerTest, SubscribeSwitchesData, TestSize.Level1)
866 {
867     ZLOGI("SubscribeSwitchesData begin.");
868     std::shared_ptr<SwitchDataObserver> observer = std::make_shared<SwitchDataObserver>();
869     auto status = manager.SubscribeSwitchData({ "switches_test_appId" }, observer);
870     ASSERT_EQ(status, Status::PERMISSION_DENIED);
871 }
872 
873 /**
874 * @tc.name: UnsubscribeSwitchesData
875 * @tc.desc: unsubscribe switch data.
876 * @tc.type: FUNC
877 * @tc.require:
878 * @tc.author: zuojiangjiang
879 */
880 HWTEST_F(DistributedKvDataManagerTest, UnsubscribeSwitchesData, TestSize.Level1)
881 {
882     ZLOGI("UnsubscribeSwitchesData begin.");
883     std::shared_ptr<SwitchDataObserver> observer = std::make_shared<SwitchDataObserver>();
884     auto status = manager.SubscribeSwitchData({ "switches_test_appId" }, observer);
885     ASSERT_EQ(status, Status::PERMISSION_DENIED);
886     status = manager.UnsubscribeSwitchData({ "switches_test_appId" }, observer);
887     ASSERT_EQ(status, Status::PERMISSION_DENIED);
888 }
889 } // namespace OHOS::Test