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
16 #include <gtest/gtest.h>
17 #include <unistd.h>
18
19 #include "accesstoken_kit.h"
20 #include "cloud_manager.h"
21 #include "cloud_types.h"
22 #include "cloud_types_util.h"
23 #include "logger.h"
24 #include "token_setproc.h"
25
26 namespace OHOS::CloudData {
27 using namespace testing::ext;
28 using namespace OHOS::Security::AccessToken;
29 using namespace OHOS::Rdb;
30 uint64_t g_selfTokenID = 0;
31 static constexpr const char *TEST_BUNDLE_NAME = "bundleName";
32 static constexpr const char *TEST_ACCOUNT_ID = "testId";
33 static constexpr const char *TEST_STORE_ID = "storeId";
AllocSystemHapToken(const HapPolicyParams & policy)34 void AllocSystemHapToken(const HapPolicyParams &policy)
35 {
36 HapInfoParams info = {
37 .userID = 100,
38 .bundleName = "ohos.clouddatatest.demo",
39 .instIndex = 0,
40 .appIDDesc = "ohos.clouddatatest.demo",
41 .isSystemApp = true
42 };
43 auto token = AccessTokenKit::AllocHapToken(info, policy);
44 SetSelfTokenID(token.tokenIDEx);
45 }
46
AllocNormalHapToken(const HapPolicyParams & policy)47 void AllocNormalHapToken(const HapPolicyParams &policy)
48 {
49 HapInfoParams info = {
50 .userID = 100,
51 .bundleName = "ohos.clouddatatest.demo",
52 .instIndex = 0,
53 .appIDDesc = "ohos.clouddatatest.demo",
54 .isSystemApp = false
55 };
56 auto token = AccessTokenKit::AllocHapToken(info, policy);
57 SetSelfTokenID(token.tokenIDEx);
58 }
59
60 HapPolicyParams g_normalPolicy = {
61 .apl = APL_NORMAL,
62 .domain = "test.domain",
63 .permList = {
64 {
65 .permissionName = "ohos.permission.CLOUDDATA_CONFIG",
66 .bundleName = "ohos.clouddatatest.demo",
67 .grantMode = 1,
68 .availableLevel = APL_NORMAL,
69 .label = "label",
70 .labelId = 1,
71 .description = "ohos.clouddatatest.demo",
72 .descriptionId = 1
73 }
74 },
75 .permStateList = {
76 {
77 .permissionName = "ohos.permission.CLOUDDATA_CONFIG",
78 .isGeneral = true,
79 .resDeviceID = { "local" },
80 .grantStatus = { PermissionState::PERMISSION_GRANTED },
81 .grantFlags = { 1 }
82 }
83 }
84 };
85
86 HapPolicyParams g_systemPolicy = {
87 .apl = APL_SYSTEM_BASIC,
88 .domain = "test.domain",
89 .permList = {
90 {
91 .permissionName = "ohos.permission.CLOUDDATA_CONFIG",
92 .bundleName = "ohos.clouddatatest.demo",
93 .grantMode = 1,
94 .availableLevel = APL_SYSTEM_BASIC,
95 .label = "label",
96 .labelId = 1,
97 .description = "ohos.clouddatatest.demo",
98 .descriptionId = 1
99 }
100 },
101 .permStateList = {
102 {
103 .permissionName = "ohos.permission.CLOUDDATA_CONFIG",
104 .isGeneral = true,
105 .resDeviceID = { "local" },
106 .grantStatus = { PermissionState::PERMISSION_GRANTED },
107 .grantFlags = { 1 }
108 }
109 }
110 };
111
112 HapPolicyParams g_notPermissonPolicy = {
113 .apl = APL_SYSTEM_BASIC,
114 .domain = "test.domain",
115 .permList = {
116 {
117 .permissionName = "ohos.permission.TEST",
118 .bundleName = "ohos.clouddatatest.demo",
119 .grantMode = 1,
120 .availableLevel = APL_SYSTEM_BASIC,
121 .label = "label",
122 .labelId = 1,
123 .description = "ohos.clouddatatest.demo",
124 .descriptionId = 1
125 }
126 },
127 .permStateList = {
128 {
129 .permissionName = "ohos.permission.TEST",
130 .isGeneral = true,
131 .resDeviceID = { "local" },
132 .grantStatus = { PermissionState::PERMISSION_GRANTED },
133 .grantFlags = { 1 }
134 }
135 }
136 };
137
138 class CloudDataTest : public testing::Test {
139 public:
140 static void SetUpTestCase(void);
141 static void TearDownTestCase(void);
SetUp()142 void SetUp()
143 {
144 }
TearDown()145 void TearDown()
146 {
147 SetSelfTokenID(g_selfTokenID);
148 }
149 };
150
SetUpTestCase(void)151 void CloudDataTest::SetUpTestCase(void)
152 {
153 g_selfTokenID = GetSelfTokenID();
154 }
155
TearDownTestCase(void)156 void CloudDataTest::TearDownTestCase(void)
157 {
158 SetSelfTokenID(g_selfTokenID);
159 }
160
161 /* *
162 * @tc.name: CloudDataTest_001
163 * @tc.desc: Test the system application permissions of the QueryStatistics API
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 HWTEST_F(CloudDataTest, CloudDataTest_001, TestSize.Level0)
168 {
169 AllocNormalHapToken(g_normalPolicy);
170 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
171 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
172 auto [status, info] = proxy->QueryStatistics(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, TEST_STORE_ID);
173 EXPECT_EQ(status, CloudService::PERMISSION_DENIED);
174 EXPECT_TRUE(info.empty());
175 }
176
177 /* *
178 * @tc.name: CloudDataTest_002
179 * @tc.desc: Test the permissions of the QueryStatistics API
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(CloudDataTest, CloudDataTest_002, TestSize.Level0)
184 {
185 AllocSystemHapToken(g_notPermissonPolicy);
186 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
187 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
188 auto [status, info] = proxy->QueryStatistics(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, TEST_STORE_ID);
189 EXPECT_EQ(status, CloudService::CLOUD_CONFIG_PERMISSION_DENIED);
190 EXPECT_TRUE(info.empty());
191 }
192
193 /* *
194 * @tc.name: CloudDataTest_003
195 * @tc.desc: Test the permissions of the QueryStatistics API
196 * @tc.type: FUNC
197 * @tc.require:
198 */
199 HWTEST_F(CloudDataTest, CloudDataTest_003, TestSize.Level0)
200 {
201 AllocSystemHapToken(g_systemPolicy);
202 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
203 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
204 auto [status, info] = proxy->QueryStatistics(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, TEST_STORE_ID);
205 EXPECT_EQ(status, CloudService::ERROR);
206 EXPECT_TRUE(info.empty());
207 }
208
209 /* *
210 * @tc.name: EnableCloud001
211 * @tc.desc: Test the EnableCloud API
212 * @tc.type: FUNC
213 * @tc.require:
214 */
215 HWTEST_F(CloudDataTest, EnableCloud001, TestSize.Level0)
216 {
217 AllocSystemHapToken(g_systemPolicy);
218 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
219 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
220 std::map<std::string, int32_t> switches;
221 switches.emplace(TEST_BUNDLE_NAME, 0);
222 auto status = proxy->EnableCloud(TEST_ACCOUNT_ID, switches);
223 EXPECT_NE(status, CloudService::SUCCESS);
224 }
225
226 /* *
227 * @tc.name: DisableCloud001
228 * @tc.desc: Test the DisableCloud API
229 * @tc.type: FUNC
230 * @tc.require:
231 */
232 HWTEST_F(CloudDataTest, DisableCloud001, TestSize.Level0)
233 {
234 AllocSystemHapToken(g_systemPolicy);
235 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
236 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
237 auto status = proxy->DisableCloud(TEST_ACCOUNT_ID);
238 EXPECT_NE(status, CloudService::SUCCESS);
239 }
240
241 /* *
242 * @tc.name: ChangeAppSwitch001
243 * @tc.desc: Test the ChangeAppSwitch API
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(CloudDataTest, ChangeAppSwitch001, TestSize.Level0)
248 {
249 AllocSystemHapToken(g_systemPolicy);
250 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
251 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
252 auto status = proxy->ChangeAppSwitch(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, 0);
253 EXPECT_NE(status, CloudService::SUCCESS);
254 }
255
256 /* *
257 * @tc.name: Clean001
258 * @tc.desc: Test the Clean API
259 * @tc.type: FUNC
260 * @tc.require:
261 */
262 HWTEST_F(CloudDataTest, Clean001, TestSize.Level0)
263 {
264 AllocSystemHapToken(g_systemPolicy);
265 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
266 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
267 std::map<std::string, int32_t> actions;
268 actions.emplace(TEST_BUNDLE_NAME, 0);
269 auto status = proxy->Clean(TEST_ACCOUNT_ID, actions);
270 EXPECT_EQ(status, CloudService::ERROR);
271 }
272
273 /* *
274 * @tc.name: NotifyDataChange001
275 * @tc.desc: Test the NotifyDataChange API
276 * @tc.type: FUNC
277 * @tc.require:
278 */
279 HWTEST_F(CloudDataTest, NotifyDataChange001, TestSize.Level0)
280 {
281 AllocSystemHapToken(g_systemPolicy);
282 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
283 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
284 auto status = proxy->NotifyDataChange("id", "data", 100);
285 EXPECT_EQ(status, CloudService::INVALID_ARGUMENT);
286 }
287
288 /* *
289 * @tc.name: NotifyDataChange002
290 * @tc.desc: Test the NotifyDataChange API
291 * @tc.type: FUNC
292 * @tc.require:
293 */
294 HWTEST_F(CloudDataTest, NotifyDataChange002, TestSize.Level0)
295 {
296 AllocSystemHapToken(g_systemPolicy);
297 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
298 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
299 auto status = proxy->NotifyDataChange(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME);
300 EXPECT_EQ(status, CloudService::INVALID_ARGUMENT);
301 }
302
303 /* *
304 * @tc.name: SetGlobalCloudStrategy001
305 * @tc.desc: Test the SetGlobalCloudStrategy API
306 * @tc.type: FUNC
307 * @tc.require:
308 */
309 HWTEST_F(CloudDataTest, SetGlobalCloudStrategy001, TestSize.Level0)
310 {
311 AllocSystemHapToken(g_systemPolicy);
312 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
313 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
314 std::vector<CommonType::Value> values = { 0 };
315 auto status = proxy->SetGlobalCloudStrategy(Strategy::STRATEGY_NETWORK, values);
316 EXPECT_EQ(status, CloudService::SUCCESS);
317 }
318
319 /* *
320 * @tc.name: QueryLastSyncInfo001
321 * @tc.desc: Test the system application permissions of the QueryLastSyncInfo API
322 * @tc.type: FUNC
323 * @tc.require:
324 */
325 HWTEST_F(CloudDataTest, QueryLastSyncInfo001, TestSize.Level0)
326 {
327 AllocNormalHapToken(g_normalPolicy);
328 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
329 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
330 auto [status, info] = proxy->QueryLastSyncInfo(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, TEST_STORE_ID);
331 EXPECT_EQ(status, CloudService::PERMISSION_DENIED);
332 EXPECT_TRUE(info.empty());
333 }
334
335 /* *
336 * @tc.name: QueryLastSyncInfo002
337 * @tc.desc: Test the permission name of the QueryLastSyncInfo API
338 * @tc.type: FUNC
339 * @tc.require:
340 */
341 HWTEST_F(CloudDataTest, QueryLastSyncInfo002, TestSize.Level0)
342 {
343 AllocSystemHapToken(g_notPermissonPolicy);
344 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
345 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
346 auto [status, info] = proxy->QueryLastSyncInfo(TEST_ACCOUNT_ID, TEST_BUNDLE_NAME, TEST_STORE_ID);
347 EXPECT_EQ(status, CloudService::CLOUD_CONFIG_PERMISSION_DENIED);
348 EXPECT_TRUE(info.empty());
349 }
350
351 /* *
352 * @tc.name: AllocResourceAndShare001
353 * @tc.desc: Test the system application permissions of the AllocResourceAndShare001 API
354 * @tc.type: FUNC
355 * @tc.require:
356 */
357 HWTEST_F(CloudDataTest, AllocResourceAndShare001, TestSize.Level0)
358 {
359 AllocNormalHapToken(g_normalPolicy);
360 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
361 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
362 DistributedRdb::PredicatesMemo predicates;
363 predicates.tables_.push_back(TEST_BUNDLE_NAME);
364 std::vector<std::string> columns;
365 CloudData::Participants participants;
366 auto [ret, _] = proxy->AllocResourceAndShare(TEST_STORE_ID, predicates, columns, participants);
367 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
368 }
369
370 /* *
371 * @tc.name: Share001
372 * @tc.desc: Test the system application permissions of the Share001 API
373 * @tc.type: FUNC
374 * @tc.require:
375 */
376 HWTEST_F(CloudDataTest, Share001, TestSize.Level0)
377 {
378 AllocNormalHapToken(g_normalPolicy);
379 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
380 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
381 std::string sharingRes = "";
382 CloudData::Participants participants{};
383 CloudData::Results results;
384 auto ret = proxy->Share(sharingRes, participants, results);
385 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
386 }
387
388 /* *
389 * @tc.name: Unshare001
390 * @tc.desc: Test the system application permissions of the Unshare001 API
391 * @tc.type: FUNC
392 * @tc.require:
393 */
394 HWTEST_F(CloudDataTest, Unshare001, TestSize.Level0)
395 {
396 AllocNormalHapToken(g_normalPolicy);
397 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
398 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
399 std::string sharingRes = "";
400 CloudData::Participants participants{};
401 CloudData::Results results;
402 auto ret = proxy->Unshare(sharingRes, participants, results);
403 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
404 }
405
406 /* *
407 * @tc.name: Exit001
408 * @tc.desc: Test the system application permissions of the Exit001 API
409 * @tc.type: FUNC
410 * @tc.require:
411 */
412 HWTEST_F(CloudDataTest, Exit001, TestSize.Level0)
413 {
414 AllocNormalHapToken(g_normalPolicy);
415 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
416 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
417 std::string sharingRes = "";
418 std::pair<int32_t, std::string> result;
419 auto ret = proxy->Exit(sharingRes, result);
420 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
421 }
422
423 /* *
424 * @tc.name: ChangePrivilege001
425 * @tc.desc: Test the system application permissions of the ChangePrivilege001 API
426 * @tc.type: FUNC
427 * @tc.require:
428 */
429 HWTEST_F(CloudDataTest, ChangePrivilege001, TestSize.Level0)
430 {
431 AllocNormalHapToken(g_normalPolicy);
432 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
433 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
434 std::string sharingRes = "";
435 CloudData::Participants participants{};
436 CloudData::Results results;
437 auto ret = proxy->ChangePrivilege(sharingRes, participants, results);
438 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
439 }
440
441 /* *
442 * @tc.name: Query001
443 * @tc.desc: Test the system application permissions of the Query001 API
444 * @tc.type: FUNC
445 * @tc.require:
446 */
447 HWTEST_F(CloudDataTest, Query001, TestSize.Level0)
448 {
449 AllocNormalHapToken(g_normalPolicy);
450 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
451 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
452 std::string sharingRes = "";
453 CloudData::QueryResults result;
454 auto ret = proxy->Query(sharingRes, result);
455 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
456 }
457
458 /* *
459 * @tc.name:QueryByInvitation001
460 * @tc.desc: Test the system application permissions of the QueryByInvitation001 API
461 * @tc.type: FUNC
462 * @tc.require:
463 */
464 HWTEST_F(CloudDataTest, QueryByInvitation001, TestSize.Level0)
465 {
466 AllocNormalHapToken(g_normalPolicy);
467 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
468 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
469 std::string invitation = "";
470 CloudData::QueryResults result;
471 auto ret = proxy->QueryByInvitation(invitation, result);
472 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
473 }
474
475 /* *
476 * @tc.name:ConfirmInvitation001
477 * @tc.desc: Test the system application permissions of the ConfirmInvitation001 API
478 * @tc.type: FUNC
479 * @tc.require:
480 */
481 HWTEST_F(CloudDataTest, ConfirmInvitation001, TestSize.Level0)
482 {
483 AllocNormalHapToken(g_normalPolicy);
484 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
485 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
486 std::string sharingRes = "";
487 int32_t confirmation = 0;
488 std::tuple<int32_t, std::string, std::string> result;
489 auto ret = proxy->ConfirmInvitation(sharingRes, confirmation, result);
490 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
491 }
492
493 /* *
494 * @tc.name:ChangeConfirmation001
495 * @tc.desc: Test the system application permissions of the ChangeConfirmation001 API
496 * @tc.type: FUNC
497 * @tc.require:
498 */
499 HWTEST_F(CloudDataTest, ChangeConfirmation001, TestSize.Level0)
500 {
501 AllocNormalHapToken(g_normalPolicy);
502 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
503 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
504 std::string sharingRes = "";
505 int32_t confirmation = 0;
506 std::pair<int32_t, std::string> result;
507 auto ret = proxy->ChangeConfirmation(sharingRes, confirmation, result);
508 EXPECT_EQ(ret, CloudService::PERMISSION_DENIED);
509 }
510
511 /* *
512 * @tc.name:SetCloudStrategy001
513 * @tc.desc: Test the system application permissions of the SetCloudStrategy001 API
514 * @tc.type: FUNC
515 * @tc.require:
516 */
517 HWTEST_F(CloudDataTest, SetCloudStrategy001, TestSize.Level0)
518 {
519 AllocNormalHapToken(g_normalPolicy);
520 auto [state, proxy] = CloudManager::GetInstance().GetCloudService();
521 ASSERT_EQ(state == CloudService::SUCCESS && proxy != nullptr, true);
522 std::vector<CommonType::Value> values;
523 values.push_back(CloudData::NetWorkStrategy::WIFI);
524 CloudData::Strategy strategy = CloudData::Strategy::STRATEGY_BUTT;
525 auto ret = proxy->SetCloudStrategy(strategy, values);
526 EXPECT_EQ(ret, CloudService::IPC_ERROR);
527 strategy = CloudData::Strategy::STRATEGY_NETWORK;
528 ret = proxy->SetCloudStrategy(strategy, values);
529 EXPECT_EQ(ret, CloudService::SUCCESS);
530 }
531 } // namespace OHOS::CloudData