Line data Source code
1 : // Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libaddr
4 : // contact@m2osw.com
5 : //
6 : // Permission is hereby granted, free of charge, to any
7 : // person obtaining a copy of this software and
8 : // associated documentation files (the "Software"), to
9 : // deal in the Software without restriction, including
10 : // without limitation the rights to use, copy, modify,
11 : // merge, publish, distribute, sublicense, and/or sell
12 : // copies of the Software, and to permit persons to whom
13 : // the Software is furnished to do so, subject to the
14 : // following conditions:
15 : //
16 : // The above copyright notice and this permission notice
17 : // shall be included in all copies or substantial
18 : // portions of the Software.
19 : //
20 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
21 : // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 : // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23 : // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
24 : // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 : // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 : // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 : // ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 : // SOFTWARE.
30 :
31 : /** \file
32 : * \brief Verify the route class a little further.
33 : *
34 : * This file implements a test that verifies a few additional things
35 : * in the route table.
36 : */
37 :
38 : // libaddr
39 : //
40 : #include <libaddr/route.h>
41 :
42 :
43 : // self
44 : //
45 : #include "catch_main.h"
46 :
47 :
48 :
49 : // C
50 : //
51 : #include <net/if.h>
52 : #include <net/route.h>
53 :
54 :
55 : // last include
56 : //
57 : #include <snapdev/poison.h>
58 :
59 :
60 :
61 2 : CATCH_TEST_CASE("ipv4::routes", "[ipv4]")
62 : {
63 2 : CATCH_GIVEN("route::get_ipv4_routes()")
64 : {
65 2 : addr::route::vector_t routes(addr::route::get_ipv4_routes());
66 2 : addr::route::vector_t routes_without_default;
67 :
68 2 : CATCH_START_SECTION("routes: verify list")
69 : {
70 1 : CATCH_REQUIRE_FALSE(routes.empty()); // at least the default route
71 :
72 : // check a few things
73 : //
74 1 : bool found_default(false);
75 1 : bool found_gateway(false);
76 : //std::map<std::string, bool> found;
77 11 : for(auto r : routes)
78 : {
79 10 : CATCH_REQUIRE_FALSE(r->get_interface_name().empty());
80 10 : CATCH_REQUIRE(r->get_interface_name().length() < IFNAMSIZ); // IFNAMSIZ includes the '\0' so '<' and not '<='
81 :
82 : //if(found[r->get_interface_name()])
83 : //{
84 : // std::cerr
85 : // << "WARNING: found interface \""
86 : // << r->get_interface_name()
87 : // << "\" twice.\n";
88 : // continue;
89 : //}
90 : //found[r->get_interface_name()] = true;
91 :
92 : // at least one flag is not zero
93 10 : int const f(r->get_flags());
94 10 : std::string const flags(r->flags_to_string());
95 :
96 10 : CATCH_REQUIRE(f != 0);
97 10 : CATCH_REQUIRE(!flags.empty());
98 :
99 : #if 0
100 : // output similar to `route`
101 : std::cout << "Route: Dest: " << r->get_destination_address().to_ipv4or6_string(addr::STRING_IP_ADDRESS)
102 : << " Gateway: " << r->get_gateway_address().to_ipv4or6_string(addr::STRING_IP_ADDRESS)
103 : << " Flags: " << r->get_flags()
104 : << " Metric: " << r->get_metric()
105 : << " Iface: " << r->get_interface_name()
106 : << "\n";
107 : #endif
108 10 : if(r->get_destination_address().is_default())
109 : {
110 1 : CATCH_REQUIRE_FALSE(found_default);
111 1 : found_default = true;
112 :
113 1 : CATCH_REQUIRE((f & RTF_UP) != 0);
114 :
115 1 : if(!r->get_gateway_address().is_default())
116 : {
117 1 : CATCH_REQUIRE_FALSE(found_gateway);
118 1 : found_gateway = true;
119 : }
120 : }
121 : else
122 : {
123 9 : routes_without_default.push_back(r);
124 : }
125 :
126 10 : if(!r->get_gateway_address().is_default())
127 : {
128 1 : CATCH_REQUIRE((f & RTF_GATEWAY) != 0);
129 : }
130 :
131 : // Not much I can test on the following?
132 : //r->get_flags()
133 10 : CATCH_REQUIRE(r->get_reference_count() >= 0);
134 10 : CATCH_REQUIRE(r->get_use() >= 0);
135 10 : CATCH_REQUIRE(r->get_metric() >= 0);
136 10 : CATCH_REQUIRE(r->get_mtu() >= 0);
137 10 : CATCH_REQUIRE(r->get_window() >= 0);
138 10 : CATCH_REQUIRE(r->get_irtt() >= 0);
139 10 : }
140 1 : CATCH_REQUIRE(found_default);
141 1 : CATCH_REQUIRE(found_gateway);
142 : }
143 2 : CATCH_END_SECTION()
144 :
145 2 : CATCH_START_SECTION("routes: verify a search without a default route")
146 : {
147 1 : CATCH_REQUIRE(find_default_route(routes_without_default) == nullptr);
148 : }
149 2 : CATCH_END_SECTION()
150 4 : }
151 2 : }
152 :
153 :
154 : // vim: ts=4 sw=4 et
|