Line data Source code
1 : // Copyright (c) 2011-2023 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 1 : std::map<std::string, bool> found; 77 14 : for(auto r : routes) 78 : { 79 13 : CATCH_REQUIRE_FALSE(r->get_interface_name().empty()); 80 13 : CATCH_REQUIRE(r->get_interface_name().length() < IFNAMSIZ); // IFNAMSIZ includes the '\0' so '<' and not '<=' 81 : 82 13 : if(found[r->get_interface_name()]) 83 : { 84 : std::cerr 85 : << "WARNING: found interface \"" 86 10 : << r->get_interface_name() 87 20 : << "\" twice.\n"; 88 10 : continue; 89 : } 90 3 : found[r->get_interface_name()] = true; 91 : 92 : // at least one flag is not zero 93 3 : int const f(r->get_flags()); 94 3 : std::string const flags(r->flags_to_string()); 95 : 96 3 : CATCH_REQUIRE(f != 0); 97 3 : CATCH_REQUIRE(!flags.empty()); 98 : 99 3 : if(r->get_destination_address().is_default()) 100 : { 101 1 : CATCH_REQUIRE_FALSE(found_default); 102 1 : found_default = true; 103 : 104 1 : CATCH_REQUIRE((f & RTF_UP) != 0); 105 : } 106 : else 107 : { 108 2 : routes_without_default.push_back(r); 109 : } 110 : 111 3 : if(!r->get_gateway_address().is_default()) 112 : { 113 1 : std::cerr << "got a gateway [" << r->get_interface_name() << "]\n"; 114 1 : CATCH_REQUIRE_FALSE(found_gateway); 115 1 : found_gateway = true; 116 : 117 1 : CATCH_REQUIRE((f & RTF_GATEWAY) != 0); 118 : } 119 : 120 : // Not much I can test on the following? 121 : //r->get_flags() 122 3 : CATCH_REQUIRE(r->get_reference_count() >= 0); 123 3 : CATCH_REQUIRE(r->get_use() >= 0); 124 3 : CATCH_REQUIRE(r->get_metric() >= 0); 125 3 : CATCH_REQUIRE(r->get_mtu() >= 0); 126 3 : CATCH_REQUIRE(r->get_window() >= 0); 127 3 : CATCH_REQUIRE(r->get_irtt() >= 0); 128 13 : } 129 1 : CATCH_REQUIRE(found_default); 130 1 : CATCH_REQUIRE(found_gateway); 131 1 : } 132 2 : CATCH_END_SECTION() 133 : 134 2 : CATCH_START_SECTION("routes: verify a search without a default route") 135 : { 136 1 : CATCH_REQUIRE(find_default_route(routes_without_default) == nullptr); 137 : } 138 2 : CATCH_END_SECTION() 139 4 : } 140 2 : } 141 : 142 : 143 : // vim: ts=4 sw=4 et