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