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     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Enable(context, datas, reply));
134 }
135 
136 /**
137  * @tc.name: DisableTest1
138  * @tc.desc: Test func named disable
139  * @tc.type: FUNC
140  * @tc.require:
141  */
142 HWTEST_F(CooperateServerTest, DisableTest1, TestSize.Level0)
143 {
144     CALL_TEST_DEBUG;
145     CallingContext context {
146         .intention = intention_,
147         .tokenId = IPCSkeleton::GetCallingTokenID(),
148         .uid = IPCSkeleton::GetCallingUid(),
149         .pid = IPCSkeleton::GetCallingPid(),
150     };
151     MessageParcel datas;
152     MessageParcel reply;
153     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Disable(context, datas, reply));
154 }
155 
156 /**
157  * @tc.name: StartTest1
158  * @tc.desc: Test func named start
159  * @tc.type: FUNC
160  * @tc.require:
161  */
162 HWTEST_F(CooperateServerTest, StartTest1, TestSize.Level0)
163 {
164     CALL_TEST_DEBUG;
165     CallingContext context {
166         .intention = intention_,
167         .tokenId = IPCSkeleton::GetCallingTokenID(),
168         .uid = IPCSkeleton::GetCallingUid(),
169         .pid = IPCSkeleton::GetCallingPid(),
170     };
171     MessageParcel datas;
172     MessageParcel reply;
173     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Start(context, datas, reply));
174 }
175 
176 /**
177  * @tc.name: StopTest1
178  * @tc.desc: Test func named stop
179  * @tc.type: FUNC
180  * @tc.require:
181  */
182 HWTEST_F(CooperateServerTest, StopTest1, TestSize.Level0)
183 {
184     CALL_TEST_DEBUG;
185     CallingContext context {
186         .intention = intention_,
187         .tokenId = IPCSkeleton::GetCallingTokenID(),
188         .uid = IPCSkeleton::GetCallingUid(),
189         .pid = IPCSkeleton::GetCallingPid(),
190     };
191     MessageParcel datas;
192     MessageParcel reply;
193     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Stop(context, datas, reply));
194 }
195 
196 /**
197  * @tc.name: EnableTest2
198  * @tc.desc: Test func named enable
199  * @tc.type: FUNC
200  * @tc.require:
201  */
202 HWTEST_F(CooperateServerTest, EnableTest2, TestSize.Level0)
203 {
204     CALL_TEST_DEBUG;
205     CallingContext context {
206         .intention = intention_,
207         .tokenId = IPCSkeleton::GetCallingTokenID(),
208         .uid = IPCSkeleton::GetCallingUid(),
209         .pid = IPCSkeleton::GetCallingPid(),
210     };
211     MessageParcel data;
212     MessageParcel reply;
213     DefaultParam  param { 1 };
214     ASSERT_TRUE(param.Marshalling(data));
215     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Enable(context, data, reply));
216     context_->GetPluginManager().UnloadCooperate();
217 }
218 
219 /**
220  * @tc.name: AddWatchTest1
221  * @tc.desc: Test func named add watch
222  * @tc.type: FUNC
223  * @tc.require:
224  */
225 HWTEST_F(CooperateServerTest, AddWatchTest1, TestSize.Level0)
226 {
227     CALL_TEST_DEBUG;
228     CallingContext context {
229         .intention = intention_,
230         .tokenId = IPCSkeleton::GetCallingTokenID(),
231         .uid = IPCSkeleton::GetCallingUid(),
232         .pid = IPCSkeleton::GetCallingPid(),
233     };
234     MessageParcel data;
235     MessageParcel reply;
236     DefaultParam  param { 1 };
237     ASSERT_TRUE(param.Marshalling(data));
238     ASSERT_NO_FATAL_FAILURE(cooperateServer_->AddWatch(context, CooperateRequestID::REGISTER_LISTENER, data, reply));
239     context_->GetPluginManager().UnloadCooperate();
240 }
241 
242 /**
243  * @tc.name: DisableTest2
244  * @tc.desc: Test func named disable
245  * @tc.type: FUNC
246  * @tc.require:
247  */
248 HWTEST_F(CooperateServerTest, DisableTest2, TestSize.Level0)
249 {
250     CALL_TEST_DEBUG;
251     CallingContext context {
252         .intention = intention_,
253         .tokenId = IPCSkeleton::GetCallingTokenID(),
254         .uid = IPCSkeleton::GetCallingUid(),
255         .pid = IPCSkeleton::GetCallingPid(),
256     };
257     DefaultParam  param { 1 };
258     MessageParcel datas;
259     MessageParcel reply;
260     param.Marshalling(datas);
261     ASSERT_TRUE(param.Marshalling(datas));
262     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Disable(context, datas, reply));
263     context_->GetPluginManager().UnloadCooperate();
264 }
265 
266 /**
267  * @tc.name: AddWatchTest2
268  * @tc.desc: Test func named add watch
269  * @tc.type: FUNC
270  * @tc.require:
271  */
272 HWTEST_F(CooperateServerTest, AddWatchTest2, TestSize.Level0)
273 {
274     CALL_TEST_DEBUG;
275     CallingContext context {
276         .intention = intention_,
277         .tokenId = IPCSkeleton::GetCallingTokenID(),
278         .uid = IPCSkeleton::GetCallingUid(),
279         .pid = IPCSkeleton::GetCallingPid(),
280     };
281     MessageParcel data;
282     MessageParcel reply;
283     ASSERT_NO_FATAL_FAILURE(cooperateServer_->AddWatch(
284         context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply));
285     context_->GetPluginManager().UnloadCooperate();
286 }
287 
288 /**
289  * @tc.name: AddWatchTest3
290  * @tc.desc: Test func named add watch
291  * @tc.type: FUNC
292  * @tc.require:
293  */
294 HWTEST_F(CooperateServerTest, AddWatchTest3, TestSize.Level0)
295 {
296     CALL_TEST_DEBUG;
297     CallingContext context {
298         .intention = intention_,
299         .tokenId = IPCSkeleton::GetCallingTokenID(),
300         .uid = IPCSkeleton::GetCallingUid(),
301         .pid = IPCSkeleton::GetCallingPid(),
302     };
303     MessageParcel data;
304     MessageParcel reply;
305     ASSERT_NO_FATAL_FAILURE(cooperateServer_->AddWatch(
306         context, CooperateRequestID::REGISTER_HOTAREA_LISTENER, data, reply));
307     context_->GetPluginManager().UnloadCooperate();
308 }
309 
310 /**
311  * @tc.name: AddWatchTest4
312  * @tc.desc: Test func named add watch
313  * @tc.type: FUNC
314  * @tc.require:
315  */
316 HWTEST_F(CooperateServerTest, AddWatchTest4, TestSize.Level0)
317 {
318     CALL_TEST_DEBUG;
319     CallingContext context {
320         .intention = intention_,
321         .tokenId = IPCSkeleton::GetCallingTokenID(),
322         .uid = IPCSkeleton::GetCallingUid(),
323         .pid = IPCSkeleton::GetCallingPid(),
324     };
325     MessageParcel data;
326     MessageParcel reply;
327     RegisterEventListenerParam  param { "networkId" };
328     param.Marshalling(data);
329     ASSERT_TRUE(param.Marshalling(data));
330     ASSERT_NO_FATAL_FAILURE(cooperateServer_->AddWatch(
331         context, CooperateRequestID::REGISTER_EVENT_LISTENER, data, reply));
332     context_->GetPluginManager().UnloadCooperate();
333 }
334 
335 /**
336  * @tc.name: AddWatchTest5
337  * @tc.desc: Test func named add watch
338  * @tc.type: FUNC
339  * @tc.require:
340  */
341 HWTEST_F(CooperateServerTest, AddWatchTest5, TestSize.Level0)
342 {
343     CALL_TEST_DEBUG;
344     CallingContext context {
345         .intention = intention_,
346         .tokenId = IPCSkeleton::GetCallingTokenID(),
347         .uid = IPCSkeleton::GetCallingUid(),
348         .pid = IPCSkeleton::GetCallingPid(),
349     };
350     MessageParcel data;
351     MessageParcel reply;
352     ASSERT_NO_FATAL_FAILURE(cooperateServer_->AddWatch(
353         context, CooperateRequestID::REGISTER_EVENT_LISTENER, data, reply));
354     context_->GetPluginManager().UnloadCooperate();
355 }
356 
357 /**
358  * @tc.name: RemoveWatch1
359  * @tc.desc: Test func named remove watch
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(CooperateServerTest, RemoveWatch1, TestSize.Level0)
364 {
365     CALL_TEST_DEBUG;
366     CallingContext context {
367         .intention = intention_,
368         .tokenId = IPCSkeleton::GetCallingTokenID(),
369         .uid = IPCSkeleton::GetCallingUid(),
370         .pid = IPCSkeleton::GetCallingPid(),
371     };
372     MessageParcel data;
373     MessageParcel reply;
374     ASSERT_NO_FATAL_FAILURE(cooperateServer_->RemoveWatch(
375         context, CooperateRequestID::UNREGISTER_LISTENER, data, reply));
376     context_->GetPluginManager().UnloadCooperate();
377 }
378 
379 /**
380  * @tc.name: RemoveWatch2
381  * @tc.desc: Test func named remove watch
382  * @tc.type: FUNC
383  * @tc.require:
384  */
385 HWTEST_F(CooperateServerTest, RemoveWatch2, TestSize.Level0)
386 {
387     CALL_TEST_DEBUG;
388     CallingContext context {
389         .intention = intention_,
390         .tokenId = IPCSkeleton::GetCallingTokenID(),
391         .uid = IPCSkeleton::GetCallingUid(),
392         .pid = IPCSkeleton::GetCallingPid(),
393     };
394     MessageParcel data;
395     MessageParcel reply;
396     ASSERT_NO_FATAL_FAILURE(cooperateServer_->RemoveWatch(
397         context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply));
398     context_->GetPluginManager().UnloadCooperate();
399 }
400 
401 /**
402  * @tc.name: RemoveWatch3
403  * @tc.desc: Test func named remove watch
404  * @tc.type: FUNC
405  * @tc.require:
406  */
407 HWTEST_F(CooperateServerTest, RemoveWatch3, TestSize.Level0)
408 {
409     CALL_TEST_DEBUG;
410     CallingContext context {
411         .intention = intention_,
412         .tokenId = IPCSkeleton::GetCallingTokenID(),
413         .uid = IPCSkeleton::GetCallingUid(),
414         .pid = IPCSkeleton::GetCallingPid(),
415     };
416     MessageParcel data;
417     MessageParcel reply;
418     ASSERT_NO_FATAL_FAILURE(cooperateServer_->RemoveWatch(
419         context, CooperateRequestID::UNREGISTER_HOTAREA_LISTENER, data, reply));
420     context_->GetPluginManager().UnloadCooperate();
421 }
422 
423 /**
424  * @tc.name: RemoveWatch4
425  * @tc.desc: Test func named remove watch
426  * @tc.type: FUNC
427  * @tc.require:
428  */
429 HWTEST_F(CooperateServerTest, RemoveWatch4, TestSize.Level0)
430 {
431     CALL_TEST_DEBUG;
432     CallingContext context {
433         .intention = intention_,
434         .tokenId = IPCSkeleton::GetCallingTokenID(),
435         .uid = IPCSkeleton::GetCallingUid(),
436         .pid = IPCSkeleton::GetCallingPid(),
437     };
438     MessageParcel data;
439     MessageParcel reply;
440     ASSERT_NO_FATAL_FAILURE(cooperateServer_->RemoveWatch(
441         context, CooperateRequestID::UNREGISTER_EVENT_LISTENER, data, reply));
442     context_->GetPluginManager().UnloadCooperate();
443 }
444 
445 /**
446  * @tc.name: RemoveWatch5
447  * @tc.desc: Test func named remove watch
448  * @tc.type: FUNC
449  * @tc.require:
450  */
451 HWTEST_F(CooperateServerTest, RemoveWatch5, TestSize.Level0)
452 {
453     CALL_TEST_DEBUG;
454     CallingContext context {
455         .intention = intention_,
456         .tokenId = IPCSkeleton::GetCallingTokenID(),
457         .uid = IPCSkeleton::GetCallingUid(),
458         .pid = IPCSkeleton::GetCallingPid(),
459     };
460     MessageParcel data;
461     MessageParcel reply;
462     UnregisterEventListenerParam param { "networkId" };
463     param.Marshalling(data);
464     ASSERT_TRUE(param.Marshalling(data));
465     ASSERT_NO_FATAL_FAILURE(cooperateServer_->RemoveWatch(
466         context, CooperateRequestID::UNREGISTER_EVENT_LISTENER, data, reply));
467     context_->GetPluginManager().UnloadCooperate();
468 }
469 
470 /**
471  * @tc.name: SetParam1
472  * @tc.desc: Test func named set param
473  * @tc.type: FUNC
474  * @tc.require:
475  */
476 HWTEST_F(CooperateServerTest, SetParam1, TestSize.Level0)
477 {
478     CALL_TEST_DEBUG;
479     CallingContext context {
480         .intention = intention_,
481         .tokenId = IPCSkeleton::GetCallingTokenID(),
482         .uid = IPCSkeleton::GetCallingUid(),
483         .pid = IPCSkeleton::GetCallingPid(),
484     };
485     MessageParcel data;
486     MessageParcel reply;
487     ASSERT_NO_FATAL_FAILURE(cooperateServer_->SetParam(
488         context, CooperateRequestID::REGISTER_LISTENER, data, reply));
489 }
490 
491 /**
492  * @tc.name: GetParam1
493  * @tc.desc: Test func named get param
494  * @tc.type: FUNC
495  * @tc.require:
496  */
497 HWTEST_F(CooperateServerTest, GetParam1, TestSize.Level0)
498 {
499     CALL_TEST_DEBUG;
500     CallingContext context {
501         .intention = intention_,
502         .tokenId = IPCSkeleton::GetCallingTokenID(),
503         .uid = IPCSkeleton::GetCallingUid(),
504         .pid = IPCSkeleton::GetCallingPid(),
505     };
506     MessageParcel data;
507     MessageParcel reply;
508     GetCooperateStateParam param {1, "networkId", true};
509     param.Marshalling(data);
510     ASSERT_TRUE(param.Marshalling(data));
511     ASSERT_NO_FATAL_FAILURE(cooperateServer_->GetParam(
512         context, CooperateRequestID::GET_COOPERATE_STATE, data, reply));
513     context_->GetPluginManager().UnloadCooperate();
514 }
515 
516 /**
517  * @tc.name: GetParam2
518  * @tc.desc: Test func named get param
519  * @tc.type: FUNC
520  * @tc.require:
521  */
522 HWTEST_F(CooperateServerTest, GetParam2, TestSize.Level0)
523 {
524     CALL_TEST_DEBUG;
525     CallingContext context {
526         .intention = intention_,
527         .tokenId = IPCSkeleton::GetCallingTokenID(),
528         .uid = IPCSkeleton::GetCallingUid(),
529         .pid = IPCSkeleton::GetCallingPid(),
530     };
531     MessageParcel data;
532     MessageParcel reply;
533     GetCooperateStateSyncParam param { "networkId" };
534     param.Marshalling(data);
535     ASSERT_TRUE(param.Marshalling(data));
536     ASSERT_NO_FATAL_FAILURE(cooperateServer_->GetParam(
537         context, CooperateRequestID::GET_COOPERATE_STATE_SYNC, data, reply));
538     context_->GetPluginManager().UnloadCooperate();
539 }
540 
541 /**
542  * @tc.name: GetParam3
543  * @tc.desc: Test func named get param
544  * @tc.type: FUNC
545  * @tc.require:
546  */
547 HWTEST_F(CooperateServerTest, GetParam3, TestSize.Level0)
548 {
549     CALL_TEST_DEBUG;
550     CallingContext context {
551         .intention = intention_,
552         .tokenId = IPCSkeleton::GetCallingTokenID(),
553         .uid = IPCSkeleton::GetCallingUid(),
554         .pid = IPCSkeleton::GetCallingPid(),
555     };
556     MessageParcel data;
557     MessageParcel reply;
558     ASSERT_NO_FATAL_FAILURE(cooperateServer_->GetParam(
559         context, CooperateRequestID::GET_COOPERATE_STATE_SYNC, data, reply));
560     context_->GetPluginManager().UnloadCooperate();
561 }
562 
563 /**
564  * @tc.name: StopTest2
565  * @tc.desc: Test func named stop
566  * @tc.type: FUNC
567  * @tc.require:
568  */
569 HWTEST_F(CooperateServerTest, StopTest2, TestSize.Level0)
570 {
571     CALL_TEST_DEBUG;
572     CallingContext context {
573         .intention = intention_,
574         .tokenId = IPCSkeleton::GetCallingTokenID(),
575         .uid = IPCSkeleton::GetCallingUid(),
576         .pid = IPCSkeleton::GetCallingPid(),
577     };
578     StopCooperateParam param {1, false, true};
579     MessageParcel data;
580     MessageParcel reply;
581     param.Marshalling(data);
582     ASSERT_TRUE(param.Marshalling(data));
583     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Stop(context, data, reply));
584     context_->GetPluginManager().UnloadCooperate();
585 }
586 
587 /**
588  * @tc.name: ControlTest1
589  * @tc.desc: Test func named control
590  * @tc.type: FUNC
591  * @tc.require:
592  */
593 HWTEST_F(CooperateServerTest, ControlTest1, TestSize.Level0)
594 {
595     CALL_TEST_DEBUG;
596     CallingContext context {
597         .intention = intention_,
598         .tokenId = IPCSkeleton::GetCallingTokenID(),
599         .uid = IPCSkeleton::GetCallingUid(),
600         .pid = IPCSkeleton::GetCallingPid(),
601     };
602     MessageParcel data;
603     MessageParcel reply;
604     ASSERT_NO_FATAL_FAILURE(cooperateServer_->Control(
605         context, CooperateRequestID::UNKNOWN_COOPERATE_ACTION, data, reply));
606 }
607 } // namespace DeviceStatus
608 } // namespace Msdp
609 } // namespace OHOS