blob: f77c541a4b0b58747cb58f9a85e15822ba955f9a [file] [log] [blame]
Yanbiao Liac8b4ca2017-07-10 02:27:50 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento59e266a2018-05-27 05:20:20 -04003 * Copyright (c) 2013-2018 Regents of the University of California,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -07004 * 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
Yanbiao Liac8b4ca2017-07-10 02:27:50 -070046BOOST_AUTO_TEST_CASE(AddressFromString)
47{
48 boost::asio::ip::address addr;
49 boost::system::error_code ec;
50
51 // empty string
52 BOOST_CHECK_THROW(addressFromString(""), boost::system::system_error);
53 BOOST_CHECK_EQUAL(addressFromString("", ec), addr);
54 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
55
56 // IPv4 address
57 BOOST_CHECK_EQUAL(addressFromString("192.168.0.1", ec),
58 boost::asio::ip::address::from_string("192.168.0.1"));
59 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
60
61 BOOST_CHECK_THROW(addressFromString("192.168.0"), boost::system::system_error);
62 BOOST_CHECK_EQUAL(addressFromString("192.168.0", ec), addr);
63 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
64
65 BOOST_CHECK_THROW(addressFromString("192.168.0.1%"), boost::system::system_error);
66 BOOST_CHECK_EQUAL(addressFromString("192.168.0.1%", ec), addr);
67 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
68
69 // regular IPv6 address
70 BOOST_CHECK_EQUAL(addressFromString("2001:db8::1", ec),
71 boost::asio::ip::address::from_string("2001:db8::1"));
72 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
73
74 BOOST_CHECK_THROW(addressFromString("2001:db8:::"), boost::system::system_error);
75 BOOST_CHECK_EQUAL(addressFromString("2001:db8:::", ec), addr);
76 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
77
78 // link-local IPv6 address
79 const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
80 if (!networkInterfaces.empty()) {
81 const auto& netif = networkInterfaces.front();
82 CHECK_IPV6_ADDRESS(addressFromString("fe80::1%" + netif->getName(), ec).to_v6(),
83 "fe80::1", netif->getIndex());
84 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
85 }
86}
87
88BOOST_AUTO_TEST_CASE(AddressV6FromString)
89{
90 boost::asio::ip::address_v6 addr;
91 boost::system::error_code ec;
92
93 // empty string
94 BOOST_CHECK_THROW(addressV6FromString(""), boost::system::system_error);
95 BOOST_CHECK_EQUAL(addressV6FromString("", ec), addr);
96 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
97
98 // IPv4 address
99 BOOST_CHECK_THROW(addressV6FromString("192.168.0.1"), boost::system::system_error);
100 BOOST_CHECK_EQUAL(addressV6FromString("192.168.0.1", ec), addr);
101 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
102
103 // regular IPv6 addresses
104 BOOST_CHECK_EQUAL(addressV6FromString("2001:db8::1", ec),
105 boost::asio::ip::address_v6::from_string("2001:db8::1", ec));
106 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
107
108 BOOST_CHECK_THROW(addressV6FromString("2001:db8:::"), boost::system::system_error);
109 BOOST_CHECK_EQUAL(addressV6FromString("2001:db8:::", ec), addr);
110 BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
111
112
113 const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
114 if (!networkInterfaces.empty()) {
115 const auto& netif = networkInterfaces.front();
116 auto index = netif->getIndex();
117
118 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + netif->getName(), ec), "fe80::1", index);
119 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
120
121 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(index), ec), "fe80::1", index);
122 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
123 }
124
125 int invalidIndex = 0;
126 for (const auto& netif : networkInterfaces) {
127 invalidIndex += netif->getIndex();
128 }
129
130 // an invalid interface name will lead to a default scope id (i.e. 0) which means no scope
131 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%NotAnInterface", ec), "fe80::1", 0);
132 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
133
134 // supplying an interface index in the string won't trigger any checks on its validity
135 CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(invalidIndex), ec),
136 "fe80::1", invalidIndex);
137 BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
138}
139
140BOOST_AUTO_TEST_SUITE_END() // TestAddressConverter
141BOOST_AUTO_TEST_SUITE_END() // Net
142
143} // namespace tests
144} // namespace ip
145} // namespace ndn