• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <sys/socket.h>
17 
18 #include <gtest/gtest.h>
19 
20 #include "net_conn_types.h"
21 #include "net_manager_constants.h"
22 #ifdef GTEST_API_
23 #define private public
24 #endif
25 #include "route_utils.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 constexpr uint32_t INVALID_VALUE = 50;
32 
GetRoute()33 Route GetRoute()
34 {
35     std::string iface("eth0");
36     Route route;
37     route.iface_ = iface;
38     route.rtnType_ = RTN_UNICAST;
39     route.hasGateway_ = true;
40     route.isDefaultRoute_ = false;
41     route.destination_.type_ = INetAddr::IPV4;
42     route.destination_.family_ = AF_INET;
43     route.destination_.prefixlen_ = 0x18;
44     route.destination_.address_ = "192.168.2.10";
45     route.destination_.netMask_ = "255.255.255.0";
46     route.destination_.hostName_ = "netAddr";
47     route.gateway_.type_ = INetAddr::IPV4;
48     route.gateway_.family_ = AF_INET;
49     route.gateway_.prefixlen_ = 0x18;
50     route.gateway_.address_ = "192.168.2.1";
51     route.gateway_.netMask_ = "255.255.255.0";
52     route.gateway_.hostName_ = "netAddr";
53     return route;
54 }
55 
GetRoute2()56 Route GetRoute2()
57 {
58     std::string iface("eth0");
59     Route route;
60     route.iface_ = iface;
61     route.rtnType_ = RTN_UNREACHABLE;
62     route.hasGateway_ = false;
63     route.isDefaultRoute_ = false;
64     route.destination_.type_ = INetAddr::IPV4;
65     route.destination_.family_ = AF_INET;
66     route.destination_.prefixlen_ = 0x18;
67     route.destination_.address_ = "192.168.2.10";
68     route.destination_.netMask_ = "255.255.255.0";
69     route.destination_.hostName_ = "netAddr";
70     route.gateway_.type_ = INetAddr::IPV4;
71     route.gateway_.family_ = AF_INET;
72     route.gateway_.prefixlen_ = 0x18;
73     route.gateway_.address_ = "192.168.2.1";
74     route.gateway_.netMask_ = "255.255.255.0";
75     route.gateway_.hostName_ = "netAddr";
76     return route;
77 }
78 
GetRoute3()79 Route GetRoute3()
80 {
81     std::string iface("eth0");
82     Route route;
83     route.iface_ = iface;
84     route.rtnType_ = RTN_UNICAST;
85     route.hasGateway_ = false;
86     route.isDefaultRoute_ = false;
87     route.destination_.type_ = INetAddr::IPV4;
88     route.destination_.family_ = AF_INET;
89     route.destination_.prefixlen_ = 0x18;
90     route.destination_.address_ = "192.168.2.10";
91     route.destination_.netMask_ = "255.255.255.0";
92     route.destination_.hostName_ = "netAddr";
93     route.gateway_.type_ = INetAddr::IPV4;
94     route.gateway_.family_ = AF_INET;
95     route.gateway_.prefixlen_ = 0x18;
96     route.gateway_.address_ = "192.168.2.1";
97     route.gateway_.netMask_ = "255.255.255.0";
98     route.gateway_.hostName_ = "netAddr";
99     return route;
100 }
101 
GetRoute4()102 Route GetRoute4()
103 {
104     std::string iface("eth0");
105     Route route;
106     route.iface_ = iface;
107     route.rtnType_ = RTN_UNICAST;
108     route.hasGateway_ = false;
109     route.isDefaultRoute_ = false;
110     route.destination_.type_ = INetAddr::IPV4;
111     route.destination_.family_ = AF_INET;
112     route.destination_.prefixlen_ = 0x18;
113     route.destination_.address_ = "192168210";
114     route.destination_.netMask_ = "255.255.255.0";
115     route.destination_.hostName_ = "netAddr";
116     route.gateway_.type_ = INetAddr::IPV4;
117     route.gateway_.family_ = AF_INET;
118     route.gateway_.prefixlen_ = 0x18;
119     route.gateway_.address_ = "192.168.2.1";
120     route.gateway_.netMask_ = "255.255.255.0";
121     route.gateway_.hostName_ = "netAddr";
122     return route;
123 }
124 
125 constexpr uint32_t TEST_NETID = 110;
126 } // namespace
127 
128 class RouteUtilsTest : public testing::Test {
129 public:
130     static void SetUpTestCase();
131     static void TearDownTestCase();
132     void SetUp();
133     void TearDown();
134 };
135 
SetUpTestCase()136 void RouteUtilsTest::SetUpTestCase() {}
137 
TearDownTestCase()138 void RouteUtilsTest::TearDownTestCase() {}
139 
SetUp()140 void RouteUtilsTest::SetUp() {}
141 
TearDown()142 void RouteUtilsTest::TearDown() {}
143 
144 HWTEST_F(RouteUtilsTest, AddRouteToLocal01, TestSize.Level1)
145 {
146     std::list<Route> rList;
147     std::string iface("eth0");
148     rList.push_back(GetRoute());
149     RouteUtils::AddRoutesToLocal(iface, rList);
150     EXPECT_FALSE(rList.empty());
151 }
152 
153 HWTEST_F(RouteUtilsTest, RemoveRouteFromLocal01, TestSize.Level1)
154 {
155     std::list<Route> rList;
156     rList.push_back(GetRoute());
157 
158     EXPECT_EQ(0, RouteUtils::RemoveRoutesFromLocal(rList));
159 }
160 
161 HWTEST_F(RouteUtilsTest, AddRouteToLocal02, TestSize.Level1)
162 {
163     std::list<Route> rList;
164     std::string iface("eth0");
165     Route route;
166     route.iface_ = iface;
167     route.rtnType_ = RTN_UNICAST;
168     route.hasGateway_ = true;
169     route.isDefaultRoute_ = false;
170     route.destination_.type_ = INetAddr::IPV4;
171     route.destination_.family_ = AF_INET;
172     route.destination_.prefixlen_ = 0;
173     route.destination_.address_ = "192.168.2.10";
174     route.destination_.netMask_ = "255.255.255.0";
175     route.destination_.hostName_ = "netAddr";
176     route.gateway_.type_ = INetAddr::IPV4;
177     route.gateway_.family_ = AF_INET;
178     route.gateway_.prefixlen_ = 0x18;
179     route.gateway_.address_ = "192.168.2.1";
180     route.gateway_.netMask_ = "255.255.255.0";
181     route.gateway_.hostName_ = "netAddr";
182     rList.push_back(route);
183     int32_t ret = RouteUtils::AddRoutesToLocal(iface, rList);
184     EXPECT_NE(ret, 0);
185 }
186 
187 HWTEST_F(RouteUtilsTest, AddRouteToLocal03, TestSize.Level1)
188 {
189     std::list<Route> rList;
190     std::string iface("eth0");
191     Route route;
192     route.iface_ = iface;
193     route.rtnType_ = RTN_UNREACHABLE;
194     route.hasGateway_ = false;
195     route.isDefaultRoute_ = false;
196     route.destination_.type_ = INetAddr::IPV4;
197     route.destination_.family_ = AF_INET;
198     route.destination_.prefixlen_ = 0x18;
199     route.destination_.address_ = "192.168.2.10";
200     route.destination_.netMask_ = "255.255.255.0";
201     route.destination_.hostName_ = "netAddr";
202     rList.push_back(route);
203     int32_t ret = RouteUtils::AddRoutesToLocal(iface, rList);
204     EXPECT_NE(ret, 0);
205 }
206 
207 HWTEST_F(RouteUtilsTest, AddRouteToLocal04, TestSize.Level1)
208 {
209     std::list<Route> rList;
210     std::string iface("eth0");
211     Route route;
212     route.iface_ = iface;
213     route.rtnType_ = RTN_THROW;
214     route.hasGateway_ = false;
215     route.isDefaultRoute_ = false;
216     route.destination_.type_ = INetAddr::IPV4;
217     route.destination_.family_ = AF_INET;
218     route.destination_.prefixlen_ = 0x18;
219     route.destination_.address_ = "192.168.2.10";
220     route.destination_.netMask_ = "255.255.255.0";
221     route.destination_.hostName_ = "netAddr";
222     rList.push_back(route);
223     int32_t ret = RouteUtils::AddRoutesToLocal(iface, rList);
224     EXPECT_NE(ret, 0);
225 }
226 
227 HWTEST_F(RouteUtilsTest, AddRoute01, TestSize.Level1)
228 {
229     EXPECT_GE(0, RouteUtils::AddRoute(TEST_NETID, GetRoute3()));
230 }
231 
232 HWTEST_F(RouteUtilsTest, AddRoute02, TestSize.Level1)
233 {
234     EXPECT_GE(0, RouteUtils::AddRoute(TEST_NETID, GetRoute4()));
235 }
236 
237 HWTEST_F(RouteUtilsTest, AddRoute03, TestSize.Level1)
238 {
239     EXPECT_GE(0, RouteUtils::AddRoute(TEST_NETID, GetRoute()));
240 }
241 
242 HWTEST_F(RouteUtilsTest, RemoveRoute01, TestSize.Level1)
243 {
244     EXPECT_GE(0, RouteUtils::RemoveRoute(TEST_NETID, GetRoute()));
245 }
246 
247 HWTEST_F(RouteUtilsTest, UpdateRoutes01, TestSize.Level1)
248 {
249     NetLinkInfo nlio;
250     NetLinkInfo nlin;
251     nlio.routeList_.push_back(GetRoute());
252     nlin.routeList_.push_back(GetRoute2());
253 
254     EXPECT_TRUE(RouteUtils::UpdateRoutes(TEST_NETID, nlin, nlio));
255 }
256 
257 HWTEST_F(RouteUtilsTest, UpdateRoutes02, TestSize.Level1)
258 {
259     NetLinkInfo nlio;
260     NetLinkInfo nlin;
261     nlio.routeList_.push_back(GetRoute());
262 
263     EXPECT_TRUE(RouteUtils::UpdateRoutes(TEST_NETID, nlin, nlio));
264 }
265 
266 HWTEST_F(RouteUtilsTest, RouteUtilsBranchTest001, TestSize.Level1)
267 {
268     Route route = {};
269     route.rtnType_ = INVALID_VALUE;
270     routeOperateType op = static_cast<routeOperateType>(INVALID_VALUE);
271     int32_t netId = 100;
272     int32_t ret = RouteUtils::ModifyRoute(op, netId, route);
273     EXPECT_EQ(ret, NETMANAGER_ERROR);
274     int32_t prefixLen = 0;
275     std::string str = RouteUtils::MaskAddress("", prefixLen);
276     EXPECT_EQ(str, "");
277 }
278 } // namespace NetManagerStandard
279 } // namespace OHOS
280