Line data Source code
1 : // Copyright (c) 2011-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // Project: https://snapwebsites.org/project/libaddr
4 : //
5 : // Permission is hereby granted, free of charge, to any
6 : // person obtaining a copy of this software and
7 : // associated documentation files (the "Software"), to
8 : // deal in the Software without restriction, including
9 : // without limitation the rights to use, copy, modify,
10 : // merge, publish, distribute, sublicense, and/or sell
11 : // copies of the Software, and to permit persons to whom
12 : // the Software is furnished to do so, subject to the
13 : // following conditions:
14 : //
15 : // The above copyright notice and this permission notice
16 : // shall be included in all copies or substantial
17 : // portions of the Software.
18 : //
19 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
20 : // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
21 : // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22 : // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
23 : // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 : // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 : // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 : // ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 : // SOFTWARE.
29 :
30 :
31 : /** \file
32 : * \brief Verify the interfaces function.
33 : *
34 : * This file implements a test that verifies the function that
35 : * reads the list of IP addresses as defined in your local
36 : * interfaces.
37 : */
38 :
39 : // self
40 : //
41 : #include "catch_main.h"
42 :
43 :
44 : // addr lib
45 : //
46 : #include <libaddr/iface.h>
47 :
48 :
49 : // C lib
50 : //
51 : #include <net/if.h>
52 :
53 :
54 : // last include
55 : //
56 : #include <snapdev/poison.h>
57 :
58 :
59 :
60 3 : CATCH_TEST_CASE( "ipv4::interfaces", "[ipv4]" )
61 : {
62 2 : CATCH_GIVEN("iface::get_local_addresses()")
63 : {
64 2 : addr::iface::vector_t list(addr::iface::get_local_addresses());
65 :
66 2 : CATCH_SECTION("verify list")
67 : {
68 1 : CATCH_REQUIRE_FALSE(list.empty()); // at least "lo"
69 :
70 : // add stuff like verify there is an "lo" entry?
71 20 : for(auto i : list)
72 : {
73 19 : CATCH_REQUIRE_FALSE(i.get_name().empty());
74 19 : CATCH_REQUIRE(i.get_flags() != 0);
75 :
76 19 : switch(i.get_address().get_network_type())
77 : {
78 19 : case addr::addr::network_type_t::NETWORK_TYPE_PRIVATE:
79 : case addr::addr::network_type_t::NETWORK_TYPE_PUBLIC:
80 : case addr::addr::network_type_t::NETWORK_TYPE_LOOPBACK:
81 : case addr::addr::network_type_t::NETWORK_TYPE_LINK_LOCAL:
82 19 : break;
83 :
84 0 : default:
85 0 : std::cerr << "unexpected interface type " << static_cast<int>(i.get_address().get_network_type()) << "\n";
86 0 : CATCH_REQUIRE_FALSE("unexpected network type for interface");
87 0 : break;
88 :
89 : }
90 :
91 19 : CATCH_REQUIRE(i.has_broadcast_address() == ((i.get_flags() & IFF_BROADCAST) != 0));
92 19 : CATCH_REQUIRE(i.has_destination_address() == ((i.get_flags() & IFF_POINTOPOINT) != 0));
93 :
94 19 : addr::addr const & b(i.get_broadcast_address());
95 19 : if(!i.has_broadcast_address())
96 : {
97 2 : CATCH_REQUIRE(b.is_default());
98 : }
99 :
100 19 : addr::addr const & d(i.get_destination_address());
101 19 : if(!i.has_destination_address())
102 : {
103 19 : CATCH_REQUIRE(d.is_default());
104 : }
105 : }
106 : }
107 : }
108 7 : }
109 :
110 :
111 : // vim: ts=4 sw=4 et
|