blob: 1b8604e0d3697d27faf1742a5f4a00da506f8483 [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
Davide Pesavento22fba352017-10-17 15:53:51 -040026#ifndef NFD_TESTS_DAEMON_FACE_TEST_IP_HPP
27#define NFD_TESTS_DAEMON_FACE_TEST_IP_HPP
Junxiao Shi84d62cb2017-07-12 16:15:18 +000028
29#include "core/common.hpp"
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040030
31#include <boost/asio/ip/address.hpp>
Junxiao Shi84d62cb2017-07-12 16:15:18 +000032#include <ndn-cxx/net/network-address.hpp>
Junxiao Shi84d62cb2017-07-12 16:15:18 +000033
34namespace nfd {
Davide Pesavento22fba352017-10-17 15:53:51 -040035namespace face {
Junxiao Shi84d62cb2017-07-12 16:15:18 +000036namespace tests {
37
Davide Pesavento22fba352017-10-17 15:53:51 -040038enum class AddressFamily {
39 V4 = static_cast<int>(ndn::net::AddressFamily::V4),
40 V6 = static_cast<int>(ndn::net::AddressFamily::V6),
41 Any
42};
Junxiao Shi84d62cb2017-07-12 16:15:18 +000043
Davide Pesavento22fba352017-10-17 15:53:51 -040044std::ostream&
45operator<<(std::ostream& os, AddressFamily family);
Junxiao Shi84d62cb2017-07-12 16:15:18 +000046
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040047enum class AddressScope {
48 Loopback = static_cast<int>(ndn::net::AddressScope::HOST),
49 LinkLocal = static_cast<int>(ndn::net::AddressScope::LINK),
50 Global = static_cast<int>(ndn::net::AddressScope::GLOBAL),
Davide Pesavento22fba352017-10-17 15:53:51 -040051 Any
Junxiao Shi84d62cb2017-07-12 16:15:18 +000052};
53
Davide Pesavento22fba352017-10-17 15:53:51 -040054std::ostream&
55operator<<(std::ostream& os, AddressScope scope);
56
Junxiao Shi84d62cb2017-07-12 16:15:18 +000057enum class MulticastInterface {
58 No,
59 Yes,
Davide Pesavento22fba352017-10-17 15:53:51 -040060 Any
Junxiao Shi84d62cb2017-07-12 16:15:18 +000061};
62
Davide Pesavento22fba352017-10-17 15:53:51 -040063std::ostream&
64operator<<(std::ostream& os, MulticastInterface mcast);
65
Junxiao Shi84d62cb2017-07-12 16:15:18 +000066/** \brief Derives IP address type from AddressFamily
67 */
68template<AddressFamily AF>
69struct IpAddressFromFamily;
70
71template<>
72struct IpAddressFromFamily<AddressFamily::V4>
73{
74 using type = boost::asio::ip::address_v4;
75};
76
77template<>
78struct IpAddressFromFamily<AddressFamily::V6>
79{
80 using type = boost::asio::ip::address_v6;
81};
82
83/** \brief Get an IP address from any available network interface
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040084 * \param family the desired address family
85 * \param scope the desired address scope
Junxiao Shi84d62cb2017-07-12 16:15:18 +000086 * \param mcast specifies if the address can, must, or must not be chosen from a multicast-capable interface
87 * \return an IP address, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6
88 * \retval unspecified address, if no appropriate address is available
89 */
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040090boost::asio::ip::address
Davide Pesavento22fba352017-10-17 15:53:51 -040091getTestIp(AddressFamily family = AddressFamily::Any,
92 AddressScope scope = AddressScope::Any,
93 MulticastInterface mcast = MulticastInterface::Any);
Junxiao Shi84d62cb2017-07-12 16:15:18 +000094
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040095/** \brief Skip the rest of the test case if \p address is unavailable
Junxiao Shi84d62cb2017-07-12 16:15:18 +000096 *
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040097 * This macro can be used in conjunction with nfd::tests::getTestIp in a test case. Example:
Junxiao Shi84d62cb2017-07-12 16:15:18 +000098 * \code
99 * BOOST_AUTO_TEST_CASE(TestCase)
100 * {
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400101 * auto ip = getTestIp(AddressFamily::V4);
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000102 * SKIP_IF_IP_UNAVAILABLE(ip);
103 * // Test something that requires an IPv4 address.
104 * }
105 * \endcode
106 */
107#define SKIP_IF_IP_UNAVAILABLE(address) \
108 do { \
109 if ((address).is_unspecified()) { \
110 BOOST_WARN_MESSAGE(false, "skipping assertions that require a valid IP address"); \
111 return; \
112 } \
113 } while (false)
114
115} // namespace tests
Davide Pesavento22fba352017-10-17 15:53:51 -0400116} // namespace face
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000117} // namespace nfd
118
Davide Pesavento22fba352017-10-17 15:53:51 -0400119#endif // NFD_TESTS_DAEMON_FACE_TEST_IP_HPP