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