blob: 1234737bbb56805d717b630efa669f2df23f4b7c [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
31namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000032namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070033namespace tests {
34
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040035class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory>
36{
37protected:
38 shared_ptr<WebSocketChannel>
39 createChannel(const std::string& localIp, const std::string& localPort)
40 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040041 websocket::Endpoint endpoint(boost::asio::ip::address::from_string(localIp),
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040042 boost::lexical_cast<uint16_t>(localPort));
43 return factory.createChannel(endpoint);
44 }
45};
Junxiao Shicde37ad2015-12-24 01:02:05 -070046
Junxiao Shi0ba6d642017-07-17 00:53:22 +000047BOOST_AUTO_TEST_SUITE(Face)
48BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture)
49
50BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi3409cd32017-01-18 15:31:27 +000051
Davide Pesavento494a9552018-02-04 22:16:05 -050052BOOST_AUTO_TEST_CASE(Defaults)
Junxiao Shi3409cd32017-01-18 15:31:27 +000053{
54 const std::string CONFIG = R"CONFIG(
55 face_system
56 {
57 websocket
Junxiao Shi3409cd32017-01-18 15:31:27 +000058 }
59 )CONFIG";
60
Junxiao Shi1b65ca12017-01-21 23:04:41 +000061 parseConfig(CONFIG, true);
62 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000063
Davide Pesavento096f86a2018-11-10 19:51:45 -050064 checkChannelListEqual(factory, {"ws://0.0.0.0:9696", "ws://[::]:9696"});
Davide Pesavento494a9552018-02-04 22:16:05 -050065 auto channels = factory.getChannels();
66 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
Davide Pesavento096f86a2018-11-10 19:51:45 -050067 [] (const auto& ch) { return ch->isListening(); }));
Junxiao Shi3409cd32017-01-18 15:31:27 +000068}
69
Davide Pesavento494a9552018-02-04 22:16:05 -050070BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi3409cd32017-01-18 15:31:27 +000071{
72 const std::string CONFIG = R"CONFIG(
73 face_system
74 {
75 websocket
76 {
Davide Pesavento494a9552018-02-04 22:16:05 -050077 listen no
78 port 7001
79 }
80 }
81 )CONFIG";
82
83 parseConfig(CONFIG, true);
84 parseConfig(CONFIG, false);
85
86 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
87}
88
89BOOST_AUTO_TEST_CASE(DisableV4)
90{
91 const std::string CONFIG = R"CONFIG(
92 face_system
93 {
94 websocket
95 {
96 port 7001
97 enable_v4 no
98 enable_v6 yes
99 }
100 }
101 )CONFIG";
102
Davide Pesavento096f86a2018-11-10 19:51:45 -0500103 parseConfig(CONFIG, true);
104 parseConfig(CONFIG, false);
105
106 checkChannelListEqual(factory, {"ws://[::]:7001"});
Davide Pesavento494a9552018-02-04 22:16:05 -0500107}
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
Davide Pesavento096f86a2018-11-10 19:51:45 -0500129BOOST_AUTO_TEST_CASE(ChangePort)
Junxiao Shi3409cd32017-01-18 15:31:27 +0000130{
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);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500142 checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000143
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);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500155 checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001",
156 "ws://0.0.0.0:9002", "ws://[::]:9002"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000157}
158
Davide Pesavento494a9552018-02-04 22:16:05 -0500159BOOST_AUTO_TEST_CASE(Omitted)
160{
161 const std::string CONFIG = R"CONFIG(
162 face_system
163 {
164 }
165 )CONFIG";
166
167 parseConfig(CONFIG, true);
168 parseConfig(CONFIG, false);
169
170 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
171}
172
173BOOST_AUTO_TEST_CASE(AllDisabled)
174{
175 const std::string CONFIG = R"CONFIG(
176 face_system
177 {
178 websocket
179 {
180 enable_v4 no
181 enable_v6 no
182 }
183 }
184 )CONFIG";
185
186 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
187 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
188}
189
190BOOST_AUTO_TEST_CASE(BadListen)
191{
192 const std::string CONFIG = R"CONFIG(
193 face_system
194 {
195 websocket
196 {
197 listen hello
198 }
199 }
200 )CONFIG";
201
202 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
203 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
204}
205
206BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489
207BOOST_AUTO_TEST_CASE(BadPort)
208{
209 // not a number
210 const std::string CONFIG1 = R"CONFIG(
211 face_system
212 {
213 websocket
214 {
215 port hello
216 }
217 }
218 )CONFIG";
219
220 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
221 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
222
223 // negative number
224 const std::string CONFIG2 = R"CONFIG(
225 face_system
226 {
227 websocket
228 {
229 port -1
230 }
231 }
232 )CONFIG";
233
234 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
235 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
236
237 // out of range
238 const std::string CONFIG3 = R"CONFIG(
239 face_system
240 {
241 websocket
242 {
243 port 65536
244 }
245 }
246 )CONFIG";
247
248 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
249 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
250}
251
252BOOST_AUTO_TEST_CASE(UnknownOption)
253{
254 const std::string CONFIG = R"CONFIG(
255 face_system
256 {
257 websocket
258 {
259 hello
260 }
261 }
262 )CONFIG";
263
264 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
265 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
266}
267
Junxiao Shi3409cd32017-01-18 15:31:27 +0000268BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700269
270BOOST_AUTO_TEST_CASE(GetChannels)
271{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400272 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700273
Davide Pesaventob15276f2017-07-15 16:27:13 -0400274 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400275 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
276 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
277 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400278 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700279}
280
Davide Pesavento096f86a2018-11-10 19:51:45 -0500281BOOST_AUTO_TEST_CASE(CreateChannel)
282{
283 auto channel1 = createChannel("127.0.0.1", "20070");
284 auto channel1a = createChannel("127.0.0.1", "20070");
285 BOOST_CHECK_EQUAL(channel1, channel1a);
286 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "ws://127.0.0.1:20070");
287
288 auto channel2 = createChannel("127.0.0.1", "20071");
289 BOOST_CHECK_NE(channel1, channel2);
290
291 auto channel3 = createChannel("::1", "20071");
292 BOOST_CHECK_NE(channel2, channel3);
293 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "ws://[::1]:20071");
294}
295
Davide Pesaventob15276f2017-07-15 16:27:13 -0400296BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700297{
Eric Newberry42602412016-08-27 09:33:18 -0700298 createFace(factory,
299 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000300 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700301 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700302 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700303
Eric Newberry42602412016-08-27 09:33:18 -0700304 createFace(factory,
305 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000306 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700307 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700308 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700309
Eric Newberry42602412016-08-27 09:33:18 -0700310 createFace(factory,
311 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000312 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700313 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700314 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700315}
316
317BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
318BOOST_AUTO_TEST_SUITE_END() // Face
319
320} // namespace tests
Junxiao Shi3409cd32017-01-18 15:31:27 +0000321} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700322} // namespace nfd