• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <vector>
17 
18 #include <gtest/gtest.h>
19 
20 #ifdef GTEST_API_
21 #define private public
22 #define protected public
23 #endif
24 #include "net_policy_service_stub.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ":"
30 constexpr const char *ETH_IFACE_NAME = "lo";
31 constexpr int32_t TEST_UID = 1010;
32 class MockNetPolicyServiceStubTest : public NetPolicyServiceStub {
33 public:
34     MockNetPolicyServiceStubTest() = default;
35     ~MockNetPolicyServiceStubTest() = default;
SetPolicyByUid(uint32_t uid,uint32_t policy)36     int32_t SetPolicyByUid(uint32_t uid, uint32_t policy) override
37     {
38         return 0;
39     }
40 
GetPolicyByUid(uint32_t uid,uint32_t & policy)41     int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy) override
42     {
43         return 0;
44     }
45 
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)46     int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids) override
47     {
48         return 0;
49     }
50 
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)51     int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed) override
52     {
53         return 0;
54     }
55 
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)56     int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed) override
57     {
58         return 0;
59     }
60 
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)61     int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override
62     {
63         return 0;
64     }
65 
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)66     int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override
67     {
68         return 0;
69     }
70 
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)71     int32_t SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies) override
72     {
73         return 0;
74     }
75 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)76     int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies) override
77     {
78         return 0;
79     }
80 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)81     int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType) override
82     {
83         return 0;
84     }
85 
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)86     int32_t SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed) override
87     {
88         return 0;
89     }
90 
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)91     int32_t GetDeviceIdleTrustlist(std::vector<uint32_t> &uids) override
92     {
93         return 0;
94     }
95 
SetDeviceIdlePolicy(bool enable)96     int32_t SetDeviceIdlePolicy(bool enable) override
97     {
98         return 0;
99     }
100 
ResetPolicies(const std::string & simId)101     int32_t ResetPolicies(const std::string &simId) override
102     {
103         return 0;
104     }
105 
SetBackgroundPolicy(bool isAllowed)106     int32_t SetBackgroundPolicy(bool isAllowed) override
107     {
108         return 0;
109     }
110 
GetBackgroundPolicy(bool & backgroundPolicy)111     int32_t GetBackgroundPolicy(bool &backgroundPolicy) override
112     {
113         return 0;
114     }
115 
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)116     int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid) override
117     {
118         return 0;
119     }
120 
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)121     int32_t GetPowerSaveTrustlist(std::vector<uint32_t> &uids) override
122     {
123         return 0;
124     }
125 
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)126     int32_t SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed) override
127     {
128         return 0;
129     }
130 
SetPowerSavePolicy(bool enable)131     int32_t SetPowerSavePolicy(bool enable) override
132     {
133         return 0;
134     }
135 
CheckPermission()136     int32_t CheckPermission() override
137     {
138         return 0;
139     }
140 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)141     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
142     {
143         bool byPassPolicyPermission = false;
144         if (!data.ReadBool(byPassPolicyPermission)) {
145             return NETMANAGER_ERR_READ_DATA_FAIL;
146         }
147 
148         if (!byPassPolicyPermission) {
149             return NetPolicyServiceStub::OnRemoteRequest(code, data, reply, option);
150         }
151 
152         auto itFunc = memberFuncMap_.find(code);
153         int32_t result = NETMANAGER_SUCCESS;
154         if (itFunc != memberFuncMap_.end()) {
155             auto requestFunc = itFunc->second;
156             if (requestFunc != nullptr) {
157                 handler_->PostSyncTask(
158                     [this, &data, &reply, &requestFunc, &result]() { result = (this->*requestFunc)(data, reply); },
159                     AppExecFwk::EventQueue::Priority::HIGH);
160                 return result;
161             }
162         }
163 
164         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
165     }
166 };
167 
168 } // namespace
169 
170 using namespace testing::ext;
171 class NetPolicyServiceStubTest : public testing::Test {
172 public:
173     static void SetUpTestCase();
174     static void TearDownTestCase();
175     void SetUp();
176     void TearDown();
177 
178     static inline sptr<NetPolicyServiceStub> instance_ = new (std::nothrow) MockNetPolicyServiceStubTest();
179 };
180 
SetUpTestCase()181 void NetPolicyServiceStubTest::SetUpTestCase() {}
182 
TearDownTestCase()183 void NetPolicyServiceStubTest::TearDownTestCase()
184 {
185     instance_ = nullptr;
186 }
187 
SetUp()188 void NetPolicyServiceStubTest::SetUp() {}
189 
TearDown()190 void NetPolicyServiceStubTest::TearDown() {}
191 
192 /**
193  * @tc.name: OnRemoteRequestTest001
194  * @tc.desc: Test NetPolicyServiceStub OnRemoteRequest.
195  * @tc.type: FUNC
196  */
197 HWTEST_F(NetPolicyServiceStubTest, OnRemoteRequestTest001, TestSize.Level1)
198 {
199     MessageParcel data;
200     MessageParcel reply;
201     MessageOption option;
202     data.WriteBool(false);
203     int32_t ret =
204         instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_END), data, reply, option);
205     EXPECT_EQ(ret, NETMANAGER_ERR_DESCRIPTOR_MISMATCH);
206 }
207 
208 /**
209  * @tc.name: OnRemoteRequestTest002
210  * @tc.desc: Test NetPolicyServiceStub OnRemoteRequest.
211  * @tc.type: FUNC
212  */
213 HWTEST_F(NetPolicyServiceStubTest, OnRemoteRequestTest002, TestSize.Level1)
214 {
215     MessageParcel data;
216     data.WriteBool(false);
217     if (!data.WriteInterfaceToken(NetPolicyServiceStub::GetDescriptor())) {
218         return;
219     }
220     MessageParcel reply;
221     MessageOption option;
222     int32_t ret =
223         instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_END), data, reply, option);
224     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR);
225 }
226 
227 /**
228  * @tc.name: OnRemoteRequestTest003
229  * @tc.desc: Test NetPolicyServiceStub OnRemoteRequest.
230  * @tc.type: FUNC
231  */
232 HWTEST_F(NetPolicyServiceStubTest, OnRemoteRequestTest003, TestSize.Level1)
233 {
234     MessageParcel data;
235     data.WriteBool(false);
236     if (!data.WriteInterfaceToken(NetPolicyServiceStub::GetDescriptor())) {
237         return;
238     }
239     MessageParcel reply;
240     MessageOption option;
241     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POLICY_BY_UID),
242                                              data, reply, option);
243     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
244 
245     int32_t result = NETMANAGER_ERROR;
246     EXPECT_EQ(reply.ReadInt32(result), true);
247     EXPECT_EQ(result, NETMANAGER_ERR_PERMISSION_DENIED);
248 }
249 
250 /**
251  * @tc.name: OnSetPolicyByUidTest001
252  * @tc.desc: Test NetPolicyServiceStub OnSetPolicyByUid.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(NetPolicyServiceStubTest, OnSetPolicyByUidTest001, TestSize.Level1)
256 {
257     MessageParcel data;
258     data.WriteBool(true);
259     data.WriteUint32(TEST_UID);
260     data.WriteUint32(0);
261     MessageParcel reply;
262     MessageOption option;
263     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POLICY_BY_UID),
264                                              data, reply, option);
265     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
266 }
267 
268 /**
269  * @tc.name: OnGetPolicyByUidTest001
270  * @tc.desc: Test NetPolicyServiceStub OnGetPolicyByUid.
271  * @tc.type: FUNC
272  */
273 HWTEST_F(NetPolicyServiceStubTest, OnGetPolicyByUidTest001, TestSize.Level1)
274 {
275     MessageParcel data;
276     data.WriteBool(true);
277     data.WriteUint32(TEST_UID);
278     MessageParcel reply;
279     MessageOption option;
280     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POLICY_BY_UID),
281                                              data, reply, option);
282     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
283 }
284 
285 /**
286  * @tc.name: OnGetUidsByPolicyTest001
287  * @tc.desc: Test NetPolicyServiceStub OnGetUidsByPolicy.
288  * @tc.type: FUNC
289  */
290 HWTEST_F(NetPolicyServiceStubTest, OnGetUidsByPolicyTest001, TestSize.Level1)
291 {
292     MessageParcel data;
293     data.WriteBool(true);
294     data.WriteUint32(TEST_UID);
295     MessageParcel reply;
296     MessageOption option;
297     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_UIDS_BY_POLICY),
298                                              data, reply, option);
299     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
300 }
301 
302 /**
303  * @tc.name: OnIsUidNetAllowedMeteredTest001
304  * @tc.desc: Test NetPolicyServiceStub OnIsUidNetAllowedMetered.
305  * @tc.type: FUNC
306  */
307 HWTEST_F(NetPolicyServiceStubTest, OnIsUidNetAllowedMeteredTest001, TestSize.Level1)
308 {
309     MessageParcel data;
310     data.WriteBool(true);
311     data.WriteUint32(TEST_UID);
312     data.WriteBool(true);
313     MessageParcel reply;
314     MessageOption option;
315     int32_t ret = instance_->OnRemoteRequest(
316         static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_METERED), data, reply, option);
317     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
318 }
319 
320 /**
321  * @tc.name: OnIsUidNetAllowedIfaceNameTest001
322  * @tc.desc: Test NetPolicyServiceStub OnIsUidNetAllowedIfaceName.
323  * @tc.type: FUNC
324  */
325 HWTEST_F(NetPolicyServiceStubTest, OnIsUidNetAllowedIfaceNameTest001, TestSize.Level1)
326 {
327     MessageParcel data;
328     data.WriteBool(true);
329     data.WriteUint32(TEST_UID);
330     data.WriteString(ETH_IFACE_NAME);
331     MessageParcel reply;
332     MessageOption option;
333     int32_t ret = instance_->OnRemoteRequest(
334         static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_IFACE), data, reply, option);
335     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
336 }
337 
338 /**
339  * @tc.name: OnSetNetQuotaPoliciesTest001
340  * @tc.desc: Test NetPolicyServiceStub OnSetNetQuotaPolicies.
341  * @tc.type: FUNC
342  */
343 HWTEST_F(NetPolicyServiceStubTest, OnSetNetQuotaPoliciesTest001, TestSize.Level1)
344 {
345     MessageParcel data;
346     data.WriteBool(true);
347     NetQuotaPolicy quotaPolicy;
348     quotaPolicy.quotapolicy.title = "test";
349     std::vector<NetQuotaPolicy> quotaPolicies;
350     quotaPolicies.emplace_back(quotaPolicy);
351     NetQuotaPolicy::Marshalling(data, quotaPolicies);
352     MessageParcel reply;
353     MessageOption option;
354     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NET_QUOTA_POLICIES),
355                                              data, reply, option);
356     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
357 }
358 
359 /**
360  * @tc.name: OnGetNetQuotaPoliciesTest001
361  * @tc.desc: Test NetPolicyServiceStub OnGetNetQuotaPolicies.
362  * @tc.type: FUNC
363  */
364 HWTEST_F(NetPolicyServiceStubTest, OnGetNetQuotaPoliciesTest001, TestSize.Level1)
365 {
366     MessageParcel data;
367     data.WriteBool(true);
368     MessageParcel reply;
369     MessageOption option;
370     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_NET_QUOTA_POLICIES),
371                                              data, reply, option);
372     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
373 }
374 
375 /**
376  * @tc.name: OnResetPoliciesTest001
377  * @tc.desc: Test NetPolicyServiceStub OnResetPolicies.
378  * @tc.type: FUNC
379  */
380 HWTEST_F(NetPolicyServiceStubTest, OnResetPoliciesTest001, TestSize.Level1)
381 {
382     MessageParcel data;
383     data.WriteBool(true);
384     data.WriteString("subscriberId");
385     MessageParcel reply;
386     MessageOption option;
387     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_RESET_POLICIES), data,
388                                              reply, option);
389     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
390 }
391 
392 /**
393  * @tc.name: OnSetBackgroundPolicyTest001
394  * @tc.desc: Test NetPolicyServiceStub OnSetBackgroundPolicy.
395  * @tc.type: FUNC
396  */
397 HWTEST_F(NetPolicyServiceStubTest, OnSetBackgroundPolicyTest001, TestSize.Level1)
398 {
399     MessageParcel data;
400     data.WriteBool(true);
401     data.WriteBool(true);
402     MessageParcel reply;
403     MessageOption option;
404     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_BACKGROUND_POLICY),
405                                              data, reply, option);
406     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
407 }
408 
409 /**
410  * @tc.name: OnGetBackgroundPolicyTest001
411  * @tc.desc: Test NetPolicyServiceStub OnGetBackgroundPolicy.
412  * @tc.type: FUNC
413  */
414 HWTEST_F(NetPolicyServiceStubTest, OnGetBackgroundPolicyTest001, TestSize.Level1)
415 {
416     MessageParcel data;
417     data.WriteBool(true);
418     MessageParcel reply;
419     MessageOption option;
420     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY),
421                                              data, reply, option);
422     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
423 }
424 
425 /**
426  * @tc.name: OnGetBackgroundPolicyByUidTest001
427  * @tc.desc: Test NetPolicyServiceStub OnGetBackgroundPolicyByUid.
428  * @tc.type: FUNC
429  */
430 HWTEST_F(NetPolicyServiceStubTest, OnGetBackgroundPolicyByUidTest001, TestSize.Level1)
431 {
432     MessageParcel data;
433     data.WriteBool(true);
434     data.WriteBool(true);
435     MessageParcel reply;
436     MessageOption option;
437     int32_t ret = instance_->OnRemoteRequest(
438         static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY_BY_UID), data, reply, option);
439     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
440 }
441 
442 /**
443  * @tc.name: OnSnoozePolicyTest001
444  * @tc.desc: Test NetPolicyServiceStub OnSnoozePolicy.
445  * @tc.type: FUNC
446  */
447 HWTEST_F(NetPolicyServiceStubTest, OnSnoozePolicyTest001, TestSize.Level1)
448 {
449     MessageParcel data;
450     data.WriteBool(true);
451     data.WriteInt32(0);
452     data.WriteString("simId");
453     data.WriteInt32(0);
454     MessageParcel reply;
455     MessageOption option;
456     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UPDATE_REMIND_POLICY),
457                                              data, reply, option);
458     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
459 }
460 
461 /**
462  * @tc.name: OnSetDeviceIdleTrustlistTest003
463  * @tc.desc: Test NetPolicyServiceStub OnSetDeviceIdleTrustlist.
464  * @tc.type: FUNC
465  */
466 HWTEST_F(NetPolicyServiceStubTest, OnSetDeviceIdleTrustlistTest003, TestSize.Level1)
467 {
468     MessageParcel data;
469     data.WriteBool(true);
470     std::vector<uint32_t> uids;
471     uids.emplace_back(TEST_UID);
472     data.WriteUInt32Vector(uids);
473     data.WriteBool(true);
474     MessageParcel reply;
475     MessageOption option;
476     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_IDLE_TRUSTLIST),
477                                              data, reply, option);
478     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
479 }
480 
481 /**
482  * @tc.name: OnGetDeviceIdleTrustlistTest001
483  * @tc.desc: Test NetPolicyServiceStub OnGetDeviceIdleTrustlist.
484  * @tc.type: FUNC
485  */
486 HWTEST_F(NetPolicyServiceStubTest, OnGetDeviceIdleTrustlistTest001, TestSize.Level1)
487 {
488     MessageParcel data;
489     data.WriteBool(true);
490     MessageParcel reply;
491     MessageOption option;
492     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_IDLE_TRUSTLIST),
493                                              data, reply, option);
494     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
495 }
496 
497 /**
498  * @tc.name: OnSetDeviceIdlePolicyTest001
499  * @tc.desc: Test NetPolicyServiceStub OnSetDeviceIdlePolicy.
500  * @tc.type: FUNC
501  */
502 HWTEST_F(NetPolicyServiceStubTest, OnSetDeviceIdlePolicyTest001, TestSize.Level1)
503 {
504     MessageParcel data;
505     data.WriteBool(true);
506     data.WriteBool(true);
507     MessageParcel reply;
508     MessageOption option;
509     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_DEVICE_IDLE_POLICY),
510                                              data, reply, option);
511     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
512 }
513 
514 /**
515  * @tc.name: OnGetPowerSaveTrustlistTest001
516  * @tc.desc: Test NetPolicyServiceStub OnGetPowerSaveTrustlist.
517  * @tc.type: FUNC
518  */
519 HWTEST_F(NetPolicyServiceStubTest, OnGetPowerSaveTrustlistTest001, TestSize.Level1)
520 {
521     MessageParcel data;
522     data.WriteBool(true);
523     MessageParcel reply;
524     MessageOption option;
525     int32_t ret = instance_->OnRemoteRequest(
526         static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POWER_SAVE_TRUSTLIST), data, reply, option);
527     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
528 }
529 
530 /**
531  * @tc.name: OnSetPowerSaveTrustlistTest001
532  * @tc.desc: Test NetPolicyServiceStub OnSetPowerSaveTrustlist.
533  * @tc.type: FUNC
534  */
535 HWTEST_F(NetPolicyServiceStubTest, OnSetPowerSaveTrustlistTest001, TestSize.Level1)
536 {
537     MessageParcel data;
538     data.WriteBool(true);
539     std::vector<uint32_t> uids;
540     uids.emplace_back(TEST_UID);
541     data.WriteUInt32Vector(uids);
542     data.WriteBool(true);
543     MessageParcel reply;
544     MessageOption option;
545     int32_t ret = instance_->OnRemoteRequest(
546         static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_TRUSTLIST), data, reply, option);
547     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
548 }
549 
550 } // namespace NetManagerStandard
551 } // namespace OHOS