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 <gtest/gtest.h>
17 
18 #define private public
19 #include "bandwidth_manager.h"
20 #undef private
21 #include "iptables_type.h"
22 #include "net_manager_constants.h"
23 #include "netnative_log_wrapper.h"
24 #include "netsys_controller.h"
25 #include "netsys_native_service_proxy.h"
26 
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace NetsysNative {
32 using namespace testing::ext;
33 using namespace NetManagerStandard;
34 using namespace nmd;
35 std::shared_ptr<BandwidthManager> g_BandwidthManager = nullptr;
36 
37 class BandwidthManagerTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase()45 void BandwidthManagerTest::SetUpTestCase()
46 {
47     g_BandwidthManager = std::make_shared<BandwidthManager>();
48 }
49 
TearDownTestCase()50 void BandwidthManagerTest::TearDownTestCase()
51 {
52     g_BandwidthManager.reset();
53 }
54 
SetUp()55 void BandwidthManagerTest::SetUp() {}
56 
TearDown()57 void BandwidthManagerTest::TearDown() {}
58 
59 /**
60  * @tc.name: BandwidthInnerFuctionTest
61  * @tc.desc: Test BandwidthManager BandwidthInnerFuctionTest.
62  * @tc.type: FUNC
63  */
64 HWTEST_F(BandwidthManagerTest, BandwidthInnerFuctionTest, TestSize.Level1)
65 {
66     if (!g_BandwidthManager->chainInitFlag_) {
67         int32_t ret = g_BandwidthManager->InitChain();
68         EXPECT_EQ(ret, NETMANAGER_SUCCESS);
69         ret = g_BandwidthManager->InitDefaultRules();
70         EXPECT_EQ(ret, NETMANAGER_SUCCESS);
71     }
72     ChainType chain = ChainType::CHAIN_OHBW_INPUT;
73     std::string resultStr = g_BandwidthManager->FetchChainName(chain);
74     EXPECT_EQ((resultStr == "ohbw_INPUT"), true);
75 
76     chain = ChainType::CHAIN_OHBW_OUTPUT;
77     resultStr = g_BandwidthManager->FetchChainName(chain);
78     EXPECT_EQ((resultStr == "ohbw_OUTPUT"), true);
79 
80     chain = ChainType::CHAIN_OHBW_FORWARD;
81     resultStr = g_BandwidthManager->FetchChainName(chain);
82     EXPECT_EQ((resultStr == "ohbw_FORWARD"), true);
83 
84     chain = ChainType::CHAIN_OHBW_DENIED_LIST_BOX;
85     resultStr = g_BandwidthManager->FetchChainName(chain);
86     EXPECT_EQ((resultStr == "ohbw_denied_list_box"), true);
87 
88     chain = ChainType::CHAIN_OHBW_ALLOWED_LIST_BOX;
89     resultStr = g_BandwidthManager->FetchChainName(chain);
90     EXPECT_EQ((resultStr == "ohbw_allowed_list_box"), true);
91 
92     chain = ChainType::CHAIN_OHBW_GLOBAL_ALERT;
93     resultStr = g_BandwidthManager->FetchChainName(chain);
94     EXPECT_EQ((resultStr == "ohbw_global_alert"), true);
95 
96     chain = ChainType::CHAIN_OHBW_COSTLY_SHARED;
97     resultStr = g_BandwidthManager->FetchChainName(chain);
98     EXPECT_EQ((resultStr == "ohbw_costly_shared"), true);
99 
100     chain = ChainType::CHAIN_OHBW_DATA_SAVER;
101     resultStr = g_BandwidthManager->FetchChainName(chain);
102     EXPECT_EQ((resultStr == "ohbw_data_saver"), true);
103 
104     chain = ChainType::CHAIN_OHFW_UNDOZABLE;
105     resultStr = g_BandwidthManager->FetchChainName(chain);
106     EXPECT_EQ((resultStr == "oh_unusable"), true);
107     g_BandwidthManager->DeInitChain();
108 }
109 
110 /**
111  * @tc.name: IptablesNewChain000
112  * @tc.desc: Test BandwidthManager IptablesNewChain.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(BandwidthManagerTest, IptablesNewChain000, TestSize.Level1)
116 {
117     std::string chainName = g_BandwidthManager->FetchChainName(ChainType::CHAIN_OHFW_INPUT);
118     int32_t ret = g_BandwidthManager->IptablesNewChain(chainName);
119     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
120 }
121 
122 /**
123  * @tc.name: IptablesDeleteChain000
124  * @tc.desc: Test BandwidthManager IptablesDeleteChain.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(BandwidthManagerTest, IptablesDeleteChain000, TestSize.Level1)
128 {
129     std::string chainName = g_BandwidthManager->FetchChainName(ChainType::CHAIN_OHFW_INPUT);
130     int32_t ret = g_BandwidthManager->IptablesDeleteChain(chainName);
131     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
132 }
133 
134 /**
135  * @tc.name: SetGlobalAlert000
136  * @tc.desc: Test BandwidthManager SetGlobalAlert.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(BandwidthManagerTest, SetGlobalAlert000, TestSize.Level1)
140 {
141     int64_t bytes = 0;
142     int32_t ret = g_BandwidthManager->SetGlobalAlert(BandwidthManager::OP_SET, bytes);
143     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
144 }
145 
146 /**
147  * @tc.name: SetGlobalAlert001
148  * @tc.desc: Test BandwidthManager SetGlobalAlert.
149  * @tc.type: FUNC
150  */
151 HWTEST_F(BandwidthManagerTest, SetGlobalAlert001, TestSize.Level1)
152 {
153     int64_t bytes = 0;
154     int32_t ret = g_BandwidthManager->SetGlobalAlert(BandwidthManager::Operate::OP_UNSET, bytes);
155     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
156 }
157 
158 /**
159  * @tc.name: SetGlobalAlert002
160  * @tc.desc: Test BandwidthManager SetGlobalAlert.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(BandwidthManagerTest, SetGlobalAlert002, TestSize.Level1)
164 {
165     int64_t bytes = 1;
166     int32_t ret = g_BandwidthManager->SetGlobalAlert(BandwidthManager::Operate::OP_SET, bytes);
167     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
168 }
169 
170 /**
171  * @tc.name: SetGlobalAlert003
172  * @tc.desc: Test BandwidthManager SetGlobalAlert.
173  * @tc.type: FUNC
174  */
175 HWTEST_F(BandwidthManagerTest, SetGlobalAlert003, TestSize.Level1)
176 {
177     int64_t bytes = 1;
178     int32_t ret = g_BandwidthManager->SetGlobalAlert(BandwidthManager::Operate::OP_UNSET, bytes);
179     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
180 }
181 
182 /**
183  * @tc.name: SetCostlyAlert000
184  * @tc.desc: Test BandwidthManager SetCostlyAlert.
185  * @tc.type: FUNC
186  */
187 HWTEST_F(BandwidthManagerTest, SetCostlyAlert000, TestSize.Level1)
188 {
189     std::string iface = "wlan0";
190     int64_t bytes = 0;
191     int32_t ret = g_BandwidthManager->SetCostlyAlert(BandwidthManager::Operate::OP_SET, iface, bytes);
192     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
193 }
194 
195 /**
196  * @tc.name: SetCostlyAlert001
197  * @tc.desc: Test BandwidthManager SetCostlyAlert.
198  * @tc.type: FUNC
199  */
200 HWTEST_F(BandwidthManagerTest, SetCostlyAlert001, TestSize.Level1)
201 {
202     std::string iface = "wlan0";
203     int64_t bytes = 0;
204     int32_t ret = g_BandwidthManager->SetCostlyAlert(BandwidthManager::Operate::OP_SET, iface, bytes);
205     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
206     ret = g_BandwidthManager->SetCostlyAlert(BandwidthManager::Operate::OP_UNSET, iface, bytes);
207     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
208 }
209 
210 /**
211  * @tc.name: SetCostlyAlert002
212  * @tc.desc: Test BandwidthManager SetCostlyAlert.
213  * @tc.type: FUNC
214  */
215 HWTEST_F(BandwidthManagerTest, SetCostlyAlert002, TestSize.Level1)
216 {
217     std::string iface = "wlan0";
218     int64_t bytes = 0;
219     int32_t ret = g_BandwidthManager->SetCostlyAlert(BandwidthManager::Operate::OP_SET, iface, bytes);
220     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
221     bytes = 100;
222     ret = g_BandwidthManager->SetCostlyAlert(BandwidthManager::Operate::OP_UNSET, iface, bytes);
223     EXPECT_EQ(ret, NETMANAGER_ERROR);
224 }
225 
226 /**
227  * @tc.name: EnableDataSaver000
228  * @tc.desc: Test BandwidthManager EnableDataSaver.
229  * @tc.type: FUNC
230  */
231 HWTEST_F(BandwidthManagerTest, EnableDataSaver000, TestSize.Level1)
232 {
233     int32_t ret = g_BandwidthManager->EnableDataSaver(false);
234     EXPECT_EQ(ret, NETMANAGER_ERROR);
235 }
236 
237 /**
238  * @tc.name: EnableDataSaver001
239  * @tc.desc: Test BandwidthManager EnableDataSaver.
240  * @tc.type: FUNC
241  */
242 HWTEST_F(BandwidthManagerTest, EnableDataSaver001, TestSize.Level1)
243 {
244     int32_t ret = g_BandwidthManager->EnableDataSaver(true);
245     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
246     ret = g_BandwidthManager->EnableDataSaver(false);
247     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
248 }
249 
250 /**
251  * @tc.name: SetIfaceQuota000
252  * @tc.desc: Test BandwidthManager SetIfaceQuota.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(BandwidthManagerTest, SetIfaceQuota000, TestSize.Level1)
256 {
257     std::string iface = "wlan0";
258     int64_t bytes = 0;
259     int32_t ret = g_BandwidthManager->SetIfaceQuota(iface, bytes);
260     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
261 }
262 
263 /**
264  * @tc.name: SetIfaceQuota001
265  * @tc.desc: Test BandwidthManager SetIfaceQuota.
266  * @tc.type: FUNC
267  */
268 HWTEST_F(BandwidthManagerTest, SetIfaceQuota001, TestSize.Level1)
269 {
270     std::string iface = "// /.";
271     int64_t bytes = 0;
272     int32_t ret = g_BandwidthManager->SetIfaceQuota(iface, bytes);
273     EXPECT_EQ(ret, NETMANAGER_ERROR);
274 }
275 
276 /**
277  * @tc.name: SetIfaceQuota002
278  * @tc.desc: Test BandwidthManager SetIfaceQuota.
279  * @tc.type: FUNC
280  */
281 HWTEST_F(BandwidthManagerTest, SetIfaceQuota002, TestSize.Level1)
282 {
283     std::string iface = "wlan0";
284     int64_t bytes = 0;
285     int32_t ret = g_BandwidthManager->SetIfaceQuota(iface, bytes);
286     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
287     bytes = 1;
288     ret = g_BandwidthManager->SetIfaceQuota(iface, bytes);
289     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
290 }
291 
292 /**
293  * @tc.name: RemoveIfaceQuota000
294  * @tc.desc: Test BandwidthManager RemoveIfaceQuota.
295  * @tc.type: FUNC
296  */
297 HWTEST_F(BandwidthManagerTest, RemoveIfaceQuota000, TestSize.Level1)
298 {
299     std::string iface = "// /.";
300     int32_t ret = g_BandwidthManager->RemoveIfaceQuota(iface);
301     EXPECT_EQ(ret, NETMANAGER_ERROR);
302 }
303 
304 /**
305  * @tc.name: RemoveIfaceQuota001
306  * @tc.desc: Test BandwidthManager RemoveIfaceQuota.
307  * @tc.type: FUNC
308  */
309 HWTEST_F(BandwidthManagerTest, RemoveIfaceQuota001, TestSize.Level1)
310 {
311     std::string iface = "wlan0";
312     int32_t ret = g_BandwidthManager->RemoveIfaceQuota(iface);
313     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
314 }
315 
316 /**
317  * @tc.name: RemoveIfaceQuota002
318  * @tc.desc: Test BandwidthManager RemoveIfaceQuota.
319  * @tc.type: FUNC
320  */
321 HWTEST_F(BandwidthManagerTest, RemoveIfaceQuota002, TestSize.Level1)
322 {
323     std::string iface = "wlan0";
324     int64_t bytes = 0;
325     int32_t ret = g_BandwidthManager->SetIfaceQuota(iface, bytes);
326     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
327     ret = g_BandwidthManager->RemoveIfaceQuota(iface);
328     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
329 }
330 
331 /**
332  * @tc.name: AddDeniedList000
333  * @tc.desc: Test BandwidthManager AddDeniedList.
334  * @tc.type: FUNC
335  */
336 HWTEST_F(BandwidthManagerTest, AddDeniedList000, TestSize.Level1)
337 {
338     uint32_t uid = 150000;
339     int32_t ret = g_BandwidthManager->AddDeniedList(uid);
340     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
341     ret = g_BandwidthManager->AddDeniedList(uid);
342     EXPECT_EQ(ret, NETMANAGER_ERROR);
343 }
344 
345 /**
346  * @tc.name: RemoveDeniedList000
347  * @tc.desc: Test BandwidthManager RemoveDeniedList.
348  * @tc.type: FUNC
349  */
350 HWTEST_F(BandwidthManagerTest, RemoveDeniedList000, TestSize.Level1)
351 {
352     uint32_t uid = 150000;
353     int32_t ret = g_BandwidthManager->RemoveDeniedList(uid);
354     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
355 }
356 
357 /**
358  * @tc.name: RemoveDeniedList001
359  * @tc.desc: Test BandwidthManager RemoveDeniedList.
360  * @tc.type: FUNC
361  */
362 HWTEST_F(BandwidthManagerTest, RemoveDeniedList001, TestSize.Level1)
363 {
364     uint32_t uid = 150000;
365     int32_t ret = g_BandwidthManager->AddDeniedList(uid);
366     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
367     ret = g_BandwidthManager->RemoveDeniedList(uid);
368     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
369 }
370 
371 /**
372  * @tc.name: AddAllowedList000
373  * @tc.desc: Test BandwidthManager AddAllowedList.
374  * @tc.type: FUNC
375  */
376 HWTEST_F(BandwidthManagerTest, AddAllowedList000, TestSize.Level1)
377 {
378     uint32_t uid = 150000;
379     int32_t ret = g_BandwidthManager->AddAllowedList(uid);
380     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
381     ret = g_BandwidthManager->AddAllowedList(uid);
382     EXPECT_EQ(ret, NETMANAGER_ERROR);
383 }
384 
385 /**
386  * @tc.name: RemoveAllowedList000
387  * @tc.desc: Test BandwidthManager RemoveAllowedList.
388  * @tc.type: FUNC
389  */
390 HWTEST_F(BandwidthManagerTest, RemoveAllowedList000, TestSize.Level1)
391 {
392     uint32_t uid = 150000;
393     int32_t ret = g_BandwidthManager->RemoveAllowedList(uid);
394     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
395 }
396 
397 /**
398  * @tc.name: RemoveAllowedList001
399  * @tc.desc: Test BandwidthManager RemoveAllowedList.
400  * @tc.type: FUNC
401  */
402 HWTEST_F(BandwidthManagerTest, RemoveAllowedList001, TestSize.Level1)
403 {
404     uint32_t uid = 150000;
405     int32_t ret = g_BandwidthManager->AddDeniedList(uid);
406     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
407     ret = g_BandwidthManager->RemoveAllowedList(uid);
408     EXPECT_EQ(ret, NETMANAGER_ERROR);
409 }
410 
411 /**
412  * @tc.name: BandwidthManagerBranchTest001
413  * @tc.desc: Test BandwidthManager Branch.
414  * @tc.type: FUNC
415  */
416 HWTEST_F(BandwidthManagerTest, BandwidthManagerBranchTest001, TestSize.Level1)
417 {
418     int32_t ret = g_BandwidthManager->InitDefaultBwChainRules();
419     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
420 
421     ret = g_BandwidthManager->InitDefaultListBoxChainRules();
422     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
423 
424     ret = g_BandwidthManager->InitDefaultAlertChainRules();
425     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
426 
427     ret = g_BandwidthManager->InitDefaultRules();
428     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
429 
430     ret = g_BandwidthManager->IptablesNewChain(ChainType::CHAIN_FORWARD);
431     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
432 
433     ret = g_BandwidthManager->IptablesDeleteChain(ChainType::CHAIN_FORWARD);
434     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
435 
436     std::string ifName = "wlan0";
437     int64_t bytes = 0;
438     ret = g_BandwidthManager->SetIfaceQuotaDetail(ifName, bytes);
439     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
440 }
441 } // namespace NetsysNative
442 } // namespace OHOS