blob: 3ec581b6d19d94d911877987e41f3b62326edf6b [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/*
susmit21f22e62017-03-25 15:44:45 -05003 * Copyright (c) 2014-2017, 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
26#include "core/network-interface-predicate.hpp"
27#include "core/network-interface.hpp"
28
29#include "tests/test-common.hpp"
30
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
38class NetworkInterfacePredicateFixture : public BaseFixture
39{
40protected:
41 NetworkInterfacePredicateFixture()
42 {
43 using namespace boost::asio::ip;
44 interfaces.push_back(
45 NetworkInterfaceInfo{0, "eth0",
46 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
47 {address_v4::from_string("129.82.100.1")},
48 {},
49 address_v4::from_string("129.82.255.255"),
50 IFF_UP});
51 interfaces.push_back(
52 NetworkInterfaceInfo{1, "eth1",
53 ethernet::Address::fromString("3e:15:c2:8b:65:01"),
54 {address_v4::from_string("192.168.2.1")},
55 {},
56 address_v4::from_string("192.168.2.255"),
57 IFF_UP});
58 interfaces.push_back(
59 NetworkInterfaceInfo{2, "eth2",
60 ethernet::Address::fromString("3e:15:c2:8b:65:02"),
61 {address_v4::from_string("198.51.100.1")},
62 {address_v6::from_string("2001:db8::1")},
63 address_v4::from_string("198.51.100.255"),
64 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
susmit21f22e62017-03-25 15:44:45 -050065 interfaces.push_back(
66 NetworkInterfaceInfo{3, "enp68s0f1",
67 ethernet::Address::fromString("3e:15:c2:8b:65:03"),
68 {address_v4::from_string("192.168.2.3")},
69 {},
70 address_v4::from_string("192.168.2.255"),
71 IFF_UP});
72
Junxiao Shi2760adc2017-07-06 05:44:52 +000073 using namespace ndn::net;
74
75 netifs.push_back(NetworkMonitorStub::makeNetworkInterface());
76 netifs.back()->setIndex(0);
77 netifs.back()->setName("eth0");
78 netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:00"));
79 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4,
80 address_v4::from_string("129.82.100.1"), address_v4::from_string("129.82.255.255"),
81 16, AddressScope::GLOBAL, 0));
82 netifs.back()->setFlags(IFF_UP);
83
84 netifs.push_back(NetworkMonitorStub::makeNetworkInterface());
85 netifs.back()->setIndex(1);
86 netifs.back()->setName("eth1");
87 netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:01"));
88 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4,
89 address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.255"),
90 24, AddressScope::GLOBAL, 0));
91 netifs.back()->setFlags(IFF_UP);
92
93 netifs.push_back(NetworkMonitorStub::makeNetworkInterface());
94 netifs.back()->setIndex(2);
95 netifs.back()->setName("eth2");
96 netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:02"));
97 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4,
98 address_v4::from_string("198.51.100.1"), address_v4::from_string("198.51.100.255"),
99 24, AddressScope::GLOBAL, 0));
100 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V6,
101 address_v6::from_string("2001:db8::1"), address_v6::from_string("2001:db8::ffff"),
102 112, AddressScope::GLOBAL, 0));
103 netifs.back()->setFlags(IFF_MULTICAST | IFF_BROADCAST | IFF_UP);
104
105 netifs.push_back(NetworkMonitorStub::makeNetworkInterface());
106 netifs.back()->setIndex(3);
107 netifs.back()->setName("enp68s0f1");
108 netifs.back()->setEthernetAddress(ethernet::Address::fromString("3e:15:c2:8b:65:03"));
109 netifs.back()->addNetworkAddress(NetworkAddress(AddressFamily::V4,
110 address_v4::from_string("192.168.2.3"), address_v4::from_string("192.168.2.255"),
111 24, AddressScope::GLOBAL, 0));
112 netifs.back()->setFlags(IFF_UP);
susmit91e1d7c2016-10-03 16:16:57 -0600113 }
114
115 void
116 parseConfig(const std::string& config)
117 {
118 std::istringstream input(config);
119 boost::property_tree::ptree ptree;
120 boost::property_tree::read_info(input, ptree);
121
122 for (const auto& i : ptree) {
123 if (i.first == "whitelist") {
124 predicate.parseWhitelist(i.second);
125 }
126 else if (i.first == "blacklist") {
127 predicate.parseBlacklist(i.second);
128 }
129 }
130 }
131
132protected:
133 NetworkInterfacePredicate predicate;
134 std::vector<NetworkInterfaceInfo> interfaces;
Junxiao Shi2760adc2017-07-06 05:44:52 +0000135 std::vector<shared_ptr<ndn::net::NetworkInterface>> netifs;
susmit91e1d7c2016-10-03 16:16:57 -0600136};
137
138BOOST_FIXTURE_TEST_SUITE(TestNetworkInterfacePredicate, NetworkInterfacePredicateFixture)
139
140BOOST_AUTO_TEST_CASE(Default)
141{
142 parseConfig("");
143
144 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
145 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
146 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500147 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000148
149 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
150 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
151 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
152 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600153}
154
155BOOST_AUTO_TEST_CASE(EmptyWhitelist)
156{
157 parseConfig("whitelist\n"
158 "{\n"
159 "}");
160
161 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
162 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
163 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500164 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000165
166 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
167 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
168 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
169 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600170}
171
172BOOST_AUTO_TEST_CASE(WildcardBlacklist)
173{
174 parseConfig("blacklist\n"
175 "{\n"
176 " *\n"
177 "}");
178
179 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
180 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
181 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500182 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000183
184 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
185 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
186 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
187 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600188}
189
190BOOST_AUTO_TEST_CASE(IfnameWhitelist)
191{
192 parseConfig("whitelist\n"
193 "{\n"
194 " ifname eth0\n"
195 " ifname eth1\n"
196 "}");
197
198 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
199 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
200 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500201 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000202
203 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
204 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
205 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
206 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600207}
208
209BOOST_AUTO_TEST_CASE(IfnameBlacklist)
210{
211 parseConfig("blacklist\n"
212 "{\n"
213 " ifname eth0\n"
214 " ifname eth1\n"
215 "}");
216
217 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
218 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
219 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500220 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000221
222 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
223 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
224 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
225 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit21f22e62017-03-25 15:44:45 -0500226}
227
228BOOST_AUTO_TEST_CASE(IfnameWildcardStart)
229{
230 parseConfig("whitelist\n"
231 "{\n"
232 " ifname enp*\n"
233 "}");
234
235 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
236 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
237 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
238 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000239
240 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
241 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
242 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
243 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit21f22e62017-03-25 15:44:45 -0500244}
245
246BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle)
247{
248 parseConfig("whitelist\n"
249 "{\n"
250 " ifname *th*\n"
251 "}");
252
253 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
254 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
255 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
256 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000257
258 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
259 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
260 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
261 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit21f22e62017-03-25 15:44:45 -0500262}
263
264BOOST_AUTO_TEST_CASE(IfnameWildcardDouble)
265{
266 parseConfig("whitelist\n"
267 "{\n"
268 " ifname eth**\n"
269 "}");
270
271 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
272 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
273 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
274 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000275
276 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
277 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
278 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
279 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit21f22e62017-03-25 15:44:45 -0500280}
281
282BOOST_AUTO_TEST_CASE(IfnameWildcardOnly)
283{
284 parseConfig("whitelist\n"
285 "{\n"
286 " ifname *\n"
287 "}");
288
289 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
290 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
291 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
292 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000293
294 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
295 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
296 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
297 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit21f22e62017-03-25 15:44:45 -0500298}
299
300BOOST_AUTO_TEST_CASE(IfnameQuestionMark)
301{
302 parseConfig("whitelist\n"
303 "{\n"
304 " ifname eth?\n"
305 "}");
306
307 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
308 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
309 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
310 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000311
312 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
313 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
314 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
315 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600316}
317
318BOOST_AUTO_TEST_CASE(IfnameMalformed)
319{
320 BOOST_CHECK_THROW(
321 parseConfig("whitelist\n"
322 "{\n"
323 " ifname\n"
324 "}"),
325 ConfigFile::Error);
326}
327
328BOOST_AUTO_TEST_CASE(EtherWhitelist)
329{
330 parseConfig("whitelist\n"
331 "{\n"
332 " ether 3e:15:c2:8b:65:00\n"
333 " ether 3e:15:c2:8b:65:01\n"
334 "}");
335
336 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
337 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
338 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500339 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000340
341 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
342 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
343 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
344 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600345}
346
347BOOST_AUTO_TEST_CASE(EtherBlacklist)
348{
349 parseConfig("blacklist\n"
350 "{\n"
351 " ether 3e:15:c2:8b:65:00\n"
352 " ether 3e:15:c2:8b:65:01\n"
353 "}");
354
355 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
356 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
357 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500358 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000359
360 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
361 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
362 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
363 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600364}
365
366BOOST_AUTO_TEST_CASE(EtherMalformed)
367{
368 BOOST_CHECK_THROW(
369 parseConfig("blacklist\n"
370 "{\n"
371 " ether foo\n"
372 "}"),
373 ConfigFile::Error);
374}
375
376BOOST_AUTO_TEST_CASE(SubnetWhitelist)
377{
378 parseConfig("whitelist\n"
379 "{\n"
380 " subnet 192.168.0.0/16\n"
381 "}");
382
383 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
384 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
385 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500386 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000387
388 BOOST_CHECK_EQUAL(predicate(*netifs[0]), false);
389 BOOST_CHECK_EQUAL(predicate(*netifs[1]), true);
390 BOOST_CHECK_EQUAL(predicate(*netifs[2]), false);
391 BOOST_CHECK_EQUAL(predicate(*netifs[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600392}
393
394BOOST_AUTO_TEST_CASE(SubnetBlacklist)
395{
396 parseConfig("blacklist\n"
397 "{\n"
398 " subnet 192.168.0.0/16\n"
399 "}");
400
401 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
402 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
403 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500404 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
Junxiao Shi2760adc2017-07-06 05:44:52 +0000405
406 BOOST_CHECK_EQUAL(predicate(*netifs[0]), true);
407 BOOST_CHECK_EQUAL(predicate(*netifs[1]), false);
408 BOOST_CHECK_EQUAL(predicate(*netifs[2]), true);
409 BOOST_CHECK_EQUAL(predicate(*netifs[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600410}
411
412BOOST_AUTO_TEST_CASE(SubnetMalformed)
413{
414 BOOST_CHECK_THROW(
415 parseConfig("blacklist\n"
416 "{\n"
417 " subnet 266.0.0.91/\n"
418 "}"),
419 ConfigFile::Error);
420}
421
422BOOST_AUTO_TEST_SUITE_END() // TestNetworkInterfacePredicate
423
424} // namespace tests
425} // namespace nfd