blob: 378511173a9af47f8bdd57c80048e744ab8926ff [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/*
Davide Pesavento15b55052018-01-27 19:09:28 -05003 * Copyright (c) 2014-2018, 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
Junxiao Shi38b24c72017-01-05 02:59:31 +000028#include "face-system-fixture.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040029#include "factory-test-common.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
31
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040032#include <ndn-cxx/net/address-converter.hpp>
33
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000035namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070036namespace tests {
37
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040038class TcpFactoryFixture : public FaceSystemFactoryFixture<TcpFactory>
39{
40protected:
41 shared_ptr<TcpChannel>
42 createChannel(const std::string& localIp, const std::string& localPort)
43 {
44 tcp::Endpoint endpoint(ndn::ip::addressFromString(localIp),
45 boost::lexical_cast<uint16_t>(localPort));
46 return factory.createChannel(endpoint);
47 }
48};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049
Junxiao Shicde37ad2015-12-24 01:02:05 -070050BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000051BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, TcpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -070052
53using nfd::Face;
54
Junxiao Shi0ba6d642017-07-17 00:53:22 +000055BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi38b24c72017-01-05 02:59:31 +000056
57BOOST_AUTO_TEST_CASE(Normal)
58{
59 const std::string CONFIG = R"CONFIG(
60 face_system
61 {
62 tcp
63 {
64 listen yes
65 port 16363
66 enable_v4 yes
67 enable_v6 yes
68 }
69 }
70 )CONFIG";
71
Junxiao Shi1b65ca12017-01-21 23:04:41 +000072 parseConfig(CONFIG, true);
73 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +000074
Junxiao Shi38b24c72017-01-05 02:59:31 +000075 BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
76}
77
78BOOST_AUTO_TEST_CASE(Omitted)
79{
80 const std::string CONFIG = R"CONFIG(
81 face_system
82 {
83 }
84 )CONFIG";
85
Junxiao Shi1b65ca12017-01-21 23:04:41 +000086 parseConfig(CONFIG, true);
87 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +000088
Junxiao Shi38b24c72017-01-05 02:59:31 +000089 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
90}
91
92BOOST_AUTO_TEST_CASE(BadListen)
93{
94 const std::string CONFIG = R"CONFIG(
95 face_system
96 {
97 tcp
98 {
99 listen hello
100 }
101 }
102 )CONFIG";
103
104 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
105 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
106}
107
108BOOST_AUTO_TEST_CASE(ChannelsDisabled)
109{
110 const std::string CONFIG = R"CONFIG(
111 face_system
112 {
113 tcp
114 {
115 port 6363
116 enable_v4 no
117 enable_v6 no
118 }
119 }
120 )CONFIG";
121
122 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
123 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
124}
125
126BOOST_AUTO_TEST_CASE(UnknownOption)
127{
128 const std::string CONFIG = R"CONFIG(
129 face_system
130 {
131 tcp
132 {
133 hello
134 }
135 }
136 )CONFIG";
137
138 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
139 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
140}
141
142BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
143
Davide Pesaventob15276f2017-07-15 16:27:13 -0400144BOOST_AUTO_TEST_CASE(GetChannels)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700145{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400146 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
147
148 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400149 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
150 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
151 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400152 checkChannelListEqual(factory, expected);
153}
154
155BOOST_AUTO_TEST_CASE(CreateChannel)
156{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400157 auto channel1 = createChannel("127.0.0.1", "20070");
158 auto channel1a = createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700159 BOOST_CHECK_EQUAL(channel1, channel1a);
160 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
161
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400162 auto channel2 = createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700163 BOOST_CHECK_NE(channel1, channel2);
164
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400165 auto channel3 = createChannel("::1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700166 BOOST_CHECK_NE(channel2, channel3);
167 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
168}
169
Davide Pesaventob15276f2017-07-15 16:27:13 -0400170BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700171{
Eric Newberry42602412016-08-27 09:33:18 -0700172 createFace(factory,
173 FaceUri("tcp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000174 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700175 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700176 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700177
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400178 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700179
Eric Newberry42602412016-08-27 09:33:18 -0700180 createFace(factory,
Eric Newberry78e32b02017-04-01 14:34:44 +0000181 FaceUri("tcp4://127.0.0.1:6363"),
182 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700183 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700184 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400185
186 createFace(factory,
187 FaceUri("tcp4://127.0.0.1:6363"),
188 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700189 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400190 {CreateFaceExpectedResult::SUCCESS, 0, ""});
191
192 createFace(factory,
193 FaceUri("tcp4://127.0.0.1:20072"),
194 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700195 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -0400196 {CreateFaceExpectedResult::SUCCESS, 0, ""});
197
198 createFace(factory,
199 FaceUri("tcp4://127.0.0.1:20073"),
200 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700201 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, true, false},
202 {CreateFaceExpectedResult::SUCCESS, 0, ""});
203
204 createFace(factory,
205 FaceUri("tcp4://127.0.0.1:20073"),
206 {},
207 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, true},
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400208 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700209}
210
Davide Pesaventob15276f2017-07-15 16:27:13 -0400211BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700212{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400213 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700214
Eric Newberry42602412016-08-27 09:33:18 -0700215 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400216 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry42602412016-08-27 09:33:18 -0700217 FaceUri("tcp4://127.0.0.1:20071"),
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700218 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -0400219 {CreateFaceExpectedResult::FAILURE, 406,
220 "Unicast TCP faces cannot be created with a LocalUri"});
221
222 createFace(factory,
223 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000224 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700225 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700226 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400227 "Outgoing TCP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000228
229 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400230 FaceUri("tcp4://198.51.100.100:6363"),
231 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700232 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -0400233 {CreateFaceExpectedResult::FAILURE, 406,
234 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235}
236
Davide Pesaventob15276f2017-07-15 16:27:13 -0400237class CreateFaceTimeoutFixture : public TcpFactoryFixture
Junxiao Shicde37ad2015-12-24 01:02:05 -0700238{
239public:
240 void
241 onFaceCreated(const shared_ptr<Face>& newFace)
242 {
243 BOOST_CHECK_MESSAGE(false, "Timeout expected");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400244 face = newFace;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700245
246 limitedIo.afterOp();
247 }
248
249 void
250 onConnectFailed(const std::string& reason)
251 {
252 BOOST_CHECK_MESSAGE(true, reason);
253
254 limitedIo.afterOp();
255 }
256
257public:
258 LimitedIo limitedIo;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400259 shared_ptr<Face> face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700260};
261
Davide Pesaventob15276f2017-07-15 16:27:13 -0400262BOOST_FIXTURE_TEST_CASE(CreateFaceTimeout, CreateFaceTimeoutFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700263{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400264 createChannel("0.0.0.0", "20070");
Davide Pesavento15b55052018-01-27 19:09:28 -0500265 factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {}, {}},
Davide Pesaventob15276f2017-07-15 16:27:13 -0400266 bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1),
267 bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700268
Davide Pesavento15b55052018-01-27 19:09:28 -0500269 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 10_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400270 BOOST_CHECK(face == nullptr);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700271}
272
Junxiao Shicde37ad2015-12-24 01:02:05 -0700273BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
274BOOST_AUTO_TEST_SUITE_END() // Face
275
276} // namespace tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000277} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700278} // namespace nfd