blob: 9ae6c44717c86b3d34b12c88c702c47708701e93 [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"
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
38class NetworkInterfacePredicateFixture : public BaseFixture
39{
40protected:
41 NetworkInterfacePredicateFixture()
42 {
43 using namespace boost::asio::ip;
Junxiao Shi2760adc2017-07-06 05:44:52 +000044 using namespace ndn::net;
Junxiao Shia5765d62017-08-09 04:07:46 +000045 namespace ethernet = ndn::ethernet;
Junxiao Shi2760adc2017-07-06 05:44:52 +000046
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);
susmit91e1d7c2016-10-03 16:16:57 -060085 }
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
104protected:
105 NetworkInterfacePredicate predicate;
Junxiao Shi2760adc2017-07-06 05:44:52 +0000106 std::vector<shared_ptr<ndn::net::NetworkInterface>> netifs;
susmit91e1d7c2016-10-03 16:16:57 -0600107};
108
109BOOST_FIXTURE_TEST_SUITE(TestNetworkInterfacePredicate, NetworkInterfacePredicateFixture)
110
111BOOST_AUTO_TEST_CASE(Default)
112{
113 parseConfig("");
114
Junxiao Shi2760adc2017-07-06 05:44:52 +0000115 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);
susmit91e1d7c2016-10-03 16:16:57 -0600119}
120
121BOOST_AUTO_TEST_CASE(EmptyWhitelist)
122{
123 parseConfig("whitelist\n"
124 "{\n"
125 "}");
126
Junxiao Shi2760adc2017-07-06 05:44:52 +0000127 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);
susmit91e1d7c2016-10-03 16:16:57 -0600131}
132
133BOOST_AUTO_TEST_CASE(WildcardBlacklist)
134{
135 parseConfig("blacklist\n"
136 "{\n"
137 " *\n"
138 "}");
139
Junxiao Shi2760adc2017-07-06 05:44:52 +0000140 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);
susmit91e1d7c2016-10-03 16:16:57 -0600144}
145
146BOOST_AUTO_TEST_CASE(IfnameWhitelist)
147{
148 parseConfig("whitelist\n"
149 "{\n"
150 " ifname eth0\n"
151 " ifname eth1\n"
152 "}");
153
Junxiao Shi2760adc2017-07-06 05:44:52 +0000154 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);
susmit91e1d7c2016-10-03 16:16:57 -0600158}
159
160BOOST_AUTO_TEST_CASE(IfnameBlacklist)
161{
162 parseConfig("blacklist\n"
163 "{\n"
164 " ifname eth0\n"
165 " ifname eth1\n"
166 "}");
167
Junxiao Shi2760adc2017-07-06 05:44:52 +0000168 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);
susmit21f22e62017-03-25 15:44:45 -0500172}
173
174BOOST_AUTO_TEST_CASE(IfnameWildcardStart)
175{
176 parseConfig("whitelist\n"
177 "{\n"
178 " ifname enp*\n"
179 "}");
180
Junxiao Shi2760adc2017-07-06 05:44:52 +0000181 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);
susmit21f22e62017-03-25 15:44:45 -0500185}
186
187BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle)
188{
189 parseConfig("whitelist\n"
190 "{\n"
191 " ifname *th*\n"
192 "}");
193
Junxiao Shi2760adc2017-07-06 05:44:52 +0000194 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);
susmit21f22e62017-03-25 15:44:45 -0500198}
199
200BOOST_AUTO_TEST_CASE(IfnameWildcardDouble)
201{
202 parseConfig("whitelist\n"
203 "{\n"
204 " ifname eth**\n"
205 "}");
206
Junxiao Shi2760adc2017-07-06 05:44:52 +0000207 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);
susmit21f22e62017-03-25 15:44:45 -0500211}
212
213BOOST_AUTO_TEST_CASE(IfnameWildcardOnly)
214{
215 parseConfig("whitelist\n"
216 "{\n"
217 " ifname *\n"
218 "}");
219
Junxiao Shi2760adc2017-07-06 05:44:52 +0000220 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);
susmit21f22e62017-03-25 15:44:45 -0500224}
225
226BOOST_AUTO_TEST_CASE(IfnameQuestionMark)
227{
228 parseConfig("whitelist\n"
229 "{\n"
230 " ifname eth?\n"
231 "}");
232
Junxiao Shi2760adc2017-07-06 05:44:52 +0000233 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);
susmit91e1d7c2016-10-03 16:16:57 -0600237}
238
239BOOST_AUTO_TEST_CASE(IfnameMalformed)
240{
241 BOOST_CHECK_THROW(
242 parseConfig("whitelist\n"
243 "{\n"
244 " ifname\n"
245 "}"),
246 ConfigFile::Error);
247}
248
249BOOST_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 Shi2760adc2017-07-06 05:44:52 +0000257 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);
susmit91e1d7c2016-10-03 16:16:57 -0600261}
262
263BOOST_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 Shi2760adc2017-07-06 05:44:52 +0000271 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);
susmit91e1d7c2016-10-03 16:16:57 -0600275}
276
277BOOST_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
287BOOST_AUTO_TEST_CASE(SubnetWhitelist)
288{
289 parseConfig("whitelist\n"
290 "{\n"
291 " subnet 192.168.0.0/16\n"
292 "}");
293
Junxiao Shi2760adc2017-07-06 05:44:52 +0000294 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);
susmit91e1d7c2016-10-03 16:16:57 -0600298}
299
300BOOST_AUTO_TEST_CASE(SubnetBlacklist)
301{
302 parseConfig("blacklist\n"
303 "{\n"
304 " subnet 192.168.0.0/16\n"
305 "}");
306
Junxiao Shi2760adc2017-07-06 05:44:52 +0000307 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);
susmit91e1d7c2016-10-03 16:16:57 -0600311}
312
313BOOST_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
323BOOST_AUTO_TEST_SUITE_END() // TestNetworkInterfacePredicate
324
325} // namespace tests
326} // namespace nfd