Skip to content

Commit 6ff8591

Browse files
committed
test: added router unit test
1 parent e4a81b2 commit 6ff8591

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ set(TEST_SOURCES
5959
${TEST}/net/unit/tcp_packet_test.cpp
6060
${TEST}/net/unit/tcp_socket.cpp
6161
${TEST}/net/unit/tcp_write_queue.cpp
62+
${TEST}/net/unit/router.cpp
6263
${TEST}/posix/unit/fd_map_test.cpp
6364
${TEST}/posix/unit/inet_test.cpp
6465
${TEST}/util/unit/base64.cpp

test/net/unit/router.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#include <common.cxx>
19+
#include <net/router.hpp>
20+
21+
#include <net/ip4/ip4.hpp>
22+
#include <net/inet.hpp>
23+
24+
using namespace net;
25+
26+
// We need discrete inet pointers for the tests
27+
Inet<IP4>* eth1 = (Inet<IP4>*) 1;
28+
Inet<IP4>* eth2 = (Inet<IP4>*) 2;
29+
Inet<IP4>* eth3 = (Inet<IP4>*) 3;
30+
Inet<IP4>* eth4 = (Inet<IP4>*) 4;
31+
32+
33+
CASE("Creating and matching against routes")
34+
{
35+
36+
Route<IP4> r1{{10, 42, 42, 0 }, { 255, 255, 255, 0}, {10, 42, 42, 2}, *eth1 , 1 };
37+
38+
EXPECT(r1.netmask() == (IP4::addr{255, 255, 255, 0}));
39+
EXPECT(r1.net() == (IP4::addr{10, 42, 42, 0}));
40+
EXPECT(r1.gateway() == (IP4::addr{10, 42, 42, 2}));
41+
EXPECT(r1.cost() == 1);
42+
EXPECT(r1.interface() == eth1);
43+
44+
45+
EXPECT(r1.match({10, 42, 42, 3}));
46+
EXPECT(not r1.match({10, 42, 43, 3}));
47+
EXPECT(not r1.match({255, 255, 255, 255}));
48+
49+
// Routes are copy-constructible etc.
50+
Route<IP4> r2 = r1;
51+
EXPECT(r2.interface() == r1.interface());
52+
EXPECT(r2 == r1);
53+
54+
}
55+
56+
57+
CASE("Creating and using a router and routing table")
58+
{
59+
60+
Router<IP4>::Routing_table tbl{
61+
{{10, 42, 42, 0 }, { 255, 255, 255, 0}, {10, 42, 42, 2}, *eth1 , 2 },
62+
{{10, 42, 0, 0 }, { 255, 255, 0, 0}, {10, 42, 42, 3}, *eth2 , 2 },
63+
{{50, 42, 0, 0 }, { 255, 255, 0, 0}, {50, 42, 42, 2}, *eth3 , 3 },
64+
{{160, 60, 0, 0 }, { 255, 255, 0, 0}, {50, 42, 42, 2}, *eth4 , 4 },
65+
{{160, 60, 0, 0 }, { 255, 255, 0, 0}, {50, 42, 42, 2}, *eth1 , 4 },
66+
{{160, 60, 0, 0 }, { 255, 255, 0, 0}, {50, 42, 42, 2}, *eth2 , 4 }
67+
};
68+
69+
70+
// Routing tables can be sorted, filtered, (transformed) etc.
71+
Router<IP4>::Routing_table sorted = tbl;
72+
std::sort(sorted.begin(), sorted.end());
73+
74+
Router<IP4>::Routing_table filtered{};
75+
std::copy_if(tbl.begin(), tbl.end(), std::back_inserter(filtered), [](const Route<IP4>& r){ return r.match({10,42,42,3}) != nullptr; });
76+
77+
78+
EXPECT(sorted.size() == tbl.size());
79+
EXPECT(filtered.size() == 2u);
80+
81+
// Provide a (empty) set of interfaces
82+
std::vector<std::unique_ptr<Inet<net::IP4>>> ifaces{};
83+
84+
// Construct router object
85+
Router<IP4> router(ifaces, tbl);
86+
87+
// Check for route to a couple of IP's
88+
EXPECT(router.route_check({10,42,42,5}));
89+
EXPECT(not router.route_check({100,43,43,5}));
90+
91+
// Get all routes for an IP
92+
auto routes = router.get_all_routes({10,42,42,10});
93+
EXPECT(routes.size() == 2u);
94+
EXPECT((routes.front().gateway() == IP4::addr{10,42,42,2} or routes.front().gateway() == IP4::addr{10,42,42,3}));
95+
96+
// Get the cheapest route for an IP
97+
auto route = router.get_cheapest_route({10,42,42,10});
98+
EXPECT((route->gateway() == IP4::addr{10,42,42,2} and route->interface() == eth1));
99+
100+
// Change the routing table
101+
router.set_routing_table( {{{10, 42, 43, 0 }, { 255, 255, 255, 0}, {10, 42, 42, 2}, *eth1 , 2 }} );
102+
103+
// Now there's no cheapest route
104+
EXPECT(not router.get_cheapest_route({10,42,42,10}));
105+
106+
107+
108+
}

0 commit comments

Comments
 (0)