blob: dc076d738fce65a346c110402745b7594787494e [file] [log] [blame]
Junxiao Shi84d62cb2017-07-12 16:15:18 +00001/* -*- 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"
30#include <type_traits>
31#include <boost/asio/ip/address_v4.hpp>
32#include <boost/asio/ip/address_v6.hpp>
33#include <ndn-cxx/net/network-address.hpp>
34#include <ndn-cxx/net/network-interface.hpp>
35
36namespace nfd {
37namespace tests {
38
39using ndn::net::AddressFamily;
40using ndn::net::NetworkAddress;
41using ndn::net::NetworkInterface;
42
43// ---- network interface ----
44
45/** \brief Collect information about network interfaces
46 * \param allowCached if true, previously collected information can be returned
47 * \note This function is blocking if \p allowCached is false or no previous information exists
48 * \throw ndn::net::NetworkMonitor::Error NetworkMonitor::CAP_ENUM is unavailable
49 */
50std::vector<shared_ptr<const NetworkInterface>>
51collectNetworkInterfaces(bool allowCached = true);
52
53template<AddressFamily AF>
54bool
55isAddressFamily(const NetworkAddress& a)
56{
57 return a.getFamily() == AF;
58}
59
60template<AddressFamily AF>
61bool
62hasAddressFamily(const NetworkInterface& netif)
63{
64 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
65 &isAddressFamily<AF>);
66}
67
68// ---- IP address ----
69
70enum class LoopbackAddress {
71 No,
72 Yes,
73 DontCare,
74 Default = Yes
75};
76
77enum class MulticastInterface {
78 No,
79 Yes,
80 DontCare,
81 Default = DontCare
82};
83
84/** \brief Derives IP address type from AddressFamily
85 */
86template<AddressFamily AF>
87struct IpAddressFromFamily;
88
89template<>
90struct IpAddressFromFamily<AddressFamily::V4>
91{
92 using type = boost::asio::ip::address_v4;
93};
94
95template<>
96struct IpAddressFromFamily<AddressFamily::V6>
97{
98 using type = boost::asio::ip::address_v6;
99};
100
101/** \brief Get an IP address from any available network interface
102 * \tparam F address family, either AddressFamily::V4 or AddressFamily::V6
103 * \param loopback specifies if the address can, must, or must not be a loopback address
104 * \param mcast specifies if the address can, must, or must not be chosen from a multicast-capable interface
105 * \return an IP address, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6
106 * \retval unspecified address, if no appropriate address is available
107 */
108template<AddressFamily F>
109typename IpAddressFromFamily<F>::type
110getTestIp(LoopbackAddress loopback = LoopbackAddress::Default,
111 MulticastInterface mcast = MulticastInterface::Default);
112
113extern template
114IpAddressFromFamily<AddressFamily::V4>::type
115getTestIp<AddressFamily::V4>(LoopbackAddress loopback, MulticastInterface mcast);
116
117extern template
118IpAddressFromFamily<AddressFamily::V6>::type
119getTestIp<AddressFamily::V6>(LoopbackAddress loopback, MulticastInterface mcast);
120
121/** \brief Skip rest of the test case if \p address is unavailable
122 *
123 * This macro can be used in conjunction with \p nfd::tests::getTestIp in a test case. Example:
124 * \code
125 * BOOST_AUTO_TEST_CASE(TestCase)
126 * {
127 * auto ip = getTestIp<AddressFamily::V4>();
128 * SKIP_IF_IP_UNAVAILABLE(ip);
129 * // Test something that requires an IPv4 address.
130 * }
131 * \endcode
132 */
133#define SKIP_IF_IP_UNAVAILABLE(address) \
134 do { \
135 if ((address).is_unspecified()) { \
136 BOOST_WARN_MESSAGE(false, "skipping assertions that require a valid IP address"); \
137 return; \
138 } \
139 } while (false)
140
141} // namespace tests
142} // namespace nfd
143
144#endif // NFD_TESTS_DAEMON_FACE_TEST_NETIF_IP_HPP