blob: 4b6a375d0bb3573b5cdc0d8e317951937e077050 [file] [log] [blame]
susmit91e1d7c2016-10-03 16:16:57 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
31#include <boost/property_tree/info_parser.hpp>
32#include <sstream>
33
34namespace nfd {
35namespace tests {
36
37class NetworkInterfacePredicateFixture : public BaseFixture
38{
39protected:
40 NetworkInterfacePredicateFixture()
41 {
42 using namespace boost::asio::ip;
43 interfaces.push_back(
44 NetworkInterfaceInfo{0, "eth0",
45 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
46 {address_v4::from_string("129.82.100.1")},
47 {},
48 address_v4::from_string("129.82.255.255"),
49 IFF_UP});
50 interfaces.push_back(
51 NetworkInterfaceInfo{1, "eth1",
52 ethernet::Address::fromString("3e:15:c2:8b:65:01"),
53 {address_v4::from_string("192.168.2.1")},
54 {},
55 address_v4::from_string("192.168.2.255"),
56 IFF_UP});
57 interfaces.push_back(
58 NetworkInterfaceInfo{2, "eth2",
59 ethernet::Address::fromString("3e:15:c2:8b:65:02"),
60 {address_v4::from_string("198.51.100.1")},
61 {address_v6::from_string("2001:db8::1")},
62 address_v4::from_string("198.51.100.255"),
63 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
susmit21f22e62017-03-25 15:44:45 -050064 interfaces.push_back(
65 NetworkInterfaceInfo{3, "enp68s0f1",
66 ethernet::Address::fromString("3e:15:c2:8b:65:03"),
67 {address_v4::from_string("192.168.2.3")},
68 {},
69 address_v4::from_string("192.168.2.255"),
70 IFF_UP});
71
susmit91e1d7c2016-10-03 16:16:57 -060072 }
73
74 void
75 parseConfig(const std::string& config)
76 {
77 std::istringstream input(config);
78 boost::property_tree::ptree ptree;
79 boost::property_tree::read_info(input, ptree);
80
81 for (const auto& i : ptree) {
82 if (i.first == "whitelist") {
83 predicate.parseWhitelist(i.second);
84 }
85 else if (i.first == "blacklist") {
86 predicate.parseBlacklist(i.second);
87 }
88 }
89 }
90
91protected:
92 NetworkInterfacePredicate predicate;
93 std::vector<NetworkInterfaceInfo> interfaces;
94};
95
96BOOST_FIXTURE_TEST_SUITE(TestNetworkInterfacePredicate, NetworkInterfacePredicateFixture)
97
98BOOST_AUTO_TEST_CASE(Default)
99{
100 parseConfig("");
101
102 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
103 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
104 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500105 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600106}
107
108BOOST_AUTO_TEST_CASE(EmptyWhitelist)
109{
110 parseConfig("whitelist\n"
111 "{\n"
112 "}");
113
114 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
115 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
116 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500117 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600118}
119
120BOOST_AUTO_TEST_CASE(WildcardBlacklist)
121{
122 parseConfig("blacklist\n"
123 "{\n"
124 " *\n"
125 "}");
126
127 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
128 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
129 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500130 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600131}
132
133BOOST_AUTO_TEST_CASE(IfnameWhitelist)
134{
135 parseConfig("whitelist\n"
136 "{\n"
137 " ifname eth0\n"
138 " ifname eth1\n"
139 "}");
140
141 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
142 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
143 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500144 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600145}
146
147BOOST_AUTO_TEST_CASE(IfnameBlacklist)
148{
149 parseConfig("blacklist\n"
150 "{\n"
151 " ifname eth0\n"
152 " ifname eth1\n"
153 "}");
154
155 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
156 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
157 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500158 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
159}
160
161BOOST_AUTO_TEST_CASE(IfnameWildcardStart)
162{
163 parseConfig("whitelist\n"
164 "{\n"
165 " ifname enp*\n"
166 "}");
167
168 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
169 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
170 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
171 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
172}
173
174BOOST_AUTO_TEST_CASE(IfnameWildcardMiddle)
175{
176 parseConfig("whitelist\n"
177 "{\n"
178 " ifname *th*\n"
179 "}");
180
181 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
182 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
183 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
184 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
185}
186
187BOOST_AUTO_TEST_CASE(IfnameWildcardDouble)
188{
189 parseConfig("whitelist\n"
190 "{\n"
191 " ifname eth**\n"
192 "}");
193
194 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
195 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
196 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
197 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
198}
199
200BOOST_AUTO_TEST_CASE(IfnameWildcardOnly)
201{
202 parseConfig("whitelist\n"
203 "{\n"
204 " ifname *\n"
205 "}");
206
207 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
208 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
209 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
210 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
211}
212
213BOOST_AUTO_TEST_CASE(IfnameQuestionMark)
214{
215 parseConfig("whitelist\n"
216 "{\n"
217 " ifname eth?\n"
218 "}");
219
220 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
221 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
222 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
223 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600224}
225
226BOOST_AUTO_TEST_CASE(IfnameMalformed)
227{
228 BOOST_CHECK_THROW(
229 parseConfig("whitelist\n"
230 "{\n"
231 " ifname\n"
232 "}"),
233 ConfigFile::Error);
234}
235
236BOOST_AUTO_TEST_CASE(EtherWhitelist)
237{
238 parseConfig("whitelist\n"
239 "{\n"
240 " ether 3e:15:c2:8b:65:00\n"
241 " ether 3e:15:c2:8b:65:01\n"
242 "}");
243
244 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
245 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
246 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500247 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600248}
249
250BOOST_AUTO_TEST_CASE(EtherBlacklist)
251{
252 parseConfig("blacklist\n"
253 "{\n"
254 " ether 3e:15:c2:8b:65:00\n"
255 " ether 3e:15:c2:8b:65:01\n"
256 "}");
257
258 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
259 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
260 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500261 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600262}
263
264BOOST_AUTO_TEST_CASE(EtherMalformed)
265{
266 BOOST_CHECK_THROW(
267 parseConfig("blacklist\n"
268 "{\n"
269 " ether foo\n"
270 "}"),
271 ConfigFile::Error);
272}
273
274BOOST_AUTO_TEST_CASE(SubnetWhitelist)
275{
276 parseConfig("whitelist\n"
277 "{\n"
278 " subnet 192.168.0.0/16\n"
279 "}");
280
281 BOOST_CHECK_EQUAL(predicate(interfaces[0]), false);
282 BOOST_CHECK_EQUAL(predicate(interfaces[1]), true);
283 BOOST_CHECK_EQUAL(predicate(interfaces[2]), false);
susmit21f22e62017-03-25 15:44:45 -0500284 BOOST_CHECK_EQUAL(predicate(interfaces[3]), true);
susmit91e1d7c2016-10-03 16:16:57 -0600285}
286
287BOOST_AUTO_TEST_CASE(SubnetBlacklist)
288{
289 parseConfig("blacklist\n"
290 "{\n"
291 " subnet 192.168.0.0/16\n"
292 "}");
293
294 BOOST_CHECK_EQUAL(predicate(interfaces[0]), true);
295 BOOST_CHECK_EQUAL(predicate(interfaces[1]), false);
296 BOOST_CHECK_EQUAL(predicate(interfaces[2]), true);
susmit21f22e62017-03-25 15:44:45 -0500297 BOOST_CHECK_EQUAL(predicate(interfaces[3]), false);
susmit91e1d7c2016-10-03 16:16:57 -0600298}
299
300BOOST_AUTO_TEST_CASE(SubnetMalformed)
301{
302 BOOST_CHECK_THROW(
303 parseConfig("blacklist\n"
304 "{\n"
305 " subnet 266.0.0.91/\n"
306 "}"),
307 ConfigFile::Error);
308}
309
310BOOST_AUTO_TEST_SUITE_END() // TestNetworkInterfacePredicate
311
312} // namespace tests
313} // namespace nfd