Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TESTS_DAEMON_FACE_TEST_NETIF_IP_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_TEST_NETIF_IP_HPP |
| 28 | |
| 29 | #include "core/common.hpp" |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 30 | |
| 31 | #include <boost/asio/ip/address.hpp> |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/net/network-address.hpp> |
| 33 | #include <ndn-cxx/net/network-interface.hpp> |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace tests { |
| 37 | |
| 38 | using ndn::net::AddressFamily; |
| 39 | using ndn::net::NetworkAddress; |
| 40 | using ndn::net::NetworkInterface; |
| 41 | |
| 42 | // ---- network interface ---- |
| 43 | |
| 44 | /** \brief Collect information about network interfaces |
| 45 | * \param allowCached if true, previously collected information can be returned |
| 46 | * \note This function is blocking if \p allowCached is false or no previous information exists |
| 47 | * \throw ndn::net::NetworkMonitor::Error NetworkMonitor::CAP_ENUM is unavailable |
| 48 | */ |
| 49 | std::vector<shared_ptr<const NetworkInterface>> |
| 50 | collectNetworkInterfaces(bool allowCached = true); |
| 51 | |
| 52 | template<AddressFamily AF> |
| 53 | bool |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 54 | hasAddressFamily(const NetworkInterface& netif) |
| 55 | { |
| 56 | return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(), |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 57 | [] (const NetworkAddress& a) { return a.getFamily() == AF; }); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // ---- IP address ---- |
| 61 | |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 62 | enum class AddressScope { |
| 63 | Loopback = static_cast<int>(ndn::net::AddressScope::HOST), |
| 64 | LinkLocal = static_cast<int>(ndn::net::AddressScope::LINK), |
| 65 | Global = static_cast<int>(ndn::net::AddressScope::GLOBAL), |
| 66 | Unspecified |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | enum class MulticastInterface { |
| 70 | No, |
| 71 | Yes, |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 72 | Unspecified |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** \brief Derives IP address type from AddressFamily |
| 76 | */ |
| 77 | template<AddressFamily AF> |
| 78 | struct IpAddressFromFamily; |
| 79 | |
| 80 | template<> |
| 81 | struct IpAddressFromFamily<AddressFamily::V4> |
| 82 | { |
| 83 | using type = boost::asio::ip::address_v4; |
| 84 | }; |
| 85 | |
| 86 | template<> |
| 87 | struct IpAddressFromFamily<AddressFamily::V6> |
| 88 | { |
| 89 | using type = boost::asio::ip::address_v6; |
| 90 | }; |
| 91 | |
| 92 | /** \brief Get an IP address from any available network interface |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 93 | * \param family the desired address family |
| 94 | * \param scope the desired address scope |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 95 | * \param mcast specifies if the address can, must, or must not be chosen from a multicast-capable interface |
| 96 | * \return an IP address, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6 |
| 97 | * \retval unspecified address, if no appropriate address is available |
| 98 | */ |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 99 | boost::asio::ip::address |
| 100 | getTestIp(AddressFamily family = AddressFamily::UNSPECIFIED, |
| 101 | AddressScope scope = AddressScope::Unspecified, |
| 102 | MulticastInterface mcast = MulticastInterface::Unspecified); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 103 | |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 104 | /** \brief Skip the rest of the test case if \p address is unavailable |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 105 | * |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 106 | * This macro can be used in conjunction with nfd::tests::getTestIp in a test case. Example: |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 107 | * \code |
| 108 | * BOOST_AUTO_TEST_CASE(TestCase) |
| 109 | * { |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 110 | * auto ip = getTestIp(AddressFamily::V4); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 111 | * SKIP_IF_IP_UNAVAILABLE(ip); |
| 112 | * // Test something that requires an IPv4 address. |
| 113 | * } |
| 114 | * \endcode |
| 115 | */ |
| 116 | #define SKIP_IF_IP_UNAVAILABLE(address) \ |
| 117 | do { \ |
| 118 | if ((address).is_unspecified()) { \ |
| 119 | BOOST_WARN_MESSAGE(false, "skipping assertions that require a valid IP address"); \ |
| 120 | return; \ |
| 121 | } \ |
| 122 | } while (false) |
| 123 | |
| 124 | } // namespace tests |
| 125 | } // namespace nfd |
| 126 | |
| 127 | #endif // NFD_TESTS_DAEMON_FACE_TEST_NETIF_IP_HPP |