blob: 27a2489a4b4b3fcf2a75f710225a402f390a4464 [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
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
Junxiao Shi7003c602017-01-10 13:35:28 +000029#include "ethernet-fixture.hpp"
30#include "face-system-fixture.hpp"
Junxiao Shi79a92082017-08-08 02:40:59 +000031#include "factory-test-common.hpp"
Junxiao Shi7003c602017-01-10 13:35:28 +000032#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 Shi0ba6d642017-07-17 00:53:22 +000039class EthernetFactoryFixture : public EthernetFixture
40 , public FaceSystemFactoryFixture<EthernetFactory>
Junxiao Shi7003c602017-01-10 13:35:28 +000041{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000042protected:
Junxiao Shi79a92082017-08-08 02:40:59 +000043 EthernetFactoryFixture()
44 {
45 this->copyRealNetifsToNetmon();
46 }
47
Junxiao Shi7003c602017-01-10 13:35:28 +000048 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
Junxiao Shi0ba6d642017-07-17 00:53:22 +000061BOOST_AUTO_TEST_SUITE(Face)
62BOOST_FIXTURE_TEST_SUITE(TestEthernetFactory, EthernetFactoryFixture)
63
64using nfd::Face;
65
66BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi7003c602017-01-10 13:35:28 +000067
68BOOST_AUTO_TEST_CASE(Normal)
69{
70 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
71
72 const std::string CONFIG = R"CONFIG(
73 face_system
74 {
75 ether
76 {
77 mcast yes
78 mcast_group 01:00:5E:00:17:AA
Teng Liangbfea5752017-03-29 04:51:10 +000079 mcast_ad_hoc no
Junxiao Shi7003c602017-01-10 13:35:28 +000080 whitelist
81 {
82 *
83 }
84 blacklist
85 {
86 }
87 }
88 }
89 )CONFIG";
90
Junxiao Shi1b65ca12017-01-21 23:04:41 +000091 parseConfig(CONFIG, true);
92 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +000093
94 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
95}
96
Teng Liangbfea5752017-03-29 04:51:10 +000097BOOST_AUTO_TEST_CASE(EnableDisableMcast)
Junxiao Shi7003c602017-01-10 13:35:28 +000098{
Teng Liangbfea5752017-03-29 04:51:10 +000099 const std::string CONFIG_WITH_MCAST = R"CONFIG(
Junxiao Shi7003c602017-01-10 13:35:28 +0000100 face_system
101 {
Teng Liangbfea5752017-03-29 04:51:10 +0000102 ether
103 {
104 mcast yes
105 }
106 }
107 )CONFIG";
108 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
109 face_system
110 {
111 ether
112 {
113 mcast no
114 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000115 }
116 )CONFIG";
117
Teng Liangbfea5752017-03-29 04:51:10 +0000118 parseConfig(CONFIG_WITHOUT_MCAST, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000119 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
Teng Liangbfea5752017-03-29 04:51:10 +0000120
121 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
122
123 parseConfig(CONFIG_WITH_MCAST, false);
124 g_io.poll();
125 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
126
127 parseConfig(CONFIG_WITHOUT_MCAST, false);
128 g_io.poll();
129 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
130}
131
132BOOST_AUTO_TEST_CASE(McastAdHoc)
133{
134 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
135
136 const std::string CONFIG = R"CONFIG(
137 face_system
138 {
139 ether
140 {
141 mcast_ad_hoc yes
142 }
143 }
144 )CONFIG";
145
146 parseConfig(CONFIG, false);
147 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size());
148}
149
150BOOST_AUTO_TEST_CASE(ChangeMcastGroup)
151{
152 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
153
154 const std::string CONFIG1 = R"CONFIG(
155 face_system
156 {
157 ether
158 {
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400159 mcast_group 01:00:5e:90:10:01
Teng Liangbfea5752017-03-29 04:51:10 +0000160 }
161 }
162 )CONFIG";
163 const std::string CONFIG2 = R"CONFIG(
164 face_system
165 {
166 ether
167 {
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400168 mcast_group 01:00:5e:90:10:02
Teng Liangbfea5752017-03-29 04:51:10 +0000169 }
170 }
171 )CONFIG";
172
173 parseConfig(CONFIG1, false);
174 auto etherMcastFaces = this->listEtherMcastFaces();
175 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size());
176 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(),
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400177 FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x01}));
Teng Liangbfea5752017-03-29 04:51:10 +0000178
179 parseConfig(CONFIG2, false);
180 g_io.poll();
181 etherMcastFaces = this->listEtherMcastFaces();
182 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size());
183 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(),
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400184 FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x02}));
Junxiao Shi7003c602017-01-10 13:35:28 +0000185}
186
187BOOST_AUTO_TEST_CASE(Whitelist)
188{
189 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
190
191 std::string CONFIG = R"CONFIG(
192 face_system
193 {
194 ether
195 {
196 whitelist
197 {
198 ifname %ifname
199 }
200 }
201 }
202 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000203 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000204
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000205 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000206 auto etherMcastFaces = this->listEtherMcastFaces();
207 BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), 1);
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000208 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri().getHost(), netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000209}
210
211BOOST_AUTO_TEST_CASE(Blacklist)
212{
213 SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
214
215 std::string CONFIG = R"CONFIG(
216 face_system
217 {
218 ether
219 {
220 blacklist
221 {
222 ifname %ifname
223 }
224 }
225 }
226 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000227 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000228
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000229 parseConfig(CONFIG, false);
Junxiao Shi7003c602017-01-10 13:35:28 +0000230 auto etherMcastFaces = this->listEtherMcastFaces();
231 BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size() - 1);
232 BOOST_CHECK_EQUAL(boost::count_if(etherMcastFaces, [=] (const Face* face) {
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000233 return face->getLocalUri().getHost() == netifs.front()->getName();
Junxiao Shi7003c602017-01-10 13:35:28 +0000234 }), 0);
235}
236
Teng Liangbfea5752017-03-29 04:51:10 +0000237BOOST_AUTO_TEST_CASE(Omitted)
Junxiao Shi7003c602017-01-10 13:35:28 +0000238{
Teng Liangbfea5752017-03-29 04:51:10 +0000239 const std::string CONFIG = R"CONFIG(
Junxiao Shi7003c602017-01-10 13:35:28 +0000240 face_system
241 {
Junxiao Shi7003c602017-01-10 13:35:28 +0000242 }
243 )CONFIG";
244
Teng Liangbfea5752017-03-29 04:51:10 +0000245 parseConfig(CONFIG, true);
246 parseConfig(CONFIG, false);
247
Junxiao Shi7003c602017-01-10 13:35:28 +0000248 BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
Junxiao Shi7003c602017-01-10 13:35:28 +0000249}
250
251BOOST_AUTO_TEST_CASE(BadMcast)
252{
253 const std::string CONFIG = R"CONFIG(
254 face_system
255 {
256 ether
257 {
258 mcast hello
259 }
260 }
261 )CONFIG";
262
263 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
264 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
265}
266
267BOOST_AUTO_TEST_CASE(BadMcastGroup)
268{
269 const std::string CONFIG = R"CONFIG(
270 face_system
271 {
272 ether
273 {
274 mcast yes
275 mcast_group hello
276 }
277 }
278 )CONFIG";
279
280 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
281 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
282}
283
284BOOST_AUTO_TEST_CASE(UnicastMcastGroup)
285{
286 const std::string CONFIG = R"CONFIG(
287 face_system
288 {
289 ether
290 {
291 mcast yes
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400292 mcast_group 00:00:5e:00:53:5e
Junxiao Shi7003c602017-01-10 13:35:28 +0000293 }
294 }
295 )CONFIG";
296
297 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
298 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
299}
300
301BOOST_AUTO_TEST_CASE(UnknownOption)
302{
303 const std::string CONFIG = R"CONFIG(
304 face_system
305 {
306 ether
307 {
308 hello
309 }
310 }
311 )CONFIG";
312
313 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
314 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
315}
316
317BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700318
319BOOST_AUTO_TEST_CASE(GetChannels)
320{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700321 auto channels = factory.getChannels();
322 BOOST_CHECK_EQUAL(channels.empty(), true);
323}
324
Junxiao Shicde37ad2015-12-24 01:02:05 -0700325BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
326{
Eric Newberry42602412016-08-27 09:33:18 -0700327 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400328 FaceUri("ether://[00:00:5e:00:53:5e]"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000329 {},
Eric Newberry42602412016-08-27 09:33:18 -0700330 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700331 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400332 {CreateFaceExpectedResult::FAILURE, 406,
333 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
334
335 createFace(factory,
336 FaceUri("ether://[00:00:5e:00:53:5e]"),
337 FaceUri("udp4://127.0.0.1:20071"),
338 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
339 false,
340 {CreateFaceExpectedResult::FAILURE, 406,
341 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
342
343 createFace(factory,
344 FaceUri("ether://[00:00:5e:00:53:5e]"),
345 FaceUri("dev://eth0"),
346 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
347 false,
348 {CreateFaceExpectedResult::FAILURE, 406,
349 "Outgoing Ethernet faces do not support on-demand persistency"});
350
351 createFace(factory,
352 FaceUri("ether://[01:00:5e:90:10:5e]"),
353 FaceUri("dev://eth0"),
354 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
355 false,
356 {CreateFaceExpectedResult::FAILURE, 406,
357 "Cannot create multicast Ethernet faces"});
358
359 createFace(factory,
360 FaceUri("ether://[00:00:5e:00:53:5e]"),
361 FaceUri("dev://eth0"),
362 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
363 true,
364 {CreateFaceExpectedResult::FAILURE, 406,
365 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700366}
367
368BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory
369BOOST_AUTO_TEST_SUITE_END() // Face
370
371} // namespace tests
Junxiao Shi7003c602017-01-10 13:35:28 +0000372} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700373} // namespace nfd