blob: 1dfd084c0d2a1e008b2b27da446ae78f59107e6d [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tests {
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080031
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032BOOST_AUTO_TEST_SUITE(TestNetwork)
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080033
34using boost::asio::ip::address;
35
36BOOST_AUTO_TEST_CASE(Empty)
37{
38 Network n;
39 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
40 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
41 false);
42
43 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
44 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
45
46 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
47 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
48 false);
49}
50
51BOOST_AUTO_TEST_CASE(MaxRangeV4)
52{
53 Network n = Network::getMaxRangeV4();
54 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
55 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
56 false);
57
58 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), true);
59 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), true);
60
61 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
62 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
63 false);
64}
65
66BOOST_AUTO_TEST_CASE(RangeV4)
67{
68 Network n = boost::lexical_cast<Network>("192.0.2.0/24");
69 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), "192.0.2.0 <-> 192.0.2.255");
70
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040071 BOOST_CHECK_THROW(boost::lexical_cast<Network>("192.0.2.0/255"), boost::bad_lexical_cast);
72 BOOST_CHECK_THROW(boost::lexical_cast<Network>("256.0.2.0/24"), boost::bad_lexical_cast);
73
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080074 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
75 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.254")), true);
76 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.1.255")), false);
77 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.3.0")), false);
78
79 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
80 false);
81
82 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
83 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
84
85 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
86 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
87 false);
88}
89
90BOOST_AUTO_TEST_CASE(MaxRangeV6)
91{
92 Network n = Network::getMaxRangeV6();
93 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
94 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
95 true);
96
97 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
98 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
99
100 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), true);
101 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
102 true);
103}
104
105BOOST_AUTO_TEST_CASE(RangeV6)
106{
107 Network n = boost::lexical_cast<Network>("2001:db8:3f9:1::/64");
108 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n),
109 "2001:db8:3f9:1:: <-> 2001:db8:3f9:1:ffff:ffff:ffff:ffff");
110
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400111 BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9:1::/129"), boost::bad_lexical_cast);
112 BOOST_CHECK_THROW(boost::lexical_cast<Network>("200x:db8:3f9:1::/64"), boost::bad_lexical_cast);
113 BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9::1::/64"), boost::bad_lexical_cast);
114
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800115 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
116 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
117 true);
118 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1::")),
119 true);
120 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:ffff:ffff:ffff:ffff")),
121 true);
122 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:ffff:ffff:ffff:ffff")),
123 false);
124 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:2::")),
125 false);
126
127 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
128 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
129
130 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
131 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
132 false);
133}
134
135BOOST_AUTO_TEST_CASE(Comparisons)
136{
137 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("192.0.2.0/24"),
138 boost::lexical_cast<Network>("192.0.2.127/24"));
139
140 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
141 boost::lexical_cast<Network>("2001:db8:3f9:0:ffff::/64"));
142
143 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
144 boost::lexical_cast<Network>("192.0.3.127/24"));
145
146 BOOST_CHECK_NE(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
147 boost::lexical_cast<Network>("2001:db8:3f9:1::/64"));
148
149 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
150 boost::lexical_cast<Network>("2001:db8:3f9:0::/64"));
151}
152
susmit91e1d7c2016-10-03 16:16:57 -0600153BOOST_AUTO_TEST_CASE(IsValidCidr)
154{
155 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24"), true);
Davide Pesavento89567d32016-11-19 16:39:45 +0100156 BOOST_CHECK_EQUAL(Network::isValidCidr("192.1.2.3/32"), true);
157 BOOST_CHECK_EQUAL(Network::isValidCidr("0.0.0.0/0"), true);
susmit91e1d7c2016-10-03 16:16:57 -0600158 BOOST_CHECK_EQUAL(Network::isValidCidr(""), false);
Davide Pesavento89567d32016-11-19 16:39:45 +0100159 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24/8"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600160 BOOST_CHECK_EQUAL(Network::isValidCidr("/192.0.0.0/24"), false);
161 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/+24"), false);
Davide Pesavento89567d32016-11-19 16:39:45 +0100162 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/-24"), false);
163 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/ 24"), false);
164 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24a"), false);
165 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/0x42"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600166 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24.42"), false);
167 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/foo"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600168 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/33"), false);
Davide Pesaventoac767aa2016-11-19 18:17:35 +0100169 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/999999999999999"), false);
susmit91e1d7c2016-10-03 16:16:57 -0600170 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/"), false);
171 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0"), false);
172 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/4"), false);
173 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/"), false);
174 BOOST_CHECK_EQUAL(Network::isValidCidr("foo"), false);
175 BOOST_CHECK_EQUAL(Network::isValidCidr("256.0.256.0/24"), false);
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400176
177 BOOST_CHECK_EQUAL(Network::isValidCidr("::1"), false);
178 BOOST_CHECK_EQUAL(Network::isValidCidr("::1/128"), true);
susmit91e1d7c2016-10-03 16:16:57 -0600179}
180
Davide Pesavento97210d52016-10-14 15:45:48 +0200181BOOST_AUTO_TEST_SUITE_END() // TestNetwork
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800182
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400183} // namespace nfd::tests