Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 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 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_FACE_TEST_IP_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_TEST_IP_HPP |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 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> |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 33 | #include <ndn-cxx/net/network-interface.hpp> |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::tests { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 37 | enum class AddressFamily { |
| 38 | V4 = static_cast<int>(ndn::net::AddressFamily::V4), |
| 39 | V6 = static_cast<int>(ndn::net::AddressFamily::V6), |
| 40 | Any |
| 41 | }; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 42 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 43 | std::ostream& |
| 44 | operator<<(std::ostream& os, AddressFamily family); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 45 | |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 46 | enum class AddressScope { |
| 47 | Loopback = static_cast<int>(ndn::net::AddressScope::HOST), |
| 48 | LinkLocal = static_cast<int>(ndn::net::AddressScope::LINK), |
| 49 | Global = static_cast<int>(ndn::net::AddressScope::GLOBAL), |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 50 | Any |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 53 | std::ostream& |
| 54 | operator<<(std::ostream& os, AddressScope scope); |
| 55 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 56 | enum class MulticastInterface { |
| 57 | No, |
| 58 | Yes, |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 59 | Any |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 62 | std::ostream& |
| 63 | operator<<(std::ostream& os, MulticastInterface mcast); |
| 64 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 65 | template<AddressFamily AF> |
| 66 | struct IpAddressFromFamily; |
| 67 | |
| 68 | template<> |
| 69 | struct IpAddressFromFamily<AddressFamily::V4> |
| 70 | { |
| 71 | using type = boost::asio::ip::address_v4; |
| 72 | }; |
| 73 | |
| 74 | template<> |
| 75 | struct IpAddressFromFamily<AddressFamily::V6> |
| 76 | { |
| 77 | using type = boost::asio::ip::address_v6; |
| 78 | }; |
| 79 | |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 80 | /** |
| 81 | * \brief Derives IP address type from AddressFamily |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 82 | */ |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 83 | template<AddressFamily AF> |
| 84 | using IpAddressTypeFromFamily = typename IpAddressFromFamily<AF>::type; |
| 85 | |
| 86 | /** |
| 87 | * \brief Get an IP address from any available network interface, satisfying the given requirements |
| 88 | * \param family the desired address family |
| 89 | * \param scope the desired address scope |
| 90 | * \param mcast specifies if the address can, must, or must not be chosen from a multicast-capable interface |
| 91 | * \return an IP address, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6 |
| 92 | * \retval unspecified address, if no appropriate address is available |
| 93 | */ |
| 94 | [[nodiscard]] boost::asio::ip::address |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 95 | getTestIp(AddressFamily family = AddressFamily::Any, |
| 96 | AddressScope scope = AddressScope::Any, |
| 97 | MulticastInterface mcast = MulticastInterface::Any); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 98 | |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 99 | /** |
| 100 | * \brief Get a network interface and an IP address on it that satisfy the given requirements |
| 101 | * \param family the desired address family |
| 102 | * \param scope the desired address scope |
| 103 | * \param mcast specifies if the address can, must, or must not be chosen from a multicast-capable interface |
| 104 | */ |
| 105 | [[nodiscard]] std::tuple<shared_ptr<const ndn::net::NetworkInterface>, boost::asio::ip::address> |
| 106 | getTestInterfaceAndIp(AddressFamily family = AddressFamily::Any, |
| 107 | AddressScope scope = AddressScope::Any, |
| 108 | MulticastInterface mcast = MulticastInterface::Any); |
| 109 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 110 | } // namespace nfd::tests |
| 111 | |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 112 | /** \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] | 113 | * |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 114 | * 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] | 115 | * \code |
| 116 | * BOOST_AUTO_TEST_CASE(TestCase) |
| 117 | * { |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 118 | * auto ip = getTestIp(AddressFamily::V4); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 119 | * SKIP_IF_IP_UNAVAILABLE(ip); |
| 120 | * // Test something that requires an IPv4 address. |
| 121 | * } |
| 122 | * \endcode |
| 123 | */ |
| 124 | #define SKIP_IF_IP_UNAVAILABLE(address) \ |
| 125 | do { \ |
| 126 | if ((address).is_unspecified()) { \ |
| 127 | BOOST_WARN_MESSAGE(false, "skipping assertions that require a valid IP address"); \ |
| 128 | return; \ |
| 129 | } \ |
| 130 | } while (false) |
| 131 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 132 | #endif // NFD_TESTS_DAEMON_FACE_TEST_IP_HPP |