• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <thread>
17 
18 #include <gtest/gtest.h>
19 
20 #include "net_mgr_log_wrapper.h"
21 #include "net_policy_callback_test.h"
22 #include "net_policy_client.h"
23 #include "net_policy_constants.h"
24 #include "net_policy_firewall.h"
25 #include "net_policy_inner_define.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 const std::string TEST_STRING_PERIODDURATION = "M1";
30 const std::string ICCID_1 = "sim_abcdefg_1";
31 const std::string ICCID_2 = "sim_abcdefg_2";
32 
33 static std::shared_ptr<NetPolicyFirewall> netPolicyFirewall_ = nullptr;
34 
35 using namespace testing::ext;
36 class UtNetPolicyFirewall : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42     sptr<NetPolicyCallbackTest> GetINetPolicyCallbackSample() const;
43 };
44 
SetUpTestCase()45 void UtNetPolicyFirewall::SetUpTestCase()
46 {
47     netPolicyFirewall_ = std::make_shared<NetPolicyFirewall>();
48     netPolicyFirewall_->Init();
49 }
50 
TearDownTestCase()51 void UtNetPolicyFirewall::TearDownTestCase()
52 {
53     netPolicyFirewall_.reset();
54 }
55 
SetUp()56 void UtNetPolicyFirewall::SetUp() {}
57 
TearDown()58 void UtNetPolicyFirewall::TearDown() {}
59 
60 /**
61  * @tc.name: NetPolicyFirewall001
62  * @tc.desc: Test NetPolicyFirewall SetDeviceIdleAllowedList.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall001, TestSize.Level1)
66 {
67     const uint32_t uid = 123;
68     netPolicyFirewall_->SetDeviceIdleAllowedList(uid, false);
69     std::vector<uint32_t> allowedList;
70     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
71     ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) == allowedList.end());
72 }
73 
74 /**
75  * @tc.name: NetPolicyFirewall002
76  * @tc.desc: Test NetPolicyFirewall GetDeviceIdleAllowedList.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall002, TestSize.Level1)
80 {
81     const uint32_t uid = 456;
82     netPolicyFirewall_->SetDeviceIdleAllowedList(uid, true);
83     std::vector<uint32_t> allowedList;
84     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
85     ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
86 }
87 
88 /**
89  * @tc.name: NetPolicyFirewall003
90  * @tc.desc: Test NetPolicyFirewall UpdateDeviceIdlePolicy
91  * @tc.type: FUNC
92  */
93 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall003, TestSize.Level1)
94 {
95     const uint32_t uid = 789;
96     netPolicyFirewall_->SetDeviceIdleAllowedList(uid, true);
97     netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
98     std::vector<uint32_t> allowedList;
99     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
100     ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
101 }
102 
103 /**
104  * @tc.name: NetPolicyFirewall004
105  * @tc.desc: Test NetPolicyFirewall ResetPolicies
106  * @tc.type: FUNC
107  */
108 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall004, TestSize.Level1)
109 {
110     netPolicyFirewall_->ResetPolicies();
111     std::vector<uint32_t> allowedList;
112     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
113     ASSERT_TRUE(allowedList.size() == 0);
114 }
115 
116 /**
117  * @tc.name: NetPolicyFirewall005
118  * @tc.desc: Test NetPolicyFirewall HandleEvent.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall005, TestSize.Level1)
122 {
123     const uint32_t uid = 101;
124     netPolicyFirewall_->SetDeviceIdleAllowedList(uid, true);
125     std::vector<uint32_t> allowedList;
126     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
127     ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
128     auto policyEvent = std::make_shared<PolicyEvent>();
129     policyEvent->deletedUid = uid;
130     netPolicyFirewall_->HandleEvent(NetPolicyEventHandler::MSG_UID_REMOVED, policyEvent);
131     netPolicyFirewall_->GetDeviceIdleAllowedList(allowedList);
132     ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) == allowedList.end());
133 }
134 
135 /**
136  * @tc.name: NetPolicyFirewall006
137  * @tc.desc: Test NetPolicyFirewall UpdateDeviceIdlePolicy.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall006, TestSize.Level1)
141 {
142     netPolicyFirewall_->UpdateDeviceIdlePolicy(false);
143     netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
144     int32_t ret = netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
145     EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
146 }
147 } // namespace NetManagerStandard
148 } // namespace OHOS
149