Line data Source code
1 : /* test_addr_interfaces.cpp
2 : * Copyright (c) 2011-2018 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 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 : // self
41 : //
42 : #include "test_addr_main.h"
43 :
44 : // addr lib
45 : //
46 : #include "libaddr/iface.h"
47 :
48 : // C lib
49 : //
50 : #include <net/if.h>
51 :
52 :
53 3 : TEST_CASE( "ipv4::interfaces", "[ipv4]" )
54 : {
55 2 : GIVEN("iface::get_local_addresses()")
56 : {
57 2 : addr::iface::vector_t list(addr::iface::get_local_addresses());
58 :
59 2 : SECTION("verify list")
60 : {
61 1 : REQUIRE_FALSE(list.empty()); // at least "lo"
62 :
63 : // add stuff like verify there is an "lo" entry?
64 11 : for(auto i : list)
65 : {
66 10 : REQUIRE_FALSE(i.get_name().empty());
67 10 : REQUIRE(i.get_flags() != 0);
68 :
69 10 : switch(i.get_address().get_network_type())
70 : {
71 : case addr::addr::network_type_t::NETWORK_TYPE_PRIVATE:
72 : case addr::addr::network_type_t::NETWORK_TYPE_PUBLIC:
73 : case addr::addr::network_type_t::NETWORK_TYPE_LOOPBACK:
74 : case addr::addr::network_type_t::NETWORK_TYPE_LINK_LOCAL:
75 10 : break;
76 :
77 : default:
78 0 : std::cerr << "unexpected interface type " << static_cast<int>(i.get_address().get_network_type()) << "\n";
79 0 : REQUIRE_FALSE("unexpected network type for interface");
80 0 : break;
81 :
82 : }
83 :
84 10 : REQUIRE(i.has_broadcast_address() == ((i.get_flags() & IFF_BROADCAST) != 0));
85 10 : REQUIRE(i.has_destination_address() == ((i.get_flags() & IFF_POINTOPOINT) != 0));
86 :
87 10 : addr::addr const & b(i.get_broadcast_address());
88 10 : if(!i.has_broadcast_address())
89 : {
90 2 : REQUIRE(b.is_default());
91 : }
92 :
93 10 : addr::addr const & d(i.get_destination_address());
94 10 : if(!i.has_destination_address())
95 : {
96 10 : REQUIRE(d.is_default());
97 : }
98 : }
99 : }
100 : }
101 7 : }
102 :
103 :
104 : // vim: ts=4 sw=4 et
|