Line data Source code
1 : /* test_addr_routes.cpp
2 : * Copyright (c) 2011-2021 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * Project: https://snapwebsites.org/project/libaddr
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 :
32 : /** \file
33 : * \brief Verify the route class a little further.
34 : *
35 : * This file implements a test that verifies a few additional things
36 : * in the route table.
37 : */
38 :
39 : // self
40 : //
41 : #include "catch_main.h"
42 :
43 :
44 :
45 : // addr lib
46 : //
47 : #include "libaddr/route.h"
48 :
49 :
50 : // C lib
51 : //
52 : #include <net/if.h>
53 : #include <net/route.h>
54 :
55 :
56 : // last include
57 : //
58 : #include <snapdev/poison.h>
59 :
60 :
61 :
62 4 : CATCH_TEST_CASE("ipv4::routes", "[ipv4]")
63 : {
64 4 : CATCH_GIVEN("route::get_ipv4_routes()")
65 : {
66 4 : addr::route::vector_t routes(addr::route::get_ipv4_routes());
67 4 : addr::route::vector_t routes_without_default;
68 :
69 4 : CATCH_SECTION("verify list")
70 : {
71 1 : CATCH_REQUIRE_FALSE(routes.empty()); // at least the default route
72 :
73 : // check a few things
74 : //
75 1 : bool found_default(false);
76 1 : bool found_gateway(false);
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 : // at least one flag is not zero
83 10 : int const f(r->get_flags());
84 20 : std::string const flags(r->flags_to_string());
85 :
86 10 : CATCH_REQUIRE(f != 0);
87 10 : CATCH_REQUIRE(!flags.empty());
88 :
89 10 : if(r->get_destination_address().is_default())
90 : {
91 1 : CATCH_REQUIRE_FALSE(found_default);
92 1 : found_default = true;
93 :
94 1 : CATCH_REQUIRE((f & RTF_UP) != 0);
95 : }
96 : else
97 : {
98 9 : routes_without_default.push_back(r);
99 : }
100 :
101 10 : if(!r->get_gateway_address().is_default())
102 : {
103 1 : CATCH_REQUIRE_FALSE(found_gateway);
104 1 : found_gateway = true;
105 :
106 1 : CATCH_REQUIRE((f & RTF_GATEWAY) != 0);
107 : }
108 :
109 : // Not much I can test on the following?
110 : //r->get_flags()
111 10 : CATCH_REQUIRE(r->get_reference_count() >= 0);
112 10 : CATCH_REQUIRE(r->get_use() >= 0);
113 10 : CATCH_REQUIRE(r->get_metric() >= 0);
114 10 : CATCH_REQUIRE(r->get_mtu() >= 0);
115 10 : CATCH_REQUIRE(r->get_window() >= 0);
116 10 : CATCH_REQUIRE(r->get_irtt() >= 0);
117 : }
118 1 : CATCH_REQUIRE(found_default);
119 1 : CATCH_REQUIRE(found_gateway);
120 : }
121 :
122 4 : CATCH_SECTION("verify a search without a default route")
123 : {
124 1 : CATCH_REQUIRE(find_default_route(routes_without_default) == nullptr);
125 : }
126 : }
127 8 : }
128 :
129 :
130 : // vim: ts=4 sw=4 et
|