blob: 39567d6df9037fb14e3936773c158fafc084b523 [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
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 Newberry42602412016-08-27 09:33:18 -0700175 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700176 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400177 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
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400180 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700181
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 Newberry2642cd22017-07-13 21:34:53 -0400187 false,
Eric Newberry42602412016-08-27 09:33:18 -0700188 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400189
190 createFace(factory,
191 FaceUri("tcp4://127.0.0.1:6363"),
192 {},
193 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
194 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400195 false,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400196 {CreateFaceExpectedResult::SUCCESS, 0, ""});
197
198 createFace(factory,
199 FaceUri("tcp4://127.0.0.1:20072"),
200 {},
201 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
202 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400203 false,
204 {CreateFaceExpectedResult::SUCCESS, 0, ""});
205
206 createFace(factory,
207 FaceUri("tcp4://127.0.0.1:20073"),
208 {},
209 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
210 false,
211 true,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400212 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213}
214
Davide Pesaventob15276f2017-07-15 16:27:13 -0400215BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700216{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400217 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700218
Eric Newberry42602412016-08-27 09:33:18 -0700219 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400220 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry42602412016-08-27 09:33:18 -0700221 FaceUri("tcp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400222 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
223 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400224 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400225 {CreateFaceExpectedResult::FAILURE, 406,
226 "Unicast TCP faces cannot be created with a LocalUri"});
227
228 createFace(factory,
229 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000230 {},
Eric Newberry42602412016-08-27 09:33:18 -0700231 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700232 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400233 false,
Eric Newberry42602412016-08-27 09:33:18 -0700234 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400235 "Outgoing TCP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000236
237 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400238 FaceUri("tcp4://198.51.100.100:6363"),
239 {},
240 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
241 true,
Eric Newberry2642cd22017-07-13 21:34:53 -0400242 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400243 {CreateFaceExpectedResult::FAILURE, 406,
244 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700245}
246
Davide Pesaventob15276f2017-07-15 16:27:13 -0400247class CreateFaceTimeoutFixture : public TcpFactoryFixture
Junxiao Shicde37ad2015-12-24 01:02:05 -0700248{
249public:
250 void
251 onFaceCreated(const shared_ptr<Face>& newFace)
252 {
253 BOOST_CHECK_MESSAGE(false, "Timeout expected");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400254 face = newFace;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700255
256 limitedIo.afterOp();
257 }
258
259 void
260 onConnectFailed(const std::string& reason)
261 {
262 BOOST_CHECK_MESSAGE(true, reason);
263
264 limitedIo.afterOp();
265 }
266
267public:
268 LimitedIo limitedIo;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400269 shared_ptr<Face> face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700270};
271
Davide Pesaventob15276f2017-07-15 16:27:13 -0400272BOOST_FIXTURE_TEST_CASE(CreateFaceTimeout, CreateFaceTimeoutFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700273{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400274 createChannel("0.0.0.0", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700275
Eric Newberry944f38b2017-07-20 20:54:22 -0400276 factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {},
Eric Newberry2642cd22017-07-13 21:34:53 -0400277 ndn::nfd::FACE_PERSISTENCY_PERSISTENT, false, false},
Davide Pesaventob15276f2017-07-15 16:27:13 -0400278 bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1),
279 bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700280
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400281 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS);
282 BOOST_CHECK(face == nullptr);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700283}
284
Junxiao Shicde37ad2015-12-24 01:02:05 -0700285BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
286BOOST_AUTO_TEST_SUITE_END() // Face
287
288} // namespace tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000289} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700290} // namespace nfd