1 /*
2 * Copyright (c) 2024 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 "KvdbServiceImplTest"
16 #include "kvdb_service_impl.h"
17 
18 #include <gtest/gtest.h>
19 #include <vector>
20 
21 #include "accesstoken_kit.h"
22 #include "bootstrap.h"
23 #include "checker/checker_manager.h"
24 #include "device_manager_adapter.h"
25 #include "distributed_kv_data_manager.h"
26 #include "ipc_skeleton.h"
27 #include "kvdb_service_stub.h"
28 #include "kvstore_death_recipient.h"
29 #include "kvstore_meta_manager.h"
30 #include "log_print.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #include "types.h"
34 #include "utils/anonymous.h"
35 #include "utils/constant.h"
36 
37 using namespace testing::ext;
38 using namespace OHOS::DistributedData;
39 using namespace OHOS::Security::AccessToken;
40 using Action = OHOS::DistributedData::MetaDataManager::Action;
41 using AppId = OHOS::DistributedKv::AppId;
42 using ChangeType = OHOS::DistributedData::DeviceMatrix::ChangeType;
43 using DistributedKvDataManager = OHOS::DistributedKv::DistributedKvDataManager;
44 using DBStatus = DistributedDB::DBStatus;
45 using DBMode = DistributedDB::SyncMode;
46 using Options = OHOS::DistributedKv::Options;
47 using Status = OHOS::DistributedKv::Status;
48 using SingleKvStore = OHOS::DistributedKv::SingleKvStore;
49 using StoreId = OHOS::DistributedKv::StoreId;
50 using SyncInfo = OHOS::DistributedKv::KVDBService::SyncInfo;
51 using SyncMode = OHOS::DistributedKv::SyncMode;
52 using SyncAction = OHOS::DistributedKv::KVDBServiceImpl::SyncAction;
53 using SwitchState = OHOS::DistributedKv::SwitchState;
54 using UserId = OHOS::DistributedKv::UserId;
55 static OHOS::DistributedKv::StoreId storeId = { "kvdb_test_storeid" };
56 static OHOS::DistributedKv::AppId appId = { "ohos.test.kvdb" };
57 
58 namespace OHOS::Test {
59 namespace DistributedDataTest {
60 class KvdbServiceImplTest : public testing::Test {
61 public:
62     static constexpr size_t NUM_MIN = 5;
63     static constexpr size_t NUM_MAX = 12;
64     static DistributedKvDataManager manager;
65     static Options create;
66     static UserId userId;
67 
68     std::shared_ptr<SingleKvStore> kvStore;
69 
70     static AppId appId;
71     static StoreId storeId64;
72     static StoreId storeId65;
73 
74     static void SetUpTestCase(void);
75     static void TearDownTestCase(void);
76     static void RemoveAllStore(OHOS::DistributedKv::DistributedKvDataManager &manager);
77     void SetUp();
78     void TearDown();
79 
80     KvdbServiceImplTest();
81 
82 protected:
83     std::shared_ptr<DistributedKv::KVDBServiceImpl> kvdbServiceImpl_;
84 };
85 
86 OHOS::DistributedKv::DistributedKvDataManager KvdbServiceImplTest::manager;
87 Options KvdbServiceImplTest::create;
88 UserId KvdbServiceImplTest::userId;
89 
90 AppId KvdbServiceImplTest::appId;
91 StoreId KvdbServiceImplTest::storeId64;
92 StoreId KvdbServiceImplTest::storeId65;
93 
RemoveAllStore(DistributedKvDataManager &manager)94 void KvdbServiceImplTest::RemoveAllStore(DistributedKvDataManager &manager)
95 {
96     manager.CloseAllKvStore(appId);
97     manager.DeleteAllKvStore(appId, create.baseDir);
98 }
99 
SetUpTestCase(void)100 void KvdbServiceImplTest::SetUpTestCase(void)
101 {
102     auto executors = std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
103     manager.SetExecutors(executors);
104     userId.userId = "kvdbserviceimpltest1";
105     appId.appId = "ohos.kvdbserviceimpl.test";
106     create.createIfMissing = true;
107     create.encrypt = false;
108     create.securityLevel = OHOS::DistributedKv::S1;
109     create.autoSync = true;
110     create.kvStoreType = OHOS::DistributedKv::SINGLE_VERSION;
111     create.area = OHOS::DistributedKv::EL1;
112     create.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
113     mkdir(create.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
114 
115     storeId64.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000";
116     storeId65.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000"
117                         "a000000000b000000000c000000000d000000000e000000000f000000000g0000";
118     RemoveAllStore(manager);
119 }
120 
TearDownTestCase()121 void KvdbServiceImplTest::TearDownTestCase()
122 {
123     RemoveAllStore(manager);
124     (void)remove((create.baseDir + "/kvdb").c_str());
125     (void)remove(create.baseDir.c_str());
126 }
127 
SetUp(void)128 void KvdbServiceImplTest::SetUp(void)
129 {
130     kvdbServiceImpl_ = std::make_shared<DistributedKv::KVDBServiceImpl>();
131 }
132 
TearDown(void)133 void KvdbServiceImplTest::TearDown(void)
134 {
135     RemoveAllStore(manager);
136 }
137 
KvdbServiceImplTest(void)138 KvdbServiceImplTest::KvdbServiceImplTest(void) {}
139 
140 /**
141 * @tc.name: KvdbServiceImpl001
142 * @tc.desc: KvdbServiceImplTest function test.
143 * @tc.type: FUNC
144 * @tc.author: SQL
145 */
HWTEST_F(KvdbServiceImplTest, KvdbServiceImpl001, TestSize.Level0)146 HWTEST_F(KvdbServiceImplTest, KvdbServiceImpl001, TestSize.Level0)
147 {
148     std::string device = "OH_device_test";
149     StoreId id1;
150     id1.storeId = "id1";
151     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
152     EXPECT_NE(kvStore, nullptr);
153     EXPECT_EQ(status, Status::SUCCESS);
154     int32_t result = kvdbServiceImpl_->OnInitialize();
155     EXPECT_EQ(result, Status::SUCCESS);
156     FeatureSystem::Feature::BindInfo bindInfo;
157     result = kvdbServiceImpl_->OnBind(bindInfo);
158     EXPECT_EQ(result, Status::SUCCESS);
159     result = kvdbServiceImpl_->Online(device);
160     EXPECT_EQ(result, Status::SUCCESS);
161     status = kvdbServiceImpl_->SubscribeSwitchData(appId);
162     EXPECT_EQ(status, Status::SUCCESS);
163     SyncInfo syncInfo;
164     status = kvdbServiceImpl_->CloudSync(appId, id1, syncInfo);
165     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
166 
167     DistributedKv::StoreConfig storeConfig;
168     status = kvdbServiceImpl_->SetConfig(appId, id1, storeConfig);
169     EXPECT_EQ(status, Status::SUCCESS);
170     status = kvdbServiceImpl_->NotifyDataChange(appId, id1, 0);
171     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
172 
173     status = kvdbServiceImpl_->UnsubscribeSwitchData(appId);
174     EXPECT_EQ(status, Status::SUCCESS);
175     status = kvdbServiceImpl_->Close(appId, id1);
176     EXPECT_EQ(status, Status::SUCCESS);
177 }
178 
179 /**
180 * @tc.name: GetStoreIdsTest001
181 * @tc.desc: GetStoreIds
182 * @tc.type: FUNC
183 * @tc.author: wangbin
184 */
HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest001, TestSize.Level0)185 HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest001, TestSize.Level0)
186 {
187     ZLOGI("GetStoreIdsTest001 start");
188     StoreId id1;
189     id1.storeId = "id1";
190     StoreId id2;
191     id2.storeId = "id2";
192     StoreId id3;
193     id3.storeId = "id3";
194     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
195     ASSERT_NE(kvStore, nullptr);
196     ASSERT_EQ(status, Status::SUCCESS);
197     status = manager.GetSingleKvStore(create, appId, id2, kvStore);
198     ASSERT_NE(kvStore, nullptr);
199     ASSERT_EQ(status, Status::SUCCESS);
200     status = manager.GetSingleKvStore(create, appId, id3, kvStore);
201     ASSERT_NE(kvStore, nullptr);
202     ASSERT_EQ(status, Status::SUCCESS);
203     std::vector<StoreId> storeIds;
204     status = kvdbServiceImpl_->GetStoreIds(appId, storeIds);
205     ASSERT_EQ(status, Status::SUCCESS);
206 }
207 
208 /**
209 * @tc.name: GetStoreIdsTest002
210 * @tc.desc: GetStoreIds
211 * @tc.type: FUNC
212 * @tc.author: wangbin
213 */
HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest002, TestSize.Level0)214 HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest002, TestSize.Level0)
215 {
216     ZLOGI("GetStoreIdsTest002 start");
217     std::vector<StoreId> storeIds;
218     AppId appId01;
219     auto status = kvdbServiceImpl_->GetStoreIds(appId01, storeIds);
220     ZLOGI("GetStoreIdsTest002 status = :%{public}d", status);
221     ASSERT_EQ(status, Status::SUCCESS);
222 }
223 
224 /**
225 * @tc.name: DeleteTest001
226 * @tc.desc: GetStoreIds
227 * @tc.type: FUNC
228 * @tc.author: wangbin
229 */
HWTEST_F(KvdbServiceImplTest, DeleteTest001, TestSize.Level0)230 HWTEST_F(KvdbServiceImplTest, DeleteTest001, TestSize.Level0)
231 {
232     ZLOGI("DeleteTest001 start");
233     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
234     ASSERT_NE(kvStore, nullptr);
235     ASSERT_EQ(status1, Status::SUCCESS);
236     auto status = kvdbServiceImpl_->Delete(appId, storeId);
237     ZLOGI("DeleteTest001 status = :%{public}d", status);
238     ASSERT_EQ(status, Status::SUCCESS);
239 }
240 
241 /**
242 * @tc.name: DeleteTest002
243 * @tc.desc: GetStoreIds
244 * @tc.type: FUNC
245 * @tc.author: wangbin
246 */
HWTEST_F(KvdbServiceImplTest, DeleteTest002, TestSize.Level0)247 HWTEST_F(KvdbServiceImplTest, DeleteTest002, TestSize.Level0)
248 {
249     ZLOGI("DeleteTest002 start");
250     AppId appId01 = { "ohos.kvdbserviceimpl.test01" };
251     StoreId storeId01 = { "meta_test_storeid" };
252     auto status = kvdbServiceImpl_->Delete(appId01, storeId01);
253     ZLOGI("DeleteTest002 status = :%{public}d", status);
254     ASSERT_EQ(status, Status::SUCCESS);
255 }
256 
257 /**
258 * @tc.name: syncTest001
259 * @tc.desc: GetStoreIds
260 * @tc.type: FUNC
261 * @tc.author: wangbin
262 */
HWTEST_F(KvdbServiceImplTest, syncTest001, TestSize.Level0)263 HWTEST_F(KvdbServiceImplTest, syncTest001, TestSize.Level0)
264 {
265     ZLOGI("syncTest001 start");
266     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
267     ASSERT_NE(kvStore, nullptr);
268     ASSERT_EQ(status1, Status::SUCCESS);
269     SyncInfo syncInfo;
270     auto status = kvdbServiceImpl_->Sync(appId, storeId, syncInfo);
271     ZLOGI("syncTest001 status = :%{public}d", status);
272     ASSERT_NE(status, Status::SUCCESS);
273 }
274 
275 /**
276 * @tc.name: RegisterSyncCallbackTest001
277 * @tc.desc: GetStoreIds
278 * @tc.type: FUNC
279 * @tc.author: wangbin
280 */
HWTEST_F(KvdbServiceImplTest, RegisterSyncCallbackTest001, TestSize.Level0)281 HWTEST_F(KvdbServiceImplTest, RegisterSyncCallbackTest001, TestSize.Level0)
282 {
283     ZLOGI("RegisterSyncCallbackTest001 start");
284     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
285     ASSERT_NE(kvStore, nullptr);
286     ASSERT_EQ(status1, Status::SUCCESS);
287     sptr<OHOS::DistributedKv::IKVDBNotifier> notifier;
288     auto status = kvdbServiceImpl_->RegServiceNotifier(appId, notifier);
289     ZLOGI("RegisterSyncCallbackTest001 status = :%{public}d", status);
290     ASSERT_EQ(status, Status::SUCCESS);
291 }
292 
293 /**
294 * @tc.name: UnregisterSyncCallbackTest001
295 * @tc.desc: GetStoreIds
296 * @tc.type: FUNC
297 * @tc.author: wangbin
298 */
HWTEST_F(KvdbServiceImplTest, UnregisterSyncCallbackTest001, TestSize.Level0)299 HWTEST_F(KvdbServiceImplTest, UnregisterSyncCallbackTest001, TestSize.Level0)
300 {
301     ZLOGI("UnregisterSyncCallbackTest001 start");
302     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
303     ASSERT_NE(kvStore, nullptr);
304     ASSERT_EQ(status1, Status::SUCCESS);
305     auto status = kvdbServiceImpl_->UnregServiceNotifier(appId);
306     ZLOGI("UnregisterSyncCallbackTest001 status = :%{public}d", status);
307     ASSERT_EQ(status, Status::SUCCESS);
308 }
309 
310 /**
311 * @tc.name: SetSyncParamTest001
312 * @tc.desc: GetStoreIds
313 * @tc.type: FUNC
314 * @tc.author: wangbin
315 */
HWTEST_F(KvdbServiceImplTest, SetSyncParamTest001, TestSize.Level0)316 HWTEST_F(KvdbServiceImplTest, SetSyncParamTest001, TestSize.Level0)
317 {
318     ZLOGI("SetSyncParamTest001 start");
319     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
320     ASSERT_NE(kvStore, nullptr);
321     ASSERT_EQ(status1, Status::SUCCESS);
322     OHOS::DistributedKv::KvSyncParam const syncparam;
323     auto status = kvdbServiceImpl_->SetSyncParam(appId, storeId, syncparam);
324     ZLOGI("SetSyncParamTest001 status = :%{public}d", status);
325     ASSERT_EQ(status, Status::SUCCESS);
326 }
327 
328 /**
329 * @tc.name: GetSyncParamTest001
330 * @tc.desc: GetStoreIds
331 * @tc.type: FUNC
332 * @tc.author: wangbin
333 */
HWTEST_F(KvdbServiceImplTest, GetSyncParamTest001, TestSize.Level0)334 HWTEST_F(KvdbServiceImplTest, GetSyncParamTest001, TestSize.Level0)
335 {
336     ZLOGI("GetSyncParamTest001 start");
337     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
338     ASSERT_NE(kvStore, nullptr);
339     ASSERT_EQ(status1, Status::SUCCESS);
340     OHOS::DistributedKv::KvSyncParam syncparam;
341     auto status = kvdbServiceImpl_->GetSyncParam(appId, storeId, syncparam);
342     ZLOGI("GetSyncParamTest001 status = :%{public}d", status);
343     ASSERT_EQ(status, Status::SUCCESS);
344 }
345 
346 /**
347 * @tc.name: EnableCapabilityTest001
348 * @tc.desc: GetStoreIds
349 * @tc.type: FUNC
350 * @tc.author: wangbin
351 */
HWTEST_F(KvdbServiceImplTest, EnableCapabilityTest001, TestSize.Level0)352 HWTEST_F(KvdbServiceImplTest, EnableCapabilityTest001, TestSize.Level0)
353 {
354     ZLOGI("EnableCapabilityTest001 start");
355     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
356     ASSERT_NE(kvStore, nullptr);
357     ASSERT_EQ(status1, Status::SUCCESS);
358     auto status = kvdbServiceImpl_->EnableCapability(appId, storeId);
359     ZLOGI("EnableCapabilityTest001 status = :%{public}d", status);
360     ASSERT_EQ(status, Status::SUCCESS);
361 }
362 
363 /**
364 * @tc.name: DisableCapabilityTest001
365 * @tc.desc: GetStoreIds
366 * @tc.type: FUNC
367 * @tc.author: wangbin
368 */
HWTEST_F(KvdbServiceImplTest, DisableCapabilityTest001, TestSize.Level0)369 HWTEST_F(KvdbServiceImplTest, DisableCapabilityTest001, TestSize.Level0)
370 {
371     ZLOGI("DisableCapabilityTest001 start");
372     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
373     ASSERT_NE(kvStore, nullptr);
374     ASSERT_EQ(status1, Status::SUCCESS);
375     auto status = kvdbServiceImpl_->DisableCapability(appId, storeId);
376     ZLOGI("DisableCapabilityTest001 status = :%{public}d", status);
377     ASSERT_EQ(status, Status::SUCCESS);
378 }
379 
380 /**
381 * @tc.name: SetCapabilityTest001
382 * @tc.desc: GetStoreIds
383 * @tc.type: FUNC
384 * @tc.author: wangbin
385 */
HWTEST_F(KvdbServiceImplTest, SetCapabilityTest001, TestSize.Level0)386 HWTEST_F(KvdbServiceImplTest, SetCapabilityTest001, TestSize.Level0)
387 {
388     ZLOGI("SetCapabilityTest001 start");
389     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
390     ASSERT_NE(kvStore, nullptr);
391     ASSERT_EQ(status1, Status::SUCCESS);
392     std::vector<std::string> local;
393     std::vector<std::string> remote;
394     auto status = kvdbServiceImpl_->SetCapability(appId, storeId, local, remote);
395     ZLOGI("SetCapabilityTest001 status = :%{public}d", status);
396     ASSERT_EQ(status, Status::SUCCESS);
397 }
398 
399 /**
400 * @tc.name: AddSubscribeInfoTest001
401 * @tc.desc: GetStoreIds
402 * @tc.type: FUNC
403 * @tc.author: wangbin
404 */
HWTEST_F(KvdbServiceImplTest, AddSubscribeInfoTest001, TestSize.Level0)405 HWTEST_F(KvdbServiceImplTest, AddSubscribeInfoTest001, TestSize.Level0)
406 {
407     ZLOGI("AddSubscribeInfoTest001 start");
408     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
409     ASSERT_NE(kvStore, nullptr);
410     ASSERT_EQ(status1, Status::SUCCESS);
411     SyncInfo syncInfo;
412     auto status = kvdbServiceImpl_->AddSubscribeInfo(appId, storeId, syncInfo);
413     ZLOGI("AddSubscribeInfoTest001 status = :%{public}d", status);
414     ASSERT_NE(status, Status::SUCCESS);
415 }
416 
417 /**
418 * @tc.name: RmvSubscribeInfoTest001
419 * @tc.desc: GetStoreIds
420 * @tc.type: FUNC
421 * @tc.author: wangbin
422 */
HWTEST_F(KvdbServiceImplTest, RmvSubscribeInfoTest001, TestSize.Level0)423 HWTEST_F(KvdbServiceImplTest, RmvSubscribeInfoTest001, TestSize.Level0)
424 {
425     ZLOGI("RmvSubscribeInfoTest001 start");
426     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
427     ASSERT_NE(kvStore, nullptr);
428     ASSERT_EQ(status1, Status::SUCCESS);
429     SyncInfo syncInfo;
430     auto status = kvdbServiceImpl_->RmvSubscribeInfo(appId, storeId, syncInfo);
431     ZLOGI("RmvSubscribeInfoTest001 status = :%{public}d", status);
432     ASSERT_NE(status, Status::SUCCESS);
433 }
434 
435 /**
436 * @tc.name: SubscribeTest001
437 * @tc.desc: GetStoreIds
438 * @tc.type: FUNC
439 * @tc.author: wangbin
440 */
HWTEST_F(KvdbServiceImplTest, SubscribeTest001, TestSize.Level0)441 HWTEST_F(KvdbServiceImplTest, SubscribeTest001, TestSize.Level0)
442 {
443     ZLOGI("SubscribeTest001 start");
444     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
445     ASSERT_NE(kvStore, nullptr);
446     ASSERT_EQ(status1, Status::SUCCESS);
447     sptr<OHOS::DistributedKv::IKvStoreObserver> observer;
448     auto status = kvdbServiceImpl_->Subscribe(appId, storeId, observer);
449     ZLOGI("SubscribeTest001 status = :%{public}d", status);
450     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
451 }
452 
453 /**
454 * @tc.name: UnsubscribeTest001
455 * @tc.desc: GetStoreIds
456 * @tc.type: FUNC
457 * @tc.author: wangbin
458 */
HWTEST_F(KvdbServiceImplTest, UnsubscribeTest001, TestSize.Level0)459 HWTEST_F(KvdbServiceImplTest, UnsubscribeTest001, TestSize.Level0)
460 {
461     ZLOGI("UnsubscribeTest001 start");
462     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
463     ASSERT_NE(kvStore, nullptr);
464     ASSERT_EQ(status1, Status::SUCCESS);
465     sptr<OHOS::DistributedKv::IKvStoreObserver> observer;
466     auto status = kvdbServiceImpl_->Unsubscribe(appId, storeId, observer);
467     ZLOGI("UnsubscribeTest001 status = :%{public}d", status);
468     ASSERT_EQ(status, Status::SUCCESS);
469 }
470 
471 /**
472 * @tc.name: GetBackupPasswordTest001
473 * @tc.desc: GetStoreIds
474 * @tc.type: FUNC
475 * @tc.author: wangbin
476 */
HWTEST_F(KvdbServiceImplTest, GetBackupPasswordTest001, TestSize.Level0)477 HWTEST_F(KvdbServiceImplTest, GetBackupPasswordTest001, TestSize.Level0)
478 {
479     ZLOGI("GetBackupPasswordTest001 start");
480     auto status = manager.GetSingleKvStore(create, appId, storeId, kvStore);
481     ASSERT_NE(kvStore, nullptr);
482     ASSERT_EQ(status, Status::SUCCESS);
483 }
484 
485 /**
486 * @tc.name: BeforeCreateTest001
487 * @tc.desc: GetStoreIds
488 * @tc.type: FUNC
489 * @tc.author: wangbin
490 */
HWTEST_F(KvdbServiceImplTest, BeforeCreateTest001, TestSize.Level0)491 HWTEST_F(KvdbServiceImplTest, BeforeCreateTest001, TestSize.Level0)
492 {
493     ZLOGI("BeforeCreateTest001 start");
494     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
495     ASSERT_NE(kvStore, nullptr);
496     ASSERT_EQ(status1, Status::SUCCESS);
497     auto status = kvdbServiceImpl_->BeforeCreate(appId, storeId, create);
498     ZLOGI("BeforeCreateTest001 status = :%{public}d", status);
499     ASSERT_EQ(status, Status::SUCCESS);
500 }
501 
502 /**
503 * @tc.name: AfterCreateTest001
504 * @tc.desc: GetStoreIds
505 * @tc.type: FUNC
506 * @tc.author: wangbin
507 */
HWTEST_F(KvdbServiceImplTest, AfterCreateTest001, TestSize.Level0)508 HWTEST_F(KvdbServiceImplTest, AfterCreateTest001, TestSize.Level0)
509 {
510     ZLOGI("AfterCreateTest001 start");
511     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
512     ASSERT_NE(kvStore, nullptr);
513     ASSERT_EQ(status1, Status::SUCCESS);
514     std::vector<uint8_t> password;
515     auto status = kvdbServiceImpl_->AfterCreate(appId, storeId, create, password);
516     ZLOGI("AfterCreateTest001 status = :%{public}d", status);
517     ASSERT_EQ(status, Status::SUCCESS);
518 }
519 
520 /**
521 * @tc.name: OnAppExitTest001
522 * @tc.desc: GetStoreIds
523 * @tc.type: FUNC
524 * @tc.author: wangbin
525 */
HWTEST_F(KvdbServiceImplTest, OnAppExitTest001, TestSize.Level0)526 HWTEST_F(KvdbServiceImplTest, OnAppExitTest001, TestSize.Level0)
527 {
528     ZLOGI("OnAppExitTest001 start");
529     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
530     ASSERT_NE(kvStore, nullptr);
531     ASSERT_EQ(status1, Status::SUCCESS);
532     pid_t uid = 1;
533     pid_t pid = 2;
534     const char **perms = new const char *[2];
535     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
536     perms[1] = "ohos.permission.ACCESS_SERVICE_DM";
537     TokenInfoParams infoInstance = {
538         .dcapsNum = 0,
539         .permsNum = 2,
540         .aclsNum = 0,
541         .dcaps = nullptr,
542         .perms = perms,
543         .acls = nullptr,
544         .processName = "distributed_data_test",
545         .aplStr = "system_basic",
546     };
547     uint32_t tokenId = GetAccessTokenId(&infoInstance);
548     auto status = kvdbServiceImpl_->OnAppExit(uid, pid, tokenId, appId);
549     ZLOGI("OnAppExitTest001 status = :%{public}d", status);
550     ASSERT_EQ(status, Status::SUCCESS);
551 }
552 
553 /**
554 * @tc.name: OnUserChangeTest001
555 * @tc.desc: GetStoreIds
556 * @tc.type: FUNC
557 * @tc.author: wangbin
558 */
HWTEST_F(KvdbServiceImplTest, OnUserChangeTest001, TestSize.Level0)559 HWTEST_F(KvdbServiceImplTest, OnUserChangeTest001, TestSize.Level0)
560 {
561     ZLOGI("OnUserChangeTest001 start");
562     uint32_t code = 1;
563     std::string user = "OH_USER_test";
564     std::string account = "OH_ACCOUNT_test";
565     auto status = kvdbServiceImpl_->OnUserChange(code, user, account);
566     ZLOGI("OnUserChangeTest001 status = :%{public}d", status);
567     ASSERT_EQ(status, Status::SUCCESS);
568 }
569 
570 /**
571 * @tc.name: OnReadyTest001
572 * @tc.desc: GetStoreIds
573 * @tc.type: FUNC
574 * @tc.author: wangbin
575 */
HWTEST_F(KvdbServiceImplTest, OnReadyTest001, TestSize.Level0)576 HWTEST_F(KvdbServiceImplTest, OnReadyTest001, TestSize.Level0)
577 {
578     ZLOGI("OnReadyTest001 start");
579     std::string device = "OH_device_test";
580     auto status = kvdbServiceImpl_->OnReady(device);
581     ZLOGI("OnReadyTest001 status = :%{public}d", status);
582     ASSERT_EQ(status, Status::SUCCESS);
583     status = kvdbServiceImpl_->OnSessionReady(device);
584     ASSERT_EQ(status, Status::SUCCESS);
585 }
586 
587 /**
588 * @tc.name: ResolveAutoLaunch
589 * @tc.desc: ResolveAutoLaunch function test.
590 * @tc.type: FUNC
591 * @tc.author: SQL
592 */
HWTEST_F(KvdbServiceImplTest, ResolveAutoLaunch, TestSize.Level0)593 HWTEST_F(KvdbServiceImplTest, ResolveAutoLaunch, TestSize.Level0)
594 {
595     StoreId id1;
596     id1.storeId = "id1";
597     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
598     EXPECT_NE(kvStore, nullptr);
599     EXPECT_EQ(status, Status::SUCCESS);
600     std::string identifier = "identifier";
601     DistributedKv::KVDBServiceImpl::DBLaunchParam launchParam;
602     auto result = kvdbServiceImpl_->ResolveAutoLaunch(identifier, launchParam);
603     EXPECT_EQ(result, Status::STORE_NOT_FOUND);
604     std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(1, 0);
605     Bootstrap::GetInstance().LoadDirectory();
606     Bootstrap::GetInstance().LoadCheckers();
607     DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors);
608     DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter();
609     DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener();
610     result = kvdbServiceImpl_->ResolveAutoLaunch(identifier, launchParam);
611     EXPECT_EQ(result, Status::SUCCESS);
612 }
613 
614 /**
615 * @tc.name: PutSwitch
616 * @tc.desc: PutSwitch function test.
617 * @tc.type: FUNC
618 * @tc.author: SQL
619 */
HWTEST_F(KvdbServiceImplTest, PutSwitch, TestSize.Level0)620 HWTEST_F(KvdbServiceImplTest, PutSwitch, TestSize.Level0)
621 {
622     StoreId id1;
623     id1.storeId = "id1";
624     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
625     ASSERT_NE(kvStore, nullptr);
626     ASSERT_EQ(status, Status::SUCCESS);
627     DistributedKv::SwitchData switchData;
628     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
629     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
630     switchData.value = DeviceMatrix::INVALID_VALUE;
631     switchData.length = DeviceMatrix::INVALID_LEVEL;
632     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
633     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
634     switchData.value = DeviceMatrix::INVALID_MASK;
635     switchData.length = DeviceMatrix::INVALID_LENGTH;
636     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
637     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
638     switchData.value = DeviceMatrix::INVALID_VALUE;
639     switchData.length = DeviceMatrix::INVALID_LENGTH;
640     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
641     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
642     switchData.value = DeviceMatrix::INVALID_MASK;
643     switchData.length = DeviceMatrix::INVALID_LEVEL;
644     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
645     EXPECT_EQ(status, Status::SUCCESS);
646     std::string networkId = "networkId";
647     status = kvdbServiceImpl_->GetSwitch(appId, networkId, switchData);
648     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
649 }
650 
651 /**
652 * @tc.name: DoCloudSync
653 * @tc.desc: DoCloudSync error function test.
654 * @tc.type: FUNC
655 * @tc.author: SQL
656 */
HWTEST_F(KvdbServiceImplTest, DoCloudSync, TestSize.Level0)657 HWTEST_F(KvdbServiceImplTest, DoCloudSync, TestSize.Level0)
658 {
659     StoreId id1;
660     id1.storeId = "id1";
661     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
662     ASSERT_NE(kvStore, nullptr);
663     ASSERT_EQ(status, Status::SUCCESS);
664     StoreMetaData metaData;
665     SyncInfo syncInfo;
666     status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
667     EXPECT_EQ(status, Status::NOT_SUPPORT);
668     syncInfo.devices = { "device1", "device2" };
669     syncInfo.query = "query";
670     metaData.enableCloud = true;
671     status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
672     EXPECT_EQ(status, Status::CLOUD_DISABLED);
673 }
674 
675 /**
676 * @tc.name: ConvertDbStatus
677 * @tc.desc: ConvertDbStatus test the return result of input with different values.
678 * @tc.type: FUNC
679 * @tc.author: SQL
680 */
HWTEST_F(KvdbServiceImplTest, ConvertDbStatus, TestSize.Level0)681 HWTEST_F(KvdbServiceImplTest, ConvertDbStatus, TestSize.Level0)
682 {
683     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::BUSY), Status::DB_ERROR);
684     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::DB_ERROR), Status::DB_ERROR);
685     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::OK), Status::SUCCESS);
686     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_ARGS), Status::INVALID_ARGUMENT);
687     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::NOT_FOUND), Status::KEY_NOT_FOUND);
688     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_VALUE_FIELDS), Status::INVALID_VALUE_FIELDS);
689     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_FIELD_TYPE), Status::INVALID_FIELD_TYPE);
690     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::CONSTRAIN_VIOLATION), Status::CONSTRAIN_VIOLATION);
691     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_FORMAT), Status::INVALID_FORMAT);
692     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_QUERY_FORMAT), Status::INVALID_QUERY_FORMAT);
693     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_QUERY_FIELD), Status::INVALID_QUERY_FIELD);
694     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::NOT_SUPPORT), Status::NOT_SUPPORT);
695     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::TIME_OUT), Status::TIME_OUT);
696     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::OVER_MAX_LIMITS), Status::OVER_MAX_LIMITS);
697     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::EKEYREVOKED_ERROR), Status::SECURITY_LEVEL_ERROR);
698     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::SECURITY_OPTION_CHECK_ERROR), Status::SECURITY_LEVEL_ERROR);
699     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::SCHEMA_VIOLATE_VALUE), Status::ERROR);
700 }
701 
702 /**
703 * @tc.name: ConvertDBMode
704 * @tc.desc: ConvertDBMode test the return result of input with different values.
705 * @tc.type: FUNC
706 * @tc.author: SQL
707 */
HWTEST_F(KvdbServiceImplTest, ConvertDBMode, TestSize.Level0)708 HWTEST_F(KvdbServiceImplTest, ConvertDBMode, TestSize.Level0)
709 {
710     auto status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PUSH);
711     EXPECT_EQ(status, DBMode::SYNC_MODE_PUSH_ONLY);
712     status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PULL);
713     EXPECT_EQ(status, DBMode::SYNC_MODE_PULL_ONLY);
714     status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PUSH_PULL);
715     EXPECT_EQ(status, DBMode::SYNC_MODE_PUSH_PULL);
716 }
717 
718 /**
719 * @tc.name: ConvertGeneralSyncMode
720 * @tc.desc: ConvertGeneralSyncMode test the return result of input with different values.
721 * @tc.type: FUNC
722 * @tc.author: SQL
723 */
HWTEST_F(KvdbServiceImplTest, ConvertGeneralSyncMode, TestSize.Level0)724 HWTEST_F(KvdbServiceImplTest, ConvertGeneralSyncMode, TestSize.Level0)
725 {
726     auto status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_SUBSCRIBE);
727     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE);
728     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_UNSUBSCRIBE);
729     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE);
730     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_SYNC);
731     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PUSH);
732     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PULL, SyncAction::ACTION_SYNC);
733     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PULL);
734     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH_PULL, SyncAction::ACTION_SYNC);
735     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PULL_PUSH);
736     auto action = static_cast<SyncAction>(SyncAction::ACTION_UNSUBSCRIBE + 1);
737     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, action);
738     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_END);
739 }
740 
741 /**
742 * @tc.name: ConvertType
743 * @tc.desc: ConvertType test the return result of input with different values.
744 * @tc.type: FUNC
745 * @tc.author: SQL
746 */
HWTEST_F(KvdbServiceImplTest, ConvertType, TestSize.Level0)747 HWTEST_F(KvdbServiceImplTest, ConvertType, TestSize.Level0)
748 {
749     auto status = kvdbServiceImpl_->ConvertType(SyncMode::PUSH);
750     EXPECT_EQ(status, ChangeType::CHANGE_LOCAL);
751     status = kvdbServiceImpl_->ConvertType(SyncMode::PULL);
752     EXPECT_EQ(status, ChangeType::CHANGE_REMOTE);
753     status = kvdbServiceImpl_->ConvertType(SyncMode::PUSH_PULL);
754     EXPECT_EQ(status, ChangeType::CHANGE_ALL);
755     auto action = static_cast<SyncMode>(SyncMode::PUSH_PULL + 1);
756     status = kvdbServiceImpl_->ConvertType(action);
757     EXPECT_EQ(status, ChangeType::CHANGE_ALL);
758 }
759 
760 /**
761 * @tc.name: ConvertAction
762 * @tc.desc: ConvertAction test the return result of input with different values.
763 * @tc.type: FUNC
764 * @tc.author: SQL
765 */
HWTEST_F(KvdbServiceImplTest, ConvertAction, TestSize.Level0)766 HWTEST_F(KvdbServiceImplTest, ConvertAction, TestSize.Level0)
767 {
768     auto status = kvdbServiceImpl_->ConvertAction(Action::INSERT);
769     EXPECT_EQ(status, SwitchState::INSERT);
770     status = kvdbServiceImpl_->ConvertAction(Action::UPDATE);
771     EXPECT_EQ(status, SwitchState::UPDATE);
772     status = kvdbServiceImpl_->ConvertAction(Action::DELETE);
773     EXPECT_EQ(status, SwitchState::DELETE);
774     auto action = static_cast<Action>(Action::DELETE + 1);
775     status = kvdbServiceImpl_->ConvertAction(action);
776     EXPECT_EQ(status, SwitchState::INSERT);
777 }
778 
779 /**
780 * @tc.name: GetSyncMode
781 * @tc.desc: GetSyncMode test the return result of input with different values.
782 * @tc.type: FUNC
783 * @tc.author: SQL
784 */
HWTEST_F(KvdbServiceImplTest, GetSyncMode, TestSize.Level0)785 HWTEST_F(KvdbServiceImplTest, GetSyncMode, TestSize.Level0)
786 {
787     auto status = kvdbServiceImpl_->GetSyncMode(true, true);
788     EXPECT_EQ(status, SyncMode::PUSH_PULL);
789     status = kvdbServiceImpl_->GetSyncMode(true, false);
790     EXPECT_EQ(status, SyncMode::PUSH);
791     status = kvdbServiceImpl_->GetSyncMode(false, true);
792     EXPECT_EQ(status, SyncMode::PULL);
793     status = kvdbServiceImpl_->GetSyncMode(false, false);
794     EXPECT_EQ(status, SyncMode::PUSH_PULL);
795 }
796 } // namespace DistributedDataTest
797 } // namespace OHOS::Test