blob: d994fa2afef181ae61d3058903b72c6880fb4dfa [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0ba6d642017-07-17 00:53:22 +00002/*
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 Shi0ba6d642017-07-17 00:53:22 +000036using TcpFactoryFixture = FaceSystemFactoryFixture<TcpFactory>;
37
Junxiao Shicde37ad2015-12-24 01:02:05 -070038BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000039BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, TcpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -070040
41using nfd::Face;
42
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi38b24c72017-01-05 02:59:31 +000044
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
Junxiao Shi38b24c72017-01-05 02:59:31 +000063 BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
64}
65
66BOOST_AUTO_TEST_CASE(Omitted)
67{
68 const std::string CONFIG = R"CONFIG(
69 face_system
70 {
71 }
72 )CONFIG";
73
Junxiao Shi1b65ca12017-01-21 23:04:41 +000074 parseConfig(CONFIG, true);
75 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +000076
Junxiao Shi38b24c72017-01-05 02:59:31 +000077 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
78}
79
80BOOST_AUTO_TEST_CASE(BadListen)
81{
82 const std::string CONFIG = R"CONFIG(
83 face_system
84 {
85 tcp
86 {
87 listen hello
88 }
89 }
90 )CONFIG";
91
92 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
93 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
94}
95
96BOOST_AUTO_TEST_CASE(ChannelsDisabled)
97{
98 const std::string CONFIG = R"CONFIG(
99 face_system
100 {
101 tcp
102 {
103 port 6363
104 enable_v4 no
105 enable_v6 no
106 }
107 }
108 )CONFIG";
109
110 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
111 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
112}
113
114BOOST_AUTO_TEST_CASE(UnknownOption)
115{
116 const std::string CONFIG = R"CONFIG(
117 face_system
118 {
119 tcp
120 {
121 hello
122 }
123 }
124 )CONFIG";
125
126 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
127 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
128}
129
130BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
131
Junxiao Shicde37ad2015-12-24 01:02:05 -0700132BOOST_AUTO_TEST_CASE(ChannelMap)
133{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700134 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
135 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
136 BOOST_CHECK_EQUAL(channel1, channel1a);
137 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
138
139 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
140 BOOST_CHECK_NE(channel1, channel2);
141
142 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
143 BOOST_CHECK_NE(channel2, channel3);
144 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
145}
146
147BOOST_AUTO_TEST_CASE(GetChannels)
148{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700149 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
150
151 std::vector<shared_ptr<const Channel>> expectedChannels;
152 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
153 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
154 expectedChannels.push_back(factory.createChannel("::1", "20071"));
155
156 for (const auto& ch : factory.getChannels()) {
157 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
158 BOOST_REQUIRE(pos != expectedChannels.end());
159 expectedChannels.erase(pos);
160 }
161 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
162}
163
Eric Newberry42602412016-08-27 09:33:18 -0700164BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700165{
Eric Newberry42602412016-08-27 09:33:18 -0700166 createFace(factory,
167 FaceUri("tcp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000168 {},
Eric Newberry42602412016-08-27 09:33:18 -0700169 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700170 false,
Eric Newberry42602412016-08-27 09:33:18 -0700171 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700172
173 factory.createChannel("127.0.0.1", "20071");
174
Eric Newberry42602412016-08-27 09:33:18 -0700175 createFace(factory,
Eric Newberry78e32b02017-04-01 14:34:44 +0000176 FaceUri("tcp4://127.0.0.1:6363"),
177 {},
Eric Newberry42602412016-08-27 09:33:18 -0700178 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700179 false,
Eric Newberry42602412016-08-27 09:33:18 -0700180 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400181
182 createFace(factory,
183 FaceUri("tcp4://127.0.0.1:6363"),
184 {},
185 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
186 false,
187 {CreateFaceExpectedResult::SUCCESS, 0, ""});
188
189 createFace(factory,
190 FaceUri("tcp4://127.0.0.1:20072"),
191 {},
192 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
193 false,
194 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700195}
196
197BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
198{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700199 factory.createChannel("127.0.0.1", "20071");
200
Eric Newberry42602412016-08-27 09:33:18 -0700201 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400202 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry42602412016-08-27 09:33:18 -0700203 FaceUri("tcp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400204 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
205 false,
206 {CreateFaceExpectedResult::FAILURE, 406,
207 "Unicast TCP faces cannot be created with a LocalUri"});
208
209 createFace(factory,
210 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000211 {},
Eric Newberry42602412016-08-27 09:33:18 -0700212 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700213 false,
Eric Newberry42602412016-08-27 09:33:18 -0700214 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400215 "Outgoing TCP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000216
217 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400218 FaceUri("tcp4://127.0.0.1:20071"),
219 {},
Eric Newberry78e32b02017-04-01 14:34:44 +0000220 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
221 false,
222 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400223 "Requested endpoint is prohibited"});
224
225 createFace(factory,
226 FaceUri("tcp4://198.51.100.100:6363"),
227 {},
228 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
229 true,
230 {CreateFaceExpectedResult::FAILURE, 406,
231 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700232}
233
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000234class FaceCreateTimeoutFixture : public TcpFactoryFixture
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235{
236public:
237 void
238 onFaceCreated(const shared_ptr<Face>& newFace)
239 {
240 BOOST_CHECK_MESSAGE(false, "Timeout expected");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400241 face = newFace;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700242
243 limitedIo.afterOp();
244 }
245
246 void
247 onConnectFailed(const std::string& reason)
248 {
249 BOOST_CHECK_MESSAGE(true, reason);
250
251 limitedIo.afterOp();
252 }
253
254public:
255 LimitedIo limitedIo;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400256 shared_ptr<Face> face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700257};
258
259BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
260{
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400261 factory.createChannel("0.0.0.0", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700262
263 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000264 {},
Junxiao Shicde37ad2015-12-24 01:02:05 -0700265 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700266 false,
Junxiao Shicde37ad2015-12-24 01:02:05 -0700267 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
Eric Newberry42602412016-08-27 09:33:18 -0700268 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700269
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400270 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS);
271 BOOST_CHECK(face == nullptr);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700272}
273
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000274class FakeNetworkInterfaceFixture : public TcpFactoryFixture
Junxiao Shicde37ad2015-12-24 01:02:05 -0700275{
276public:
277 FakeNetworkInterfaceFixture()
278 {
279 using namespace boost::asio::ip;
280
281 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
282
283 fakeInterfaces->push_back(
284 NetworkInterfaceInfo {0, "eth0",
285 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
286 {address_v4::from_string("0.0.0.0")},
287 {address_v6::from_string("::")},
288 address_v4(),
289 IFF_UP});
290 fakeInterfaces->push_back(
291 NetworkInterfaceInfo {1, "eth0",
292 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
293 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
294 {},
295 address_v4::from_string("192.168.2.255"),
296 0});
297 fakeInterfaces->push_back(
298 NetworkInterfaceInfo {2, "eth1",
299 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
300 {address_v4::from_string("198.51.100.1")},
301 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
302 address_v4::from_string("198.51.100.255"),
303 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
304
305 setDebugNetworkInterfaces(fakeInterfaces);
306 }
307
308 ~FakeNetworkInterfaceFixture()
309 {
310 setDebugNetworkInterfaces(nullptr);
311 }
312};
313
314BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
315{
316 using namespace boost::asio::ip;
317
Junxiao Shicde37ad2015-12-24 01:02:05 -0700318 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
319 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
320 BOOST_CHECK((factory.m_prohibitedEndpoints ==
321 std::set<tcp::Endpoint> {
322 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
323 }));
324
325 factory.m_prohibitedEndpoints.clear();
326 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
327 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
328 BOOST_CHECK((factory.m_prohibitedEndpoints ==
329 std::set<tcp::Endpoint> {
330 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
331 }));
332
333 factory.m_prohibitedEndpoints.clear();
334 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
335 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
336 BOOST_CHECK((factory.m_prohibitedEndpoints ==
337 std::set<tcp::Endpoint> {
338 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
339 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
340 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
341 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
342 }));
343
344 factory.m_prohibitedEndpoints.clear();
345 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
346 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
347 BOOST_CHECK((factory.m_prohibitedEndpoints ==
348 std::set<tcp::Endpoint> {
349 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
350 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
351 tcp::Endpoint(address_v6::from_string("::"), 2048)
352 }));
353}
354
355BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
356BOOST_AUTO_TEST_SUITE_END() // Face
357
358} // namespace tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000359} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700360} // namespace nfd