blob: f6307d67236626a077823d1bc4ed5ce4211c2e75 [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
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
Davide Pesaventob15276f2017-07-15 16:27:13 -0400132BOOST_AUTO_TEST_CASE(GetChannels)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700133{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400134 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
135
136 std::set<std::string> expected;
137 expected.insert(factory.createChannel("127.0.0.1", "20070")->getUri().toString());
138 expected.insert(factory.createChannel("127.0.0.1", "20071")->getUri().toString());
139 expected.insert(factory.createChannel("::1", "20071")->getUri().toString());
140 checkChannelListEqual(factory, expected);
141}
142
143BOOST_AUTO_TEST_CASE(CreateChannel)
144{
145 auto channel1 = factory.createChannel("127.0.0.1", "20070");
146 auto channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700147 BOOST_CHECK_EQUAL(channel1, channel1a);
148 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
149
Davide Pesaventob15276f2017-07-15 16:27:13 -0400150 auto channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700151 BOOST_CHECK_NE(channel1, channel2);
152
Davide Pesaventob15276f2017-07-15 16:27:13 -0400153 auto channel3 = factory.createChannel("::1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700154 BOOST_CHECK_NE(channel2, channel3);
155 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
156}
157
Davide Pesaventob15276f2017-07-15 16:27:13 -0400158BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700159{
Eric Newberry42602412016-08-27 09:33:18 -0700160 createFace(factory,
161 FaceUri("tcp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000162 {},
Eric Newberry42602412016-08-27 09:33:18 -0700163 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700164 false,
Eric Newberry42602412016-08-27 09:33:18 -0700165 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700166
167 factory.createChannel("127.0.0.1", "20071");
168
Eric Newberry42602412016-08-27 09:33:18 -0700169 createFace(factory,
Eric Newberry78e32b02017-04-01 14:34:44 +0000170 FaceUri("tcp4://127.0.0.1:6363"),
171 {},
Eric Newberry42602412016-08-27 09:33:18 -0700172 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700173 false,
Eric Newberry42602412016-08-27 09:33:18 -0700174 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400175
176 createFace(factory,
177 FaceUri("tcp4://127.0.0.1:6363"),
178 {},
179 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
180 false,
181 {CreateFaceExpectedResult::SUCCESS, 0, ""});
182
183 createFace(factory,
184 FaceUri("tcp4://127.0.0.1:20072"),
185 {},
186 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
187 false,
188 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700189}
190
Davide Pesaventob15276f2017-07-15 16:27:13 -0400191BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700193 factory.createChannel("127.0.0.1", "20071");
194
Eric Newberry42602412016-08-27 09:33:18 -0700195 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400196 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry42602412016-08-27 09:33:18 -0700197 FaceUri("tcp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400198 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
199 false,
200 {CreateFaceExpectedResult::FAILURE, 406,
201 "Unicast TCP faces cannot be created with a LocalUri"});
202
203 createFace(factory,
204 FaceUri("tcp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000205 {},
Eric Newberry42602412016-08-27 09:33:18 -0700206 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700207 false,
Eric Newberry42602412016-08-27 09:33:18 -0700208 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400209 "Outgoing TCP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000210
211 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400212 FaceUri("tcp4://198.51.100.100:6363"),
213 {},
214 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
215 true,
216 {CreateFaceExpectedResult::FAILURE, 406,
217 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700218}
219
Davide Pesaventob15276f2017-07-15 16:27:13 -0400220class CreateFaceTimeoutFixture : public TcpFactoryFixture
Junxiao Shicde37ad2015-12-24 01:02:05 -0700221{
222public:
223 void
224 onFaceCreated(const shared_ptr<Face>& newFace)
225 {
226 BOOST_CHECK_MESSAGE(false, "Timeout expected");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400227 face = newFace;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700228
229 limitedIo.afterOp();
230 }
231
232 void
233 onConnectFailed(const std::string& reason)
234 {
235 BOOST_CHECK_MESSAGE(true, reason);
236
237 limitedIo.afterOp();
238 }
239
240public:
241 LimitedIo limitedIo;
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400242 shared_ptr<Face> face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700243};
244
Davide Pesaventob15276f2017-07-15 16:27:13 -0400245BOOST_FIXTURE_TEST_CASE(CreateFaceTimeout, CreateFaceTimeoutFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700246{
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400247 factory.createChannel("0.0.0.0", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700248
Eric Newberry944f38b2017-07-20 20:54:22 -0400249 factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {},
250 ndn::nfd::FACE_PERSISTENCY_PERSISTENT, false},
Davide Pesaventob15276f2017-07-15 16:27:13 -0400251 bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1),
252 bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700253
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400254 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS);
255 BOOST_CHECK(face == nullptr);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700256}
257
Junxiao Shicde37ad2015-12-24 01:02:05 -0700258BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
259BOOST_AUTO_TEST_SUITE_END() // Face
260
261} // namespace tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000262} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700263} // namespace nfd