blob: e48c38f13f5f3d548651307bb14450b185fa56a6 [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
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 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 Shi7003c602017-01-10 13:35:28 +000043 std::vector<const Face*>
Teng Liangbfea5752017-03-29 04:51:10 +000044 listEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi7003c602017-01-10 13:35:28 +000045 {
Teng Liangbfea5752017-03-29 04:51:10 +000046 return this->listFacesByScheme("ether", linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +000047 }
48
49 size_t
Teng Liangbfea5752017-03-29 04:51:10 +000050 countEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi7003c602017-01-10 13:35:28 +000051 {
Teng Liangbfea5752017-03-29 04:51:10 +000052 return this->listEtherMcastFaces(linkType).size();
Junxiao Shi7003c602017-01-10 13:35:28 +000053 }
54};
55
Junxiao Shi0ba6d642017-07-17 00:53:22 +000056BOOST_AUTO_TEST_SUITE(Face)
57BOOST_FIXTURE_TEST_SUITE(TestEthernetFactory, EthernetFactoryFixture)
58
59using nfd::Face;
60
61BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi7003c602017-01-10 13:35:28 +000062
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";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000198 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000199
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);
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000203 BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri().getHost(), netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000204}
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";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000222 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shi7003c602017-01-10 13:35:28 +0000223
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) {
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000228 return face->getLocalUri().getHost() == netifs.front()->getName();
Junxiao Shi7003c602017-01-10 13:35:28 +0000229 }), 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{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700316 auto channels = factory.getChannels();
317 BOOST_CHECK_EQUAL(channels.empty(), true);
318}
319
Junxiao Shicde37ad2015-12-24 01:02:05 -0700320BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
321{
Eric Newberry42602412016-08-27 09:33:18 -0700322 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400323 FaceUri("ether://[00:00:5e:00:53:5e]"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000324 {},
Eric Newberry42602412016-08-27 09:33:18 -0700325 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700326 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400327 {CreateFaceExpectedResult::FAILURE, 406,
328 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
329
330 createFace(factory,
331 FaceUri("ether://[00:00:5e:00:53:5e]"),
332 FaceUri("udp4://127.0.0.1:20071"),
333 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
334 false,
335 {CreateFaceExpectedResult::FAILURE, 406,
336 "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"});
337
338 createFace(factory,
339 FaceUri("ether://[00:00:5e:00:53:5e]"),
340 FaceUri("dev://eth0"),
341 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
342 false,
343 {CreateFaceExpectedResult::FAILURE, 406,
344 "Outgoing Ethernet faces do not support on-demand persistency"});
345
346 createFace(factory,
347 FaceUri("ether://[01:00:5e:90:10:5e]"),
348 FaceUri("dev://eth0"),
349 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
350 false,
351 {CreateFaceExpectedResult::FAILURE, 406,
352 "Cannot create multicast Ethernet faces"});
353
354 createFace(factory,
355 FaceUri("ether://[00:00:5e:00:53:5e]"),
356 FaceUri("dev://eth0"),
357 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
358 true,
359 {CreateFaceExpectedResult::FAILURE, 406,
360 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700361}
362
363BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory
364BOOST_AUTO_TEST_SUITE_END() // Face
365
366} // namespace tests
Junxiao Shi7003c602017-01-10 13:35:28 +0000367} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700368} // namespace nfd