• 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 <algorithm>
17 #include <gtest/gtest.h>
18 
19 #include "net_manager_constants.h"
20 #include "netnative_log_wrapper.h"
21 #include "netsys_controller.h"
22 #define private public
23 #define protected public
24 #include "interface_manager.h"
25 #include "physical_network.h"
26 #undef private
27 #undef protected
28 
29 namespace OHOS {
30 namespace NetsysNative {
31 namespace {
32 using namespace testing::ext;
33 using namespace OHOS::nmd;
34 using namespace OHOS::NetManagerStandard;
35 constexpr uint16_t TEST_NETID = 2000;
36 } // namespace
37 class PhysicalNetworkTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43     static inline std::shared_ptr<PhysicalNetwork> instance_ = nullptr;
44 };
45 
SetUpTestCase()46 void PhysicalNetworkTest::SetUpTestCase()
47 {
48     instance_ = std::make_shared<PhysicalNetwork>(TEST_NETID, NetworkPermission::PERMISSION_NETWORK);
49 }
50 
TearDownTestCase()51 void PhysicalNetworkTest::TearDownTestCase() {}
52 
SetUp()53 void PhysicalNetworkTest::SetUp() {}
54 
TearDown()55 void PhysicalNetworkTest::TearDown() {}
56 
57 HWTEST_F(PhysicalNetworkTest, AddInterfaceTest001, TestSize.Level1)
58 {
59     NETNATIVE_LOGI("AddInterfaceTest001 enter");
60     PhysicalNetwork physicNetwork(2, NetworkPermission::PERMISSION_NETWORK);
61     std::string interfaceName1 = "wlan0";
62     auto ifaceList = InterfaceManager::GetInterfaceNames();
63     bool wlan0NotExist = std::find(ifaceList.begin(), ifaceList.end(), interfaceName1) == ifaceList.end();
64     if (wlan0NotExist) {
65         return;
66     }
67     int32_t ret = physicNetwork.AddInterface(interfaceName1);
68     EXPECT_EQ(ret, 0);
69     physicNetwork.AddDefault();
70     physicNetwork.interfaces_.insert(interfaceName1);
71     ret = physicNetwork.AddInterface(interfaceName1);
72     EXPECT_EQ(ret, 0);
73     std::string interfaceName2 = "eth1";
74     ifaceList = InterfaceManager::GetInterfaceNames();
75     bool eth1Exist = std::find(ifaceList.begin(), ifaceList.end(), interfaceName2) != ifaceList.end();
76     if (eth1Exist) {
77         ret = physicNetwork.AddInterface(interfaceName2);
78         EXPECT_EQ(ret, 0);
79     }
80 
81     ret = physicNetwork.RemoveInterface(interfaceName1);
82     physicNetwork.RemoveDefault();
83     physicNetwork.RemoveInterface(interfaceName2);
84     std::string interfaceName3 = "eth2";
85     physicNetwork.RemoveInterface(interfaceName3);
86     physicNetwork.IsPhysical();
87     physicNetwork.GetPermission();
88     physicNetwork.GetNetworkType();
89 }
90 
91 HWTEST_F(PhysicalNetworkTest, AddInterfaceTest002, TestSize.Level1)
92 {
93     std::string interfaceName = "test";
94     auto ret = instance_->AddInterface(interfaceName);
95     EXPECT_EQ(ret, NETMANAGER_ERROR);
96     interfaceName = "wlan0";
97     ret = instance_->AddInterface(interfaceName);
98     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
99     interfaceName = "wlan1";
100     auto ifaceList = InterfaceManager::GetInterfaceNames();
101     bool wlan1Exist = std::find(ifaceList.begin(), ifaceList.end(), interfaceName) != ifaceList.end();
102     if (wlan1Exist) {
103         return;
104     }
105     ret = instance_->AddInterface(interfaceName);
106     EXPECT_EQ(ret, NETMANAGER_ERROR);
107 }
108 
109 HWTEST_F(PhysicalNetworkTest, AddInterfaceTest003, TestSize.Level1)
110 {
111     instance_->interfaces_.clear();
112     std::string interfaceName = "wlan0";
113     instance_->isDefault_ = true;
114     auto ret = instance_->AddInterface(interfaceName);
115     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
116 }
117 
118 HWTEST_F(PhysicalNetworkTest, AddInterfaceTest004, TestSize.Level1)
119 {
120     instance_->interfaces_.clear();
121     std::string interfaceName = "wlan0";
122     instance_->isDefault_ = false;
123     auto ret = instance_->AddInterface(interfaceName);
124     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
125 }
126 
127 HWTEST_F(PhysicalNetworkTest, RemoveInterfaceTest001, TestSize.Level1)
128 {
129     std::string interfaceName = "test";
130     auto ret = instance_->RemoveInterface(interfaceName);
131     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
132     interfaceName = "wlan0";
133     ret = instance_->RemoveInterface(interfaceName);
134     EXPECT_TRUE(ret == NETMANAGER_ERROR || ret == NETMANAGER_SUCCESS);
135     interfaceName = "wlan1";
136     ret = instance_->RemoveInterface(interfaceName);
137     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
138 }
139 
140 HWTEST_F(PhysicalNetworkTest, RemoveInterfaceTest002, TestSize.Level1)
141 {
142     instance_->interfaces_.clear();
143     std::string interfaceName = "wlan0";
144     instance_->isDefault_ = true;
145     auto ret = instance_->AddInterface(interfaceName);
146     ret = instance_->RemoveInterface(interfaceName);
147     EXPECT_TRUE(ret == NETMANAGER_ERROR || ret == NETMANAGER_SUCCESS);
148 }
149 
150 HWTEST_F(PhysicalNetworkTest, RemoveInterfaceTest003, TestSize.Level1)
151 {
152     instance_->interfaces_.clear();
153     std::string interfaceName = "wlan0";
154     instance_->isDefault_ = false;
155     auto ret = instance_->AddInterface(interfaceName);
156     ret = instance_->RemoveInterface(interfaceName);
157     EXPECT_TRUE(ret == NETMANAGER_ERROR || ret == NETMANAGER_SUCCESS);
158 }
159 
160 HWTEST_F(PhysicalNetworkTest, RemoveInterfaceTest004, TestSize.Level1)
161 {
162     instance_->interfaces_.clear();
163     std::string interfaceName = "wlan0";
164     instance_->isDefault_ = false;
165     auto ret = instance_->RemoveInterface(interfaceName);
166     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
167 }
168 
169 HWTEST_F(PhysicalNetworkTest, AddDefaultTest001, TestSize.Level1)
170 {
171     instance_->AddDefault();
172     EXPECT_TRUE(instance_->isDefault_);
173     instance_->RemoveDefault();
174     EXPECT_FALSE(instance_->isDefault_);
175 }
176 
177 HWTEST_F(PhysicalNetworkTest, RemoveDefaultTest001, TestSize.Level1)
178 {
179     instance_->RemoveDefault();
180     EXPECT_FALSE(instance_->isDefault_);
181 }
182 } // namespace NetsysNative
183 } // namespace OHOS
184