• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <thread>
17 
18 #include <gtest/gtest.h>
19 
20 #ifdef GTEST_API_
21 #define private public
22 #define protected public
23 #endif
24 
25 #include "net_policy_firewall.h"
26 #include "net_policy_rule.h"
27 #include "net_policy_service.h"
28 #include "net_policy_traffic.h"
29 #include "system_ability_definition.h"
30 #include "netmanager_base_test_security.h"
31 #include "net_policy_callback_proxy.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 using namespace testing::ext;
36 namespace {
37 constexpr uint32_t TEST_UID = 1;
38 }
39 
40 class UtNetPolicyService : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46     static inline std::shared_ptr<NetPolicyService> instance_ = nullptr;
47 };
48 
SetUpTestCase()49 void UtNetPolicyService::SetUpTestCase()
50 {
51     instance_ = DelayedSingleton<NetPolicyService>::GetInstance();
52     instance_->netPolicyRule_ = std::make_shared<NetPolicyRule>();
53     instance_->netPolicyFirewall_ = std::make_shared<NetPolicyFirewall>();
54     instance_->netPolicyTraffic_ = std::make_shared<NetPolicyTraffic>();
55 }
56 
TearDownTestCase()57 void UtNetPolicyService::TearDownTestCase() {}
58 
SetUp()59 void UtNetPolicyService::SetUp() {}
60 
TearDown()61 void UtNetPolicyService::TearDown() {}
62 
63 HWTEST_F(UtNetPolicyService, OnStart001, TestSize.Level1)
64 {
65     instance_->OnStart();
66     EXPECT_EQ(instance_->state_, instance_->ServiceRunningState::STATE_STOPPED);
67 }
68 
69 HWTEST_F(UtNetPolicyService, FactoryResetPolicies001, TestSize.Level1)
70 {
71     auto ret = instance_->FactoryResetPolicies();
72     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
73 }
74 
75 HWTEST_F(UtNetPolicyService, RegisterFactoryResetCallback001, TestSize.Level1)
76 {
77     instance_->RegisterFactoryResetCallback();
78     instance_->UpdateNetAccessPolicyToMapFromDB();
79     EXPECT_NE(instance_->netFactoryResetCallback_, nullptr);
80 }
81 
82 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag001, TestSize.Level1)
83 {
84     uint32_t uid = 10000;
85     auto ret = instance_->NotifyNetAccessPolicyDiag(uid);
86     EXPECT_NE(ret, NETMANAGER_SUCCESS);
87 }
88 
89 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag002, TestSize.Level1)
90 {
91     instance_->netPolicyRule_ = nullptr;
92     auto ret = instance_->SetPolicyByUid(0, 0);
93     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
94 
95     uint32_t policy = 0;
96     ret = instance_->GetPolicyByUid(0, policy);
97     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
98 
99     std::vector<uint32_t> uids;
100     ret = instance_->GetUidsByPolicy(0, uids);
101     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
102 }
103 
104 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag003, TestSize.Level1)
105 {
106     instance_->netPolicyRule_ = std::make_shared<NetPolicyRule>();
107     std::string ifaceName = "faces";
108     bool isAllowed = true;
109     std::vector<std::string> newMeteredIfaces;
110     newMeteredIfaces.push_back("faces");
111     instance_->netPolicyTraffic_->UpdateMeteredIfaces(newMeteredIfaces);
112     auto ret = instance_->IsUidNetAllowed(0, ifaceName, isAllowed);
113     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
114 }
115 
116 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag004, TestSize.Level1)
117 {
118     auto ret = instance_->RegisterNetPolicyCallback(NULL);
119     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
120 
121     sptr<IRemoteObject> impl = new (std::nothrow) IPCObjectStub();
122     sptr<NetPolicyCallbackProxy> callback = new (std::nothrow) NetPolicyCallbackProxy(impl);
123     ret = instance_->RegisterNetPolicyCallback(callback);
124     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
125 
126     instance_->netPolicyCallback_ = std::make_shared<NetPolicyCallback>();
127     ret = instance_->RegisterNetPolicyCallback(callback);
128     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
129 }
130 
131 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag005, TestSize.Level1)
132 {
133     sptr<IRemoteObject> impl = new (std::nothrow) IPCObjectStub();
134     sptr<NetPolicyCallbackProxy> callback = new (std::nothrow) NetPolicyCallbackProxy(impl);
135     auto ret = instance_->UnregisterNetPolicyCallback(callback);
136     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
137 
138     instance_->netPolicyCallback_ = nullptr;
139     ret = instance_->UnregisterNetPolicyCallback(callback);
140     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
141 
142     instance_->netPolicyCallback_ = std::make_shared<NetPolicyCallback>();
143     ret = instance_->UnregisterNetPolicyCallback(callback);
144     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
145 }
146 
147 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag006, TestSize.Level1)
148 {
149     instance_->netPolicyTraffic_ = nullptr;
150     std::vector<NetQuotaPolicy> quotaPolicies;
151     auto ret = instance_->SetNetQuotaPolicies(quotaPolicies);
152     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
153 
154     ret = instance_->GetNetQuotaPolicies(quotaPolicies);
155     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
156     instance_->netPolicyFirewall_ = nullptr;
157     std::string simId;
158     ret = instance_->ResetPolicies(simId);
159     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
160 }
161 
162 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag007, TestSize.Level1)
163 {
164     instance_->netPolicyRule_ = nullptr;
165     auto ret = instance_->SetBackgroundPolicy(true);
166     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
167     bool backgroundPolicy = true;
168     ret = instance_->GetBackgroundPolicy(backgroundPolicy);
169     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
170     uint32_t backgroundPolicyOfUid = 0;
171     ret = instance_->GetBackgroundPolicyByUid(0, backgroundPolicyOfUid);
172     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
173     std::vector<uint32_t> uids;
174     instance_->netPolicyFirewall_ = nullptr;
175     ret = instance_->SetDeviceIdleTrustlist(uids, true);
176     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
177     instance_->netPolicyFirewall_ = std::make_shared<NetPolicyFirewall>();
178     for (int i = 0; i <= 1001; i++) {
179         instance_->netPolicyFirewall_->powerSaveAllowedList_.insert(i);
180     }
181 
182     ret = instance_->SetDeviceIdleTrustlist(uids, true);
183     EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
184     instance_->netPolicyTraffic_ = nullptr;
185     std::string message;
186     ret = instance_->GetDumpMessage(message);
187     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
188 }
189 
190 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag008, TestSize.Level1)
191 {
192     instance_->netPolicyRule_ = nullptr;
193     instance_->netPolicyFirewall_ = nullptr;
194     instance_->netPolicyTraffic_ = nullptr;
195     auto ret = instance_->FactoryResetPolicies();
196     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
197 
198     instance_->netPolicyRule_ = nullptr;
199     std::vector<std::string> ifaceNames;
200     ret = instance_->SetNicTrafficAllowed(ifaceNames, true);
201     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
202 
203     instance_->netPolicyRule_ = nullptr;
204     ret = instance_->DeleteNetworkAccessPolicy(0);
205     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
206     instance_->netPolicyRule_ = nullptr;
207     NetworkAccessPolicy policy;
208     ret = instance_->SetNetworkAccessPolicy(0, policy, true);
209     EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
210 }
211 
212 HWTEST_F(UtNetPolicyService, NotifyNetAccessPolicyDiag009, TestSize.Level1)
213 {
214     std::shared_ptr<NetPolicyService> netPolicy = std::make_shared<NetPolicyService>();
215     instance_->netFactoryResetCallback_ = new NetPolicyService::FactoryResetCallBack(netPolicy);
216     instance_->RegisterFactoryResetCallback();
217     EXPECT_EQ(NetManagerCenter::GetInstance().connService_, nullptr);
218 
219     instance_->netPolicyRule_ = nullptr;
220     AccessPolicyParameter parameter = {true, 0, 0};
221     AccessPolicySave policys;
222     auto ret = instance_->GetNetworkAccessPolicy(parameter, policys);
223     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
224 }
225 } // namespace NetManagerStandard
226 } // namespace OHOS
227