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 "rdb_query.h"
45 #include "rdb_service.h"
46 #include "rdb_service_impl.h"
47 #include "rdb_types.h"
48 #include "store/auto_cache.h"
49 #include "store/store_info.h"
50 #include "token_setproc.h"
51
52 using namespace testing::ext;
53 using namespace OHOS::DistributedData;
54 using namespace OHOS::Security::AccessToken;
55 using Confirmation = OHOS::CloudData::Confirmation;
56 using Status = OHOS::CloudData::CloudService::Status;
57
58 namespace OHOS::Test {
59 namespace DistributedDataTest {
60 static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName";
61 static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid";
62 static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store";
63 static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1";
64 class CloudServiceImplTest : public testing::Test {
65 public:
66 static void SetUpTestCase(void);
67 static void TearDownTestCase(void);
68 void SetUp();
69 void TearDown();
70
71 static std::shared_ptr<CloudData::CloudServiceImpl> cloudServiceImpl_;
72 };
73 std::shared_ptr<CloudData::CloudServiceImpl> CloudServiceImplTest::cloudServiceImpl_ =
74 std::make_shared<CloudData::CloudServiceImpl>();
75
SetUpTestCase(void)76 void CloudServiceImplTest::SetUpTestCase(void)
77 {
78 size_t max = 12;
79 size_t min = 5;
80 auto executor = std::make_shared<ExecutorPool>(max, min);
81 DeviceManagerAdapter::GetInstance().Init(executor);
82 }
83
TearDownTestCase()84 void CloudServiceImplTest::TearDownTestCase() {}
85
SetUp()86 void CloudServiceImplTest::SetUp() {}
87
TearDown()88 void CloudServiceImplTest::TearDown() {}
89
90 /**
91 * @tc.name: EnableCloud001
92 * @tc.desc: Test EnableCloud functions with user is invalid.
93 * @tc.type: FUNC
94 * @tc.require:
95 */
HWTEST_F(CloudServiceImplTest, EnableCloud001, TestSize.Level0)96 HWTEST_F(CloudServiceImplTest, EnableCloud001, TestSize.Level0)
97 {
98 ZLOGI("CloudServiceImplTest EnableCloud001 start");
99 std::map<std::string, int32_t> switches;
100 switches.insert_or_assign(TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
101 auto status = cloudServiceImpl_->EnableCloud(TEST_CLOUD_APPID, switches);
102 EXPECT_EQ(status, CloudData::CloudService::ERROR);
103 }
104
105 /**
106 * @tc.name: DisableCloud001
107 * @tc.desc: Test DisableCloud functions with user is invalid.
108 * @tc.type: FUNC
109 * @tc.require:
110 */
HWTEST_F(CloudServiceImplTest, DisableCloud001, TestSize.Level0)111 HWTEST_F(CloudServiceImplTest, DisableCloud001, TestSize.Level0)
112 {
113 ZLOGI("CloudServiceImplTest DisableCloud001 start");
114 auto status = cloudServiceImpl_->DisableCloud(TEST_CLOUD_APPID);
115 EXPECT_EQ(status, CloudData::CloudService::ERROR);
116 }
117
118 /**
119 * @tc.name: ChangeAppSwitch001
120 * @tc.desc: Test ChangeAppSwitch functions with user is invalid.
121 * @tc.type: FUNC
122 * @tc.require:
123 */
HWTEST_F(CloudServiceImplTest, ChangeAppSwitch001, TestSize.Level0)124 HWTEST_F(CloudServiceImplTest, ChangeAppSwitch001, TestSize.Level0)
125 {
126 ZLOGI("CloudServiceImplTest ChangeAppSwitch001 start");
127 auto status =
128 cloudServiceImpl_->ChangeAppSwitch(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
129 EXPECT_EQ(status, CloudData::CloudService::ERROR);
130 }
131
132 /**
133 * @tc.name: Clean001
134 * @tc.desc: Test Clean functions with user is invalid.
135 * @tc.type: FUNC
136 * @tc.require:
137 */
HWTEST_F(CloudServiceImplTest, Clean001, TestSize.Level0)138 HWTEST_F(CloudServiceImplTest, Clean001, TestSize.Level0)
139 {
140 ZLOGI("CloudServiceImplTest Clean001 start");
141 std::map<std::string, int32_t> actions;
142 actions.insert_or_assign(TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_ON);
143 auto status = cloudServiceImpl_->Clean(TEST_CLOUD_APPID, actions);
144 EXPECT_EQ(status, CloudData::CloudService::ERROR);
145 }
146
147 /**
148 * @tc.name: NotifyDataChange001
149 * @tc.desc: Test the EnableCloud function in case it doesn't get cloudInfo.
150 * @tc.type: FUNC
151 * @tc.require:
152 */
HWTEST_F(CloudServiceImplTest, NotifyDataChange001, TestSize.Level0)153 HWTEST_F(CloudServiceImplTest, NotifyDataChange001, TestSize.Level0)
154 {
155 ZLOGI("CloudServiceImplTest NotifyDataChange001 start");
156 auto status = cloudServiceImpl_->NotifyDataChange(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE);
157 EXPECT_EQ(status, CloudData::CloudService::INVALID_ARGUMENT);
158 }
159
160 /**
161 * @tc.name: ExecuteStatistics001
162 * @tc.desc: Test the ExecuteStatistics function if the package name does not support CloudSync.
163 * @tc.type: FUNC
164 * @tc.require:
165 */
HWTEST_F(CloudServiceImplTest, ExecuteStatistics001, TestSize.Level0)166 HWTEST_F(CloudServiceImplTest, ExecuteStatistics001, TestSize.Level0)
167 {
168 ZLOGI("CloudServiceImplTest ExecuteStatistics001 start");
169 CloudInfo cloudInfo;
170 CloudInfo::AppInfo appInfo;
171 appInfo.bundleName = TEST_CLOUD_BUNDLE;
172 cloudInfo.apps[TEST_CLOUD_BUNDLE] = std::move(appInfo);
173 SchemaMeta::Database database;
174 database.name = TEST_CLOUD_STORE;
175 database.alias = TEST_CLOUD_DATABASE_ALIAS_1;
176 SchemaMeta schemaMeta;
177 schemaMeta.bundleName = "testBundle";
178 schemaMeta.databases.emplace_back(database);
179 auto result = cloudServiceImpl_->ExecuteStatistics("", cloudInfo, schemaMeta);
180 EXPECT_TRUE(result.empty());
181 }
182
183 /**
184 * @tc.name: QueryStatistics001
185 * @tc.desc: When metadata is not supported store test the QueryStatistics function.
186 * @tc.type: FUNC
187 * @tc.require:
188 */
HWTEST_F(CloudServiceImplTest, QueryStatistics001, TestSize.Level0)189 HWTEST_F(CloudServiceImplTest, QueryStatistics001, TestSize.Level0)
190 {
191 ZLOGI("CloudServiceImplTest QueryStatistics start");
192 StoreMetaData metaData;
193 metaData.storeType = -1;
194 DistributedData::Database database;
195 auto result = cloudServiceImpl_->QueryStatistics(metaData, database);
196 EXPECT_TRUE(result.empty());
197 }
198
199 /**
200 * @tc.name: QueryLastSyncInfo001
201 * @tc.desc: Test QueryLastSyncInfo functions with invalid parameter.
202 * @tc.type: FUNC
203 * @tc.require:
204 */
HWTEST_F(CloudServiceImplTest, QueryLastSyncInfo001, TestSize.Level0)205 HWTEST_F(CloudServiceImplTest, QueryLastSyncInfo001, TestSize.Level0)
206 {
207 ZLOGI("CloudServiceImplTest QueryLastSyncInfo start");
208 auto [status, result] = cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE);
209 EXPECT_EQ(status, CloudData::CloudService::ERROR);
210 EXPECT_TRUE(result.empty());
211 }
212
213 /**
214 * @tc.name: OnBind001
215 * @tc.desc: Test OnBind functions with invalid parameter.
216 * @tc.type: FUNC
217 * @tc.require:
218 */
HWTEST_F(CloudServiceImplTest, OnBind001, TestSize.Level0)219 HWTEST_F(CloudServiceImplTest, OnBind001, TestSize.Level0)
220 {
221 ZLOGI("CloudServiceImplTest OnBind001 start");
222 auto status = cloudServiceImpl_->OnBind({});
223 EXPECT_EQ(status, GeneralError::E_INVALID_ARGS);
224 }
225
226 /**
227 * @tc.name: UpdateSchema001
228 * @tc.desc: Test UpdateSchema001 functions with invalid parameter.
229 * @tc.type: FUNC
230 * @tc.require:
231 */
HWTEST_F(CloudServiceImplTest, UpdateSchema001, TestSize.Level0)232 HWTEST_F(CloudServiceImplTest, UpdateSchema001, TestSize.Level0)
233 {
234 ZLOGI("CloudServiceImplTest UpdateSchema001 start");
235 int user = -1;
236 auto status = cloudServiceImpl_->UpdateSchema(user);
237 EXPECT_FALSE(status);
238 }
239
240 /**
241 * @tc.name: GetAppSchemaFromServer001
242 * @tc.desc: Test GetAppSchemaFromServer functions not support CloudService.
243 * @tc.type: FUNC
244 * @tc.require:
245 */
HWTEST_F(CloudServiceImplTest, GetAppSchemaFromServer001, TestSize.Level0)246 HWTEST_F(CloudServiceImplTest, GetAppSchemaFromServer001, TestSize.Level0)
247 {
248 ZLOGI("CloudServiceImplTest GetAppSchemaFromServer001 start");
249 DeviceManagerAdapter::GetInstance().SetNet(DeviceManagerAdapter::WIFI);
250 int user = -1;
251 auto [status, result] = cloudServiceImpl_->GetAppSchemaFromServer(user, TEST_CLOUD_BUNDLE);
252 EXPECT_EQ(status, CloudData::CloudService::SERVER_UNAVAILABLE);
253 }
254
255 /**
256 * @tc.name: GetCloudInfoFromServer001
257 * @tc.desc: Test GetCloudInfoFromServer functions not support CloudService.
258 * @tc.type: FUNC
259 * @tc.require:
260 */
HWTEST_F(CloudServiceImplTest, GetCloudInfoFromServer001, TestSize.Level0)261 HWTEST_F(CloudServiceImplTest, GetCloudInfoFromServer001, TestSize.Level0)
262 {
263 ZLOGI("CloudServiceImplTest GetCloudInfoFromServer001 start");
264 DeviceManagerAdapter::GetInstance().SetNet(DeviceManagerAdapter::WIFI);
265 int user = -1;
266 auto [status, result] = cloudServiceImpl_->GetCloudInfoFromServer(user);
267 EXPECT_EQ(status, CloudData::CloudService::SERVER_UNAVAILABLE);
268 }
269
270 /**
271 * @tc.name: ReleaseUserInfo001
272 * @tc.desc: Test ReleaseUserInfo functions with invalid parameter.
273 * @tc.type: FUNC
274 * @tc.require:
275 */
HWTEST_F(CloudServiceImplTest, ReleaseUserInfo001, TestSize.Level0)276 HWTEST_F(CloudServiceImplTest, ReleaseUserInfo001, TestSize.Level0)
277 {
278 ZLOGI("CloudServiceImplTest ReleaseUserInfo001 start");
279 int user = 100;
280 auto status = cloudServiceImpl_->ReleaseUserInfo(user);
281 EXPECT_TRUE(status);
282 }
283
284 /**
285 * @tc.name: DoSubscribe
286 * @tc.desc: Test the DoSubscribe with not support CloudService
287 * @tc.type: FUNC
288 * @tc.require:
289 */
HWTEST_F(CloudServiceImplTest, DoSubscribe, TestSize.Level0)290 HWTEST_F(CloudServiceImplTest, DoSubscribe, TestSize.Level0)
291 {
292 int32_t user = 100;
293 auto status = cloudServiceImpl_->DoSubscribe(user);
294 EXPECT_TRUE(status);
295 }
296
297 /**
298 * @tc.name: Share001
299 * @tc.desc: Test the Share with invalid parameters
300 * @tc.type: FUNC
301 * @tc.require:
302 */
HWTEST_F(CloudServiceImplTest, Share001, TestSize.Level0)303 HWTEST_F(CloudServiceImplTest, Share001, TestSize.Level0)
304 {
305 std::string sharingRes;
306 CloudData::Participants participants;
307 CloudData::Results results;
308 auto status = cloudServiceImpl_->Share(sharingRes, participants, results);
309 EXPECT_EQ(status, GeneralError::E_ERROR);
310 }
311
312 /**
313 * @tc.name: Unshare001
314 * @tc.desc: Test the Unshare with invalid parameters
315 * @tc.type: FUNC
316 * @tc.require:
317 */
HWTEST_F(CloudServiceImplTest, Unshare001, TestSize.Level0)318 HWTEST_F(CloudServiceImplTest, Unshare001, TestSize.Level0)
319 {
320 std::string sharingRes;
321 CloudData::Participants participants;
322 CloudData::Results results;
323 auto status = cloudServiceImpl_->Unshare(sharingRes, participants, results);
324 EXPECT_EQ(status, GeneralError::E_ERROR);
325 }
326
327 /**
328 * @tc.name: Exit001
329 * @tc.desc: Test the Exit with invalid parameters
330 * @tc.type: FUNC
331 * @tc.require:
332 */
HWTEST_F(CloudServiceImplTest, Exit001, TestSize.Level0)333 HWTEST_F(CloudServiceImplTest, Exit001, TestSize.Level0)
334 {
335 std::string sharingRes;
336 std::pair<int32_t, std::string> result;
337 auto status = cloudServiceImpl_->Exit(sharingRes, result);
338 EXPECT_EQ(status, GeneralError::E_ERROR);
339 }
340
341 /**
342 * @tc.name: ChangePrivilege001
343 * @tc.desc: Test the ChangePrivilege with invalid parameters
344 * @tc.type: FUNC
345 * @tc.require:
346 */
HWTEST_F(CloudServiceImplTest, ChangePrivilege001, TestSize.Level0)347 HWTEST_F(CloudServiceImplTest, ChangePrivilege001, TestSize.Level0)
348 {
349 std::string sharingRes;
350 CloudData::Participants participants;
351 CloudData::Results results;
352 auto status = cloudServiceImpl_->ChangePrivilege(sharingRes, participants, results);
353 EXPECT_EQ(status, GeneralError::E_ERROR);
354 }
355
356 /**
357 * @tc.name: Query001
358 * @tc.desc: Test the Query with invalid parameters
359 * @tc.type: FUNC
360 * @tc.require:
361 */
HWTEST_F(CloudServiceImplTest, Query001, TestSize.Level0)362 HWTEST_F(CloudServiceImplTest, Query001, TestSize.Level0)
363 {
364 std::string sharingRes;
365 CloudData::QueryResults results;
366 auto status = cloudServiceImpl_->Query(sharingRes, results);
367 EXPECT_EQ(status, GeneralError::E_ERROR);
368 }
369
370 /**
371 * @tc.name: QueryByInvitation001
372 * @tc.desc: Test the QueryByInvitation with invalid parameters
373 * @tc.type: FUNC
374 * @tc.require:
375 */
HWTEST_F(CloudServiceImplTest, QueryByInvitation001, TestSize.Level0)376 HWTEST_F(CloudServiceImplTest, QueryByInvitation001, TestSize.Level0)
377 {
378 std::string invitation;
379 CloudData::QueryResults results;
380 auto status = cloudServiceImpl_->QueryByInvitation(invitation, results);
381 EXPECT_EQ(status, GeneralError::E_ERROR);
382 }
383
384 /**
385 * @tc.name: ConfirmInvitation001
386 * @tc.desc: Test the ConfirmInvitation with invalid parameters
387 * @tc.type: FUNC
388 * @tc.require:
389 */
HWTEST_F(CloudServiceImplTest, ConfirmInvitation001, TestSize.Level0)390 HWTEST_F(CloudServiceImplTest, ConfirmInvitation001, TestSize.Level0)
391 {
392 int32_t confirmation = 0;
393 std::tuple<int32_t, std::string, std::string> result{ 0, "", "" };
394 std::string invitation;
395 auto status = cloudServiceImpl_->ConfirmInvitation(invitation, confirmation, result);
396 EXPECT_EQ(status, GeneralError::E_ERROR);
397 }
398
399 /**
400 * @tc.name: ChangeConfirmation001
401 * @tc.desc: Test the ChangeConfirmation with invalid parameters
402 * @tc.type: FUNC
403 * @tc.require:
404 */
HWTEST_F(CloudServiceImplTest, ChangeConfirmation001, TestSize.Level0)405 HWTEST_F(CloudServiceImplTest, ChangeConfirmation001, TestSize.Level0)
406 {
407 int32_t confirmation = 0;
408 std::string sharingRes;
409 std::pair<int32_t, std::string> result;
410 auto status = cloudServiceImpl_->ChangeConfirmation(sharingRes, confirmation, result);
411 EXPECT_EQ(status, GeneralError::E_ERROR);
412 }
413
414 /**
415 * @tc.name: GetSharingHandle001
416 * @tc.desc: Test the GetSharingHandle with invalid parameters
417 * @tc.type: FUNC
418 * @tc.require:
419 */
HWTEST_F(CloudServiceImplTest, GetSharingHandle001, TestSize.Level0)420 HWTEST_F(CloudServiceImplTest, GetSharingHandle001, TestSize.Level0)
421 {
422 CloudData::CloudServiceImpl::HapInfo hapInfo;
423 auto status = cloudServiceImpl_->GetSharingHandle(hapInfo);
424 EXPECT_EQ(status, nullptr);
425 }
426
427 /**
428 * @tc.name: SetCloudStrategy001
429 * @tc.desc: Test the SetCloudStrategy with get hapInfo failed
430 * @tc.type: FUNC
431 * @tc.require:
432 */
HWTEST_F(CloudServiceImplTest, SetCloudStrategy001, TestSize.Level0)433 HWTEST_F(CloudServiceImplTest, SetCloudStrategy001, TestSize.Level0)
434 {
435 CloudData::Strategy strategy = CloudData::Strategy::STRATEGY_NETWORK;
436 std::vector<CommonType::Value> values;
437 values.push_back(CloudData::NetWorkStrategy::WIFI);
438
439 auto status = cloudServiceImpl_->SetCloudStrategy(strategy, values);
440 EXPECT_EQ(status, CloudData::CloudService::ERROR);
441 }
442
443 /**
444 * @tc.name: SetGlobalCloudStrategy001
445 * @tc.desc: Test the SetGlobalCloudStrategy with get hapInfo failed
446 * @tc.type: FUNC
447 * @tc.require:
448 */
HWTEST_F(CloudServiceImplTest, SetGlobalCloudStrategy001, TestSize.Level0)449 HWTEST_F(CloudServiceImplTest, SetGlobalCloudStrategy001, TestSize.Level0)
450 {
451 CloudData::Strategy strategy = CloudData::Strategy::STRATEGY_NETWORK;
452 std::vector<CommonType::Value> values;
453 values.push_back(CloudData::NetWorkStrategy::WIFI);
454
455 auto status = cloudServiceImpl_->SetGlobalCloudStrategy(strategy, values);
456 EXPECT_EQ(status, CloudData::CloudService::ERROR);
457 }
458
459 /**
460 * @tc.name: CheckNotifyConditions001
461 * @tc.desc: Test the CheckNotifyConditions with invalid parameters
462 * @tc.type: FUNC
463 * @tc.require:
464 */
HWTEST_F(CloudServiceImplTest, CheckNotifyConditions, TestSize.Level0)465 HWTEST_F(CloudServiceImplTest, CheckNotifyConditions, TestSize.Level0)
466 {
467 CloudInfo cloudInfo;
468 cloudInfo.enableCloud = false;
469 cloudInfo.id = TEST_CLOUD_APPID;
470
471 auto status = cloudServiceImpl_->CheckNotifyConditions(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, cloudInfo);
472 EXPECT_EQ(status, CloudData::CloudService::CLOUD_DISABLE);
473 cloudInfo.enableCloud = true;
474 CloudInfo::AppInfo appInfo;
475 appInfo.bundleName = TEST_CLOUD_BUNDLE;
476 appInfo.cloudSwitch = false;
477 cloudInfo.apps.insert_or_assign(TEST_CLOUD_BUNDLE, appInfo);
478 status = cloudServiceImpl_->CheckNotifyConditions(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, cloudInfo);
479 EXPECT_EQ(status, CloudData::CloudService::CLOUD_DISABLE_SWITCH);
480 }
481 } // namespace DistributedDataTest
482 } // namespace OHOS::Test