blob: 712bf6dac8d6339dac8a781d1f941dc2c2505ea0 [file] [log] [blame]
Yanbiao Liac8b4ca2017-07-10 02:27:50 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-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 ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12 *
13 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later version.
16 *
17 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 *
21 * You should have received copies of the GNU General Public License and GNU Lesser
22 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23 * <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26 */
27
28#include "net/address-converter.hpp"
29
30#include "boost-test.hpp"
31#include "collect-netifs.hpp"
32
33namespace ndn {
34namespace ip {
35namespace tests {
36
37BOOST_AUTO_TEST_SUITE(Net)
38BOOST_AUTO_TEST_SUITE(TestAddressConverter)
39
40#define CHECK_IPV6_ADDRESS(address, string, scope) do { \
41 auto addrV6 = boost::asio::ip::address_v6::from_string(string); \
42 addrV6.scope_id(scope); \
43 BOOST_CHECK_EQUAL(address, addrV6); \
44} while (false)
45
46BOOST_AUTO_TEST_CASE(ScopeNameFromId)
47{
48 const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
49 if (!networkInterfaces.empty()) {
50 const auto& netif = networkInterfaces.front();
51 auto index = netif->getIndex();
52 auto name = netif->getName();
53
54 BOOST_CHECK_EQUAL(scopeNameFromId(index).value(), name);
55 }
56
57 BOOST_CHECK(!scopeNameFromId(std::numeric_limits<unsigned int>::max()));
58}
59
60BOOST_AUTO_TEST_CASE(AddressFromString)
61{
62 boost::asio::ip::address addr;
63 boost::system::error_code ec;
64
65 // empty string
66 BOOST_CHECK_THROW(addressFromString(""), boost::system::system_error);
67 BOOST_CHECK_EQUAL(addressFromString("", ec), addr);
68 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
69
70 // IPv4 address
71 BOOST_CHECK_EQUAL(addressFromString("192.168.0.1", ec),
72 boost::asio::ip::address::from_string("192.168.0.1"));
73 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
74
75 BOOST_CHECK_THROW(addressFromString("192.168.0"), boost::system::system_error);
76 BOOST_CHECK_EQUAL(addressFromString("192.168.0", ec), addr);
77 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
78
79 BOOST_CHECK_THROW(addressFromString("192.168.0.1%"), boost::system::system_error);
80 BOOST_CHECK_EQUAL(addressFromString("192.168.0.1%", ec), addr);
81 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
82
83 // regular IPv6 address
84 BOOST_CHECK_EQUAL(addressFromString("2001:db8::1", ec),
85 boost::asio::ip::address::from_string("2001:db8::1"));
86 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
87
88 BOOST_CHECK_THROW(addressFromString("2001:db8:::"), boost::system::system_error);
89 BOOST_CHECK_EQUAL(addressFromString("2001:db8:::", ec), addr);
90 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
91
92 // link-local IPv6 address
93 const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
94 if (!networkInterfaces.empty()) {
95 const auto& netif = networkInterfaces.front();
96 CHECK_IPV6_ADDRESS(addressFromString("fe80::1%" + netif->getName(), ec).to_v6(),
97 "fe80::1", netif->getIndex());
98 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
99 }
100}
101
102BOOST_AUTO_TEST_CASE(AddressV6FromString)
103{
104 boost::asio::ip::address_v6 addr;
105 boost::system::error_code ec;
106
107 // empty string
108 BOOST_CHECK_THROW(addressV6FromString(""), boost::system::system_error);
109 BOOST_CHECK_EQUAL(addressV6FromString("", ec), addr);
110 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
111
112 // IPv4 address
113 BOOST_CHECK_THROW(addressV6FromString("192.168.0.1"), boost::system::system_error);
114 BOOST_CHECK_EQUAL(addressV6FromString("192.168.0.1", ec), addr);
115 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
116
117 // regular IPv6 addresses
118 BOOST_CHECK_EQUAL(addressV6FromString("2001:db8::1", ec),
119 boost::asio::ip::address_v6::from_string("2001:db8::1", ec));
120 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
121
122 BOOST_CHECK_THROW(addressV6FromString("2001:db8:::"), boost::system::system_error);
123 BOOST_CHECK_EQUAL(addressV6FromString("2001:db8:::", ec), addr);
124 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
125
126
127 const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
128 if (!networkInterfaces.empty()) {
129 const auto& netif = networkInterfaces.front();
130 auto index = netif->getIndex();
131
132 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + netif->getName(), ec), "fe80::1", index);
133 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
134
135 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(index), ec), "fe80::1", index);
136 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
137 }
138
139 int invalidIndex = 0;
140 for (const auto& netif : networkInterfaces) {
141 invalidIndex += netif->getIndex();
142 }
143
144 // an invalid interface name will lead to a default scope id (i.e. 0) which means no scope
145 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%NotAnInterface", ec), "fe80::1", 0);
146 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
147
148 // supplying an interface index in the string won't trigger any checks on its validity
149 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(invalidIndex), ec),
150 "fe80::1", invalidIndex);
151 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
152}
153
154BOOST_AUTO_TEST_SUITE_END() // TestAddressConverter
155BOOST_AUTO_TEST_SUITE_END() // Net
156
157} // namespace tests
158} // namespace ip
159} // namespace ndn