2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2019 Cloudius Systems, Ltd.
22 #include <seastar/testing/test_case.hh>
23 #include <seastar/net/api.hh>
24 #include <seastar/net/inet_address.hh>
25 #include <seastar/net/ethernet.hh>
26 #include <seastar/net/ip.hh>
27 #include <seastar/core/reactor.hh>
28 #include <seastar/core/thread.hh>
29 #include <seastar/util/log.hh>
31 using namespace seastar
;
33 static logger
niflog("network_interface_test");
35 static_assert(std::is_nothrow_default_constructible_v
<net::ethernet_address
>);
36 static_assert(std::is_nothrow_copy_constructible_v
<net::ethernet_address
>);
37 static_assert(std::is_nothrow_move_constructible_v
<net::ethernet_address
>);
39 SEASTAR_TEST_CASE(list_interfaces
) {
40 // just verifying we have something. And can access all the stuff.
41 auto interfaces
= engine().net().network_interfaces();
42 BOOST_REQUIRE_GT(interfaces
.size(), 0);
44 for (auto& nif
: interfaces
) {
45 niflog
.info("Iface: {}, index = {}, mtu = {}, loopback = {}, virtual = {}, up = {}",
46 nif
.name(), nif
.index(), nif
.mtu(), nif
.is_loopback(), nif
.is_virtual(), nif
.is_up()
48 if (nif
.hardware_address().size() >= 6) {
49 niflog
.info(" HW: {}", net::ethernet_address(nif
.hardware_address().data()));
51 for (auto& addr
: nif
.addresses()) {
52 niflog
.info(" Addr: {}", addr
);
56 return make_ready_future();
59 SEASTAR_TEST_CASE(match_ipv6_scope
) {
60 auto interfaces
= engine().net().network_interfaces();
62 for (auto& nif
: interfaces
) {
63 if (nif
.is_loopback()) {
66 auto i
= std::find_if(nif
.addresses().begin(), nif
.addresses().end(), std::mem_fn(&net::inet_address::is_ipv6
));
67 if (i
== nif
.addresses().end()) {
71 std::ostringstream ss
;
72 ss
<< net::inet_address(i
->as_ipv6_address()) << "%" << nif
.name();
75 net::inet_address
na(text
);
77 BOOST_REQUIRE_EQUAL(na
.as_ipv6_address(), i
->as_ipv6_address());
78 // also verify that the inet_address itself matches
79 BOOST_REQUIRE_EQUAL(na
, *i
);
80 // and that inet_address _without_ scope matches.
81 BOOST_REQUIRE_EQUAL(net::inet_address(na
.as_ipv6_address()), *i
);
82 BOOST_REQUIRE_EQUAL(na
.scope(), nif
.index());
83 // and that they are not ipv4 addresses
84 BOOST_REQUIRE_THROW(i
->as_ipv4_address(), std::invalid_argument
);
85 BOOST_REQUIRE_THROW(na
.as_ipv4_address(), std::invalid_argument
);
87 niflog
.info("Org: {}, Parsed: {}, Text: {}", *i
, na
, text
);
91 return make_ready_future();
94 SEASTAR_TEST_CASE(is_standard_addresses_sanity
) {
95 BOOST_REQUIRE_EQUAL(net::inet_address("127.0.0.1").is_loopback(), true);
96 BOOST_REQUIRE_EQUAL(net::inet_address("127.0.0.11").is_loopback(), true);
97 BOOST_REQUIRE_EQUAL(net::inet_address(::in_addr
{INADDR_ANY
}).is_addr_any(), true);
98 auto addr
= net::inet_address("1.2.3.4");
99 BOOST_REQUIRE_EQUAL(addr
.is_loopback(), false);
100 BOOST_REQUIRE_EQUAL(addr
.is_addr_any(), false);
102 BOOST_REQUIRE_EQUAL(net::inet_address("::1").is_loopback(), true);
103 BOOST_REQUIRE_EQUAL(net::inet_address(::in6addr_any
).is_addr_any(), true);
104 auto addr6
= net::inet_address("acf1:f5e5:5a99:337f:ebe2:c57e:0e27:69c6");
105 BOOST_REQUIRE_EQUAL(addr6
.is_loopback(), false);
106 BOOST_REQUIRE_EQUAL(addr6
.is_addr_any(), false);
108 return make_ready_future
<>();