blob: b83e33124eb5709d7ada6dc02e9212c270df96ac [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7003c602017-01-10 13:35:28 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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 "face/ethernet-factory.hpp"
Junxiao Shi7003c602017-01-10 13:35:28 +000027#include "face/face.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070028
Eric Newberry42602412016-08-27 09:33:18 -070029#include "factory-test-common.hpp"
Junxiao Shi7003c602017-01-10 13:35:28 +000030#include "ethernet-fixture.hpp"
31#include "face-system-fixture.hpp"
32#include <boost/algorithm/string/replace.hpp>
33#include <boost/range/algorithm/count_if.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070034
35namespace nfd {
Junxiao Shi7003c602017-01-10 13:35:28 +000036namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070037namespace tests {
38
Junxiao Shicde37ad2015-12-24 01:02:05 -070039BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi7003c602017-01-10 13:35:28 +000040BOOST_FIXTURE_TEST_SUITE(TestEthernetFactory, EthernetFixture)
41
42using face::Face;
43
44class EthernetConfigFixture : public EthernetFixture
45 , public FaceSystemFixture
46{
47public:
48 std::vector<const Face*>
Teng Liangbfea5752017-03-29 04:51:10 +000049 listEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi7003c602017-01-10 13:35:28 +000050 {
Teng Liangbfea5752017-03-29 04:51:10 +000051 return this->listFacesByScheme("ether", linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +000052 }
53
54 size_t
Teng Liangbfea5752017-03-29 04:51:10 +000055 countEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi7003c602017-01-10 13:35:28 +000056 {
Teng Liangbfea5752017-03-29 04:51:10 +000057 return this->listEtherMcastFaces(linkType).size();
Junxiao Shi7003c602017-01-10 13:35:28 +000058 }
59};
60
61BOOST_FIXTURE_TEST_SUITE(ProcessConfig, EthernetConfigFixture)
62
63BOOST_AUTO_TEST_CASE(Normal)
64{
65 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
66
67 const std::string CONFIG = R"CONFIG(
68 face_system
69 {
70 ether
71 {
72 mcast yes
73 mcast_group 01:00:5E:00:17:AA
Teng Liangbfea5752017-03-29 04:51:10 +000074 mcast_ad_hoc no
Junxiao Shi7003c602017-01-10 13:35:28 +000075 whitelist
76 {
77 *
78 }
79 blacklist
80 {
81 }
82 }
83 }
84 )CONFIG";
85
Junxiao Shi1b65ca12017-01-21 23:04:41 +000086 parseConfig(CONFIG, true);
87 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +000088
89 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
90}
91
Teng Liangbfea5752017-03-29 04:51:10 +000092BOOST_AUTO_TEST_CASE(EnableDisableMcast)
Junxiao Shi7003c602017-01-10 13:35:28 +000093{
Teng Liangbfea5752017-03-29 04:51:10 +000094 const std::string CONFIG_WITH_MCAST = R"CONFIG(
Junxiao Shi7003c602017-01-10 13:35:28 +000095 face_system
96 {
Teng Liangbfea5752017-03-29 04:51:10 +000097 ether
98 {
99 mcast yes
100 }
101 }
102 )CONFIG";
103 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
104 face_system
105 {
106 ether
107 {
108 mcast no
109 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000110 }
111 )CONFIG";
112
Teng Liangbfea5752017-03-29 04:51:10 +0000113 parseConfig(CONFIG_WITHOUT_MCAST, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000114 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
Teng Liangbfea5752017-03-29 04:51:10 +0000115
116 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
117
118 parseConfig(CONFIG_WITH_MCAST, false);
119 g_io.poll();
120 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
121
122 parseConfig(CONFIG_WITHOUT_MCAST, false);
123 g_io.poll();
124 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
125}
126
127BOOST_AUTO_TEST_CASE(McastAdHoc)
128{
129 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
130
131 const std::string CONFIG = R"CONFIG(
132 face_system
133 {
134 ether
135 {
136 mcast_ad_hoc yes
137 }
138 }
139 )CONFIG";
140
141 parseConfig(CONFIG, false);
142 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size());
143}
144
145BOOST_AUTO_TEST_CASE(ChangeMcastGroup)
146{
147 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
148
149 const std::string CONFIG1 = R"CONFIG(
150 face_system
151 {
152 ether
153 {
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400154 mcast_group 01:00:5e:90:10:01
Teng Liangbfea5752017-03-29 04:51:10 +0000155 }
156 }
157 )CONFIG";
158 const std::string CONFIG2 = R"CONFIG(
159 face_system
160 {
161 ether
162 {
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400163 mcast_group 01:00:5e:90:10:02
Teng Liangbfea5752017-03-29 04:51:10 +0000164 }
165 }
166 )CONFIG";
167
168 parseConfig(CONFIG1, false);
169 auto etherMcastFaces = this->listEtherMcastFaces();
170 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size());
171 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(),
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400172 FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x01}));
Teng Liangbfea5752017-03-29 04:51:10 +0000173
174 parseConfig(CONFIG2, false);
175 g_io.poll();
176 etherMcastFaces = this->listEtherMcastFaces();
177 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size());
178 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(),
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400179 FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x02}));
Junxiao Shi7003c602017-01-10 13:35:28 +0000180}
181
182BOOST_AUTO_TEST_CASE(Whitelist)
183{
184 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
185
186 std::string CONFIG = R"CONFIG(
187 face_system
188 {
189 ether
190 {
191 whitelist
192 {
193 ifname %ifname
194 }
195 }
196 }
197 )CONFIG";
198 boost::replace_first(CONFIG, "%ifname", netifs.front().name);
199
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000200 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000201 auto etherMcastFaces = this->listEtherMcastFaces();
202 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), 1);
203 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri().getHost(), netifs.front().name);
204}
205
206BOOST_AUTO_TEST_CASE(Blacklist)
207{
208 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
209
210 std::string CONFIG = R"CONFIG(
211 face_system
212 {
213 ether
214 {
215 blacklist
216 {
217 ifname %ifname
218 }
219 }
220 }
221 )CONFIG";
222 boost::replace_first(CONFIG, "%ifname", netifs.front().name);
223
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000224 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000225 auto etherMcastFaces = this->listEtherMcastFaces();
226 BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size() - 1);
227 BOOST_CHECK_EQUAL(boost::count_if(etherMcastFaces, [=] (const Face* face) {
228 return face->getLocalUri().getHost() == netifs.front().name;
229 }), 0);
230}
231
Teng Liangbfea5752017-03-29 04:51:10 +0000232BOOST_AUTO_TEST_CASE(Omitted)
Junxiao Shi7003c602017-01-10 13:35:28 +0000233{
Teng Liangbfea5752017-03-29 04:51:10 +0000234 const std::string CONFIG = R"CONFIG(
Junxiao Shi7003c602017-01-10 13:35:28 +0000235 face_system
236 {
Junxiao Shi7003c602017-01-10 13:35:28 +0000237 }
238 )CONFIG";
239
Teng Liangbfea5752017-03-29 04:51:10 +0000240 parseConfig(CONFIG, true);
241 parseConfig(CONFIG, false);
242
Junxiao Shi7003c602017-01-10 13:35:28 +0000243 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
Junxiao Shi7003c602017-01-10 13:35:28 +0000244}
245
246BOOST_AUTO_TEST_CASE(BadMcast)
247{
248 const std::string CONFIG = R"CONFIG(
249 face_system
250 {
251 ether
252 {
253 mcast hello
254 }
255 }
256 )CONFIG";
257
258 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
259 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
260}
261
262BOOST_AUTO_TEST_CASE(BadMcastGroup)
263{
264 const std::string CONFIG = R"CONFIG(
265 face_system
266 {
267 ether
268 {
269 mcast yes
270 mcast_group hello
271 }
272 }
273 )CONFIG";
274
275 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
276 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
277}
278
279BOOST_AUTO_TEST_CASE(UnicastMcastGroup)
280{
281 const std::string CONFIG = R"CONFIG(
282 face_system
283 {
284 ether
285 {
286 mcast yes
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400287 mcast_group 00:00:5e:00:53:5e
Junxiao Shi7003c602017-01-10 13:35:28 +0000288 }
289 }
290 )CONFIG";
291
292 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
293 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
294}
295
296BOOST_AUTO_TEST_CASE(UnknownOption)
297{
298 const std::string CONFIG = R"CONFIG(
299 face_system
300 {
301 ether
302 {
303 hello
304 }
305 }
306 )CONFIG";
307
308 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
309 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
310}
311
312BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700313
314BOOST_AUTO_TEST_CASE(GetChannels)
315{
316 EthernetFactory factory;
317
318 auto channels = factory.getChannels();
319 BOOST_CHECK_EQUAL(channels.empty(), true);
320}
321
Junxiao Shicde37ad2015-12-24 01:02:05 -0700322BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
323{
324 EthernetFactory factory;
325
Eric Newberry42602412016-08-27 09:33:18 -0700326 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400327 FaceUri("ether://[00:00:5e:00:53:5e]"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000328 {},
Eric Newberry42602412016-08-27 09:33:18 -0700329 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700330 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400331 {CreateFaceExpectedResult::FAILURE, 406,
332 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
333
334 createFace(factory,
335 FaceUri("ether://[00:00:5e:00:53:5e]"),
336 FaceUri("udp4://127.0.0.1:20071"),
337 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
338 false,
339 {CreateFaceExpectedResult::FAILURE, 406,
340 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
341
342 createFace(factory,
343 FaceUri("ether://[00:00:5e:00:53:5e]"),
344 FaceUri("dev://eth0"),
345 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
346 false,
347 {CreateFaceExpectedResult::FAILURE, 406,
348 "Outgoing Ethernet faces do not support on-demand persistency"});
349
350 createFace(factory,
351 FaceUri("ether://[01:00:5e:90:10:5e]"),
352 FaceUri("dev://eth0"),
353 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
354 false,
355 {CreateFaceExpectedResult::FAILURE, 406,
356 "Cannot create multicast Ethernet faces"});
357
358 createFace(factory,
359 FaceUri("ether://[00:00:5e:00:53:5e]"),
360 FaceUri("dev://eth0"),
361 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
362 true,
363 {CreateFaceExpectedResult::FAILURE, 406,
364 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700365}
366
367BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory
368BOOST_AUTO_TEST_SUITE_END() // Face
369
370} // namespace tests
Junxiao Shi7003c602017-01-10 13:35:28 +0000371} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700372} // namespace nfd