susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [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. |
| 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 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 26 | #include "core/network-predicate.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 27 | #include "core/config-file.hpp" |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/net/ethernet.hpp> |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/net/network-monitor-stub.hpp> |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 33 | #include <boost/property_tree/info_parser.hpp> |
| 34 | #include <sstream> |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace tests { |
| 38 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(TestNetworkPredicate) |
| 40 | |
| 41 | template<class T> |
| 42 | class NetworkPredicateBaseFixture : public BaseFixture |
| 43 | { |
| 44 | public: |
| 45 | void |
| 46 | parseConfig(const std::string& config) |
| 47 | { |
| 48 | std::istringstream input(config); |
| 49 | boost::property_tree::ptree ptree; |
| 50 | boost::property_tree::read_info(input, ptree); |
| 51 | |
| 52 | for (const auto& i : ptree) { |
| 53 | if (i.first == "whitelist") { |
| 54 | predicate.parseWhitelist(i.second); |
| 55 | } |
| 56 | else if (i.first == "blacklist") { |
| 57 | predicate.parseBlacklist(i.second); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | T predicate; |
| 64 | }; |
| 65 | |
| 66 | class NetworkInterfacePredicateFixture : public NetworkPredicateBaseFixture<NetworkInterfacePredicate> |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 67 | { |
| 68 | protected: |
| 69 | NetworkInterfacePredicateFixture() |
| 70 | { |
| 71 | using namespace boost::asio::ip; |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 72 | using namespace ndn::net; |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame] | 73 | namespace ethernet = ndn::ethernet; |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 74 | |
| 75 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 76 | netifs.back()->setIndex(0); |
| 77 | netifs.back()->setName("eth0"); |
| 78 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:00")); |
| 79 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 80 | address_v4::from_string("129.82.100.1"), address_v4::from_string("129.82.255.255"), |
| 81 | 16, AddressScope::GLOBAL, 0)); |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 82 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6, |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 83 | address_v6::from_string("2001:db8:1::1"), address_v6::from_string("2001:db8:1::ffff:ffff:ffff:ffff"), |
| 84 | 64, AddressScope::GLOBAL, 0)); |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 85 | netifs.back()->setFlags(IFF_UP); |
| 86 | |
| 87 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 88 | netifs.back()->setIndex(1); |
| 89 | netifs.back()->setName("eth1"); |
| 90 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:01")); |
| 91 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 92 | address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.255"), |
| 93 | 24, AddressScope::GLOBAL, 0)); |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 94 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6, |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 95 | address_v6::from_string("2001:db8:2::1"), address_v6::from_string("2001:db8:2::ffff:ffff:ffff:ffff"), |
| 96 | 64, AddressScope::GLOBAL, 0)); |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 97 | netifs.back()->setFlags(IFF_UP); |
| 98 | |
| 99 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 100 | netifs.back()->setIndex(2); |
| 101 | netifs.back()->setName("eth2"); |
| 102 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:02")); |
| 103 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 104 | address_v4::from_string("198.51.100.1"), address_v4::from_string("198.51.100.255"), |
| 105 | 24, AddressScope::GLOBAL, 0)); |
| 106 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6, |
| 107 | address_v6::from_string("2001:db8::1"), address_v6::from_string("2001:db8::ffff"), |
| 108 | 112, AddressScope::GLOBAL, 0)); |
| 109 | netifs.back()->setFlags(IFF_MULTICAST | IFF_BROADCAST | IFF_UP); |
| 110 | |
| 111 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 112 | netifs.back()->setIndex(3); |
| 113 | netifs.back()->setName("enp68s0f1"); |
| 114 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:03")); |
| 115 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 116 | address_v4::from_string("192.168.2.3"), address_v4::from_string("192.168.2.255"), |
| 117 | 24, AddressScope::GLOBAL, 0)); |
| 118 | netifs.back()->setFlags(IFF_UP); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 119 | } |
| 120 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 121 | protected: |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 122 | std::vector<shared_ptr<ndn::net::NetworkInterface>> netifs; |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 123 | }; |
| 124 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 125 | BOOST_FIXTURE_TEST_SUITE(NetworkInterface, NetworkInterfacePredicateFixture) |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Default) |
| 128 | { |
| 129 | parseConfig(""); |
| 130 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 132 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 133 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 134 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(EmptyWhitelist) |
| 138 | { |
| 139 | parseConfig("whitelist\n" |
| 140 | "{\n" |
| 141 | "}"); |
| 142 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 144 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 145 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 146 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(WildcardBlacklist) |
| 150 | { |
| 151 | parseConfig("blacklist\n" |
| 152 | "{\n" |
| 153 | " *\n" |
| 154 | "}"); |
| 155 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 157 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 158 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 159 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(IfnameWhitelist) |
| 163 | { |
| 164 | parseConfig("whitelist\n" |
| 165 | "{\n" |
| 166 | " ifname eth0\n" |
| 167 | " ifname eth1\n" |
| 168 | "}"); |
| 169 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 171 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 172 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 173 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE(IfnameBlacklist) |
| 177 | { |
| 178 | parseConfig("blacklist\n" |
| 179 | "{\n" |
| 180 | " ifname eth0\n" |
| 181 | " ifname eth1\n" |
| 182 | "}"); |
| 183 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 185 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 186 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 187 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(IfnameWildcardStart) |
| 191 | { |
| 192 | parseConfig("whitelist\n" |
| 193 | "{\n" |
| 194 | " ifname enp*\n" |
| 195 | "}"); |
| 196 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 198 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 199 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 200 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle) |
| 204 | { |
| 205 | parseConfig("whitelist\n" |
| 206 | "{\n" |
| 207 | " ifname *th*\n" |
| 208 | "}"); |
| 209 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 211 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 212 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 213 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_CASE(IfnameWildcardDouble) |
| 217 | { |
| 218 | parseConfig("whitelist\n" |
| 219 | "{\n" |
| 220 | " ifname eth**\n" |
| 221 | "}"); |
| 222 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 224 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 225 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 226 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE(IfnameWildcardOnly) |
| 230 | { |
| 231 | parseConfig("whitelist\n" |
| 232 | "{\n" |
| 233 | " ifname *\n" |
| 234 | "}"); |
| 235 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 237 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 238 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 239 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_CASE(IfnameQuestionMark) |
| 243 | { |
| 244 | parseConfig("whitelist\n" |
| 245 | "{\n" |
| 246 | " ifname eth?\n" |
| 247 | "}"); |
| 248 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 250 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 251 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 252 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | BOOST_AUTO_TEST_CASE(IfnameMalformed) |
| 256 | { |
| 257 | BOOST_CHECK_THROW( |
| 258 | parseConfig("whitelist\n" |
| 259 | "{\n" |
| 260 | " ifname\n" |
| 261 | "}"), |
| 262 | ConfigFile::Error); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(EtherWhitelist) |
| 266 | { |
| 267 | parseConfig("whitelist\n" |
| 268 | "{\n" |
| 269 | " ether 3e:15:c2:8b:65:00\n" |
| 270 | " ether 3e:15:c2:8b:65:01\n" |
| 271 | "}"); |
| 272 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 274 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 275 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 276 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | BOOST_AUTO_TEST_CASE(EtherBlacklist) |
| 280 | { |
| 281 | parseConfig("blacklist\n" |
| 282 | "{\n" |
| 283 | " ether 3e:15:c2:8b:65:00\n" |
| 284 | " ether 3e:15:c2:8b:65:01\n" |
| 285 | "}"); |
| 286 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 287 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 288 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 289 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 290 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | BOOST_AUTO_TEST_CASE(EtherMalformed) |
| 294 | { |
| 295 | BOOST_CHECK_THROW( |
| 296 | parseConfig("blacklist\n" |
| 297 | "{\n" |
| 298 | " ether foo\n" |
| 299 | "}"), |
| 300 | ConfigFile::Error); |
| 301 | } |
| 302 | |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 303 | BOOST_AUTO_TEST_CASE(Subnet4Whitelist) |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 304 | { |
| 305 | parseConfig("whitelist\n" |
| 306 | "{\n" |
| 307 | " subnet 192.168.0.0/16\n" |
| 308 | "}"); |
| 309 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 311 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 312 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 313 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 314 | } |
| 315 | |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 316 | BOOST_AUTO_TEST_CASE(Subnet4Blacklist) |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 317 | { |
| 318 | parseConfig("blacklist\n" |
| 319 | "{\n" |
| 320 | " subnet 192.168.0.0/16\n" |
| 321 | "}"); |
| 322 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 323 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 324 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 325 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 326 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 327 | } |
| 328 | |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 329 | BOOST_AUTO_TEST_CASE(Subnet6Whitelist) |
| 330 | { |
| 331 | parseConfig("whitelist\n" |
| 332 | "{\n" |
| 333 | " subnet 2001:db8:2::1/120\n" |
| 334 | "}"); |
| 335 | |
| 336 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 337 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 338 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 339 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
| 340 | } |
| 341 | |
| 342 | BOOST_AUTO_TEST_CASE(Subnet6Blacklist) |
| 343 | { |
| 344 | parseConfig("blacklist\n" |
| 345 | "{\n" |
| 346 | " subnet 2001:db8:2::1/120\n" |
| 347 | "}"); |
| 348 | |
| 349 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 350 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 351 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 352 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
| 353 | } |
| 354 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 355 | BOOST_AUTO_TEST_CASE(SubnetMalformed) |
| 356 | { |
| 357 | BOOST_CHECK_THROW( |
| 358 | parseConfig("blacklist\n" |
| 359 | "{\n" |
| 360 | " subnet 266.0.0.91/\n" |
| 361 | "}"), |
| 362 | ConfigFile::Error); |
| 363 | } |
| 364 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 365 | BOOST_AUTO_TEST_CASE(UnrecognizedKey) |
| 366 | { |
| 367 | BOOST_CHECK_THROW( |
| 368 | parseConfig("blacklist\n" |
| 369 | "{\n" |
| 370 | " key unrecognized\n" |
| 371 | "}"), |
| 372 | ConfigFile::Error); |
| 373 | } |
| 374 | |
| 375 | BOOST_AUTO_TEST_SUITE_END() // NetworkInterface |
| 376 | |
| 377 | class IpAddressPredicateFixture : public NetworkPredicateBaseFixture<IpAddressPredicate> |
| 378 | { |
| 379 | protected: |
| 380 | IpAddressPredicateFixture() |
| 381 | { |
| 382 | using namespace boost::asio::ip; |
| 383 | |
| 384 | addrs.push_back(address_v4::from_string("129.82.100.1")); |
| 385 | addrs.push_back(address_v6::from_string("2001:db8:1::1")); |
| 386 | addrs.push_back(address_v4::from_string("192.168.2.1")); |
| 387 | addrs.push_back(address_v6::from_string("2001:db8:2::1")); |
| 388 | } |
| 389 | |
| 390 | protected: |
| 391 | std::vector<boost::asio::ip::address> addrs; |
| 392 | }; |
| 393 | |
| 394 | BOOST_FIXTURE_TEST_SUITE(IpAddress, IpAddressPredicateFixture) |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE(Default) |
| 397 | { |
| 398 | parseConfig(""); |
| 399 | |
| 400 | BOOST_CHECK_EQUAL(predicate(addrs[0]), true); |
| 401 | BOOST_CHECK_EQUAL(predicate(addrs[1]), true); |
| 402 | BOOST_CHECK_EQUAL(predicate(addrs[2]), true); |
| 403 | BOOST_CHECK_EQUAL(predicate(addrs[3]), true); |
| 404 | } |
| 405 | |
| 406 | BOOST_AUTO_TEST_CASE(EmptyWhitelist) |
| 407 | { |
| 408 | parseConfig("whitelist\n" |
| 409 | "{\n" |
| 410 | "}"); |
| 411 | |
| 412 | BOOST_CHECK_EQUAL(predicate(addrs[0]), false); |
| 413 | BOOST_CHECK_EQUAL(predicate(addrs[1]), false); |
| 414 | BOOST_CHECK_EQUAL(predicate(addrs[2]), false); |
| 415 | BOOST_CHECK_EQUAL(predicate(addrs[3]), false); |
| 416 | } |
| 417 | |
| 418 | BOOST_AUTO_TEST_CASE(WildcardBlacklist) |
| 419 | { |
| 420 | parseConfig("blacklist\n" |
| 421 | "{\n" |
| 422 | " *\n" |
| 423 | "}"); |
| 424 | |
| 425 | BOOST_CHECK_EQUAL(predicate(addrs[0]), false); |
| 426 | BOOST_CHECK_EQUAL(predicate(addrs[1]), false); |
| 427 | BOOST_CHECK_EQUAL(predicate(addrs[2]), false); |
| 428 | BOOST_CHECK_EQUAL(predicate(addrs[3]), false); |
| 429 | } |
| 430 | |
| 431 | BOOST_AUTO_TEST_CASE(Subnet4Whitelist) |
| 432 | { |
| 433 | parseConfig("whitelist\n" |
| 434 | "{\n" |
| 435 | " subnet 192.168.0.0/16\n" |
| 436 | "}"); |
| 437 | |
| 438 | BOOST_CHECK_EQUAL(predicate(addrs[0]), false); |
| 439 | BOOST_CHECK_EQUAL(predicate(addrs[1]), false); |
| 440 | BOOST_CHECK_EQUAL(predicate(addrs[2]), true); |
| 441 | BOOST_CHECK_EQUAL(predicate(addrs[3]), false); |
| 442 | } |
| 443 | |
| 444 | BOOST_AUTO_TEST_CASE(Subnet4Blacklist) |
| 445 | { |
| 446 | parseConfig("blacklist\n" |
| 447 | "{\n" |
| 448 | " subnet 192.168.0.0/16\n" |
| 449 | "}"); |
| 450 | |
| 451 | BOOST_CHECK_EQUAL(predicate(addrs[0]), true); |
| 452 | BOOST_CHECK_EQUAL(predicate(addrs[1]), true); |
| 453 | BOOST_CHECK_EQUAL(predicate(addrs[2]), false); |
| 454 | BOOST_CHECK_EQUAL(predicate(addrs[3]), true); |
| 455 | } |
| 456 | |
| 457 | BOOST_AUTO_TEST_CASE(Subnet6Whitelist) |
| 458 | { |
| 459 | parseConfig("whitelist\n" |
| 460 | "{\n" |
| 461 | " subnet 2001:db8:2::1/120\n" |
| 462 | "}"); |
| 463 | |
| 464 | BOOST_CHECK_EQUAL(predicate(addrs[0]), false); |
| 465 | BOOST_CHECK_EQUAL(predicate(addrs[1]), false); |
| 466 | BOOST_CHECK_EQUAL(predicate(addrs[2]), false); |
| 467 | BOOST_CHECK_EQUAL(predicate(addrs[3]), true); |
| 468 | } |
| 469 | |
| 470 | BOOST_AUTO_TEST_CASE(Subnet6Blacklist) |
| 471 | { |
| 472 | parseConfig("blacklist\n" |
| 473 | "{\n" |
| 474 | " subnet 2001:db8:2::1/120\n" |
| 475 | "}"); |
| 476 | |
| 477 | BOOST_CHECK_EQUAL(predicate(addrs[0]), true); |
| 478 | BOOST_CHECK_EQUAL(predicate(addrs[1]), true); |
| 479 | BOOST_CHECK_EQUAL(predicate(addrs[2]), true); |
| 480 | BOOST_CHECK_EQUAL(predicate(addrs[3]), false); |
| 481 | } |
| 482 | |
| 483 | BOOST_AUTO_TEST_CASE(UnrecognizedKey) |
| 484 | { |
| 485 | BOOST_CHECK_THROW( |
| 486 | parseConfig("blacklist\n" |
| 487 | "{\n" |
| 488 | " ether 3e:15:c2:8b:65:00\n" |
| 489 | "}"), |
| 490 | ConfigFile::Error); |
| 491 | |
| 492 | BOOST_CHECK_THROW( |
| 493 | parseConfig("blacklist\n" |
| 494 | "{\n" |
| 495 | " ifname eth**\n" |
| 496 | "}"), |
| 497 | ConfigFile::Error); |
| 498 | |
| 499 | BOOST_CHECK_THROW( |
| 500 | parseConfig("blacklist\n" |
| 501 | "{\n" |
| 502 | " key unrecognized\n" |
| 503 | "}"), |
| 504 | ConfigFile::Error); |
| 505 | } |
| 506 | |
| 507 | BOOST_AUTO_TEST_SUITE_END() // IpAddress |
| 508 | |
| 509 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkPredicate |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 510 | |
| 511 | } // namespace tests |
| 512 | } // namespace nfd |