blob: b5104ea0ecb4b1d8e56ff17af37fd3f91e774e87 [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 Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, 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 Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/lexical_cast.hpp>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
34
35using face::WebSocketChannel;
36using face::WebSocketFactory;
Junxiao Shicde37ad2015-12-24 01:02:05 -070037
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040038class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory>
39{
40protected:
41 shared_ptr<WebSocketChannel>
42 createChannel(const std::string& localIp, const std::string& localPort)
43 {
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040044 websocket::Endpoint endpoint(boost::asio::ip::make_address(localIp),
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040045 boost::lexical_cast<uint16_t>(localPort));
46 return factory.createChannel(endpoint);
47 }
48};
Junxiao Shicde37ad2015-12-24 01:02:05 -070049
Junxiao Shi0ba6d642017-07-17 00:53:22 +000050BOOST_AUTO_TEST_SUITE(Face)
51BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture)
52
53BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi3409cd32017-01-18 15:31:27 +000054
Davide Pesavento494a9552018-02-04 22:16:05 -050055BOOST_AUTO_TEST_CASE(Defaults)
Junxiao Shi3409cd32017-01-18 15:31:27 +000056{
57 const std::string CONFIG = R"CONFIG(
58 face_system
59 {
60 websocket
Junxiao Shi3409cd32017-01-18 15:31:27 +000061 }
62 )CONFIG";
63
Junxiao Shi1b65ca12017-01-21 23:04:41 +000064 parseConfig(CONFIG, true);
65 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000066
Davide Pesavento096f86a2018-11-10 19:51:45 -050067 checkChannelListEqual(factory, {"ws://0.0.0.0:9696", "ws://[::]:9696"});
Davide Pesavento494a9552018-02-04 22:16:05 -050068 auto channels = factory.getChannels();
69 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
Davide Pesavento096f86a2018-11-10 19:51:45 -050070 [] (const auto& ch) { return ch->isListening(); }));
Junxiao Shi3409cd32017-01-18 15:31:27 +000071}
72
Davide Pesavento494a9552018-02-04 22:16:05 -050073BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi3409cd32017-01-18 15:31:27 +000074{
75 const std::string CONFIG = R"CONFIG(
76 face_system
77 {
78 websocket
79 {
Davide Pesavento494a9552018-02-04 22:16:05 -050080 listen no
81 port 7001
82 }
83 }
84 )CONFIG";
85
86 parseConfig(CONFIG, true);
87 parseConfig(CONFIG, false);
88
89 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
90}
91
92BOOST_AUTO_TEST_CASE(DisableV4)
93{
94 const std::string CONFIG = R"CONFIG(
95 face_system
96 {
97 websocket
98 {
99 port 7001
100 enable_v4 no
101 enable_v6 yes
102 }
103 }
104 )CONFIG";
105
Davide Pesavento096f86a2018-11-10 19:51:45 -0500106 parseConfig(CONFIG, true);
107 parseConfig(CONFIG, false);
108
109 checkChannelListEqual(factory, {"ws://[::]:7001"});
Davide Pesavento494a9552018-02-04 22:16:05 -0500110}
111
112BOOST_AUTO_TEST_CASE(DisableV6)
113{
114 const std::string CONFIG = R"CONFIG(
115 face_system
116 {
117 websocket
118 {
119 port 7001
Junxiao Shi3409cd32017-01-18 15:31:27 +0000120 enable_v4 yes
121 enable_v6 no
122 }
123 }
124 )CONFIG";
125
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000126 parseConfig(CONFIG, true);
127 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000128
Davide Pesavento494a9552018-02-04 22:16:05 -0500129 checkChannelListEqual(factory, {"ws://0.0.0.0:7001"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000130}
131
Davide Pesavento096f86a2018-11-10 19:51:45 -0500132BOOST_AUTO_TEST_CASE(ChangePort)
Junxiao Shi3409cd32017-01-18 15:31:27 +0000133{
134 const std::string CONFIG1 = R"CONFIG(
135 face_system
136 {
137 websocket
138 {
139 port 9001
140 }
141 }
142 )CONFIG";
143
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000144 parseConfig(CONFIG1, false);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500145 checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000146
147 const std::string CONFIG2 = R"CONFIG(
148 face_system
149 {
150 websocket
151 {
152 port 9002
153 }
154 }
155 )CONFIG";
156
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000157 parseConfig(CONFIG2, false);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500158 checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001",
159 "ws://0.0.0.0:9002", "ws://[::]:9002"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000160}
161
Davide Pesavento494a9552018-02-04 22:16:05 -0500162BOOST_AUTO_TEST_CASE(Omitted)
163{
164 const std::string CONFIG = R"CONFIG(
165 face_system
166 {
167 }
168 )CONFIG";
169
170 parseConfig(CONFIG, true);
171 parseConfig(CONFIG, false);
172
173 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
174}
175
176BOOST_AUTO_TEST_CASE(AllDisabled)
177{
178 const std::string CONFIG = R"CONFIG(
179 face_system
180 {
181 websocket
182 {
183 enable_v4 no
184 enable_v6 no
185 }
186 }
187 )CONFIG";
188
189 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
190 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
191}
192
193BOOST_AUTO_TEST_CASE(BadListen)
194{
195 const std::string CONFIG = R"CONFIG(
196 face_system
197 {
198 websocket
199 {
200 listen hello
201 }
202 }
203 )CONFIG";
204
205 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
206 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
207}
208
Davide Pesavento494a9552018-02-04 22:16:05 -0500209BOOST_AUTO_TEST_CASE(BadPort)
210{
211 // not a number
212 const std::string CONFIG1 = R"CONFIG(
213 face_system
214 {
215 websocket
216 {
217 port hello
218 }
219 }
220 )CONFIG";
221
222 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
223 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
224
225 // negative number
226 const std::string CONFIG2 = R"CONFIG(
227 face_system
228 {
229 websocket
230 {
231 port -1
232 }
233 }
234 )CONFIG";
235
236 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
237 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
238
239 // out of range
240 const std::string CONFIG3 = R"CONFIG(
241 face_system
242 {
243 websocket
244 {
245 port 65536
246 }
247 }
248 )CONFIG";
249
250 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
251 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
252}
253
254BOOST_AUTO_TEST_CASE(UnknownOption)
255{
256 const std::string CONFIG = R"CONFIG(
257 face_system
258 {
259 websocket
260 {
261 hello
262 }
263 }
264 )CONFIG";
265
266 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
267 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
268}
269
Junxiao Shi3409cd32017-01-18 15:31:27 +0000270BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700271
272BOOST_AUTO_TEST_CASE(GetChannels)
273{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400274 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700275
Davide Pesaventob15276f2017-07-15 16:27:13 -0400276 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400277 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
278 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
279 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400280 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281}
282
Davide Pesavento096f86a2018-11-10 19:51:45 -0500283BOOST_AUTO_TEST_CASE(CreateChannel)
284{
285 auto channel1 = createChannel("127.0.0.1", "20070");
286 auto channel1a = createChannel("127.0.0.1", "20070");
287 BOOST_CHECK_EQUAL(channel1, channel1a);
288 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "ws://127.0.0.1:20070");
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400289 BOOST_CHECK_EQUAL(factory.getChannels().size(), 1);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500290
291 auto channel2 = createChannel("127.0.0.1", "20071");
292 BOOST_CHECK_NE(channel1, channel2);
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400293 BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500294
295 auto channel3 = createChannel("::1", "20071");
296 BOOST_CHECK_NE(channel2, channel3);
297 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "ws://[::1]:20071");
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400298 BOOST_CHECK_EQUAL(factory.getChannels().size(), 3);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500299}
300
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400301BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700302{
Eric Newberry42602412016-08-27 09:33:18 -0700303 createFace(factory,
304 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000305 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700306 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700307 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700308
Eric Newberry42602412016-08-27 09:33:18 -0700309 createFace(factory,
310 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000311 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700312 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700313 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700314
Eric Newberry42602412016-08-27 09:33:18 -0700315 createFace(factory,
316 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000317 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700318 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700319 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700320}
321
322BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
323BOOST_AUTO_TEST_SUITE_END() // Face
324
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400325} // namespace nfd::tests