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