blob: d7ef00877c02c1981583a78769dc3876c835d838 [file] [log] [blame]
susmit91e1d7c2016-10-03 16:16:57 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2760adc2017-07-06 05:44:52 +00002/*
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
susmit91e1d7c2016-10-03 16:16:57 -06004 * 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 Afanasyeve4d745d2018-04-08 17:55:56 -040026#include "core/network-predicate.hpp"
susmit91e1d7c2016-10-03 16:16:57 -060027
28#include "tests/test-common.hpp"
29
Junxiao Shia5765d62017-08-09 04:07:46 +000030#include <ndn-cxx/net/ethernet.hpp>
Junxiao Shi2760adc2017-07-06 05:44:52 +000031#include <ndn-cxx/net/network-monitor-stub.hpp>
susmit91e1d7c2016-10-03 16:16:57 -060032#include <boost/property_tree/info_parser.hpp>
33#include <sstream>
34
35namespace nfd {
36namespace tests {
37
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040038BOOST_AUTO_TEST_SUITE(TestNetworkPredicate)
39
40template<class T>
41class NetworkPredicateBaseFixture : public BaseFixture
42{
43public:
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
61protected:
62 T predicate;
63};
64
65class NetworkInterfacePredicateFixture : public NetworkPredicateBaseFixture<NetworkInterfacePredicate>
susmit91e1d7c2016-10-03 16:16:57 -060066{
67protected:
68 NetworkInterfacePredicateFixture()
69 {
70 using namespace boost::asio::ip;
Junxiao Shi2760adc2017-07-06 05:44:52 +000071 using namespace ndn::net;
Junxiao Shia5765d62017-08-09 04:07:46 +000072 namespace ethernet = ndn::ethernet;
Junxiao Shi2760adc2017-07-06 05:44:52 +000073
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 Afanasyeve4d745d2018-04-08 17:55:56 -040081 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6,
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040082 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 Shi2760adc2017-07-06 05:44:52 +000084 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 Afanasyeve4d745d2018-04-08 17:55:56 -040093 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6,
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040094 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 Shi2760adc2017-07-06 05:44:52 +000096 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);
susmit91e1d7c2016-10-03 16:16:57 -0600118 }
119
susmit91e1d7c2016-10-03 16:16:57 -0600120protected:
Junxiao Shi2760adc2017-07-06 05:44:52 +0000121 std::vector<shared_ptr<ndn::net::NetworkInterface>> netifs;
susmit91e1d7c2016-10-03 16:16:57 -0600122};
123
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400124BOOST_FIXTURE_TEST_SUITE(NetworkInterface, NetworkInterfacePredicateFixture)
susmit91e1d7c2016-10-03 16:16:57 -0600125
126BOOST_AUTO_TEST_CASE(Default)
127{
128 parseConfig("");
129
Junxiao Shi2760adc2017-07-06 05:44:52 +0000130 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);
susmit91e1d7c2016-10-03 16:16:57 -0600134}
135
136BOOST_AUTO_TEST_CASE(EmptyWhitelist)
137{
138 parseConfig("whitelist\n"
139 "{\n"
140 "}");
141
Junxiao Shi2760adc2017-07-06 05:44:52 +0000142 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);
susmit91e1d7c2016-10-03 16:16:57 -0600146}
147
148BOOST_AUTO_TEST_CASE(WildcardBlacklist)
149{
150 parseConfig("blacklist\n"
151 "{\n"
152 " *\n"
153 "}");
154
Junxiao Shi2760adc2017-07-06 05:44:52 +0000155 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);
susmit91e1d7c2016-10-03 16:16:57 -0600159}
160
161BOOST_AUTO_TEST_CASE(IfnameWhitelist)
162{
163 parseConfig("whitelist\n"
164 "{\n"
165 " ifname eth0\n"
166 " ifname eth1\n"
167 "}");
168
Junxiao Shi2760adc2017-07-06 05:44:52 +0000169 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);
susmit91e1d7c2016-10-03 16:16:57 -0600173}
174
175BOOST_AUTO_TEST_CASE(IfnameBlacklist)
176{
177 parseConfig("blacklist\n"
178 "{\n"
179 " ifname eth0\n"
180 " ifname eth1\n"
181 "}");
182
Junxiao Shi2760adc2017-07-06 05:44:52 +0000183 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);
susmit21f22e62017-03-25 15:44:45 -0500187}
188
189BOOST_AUTO_TEST_CASE(IfnameWildcardStart)
190{
191 parseConfig("whitelist\n"
192 "{\n"
193 " ifname enp*\n"
194 "}");
195
Junxiao Shi2760adc2017-07-06 05:44:52 +0000196 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);
susmit21f22e62017-03-25 15:44:45 -0500200}
201
202BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle)
203{
204 parseConfig("whitelist\n"
205 "{\n"
206 " ifname *th*\n"
207 "}");
208
Junxiao Shi2760adc2017-07-06 05:44:52 +0000209 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);
susmit21f22e62017-03-25 15:44:45 -0500213}
214
215BOOST_AUTO_TEST_CASE(IfnameWildcardDouble)
216{
217 parseConfig("whitelist\n"
218 "{\n"
219 " ifname eth**\n"
220 "}");
221
Junxiao Shi2760adc2017-07-06 05:44:52 +0000222 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);
susmit21f22e62017-03-25 15:44:45 -0500226}
227
228BOOST_AUTO_TEST_CASE(IfnameWildcardOnly)
229{
230 parseConfig("whitelist\n"
231 "{\n"
232 " ifname *\n"
233 "}");
234
Junxiao Shi2760adc2017-07-06 05:44:52 +0000235 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);
susmit21f22e62017-03-25 15:44:45 -0500239}
240
241BOOST_AUTO_TEST_CASE(IfnameQuestionMark)
242{
243 parseConfig("whitelist\n"
244 "{\n"
245 " ifname eth?\n"
246 "}");
247
Junxiao Shi2760adc2017-07-06 05:44:52 +0000248 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);
susmit91e1d7c2016-10-03 16:16:57 -0600252}
253
254BOOST_AUTO_TEST_CASE(IfnameMalformed)
255{
256 BOOST_CHECK_THROW(
257 parseConfig("whitelist\n"
258 "{\n"
259 " ifname\n"
260 "}"),
261 ConfigFile::Error);
262}
263
264BOOST_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 Shi2760adc2017-07-06 05:44:52 +0000272 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);
susmit91e1d7c2016-10-03 16:16:57 -0600276}
277
278BOOST_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 Shi2760adc2017-07-06 05:44:52 +0000286 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);
susmit91e1d7c2016-10-03 16:16:57 -0600290}
291
292BOOST_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 Afanasyev3bdc1de2018-04-03 17:33:31 -0400302BOOST_AUTO_TEST_CASE(Subnet4Whitelist)
susmit91e1d7c2016-10-03 16:16:57 -0600303{
304 parseConfig("whitelist\n"
305 "{\n"
306 " subnet 192.168.0.0/16\n"
307 "}");
308
Junxiao Shi2760adc2017-07-06 05:44:52 +0000309 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);
susmit91e1d7c2016-10-03 16:16:57 -0600313}
314
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400315BOOST_AUTO_TEST_CASE(Subnet4Blacklist)
susmit91e1d7c2016-10-03 16:16:57 -0600316{
317 parseConfig("blacklist\n"
318 "{\n"
319 " subnet 192.168.0.0/16\n"
320 "}");
321
Junxiao Shi2760adc2017-07-06 05:44:52 +0000322 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);
susmit91e1d7c2016-10-03 16:16:57 -0600326}
327
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400328BOOST_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
341BOOST_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
susmit91e1d7c2016-10-03 16:16:57 -0600354BOOST_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 Afanasyeve4d745d2018-04-08 17:55:56 -0400364BOOST_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
374BOOST_AUTO_TEST_SUITE_END() // NetworkInterface
375
376class IpAddressPredicateFixture : public NetworkPredicateBaseFixture<IpAddressPredicate>
377{
378protected:
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
389protected:
390 std::vector<boost::asio::ip::address> addrs;
391};
392
393BOOST_FIXTURE_TEST_SUITE(IpAddress, IpAddressPredicateFixture)
394
395BOOST_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
405BOOST_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
417BOOST_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
430BOOST_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
443BOOST_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
456BOOST_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
469BOOST_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
482BOOST_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
506BOOST_AUTO_TEST_SUITE_END() // IpAddress
507
508BOOST_AUTO_TEST_SUITE_END() // TestNetworkPredicate
susmit91e1d7c2016-10-03 16:16:57 -0600509
510} // namespace tests
511} // namespace nfd