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 "CloudServiceImplTest"
16 #include "cloud_service_impl.h"
17
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20
21 #include "accesstoken_kit.h"
22 #include "account/account_delegate.h"
23 #include "bootstrap.h"
24 #include "checker_mock.h"
25 #include "cloud/change_event.h"
26 #include "cloud/cloud_event.h"
27 #include "cloud/cloud_server.h"
28 #include "cloud/cloud_share_event.h"
29 #include "cloud/schema_meta.h"
30 #include "cloud_types.h"
31 #include "cloud_types_util.h"
32 #include "cloud_value_util.h"
33 #include "communicator/device_manager_adapter.h"
34 #include "device_matrix.h"
35 #include "eventcenter/event_center.h"
36 #include "feature/feature_system.h"
37 #include "ipc_skeleton.h"
38 #include "log_print.h"
39 #include "metadata/meta_data_manager.h"
40 #include "metadata/store_meta_data.h"
41 #include "metadata/store_meta_data_local.h"
42 #include "mock/db_store_mock.h"
43 #include "mock/general_store_mock.h"
44 #include "mock/meta_data_manager_mock.h"
45 #include "model/component_config.h"
46 #include "network/network_delegate.h"
47 #include "network_delegate_mock.h"
48 #include "rdb_query.h"
49 #include "rdb_service.h"
50 #include "rdb_service_impl.h"
51 #include "rdb_types.h"
52 #include "store/auto_cache.h"
53 #include "store/store_info.h"
54 #include "token_setproc.h"
55 #include "directory/directory_manager.h"
56
57 using namespace testing::ext;
58 using namespace testing;
59 using namespace OHOS::DistributedData;
60 using namespace OHOS::Security::AccessToken;
61 using Confirmation = OHOS::CloudData::Confirmation;
62 using Status = OHOS::CloudData::CloudService::Status;
63 using CloudSyncScene = OHOS::CloudData::CloudServiceImpl::CloudSyncScene;
64 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
65
66 namespace OHOS::Test {
67 namespace DistributedDataTest {
68 static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName";
69 static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid";
70 static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store";
71 static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1";
72 class CloudServiceImplTest : public testing::Test {
73 public:
74 static void SetUpTestCase(void);
75 static void TearDownTestCase(void);
76 void SetUp();
77 void TearDown();
78
79 static void InitMetaData();
80 static void CheckDelMeta(StoreMetaMapping &metaMapping, StoreMetaData &meta, StoreMetaData &meta1);
81 static std::shared_ptr<CloudData::CloudServiceImpl> cloudServiceImpl_;
82 static NetworkDelegateMock delegate_;
83 protected:
84 static std::shared_ptr<DBStoreMock> dbStoreMock_;
85 static StoreMetaData metaData_;
86 };
87 std::shared_ptr<CloudData::CloudServiceImpl> CloudServiceImplTest::cloudServiceImpl_ =
88 std::make_shared<CloudData::CloudServiceImpl>();
89 NetworkDelegateMock CloudServiceImplTest::delegate_;
90 std::shared_ptr<DBStoreMock> CloudServiceImplTest::dbStoreMock_ = std::make_shared<DBStoreMock>();
91 StoreMetaData CloudServiceImplTest::metaData_;
92
CheckDelMeta(StoreMetaMapping & metaMapping,StoreMetaData & meta,StoreMetaData & meta1)93 void CloudServiceImplTest::CheckDelMeta(StoreMetaMapping &metaMapping, StoreMetaData &meta, StoreMetaData &meta1)
94 {
95 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
96 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
97 metaMapping.cloudPath = "";
98 metaMapping.dataDir = DirectoryManager::GetInstance().GetStorePath(metaMapping) + "/" + metaMapping.storeId;
99 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
100 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
101 meta.user = "100";
102 auto res = cloudServiceImpl_->GetStoreMetaData(meta);
103 EXPECT_EQ(res, false);
104
105 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
106 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
107 metaMapping.dataDir = "";
108 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
109 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
110 meta.user = "100";
111 res = cloudServiceImpl_->GetStoreMetaData(meta);
112 EXPECT_EQ(res, false);
113 }
114
InitMetaData()115 void CloudServiceImplTest::InitMetaData()
116 {
117 metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
118 metaData_.appId = "test_rdb_cloud_service_impl_appid";
119 metaData_.bundleName = "test_rdb_cloud_service_impl_bundleName";
120 metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
121 metaData_.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData_.tokenId));
122 metaData_.area = OHOS::DistributedKv::EL1;
123 metaData_.instanceId = 0;
124 metaData_.isAutoSync = true;
125 metaData_.storeType = DistributedRdb::RDB_DEVICE_COLLABORATION;
126 metaData_.storeId = "test_cloud_service_impl_store";
127 metaData_.dataDir = "/test_cloud_service_impl_store";
128 }
129
SetUpTestCase(void)130 void CloudServiceImplTest::SetUpTestCase(void)
131 {
132 size_t max = 12;
133 size_t min = 5;
134 auto executor = std::make_shared<ExecutorPool>(max, min);
135 DeviceManagerAdapter::GetInstance().Init(executor);
136 Bootstrap::GetInstance().LoadCheckers();
137 CryptoManager::GetInstance().GenerateRootKey();
138 MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, "");
139 NetworkDelegate::RegisterNetworkInstance(&delegate_);
140 InitMetaData();
141 }
142
TearDownTestCase()143 void CloudServiceImplTest::TearDownTestCase() { }
144
SetUp()145 void CloudServiceImplTest::SetUp() { }
146
TearDown()147 void CloudServiceImplTest::TearDown() { }
148
149 /**
150 * @tc.name: EnableCloud001
151 * @tc.desc: Test EnableCloud functions with user is invalid.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(CloudServiceImplTest, EnableCloud001, TestSize.Level0)
156 {
157 ZLOGI("CloudServiceImplTest EnableCloud001 start");
158 std::map<std::string, int32_t> switches;
159 switches.insert_or_assign(TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
160 auto status = cloudServiceImpl_->EnableCloud(TEST_CLOUD_APPID, switches);
161 EXPECT_EQ(status, CloudData::CloudService::ERROR);
162 }
163
164 /**
165 * @tc.name: DisableCloud001
166 * @tc.desc: Test DisableCloud functions with user is invalid.
167 * @tc.type: FUNC
168 * @tc.require:
169 */
170 HWTEST_F(CloudServiceImplTest, DisableCloud001, TestSize.Level0)
171 {
172 ZLOGI("CloudServiceImplTest DisableCloud001 start");
173 auto status = cloudServiceImpl_->DisableCloud(TEST_CLOUD_APPID);
174 EXPECT_EQ(status, CloudData::CloudService::ERROR);
175 }
176
177 /**
178 * @tc.name: ChangeAppSwitch001
179 * @tc.desc: Test ChangeAppSwitch functions with user is invalid.
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(CloudServiceImplTest, ChangeAppSwitch001, TestSize.Level0)
184 {
185 ZLOGI("CloudServiceImplTest ChangeAppSwitch001 start");
186 auto status =
187 cloudServiceImpl_->ChangeAppSwitch(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
188 EXPECT_EQ(status, CloudData::CloudService::ERROR);
189 }
190
191 /**
192 * @tc.name: Clean001
193 * @tc.desc: Test Clean functions with user is invalid.
194 * @tc.type: FUNC
195 * @tc.require:
196 */
197 HWTEST_F(CloudServiceImplTest, Clean001, TestSize.Level0)
198 {
199 ZLOGI("CloudServiceImplTest Clean001 start");
200 std::map<std::string, int32_t> actions;
201 actions.insert_or_assign(TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
202 auto status = cloudServiceImpl_->Clean(TEST_CLOUD_APPID, actions);
203 EXPECT_EQ(status, CloudData::CloudService::ERROR);
204 }
205
206 /**
207 * @tc.name: NotifyDataChange001
208 * @tc.desc: Test the EnableCloud function in case it doesn't get cloudInfo.
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212 HWTEST_F(CloudServiceImplTest, NotifyDataChange001, TestSize.Level0)
213 {
214 ZLOGI("CloudServiceImplTest NotifyDataChange001 start");
215 auto status = cloudServiceImpl_->NotifyDataChange(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE);
216 EXPECT_EQ(status, CloudData::CloudService::INVALID_ARGUMENT);
217 }
218
219 /**
220 * @tc.name: ExecuteStatistics001
221 * @tc.desc: Test the ExecuteStatistics function if the package name does not support CloudSync.
222 * @tc.type: FUNC
223 * @tc.require:
224 */
225 HWTEST_F(CloudServiceImplTest, ExecuteStatistics001, TestSize.Level0)
226 {
227 ZLOGI("CloudServiceImplTest ExecuteStatistics001 start");
228 CloudInfo cloudInfo;
229 CloudInfo::AppInfo appInfo;
230 appInfo.bundleName = TEST_CLOUD_BUNDLE;
231 cloudInfo.apps[TEST_CLOUD_BUNDLE] = std::move(appInfo);
232 SchemaMeta::Database database;
233 database.name = TEST_CLOUD_STORE;
234 database.alias = TEST_CLOUD_DATABASE_ALIAS_1;
235 SchemaMeta schemaMeta;
236 schemaMeta.bundleName = "testBundle";
237 schemaMeta.databases.emplace_back(database);
238 auto result = cloudServiceImpl_->ExecuteStatistics("", cloudInfo, schemaMeta);
239 EXPECT_TRUE(result.empty());
240 }
241
242 /**
243 * @tc.name: QueryStatistics001
244 * @tc.desc: When metadata is not supported store test the QueryStatistics function.
245 * @tc.type: FUNC
246 * @tc.require:
247 */
248 HWTEST_F(CloudServiceImplTest, QueryStatistics001, TestSize.Level0)
249 {
250 ZLOGI("CloudServiceImplTest QueryStatistics start");
251 StoreMetaData metaData;
252 metaData.storeType = -1;
253 DistributedData::Database database;
254 auto result = cloudServiceImpl_->QueryStatistics(metaData, database);
255 EXPECT_TRUE(result.empty());
256 }
257
258 /**
259 * @tc.name: QueryLastSyncInfo001
260 * @tc.desc: Test QueryLastSyncInfo functions with invalid parameter.
261 * @tc.type: FUNC
262 * @tc.require:
263 */
264 HWTEST_F(CloudServiceImplTest, QueryLastSyncInfo001, TestSize.Level0)
265 {
266 ZLOGI("CloudServiceImplTest QueryLastSyncInfo start");
267 auto [status, result] = cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE);
268 EXPECT_EQ(status, CloudData::CloudService::ERROR);
269 EXPECT_TRUE(result.empty());
270 }
271
272 /**
273 * @tc.name: OnBind001
274 * @tc.desc: Test OnBind functions with invalid parameter.
275 * @tc.type: FUNC
276 * @tc.require:
277 */
278 HWTEST_F(CloudServiceImplTest, OnBind001, TestSize.Level0)
279 {
280 ZLOGI("CloudServiceImplTest OnBind001 start");
281 auto status = cloudServiceImpl_->OnBind({});
282 EXPECT_EQ(status, GeneralError::E_INVALID_ARGS);
283 }
284
285 /**
286 * @tc.name: UpdateSchema001
287 * @tc.desc: Test UpdateSchema001 functions with invalid parameter.
288 * @tc.type: FUNC
289 * @tc.require:
290 */
291 HWTEST_F(CloudServiceImplTest, UpdateSchema001, TestSize.Level0)
292 {
293 ZLOGI("CloudServiceImplTest UpdateSchema001 start");
294 int user = -1;
295 auto status = cloudServiceImpl_->UpdateSchema(user, CloudSyncScene::ENABLE_CLOUD);
296 EXPECT_FALSE(status);
297 }
298
299 /**
300 * @tc.name: GetAppSchemaFromServer001
301 * @tc.desc: Test GetAppSchemaFromServer functions not support CloudService.
302 * @tc.type: FUNC
303 * @tc.require:
304 */
305 HWTEST_F(CloudServiceImplTest, GetAppSchemaFromServer001, TestSize.Level0)
306 {
307 ZLOGI("CloudServiceImplTest GetAppSchemaFromServer001 start");
308 int user = -1;
309 auto [status, result] = cloudServiceImpl_->GetAppSchemaFromServer(user, TEST_CLOUD_BUNDLE);
310 EXPECT_EQ(status, CloudData::CloudService::SERVER_UNAVAILABLE);
311 }
312
313 /**
314 * @tc.name: GetCloudInfoFromServer001
315 * @tc.desc: Test GetCloudInfoFromServer functions not support CloudService.
316 * @tc.type: FUNC
317 * @tc.require:
318 */
319 HWTEST_F(CloudServiceImplTest, GetCloudInfoFromServer001, TestSize.Level0)
320 {
321 ZLOGI("CloudServiceImplTest GetCloudInfoFromServer001 start");
322 int user = -1;
323 auto [status, result] = cloudServiceImpl_->GetCloudInfoFromServer(user);
324 EXPECT_EQ(status, CloudData::CloudService::SERVER_UNAVAILABLE);
325 }
326
327 /**
328 * @tc.name: GetCloudInfoFromServer002
329 * @tc.desc: Test GetCloudInfoFromServer functions with cloudinfo is empty.
330 * @tc.type: FUNC
331 * @tc.require:
332 */
333 HWTEST_F(CloudServiceImplTest, GetCloudInfoFromServer002, TestSize.Level0)
334 {
335 ZLOGI("CloudServiceImplTest GetCloudInfoFromServer002 start");
336 CloudServer cloudServer;
337 CloudServer::RegisterCloudInstance(&cloudServer);
338 int user = 100;
339 auto [status, result] = cloudServiceImpl_->GetCloudInfoFromServer(user);
340 EXPECT_EQ(status, E_ERROR);
341 CloudServer::instance_ = nullptr;
342 }
343
344 /**
345 * @tc.name: ReleaseUserInfo001
346 * @tc.desc: Test ReleaseUserInfo functions with invalid parameter.
347 * @tc.type: FUNC
348 * @tc.require:
349 */
350 HWTEST_F(CloudServiceImplTest, ReleaseUserInfo001, TestSize.Level0)
351 {
352 ZLOGI("CloudServiceImplTest ReleaseUserInfo001 start");
353 int user = 100;
354 auto status = cloudServiceImpl_->ReleaseUserInfo(user, CloudSyncScene::ENABLE_CLOUD);
355 EXPECT_TRUE(status);
356 }
357
358 /**
359 * @tc.name: DoSubscribe
360 * @tc.desc: Test the DoSubscribe with not support CloudService
361 * @tc.type: FUNC
362 * @tc.require:
363 */
364 HWTEST_F(CloudServiceImplTest, DoSubscribe, TestSize.Level0)
365 {
366 int32_t user = 100;
367 auto status = cloudServiceImpl_->DoSubscribe(user, CloudSyncScene::ENABLE_CLOUD);
368 EXPECT_TRUE(status);
369 }
370
371 /**
372 * @tc.name: OnAppInstallTest
373 * @tc.desc: Test the OnAppInstallTest
374 * @tc.type: FUNC
375 * @tc.require:
376 */
377 HWTEST_F(CloudServiceImplTest, OnAppInstallTest, TestSize.Level0)
378 {
379 ZLOGI("CloudServiceImplTest OnAppInstallTest start");
380 ASSERT_NE(cloudServiceImpl_, nullptr);
381 ASSERT_NE(cloudServiceImpl_->factory_.staticActs_, nullptr);
382 int32_t user = 0;
383 int32_t index = 0;
384 auto status = cloudServiceImpl_->factory_.staticActs_->OnAppInstall(TEST_CLOUD_BUNDLE, user, index);
385 EXPECT_EQ(status, GeneralError::E_ERROR);
386 }
387
388 /**
389 * @tc.name: OnAppUpdateTest
390 * @tc.desc: Test the OnAppUpdateTest
391 * @tc.type: FUNC
392 * @tc.require:
393 */
394 HWTEST_F(CloudServiceImplTest, OnAppUpdateTest, TestSize.Level0)
395 {
396 ZLOGI("CloudServiceImplTest OnAppUpdateTest start");
397 ASSERT_NE(cloudServiceImpl_, nullptr);
398 ASSERT_NE(cloudServiceImpl_->factory_.staticActs_, nullptr);
399 int32_t user = 0;
400 int32_t index = 0;
401 auto status = cloudServiceImpl_->factory_.staticActs_->OnAppUpdate(TEST_CLOUD_BUNDLE, user, index);
402 EXPECT_EQ(status, CloudData::CloudService::SUCCESS);
403 }
404
405 /**
406 * @tc.name: CloudDriverCheckTest
407 * @tc.desc: Test the CloudDriverCheck
408 * @tc.type: FUNC
409 * @tc.require:
410 */
411 HWTEST_F(CloudServiceImplTest, CloudDriverCheckTest, TestSize.Level0)
412 {
413 ZLOGI("CloudServiceImplTest CloudDriverCheckTest start");
414 ASSERT_NE(cloudServiceImpl_, nullptr);
415 ASSERT_NE(cloudServiceImpl_->factory_.staticActs_, nullptr);
416 CloudServer cloudServer;
417 CloudServer::RegisterCloudInstance(&cloudServer);
418 int32_t user = 0;
419 auto result = cloudServiceImpl_->factory_.staticActs_->CloudDriverCheck(TEST_CLOUD_BUNDLE, user);
420 EXPECT_EQ(result, false);
421 CloudServer::instance_ = nullptr;
422 }
423
424 /**
425 * @tc.name: UpdateSchemaFromServerTest_001
426 * @tc.desc: Test UpdateSchemaFromServer functions
427 * @tc.type: FUNC
428 * @tc.require:
429 */
430 HWTEST_F(CloudServiceImplTest, UpdateSchemaFromServerTest_001, TestSize.Level0)
431 {
432 ZLOGI("CloudServiceImplTest UpdateSchemaFromServerTest_001 start");
433 CloudServer cloudServer;
434 CloudServer::RegisterCloudInstance(&cloudServer);
435 int user = 100;
436 auto status = cloudServiceImpl_->UpdateSchemaFromServer(user);
437 EXPECT_EQ(status, E_ERROR);
438 CloudServer::instance_ = nullptr;
439 }
440
441 /**
442 * @tc.name: UpdateSchemaFromServerTest_002
443 * @tc.desc: Test UpdateSchemaFromServer functions.
444 * @tc.type: FUNC
445 * @tc.require:
446 */
447 HWTEST_F(CloudServiceImplTest, UpdateSchemaFromServerTest_002, TestSize.Level0)
448 {
449 ZLOGI("CloudServiceImplTest UpdateSchemaFromServerTest_002 start");
450 CloudServer cloudServer;
451 CloudServer::RegisterCloudInstance(&cloudServer);
452 int user = 0;
453 CloudInfo cloudInfo;
454 cloudInfo.user = user;
455 DistributedData::CloudInfo::AppInfo appInfo;
456 appInfo.bundleName = TEST_CLOUD_BUNDLE;
457 appInfo.appId = TEST_CLOUD_APPID;
458 appInfo.cloudSwitch = true;
459 cloudInfo.apps = {{ TEST_CLOUD_BUNDLE, appInfo }};
460 auto status = cloudServiceImpl_->UpdateSchemaFromServer(cloudInfo, user);
461 EXPECT_EQ(status, CloudData::CloudService::SUCCESS);
462 CloudServer::instance_ = nullptr;
463 }
464
465 /**
466 * @tc.name: UpdateE2eeEnableTest
467 * @tc.desc: Test UpdateE2eeEnable functions.
468 * @tc.type: FUNC
469 * @tc.require:
470 */
471 HWTEST_F(CloudServiceImplTest, UpdateE2eeEnableTest, TestSize.Level0)
472 {
473 ZLOGI("CloudServiceImplTest UpdateE2eeEnableTest start");
474 ASSERT_NE(cloudServiceImpl_, nullptr);
475 CloudServer cloudServer;
476 auto result = cloudServer.CloudDriverUpdated(TEST_CLOUD_BUNDLE);
477 EXPECT_EQ(result, false);
478
479 std::string schemaKey = "schemaKey";
480 cloudServiceImpl_->UpdateE2eeEnable(schemaKey, true, TEST_CLOUD_BUNDLE);
481 SchemaMeta schemaMeta;
482 ASSERT_FALSE(MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true));
483 }
484
485 /**
486 * @tc.name: Share001
487 * @tc.desc: Test the Share with invalid parameters
488 * @tc.type: FUNC
489 * @tc.require:
490 */
491 HWTEST_F(CloudServiceImplTest, Share001, TestSize.Level0)
492 {
493 std::string sharingRes;
494 CloudData::Participants participants;
495 CloudData::Results results;
496 auto status = cloudServiceImpl_->Share(sharingRes, participants, results);
497 EXPECT_EQ(status, GeneralError::E_ERROR);
498 }
499
500 /**
501 * @tc.name: Unshare001
502 * @tc.desc: Test the Unshare with invalid parameters
503 * @tc.type: FUNC
504 * @tc.require:
505 */
506 HWTEST_F(CloudServiceImplTest, Unshare001, TestSize.Level0)
507 {
508 std::string sharingRes;
509 CloudData::Participants participants;
510 CloudData::Results results;
511 auto status = cloudServiceImpl_->Unshare(sharingRes, participants, results);
512 EXPECT_EQ(status, GeneralError::E_ERROR);
513 }
514
515 /**
516 * @tc.name: Exit001
517 * @tc.desc: Test the Exit with invalid parameters
518 * @tc.type: FUNC
519 * @tc.require:
520 */
521 HWTEST_F(CloudServiceImplTest, Exit001, TestSize.Level0)
522 {
523 std::string sharingRes;
524 std::pair<int32_t, std::string> result;
525 auto status = cloudServiceImpl_->Exit(sharingRes, result);
526 EXPECT_EQ(status, GeneralError::E_ERROR);
527 }
528
529 /**
530 * @tc.name: ChangePrivilege001
531 * @tc.desc: Test the ChangePrivilege with invalid parameters
532 * @tc.type: FUNC
533 * @tc.require:
534 */
535 HWTEST_F(CloudServiceImplTest, ChangePrivilege001, TestSize.Level0)
536 {
537 std::string sharingRes;
538 CloudData::Participants participants;
539 CloudData::Results results;
540 auto status = cloudServiceImpl_->ChangePrivilege(sharingRes, participants, results);
541 EXPECT_EQ(status, GeneralError::E_ERROR);
542 }
543
544 /**
545 * @tc.name: Query001
546 * @tc.desc: Test the Query with invalid parameters
547 * @tc.type: FUNC
548 * @tc.require:
549 */
550 HWTEST_F(CloudServiceImplTest, Query001, TestSize.Level0)
551 {
552 std::string sharingRes;
553 CloudData::QueryResults results;
554 auto status = cloudServiceImpl_->Query(sharingRes, results);
555 EXPECT_EQ(status, GeneralError::E_ERROR);
556 }
557
558 /**
559 * @tc.name: QueryByInvitation001
560 * @tc.desc: Test the QueryByInvitation with invalid parameters
561 * @tc.type: FUNC
562 * @tc.require:
563 */
564 HWTEST_F(CloudServiceImplTest, QueryByInvitation001, TestSize.Level0)
565 {
566 std::string invitation;
567 CloudData::QueryResults results;
568 auto status = cloudServiceImpl_->QueryByInvitation(invitation, results);
569 EXPECT_EQ(status, GeneralError::E_ERROR);
570 }
571
572 /**
573 * @tc.name: ConfirmInvitation001
574 * @tc.desc: Test the ConfirmInvitation with invalid parameters
575 * @tc.type: FUNC
576 * @tc.require:
577 */
578 HWTEST_F(CloudServiceImplTest, ConfirmInvitation001, TestSize.Level0)
579 {
580 int32_t confirmation = 0;
581 std::tuple<int32_t, std::string, std::string> result { 0, "", "" };
582 std::string invitation;
583 auto status = cloudServiceImpl_->ConfirmInvitation(invitation, confirmation, result);
584 EXPECT_EQ(status, GeneralError::E_ERROR);
585 }
586
587 /**
588 * @tc.name: GetStoreMetaData_001
589 * @tc.desc: test GetStoreMetaData LoadMeta success.
590 * @tc.type: FUNC
591 */
592 HWTEST_F(CloudServiceImplTest, GetStoreMetaData_001, TestSize.Level1)
593 {
594 StoreMetaData meta;
595 meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
596 meta.user = "0";
597 meta.bundleName = "bundleName";
598 meta.storeId = "storeName";
599 meta.dataDir = DirectoryManager::GetInstance().GetStorePath(meta) + "/" + meta.storeId;
600 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
601 bool res = cloudServiceImpl_->GetStoreMetaData(meta);
602 EXPECT_EQ(res, true);
603 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
604 meta.instanceId = 1;
605 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
606 res = cloudServiceImpl_->GetStoreMetaData(meta);
607 EXPECT_EQ(res, true);
608 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
609 }
610
611 /**
612 * @tc.name: GetStoreMetaData_002
613 * @tc.desc: test GetStoreMetaData LoadMeta failed and user is 0.
614 * @tc.type: FUNC
615 */
616 HWTEST_F(CloudServiceImplTest, GetStoreMetaData_002, TestSize.Level1)
617 {
618 StoreMetaMapping metaMapping;
619 metaMapping.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
620 metaMapping.user = "0";
621 metaMapping.bundleName = "bundleName";
622 metaMapping.storeId = "storeName";
623 metaMapping.cloudPath = DirectoryManager::GetInstance().GetStorePath(metaMapping) + "/" + metaMapping.storeId;
624 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
625
626 StoreMetaData meta;
627 meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
628 meta.user = "100";
629 meta.bundleName = "bundleName";
630 meta.storeId = "storeName";
631 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
632 bool res = cloudServiceImpl_->GetStoreMetaData(meta);
633 EXPECT_EQ(res, false);
634
635 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
636 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
637 meta.instanceId = 1;
638 metaMapping.instanceId = 1;
639 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
640 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
641 meta.user = "100";
642 res = cloudServiceImpl_->GetStoreMetaData(meta);
643 EXPECT_EQ(res, false);
644
645 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
646 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
647 metaMapping.cloudPath = "";
648 metaMapping.dataDir = DirectoryManager::GetInstance().GetStorePath(metaMapping) + "/" + metaMapping.storeId;
649 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
650 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
651 meta.user = "100";
652 res = cloudServiceImpl_->GetStoreMetaData(meta);
653 EXPECT_EQ(res, false);
654
655 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
656 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
657 metaMapping.dataDir = "";
658 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
659 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta, true), true);
660 meta.user = "100";
661 res = cloudServiceImpl_->GetStoreMetaData(meta);
662 EXPECT_EQ(res, false);
663
664 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
665 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true), true);
666 }
667
668 /**
669 * @tc.name: GetStoreMetaData_003
670 * @tc.desc: test GetStoreMetaData LoadMeta failed and user is not 0.
671 * @tc.type: FUNC
672 */
673 HWTEST_F(CloudServiceImplTest, GetStoreMetaData_003, TestSize.Level1)
674 {
675 StoreMetaMapping metaMapping;
676 metaMapping.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
677 metaMapping.user = "0";
678 metaMapping.bundleName = "bundleName";
679 metaMapping.storeId = "storeName";
680 metaMapping.cloudPath = DirectoryManager::GetInstance().GetStorePath(metaMapping) + "/" + metaMapping.storeId;
681 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
682 StoreMetaData meta;
683 meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
684 meta.user = "100";
685 meta.bundleName = "bundleName";
686 meta.storeId = "storeName";
687 StoreMetaData meta1;
688 meta1.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
689 meta1.user = "0";
690 meta1.bundleName = "bundleName";
691 meta1.storeId = "storeName";
692 meta1.dataDir = DirectoryManager::GetInstance().GetStorePath(meta) + "/" + meta.storeId;
693 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
694 StoreMetaDataLocal localMetaData;
695 bool res = cloudServiceImpl_->GetStoreMetaData(meta);
696 EXPECT_EQ(res, false);
697 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
698 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
699 }
700
701 /**
702 * @tc.name: GetStoreMetaData_004
703 * @tc.desc: test GetStoreMetaData First load of metadata failed and the user is not 0,
704 * then the load of metadata succeeded.
705 * @tc.type: FUNC
706 */
707 HWTEST_F(CloudServiceImplTest, GetStoreMetaData_004, TestSize.Level1)
708 {
709 StoreMetaMapping metaMapping;
710 metaMapping.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
711 metaMapping.user = "0";
712 metaMapping.bundleName = "bundleName";
713 metaMapping.storeId = "storeName";
714 metaMapping.instanceId = 1;
715 metaMapping.cloudPath = DirectoryManager::GetInstance().GetStorePath(metaMapping) + "/" + metaMapping.storeId;
716 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
717 StoreMetaData meta;
718 meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
719 meta.user = "100";
720 meta.bundleName = "bundleName";
721 meta.storeId = "storeName";
722 meta.instanceId = 1;
723 StoreMetaData meta1;
724 meta1.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
725 meta1.user = "0";
726 meta1.bundleName = "bundleName";
727 meta1.storeId = "storeName";
728 meta1.instanceId = 1;
729 meta1.dataDir = DirectoryManager::GetInstance().GetStorePath(meta1) + "/" + meta1.storeId;
730 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
731 StoreMetaDataLocal localMetaData;
732 bool res = cloudServiceImpl_->GetStoreMetaData(meta);
733 EXPECT_EQ(res, false);
734 CheckDelMeta(metaMapping, meta, meta1);
735
736 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
737 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
738 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKeyLocal(), localMetaData, true), true);
739 meta.user = "100";
740 res = cloudServiceImpl_->GetStoreMetaData(meta);
741 EXPECT_EQ(res, false);
742
743 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
744 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKeyLocal(), true), true);
745 localMetaData.isPublic = true;
746 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKey(), meta1, true), true);
747 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(meta1.GetKeyLocal(), localMetaData, true), true);
748 meta.user = "100";
749 metaMapping.cloudPath = DirectoryManager::GetInstance().GetStorePath(meta1) + "/" + meta1.storeId;
750 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(metaMapping.GetKey(), metaMapping, true), true);
751 res = cloudServiceImpl_->GetStoreMetaData(meta);
752 EXPECT_EQ(res, true);
753 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(metaMapping.GetKey(), true), true);
754 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKey(), true), true);
755 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(meta1.GetKeyLocal(), true), true);
756 }
757
758 /**
759 * @tc.name: PreShare_001
760 * @tc.desc: test PreShare is dataDir empty.
761 * @tc.type: FUNC
762 */
763 HWTEST_F(CloudServiceImplTest, PreShare_001, TestSize.Level1)
764 {
765 StoreInfo storeInfo;
766 storeInfo.bundleName = "bundleName";
767 storeInfo.storeName = "storeName";
768 storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
769 storeInfo.user = 100;
770 StoreMetaMapping storeMetaMapping;
771 storeMetaMapping.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
772 storeMetaMapping.user = "100";
773 storeMetaMapping.bundleName = "bundleName";
774 storeMetaMapping.storeId = "storeName";
775 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(storeMetaMapping.GetKey(), storeMetaMapping, true), true);
776 std::shared_ptr<GenQuery> query;
777 auto [errCode, cursor] = cloudServiceImpl_->PreShare(storeInfo, *query);
778 EXPECT_EQ(errCode, E_ERROR);
779 ASSERT_EQ(cursor, nullptr);
780 storeInfo.path = "path";
781 storeMetaMapping.cloudPath = "path1";
782 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(storeMetaMapping.GetKey(), true), true);
783 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(storeMetaMapping.GetKey(), storeMetaMapping, true), true);
784 auto [errCode1, cursor1] = cloudServiceImpl_->PreShare(storeInfo, *query);
785 EXPECT_EQ(errCode1, E_ERROR);
786 ASSERT_EQ(cursor1, nullptr);
787 storeInfo.path = "path";
788 storeMetaMapping.cloudPath = "path";
789 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(storeMetaMapping.GetKey(), true), true);
790 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(storeMetaMapping.GetKey(), storeMetaMapping, true), true);
791 auto [errCode2, cursor2] = cloudServiceImpl_->PreShare(storeInfo, *query);
792 EXPECT_EQ(errCode2, E_ERROR);
793 ASSERT_EQ(cursor2, nullptr);
794 storeInfo.instanceId = 1;
795 storeMetaMapping.instanceId = 1;
796 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(storeMetaMapping.GetKey(), true), true);
797 EXPECT_EQ(MetaDataManager::GetInstance().SaveMeta(storeMetaMapping.GetKey(), storeMetaMapping, true), true);
798 auto [errCode3, cursor3] = cloudServiceImpl_->PreShare(storeInfo, *query);
799 EXPECT_EQ(errCode3, E_ERROR);
800 ASSERT_EQ(cursor3, nullptr);
801 EXPECT_EQ(MetaDataManager::GetInstance().DelMeta(storeMetaMapping.GetKey(), true), true);
802 }
803
804 /**
805 * @tc.name: ChangeConfirmation001
806 * @tc.desc: Test the ChangeConfirmation with invalid parameters
807 * @tc.type: FUNC
808 * @tc.require:
809 */
810 HWTEST_F(CloudServiceImplTest, ChangeConfirmation001, TestSize.Level0)
811 {
812 int32_t confirmation = 0;
813 std::string sharingRes;
814 std::pair<int32_t, std::string> result;
815 auto status = cloudServiceImpl_->ChangeConfirmation(sharingRes, confirmation, result);
816 EXPECT_EQ(status, GeneralError::E_ERROR);
817 }
818
819 /**
820 * @tc.name: GetSharingHandle001
821 * @tc.desc: Test the GetSharingHandle with invalid parameters
822 * @tc.type: FUNC
823 * @tc.require:
824 */
825 HWTEST_F(CloudServiceImplTest, GetSharingHandle001, TestSize.Level0)
826 {
827 CloudData::CloudServiceImpl::HapInfo hapInfo;
828 auto status = cloudServiceImpl_->GetSharingHandle(hapInfo);
829 EXPECT_EQ(status, nullptr);
830 }
831
832 /**
833 * @tc.name: SetCloudStrategy001
834 * @tc.desc: Test the SetCloudStrategy with get hapInfo failed
835 * @tc.type: FUNC
836 * @tc.require:
837 */
838 HWTEST_F(CloudServiceImplTest, SetCloudStrategy001, TestSize.Level0)
839 {
840 CloudData::Strategy strategy = CloudData::Strategy::STRATEGY_NETWORK;
841 std::vector<CommonType::Value> values;
842 values.push_back(CloudData::NetWorkStrategy::WIFI);
843
844 auto status = cloudServiceImpl_->SetCloudStrategy(strategy, values);
845 EXPECT_EQ(status, CloudData::CloudService::ERROR);
846 }
847
848 /**
849 * @tc.name: SetGlobalCloudStrategy001
850 * @tc.desc: Test the SetGlobalCloudStrategy with get hapInfo failed
851 * @tc.type: FUNC
852 * @tc.require:
853 */
854 HWTEST_F(CloudServiceImplTest, SetGlobalCloudStrategy001, TestSize.Level0)
855 {
856 CloudData::Strategy strategy = CloudData::Strategy::STRATEGY_NETWORK;
857 std::vector<CommonType::Value> values;
858 values.push_back(CloudData::NetWorkStrategy::WIFI);
859
860 auto status = cloudServiceImpl_->SetGlobalCloudStrategy(strategy, values);
861 EXPECT_EQ(status, CloudData::CloudService::ERROR);
862 }
863
864 /**
865 * @tc.name: CheckNotifyConditions001
866 * @tc.desc: Test the CheckNotifyConditions with invalid parameters
867 * @tc.type: FUNC
868 * @tc.require:
869 */
870 HWTEST_F(CloudServiceImplTest, CheckNotifyConditions, TestSize.Level0)
871 {
872 CloudInfo cloudInfo;
873 cloudInfo.enableCloud = false;
874 cloudInfo.id = TEST_CLOUD_APPID;
875
876 auto status = cloudServiceImpl_->CheckNotifyConditions(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, cloudInfo);
877 EXPECT_EQ(status, CloudData::CloudService::CLOUD_DISABLE);
878 cloudInfo.enableCloud = true;
879 CloudInfo::AppInfo appInfo;
880 appInfo.bundleName = TEST_CLOUD_BUNDLE;
881 appInfo.cloudSwitch = false;
882 cloudInfo.apps.insert_or_assign(TEST_CLOUD_BUNDLE, appInfo);
883 status = cloudServiceImpl_->CheckNotifyConditions(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, cloudInfo);
884 EXPECT_EQ(status, CloudData::CloudService::CLOUD_DISABLE_SWITCH);
885 }
886
887 /**
888 * @tc.name: GetDfxFaultType
889 * @tc.desc: test GetDfxFaultType function
890 * @tc.type: FUNC
891 * @tc.require:
892 * @tc.author:
893 */
894 HWTEST_F(CloudServiceImplTest, GetDfxFaultType, TestSize.Level0)
895 {
896 ASSERT_NE(cloudServiceImpl_, nullptr);
897 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::ENABLE_CLOUD), "ENABLE_CLOUD");
898 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::DISABLE_CLOUD), "DISABLE_CLOUD");
899 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::SWITCH_ON), "SWITCH_ON");
900 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::SWITCH_OFF), "SWITCH_OFF");
901 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::QUERY_SYNC_INFO), "QUERY_SYNC_INFO");
902 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::USER_CHANGE), "USER_CHANGE");
903 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::USER_UNLOCK), "USER_UNLOCK");
904 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::NETWORK_RECOVERY), "NETWORK_RECOVERY");
905 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::SERVICE_INIT), "SERVICE_INIT");
906 EXPECT_EQ(cloudServiceImpl_->GetDfxFaultType(CloudSyncScene::ACCOUNT_STOP), "SYNC_TASK");
907 }
908
909 class ComponentConfigTest : public testing::Test {
910 public:
911 static void SetUpTestCase(void);
912 static void TearDownTestCase(void);
913 void SetUp();
914 void TearDown();
915 };
916
SetUpTestCase(void)917 void ComponentConfigTest::SetUpTestCase(void) { }
918
TearDownTestCase()919 void ComponentConfigTest::TearDownTestCase() { }
920
SetUp()921 void ComponentConfigTest::SetUp() { }
922
TearDown()923 void ComponentConfigTest::TearDown() { }
924
925 /**
926 * @tc.name: CapabilityRange
927 * @tc.desc: test CapabilityRange Marshal function
928 * @tc.type: FUNC
929 * @tc.require:
930 * @tc.author: suoqilong
931 */
932 HWTEST_F(ComponentConfigTest, ComponentConfig, TestSize.Level0)
933 {
934 DistributedData::ComponentConfig config;
935 config.description = "description";
936 config.lib = "lib";
937 config.constructor = "constructor";
938 config.destructor = "destructor";
939 config.params = "";
940 Serializable::json node;
941 EXPECT_EQ(config.params.empty(), true);
942
943 EXPECT_EQ(config.Marshal(node), true);
944 EXPECT_EQ(node["description"], config.description);
945 EXPECT_EQ(node["lib"], config.lib);
946 EXPECT_EQ(node["constructor"], config.constructor);
947 EXPECT_EQ(node["destructor"], config.destructor);
948
949 DistributedData::ComponentConfig componentConfig;
950 componentConfig.description = "description";
951 componentConfig.lib = "lib";
952 componentConfig.constructor = "constructor";
953 componentConfig.destructor = "destructor";
954 componentConfig.params = "params";
955 Serializable::json node1;
956 EXPECT_EQ(componentConfig.params.empty(), false);
957
958 EXPECT_EQ(componentConfig.Marshal(node1), true);
959 EXPECT_EQ(node["description"], componentConfig.description);
960 EXPECT_EQ(node["lib"], componentConfig.lib);
961 EXPECT_EQ(node["constructor"], componentConfig.constructor);
962 EXPECT_EQ(node["destructor"], componentConfig.destructor);
963 }
964 } // namespace DistributedDataTest
965 } // namespace OHOS::Test