blob: bedef87e81f1a9a80ffc601e963e62d1e43a7825 [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
Junxiao Shi3409cd32017-01-18 15:31:27 +000064 checkChannelListEqual(factory, {"ws://[::]:9696"});
Davide Pesavento494a9552018-02-04 22:16:05 -050065 auto channels = factory.getChannels();
66 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
67 [] (const shared_ptr<const Channel>& 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
103 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
104 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
105}
106
107BOOST_AUTO_TEST_CASE(DisableV6)
108{
109 const std::string CONFIG = R"CONFIG(
110 face_system
111 {
112 websocket
113 {
114 port 7001
Junxiao Shi3409cd32017-01-18 15:31:27 +0000115 enable_v4 yes
116 enable_v6 no
117 }
118 }
119 )CONFIG";
120
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000121 parseConfig(CONFIG, true);
122 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000123
Davide Pesavento494a9552018-02-04 22:16:05 -0500124 checkChannelListEqual(factory, {"ws://0.0.0.0:7001"});
Junxiao Shi3409cd32017-01-18 15:31:27 +0000125}
126
127BOOST_AUTO_TEST_CASE(ChangeEndpoint)
128{
129 const std::string CONFIG1 = R"CONFIG(
130 face_system
131 {
132 websocket
133 {
134 port 9001
135 }
136 }
137 )CONFIG";
138
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000139 parseConfig(CONFIG1, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000140 checkChannelListEqual(factory, {"ws://[::]:9001"});
141
142 const std::string CONFIG2 = R"CONFIG(
143 face_system
144 {
145 websocket
146 {
147 port 9002
148 }
149 }
150 )CONFIG";
151
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000152 parseConfig(CONFIG2, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000153 checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"});
154}
155
Davide Pesavento494a9552018-02-04 22:16:05 -0500156BOOST_AUTO_TEST_CASE(Omitted)
157{
158 const std::string CONFIG = R"CONFIG(
159 face_system
160 {
161 }
162 )CONFIG";
163
164 parseConfig(CONFIG, true);
165 parseConfig(CONFIG, false);
166
167 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
168}
169
170BOOST_AUTO_TEST_CASE(AllDisabled)
171{
172 const std::string CONFIG = R"CONFIG(
173 face_system
174 {
175 websocket
176 {
177 enable_v4 no
178 enable_v6 no
179 }
180 }
181 )CONFIG";
182
183 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
184 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
185}
186
187BOOST_AUTO_TEST_CASE(BadListen)
188{
189 const std::string CONFIG = R"CONFIG(
190 face_system
191 {
192 websocket
193 {
194 listen hello
195 }
196 }
197 )CONFIG";
198
199 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
200 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
201}
202
203BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489
204BOOST_AUTO_TEST_CASE(BadPort)
205{
206 // not a number
207 const std::string CONFIG1 = R"CONFIG(
208 face_system
209 {
210 websocket
211 {
212 port hello
213 }
214 }
215 )CONFIG";
216
217 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
218 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
219
220 // negative number
221 const std::string CONFIG2 = R"CONFIG(
222 face_system
223 {
224 websocket
225 {
226 port -1
227 }
228 }
229 )CONFIG";
230
231 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
232 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
233
234 // out of range
235 const std::string CONFIG3 = R"CONFIG(
236 face_system
237 {
238 websocket
239 {
240 port 65536
241 }
242 }
243 )CONFIG";
244
245 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
246 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
247}
248
249BOOST_AUTO_TEST_CASE(UnknownOption)
250{
251 const std::string CONFIG = R"CONFIG(
252 face_system
253 {
254 websocket
255 {
256 hello
257 }
258 }
259 )CONFIG";
260
261 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
262 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
263}
264
Junxiao Shi3409cd32017-01-18 15:31:27 +0000265BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700266
267BOOST_AUTO_TEST_CASE(GetChannels)
268{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400269 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700270
Davide Pesaventob15276f2017-07-15 16:27:13 -0400271 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400272 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
273 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
274 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400275 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700276}
277
Davide Pesaventob15276f2017-07-15 16:27:13 -0400278BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700279{
Eric Newberry42602412016-08-27 09:33:18 -0700280 createFace(factory,
281 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000282 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700283 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700284 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700285
Eric Newberry42602412016-08-27 09:33:18 -0700286 createFace(factory,
287 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000288 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700289 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700290 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700291
Eric Newberry42602412016-08-27 09:33:18 -0700292 createFace(factory,
293 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000294 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700295 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700296 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700297}
298
299BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
300BOOST_AUTO_TEST_SUITE_END() // Face
301
302} // namespace tests
Junxiao Shi3409cd32017-01-18 15:31:27 +0000303} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700304} // namespace nfd