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 :
32 : /** \file
33 : * \brief Verify the interfaces function.
34 : *
35 : * This file implements a test that verifies the function that
36 : * reads the list of IP addresses as defined in your local
37 : * interfaces.
38 : */
39 :
40 : // addr
41 : //
42 : #include <libaddr/iface.h>
43 :
44 :
45 : // self
46 : //
47 : #include "catch_main.h"
48 :
49 :
50 : // C
51 : //
52 : #include <net/if.h>
53 :
54 :
55 : // last include
56 : //
57 : #include <snapdev/poison.h>
58 :
59 :
60 :
61 1 : CATCH_TEST_CASE( "iface::interfaces", "[iface]" )
62 : {
63 1 : CATCH_GIVEN("iface::get_local_addresses()")
64 : {
65 1 : addr::iface::set_local_addresses_cache_ttl(1500);
66 :
67 1 : addr::iface::pointer_vector_t list(addr::iface::get_local_addresses());
68 1 : addr::iface_index_name::vector_t name_index(addr::get_interface_name_index());
69 :
70 1 : CATCH_START_SECTION("iface::interfaces: verify list")
71 : {
72 1 : CATCH_REQUIRE_FALSE(list->empty()); // at least "lo"
73 :
74 : // add stuff like verify there is an "lo" entry?
75 19 : for(auto i : *list)
76 : {
77 18 : CATCH_REQUIRE_FALSE(i->get_name().empty());
78 18 : CATCH_REQUIRE(i->get_flags() != 0);
79 :
80 18 : switch(i->get_address().get_network_type())
81 : {
82 18 : case addr::network_type_t::NETWORK_TYPE_PRIVATE:
83 : case addr::network_type_t::NETWORK_TYPE_PUBLIC:
84 : case addr::network_type_t::NETWORK_TYPE_LOOPBACK:
85 : case addr::network_type_t::NETWORK_TYPE_LINK_LOCAL:
86 18 : break;
87 :
88 0 : default:
89 : //std::cerr << "unexpected interface type " << static_cast<int>(i->get_address().get_network_type()) << "\n";
90 0 : CATCH_REQUIRE(i->get_address().get_network_type() == addr::network_type_t::NETWORK_TYPE_PUBLIC);
91 0 : break;
92 :
93 : }
94 :
95 18 : CATCH_REQUIRE(i->has_broadcast_address() == ((i->get_flags() & IFF_BROADCAST) != 0));
96 18 : CATCH_REQUIRE(i->has_destination_address() == ((i->get_flags() & IFF_POINTOPOINT) != 0));
97 :
98 18 : addr::addr const & b(i->get_broadcast_address());
99 18 : if(i->has_broadcast_address())
100 : {
101 16 : if(b.is_ipv4())
102 : {
103 9 : CATCH_REQUIRE(addr::is_broadcast_address(b));
104 : }
105 : else
106 : {
107 : // IPv6 is not offering broadcast IPs so we always
108 : // get the default IP here
109 : //
110 7 : CATCH_REQUIRE(b.is_default());
111 7 : CATCH_REQUIRE_FALSE(addr::is_broadcast_address(b));
112 : }
113 : }
114 : else
115 : {
116 2 : CATCH_REQUIRE(b.is_default());
117 : }
118 :
119 18 : addr::addr const & d(i->get_destination_address());
120 18 : if(!i->has_destination_address())
121 : {
122 18 : CATCH_REQUIRE(d.is_default());
123 : }
124 :
125 18 : unsigned int const index(addr::get_interface_index_by_name(i->get_name()));
126 90 : for(auto & ni : name_index)
127 : {
128 72 : if(ni.get_name() == i->get_name())
129 : {
130 16 : CATCH_REQUIRE(ni.get_index() == index);
131 : }
132 : }
133 18 : }
134 :
135 1 : addr::iface::reset_local_addresses_cache();
136 : }
137 1 : CATCH_END_SECTION()
138 2 : }
139 1 : }
140 :
141 :
142 : // vim: ts=4 sw=4 et
|