blob: a7af98f0a117b0ffb93b6006f7321d5b1e00cfee [file] [log] [blame]
Alexander Afanasyev689f0e92014-11-09 12:09:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento97210d52016-10-14 15:45:48 +02003 * Copyright (c) 2014-2016, 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
30namespace nfd {
31namespace tests {
32
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080033BOOST_FIXTURE_TEST_SUITE(TestNetwork, BaseFixture)
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080034
35using boost::asio::ip::address;
36
37BOOST_AUTO_TEST_CASE(Empty)
38{
39 Network n;
40 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
41 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
42 false);
43
44 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
45 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
46
47 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
48 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
49 false);
50}
51
52BOOST_AUTO_TEST_CASE(MaxRangeV4)
53{
54 Network n = Network::getMaxRangeV4();
55 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
56 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
57 false);
58
59 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), true);
60 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), true);
61
62 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
63 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
64 false);
65}
66
67BOOST_AUTO_TEST_CASE(RangeV4)
68{
69 Network n = boost::lexical_cast<Network>("192.0.2.0/24");
70 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), "192.0.2.0 <-> 192.0.2.255");
71
72 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
73 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.254")), true);
74 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.1.255")), false);
75 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.3.0")), false);
76
77 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
78 false);
79
80 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
81 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
82
83 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
84 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
85 false);
86}
87
88BOOST_AUTO_TEST_CASE(MaxRangeV6)
89{
90 Network n = Network::getMaxRangeV6();
91 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
92 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
93 true);
94
95 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
96 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
97
98 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), true);
99 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
100 true);
101}
102
103BOOST_AUTO_TEST_CASE(RangeV6)
104{
105 Network n = boost::lexical_cast<Network>("2001:db8:3f9:1::/64");
106 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n),
107 "2001:db8:3f9:1:: <-> 2001:db8:3f9:1:ffff:ffff:ffff:ffff");
108
109 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
110 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
111 true);
112 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1::")),
113 true);
114 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:ffff:ffff:ffff:ffff")),
115 true);
116 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:ffff:ffff:ffff:ffff")),
117 false);
118 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:2::")),
119 false);
120
121 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
122 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
123
124 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
125 BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
126 false);
127}
128
129BOOST_AUTO_TEST_CASE(Comparisons)
130{
131 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("192.0.2.0/24"),
132 boost::lexical_cast<Network>("192.0.2.127/24"));
133
134 BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
135 boost::lexical_cast<Network>("2001:db8:3f9:0:ffff::/64"));
136
137 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
138 boost::lexical_cast<Network>("192.0.3.127/24"));
139
140 BOOST_CHECK_NE(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
141 boost::lexical_cast<Network>("2001:db8:3f9:1::/64"));
142
143 BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
144 boost::lexical_cast<Network>("2001:db8:3f9:0::/64"));
145}
146
susmit91e1d7c2016-10-03 16:16:57 -0600147BOOST_AUTO_TEST_CASE(IsValidCidr)
148{
149 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24"), true);
150 BOOST_CHECK_EQUAL(Network::isValidCidr(""), false);
151 BOOST_CHECK_EQUAL(Network::isValidCidr("/192.0.0.0/24"), false);
152 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/+24"), false);
153 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/*24"), false);
154 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24.42"), false);
155 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/foo"), false);
156 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24/23"), false);
157 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/33"), false);
158 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/-24"), false);
159 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/"), false);
160 BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0"), false);
161 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/4"), false);
162 BOOST_CHECK_EQUAL(Network::isValidCidr("foo/"), false);
163 BOOST_CHECK_EQUAL(Network::isValidCidr("foo"), false);
164 BOOST_CHECK_EQUAL(Network::isValidCidr("256.0.256.0/24"), false);
165}
166
Davide Pesavento97210d52016-10-14 15:45:48 +0200167BOOST_AUTO_TEST_SUITE_END() // TestNetwork
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800168
169} // namespace tests
170} // namespace nfd