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 | /* |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 26 | #include "core/network-interface-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 | |
| 38 | class NetworkInterfacePredicateFixture : public BaseFixture |
| 39 | { |
| 40 | protected: |
| 41 | NetworkInterfacePredicateFixture() |
| 42 | { |
| 43 | using namespace boost::asio::ip; |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 44 | using namespace ndn::net; |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame] | 45 | namespace ethernet = ndn::ethernet; |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 46 | |
| 47 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 48 | netifs.back()->setIndex(0); |
| 49 | netifs.back()->setName("eth0"); |
| 50 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:00")); |
| 51 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 52 | address_v4::from_string("129.82.100.1"), address_v4::from_string("129.82.255.255"), |
| 53 | 16, AddressScope::GLOBAL, 0)); |
| 54 | netifs.back()->setFlags(IFF_UP); |
| 55 | |
| 56 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 57 | netifs.back()->setIndex(1); |
| 58 | netifs.back()->setName("eth1"); |
| 59 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:01")); |
| 60 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 61 | address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.255"), |
| 62 | 24, AddressScope::GLOBAL, 0)); |
| 63 | netifs.back()->setFlags(IFF_UP); |
| 64 | |
| 65 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 66 | netifs.back()->setIndex(2); |
| 67 | netifs.back()->setName("eth2"); |
| 68 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:02")); |
| 69 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 70 | address_v4::from_string("198.51.100.1"), address_v4::from_string("198.51.100.255"), |
| 71 | 24, AddressScope::GLOBAL, 0)); |
| 72 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6, |
| 73 | address_v6::from_string("2001:db8::1"), address_v6::from_string("2001:db8::ffff"), |
| 74 | 112, AddressScope::GLOBAL, 0)); |
| 75 | netifs.back()->setFlags(IFF_MULTICAST | IFF_BROADCAST | IFF_UP); |
| 76 | |
| 77 | netifs.push_back(NetworkMonitorStub::makeNetworkInterface()); |
| 78 | netifs.back()->setIndex(3); |
| 79 | netifs.back()->setName("enp68s0f1"); |
| 80 | netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:03")); |
| 81 | netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4, |
| 82 | address_v4::from_string("192.168.2.3"), address_v4::from_string("192.168.2.255"), |
| 83 | 24, AddressScope::GLOBAL, 0)); |
| 84 | netifs.back()->setFlags(IFF_UP); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | parseConfig(const std::string& config) |
| 89 | { |
| 90 | std::istringstream input(config); |
| 91 | boost::property_tree::ptree ptree; |
| 92 | boost::property_tree::read_info(input, ptree); |
| 93 | |
| 94 | for (const auto& i : ptree) { |
| 95 | if (i.first == "whitelist") { |
| 96 | predicate.parseWhitelist(i.second); |
| 97 | } |
| 98 | else if (i.first == "blacklist") { |
| 99 | predicate.parseBlacklist(i.second); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | protected: |
| 105 | NetworkInterfacePredicate predicate; |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 106 | std::vector<shared_ptr<ndn::net::NetworkInterface>> netifs; |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | BOOST_FIXTURE_TEST_SUITE(TestNetworkInterfacePredicate, NetworkInterfacePredicateFixture) |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(Default) |
| 112 | { |
| 113 | parseConfig(""); |
| 114 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 116 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 117 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 118 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(EmptyWhitelist) |
| 122 | { |
| 123 | parseConfig("whitelist\n" |
| 124 | "{\n" |
| 125 | "}"); |
| 126 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 128 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 129 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 130 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(WildcardBlacklist) |
| 134 | { |
| 135 | parseConfig("blacklist\n" |
| 136 | "{\n" |
| 137 | " *\n" |
| 138 | "}"); |
| 139 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 141 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 142 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 143 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(IfnameWhitelist) |
| 147 | { |
| 148 | parseConfig("whitelist\n" |
| 149 | "{\n" |
| 150 | " ifname eth0\n" |
| 151 | " ifname eth1\n" |
| 152 | "}"); |
| 153 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 155 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 156 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 157 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_CASE(IfnameBlacklist) |
| 161 | { |
| 162 | parseConfig("blacklist\n" |
| 163 | "{\n" |
| 164 | " ifname eth0\n" |
| 165 | " ifname eth1\n" |
| 166 | "}"); |
| 167 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 169 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 170 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 171 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(IfnameWildcardStart) |
| 175 | { |
| 176 | parseConfig("whitelist\n" |
| 177 | "{\n" |
| 178 | " ifname enp*\n" |
| 179 | "}"); |
| 180 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 182 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 183 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 184 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle) |
| 188 | { |
| 189 | parseConfig("whitelist\n" |
| 190 | "{\n" |
| 191 | " ifname *th*\n" |
| 192 | "}"); |
| 193 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 195 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 196 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 197 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(IfnameWildcardDouble) |
| 201 | { |
| 202 | parseConfig("whitelist\n" |
| 203 | "{\n" |
| 204 | " ifname eth**\n" |
| 205 | "}"); |
| 206 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 208 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 209 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 210 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_CASE(IfnameWildcardOnly) |
| 214 | { |
| 215 | parseConfig("whitelist\n" |
| 216 | "{\n" |
| 217 | " ifname *\n" |
| 218 | "}"); |
| 219 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 220 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 221 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 222 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 223 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | BOOST_AUTO_TEST_CASE(IfnameQuestionMark) |
| 227 | { |
| 228 | parseConfig("whitelist\n" |
| 229 | "{\n" |
| 230 | " ifname eth?\n" |
| 231 | "}"); |
| 232 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 233 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 234 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 235 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 236 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_CASE(IfnameMalformed) |
| 240 | { |
| 241 | BOOST_CHECK_THROW( |
| 242 | parseConfig("whitelist\n" |
| 243 | "{\n" |
| 244 | " ifname\n" |
| 245 | "}"), |
| 246 | ConfigFile::Error); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_CASE(EtherWhitelist) |
| 250 | { |
| 251 | parseConfig("whitelist\n" |
| 252 | "{\n" |
| 253 | " ether 3e:15:c2:8b:65:00\n" |
| 254 | " ether 3e:15:c2:8b:65:01\n" |
| 255 | "}"); |
| 256 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 258 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 259 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 260 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_CASE(EtherBlacklist) |
| 264 | { |
| 265 | parseConfig("blacklist\n" |
| 266 | "{\n" |
| 267 | " ether 3e:15:c2:8b:65:00\n" |
| 268 | " ether 3e:15:c2:8b:65:01\n" |
| 269 | "}"); |
| 270 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 272 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 273 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 274 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | BOOST_AUTO_TEST_CASE(EtherMalformed) |
| 278 | { |
| 279 | BOOST_CHECK_THROW( |
| 280 | parseConfig("blacklist\n" |
| 281 | "{\n" |
| 282 | " ether foo\n" |
| 283 | "}"), |
| 284 | ConfigFile::Error); |
| 285 | } |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE(SubnetWhitelist) |
| 288 | { |
| 289 | parseConfig("whitelist\n" |
| 290 | "{\n" |
| 291 | " subnet 192.168.0.0/16\n" |
| 292 | "}"); |
| 293 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 294 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), false); |
| 295 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), true); |
| 296 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), false); |
| 297 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), true); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | BOOST_AUTO_TEST_CASE(SubnetBlacklist) |
| 301 | { |
| 302 | parseConfig("blacklist\n" |
| 303 | "{\n" |
| 304 | " subnet 192.168.0.0/16\n" |
| 305 | "}"); |
| 306 | |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(predicate(*netifs[0]), true); |
| 308 | BOOST_CHECK_EQUAL(predicate(*netifs[1]), false); |
| 309 | BOOST_CHECK_EQUAL(predicate(*netifs[2]), true); |
| 310 | BOOST_CHECK_EQUAL(predicate(*netifs[3]), false); |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | BOOST_AUTO_TEST_CASE(SubnetMalformed) |
| 314 | { |
| 315 | BOOST_CHECK_THROW( |
| 316 | parseConfig("blacklist\n" |
| 317 | "{\n" |
| 318 | " subnet 266.0.0.91/\n" |
| 319 | "}"), |
| 320 | ConfigFile::Error); |
| 321 | } |
| 322 | |
| 323 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkInterfacePredicate |
| 324 | |
| 325 | } // namespace tests |
| 326 | } // namespace nfd |