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