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 <memory>
17
18 #include "accesstoken_kit.h"
19 #include "gtest/gtest.h"
20 #include "ipc_skeleton.h"
21
22 #include "cooperate_params.h"
23 #include "cooperate_server.h"
24 #include "default_params.h"
25 #include "fi_log.h"
26 #include "nativetoken_kit.h"
27 #include "test_context.h"
28 #include "token_setproc.h"
29
30 #undef LOG_TAG
31 #define LOG_TAG "CooperateServerTest"
32
33 namespace OHOS {
34 namespace Msdp {
35 namespace DeviceStatus {
36 using namespace testing::ext;
37 namespace {
38 uint64_t g_tokenID { 0 };
39 const std::string SYSTEM_BASIC { "system_basic" };
40 const char* g_basics[] = { "ohos.permission.COOPERATE_MANAGER" };
41 } // namespace
42
43 class CooperateServerTest : public testing::Test {
44 public:
45 CooperateServerTest();
46 ~CooperateServerTest() = default;
47
48 void SetUp();
49 void TearDown();
50 static void SetUpTestCase();
51 static void TearDownTestCase();
52 static void SetPermission(const std::string &level, const char** perms, size_t permAmount);
53 static void RemovePermission();
54
55 private:
56 Intention intention_ { Intention::COOPERATE };
57 std::shared_ptr<TestContext> context_ { nullptr };
58 std::shared_ptr<CooperateServer> cooperateServer_ { nullptr };
59 };
60
CooperateServerTest()61 CooperateServerTest::CooperateServerTest()
62 {
63 context_ = std::make_shared<TestContext>();
64 cooperateServer_ = std::make_shared<CooperateServer>(context_.get());
65 }
66
SetUp()67 void CooperateServerTest::SetUp()
68 {}
69
TearDown()70 void CooperateServerTest::TearDown()
71 {}
72
SetUpTestCase()73 void CooperateServerTest::SetUpTestCase()
74 {
75 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
76 }
77
TearDownTestCase()78 void CooperateServerTest::TearDownTestCase()
79 {
80 RemovePermission();
81 }
82
SetPermission(const std::string & level,const char ** perms,size_t permAmount)83 void CooperateServerTest::SetPermission(const std::string &level, const char** perms, size_t permAmount)
84 {
85 CALL_DEBUG_ENTER;
86 if (perms == nullptr || permAmount == 0) {
87 FI_HILOGE("The perms is empty");
88 return;
89 }
90
91 NativeTokenInfoParams infoInstance = {
92 .dcapsNum = 0,
93 .permsNum = permAmount,
94 .aclsNum = 0,
95 .dcaps = nullptr,
96 .perms = perms,
97 .acls = nullptr,
98 .processName = "CooperateServerTest",
99 .aplStr = level.c_str(),
100 };
101 g_tokenID = GetAccessTokenId(&infoInstance);
102 SetSelfTokenID(g_tokenID);
103 OHOS::Security::AccessToken::AccessTokenKit::AccessTokenKit::ReloadNativeTokenInfo();
104 }
105
RemovePermission()106 void CooperateServerTest::RemovePermission()
107 {
108 CALL_DEBUG_ENTER;
109 int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_tokenID);
110 if (ret != RET_OK) {
111 FI_HILOGE("Failed to remove permission");
112 return;
113 }
114 }
115
116 /**
117 * @tc.name: EnableTest1
118 * @tc.desc: Test func named enable
119 * @tc.type: FUNC
120 * @tc.require:
121 */
122 HWTEST_F(CooperateServerTest, EnableTest1, TestSize.Level0)
123 {
124 CALL_TEST_DEBUG;
125 CallingContext context {
126 .intention = intention_,
127 .tokenId = IPCSkeleton::GetCallingTokenID(),
128 .uid = IPCSkeleton::GetCallingUid(),
129 .pid = IPCSkeleton::GetCallingPid(),
130 };
131 MessageParcel datas;
132 MessageParcel reply;
133 int32_t ret = cooperateServer_->Enable(context, datas, reply);
134 EXPECT_EQ(ret, RET_ERR);
135 }
136
137 /**
138 * @tc.name: DisableTest1
139 * @tc.desc: Test func named disable
140 * @tc.type: FUNC
141 * @tc.require:
142 */
143 HWTEST_F(CooperateServerTest, DisableTest1, TestSize.Level0)
144 {
145 CALL_TEST_DEBUG;
146 CallingContext context {
147 .intention = intention_,
148 .tokenId = IPCSkeleton::GetCallingTokenID(),
149 .uid = IPCSkeleton::GetCallingUid(),
150 .pid = IPCSkeleton::GetCallingPid(),
151 };
152 MessageParcel datas;
153 MessageParcel reply;
154 int32_t ret = cooperateServer_->Disable(context, datas, reply);
155 EXPECT_EQ(ret, RET_ERR);
156 }
157
158 /**
159 * @tc.name: StartTest1
160 * @tc.desc: Test func named start
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(CooperateServerTest, StartTest1, TestSize.Level0)
165 {
166 CALL_TEST_DEBUG;
167 CallingContext context {
168 .intention = intention_,
169 .tokenId = IPCSkeleton::GetCallingTokenID(),
170 .uid = IPCSkeleton::GetCallingUid(),
171 .pid = IPCSkeleton::GetCallingPid(),
172 };
173 MessageParcel datas;
174 MessageParcel reply;
175 int32_t ret = cooperateServer_->Start(context, datas, reply);
176 EXPECT_EQ(ret, RET_ERR);
177 }
178
179 /**
180 * @tc.name: StopTest1
181 * @tc.desc: Test func named stop
182 * @tc.type: FUNC
183 * @tc.require:
184 */
185 HWTEST_F(CooperateServerTest, StopTest1, TestSize.Level0)
186 {
187 CALL_TEST_DEBUG;
188 CallingContext context {
189 .intention = intention_,
190 .tokenId = IPCSkeleton::GetCallingTokenID(),
191 .uid = IPCSkeleton::GetCallingUid(),
192 .pid = IPCSkeleton::GetCallingPid(),
193 };
194 MessageParcel datas;
195 MessageParcel reply;
196 int32_t ret = cooperateServer_->Stop(context, datas, reply);
197 EXPECT_EQ(ret, RET_ERR);
198 }
199
200 /**
201 * @tc.name: EnableTest2
202 * @tc.desc: Test func named enable
203 * @tc.type: FUNC
204 * @tc.require:
205 */
206 HWTEST_F(CooperateServerTest, EnableTest2, TestSize.Level0)
207 {
208 CALL_TEST_DEBUG;
209 CallingContext context {
210 .intention = intention_,
211 .tokenId = IPCSkeleton::GetCallingTokenID(),
212 .uid = IPCSkeleton::GetCallingUid(),
213 .pid = IPCSkeleton::GetCallingPid(),
214 };
215 MessageParcel data;
216 MessageParcel reply;
217 DefaultParam param { 1 };
218 ASSERT_TRUE(param.Marshalling(data));
219 auto ret = cooperateServer_->Enable(context, data, reply);
220 EXPECT_EQ(ret, RET_OK);
221 context_->GetPluginManager().UnloadCooperate();
222 }
223
224 /**
225 * @tc.name: AddWatchTest1
226 * @tc.desc: Test func named add watch
227 * @tc.type: FUNC
228 * @tc.require:
229 */
230 HWTEST_F(CooperateServerTest, AddWatchTest1, TestSize.Level0)
231 {
232 CALL_TEST_DEBUG;
233 CallingContext context {
234 .intention = intention_,
235 .tokenId = IPCSkeleton::GetCallingTokenID(),
236 .uid = IPCSkeleton::GetCallingUid(),
237 .pid = IPCSkeleton::GetCallingPid(),
238 };
239 MessageParcel data;
240 MessageParcel reply;
241 DefaultParam param { 1 };
242 ASSERT_TRUE(param.Marshalling(data));
243 auto ret = cooperateServer_->AddWatch(context, CooperateRequestID::REGISTER_LISTENER, data, reply);
244 EXPECT_EQ(ret, RET_OK);
245 context_->GetPluginManager().UnloadCooperate();
246 }
247
248 /**
249 * @tc.name: DisableTest2
250 * @tc.desc: Test func named disable
251 * @tc.type: FUNC
252 * @tc.require:
253 */
254 HWTEST_F(CooperateServerTest, DisableTest2, TestSize.Level0)
255 {
256 CALL_TEST_DEBUG;
257 CallingContext context {
258 .intention = intention_,
259 .tokenId = IPCSkeleton::GetCallingTokenID(),
260 .uid = IPCSkeleton::GetCallingUid(),
261 .pid = IPCSkeleton::GetCallingPid(),
262 };
263 DefaultParam param { 1 };
264 MessageParcel datas;
265 MessageParcel reply;
266 param.Marshalling(datas);
267 ASSERT_TRUE(param.Marshalling(datas));
268 int32_t ret = cooperateServer_->Disable(context, datas, reply);
269 EXPECT_EQ(ret, RET_OK);
270 context_->GetPluginManager().UnloadCooperate();
271 }
272
273 /**
274 * @tc.name: AddWatchTest2
275 * @tc.desc: Test func named add watch
276 * @tc.type: FUNC
277 * @tc.require:
278 */
279 HWTEST_F(CooperateServerTest, AddWatchTest2, TestSize.Level0)
280 {
281 CALL_TEST_DEBUG;
282 CallingContext context {
283 .intention = intention_,
284 .tokenId = IPCSkeleton::GetCallingTokenID(),
285 .uid = IPCSkeleton::GetCallingUid(),
286 .pid = IPCSkeleton::GetCallingPid(),
287 };
288 MessageParcel data;
289 MessageParcel reply;
290 auto ret = cooperateServer_->AddWatch(context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply);
291 EXPECT_EQ(ret, RET_ERR);
292 context_->GetPluginManager().UnloadCooperate();
293 }
294
295 /**
296 * @tc.name: AddWatchTest3
297 * @tc.desc: Test func named add watch
298 * @tc.type: FUNC
299 * @tc.require:
300 */
301 HWTEST_F(CooperateServerTest, AddWatchTest3, TestSize.Level0)
302 {
303 CALL_TEST_DEBUG;
304 CallingContext context {
305 .intention = intention_,
306 .tokenId = IPCSkeleton::GetCallingTokenID(),
307 .uid = IPCSkeleton::GetCallingUid(),
308 .pid = IPCSkeleton::GetCallingPid(),
309 };
310 MessageParcel data;
311 MessageParcel reply;
312 auto ret = cooperateServer_->AddWatch(context, CooperateRequestID::REGISTER_HOTAREA_LISTENER, data, reply);
313 EXPECT_EQ(ret, RET_OK);
314 context_->GetPluginManager().UnloadCooperate();
315 }
316
317 /**
318 * @tc.name: AddWatchTest4
319 * @tc.desc: Test func named add watch
320 * @tc.type: FUNC
321 * @tc.require:
322 */
323 HWTEST_F(CooperateServerTest, AddWatchTest4, TestSize.Level0)
324 {
325 CALL_TEST_DEBUG;
326 CallingContext context {
327 .intention = intention_,
328 .tokenId = IPCSkeleton::GetCallingTokenID(),
329 .uid = IPCSkeleton::GetCallingUid(),
330 .pid = IPCSkeleton::GetCallingPid(),
331 };
332 MessageParcel data;
333 MessageParcel reply;
334 RegisterEventListenerParam param { "networkId" };
335 param.Marshalling(data);
336 ASSERT_TRUE(param.Marshalling(data));
337 auto ret = cooperateServer_->AddWatch(context, CooperateRequestID::REGISTER_EVENT_LISTENER, data, reply);
338 EXPECT_EQ(ret, RET_OK);
339 context_->GetPluginManager().UnloadCooperate();
340 }
341
342 /**
343 * @tc.name: AddWatchTest5
344 * @tc.desc: Test func named add watch
345 * @tc.type: FUNC
346 * @tc.require:
347 */
348 HWTEST_F(CooperateServerTest, AddWatchTest5, TestSize.Level0)
349 {
350 CALL_TEST_DEBUG;
351 CallingContext context {
352 .intention = intention_,
353 .tokenId = IPCSkeleton::GetCallingTokenID(),
354 .uid = IPCSkeleton::GetCallingUid(),
355 .pid = IPCSkeleton::GetCallingPid(),
356 };
357 MessageParcel data;
358 MessageParcel reply;
359 auto ret = cooperateServer_->AddWatch(context, CooperateRequestID::REGISTER_EVENT_LISTENER, data, reply);
360 EXPECT_EQ(ret, RET_ERR);
361 context_->GetPluginManager().UnloadCooperate();
362 }
363
364 /**
365 * @tc.name: RemoveWatch1
366 * @tc.desc: Test func named remove watch
367 * @tc.type: FUNC
368 * @tc.require:
369 */
370 HWTEST_F(CooperateServerTest, RemoveWatch1, TestSize.Level0)
371 {
372 CALL_TEST_DEBUG;
373 CallingContext context {
374 .intention = intention_,
375 .tokenId = IPCSkeleton::GetCallingTokenID(),
376 .uid = IPCSkeleton::GetCallingUid(),
377 .pid = IPCSkeleton::GetCallingPid(),
378 };
379 MessageParcel data;
380 MessageParcel reply;
381 auto ret = cooperateServer_->RemoveWatch(context, CooperateRequestID::UNREGISTER_LISTENER, data, reply);
382 EXPECT_EQ(ret, RET_OK);
383 context_->GetPluginManager().UnloadCooperate();
384 }
385
386 /**
387 * @tc.name: RemoveWatch2
388 * @tc.desc: Test func named remove watch
389 * @tc.type: FUNC
390 * @tc.require:
391 */
392 HWTEST_F(CooperateServerTest, RemoveWatch2, TestSize.Level0)
393 {
394 CALL_TEST_DEBUG;
395 CallingContext context {
396 .intention = intention_,
397 .tokenId = IPCSkeleton::GetCallingTokenID(),
398 .uid = IPCSkeleton::GetCallingUid(),
399 .pid = IPCSkeleton::GetCallingPid(),
400 };
401 MessageParcel data;
402 MessageParcel reply;
403 auto ret = cooperateServer_->RemoveWatch(context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply);
404 EXPECT_EQ(ret, RET_ERR);
405 context_->GetPluginManager().UnloadCooperate();
406 }
407
408 /**
409 * @tc.name: RemoveWatch3
410 * @tc.desc: Test func named remove watch
411 * @tc.type: FUNC
412 * @tc.require:
413 */
414 HWTEST_F(CooperateServerTest, RemoveWatch3, TestSize.Level0)
415 {
416 CALL_TEST_DEBUG;
417 CallingContext context {
418 .intention = intention_,
419 .tokenId = IPCSkeleton::GetCallingTokenID(),
420 .uid = IPCSkeleton::GetCallingUid(),
421 .pid = IPCSkeleton::GetCallingPid(),
422 };
423 MessageParcel data;
424 MessageParcel reply;
425 auto ret = cooperateServer_->RemoveWatch(context, CooperateRequestID::UNREGISTER_HOTAREA_LISTENER, data, reply);
426 EXPECT_EQ(ret, RET_OK);
427 context_->GetPluginManager().UnloadCooperate();
428 }
429
430 /**
431 * @tc.name: RemoveWatch4
432 * @tc.desc: Test func named remove watch
433 * @tc.type: FUNC
434 * @tc.require:
435 */
436 HWTEST_F(CooperateServerTest, RemoveWatch4, TestSize.Level0)
437 {
438 CALL_TEST_DEBUG;
439 CallingContext context {
440 .intention = intention_,
441 .tokenId = IPCSkeleton::GetCallingTokenID(),
442 .uid = IPCSkeleton::GetCallingUid(),
443 .pid = IPCSkeleton::GetCallingPid(),
444 };
445 MessageParcel data;
446 MessageParcel reply;
447 auto ret = cooperateServer_->RemoveWatch(context, CooperateRequestID::UNREGISTER_EVENT_LISTENER, data, reply);
448 EXPECT_EQ(ret, RET_ERR);
449 context_->GetPluginManager().UnloadCooperate();
450 }
451
452 /**
453 * @tc.name: RemoveWatch5
454 * @tc.desc: Test func named remove watch
455 * @tc.type: FUNC
456 * @tc.require:
457 */
458 HWTEST_F(CooperateServerTest, RemoveWatch5, TestSize.Level0)
459 {
460 CALL_TEST_DEBUG;
461 CallingContext context {
462 .intention = intention_,
463 .tokenId = IPCSkeleton::GetCallingTokenID(),
464 .uid = IPCSkeleton::GetCallingUid(),
465 .pid = IPCSkeleton::GetCallingPid(),
466 };
467 MessageParcel data;
468 MessageParcel reply;
469 UnregisterEventListenerParam param { "networkId" };
470 param.Marshalling(data);
471 ASSERT_TRUE(param.Marshalling(data));
472 auto ret = cooperateServer_->RemoveWatch(context, CooperateRequestID::UNREGISTER_EVENT_LISTENER, data, reply);
473 EXPECT_EQ(ret, RET_OK);
474 context_->GetPluginManager().UnloadCooperate();
475 }
476
477 /**
478 * @tc.name: SetParam1
479 * @tc.desc: Test func named set param
480 * @tc.type: FUNC
481 * @tc.require:
482 */
483 HWTEST_F(CooperateServerTest, SetParam1, TestSize.Level0)
484 {
485 CALL_TEST_DEBUG;
486 CallingContext context {
487 .intention = intention_,
488 .tokenId = IPCSkeleton::GetCallingTokenID(),
489 .uid = IPCSkeleton::GetCallingUid(),
490 .pid = IPCSkeleton::GetCallingPid(),
491 };
492 MessageParcel data;
493 MessageParcel reply;
494 auto ret = cooperateServer_->SetParam(context, CooperateRequestID::REGISTER_LISTENER, data, reply);
495 EXPECT_EQ(ret, RET_ERR);
496 }
497
498 /**
499 * @tc.name: GetParam1
500 * @tc.desc: Test func named get param
501 * @tc.type: FUNC
502 * @tc.require:
503 */
504 HWTEST_F(CooperateServerTest, GetParam1, TestSize.Level0)
505 {
506 CALL_TEST_DEBUG;
507 CallingContext context {
508 .intention = intention_,
509 .tokenId = IPCSkeleton::GetCallingTokenID(),
510 .uid = IPCSkeleton::GetCallingUid(),
511 .pid = IPCSkeleton::GetCallingPid(),
512 };
513 MessageParcel data;
514 MessageParcel reply;
515 GetCooperateStateParam param {1, "networkId", true};
516 param.Marshalling(data);
517 ASSERT_TRUE(param.Marshalling(data));
518 auto ret = cooperateServer_->GetParam(context, CooperateRequestID::GET_COOPERATE_STATE, data, reply);
519 EXPECT_EQ(ret, RET_OK);
520 context_->GetPluginManager().UnloadCooperate();
521 }
522
523 /**
524 * @tc.name: GetParam2
525 * @tc.desc: Test func named get param
526 * @tc.type: FUNC
527 * @tc.require:
528 */
529 HWTEST_F(CooperateServerTest, GetParam2, TestSize.Level0)
530 {
531 CALL_TEST_DEBUG;
532 CallingContext context {
533 .intention = intention_,
534 .tokenId = IPCSkeleton::GetCallingTokenID(),
535 .uid = IPCSkeleton::GetCallingUid(),
536 .pid = IPCSkeleton::GetCallingPid(),
537 };
538 MessageParcel data;
539 MessageParcel reply;
540 GetCooperateStateSyncParam param { "networkId" };
541 param.Marshalling(data);
542 ASSERT_TRUE(param.Marshalling(data));
543 auto ret = cooperateServer_->GetParam(context, CooperateRequestID::GET_COOPERATE_STATE_SYNC, data, reply);
544 EXPECT_EQ(ret, RET_OK);
545 context_->GetPluginManager().UnloadCooperate();
546 }
547
548 /**
549 * @tc.name: GetParam3
550 * @tc.desc: Test func named get param
551 * @tc.type: FUNC
552 * @tc.require:
553 */
554 HWTEST_F(CooperateServerTest, GetParam3, TestSize.Level0)
555 {
556 CALL_TEST_DEBUG;
557 CallingContext context {
558 .intention = intention_,
559 .tokenId = IPCSkeleton::GetCallingTokenID(),
560 .uid = IPCSkeleton::GetCallingUid(),
561 .pid = IPCSkeleton::GetCallingPid(),
562 };
563 MessageParcel data;
564 MessageParcel reply;
565 auto ret = cooperateServer_->GetParam(context, CooperateRequestID::GET_COOPERATE_STATE_SYNC, data, reply);
566 EXPECT_EQ(ret, RET_ERR);
567 context_->GetPluginManager().UnloadCooperate();
568 }
569
570 /**
571 * @tc.name: StopTest2
572 * @tc.desc: Test func named stop
573 * @tc.type: FUNC
574 * @tc.require:
575 */
576 HWTEST_F(CooperateServerTest, StopTest2, TestSize.Level0)
577 {
578 CALL_TEST_DEBUG;
579 CallingContext context {
580 .intention = intention_,
581 .tokenId = IPCSkeleton::GetCallingTokenID(),
582 .uid = IPCSkeleton::GetCallingUid(),
583 .pid = IPCSkeleton::GetCallingPid(),
584 };
585 StopCooperateParam param {1, false, true};
586 MessageParcel data;
587 MessageParcel reply;
588 param.Marshalling(data);
589 ASSERT_TRUE(param.Marshalling(data));
590 int32_t ret = cooperateServer_->Stop(context, data, reply);
591 EXPECT_EQ(ret, RET_OK);
592 context_->GetPluginManager().UnloadCooperate();
593 }
594
595 /**
596 * @tc.name: ControlTest1
597 * @tc.desc: Test func named control
598 * @tc.type: FUNC
599 * @tc.require:
600 */
601 HWTEST_F(CooperateServerTest, ControlTest1, TestSize.Level0)
602 {
603 CALL_TEST_DEBUG;
604 CallingContext context {
605 .intention = intention_,
606 .tokenId = IPCSkeleton::GetCallingTokenID(),
607 .uid = IPCSkeleton::GetCallingUid(),
608 .pid = IPCSkeleton::GetCallingPid(),
609 };
610 MessageParcel data;
611 MessageParcel reply;
612 int32_t ret = cooperateServer_->Control(context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply);
613 EXPECT_EQ(ret, RET_ERR);
614 }
615 } // namespace DeviceStatus
616 } // namespace Msdp
617 } // namespace OHOS