blob: aa2c499ed28cf21dd91a45373dabfe98f331e37b [file] [log] [blame]
Alexander Afanasyev689f0e92014-11-09 12:09:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -04002/*
Davide Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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.
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080010 *
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#include "core/network.hpp"
27
28#include "tests/test-common.hpp"
29
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040030#include <boost/lexical_cast.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080033
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040034namespace ip = boost::asio::ip;
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080035
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040036BOOST_AUTO_TEST_SUITE(TestNetwork)
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080037
38BOOST_AUTO_TEST_CASE(Empty)
39{
40 Network n;
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040041 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.1")), false);
42 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080043
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040044 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("0.0.0.1")), false);
45 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("255.255.255.255")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080046
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040047 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("::")), false);
48 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080049}
50
51BOOST_AUTO_TEST_CASE(MaxRangeV4)
52{
53 Network n = Network::getMaxRangeV4();
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040054 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.1")), true);
55 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080056
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040057 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("0.0.0.1")), true);
58 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("255.255.255.255")), true);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080059
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040060 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("::")), false);
61 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080062}
63
64BOOST_AUTO_TEST_CASE(RangeV4)
65{
66 Network n = boost::lexical_cast<Network>("192.0.2.0/24");
67 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), "192.0.2.0 <-> 192.0.2.255");
68
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040069 BOOST_CHECK_THROW(boost::lexical_cast<Network>("192.0.2.0/255"), boost::bad_lexical_cast);
70 BOOST_CHECK_THROW(boost::lexical_cast<Network>("256.0.2.0/24"), boost::bad_lexical_cast);
71
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040072 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.1")), true);
73 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.254")), true);
74 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.1.255")), false);
75 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.3.0")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080076
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040077 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080078
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040079 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("0.0.0.1")), false);
80 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("255.255.255.255")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080081
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040082 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("::")), false);
83 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080084}
85
86BOOST_AUTO_TEST_CASE(MaxRangeV6)
87{
88 Network n = Network::getMaxRangeV6();
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040089 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.1")), false);
90 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), true);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080091
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040092 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("0.0.0.1")), false);
93 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("255.255.255.255")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080094
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040095 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("::")), true);
96 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), true);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080097}
98
99BOOST_AUTO_TEST_CASE(RangeV6)
100{
101 Network n = boost::lexical_cast<Network>("2001:db8:3f9:1::/64");
102 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n),
103 "2001:db8:3f9:1:: <-> 2001:db8:3f9:1:ffff:ffff:ffff:ffff");
104
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400105 BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9:1::/129"), boost::bad_lexical_cast);
106 BOOST_CHECK_THROW(boost::lexical_cast<Network>("200x:db8:3f9:1::/64"), boost::bad_lexical_cast);
107 BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9::1::/64"), boost::bad_lexical_cast);
108
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400109 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("192.0.2.1")), false);
110 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), true);
111 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1::")), true);
112 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:1:ffff:ffff:ffff:ffff")), true);
113 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:0:ffff:ffff:ffff:ffff")), false);
114 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("2001:db8:3f9:2::")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800115
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400116 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("0.0.0.1")), false);
117 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("255.255.255.255")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800118
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400119 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("::")), false);
120 BOOST_CHECK_EQUAL(n.doesContain(ip::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), false);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800121}
122
123BOOST_AUTO_TEST_CASE(Comparisons)
124{
125 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("192.0.2.0/24"),
126 boost::lexical_cast<Network>("192.0.2.127/24"));
127
128 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
129 boost::lexical_cast<Network>("2001:db8:3f9:0:ffff::/64"));
130
131 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
132 boost::lexical_cast<Network>("192.0.3.127/24"));
133
134 BOOST_CHECK_NE(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
135 boost::lexical_cast<Network>("2001:db8:3f9:1::/64"));
136
137 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
138 boost::lexical_cast<Network>("2001:db8:3f9:0::/64"));
139}
140
susmit91e1d7c2016-10-03 16:16:57 -0600141BOOST_AUTO_TEST_CASE(IsValidCidr)
142{
143 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24"), true);
Davide Pesavento89567d32016-11-19 16:39:45 +0100144 BOOST_CHECK_EQUAL(Network::isValidCidr("192.1.2.3/32"), true);
145 BOOST_CHECK_EQUAL(Network::isValidCidr("0.0.0.0/0"), true);
susmit91e1d7c2016-10-03 16:16:57 -0600146 BOOST_CHECK_EQUAL(Network::isValidCidr(""), false);
Davide Pesavento89567d32016-11-19 16:39:45 +0100147 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24/8"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600148 BOOST_CHECK_EQUAL(Network::isValidCidr("/192.0.0.0/24"), false);
149 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/+24"), false);
Davide Pesavento89567d32016-11-19 16:39:45 +0100150 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/-24"), false);
151 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/ 24"), false);
152 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24a"), false);
153 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/0x42"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600154 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24.42"), false);
155 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/foo"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600156 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/33"), false);
Davide Pesaventoac767aa2016-11-19 18:17:35 +0100157 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/999999999999999"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600158 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/"), false);
159 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0"), false);
160 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/4"), false);
161 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/"), false);
162 BOOST_CHECK_EQUAL(Network::isValidCidr("foo"), false);
163 BOOST_CHECK_EQUAL(Network::isValidCidr("256.0.256.0/24"), false);
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400164
165 BOOST_CHECK_EQUAL(Network::isValidCidr("::1"), false);
166 BOOST_CHECK_EQUAL(Network::isValidCidr("::1/128"), true);
susmit91e1d7c2016-10-03 16:16:57 -0600167}
168
Davide Pesavento97210d52016-10-14 15:45:48 +0200169BOOST_AUTO_TEST_SUITE_END() // TestNetwork
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800170
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400171} // namespace nfd::tests