OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_route.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
3#include <iostream>
4#include <set>
5
7
9
10using namespace openvpn;
11
12void test_route_parse(const std::string &rstr, const std::string &expected, bool ipv6)
13{
14 const IP::Route r(rstr);
15 ASSERT_EQ(r.to_string(), expected);
16
17 if (ipv6)
18 {
19 const IP::Route6 r6(rstr);
20 ASSERT_EQ(r6.to_string(), expected);
21 }
22 else
23 {
24 const IP::Route4 r4(rstr);
25 ASSERT_EQ(r4.to_string(), expected);
26 }
27}
28
29TEST(IPAddr, routeSet)
30{
31 std::set<IP::Route> routes;
32 routes.emplace("1.2.3.4/24");
33 routes.emplace("1.2.3.0/24");
34 routes.emplace("1.2.3.2/24");
35 routes.emplace("1.2.3.1/24");
36 routes.emplace("128.0.0.0/1");
37 routes.emplace("1:2:3:4:5:6:dead:beef/64");
38 routes.emplace("1:2:3:4:5:6:dead:bead/64");
39
40 std::stringstream out;
41 for (const auto &r : routes)
42 out << r.to_string() << std::endl;
43
44 ASSERT_EQ("128.0.0.0/1\n"
45 "1.2.3.0/24\n"
46 "1.2.3.1/24\n"
47 "1.2.3.2/24\n"
48 "1.2.3.4/24\n"
49 "1:2:3:4:5:6:dead:bead/64\n"
50 "1:2:3:4:5:6:dead:beef/64\n",
51 out.str());
52}
53
54template <typename LIST>
55void test_split(const LIST &rtlist, const std::string &expected)
56{
57 typedef typename LIST::value_type RT;
58 std::stringstream out;
59 for (const auto &r : rtlist)
60 {
61 RT r1, r2;
62 if (r.is_canonical() && r.split(r1, r2))
63 {
64 out << r << ' ' << r1 << ' ' << r2 << std::endl;
65 }
66 }
67 ASSERT_EQ(expected, out.str());
68}
69
70TEST(IPAddr, routeList4)
71{
72 IP::Route4List routes;
73 routes.emplace_back("1.2.3.4/24");
74 routes.emplace_back("1.2.3.0/24");
75 routes.emplace_back("1.2.3.2/24");
76 routes.emplace_back("1.2.3.1/24");
77 routes.emplace_back("128.0.0.0/1");
78 // OPENVPN_LOG_NTNL(routes.to_string());
79 ASSERT_FALSE(routes.contains(IPv4::Addr::from_string("100.1.2.3")));
80 ASSERT_TRUE(routes.contains(IPv4::Addr::from_string("200.1.2.3")));
81
82 test_split(routes, "1.2.3.0/24 1.2.3.0/25 1.2.3.128/25\n128.0.0.0/1 128.0.0.0/2 192.0.0.0/2\n");
83}
84
85TEST(IPAddr, routeList6)
86{
87 IP::Route6List routes;
88 routes.emplace_back("1:2:3:4:5:6:dead:beef/64");
89 routes.emplace_back("cafe:babe::/64");
90 // OPENVPN_LOG_NTNL(routes.to_string());
91 ASSERT_FALSE(routes.contains(IPv6::Addr::from_string("1111:2222:3333:4444:5555:6666:7777:8888")));
92 ASSERT_TRUE(routes.contains(IPv6::Addr::from_string("cafe:babe:0:0:1111:2222:3333:4444")));
93 test_split(routes, "cafe:babe::/64 cafe:babe::/65 cafe:babe:0:0:8000::/65\n");
94}
95
96TEST(IPAddr, routeList)
97{
98 IP::RouteList routes;
99 routes.emplace_back("1.2.3.4/24");
100 routes.emplace_back("1.2.3.0/24");
101 routes.emplace_back("1.2.3.2/24");
102 routes.emplace_back("1.2.3.1/24");
103 routes.emplace_back("128.0.0.0/1");
104 routes.emplace_back("1:2:3:4:5:6:dead:beef/64");
105 routes.emplace_back("cafe:babe::/64");
106 // OPENVPN_LOG_NTNL(routes.to_string());
107 ASSERT_FALSE(routes.contains(IP::Addr::from_string("100.1.2.3")));
108 ASSERT_TRUE(routes.contains(IP::Addr::from_string("200.1.2.3")));
109 ASSERT_FALSE(routes.contains(IP::Addr::from_string("1111:2222:3333:4444:5555:6666:7777:8888")));
110 ASSERT_TRUE(routes.contains(IP::Addr::from_string("cafe:babe:0:0:1111:2222:3333:4444")));
111
112 test_split(routes, "1.2.3.0/24 1.2.3.0/25 1.2.3.128/25\n"
113 "128.0.0.0/1 128.0.0.0/2 192.0.0.0/2\n"
114 "cafe:babe::/64 cafe:babe::/65 cafe:babe:0:0:8000::/65\n");
115}
116
117TEST(IPAddr, parseRoutes)
118{
119 test_route_parse("1.2.3.4", "1.2.3.4/32", false);
120 test_route_parse("192.168.4.0/24", "192.168.4.0/24", false);
121 test_route_parse("fe80::6470:7dff:fea5:f360/64", "fe80::6470:7dff:fea5:f360/64", true);
122
123 ASSERT_THROW(
124 IP::Route("192.168.4.0/33"),
125 std::exception);
126}
static Addr from_string(const std::string &ipstr, const TITLE &title, const Version required_version)
Definition ip.hpp:105
std::string to_string() const
Definition route.hpp:237
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv4.hpp:205
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv6.hpp:104
bool contains(const R &c) const
Definition route.hpp:348
remote_address ipv6
const std::string expected
static std::stringstream out
Definition test_path.cpp:10
void test_route_parse(const std::string &rstr, const std::string &expected, bool ipv6)
TEST(IPAddr, routeSet)
void test_split(const LIST &rtlist, const std::string &expected)