• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include <limits>
20 #include <vector>
21 
22 #include "bootstrap.h"
23 #include "checker/checker_manager.h"
24 #include "cloud/cloud_event.h"
25 #include "cloud/cloud_server.h"
26 #include "device_manager_adapter.h"
27 #include "distributed_kv_data_manager.h"
28 #include "event_center.h"
29 #include "ipc_skeleton.h"
30 #include "kvdb_query.h"
31 #include "kvdb_service_impl.h"
32 #include "kvdb_service_stub.h"
33 #include "kvstore_death_recipient.h"
34 #include "kvstore_meta_manager.h"
35 #include "kvstore_sync_manager.h"
36 #include "log_print.h"
37 #include "mock/access_token_mock.h"
38 #include "mock/meta_data_manager_mock.h"
39 #include "nativetoken_kit.h"
40 #include "network/network_delegate.h"
41 #include "network_delegate_mock.h"
42 #include "token_setproc.h"
43 #include "types.h"
44 #include "utils/anonymous.h"
45 #include "utils/constant.h"
46 
47 using namespace testing::ext;
48 using namespace OHOS::DistributedData;
49 using namespace OHOS::Security::AccessToken;
50 using Action = OHOS::DistributedData::MetaDataManager::Action;
51 using AppId = OHOS::DistributedKv::AppId;
52 using ChangeType = OHOS::DistributedData::DeviceMatrix::ChangeType;
53 using DistributedKvDataManager = OHOS::DistributedKv::DistributedKvDataManager;
54 using DBStatus = DistributedDB::DBStatus;
55 using DBMode = DistributedDB::SyncMode;
56 using Options = OHOS::DistributedKv::Options;
57 using Status = OHOS::DistributedKv::Status;
58 using SingleKvStore = OHOS::DistributedKv::SingleKvStore;
59 using StoreId = OHOS::DistributedKv::StoreId;
60 using SyncInfo = OHOS::DistributedKv::KVDBService::SyncInfo;
61 using SyncMode = OHOS::DistributedKv::SyncMode;
62 using SyncAction = OHOS::DistributedKv::KVDBServiceImpl::SyncAction;
63 using SwitchState = OHOS::DistributedKv::SwitchState;
64 using UserId = OHOS::DistributedKv::UserId;
65 using StoreMetaData = OHOS::DistributedData::StoreMetaData;
66 using SyncEnd = OHOS::DistributedKv::KvStoreSyncManager::SyncEnd;
67 using DBResult = std::map<std::string, DistributedDB::DBStatus>;
68 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
69 using DBLaunchParam = OHOS::DistributedKv::KVDBServiceImpl::DBLaunchParam;
70 static OHOS::DistributedKv::StoreId storeId = { "kvdb_test_storeid" };
71 static OHOS::DistributedKv::AppId appId = { "ohos.test.kvdb" };
72 static constexpr const char *TEST_USER = "0";
73 namespace OHOS::Test {
74 namespace DistributedDataTest {
75 class KvdbServiceImplTest : public testing::Test {
76 public:
77     static inline std::shared_ptr<AccessTokenKitMock> accTokenMock = nullptr;
78     static inline std::shared_ptr<MetaDataManagerMock> metaDataManagerMock = nullptr;
79     static inline std::shared_ptr<MetaDataMock<StoreMetaData>> metaDataMock = nullptr;
80     static constexpr size_t NUM_MIN = 5;
81     static constexpr size_t NUM_MAX = 12;
82     static constexpr uint32_t TOKENID1 = 123;
83     static constexpr uint32_t TOKENID2 = 456;
84     static DistributedKvDataManager manager;
85     static Options create;
86     static UserId userId;
87 
88     std::shared_ptr<SingleKvStore> kvStore;
89     std::shared_ptr<ExecutorPool> executors_;
90     static AppId appId;
91     static StoreId storeId64;
92     static StoreId storeId65;
93 
94     static void SetUpTestCase(void);
95     static void TearDownTestCase(void);
96     static void RemoveAllStore(OHOS::DistributedKv::DistributedKvDataManager &manager);
97     void SetUp();
98     void TearDown();
99     void CreateStoreMetaData(std::vector<StoreMetaData> &datas, DBLaunchParam param);
100     KvdbServiceImplTest();
101 
102 protected:
103     std::shared_ptr<DistributedKv::KVDBServiceImpl> kvdbServiceImpl_;
104     static NetworkDelegateMock delegate_;
105     StoreMetaData metaData_;
106     Options options_;
107 };
108 
109 class CloudServerMock : public CloudServer {
110 public:
111     virtual ~CloudServerMock() = default;
112     bool IsSupportCloud(int32_t userId);
113 };
114 
IsSupportCloud(int32_t userId)115 bool CloudServerMock::IsSupportCloud(int32_t userId)
116 {
117     return true;
118 }
119 
120 OHOS::DistributedKv::DistributedKvDataManager KvdbServiceImplTest::manager;
121 Options KvdbServiceImplTest::create;
122 UserId KvdbServiceImplTest::userId;
123 
124 AppId KvdbServiceImplTest::appId;
125 StoreId KvdbServiceImplTest::storeId64;
126 StoreId KvdbServiceImplTest::storeId65;
127 NetworkDelegateMock KvdbServiceImplTest::delegate_;
128 
RemoveAllStore(DistributedKvDataManager & manager)129 void KvdbServiceImplTest::RemoveAllStore(DistributedKvDataManager &manager)
130 {
131     manager.CloseAllKvStore(appId);
132     manager.DeleteAllKvStore(appId, create.baseDir);
133 }
134 
SetUpTestCase(void)135 void KvdbServiceImplTest::SetUpTestCase(void)
136 {
137     auto executors = std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
138     manager.SetExecutors(executors);
139     userId.userId = "kvdbserviceimpltest1";
140     appId.appId = "ohos.kvdbserviceimpl.test";
141     create.createIfMissing = true;
142     create.encrypt = false;
143     create.securityLevel = OHOS::DistributedKv::S1;
144     create.autoSync = true;
145     create.kvStoreType = OHOS::DistributedKv::SINGLE_VERSION;
146     create.area = OHOS::DistributedKv::EL1;
147     create.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
148     mkdir(create.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
149 
150     storeId64.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000";
151     storeId65.storeId = "a000000000b000000000c000000000d000000000e000000000f000000000g000"
152                         "a000000000b000000000c000000000d000000000e000000000f000000000g0000";
153     RemoveAllStore(manager);
154 
155     accTokenMock = std::make_shared<AccessTokenKitMock>();
156     BAccessTokenKit::accessTokenkit = accTokenMock;
157     metaDataManagerMock = std::make_shared<MetaDataManagerMock>();
158     BMetaDataManager::metaDataManager = metaDataManagerMock;
159     metaDataMock = std::make_shared<MetaDataMock<StoreMetaData>>();
160     BMetaData<StoreMetaData>::metaDataManager = metaDataMock;
161     NetworkDelegate::RegisterNetworkInstance(&delegate_);
162 }
163 
TearDownTestCase()164 void KvdbServiceImplTest::TearDownTestCase()
165 {
166     RemoveAllStore(manager);
167     (void)remove((create.baseDir + "/kvdb").c_str());
168     (void)remove(create.baseDir.c_str());
169 
170     accTokenMock = nullptr;
171     BAccessTokenKit::accessTokenkit = nullptr;
172     metaDataManagerMock = nullptr;
173     BMetaDataManager::metaDataManager = nullptr;
174     metaDataMock = nullptr;
175     BMetaData<StoreMetaData>::metaDataManager = nullptr;
176 }
177 
SetUp(void)178 void KvdbServiceImplTest::SetUp(void)
179 {
180     kvdbServiceImpl_ = std::make_shared<DistributedKv::KVDBServiceImpl>();
181 
182     options_.isNeedCompress = true;
183     metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
184     metaData_.bundleName = appId.appId;
185     metaData_.storeId = storeId.storeId;
186     metaData_.user = TEST_USER;
187     metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
188     metaData_.version = 1;
189     MetaDataManager::GetInstance().DelMeta(metaData_.GetKey());
190 }
191 
TearDown(void)192 void KvdbServiceImplTest::TearDown(void)
193 {
194     RemoveAllStore(manager);
195 }
196 
SyncEndCallback(const std::map<std::string,DistributedDB::DBStatus> & statusMap)197 void SyncEndCallback(const std::map<std::string, DistributedDB::DBStatus> &statusMap)
198 {
199     for (const auto &pair : statusMap) {
200         ZLOGI("Key: %{public}s, Status: %{public}d", pair.first.c_str(), pair.second);
201     }
202 }
203 
KvdbServiceImplTest(void)204 KvdbServiceImplTest::KvdbServiceImplTest(void)
205 {
206 }
207 
CreateStoreMetaData(std::vector<StoreMetaData> & datas,DBLaunchParam param)208 void KvdbServiceImplTest::CreateStoreMetaData(std::vector<StoreMetaData> &datas, DBLaunchParam param)
209 {
210     std::vector<StoreMetaData> metaData;
211 
212     // 1: storeType out of range.
213     StoreMetaData meta1;
214     meta1.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN - 1;
215     metaData.push_back(meta1);
216 
217     StoreMetaData meta2;
218     meta2.storeType = StoreMetaData::StoreType::STORE_KV_END + 1;
219     metaData.push_back(meta2);
220 
221     // 2: user ID mismatch.
222     StoreMetaData meta3;
223     meta3.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN;
224     meta3.user = "user2"; // param.userId = "user1"
225     metaData.push_back(meta3);
226 
227     // 3: appId equals to process label.
228     StoreMetaData meta4;
229     meta4.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN;
230     meta4.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel();
231     metaData.push_back(meta4);
232 
233     // 4: The identifier does not match and CompareTripleIdentifier is false.
234     StoreMetaData meta5;
235     meta5.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN;
236     meta5.storeId = "store_id_1";
237     meta5.appId = "app_id_1";
238     metaData.push_back(meta5);
239 
240     // 5: Normal execution logic.
241     StoreMetaData meta6;
242     meta6.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN;
243     meta6.user = param.userId; // param.userId = "user1"
244     meta6.appId = "valid_app_id";
245     meta6.storeId = "store_id_2";
246     meta6.tokenId = TOKENID1;
247     metaData.push_back(meta6);
248 
249     // 6: trigger SetEqualIdentifier.
250     StoreMetaData meta7;
251     meta7.storeType = StoreMetaData::StoreType::STORE_KV_BEGIN;
252     meta7.user = param.userId;
253     meta7.appId = "valid_app_id";
254     meta7.storeId = "store_id_3";
255     meta7.tokenId = TOKENID2;
256     metaData.push_back(meta7);
257 }
258 
259 /**
260 * @tc.name: KvdbServiceImpl001
261 * @tc.desc: KvdbServiceImplTest function test.
262 * @tc.type: FUNC
263 * @tc.author: SQL
264 */
265 HWTEST_F(KvdbServiceImplTest, KvdbServiceImpl001, TestSize.Level0)
266 {
267     std::string device = "OH_device_test";
268     StoreId id1;
269     id1.storeId = "id1";
270     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
271     EXPECT_NE(kvStore, nullptr);
272     EXPECT_EQ(status, Status::SUCCESS);
273     int32_t result = kvdbServiceImpl_->OnInitialize();
274     EXPECT_EQ(result, Status::SUCCESS);
275     FeatureSystem::Feature::BindInfo bindInfo;
276     result = kvdbServiceImpl_->OnBind(bindInfo);
277     EXPECT_EQ(result, Status::SUCCESS);
278     result = kvdbServiceImpl_->Online(device);
279     EXPECT_EQ(result, Status::SUCCESS);
280     status = kvdbServiceImpl_->SubscribeSwitchData(appId);
281     EXPECT_EQ(status, Status::SUCCESS);
282     SyncInfo syncInfo;
283     status = kvdbServiceImpl_->CloudSync(appId, id1, syncInfo);
284     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
285 
286     DistributedKv::StoreConfig storeConfig;
287     status = kvdbServiceImpl_->SetConfig(appId, id1, storeConfig);
288     EXPECT_EQ(status, Status::SUCCESS);
289     status = kvdbServiceImpl_->NotifyDataChange(appId, id1, 0);
290     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
291 
292     status = kvdbServiceImpl_->UnsubscribeSwitchData(appId);
293     EXPECT_EQ(status, Status::SUCCESS);
294 
295     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
296         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
297         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
298     status = kvdbServiceImpl_->Close(appId, id1, 0);
299     EXPECT_EQ(status, Status::SUCCESS);
300 }
301 
302 /**
303 * @tc.name: OnInitialize001
304 * @tc.desc: OnInitialize function test.
305 * @tc.type: FUNC
306 * @tc.author: wangbin
307 */
308 HWTEST_F(KvdbServiceImplTest, OnInitialize001, TestSize.Level0)
309 {
310     std::string device = "OH_device_test";
311     int32_t result = kvdbServiceImpl_->OnInitialize();
312     EXPECT_EQ(result, Status::SUCCESS);
313     DistributedData::StoreInfo storeInfo;
314     storeInfo.bundleName = appId.appId;
315     storeInfo.storeName = storeId.storeId;
316     storeInfo.instanceId = 10;
317     storeInfo.user = 100;
318     auto event = std::make_unique<CloudEvent>(CloudEvent::CLOUD_SYNC, storeInfo);
319     EXPECT_NE(event, nullptr);
320     result = EventCenter::GetInstance().PostEvent(move(event));
321     EXPECT_EQ(result, 1); // CODE_SYNC
322     auto event1 = std::make_unique<CloudEvent>(CloudEvent::CLEAN_DATA, storeInfo);
323     EXPECT_NE(event1, nullptr);
324     result = EventCenter::GetInstance().PostEvent(move(event1));
325     EXPECT_EQ(result, 1); // CODE_SYNC
326 }
327 
328 /**
329 * @tc.name: OnInitialize002
330 * @tc.desc: OnInitialize function test.
331 * @tc.type: FUNC
332 * @tc.author: my
333 */
334 HWTEST_F(KvdbServiceImplTest, OnInitialize002, TestSize.Level0)
335 {
336     auto result = kvdbServiceImpl_->OnInitialize();
337     EXPECT_EQ(result, Status::SUCCESS);
338     DistributedData::StoreInfo storeInfo;
339     storeInfo.bundleName = "bundleName";
340     storeInfo.storeName = "storeName";
341     storeInfo.user = 100;
342 
343     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
344     .WillOnce(testing::Return(false))
345     .WillRepeatedly(testing::Return(true));
346 
347     auto event = std::make_unique<CloudEvent>(CloudEvent::CLOUD_SYNC, storeInfo);
348     EXPECT_NE(event, nullptr);
349     result = EventCenter::GetInstance().PostEvent(move(event));
350     EXPECT_EQ(result, 1); // CODE_SYNC
351 }
352 
353 /**
354 * @tc.name: GetStoreIdsTest001
355 * @tc.desc: GetStoreIds
356 * @tc.type: FUNC
357 * @tc.author: wangbin
358 */
359 HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest001, TestSize.Level0)
360 {
361     ZLOGI("GetStoreIdsTest001 start");
362     StoreId id1;
363     id1.storeId = "id1";
364     StoreId id2;
365     id2.storeId = "id2";
366     StoreId id3;
367     id3.storeId = "id3";
368     Status status = manager.GetSingleKvStore(create, appId, id1, kvStore);
369     ASSERT_NE(kvStore, nullptr);
370     ASSERT_EQ(status, Status::SUCCESS);
371     status = manager.GetSingleKvStore(create, appId, id2, kvStore);
372     ASSERT_NE(kvStore, nullptr);
373     ASSERT_EQ(status, Status::SUCCESS);
374     status = manager.GetSingleKvStore(create, appId, id3, kvStore);
375     ASSERT_NE(kvStore, nullptr);
376     ASSERT_EQ(status, Status::SUCCESS);
377     std::vector<StoreId> storeIds;
378     status = kvdbServiceImpl_->GetStoreIds(appId, 0, storeIds);
379     ASSERT_EQ(status, Status::SUCCESS);
380 }
381 
382 /**
383 * @tc.name: GetStoreIdsTest002
384 * @tc.desc: GetStoreIds
385 * @tc.type: FUNC
386 * @tc.author: wangbin
387 */
388 HWTEST_F(KvdbServiceImplTest, GetStoreIdsTest002, TestSize.Level0)
389 {
390     ZLOGI("GetStoreIdsTest002 start");
391     std::vector<StoreId> storeIds;
392     AppId appId01;
393     auto status = kvdbServiceImpl_->GetStoreIds(appId01, 0, storeIds);
394     ZLOGI("GetStoreIdsTest002 status = :%{public}d", status);
395     ASSERT_EQ(status, Status::SUCCESS);
396 }
397 
398 /**
399 * @tc.name: DeleteTest001
400 * @tc.desc: Delete Test
401 * @tc.type: FUNC
402 * @tc.author: wangbin
403 */
404 HWTEST_F(KvdbServiceImplTest, DeleteTest001, TestSize.Level0)
405 {
406     ZLOGI("DeleteTest001 start");
407     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
408     ASSERT_NE(kvStore, nullptr);
409     ASSERT_EQ(status1, Status::SUCCESS);
410     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
411         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
412         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
413     auto status = kvdbServiceImpl_->Delete(appId, storeId, 0);
414     ZLOGI("DeleteTest001 status = :%{public}d", status);
415     ASSERT_EQ(status, Status::SUCCESS);
416 }
417 
418 /**
419 * @tc.name: DeleteTest002
420 * @tc.desc: Delete Test
421 * @tc.type: FUNC
422 * @tc.author: wangbin
423 */
424 HWTEST_F(KvdbServiceImplTest, DeleteTest002, TestSize.Level0)
425 {
426     ZLOGI("DeleteTest002 start");
427     AppId appId01 = { "ohos.kvdbserviceimpl.test01" };
428     StoreId storeId01 = { "meta_test_storeid" };
429     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
430         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
431         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
432     auto status = kvdbServiceImpl_->Delete(appId01, storeId01, 0);
433     ZLOGI("DeleteTest002 status = :%{public}d", status);
434     ASSERT_EQ(status, Status::SUCCESS);
435 }
436 
437 /**
438 * @tc.name: DeleteTest003
439 * @tc.desc: Delete function test.
440 * @tc.type: FUNC
441 * @tc.author: wangbin
442 */
443 HWTEST_F(KvdbServiceImplTest, DeleteTest003, TestSize.Level0)
444 {
445     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
446         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
447         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
448     EXPECT_CALL(*accTokenMock, GetHapTokenInfo(testing::_, testing::_))
449         .WillOnce(testing::Return(-1))
450         .WillRepeatedly(testing::Return(-1));
451     int32_t status = kvdbServiceImpl_->Delete(appId, storeId, 0);
452     EXPECT_EQ(status, DistributedKv::ILLEGAL_STATE);
453 }
454 
455 /**
456 * @tc.name: CloseTest001
457 * @tc.desc: Close function test.
458 * @tc.type: FUNC
459 * @tc.author: wangbin
460 */
461 HWTEST_F(KvdbServiceImplTest, CloseTest001, TestSize.Level0)
462 {
463     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
464         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
465         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
466     EXPECT_CALL(*accTokenMock, GetHapTokenInfo(testing::_, testing::_))
467         .WillOnce(testing::Return(-1))
468         .WillRepeatedly(testing::Return(-1));
469     int32_t status = kvdbServiceImpl_->Close(appId, storeId, 0);
470     EXPECT_EQ(status, DistributedKv::ILLEGAL_STATE);
471 }
472 
473 /**
474 * @tc.name: OnAsyncCompleteTest001
475 * @tc.desc: OnAsyncComplete function test.
476 * @tc.type: FUNC
477 * @tc.author: wangbin
478 */
479 HWTEST_F(KvdbServiceImplTest, OnAsyncCompleteTest001, TestSize.Level0)
480 {
481     DistributedKv::Statistic upload;
482     upload.failed = 1;    // test
483     upload.success = 1;   // test
484     upload.total = 1;     // test
485     upload.untreated = 1; // test
486     DistributedKv::Statistic download;
487     download.failed = 1;    // test
488     download.success = 1;   // test
489     download.total = 1;     // test
490     download.untreated = 1; // test
491     DistributedKv::TableDetail details;
492     details.download = download;
493     details.upload = upload;
494     DistributedKv::ProgressDetail detail;
495     detail.code = 1;     // test
496     detail.progress = 1; // test
497     detail.details = details;
498     sptr<DistributedKv::IKVDBNotifier> notifier;
499     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
500     syncAgent.pid_ = 1;                   // test
501     syncAgent.switchesObserverCount_ = 1; // test
502     syncAgent.appId_ = { "ohos.OnAsyncCompleteTest001.kvdb" };
503     syncAgent.notifier_ = notifier;
504     kvdbServiceImpl_->syncAgents_.Insert(100, syncAgent);       // test
505     kvdbServiceImpl_->OnAsyncComplete(1, 1, std::move(detail)); // test
506     EXPECT_EQ(kvdbServiceImpl_->syncAgents_.Find(1).first, false);
507     kvdbServiceImpl_->OnAsyncComplete(100, 1, std::move(detail)); // test
508     EXPECT_EQ(kvdbServiceImpl_->syncAgents_.Find(100).first, true);
509 }
510 
511 /**
512 * @tc.name: syncTest001
513 * @tc.desc: GetStoreIds
514 * @tc.type: FUNC
515 * @tc.author: wangbin
516 */
517 HWTEST_F(KvdbServiceImplTest, syncTest001, TestSize.Level0)
518 {
519     ZLOGI("syncTest001 start");
520     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
521     ASSERT_NE(kvStore, nullptr);
522     ASSERT_EQ(status1, Status::SUCCESS);
523     SyncInfo syncInfo;
524     auto status = kvdbServiceImpl_->Sync(appId, storeId, 0, syncInfo);
525     ZLOGI("syncTest001 status = :%{public}d", status);
526     ASSERT_NE(status, Status::SUCCESS);
527 }
528 
529 /**
530 * @tc.name: RegisterSyncCallbackTest001
531 * @tc.desc: GetStoreIds
532 * @tc.type: FUNC
533 * @tc.author: wangbin
534 */
535 HWTEST_F(KvdbServiceImplTest, RegisterSyncCallbackTest001, TestSize.Level0)
536 {
537     ZLOGI("RegisterSyncCallbackTest001 start");
538     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
539     ASSERT_NE(kvStore, nullptr);
540     ASSERT_EQ(status1, Status::SUCCESS);
541     sptr<OHOS::DistributedKv::IKVDBNotifier> notifier;
542     auto status = kvdbServiceImpl_->RegServiceNotifier(appId, notifier);
543     ZLOGI("RegisterSyncCallbackTest001 status = :%{public}d", status);
544     ASSERT_EQ(status, Status::SUCCESS);
545 }
546 
547 /**
548 * @tc.name: UnregServiceNotifierTest001
549 * @tc.desc: UnregServiceNotifier test
550 * @tc.type: FUNC
551 * @tc.author: wangbin
552 */
553 HWTEST_F(KvdbServiceImplTest, UnregServiceNotifierTest001, TestSize.Level0)
554 {
555     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
556     ASSERT_NE(kvStore, nullptr);
557     ASSERT_EQ(status1, Status::SUCCESS);
558     sptr<DistributedKv::IKVDBNotifier> notifier;
559     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
560     syncAgent.pid_ = 1;                   // test
561     syncAgent.switchesObserverCount_ = 1; // test
562     syncAgent.appId_ = { "ohos.OnAsyncCompleteTest001.kvdb" };
563     syncAgent.notifier_ = notifier;
564     kvdbServiceImpl_->syncAgents_.Insert(IPCSkeleton::GetCallingTokenID(), syncAgent);
565     auto status = kvdbServiceImpl_->UnregServiceNotifier(appId);
566     ASSERT_EQ(status, Status::SUCCESS);
567 }
568 
569 /**
570 * @tc.name: HandleGenDetailsTest001
571 * @tc.desc: HandleGenDetails test
572 * @tc.type: FUNC
573 * @tc.author: wangbin
574 */
575 HWTEST_F(KvdbServiceImplTest, HandleGenDetailsTest001, TestSize.Level0)
576 {
577     DistributedData::GenDetails details;
578     ASSERT_EQ(details.begin(), details.end());
579     DistributedKv::ProgressDetail progressDetails = kvdbServiceImpl_->HandleGenDetails(details);
580     GenProgressDetail detail;
581     detail.progress = GenProgress::SYNC_IN_PROGRESS;
582     detail.code = GenProgress::SYNC_IN_PROGRESS;
583     details.insert_or_assign("test", detail);
584     progressDetails = kvdbServiceImpl_->HandleGenDetails(details);
585     ASSERT_EQ(detail.details.begin(), detail.details.end());
586     ASSERT_EQ(progressDetails.progress, GenProgress::SYNC_IN_PROGRESS);
587     std::map<std::string, GenTableDetail> gentabledetail;
588     GenTableDetail tabledetail;
589     gentabledetail.insert_or_assign("test", tabledetail);
590     detail.details = gentabledetail;
591     progressDetails = kvdbServiceImpl_->HandleGenDetails(details);
592     ASSERT_NE(detail.details.begin(), detail.details.end());
593     ASSERT_EQ(progressDetails.code, GenProgress::SYNC_IN_PROGRESS);
594 }
595 
596 /**
597 * @tc.name: SetSyncParamTest001
598 * @tc.desc: SetSyncParam test
599 * @tc.type: FUNC
600 * @tc.author: wangbin
601 */
602 HWTEST_F(KvdbServiceImplTest, SetSyncParamTest001, TestSize.Level0)
603 {
604     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
605     ASSERT_NE(kvStore, nullptr);
606     ASSERT_EQ(status1, Status::SUCCESS);
607     OHOS::DistributedKv::KvSyncParam syncparam;
608     auto status = kvdbServiceImpl_->SetSyncParam(appId, storeId, 0, syncparam);
609     ASSERT_EQ(status, Status::SUCCESS);
610     syncparam.allowedDelayMs = DistributedKv::KvStoreSyncManager::SYNC_MAX_DELAY_MS + 1;
611     status = kvdbServiceImpl_->SetSyncParam(appId, storeId, 0, syncparam);
612     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
613 }
614 
615 /**
616 * @tc.name: GetSyncParamTest001
617 * @tc.desc: GetStoreIds
618 * @tc.type: FUNC
619 * @tc.author: wangbin
620 */
621 HWTEST_F(KvdbServiceImplTest, GetSyncParamTest001, TestSize.Level0)
622 {
623     ZLOGI("GetSyncParamTest001 start");
624     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
625     ASSERT_NE(kvStore, nullptr);
626     ASSERT_EQ(status1, Status::SUCCESS);
627     OHOS::DistributedKv::KvSyncParam syncparam;
628     auto status = kvdbServiceImpl_->GetSyncParam(appId, storeId, 0, syncparam);
629     ZLOGI("GetSyncParamTest001 status = :%{public}d", status);
630     ASSERT_EQ(status, Status::SUCCESS);
631 }
632 
633 /**
634 * @tc.name: EnableCapabilityTest001
635 * @tc.desc: EnableCapability test
636 * @tc.type: FUNC
637 * @tc.author: wangbin
638 */
639 HWTEST_F(KvdbServiceImplTest, EnableCapabilityTest001, TestSize.Level0)
640 {
641     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
642     ASSERT_NE(kvStore, nullptr);
643     ASSERT_EQ(status1, Status::SUCCESS);
644     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
645         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
646         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
647     auto status = kvdbServiceImpl_->EnableCapability(appId, storeId, 0);
648     ASSERT_EQ(status, Status::SUCCESS);
649     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
650         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
651         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
652     status = kvdbServiceImpl_->EnableCapability(appId, storeId, 0);
653     ASSERT_EQ(status, Status::ILLEGAL_STATE);
654 }
655 
656 /**
657 * @tc.name: GetInstIndexTest001
658 * @tc.desc: GetInstIndex test
659 * @tc.type: FUNC
660 * @tc.author: wangbin
661 */
662 HWTEST_F(KvdbServiceImplTest, GetInstIndexTest001, TestSize.Level0)
663 {
664     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
665     ASSERT_NE(kvStore, nullptr);
666     ASSERT_EQ(status1, Status::SUCCESS);
667     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
668         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
669         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
670     auto status = kvdbServiceImpl_->GetInstIndex(100, appId);
671     ASSERT_EQ(status, -1);
672 }
673 
674 /**
675 * @tc.name: IsNeedMetaSyncTest001
676 * @tc.desc: IsNeedMetaSync test
677 * @tc.type: FUNC
678 * @tc.author: wangbin
679 */
680 HWTEST_F(KvdbServiceImplTest, IsNeedMetaSyncTest001, TestSize.Level0)
681 {
682     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
683     ASSERT_NE(kvStore, nullptr);
684     ASSERT_EQ(status1, Status::SUCCESS);
685     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
686     std::vector<std::string> uuids{ "uuidtest01" };
687     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
688         .WillOnce(testing::Return(true))
689         .WillRepeatedly(testing::Return(true));
690     auto status = kvdbServiceImpl_->IsNeedMetaSync(meta, uuids);
691     ASSERT_EQ(status, false);
692     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
693         .WillOnce(testing::Return(false))
694         .WillRepeatedly(testing::Return(false));
695     status = kvdbServiceImpl_->IsNeedMetaSync(meta, uuids);
696     ASSERT_EQ(status, true);
697 }
698 
699 /**
700 * @tc.name: GetDistributedDataMetaTest001
701 * @tc.desc: GetDistributedDataMeta test
702 * @tc.type: FUNC
703 * @tc.author: wangbin
704 */
705 HWTEST_F(KvdbServiceImplTest, GetDistributedDataMetaTest001, TestSize.Level0)
706 {
707     Status status = manager.GetSingleKvStore(create, appId, storeId, kvStore);
708     ASSERT_NE(kvStore, nullptr);
709     ASSERT_EQ(status, Status::SUCCESS);
710     std::string deviceId = "KvdbServiceImplTest_deviceId";
711     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
712         .WillOnce(testing::Return(false))
713         .WillRepeatedly(testing::Return(false));
714     auto meta = kvdbServiceImpl_->GetDistributedDataMeta(deviceId);
715     ASSERT_EQ(meta.user, "0");
716     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
717         .WillOnce(testing::Return(true))
718         .WillRepeatedly(testing::Return(true));
719     meta = kvdbServiceImpl_->GetDistributedDataMeta(deviceId);
720     ASSERT_EQ(meta.deviceId, deviceId);
721 }
722 
723 /**
724 * @tc.name: ProcessResultTest001
725 * @tc.desc: ProcessResult test
726 * @tc.type: FUNC
727 * @tc.author: wangbin
728 */
729 HWTEST_F(KvdbServiceImplTest, ProcessResultTest001, TestSize.Level0)
730 {
731     Status status = manager.GetSingleKvStore(create, appId, storeId, kvStore);
732     ASSERT_NE(kvStore, nullptr);
733     ASSERT_EQ(status, Status::SUCCESS);
734     std::map<std::string, int32_t> results;
735     results.insert_or_assign("uuidtest01", DBStatus::DB_ERROR);
736     results.insert_or_assign("uuidtest02", DBStatus::OK);
737     auto result = kvdbServiceImpl_->ProcessResult(results);
738     std::vector<std::string> devices = result.first;
739     auto it = std::find(devices.begin(), devices.end(), "uuidtest02");
740     ASSERT_NE(it, devices.end());
741 }
742 
743 /**
744 * @tc.name: DoSyncBeginTest001
745 * @tc.desc: DoSyncBegin test
746 * @tc.type: FUNC
747 * @tc.author: wangbin
748 */
749 HWTEST_F(KvdbServiceImplTest, DoSyncBeginTest001, TestSize.Level0)
750 {
751     Status status = manager.GetSingleKvStore(create, appId, storeId, kvStore);
752     ASSERT_NE(kvStore, nullptr);
753     ASSERT_EQ(status, Status::SUCCESS);
754     std::vector<std::string> device1{ "uuidtest01" };
755     std::vector<std::string> device2;
756     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
757     SyncInfo syncInfo;
758     syncInfo.devices = { "device1", "device2" };
759     syncInfo.query = "query";
760     SyncEnd syncEnd = SyncEndCallback;
761     std::map<std::string, DistributedDB::DBStatus> statusMap;
762     statusMap.insert_or_assign("DoSyncBeginTest001", DBStatus::OK);
763     syncEnd(statusMap);
764     status = kvdbServiceImpl_->DoSyncBegin(device2, meta, syncInfo, syncEnd, DBStatus::OK);
765     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
766     status = kvdbServiceImpl_->DoSyncBegin(device1, meta, syncInfo, syncEnd, DBStatus::OK);
767     ASSERT_EQ(status, Status::ERROR);
768 }
769 
770 /**
771 * @tc.name: DoCompleteTest001
772 * @tc.desc: DoComplete test
773 * @tc.type: FUNC
774 * @tc.author: wangbin
775 */
776 HWTEST_F(KvdbServiceImplTest, DoCompleteTest001, TestSize.Level0)
777 {
778     Status status = manager.GetSingleKvStore(create, appId, storeId, kvStore);
779     ASSERT_NE(kvStore, nullptr);
780     ASSERT_EQ(status, Status::SUCCESS);
781     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
782     SyncInfo syncInfo;
783     syncInfo.devices = { "device1", "device2" };
784     syncInfo.query = "query";
785     syncInfo.seqId = 1; // test
786     RefCount refCount;
787     DBResult dbResult;
788     dbResult.insert_or_assign("DoCompleteTest_1", DBStatus::OK);
789     dbResult.insert_or_assign("DoCompleteTest_1", DBStatus::DB_ERROR);
790     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
791         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
792         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
793     status = kvdbServiceImpl_->DoComplete(meta, syncInfo, refCount, dbResult);
794     ASSERT_EQ(status, Status::SUCCESS);
795     syncInfo.seqId = std::numeric_limits<uint64_t>::max();
796     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
797         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
798         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
799     status = kvdbServiceImpl_->DoComplete(meta, syncInfo, refCount, dbResult);
800     ASSERT_EQ(status, Status::SUCCESS);
801 }
802 
803 /**
804 * @tc.name: ConvertDbStatusNativeTest001
805 * @tc.desc: ConvertDbStatusNative test
806 * @tc.type: FUNC
807 * @tc.author: wangbin
808 */
809 HWTEST_F(KvdbServiceImplTest, ConvertDbStatusNativeTest001, TestSize.Level0)
810 {
811     auto status = kvdbServiceImpl_->ConvertDbStatusNative(DBStatus::DB_ERROR);
812     ASSERT_EQ(status, Status::DB_ERROR);
813     status = kvdbServiceImpl_->ConvertDbStatusNative(DBStatus::COMM_FAILURE);
814     ASSERT_EQ(status, Status::DEVICE_NOT_ONLINE);
815     DBStatus dbstatus = static_cast<DBStatus>(DBStatus::OK - 1);
816     status = kvdbServiceImpl_->ConvertDbStatusNative(dbstatus);
817     ASSERT_EQ(status, -1);
818 }
819 
820 /**
821 * @tc.name: GetSyncDelayTimeTest001
822 * @tc.desc: GetSyncDelayTime test
823 * @tc.type: FUNC
824 * @tc.author: wangbin
825 */
826 HWTEST_F(KvdbServiceImplTest, GetSyncDelayTimeTest001, TestSize.Level0)
827 {
828     auto status = kvdbServiceImpl_->GetSyncDelayTime(1, storeId);
829     ASSERT_EQ(status, DistributedKv::KvStoreSyncManager::SYNC_MIN_DELAY_MS);
830     status = kvdbServiceImpl_->GetSyncDelayTime(0, storeId);
831     ASSERT_EQ(status, 0);
832 }
833 
834 /**
835 * @tc.name: ConvertGeneralErrTest
836 * @tc.desc: ConvertGeneralErr test the return result of input with different values.
837 * @tc.type: FUNC
838 * @tc.author: wangbin
839 */
840 HWTEST_F(KvdbServiceImplTest, ConvertGeneralErrTest, TestSize.Level0)
841 {
842     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_DB_ERROR), Status::DB_ERROR);
843     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_OK), Status::SUCCESS);
844     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_ARGS), Status::INVALID_ARGUMENT);
845     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_RECORD_NOT_FOUND), Status::KEY_NOT_FOUND);
846     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_VALUE_FIELDS), Status::INVALID_VALUE_FIELDS);
847     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_FIELD_TYPE), Status::INVALID_FIELD_TYPE);
848     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_CONSTRAIN_VIOLATION), Status::CONSTRAIN_VIOLATION);
849     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_FORMAT), Status::INVALID_FORMAT);
850     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_QUERY_FORMAT), Status::INVALID_QUERY_FORMAT);
851     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_INVALID_QUERY_FIELD), Status::INVALID_QUERY_FIELD);
852     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_NOT_SUPPORT), Status::NOT_SUPPORT);
853     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_TIME_OUT), Status::TIME_OUT);
854     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_OVER_MAX_LIMITS), Status::OVER_MAX_LIMITS);
855     EXPECT_EQ(kvdbServiceImpl_->ConvertGeneralErr(GeneralError::E_SECURITY_LEVEL_ERROR), Status::SECURITY_LEVEL_ERROR);
856 }
857 
858 /**
859 * @tc.name: DisableCapabilityTest001
860 * @tc.desc: DisableCapability test
861 * @tc.type: FUNC
862 * @tc.author: wangbin
863 */
864 HWTEST_F(KvdbServiceImplTest, DisableCapabilityTest001, TestSize.Level0)
865 {
866     ZLOGI("DisableCapabilityTest001 start");
867     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
868     ASSERT_NE(kvStore, nullptr);
869     ASSERT_EQ(status1, Status::SUCCESS);
870     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
871         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
872         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
873     auto status = kvdbServiceImpl_->DisableCapability(appId, storeId, 0);
874     ZLOGI("DisableCapabilityTest001 status = :%{public}d", status);
875     ASSERT_EQ(status, Status::SUCCESS);
876     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
877         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
878         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
879     status = kvdbServiceImpl_->EnableCapability(appId, storeId, 0);
880     ASSERT_EQ(status, Status::ILLEGAL_STATE);
881 }
882 
883 /**
884 * @tc.name: SetCapabilityTest001
885 * @tc.desc: SetCapability test
886 * @tc.type: FUNC
887 * @tc.author: wangbin
888 */
889 HWTEST_F(KvdbServiceImplTest, SetCapabilityTest001, TestSize.Level0)
890 {
891     ZLOGI("SetCapabilityTest001 start");
892     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
893     ASSERT_NE(kvStore, nullptr);
894     ASSERT_EQ(status1, Status::SUCCESS);
895     std::vector<std::string> local;
896     std::vector<std::string> remote;
897     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
898         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
899         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
900     auto status = kvdbServiceImpl_->SetCapability(appId, storeId, 0, local, remote);
901     ZLOGI("SetCapabilityTest001 status = :%{public}d", status);
902     ASSERT_EQ(status, Status::SUCCESS);
903     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
904         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
905         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
906     status = kvdbServiceImpl_->EnableCapability(appId, storeId, 0);
907     ASSERT_EQ(status, Status::ILLEGAL_STATE);
908 }
909 
910 /**
911 * @tc.name: AddSubscribeInfoTest001
912 * @tc.desc: AddSubscribeInfo test
913 * @tc.type: FUNC
914 * @tc.author: wangbin
915 */
916 HWTEST_F(KvdbServiceImplTest, AddSubscribeInfoTest001, TestSize.Level0)
917 {
918     ZLOGI("AddSubscribeInfoTest001 start");
919     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
920     ASSERT_NE(kvStore, nullptr);
921     ASSERT_EQ(status1, Status::SUCCESS);
922     SyncInfo syncInfo;
923     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
924         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_NATIVE))
925         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_NATIVE));
926     auto status = kvdbServiceImpl_->AddSubscribeInfo(appId, storeId, 0, syncInfo);
927     ZLOGI("AddSubscribeInfoTest001 status = :%{public}d", status);
928     ASSERT_NE(status, Status::SUCCESS);
929 }
930 
931 /**
932 * @tc.name: RmvSubscribeInfoTest001
933 * @tc.desc: RmvSubscribeInfo test
934 * @tc.type: FUNC
935 * @tc.author: wangbin
936 */
937 HWTEST_F(KvdbServiceImplTest, RmvSubscribeInfoTest001, TestSize.Level0)
938 {
939     ZLOGI("RmvSubscribeInfoTest001 start");
940     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
941     ASSERT_NE(kvStore, nullptr);
942     ASSERT_EQ(status1, Status::SUCCESS);
943     SyncInfo syncInfo;
944     auto status = kvdbServiceImpl_->RmvSubscribeInfo(appId, storeId, 0, syncInfo);
945     ZLOGI("RmvSubscribeInfoTest001 status = :%{public}d", status);
946     ASSERT_NE(status, Status::SUCCESS);
947 }
948 
949 /**
950 * @tc.name: SubscribeTest001
951 * @tc.desc: Subscribe test
952 * @tc.type: FUNC
953 * @tc.author: wangbin
954 */
955 HWTEST_F(KvdbServiceImplTest, SubscribeTest001, TestSize.Level0)
956 {
957     ZLOGI("SubscribeTest001 start");
958     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
959     ASSERT_NE(kvStore, nullptr);
960     ASSERT_EQ(status1, Status::SUCCESS);
961     sptr<OHOS::DistributedKv::IKvStoreObserver> observer;
962     auto status = kvdbServiceImpl_->Subscribe(appId, storeId, 0, observer);
963     ZLOGI("SubscribeTest001 status = :%{public}d", status);
964     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
965 }
966 
967 /**
968 * @tc.name: UnsubscribeTest001
969 * @tc.desc: Unsubscribe test
970 * @tc.type: FUNC
971 * @tc.author: wangbin
972 */
973 HWTEST_F(KvdbServiceImplTest, UnsubscribeTest001, TestSize.Level0)
974 {
975     ZLOGI("UnsubscribeTest001 start");
976     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
977     ASSERT_NE(kvStore, nullptr);
978     ASSERT_EQ(status1, Status::SUCCESS);
979     sptr<DistributedKv::IKVDBNotifier> notifier;
980     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
981     syncAgent.pid_ = 1;                   // test
982     syncAgent.switchesObserverCount_ = 1; // test
983     syncAgent.appId_ = { "ohos.OnAsyncCompleteTest001.kvdb" };
984     syncAgent.notifier_ = notifier;
985     auto tokenId = IPCSkeleton::GetCallingTokenID();
986     kvdbServiceImpl_->syncAgents_.Insert(tokenId, syncAgent);
987     sptr<OHOS::DistributedKv::IKvStoreObserver> observer;
988     auto status = kvdbServiceImpl_->Unsubscribe(appId, storeId, 0, observer);
989     ZLOGI("UnsubscribeTest001 status = :%{public}d", status);
990     ASSERT_EQ(status, Status::SUCCESS);
991 }
992 
993 /**
994 * @tc.name: BeforeCreateTest001
995 * @tc.desc: BeforeCreate test
996 * @tc.type: FUNC
997 * @tc.author: wangbin
998 */
999 HWTEST_F(KvdbServiceImplTest, BeforeCreateTest001, TestSize.Level0)
1000 {
1001     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
1002     ASSERT_NE(kvStore, nullptr);
1003     ASSERT_EQ(status1, Status::SUCCESS);
1004     Options creates;
1005     creates.createIfMissing = true;
1006     creates.encrypt = false;
1007     creates.securityLevel = OHOS::DistributedKv::S1;
1008     creates.autoSync = true;
1009     creates.kvStoreType = OHOS::DistributedKv::SINGLE_VERSION;
1010     creates.area = OHOS::DistributedKv::EL1;
1011     creates.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
1012     creates.cloudConfig.enableCloud = true;
1013     kvdbServiceImpl_->executors_ = std::make_shared<ExecutorPool>(1, 1);
1014     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
1015         .WillOnce(testing::Return(false))
1016         .WillRepeatedly(testing::Return(false));
1017     auto status = kvdbServiceImpl_->BeforeCreate(appId, storeId, creates);
1018     ASSERT_NE(status, Status::STORE_META_CHANGED);
1019     kvdbServiceImpl_->executors_ = nullptr;
1020     ASSERT_EQ(status, Status::SUCCESS);
1021 }
1022 
1023 /**
1024 * @tc.name: AfterCreateTest001
1025 * @tc.desc: AfterCreate test
1026 * @tc.type: FUNC
1027 * @tc.author: wangbin
1028 */
1029 HWTEST_F(KvdbServiceImplTest, AfterCreateTest001, TestSize.Level0)
1030 {
1031     ZLOGI("AfterCreateTest001 start");
1032     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
1033     ASSERT_NE(kvStore, nullptr);
1034     ASSERT_EQ(status1, Status::SUCCESS);
1035     std::vector<uint8_t> password;
1036     auto status = kvdbServiceImpl_->AfterCreate(appId, storeId, create, password);
1037     ZLOGI("AfterCreateTest001 status = :%{public}d", status);
1038     ASSERT_EQ(status, Status::SUCCESS);
1039     AppId appIds;
1040     appIds.appId = "";
1041     status = kvdbServiceImpl_->AfterCreate(appIds, storeId, create, password);
1042     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
1043     StoreId storeIds;
1044     storeIds.storeId = "";
1045     status = kvdbServiceImpl_->AfterCreate(appId, storeIds, create, password);
1046     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
1047     status = kvdbServiceImpl_->AfterCreate(appIds, storeIds, create, password);
1048     ASSERT_EQ(status, Status::INVALID_ARGUMENT);
1049 }
1050 
1051 /**
1052 * @tc.name: OnAppExitTest001
1053 * @tc.desc: OnAppExit test
1054 * @tc.type: FUNC
1055 * @tc.author: wangbin
1056 */
1057 HWTEST_F(KvdbServiceImplTest, OnAppExitTest001, TestSize.Level0)
1058 {
1059     ZLOGI("OnAppExitTest001 start");
1060     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
1061     ASSERT_NE(kvStore, nullptr);
1062     ASSERT_EQ(status1, Status::SUCCESS);
1063     pid_t uid = 1;
1064     pid_t pid = 2;
1065     const char **perms = new const char *[2];
1066     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
1067     perms[1] = "ohos.permission.ACCESS_SERVICE_DM";
1068     TokenInfoParams infoInstance = {
1069         .dcapsNum = 0,
1070         .permsNum = 2,
1071         .aclsNum = 0,
1072         .dcaps = nullptr,
1073         .perms = perms,
1074         .acls = nullptr,
1075         .processName = "distributed_data_test",
1076         .aplStr = "system_basic",
1077     };
1078     uint32_t tokenId = GetAccessTokenId(&infoInstance);
1079     auto status = kvdbServiceImpl_->OnAppExit(uid, pid, tokenId, appId);
1080     ZLOGI("OnAppExitTest001 status = :%{public}d", status);
1081     ASSERT_EQ(status, Status::SUCCESS);
1082 }
1083 
1084 /**
1085 * @tc.name: CompareTripleIdentifierTest001
1086 * @tc.desc: CompareTripleIdentifier test
1087 * @tc.type: FUNC
1088 * @tc.author: wangbin
1089 */
1090 HWTEST_F(KvdbServiceImplTest, CompareTripleIdentifierTest001, TestSize.Level0)
1091 {
1092     std::string accountId = "accountIdTest";
1093     std::string identifier = "identifierTest";
1094     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
1095     auto status = kvdbServiceImpl_->CompareTripleIdentifier(accountId, identifier, meta);
1096     ASSERT_EQ(status, false);
1097 }
1098 
1099 /**
1100 * @tc.name: OnUserChangeTest001
1101 * @tc.desc: OnUserChange test
1102 * @tc.type: FUNC
1103 * @tc.author: wangbin
1104 */
1105 HWTEST_F(KvdbServiceImplTest, OnUserChangeTest001, TestSize.Level0)
1106 {
1107     ZLOGI("OnUserChangeTest001 start");
1108     uint32_t code = 1;
1109     std::string user = "OH_USER_test";
1110     std::string account = "OH_ACCOUNT_test";
1111     auto status = kvdbServiceImpl_->OnUserChange(code, user, account);
1112     ZLOGI("OnUserChangeTest001 status = :%{public}d", status);
1113     ASSERT_EQ(status, Status::SUCCESS);
1114 }
1115 
1116 /**
1117 * @tc.name: OnReadyTest001
1118 * @tc.desc: OnReady test
1119 * @tc.type: FUNC
1120 * @tc.author: wangbin
1121 */
1122 HWTEST_F(KvdbServiceImplTest, OnReadyTest001, TestSize.Level0)
1123 {
1124     ZLOGI("OnReadyTest001 start");
1125     std::string device = "OH_device_test";
1126     auto status = kvdbServiceImpl_->OnReady(device);
1127     ZLOGI("OnReadyTest001 status = :%{public}d", status);
1128     ASSERT_EQ(status, Status::SUCCESS);
1129     status = kvdbServiceImpl_->OnSessionReady(device);
1130     ASSERT_EQ(status, Status::SUCCESS);
1131 }
1132 
1133 /**
1134 * @tc.name: ResolveAutoLaunch
1135 * @tc.desc: ResolveAutoLaunch function test.
1136 * @tc.type: FUNC
1137 * @tc.author: SQL
1138 */
1139 HWTEST_F(KvdbServiceImplTest, ResolveAutoLaunch, TestSize.Level0)
1140 {
1141     StoreId id;
1142     id.storeId = "id";
1143     Status status = manager.GetSingleKvStore(create, appId, id, kvStore);
1144     EXPECT_NE(kvStore, nullptr);
1145     EXPECT_EQ(status, Status::SUCCESS);
1146     std::string identifier = "identifier";
1147     DistributedKv::KVDBServiceImpl::DBLaunchParam launchParam;
1148     launchParam.userId = "user1";
1149     auto result = kvdbServiceImpl_->ResolveAutoLaunch(identifier, launchParam);
1150     EXPECT_EQ(result, Status::STORE_NOT_FOUND);
1151     std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(1, 0);
1152     Bootstrap::GetInstance().LoadDirectory();
1153     Bootstrap::GetInstance().LoadCheckers();
1154     DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors);
1155     DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter();
1156     DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener();
1157     std::vector<StoreMetaData> datas;
1158     CreateStoreMetaData(datas, launchParam);
1159     EXPECT_CALL(*metaDataMock, LoadMeta(testing::_, testing::_, testing::_))
1160         .WillRepeatedly(testing::DoAll(testing::SetArgReferee<1>(datas), testing::Return(true)));
1161     result = kvdbServiceImpl_->ResolveAutoLaunch(identifier, launchParam);
1162     EXPECT_EQ(result, Status::SUCCESS);
1163 }
1164 
1165 /**
1166 * @tc.name: IsRemoteChange
1167 * @tc.desc: IsRemoteChange function test.
1168 * @tc.type: FUNC
1169 * @tc.author: wangbin
1170 */
1171 HWTEST_F(KvdbServiceImplTest, IsRemoteChangeTest, TestSize.Level0)
1172 {
1173     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
1174     std::string devices= "IsRemoteChangeTest";
1175     auto changes = kvdbServiceImpl_->IsRemoteChange(meta, devices);
1176     EXPECT_EQ(changes, true);
1177 }
1178 
1179 /**
1180 * @tc.name: PutSwitch
1181 * @tc.desc: PutSwitch function test.
1182 * @tc.type: FUNC
1183 * @tc.author: SQL
1184 */
1185 HWTEST_F(KvdbServiceImplTest, PutSwitch, TestSize.Level0)
1186 {
1187     StoreId id;
1188     id.storeId = "id1";
1189     Status status = manager.GetSingleKvStore(create, appId, id, kvStore);
1190     ASSERT_NE(kvStore, nullptr);
1191     ASSERT_EQ(status, Status::SUCCESS);
1192     DistributedKv::SwitchData switchData;
1193     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
1194     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
1195     switchData.value = DeviceMatrix::INVALID_VALUE;
1196     switchData.length = DeviceMatrix::INVALID_LEVEL;
1197     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
1198     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
1199     switchData.value = DeviceMatrix::INVALID_MASK;
1200     switchData.length = DeviceMatrix::INVALID_LENGTH;
1201     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
1202     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
1203     switchData.value = DeviceMatrix::INVALID_VALUE;
1204     switchData.length = DeviceMatrix::INVALID_LENGTH;
1205     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
1206     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
1207     switchData.value = DeviceMatrix::INVALID_MASK;
1208     switchData.length = DeviceMatrix::INVALID_LEVEL;
1209     status = kvdbServiceImpl_->PutSwitch(appId, switchData);
1210     EXPECT_EQ(status, Status::SUCCESS);
1211     std::string networkId = "networkId";
1212     status = kvdbServiceImpl_->GetSwitch(appId, networkId, switchData);
1213     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
1214 }
1215 
1216 /**
1217 * @tc.name: DoCloudSync
1218 * @tc.desc: DoCloudSync error function test.
1219 * @tc.type: FUNC
1220 * @tc.author: SQL
1221 */
1222 HWTEST_F(KvdbServiceImplTest, DoCloudSync, TestSize.Level0)
1223 {
1224     StoreId id;
1225     id.storeId = "id1";
1226     Status status = manager.GetSingleKvStore(create, appId, id, kvStore);
1227     ASSERT_NE(kvStore, nullptr);
1228     ASSERT_EQ(status, Status::SUCCESS);
1229     StoreMetaData metaData;
1230     SyncInfo syncInfo;
1231     status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
1232     EXPECT_EQ(status, Status::NOT_SUPPORT);
1233     syncInfo.devices = { "device1", "device2" };
1234     syncInfo.query = "query";
1235     metaData.enableCloud = true;
1236     status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
1237     EXPECT_EQ(status, Status::CLOUD_DISABLED);
1238 }
1239 
1240 /**
1241 * @tc.name: ConvertDbStatus
1242 * @tc.desc: ConvertDbStatus test the return result of input with different values.
1243 * @tc.type: FUNC
1244 * @tc.author: SQL
1245 */
1246 HWTEST_F(KvdbServiceImplTest, ConvertDbStatus, TestSize.Level0)
1247 {
1248     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::BUSY), Status::DB_ERROR);
1249     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::DB_ERROR), Status::DB_ERROR);
1250     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::OK), Status::SUCCESS);
1251     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_ARGS), Status::INVALID_ARGUMENT);
1252     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::NOT_FOUND), Status::KEY_NOT_FOUND);
1253     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_VALUE_FIELDS), Status::INVALID_VALUE_FIELDS);
1254     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_FIELD_TYPE), Status::INVALID_FIELD_TYPE);
1255     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::CONSTRAIN_VIOLATION), Status::CONSTRAIN_VIOLATION);
1256     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_FORMAT), Status::INVALID_FORMAT);
1257     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_QUERY_FORMAT), Status::INVALID_QUERY_FORMAT);
1258     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::INVALID_QUERY_FIELD), Status::INVALID_QUERY_FIELD);
1259     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::NOT_SUPPORT), Status::NOT_SUPPORT);
1260     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::TIME_OUT), Status::TIME_OUT);
1261     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::OVER_MAX_LIMITS), Status::OVER_MAX_LIMITS);
1262     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::EKEYREVOKED_ERROR), Status::SECURITY_LEVEL_ERROR);
1263     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::SECURITY_OPTION_CHECK_ERROR), Status::SECURITY_LEVEL_ERROR);
1264     EXPECT_EQ(kvdbServiceImpl_->ConvertDbStatus(DBStatus::SCHEMA_VIOLATE_VALUE), Status::ERROR);
1265 }
1266 
1267 /**
1268 * @tc.name: ConvertDBMode
1269 * @tc.desc: ConvertDBMode test the return result of input with different values.
1270 * @tc.type: FUNC
1271 * @tc.author: SQL
1272 */
1273 HWTEST_F(KvdbServiceImplTest, ConvertDBMode, TestSize.Level0)
1274 {
1275     auto status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PUSH);
1276     EXPECT_EQ(status, DBMode::SYNC_MODE_PUSH_ONLY);
1277     status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PULL);
1278     EXPECT_EQ(status, DBMode::SYNC_MODE_PULL_ONLY);
1279     status = kvdbServiceImpl_->ConvertDBMode(SyncMode::PUSH_PULL);
1280     EXPECT_EQ(status, DBMode::SYNC_MODE_PUSH_PULL);
1281 }
1282 
1283 /**
1284 * @tc.name: ConvertGeneralSyncMode
1285 * @tc.desc: ConvertGeneralSyncMode test the return result of input with different values.
1286 * @tc.type: FUNC
1287 * @tc.author: SQL
1288 */
1289 HWTEST_F(KvdbServiceImplTest, ConvertGeneralSyncMode, TestSize.Level0)
1290 {
1291     auto status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_SUBSCRIBE);
1292     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE);
1293     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_UNSUBSCRIBE);
1294     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE);
1295     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, SyncAction::ACTION_SYNC);
1296     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PUSH);
1297     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PULL, SyncAction::ACTION_SYNC);
1298     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PULL);
1299     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH_PULL, SyncAction::ACTION_SYNC);
1300     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_PULL_PUSH);
1301     auto action = static_cast<SyncAction>(SyncAction::ACTION_UNSUBSCRIBE + 1);
1302     status = kvdbServiceImpl_->ConvertGeneralSyncMode(SyncMode::PUSH, action);
1303     EXPECT_EQ(status, GeneralStore::SyncMode::NEARBY_END);
1304 }
1305 
1306 /**
1307 * @tc.name: ConvertType
1308 * @tc.desc: ConvertType test the return result of input with different values.
1309 * @tc.type: FUNC
1310 * @tc.author: SQL
1311 */
1312 HWTEST_F(KvdbServiceImplTest, ConvertType, TestSize.Level0)
1313 {
1314     auto status = kvdbServiceImpl_->ConvertType(SyncMode::PUSH);
1315     EXPECT_EQ(status, ChangeType::CHANGE_LOCAL);
1316     status = kvdbServiceImpl_->ConvertType(SyncMode::PULL);
1317     EXPECT_EQ(status, ChangeType::CHANGE_REMOTE);
1318     status = kvdbServiceImpl_->ConvertType(SyncMode::PUSH_PULL);
1319     EXPECT_EQ(status, ChangeType::CHANGE_ALL);
1320     auto action = static_cast<SyncMode>(SyncMode::PUSH_PULL + 1);
1321     status = kvdbServiceImpl_->ConvertType(action);
1322     EXPECT_EQ(status, ChangeType::CHANGE_ALL);
1323 }
1324 
1325 /**
1326 * @tc.name: ConvertAction
1327 * @tc.desc: ConvertAction test the return result of input with different values.
1328 * @tc.type: FUNC
1329 * @tc.author: SQL
1330 */
1331 HWTEST_F(KvdbServiceImplTest, ConvertAction, TestSize.Level0)
1332 {
1333     auto status = kvdbServiceImpl_->ConvertAction(Action::INSERT);
1334     EXPECT_EQ(status, SwitchState::INSERT);
1335     status = kvdbServiceImpl_->ConvertAction(Action::UPDATE);
1336     EXPECT_EQ(status, SwitchState::UPDATE);
1337     status = kvdbServiceImpl_->ConvertAction(Action::DELETE);
1338     EXPECT_EQ(status, SwitchState::DELETE);
1339     auto action = static_cast<Action>(Action::DELETE + 1);
1340     status = kvdbServiceImpl_->ConvertAction(action);
1341     EXPECT_EQ(status, SwitchState::INSERT);
1342 }
1343 
1344 /**
1345 * @tc.name: GetSyncMode
1346 * @tc.desc: GetSyncMode test the return result of input with different values.
1347 * @tc.type: FUNC
1348 * @tc.author: SQL
1349 */
1350 HWTEST_F(KvdbServiceImplTest, GetSyncMode, TestSize.Level0)
1351 {
1352     auto status = kvdbServiceImpl_->GetSyncMode(true, true);
1353     EXPECT_EQ(status, SyncMode::PUSH_PULL);
1354     status = kvdbServiceImpl_->GetSyncMode(true, false);
1355     EXPECT_EQ(status, SyncMode::PUSH);
1356     status = kvdbServiceImpl_->GetSyncMode(false, true);
1357     EXPECT_EQ(status, SyncMode::PULL);
1358     status = kvdbServiceImpl_->GetSyncMode(false, false);
1359     EXPECT_EQ(status, SyncMode::PUSH_PULL);
1360 }
1361 
1362 /**
1363 * @tc.name: DoCloudSync02
1364 * @tc.desc: DoCloudSync02 function test.
1365 * @tc.type: FUNC
1366 * @tc.author:
1367 */
1368 HWTEST_F(KvdbServiceImplTest, DoCloudSync02, TestSize.Level0)
1369 {
1370     delegate_.isNetworkAvailable_ = false;
1371     auto cloudServerMock = new CloudServerMock();
1372     CloudServer::RegisterCloudInstance(cloudServerMock);
1373     StoreMetaData metaData;
1374     metaData.enableCloud = true;
1375     SyncInfo syncInfo;
1376     auto status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
1377     EXPECT_EQ(status, OHOS::DistributedKv::Status::NETWORK_ERROR);
1378 }
1379 
1380 /**
1381 * @tc.name: DoCloudSync
1382 * @tc.desc: DoCloudSync error function test.
1383 * @tc.type: FUNC
1384 * @tc.author:
1385 */
1386 HWTEST_F(KvdbServiceImplTest, DoCloudSync01, TestSize.Level0)
1387 {
1388     StoreId id;
1389     id.storeId = "id1";
1390     Status status = manager.GetSingleKvStore(create, appId, id, kvStore);
1391     ASSERT_NE(kvStore, nullptr);
1392     ASSERT_EQ(status, Status::SUCCESS);
1393     StoreMetaData metaData;
1394     metaData.enableCloud = false;
1395     SyncInfo syncInfo;
1396     status = kvdbServiceImpl_->DoCloudSync(metaData, syncInfo);
1397     EXPECT_EQ(status, Status::NOT_SUPPORT);
1398 }
1399 
1400 /**
1401 * @tc.name: OnAsyncCompleteTest002
1402 * @tc.desc: OnAsyncComplete function test.
1403 * @tc.type: FUNC
1404 * @tc.author:
1405 */
1406 HWTEST_F(KvdbServiceImplTest, OnAsyncCompleteTest002, TestSize.Level0)
1407 {
1408     DistributedKv::Statistic upload;
1409     upload.failed = 1;    // test
1410     upload.success = 1;   // test
1411     upload.total = 1;     // test
1412     upload.untreated = 1; // test
1413     DistributedKv::Statistic download;
1414     download.failed = 1;    // test
1415     download.success = 1;   // test
1416     download.total = 1;     // test
1417     download.untreated = 1; // test
1418     DistributedKv::TableDetail details;
1419     details.download = download;
1420     details.upload = upload;
1421     DistributedKv::ProgressDetail detail;
1422     detail.code = 1;     // test
1423     detail.progress = 1; // test
1424     detail.details = details;
1425     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
1426     sptr<DistributedKv::IKVDBNotifier> notifier = nullptr;
1427     syncAgent.pid_ = 1;                   // test
1428     syncAgent.switchesObserverCount_ = 1; // test
1429     syncAgent.appId_ = { "ohos.OnAsyncCompleteTest.kvdb" };
1430     syncAgent.notifier_ = notifier;
1431     EXPECT_EQ(notifier, nullptr);
1432     uint32_t tokenId = 2;
1433     uint64_t seqNum = 1;
1434     kvdbServiceImpl_->syncAgents_.Insert(tokenId, syncAgent);
1435     kvdbServiceImpl_->OnAsyncComplete(tokenId, seqNum, std::move(detail));
1436     EXPECT_TRUE(kvdbServiceImpl_->syncAgents_.Find(tokenId).first);
1437     kvdbServiceImpl_->OnAsyncComplete(0, 1, std::move(detail));
1438     EXPECT_TRUE(kvdbServiceImpl_->syncAgents_.Find(tokenId).first);
1439 }
1440 
1441 /**
1442 * @tc.name: OnAsyncCompleteTest003
1443 * @tc.desc: OnAsyncComplete function test.
1444 * @tc.type: FUNC
1445 * @tc.author:
1446 */
1447 HWTEST_F(KvdbServiceImplTest, OnAsyncCompleteTest003, TestSize.Level0)
1448 {
1449     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
1450     sptr<DistributedKv::IKVDBNotifier> notifier;
1451     syncAgent.pid_ = 1;                   // test
1452     syncAgent.switchesObserverCount_ = 1; // test
1453     syncAgent.appId_ = { "ohos.OnAsyncCompleteTest001.kvdb" };
1454     syncAgent.notifier_ = notifier;
1455     DistributedKv::ProgressDetail detail;
1456     auto tokenId = IPCSkeleton::GetCallingTokenID();
1457     kvdbServiceImpl_->syncAgents_.Insert(tokenId, syncAgent);
1458     kvdbServiceImpl_->OnAsyncComplete(tokenId, 1, std::move(detail));
1459     EXPECT_TRUE(kvdbServiceImpl_->syncAgents_.Find(tokenId).first);
1460 }
1461 
1462 /**
1463 * @tc.name: DeleteTest004
1464 * @tc.desc: Delete Test
1465 * @tc.type: FUNC
1466 * @tc.author:
1467 */
1468 HWTEST_F(KvdbServiceImplTest, DeleteTest004, TestSize.Level0)
1469 {
1470     ZLOGI("DeleteTest004 start");
1471     AppId appId = { "ohos.kvdbserviceimpl.test" };
1472     StoreId storeId = { "meta_test_storeid" };
1473     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
1474     syncAgent.pid_ = 1;
1475     auto tokenId = IPCSkeleton::GetCallingTokenID();
1476     auto status = kvdbServiceImpl_->Delete(appId, storeId, 0);
1477     ZLOGI("DeleteTest002 status = :%{public}d", status);
1478     EXPECT_NE(tokenId, syncAgent.pid_);
1479     ASSERT_EQ(status, Status::SUCCESS);
1480 }
1481 
1482 /**
1483 * @tc.name: syncTest002
1484 * @tc.desc: Sync
1485 * @tc.type: FUNC
1486 * @tc.author:
1487 */
1488 HWTEST_F(KvdbServiceImplTest, syncTest002, TestSize.Level0)
1489 {
1490     ZLOGI("syncTest002 start");
1491     StoreMetaData metaData;
1492     auto mm = std::numeric_limits<uint64_t>::max();
1493     metaData.isAutoSync = true;
1494     SyncInfo syncInfo;
1495     syncInfo.devices = { "device1", "device2" };
1496     syncInfo.query = "query";
1497     syncInfo.seqId = mm; // test
1498     auto status = kvdbServiceImpl_->Sync(appId, storeId, 0, syncInfo);
1499     ASSERT_EQ(syncInfo.seqId, std::numeric_limits<uint64_t>::max());
1500     ZLOGI("syncTest002 status = :%{public}d", status);
1501     ASSERT_NE(status, Status::SUCCESS);
1502 }
1503 
1504 /**
1505 * @tc.name: syncTest003
1506 * @tc.desc: Sync
1507 * @tc.type: FUNC
1508 * @tc.author:
1509 */
1510 HWTEST_F(KvdbServiceImplTest, syncTest003, TestSize.Level0)
1511 {
1512     ZLOGI("syncTest003 start");
1513     StoreMetaData meta = kvdbServiceImpl_->GetStoreMetaData(appId, storeId);
1514     StoreMetaData metaData;
1515     metaData.isAutoSync = true;
1516     StoreMetaDataLocal localMeta;
1517     PolicyValue value;
1518     value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE;
1519     localMeta.policies = { std::move(value) };
1520     SyncInfo syncInfo;
1521     syncInfo.seqId = std::numeric_limits<uint64_t>::max();
1522     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
1523         .WillOnce(testing::Return(true))
1524         .WillRepeatedly(testing::Return(true));
1525     auto status = kvdbServiceImpl_->Sync(appId, storeId, 0, syncInfo);
1526     EXPECT_EQ(localMeta.HasPolicy(DistributedKv::IMMEDIATE_SYNC_ON_CHANGE), false);
1527     EXPECT_NE(status, Status::SUCCESS);
1528     EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_))
1529         .WillOnce(testing::Return(false))
1530         .WillRepeatedly(testing::Return(false));
1531     status = kvdbServiceImpl_->Sync(appId, storeId, 0, syncInfo);
1532     EXPECT_EQ(localMeta.HasPolicy(DistributedKv::IMMEDIATE_SYNC_ON_ONLINE), true);
1533 }
1534 
1535 /**
1536 * @tc.name: SyncTest004
1537 * @tc.desc: twin application sync test
1538 * @tc.type: FUNC
1539 */
1540 HWTEST_F(KvdbServiceImplTest, SyncTest004, TestSize.Level0)
1541 {
1542     EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(testing::_))
1543         .WillOnce(testing::Return(ATokenTypeEnum::TOKEN_HAP))
1544         .WillRepeatedly(testing::Return(ATokenTypeEnum::TOKEN_HAP));
1545     EXPECT_CALL(*accTokenMock, GetHapTokenInfo(testing::_, testing::_))
1546         .WillOnce(testing::Return(0))
1547         .WillRepeatedly(testing::Return(0));
1548     SyncInfo syncInfo;
1549     auto status = kvdbServiceImpl_->Sync(appId, storeId, 0, syncInfo);
1550     ASSERT_EQ(status, Status::NOT_SUPPORT);
1551 }
1552 
1553 /**
1554 * @tc.name: GetSyncParamTest001
1555 * @tc.desc: GetStoreIds
1556 * @tc.type: FUNC
1557 * @tc.author:
1558 */
1559 HWTEST_F(KvdbServiceImplTest, GetSyncParamTest002, TestSize.Level0)
1560 {
1561     ZLOGI("GetSyncParamTest001 start");
1562     Status status1 = manager.GetSingleKvStore(create, appId, storeId, kvStore);
1563     ASSERT_NE(kvStore, nullptr);
1564     ASSERT_EQ(status1, Status::SUCCESS);
1565     OHOS::DistributedKv::KvSyncParam syncparam;
1566     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
1567     syncAgent.pid_ = 1;
1568     auto tokenId = IPCSkeleton::GetCallingTokenID();
1569     EXPECT_NE(tokenId, syncAgent.pid_);
1570     auto status = kvdbServiceImpl_->GetSyncParam(appId, storeId, 0, syncparam);
1571     ZLOGI("GetSyncParamTest002 status = :%{public}d", status);
1572     ASSERT_EQ(status, Status::SUCCESS);
1573 }
1574 
1575 /**
1576 * @tc.name: SubscribeSwitchData
1577 * @tc.desc: SubscribeSwitchData function test.
1578 * @tc.type: FUNC
1579 * @tc.author:
1580 */
1581 HWTEST_F(KvdbServiceImplTest, SubscribeSwitchData, TestSize.Level0)
1582 {
1583     options_.isNeedCompress = false;
1584     std::vector<uint8_t> password{};
1585     StoreMetaData metaData;
1586     auto status = kvdbServiceImpl_->AfterCreate(appId, storeId, options_, password);
1587     ASSERT_EQ(status, Status::SUCCESS);
1588     auto tokenId = IPCSkeleton::GetCallingTokenID();
1589     DistributedKv::KVDBServiceImpl::SyncAgent syncAgent;
1590     syncAgent.switchesObserverCount_ = 1;
1591     syncAgent.pid_ = tokenId;
1592     syncAgent.notifier_ = nullptr;
1593     status = kvdbServiceImpl_->SubscribeSwitchData(appId);
1594     EXPECT_EQ(status, Status::SUCCESS);
1595     ASSERT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKey(), metaData));
1596     kvdbServiceImpl_->syncAgents_.Insert(IPCSkeleton::GetCallingTokenID(), syncAgent);
1597     EXPECT_EQ(tokenId, syncAgent.pid_);
1598     status = kvdbServiceImpl_->UnregServiceNotifier(appId);
1599     ASSERT_EQ(status, Status::SUCCESS);
1600 }
1601 } // namespace DistributedDataTest
1602 } // namespace OHOS::Test
1603