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