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