blob: 7f93d7860affa8e823df302e2cab7bc511ff69f7 [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi38b24c72017-01-05 02:59:31 +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/tcp-factory.hpp"
27
Eric Newberry42602412016-08-27 09:33:18 -070028#include "factory-test-common.hpp"
Junxiao Shi38b24c72017-01-05 02:59:31 +000029#include "face-system-fixture.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
31
32namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000033namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace tests {
35
Junxiao Shi38b24c72017-01-05 02:59:31 +000036using namespace nfd::tests;
37
Junxiao Shicde37ad2015-12-24 01:02:05 -070038BOOST_AUTO_TEST_SUITE(Face)
39BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, BaseFixture)
40
41using nfd::Face;
42
Junxiao Shi38b24c72017-01-05 02:59:31 +000043BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture)
44
45BOOST_AUTO_TEST_CASE(Normal)
46{
47 const std::string CONFIG = R"CONFIG(
48 face_system
49 {
50 tcp
51 {
52 listen yes
53 port 16363
54 enable_v4 yes
55 enable_v6 yes
56 }
57 }
58 )CONFIG";
59
Junxiao Shi1b65ca12017-01-21 23:04:41 +000060 parseConfig(CONFIG, true);
61 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +000062
63 auto& factory = this->getFactoryById<TcpFactory>("tcp");
64 BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
65}
66
67BOOST_AUTO_TEST_CASE(Omitted)
68{
69 const std::string CONFIG = R"CONFIG(
70 face_system
71 {
72 }
73 )CONFIG";
74
Junxiao Shi1b65ca12017-01-21 23:04:41 +000075 parseConfig(CONFIG, true);
76 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +000077
78 auto& factory = this->getFactoryById<TcpFactory>("tcp");
79 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
80}
81
82BOOST_AUTO_TEST_CASE(BadListen)
83{
84 const std::string CONFIG = R"CONFIG(
85 face_system
86 {
87 tcp
88 {
89 listen hello
90 }
91 }
92 )CONFIG";
93
94 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
95 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
96}
97
98BOOST_AUTO_TEST_CASE(ChannelsDisabled)
99{
100 const std::string CONFIG = R"CONFIG(
101 face_system
102 {
103 tcp
104 {
105 port 6363
106 enable_v4 no
107 enable_v6 no
108 }
109 }
110 )CONFIG";
111
112 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
113 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
114}
115
116BOOST_AUTO_TEST_CASE(UnknownOption)
117{
118 const std::string CONFIG = R"CONFIG(
119 face_system
120 {
121 tcp
122 {
123 hello
124 }
125 }
126 )CONFIG";
127
128 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
129 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
130}
131
132BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
133
Junxiao Shicde37ad2015-12-24 01:02:05 -0700134BOOST_AUTO_TEST_CASE(ChannelMap)
135{
136 TcpFactory factory;
137
138 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
139 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
140 BOOST_CHECK_EQUAL(channel1, channel1a);
141 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
142
143 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
144 BOOST_CHECK_NE(channel1, channel2);
145
146 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
147 BOOST_CHECK_NE(channel2, channel3);
148 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
149}
150
151BOOST_AUTO_TEST_CASE(GetChannels)
152{
153 TcpFactory factory;
154 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
155
156 std::vector<shared_ptr<const Channel>> expectedChannels;
157 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
158 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
159 expectedChannels.push_back(factory.createChannel("::1", "20071"));
160
161 for (const auto& ch : factory.getChannels()) {
162 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
163 BOOST_REQUIRE(pos != expectedChannels.end());
164 expectedChannels.erase(pos);
165 }
166 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
167}
168
Eric Newberry42602412016-08-27 09:33:18 -0700169BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700170{
171 TcpFactory factory;
172
Eric Newberry42602412016-08-27 09:33:18 -0700173 createFace(factory,
174 FaceUri("tcp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000175 {},
Eric Newberry42602412016-08-27 09:33:18 -0700176 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700177 false,
Eric Newberry42602412016-08-27 09:33:18 -0700178 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700179
180 factory.createChannel("127.0.0.1", "20071");
181
Eric Newberry42602412016-08-27 09:33:18 -0700182 createFace(factory,
Eric Newberry78e32b02017-04-01 14:34:44 +0000183 FaceUri("tcp4://127.0.0.1:6363"),
184 {},
Eric Newberry42602412016-08-27 09:33:18 -0700185 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700186 false,
Eric Newberry42602412016-08-27 09:33:18 -0700187 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400188
189 createFace(factory,
190 FaceUri("tcp4://127.0.0.1:6363"),
191 {},
192 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
193 false,
194 {CreateFaceExpectedResult::SUCCESS, 0, ""});
195
196 createFace(factory,
197 FaceUri("tcp4://127.0.0.1:20072"),
198 {},
199 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
200 false,
201 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700202}
203
204BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
205{
206 TcpFactory factory;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700207 factory.createChannel("127.0.0.1", "20071");
208
Eric Newberry42602412016-08-27 09:33:18 -0700209 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400210 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry42602412016-08-27 09:33:18 -0700211 FaceUri("tcp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400212 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
213 false,
214 {CreateFaceExpectedResult::FAILURE, 406,
215 "Unicast TCP faces cannot be created with a LocalUri"});
216
217 createFace(factory,
218 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000219 {},
Eric Newberry42602412016-08-27 09:33:18 -0700220 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700221 false,
Eric Newberry42602412016-08-27 09:33:18 -0700222 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400223 "Outgoing TCP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000224
225 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400226 FaceUri("tcp4://127.0.0.1:20071"),
227 {},
Eric Newberry78e32b02017-04-01 14:34:44 +0000228 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
229 false,
230 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400231 "Requested endpoint is prohibited"});
232
233 createFace(factory,
234 FaceUri("tcp4://198.51.100.100:6363"),
235 {},
236 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
237 true,
238 {CreateFaceExpectedResult::FAILURE, 406,
239 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700240}
241
242class FaceCreateTimeoutFixture : public BaseFixture
243{
244public:
245 void
246 onFaceCreated(const shared_ptr<Face>& newFace)
247 {
248 BOOST_CHECK_MESSAGE(false, "Timeout expected");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400249 face = newFace;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700250
251 limitedIo.afterOp();
252 }
253
254 void
255 onConnectFailed(const std::string& reason)
256 {
257 BOOST_CHECK_MESSAGE(true, reason);
258
259 limitedIo.afterOp();
260 }
261
262public:
263 LimitedIo limitedIo;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400264 shared_ptr<Face> face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700265};
266
267BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
268{
269 TcpFactory factory;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400270 factory.createChannel("0.0.0.0", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700271
272 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000273 {},
Junxiao Shicde37ad2015-12-24 01:02:05 -0700274 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700275 false,
Junxiao Shicde37ad2015-12-24 01:02:05 -0700276 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
Eric Newberry42602412016-08-27 09:33:18 -0700277 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700278
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400279 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS);
280 BOOST_CHECK(face == nullptr);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281}
282
283class FakeNetworkInterfaceFixture : public BaseFixture
284{
285public:
286 FakeNetworkInterfaceFixture()
287 {
288 using namespace boost::asio::ip;
289
290 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
291
292 fakeInterfaces->push_back(
293 NetworkInterfaceInfo {0, "eth0",
294 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
295 {address_v4::from_string("0.0.0.0")},
296 {address_v6::from_string("::")},
297 address_v4(),
298 IFF_UP});
299 fakeInterfaces->push_back(
300 NetworkInterfaceInfo {1, "eth0",
301 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
302 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
303 {},
304 address_v4::from_string("192.168.2.255"),
305 0});
306 fakeInterfaces->push_back(
307 NetworkInterfaceInfo {2, "eth1",
308 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
309 {address_v4::from_string("198.51.100.1")},
310 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
311 address_v4::from_string("198.51.100.255"),
312 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
313
314 setDebugNetworkInterfaces(fakeInterfaces);
315 }
316
317 ~FakeNetworkInterfaceFixture()
318 {
319 setDebugNetworkInterfaces(nullptr);
320 }
321};
322
323BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
324{
325 using namespace boost::asio::ip;
326
327 TcpFactory factory;
328 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
329 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
330 BOOST_CHECK((factory.m_prohibitedEndpoints ==
331 std::set<tcp::Endpoint> {
332 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
333 }));
334
335 factory.m_prohibitedEndpoints.clear();
336 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
337 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
338 BOOST_CHECK((factory.m_prohibitedEndpoints ==
339 std::set<tcp::Endpoint> {
340 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
341 }));
342
343 factory.m_prohibitedEndpoints.clear();
344 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
345 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
346 BOOST_CHECK((factory.m_prohibitedEndpoints ==
347 std::set<tcp::Endpoint> {
348 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
349 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
350 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
351 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
352 }));
353
354 factory.m_prohibitedEndpoints.clear();
355 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
356 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
357 BOOST_CHECK((factory.m_prohibitedEndpoints ==
358 std::set<tcp::Endpoint> {
359 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
360 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
361 tcp::Endpoint(address_v6::from_string("::"), 2048)
362 }));
363}
364
365BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
366BOOST_AUTO_TEST_SUITE_END() // Face
367
368} // namespace tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000369} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700370} // namespace nfd