Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 10 | * |
| 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 Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 30 | #include <boost/lexical_cast.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 33 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestNetwork) |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 35 | |
| 36 | using boost::asio::ip::address; |
| 37 | |
| 38 | BOOST_AUTO_TEST_CASE(Empty) |
| 39 | { |
| 40 | Network n; |
| 41 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false); |
| 42 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")), |
| 43 | false); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false); |
| 46 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false); |
| 47 | |
| 48 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false); |
| 49 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), |
| 50 | false); |
| 51 | } |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(MaxRangeV4) |
| 54 | { |
| 55 | Network n = Network::getMaxRangeV4(); |
| 56 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true); |
| 57 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), |
| 58 | false); |
| 59 | |
| 60 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), true); |
| 61 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), true); |
| 62 | |
| 63 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false); |
| 64 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), |
| 65 | false); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(RangeV4) |
| 69 | { |
| 70 | Network n = boost::lexical_cast<Network>("192.0.2.0/24"); |
| 71 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), "192.0.2.0 <-> 192.0.2.255"); |
| 72 | |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 73 | BOOST_CHECK_THROW(boost::lexical_cast<Network>("192.0.2.0/255"), boost::bad_lexical_cast); |
| 74 | BOOST_CHECK_THROW(boost::lexical_cast<Network>("256.0.2.0/24"), boost::bad_lexical_cast); |
| 75 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true); |
| 77 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.254")), true); |
| 78 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.1.255")), false); |
| 79 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.3.0")), false); |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), |
| 82 | false); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false); |
| 85 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false); |
| 86 | |
| 87 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false); |
| 88 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), |
| 89 | false); |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(MaxRangeV6) |
| 93 | { |
| 94 | Network n = Network::getMaxRangeV6(); |
| 95 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false); |
| 96 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), |
| 97 | true); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false); |
| 100 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false); |
| 101 | |
| 102 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), true); |
| 103 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), |
| 104 | true); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(RangeV6) |
| 108 | { |
| 109 | Network n = boost::lexical_cast<Network>("2001:db8:3f9:1::/64"); |
| 110 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), |
| 111 | "2001:db8:3f9:1:: <-> 2001:db8:3f9:1:ffff:ffff:ffff:ffff"); |
| 112 | |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 113 | BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9:1::/129"), boost::bad_lexical_cast); |
| 114 | BOOST_CHECK_THROW(boost::lexical_cast<Network>("200x:db8:3f9:1::/64"), boost::bad_lexical_cast); |
| 115 | BOOST_CHECK_THROW(boost::lexical_cast<Network>("2001:db8:3f9::1::/64"), boost::bad_lexical_cast); |
| 116 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false); |
| 118 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")), |
| 119 | true); |
| 120 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1::")), |
| 121 | true); |
| 122 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:ffff:ffff:ffff:ffff")), |
| 123 | true); |
| 124 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:ffff:ffff:ffff:ffff")), |
| 125 | false); |
| 126 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:2::")), |
| 127 | false); |
| 128 | |
| 129 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false); |
| 130 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false); |
| 131 | |
| 132 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false); |
| 133 | BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")), |
| 134 | false); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(Comparisons) |
| 138 | { |
| 139 | BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("192.0.2.0/24"), |
| 140 | boost::lexical_cast<Network>("192.0.2.127/24")); |
| 141 | |
| 142 | BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"), |
| 143 | boost::lexical_cast<Network>("2001:db8:3f9:0:ffff::/64")); |
| 144 | |
| 145 | BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"), |
| 146 | boost::lexical_cast<Network>("192.0.3.127/24")); |
| 147 | |
| 148 | BOOST_CHECK_NE(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"), |
| 149 | boost::lexical_cast<Network>("2001:db8:3f9:1::/64")); |
| 150 | |
| 151 | BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"), |
| 152 | boost::lexical_cast<Network>("2001:db8:3f9:0::/64")); |
| 153 | } |
| 154 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(IsValidCidr) |
| 156 | { |
| 157 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24"), true); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.1.2.3/32"), true); |
| 159 | BOOST_CHECK_EQUAL(Network::isValidCidr("0.0.0.0/0"), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(Network::isValidCidr(""), false); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24/8"), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(Network::isValidCidr("/192.0.0.0/24"), false); |
| 163 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/+24"), false); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/-24"), false); |
| 165 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/ 24"), false); |
| 166 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24a"), false); |
| 167 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/0x42"), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24.42"), false); |
| 169 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/foo"), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/33"), false); |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/999999999999999"), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/"), false); |
| 173 | BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0"), false); |
| 174 | BOOST_CHECK_EQUAL(Network::isValidCidr("foo/4"), false); |
| 175 | BOOST_CHECK_EQUAL(Network::isValidCidr("foo/"), false); |
| 176 | BOOST_CHECK_EQUAL(Network::isValidCidr("foo"), false); |
| 177 | BOOST_CHECK_EQUAL(Network::isValidCidr("256.0.256.0/24"), false); |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 178 | |
| 179 | BOOST_CHECK_EQUAL(Network::isValidCidr("::1"), false); |
| 180 | BOOST_CHECK_EQUAL(Network::isValidCidr("::1/128"), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 183 | BOOST_AUTO_TEST_SUITE_END() // TestNetwork |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 184 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 185 | } // namespace nfd::tests |