blob: b1c38a6d903ea42d179e02e5c01298dc79d9a6f1 [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/websocket-factory.hpp"
27
Junxiao Shi3409cd32017-01-18 15:31:27 +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
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040031#include <ndn-cxx/net/address-converter.hpp>
32
Junxiao Shicde37ad2015-12-24 01:02:05 -070033namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
36
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040037class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory>
38{
39protected:
40 shared_ptr<WebSocketChannel>
41 createChannel(const std::string& localIp, const std::string& localPort)
42 {
43 websocket::Endpoint endpoint(ndn::ip::addressFromString(localIp),
44 boost::lexical_cast<uint16_t>(localPort));
45 return factory.createChannel(endpoint);
46 }
47};
Junxiao Shicde37ad2015-12-24 01:02:05 -070048
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049BOOST_AUTO_TEST_SUITE(Face)
50BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture)
51
52BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi3409cd32017-01-18 15:31:27 +000053
Davide Pesavento494a9552018-02-04 22:16:05 -050054BOOST_AUTO_TEST_CASE(Defaults)
Junxiao Shi3409cd32017-01-18 15:31:27 +000055{
56 const std::string CONFIG = R"CONFIG(
57 face_system
58 {
59 websocket
Junxiao Shi3409cd32017-01-18 15:31:27 +000060 }
61 )CONFIG";
62
Junxiao Shi1b65ca12017-01-21 23:04:41 +000063 parseConfig(CONFIG, true);
64 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000065
Junxiao Shi3409cd32017-01-18 15:31:27 +000066 checkChannelListEqual(factory, {"ws://[::]:9696"});
Davide Pesavento494a9552018-02-04 22:16:05 -050067 auto channels = factory.getChannels();
68 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
69 [] (const shared_ptr<const Channel>& ch) { return ch->isListening(); }));
Junxiao Shi3409cd32017-01-18 15:31:27 +000070}
71
Davide Pesavento494a9552018-02-04 22:16:05 -050072BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi3409cd32017-01-18 15:31:27 +000073{
74 const std::string CONFIG = R"CONFIG(
75 face_system
76 {
77 websocket
78 {
Davide Pesavento494a9552018-02-04 22:16:05 -050079 listen no
80 port 7001
81 }
82 }
83 )CONFIG";
84
85 parseConfig(CONFIG, true);
86 parseConfig(CONFIG, false);
87
88 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
89}
90
91BOOST_AUTO_TEST_CASE(DisableV4)
92{
93 const std::string CONFIG = R"CONFIG(
94 face_system
95 {
96 websocket
97 {
98 port 7001
99 enable_v4 no
100 enable_v6 yes
101 }
102 }
103 )CONFIG";
104
105 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
106 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
107}
108
109BOOST_AUTO_TEST_CASE(DisableV6)
110{
111 const std::string CONFIG = R"CONFIG(
112 face_system
113 {
114 websocket
115 {
116 port 7001
Junxiao Shi3409cd32017-01-18 15:31:27 +0000117 enable_v4 yes
118 enable_v6 no
119 }
120 }
121 )CONFIG";
122
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000123 parseConfig(CONFIG, true);
124 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000125
Davide Pesavento494a9552018-02-04 22:16:05 -0500126 checkChannelListEqual(factory, {"ws://0.0.0.0:7001"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000127}
128
129BOOST_AUTO_TEST_CASE(ChangeEndpoint)
130{
131 const std::string CONFIG1 = R"CONFIG(
132 face_system
133 {
134 websocket
135 {
136 port 9001
137 }
138 }
139 )CONFIG";
140
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000141 parseConfig(CONFIG1, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000142 checkChannelListEqual(factory, {"ws://[::]:9001"});
143
144 const std::string CONFIG2 = R"CONFIG(
145 face_system
146 {
147 websocket
148 {
149 port 9002
150 }
151 }
152 )CONFIG";
153
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000154 parseConfig(CONFIG2, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000155 checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"});
156}
157
Davide Pesavento494a9552018-02-04 22:16:05 -0500158BOOST_AUTO_TEST_CASE(Omitted)
159{
160 const std::string CONFIG = R"CONFIG(
161 face_system
162 {
163 }
164 )CONFIG";
165
166 parseConfig(CONFIG, true);
167 parseConfig(CONFIG, false);
168
169 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
170}
171
172BOOST_AUTO_TEST_CASE(AllDisabled)
173{
174 const std::string CONFIG = R"CONFIG(
175 face_system
176 {
177 websocket
178 {
179 enable_v4 no
180 enable_v6 no
181 }
182 }
183 )CONFIG";
184
185 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
186 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
187}
188
189BOOST_AUTO_TEST_CASE(BadListen)
190{
191 const std::string CONFIG = R"CONFIG(
192 face_system
193 {
194 websocket
195 {
196 listen hello
197 }
198 }
199 )CONFIG";
200
201 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
202 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
203}
204
205BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489
206BOOST_AUTO_TEST_CASE(BadPort)
207{
208 // not a number
209 const std::string CONFIG1 = R"CONFIG(
210 face_system
211 {
212 websocket
213 {
214 port hello
215 }
216 }
217 )CONFIG";
218
219 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
220 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
221
222 // negative number
223 const std::string CONFIG2 = R"CONFIG(
224 face_system
225 {
226 websocket
227 {
228 port -1
229 }
230 }
231 )CONFIG";
232
233 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
234 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
235
236 // out of range
237 const std::string CONFIG3 = R"CONFIG(
238 face_system
239 {
240 websocket
241 {
242 port 65536
243 }
244 }
245 )CONFIG";
246
247 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
248 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
249}
250
251BOOST_AUTO_TEST_CASE(UnknownOption)
252{
253 const std::string CONFIG = R"CONFIG(
254 face_system
255 {
256 websocket
257 {
258 hello
259 }
260 }
261 )CONFIG";
262
263 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
264 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
265}
266
Junxiao Shi3409cd32017-01-18 15:31:27 +0000267BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700268
269BOOST_AUTO_TEST_CASE(GetChannels)
270{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400271 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700272
Davide Pesaventob15276f2017-07-15 16:27:13 -0400273 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400274 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
275 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
276 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400277 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700278}
279
Davide Pesaventob15276f2017-07-15 16:27:13 -0400280BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281{
Eric Newberry42602412016-08-27 09:33:18 -0700282 createFace(factory,
283 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000284 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700285 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700286 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700287
Eric Newberry42602412016-08-27 09:33:18 -0700288 createFace(factory,
289 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000290 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700291 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700292 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700293
Eric Newberry42602412016-08-27 09:33:18 -0700294 createFace(factory,
295 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000296 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700297 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700298 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700299}
300
301BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
302BOOST_AUTO_TEST_SUITE_END() // Face
303
304} // namespace tests
Junxiao Shi3409cd32017-01-18 15:31:27 +0000305} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700306} // namespace nfd