blob: 5243c97c3cb7b4275e445f1adc18ab16a5ec279a [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3409cd32017-01-18 15:31:27 +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/websocket-factory.hpp"
27
Eric Newberry42602412016-08-27 09:33:18 -070028#include "factory-test-common.hpp"
Junxiao Shi3409cd32017-01-18 15:31:27 +000029#include "face-system-fixture.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
31
32namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000033namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace tests {
35
Junxiao Shi3409cd32017-01-18 15:31:27 +000036namespace ip = boost::asio::ip;
37
Junxiao Shicde37ad2015-12-24 01:02:05 -070038BOOST_AUTO_TEST_SUITE(Face)
39BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, BaseFixture)
40
Junxiao Shi3409cd32017-01-18 15:31:27 +000041BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture)
42
43BOOST_AUTO_TEST_CASE(Normal)
44{
45 const std::string CONFIG = R"CONFIG(
46 face_system
47 {
48 websocket
49 {
50 listen yes
51 port 9696
52 enable_v4 yes
53 enable_v6 yes
54 }
55 }
56 )CONFIG";
57
Junxiao Shi1b65ca12017-01-21 23:04:41 +000058 parseConfig(CONFIG, true);
59 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000060
61 auto& factory = this->getFactoryById<WebSocketFactory>("websocket");
62 checkChannelListEqual(factory, {"ws://[::]:9696"});
63}
64
65BOOST_AUTO_TEST_CASE(EnableIpv4Only)
66{
67 const std::string CONFIG = R"CONFIG(
68 face_system
69 {
70 websocket
71 {
72 listen yes
73 port 9696
74 enable_v4 yes
75 enable_v6 no
76 }
77 }
78 )CONFIG";
79
Junxiao Shi1b65ca12017-01-21 23:04:41 +000080 parseConfig(CONFIG, true);
81 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000082
83 auto& factory = this->getFactoryById<WebSocketFactory>("websocket");
84 checkChannelListEqual(factory, {"ws://0.0.0.0:9696"});
85}
86
87BOOST_AUTO_TEST_CASE(UnsupportedIpv6Only)
88{
89 const std::string CONFIG = R"CONFIG(
90 face_system
91 {
92 websocket
93 {
94 listen yes
95 port 9696
96 enable_v4 no
97 enable_v6 yes
98 }
99 }
100 )CONFIG";
101
102 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
103 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
104}
105
106BOOST_AUTO_TEST_CASE(ChannelsDisabled)
107{
108 const std::string CONFIG = R"CONFIG(
109 face_system
110 {
111 websocket
112 {
113 listen yes
114 port 9696
115 enable_v4 no
116 enable_v6 no
117 }
118 }
119 )CONFIG";
120
121 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
122 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
123}
124
125BOOST_AUTO_TEST_CASE(NoListen)
126{
127 const std::string CONFIG = R"CONFIG(
128 face_system
129 {
130 websocket
131 {
132 listen no
133 port 9696
134 enable_v4 yes
135 enable_v6 yes
136 }
137 }
138 )CONFIG";
139
140 auto& factory = this->getFactoryById<WebSocketFactory>("websocket");
141 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
142}
143
144BOOST_AUTO_TEST_CASE(ChangeEndpoint)
145{
146 const std::string CONFIG1 = R"CONFIG(
147 face_system
148 {
149 websocket
150 {
151 port 9001
152 }
153 }
154 )CONFIG";
155
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000156 parseConfig(CONFIG1, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000157 auto& factory = this->getFactoryById<WebSocketFactory>("websocket");
158 checkChannelListEqual(factory, {"ws://[::]:9001"});
159
160 const std::string CONFIG2 = R"CONFIG(
161 face_system
162 {
163 websocket
164 {
165 port 9002
166 }
167 }
168 )CONFIG";
169
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000170 parseConfig(CONFIG2, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000171 checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"});
172}
173
174BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700175
176BOOST_AUTO_TEST_CASE(GetChannels)
177{
178 WebSocketFactory factory;
179 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
180
181 std::vector<shared_ptr<const Channel>> expectedChannels;
182 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
183 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
184 expectedChannels.push_back(factory.createChannel("::1", "20071"));
185
186 for (const auto& i : factory.getChannels()) {
187 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
188 BOOST_REQUIRE(pos != expectedChannels.end());
189 expectedChannels.erase(pos);
190 }
191
192 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
193}
194
195BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
196{
197 WebSocketFactory factory;
198
Eric Newberry42602412016-08-27 09:33:18 -0700199 createFace(factory,
200 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000201 {},
Eric Newberry42602412016-08-27 09:33:18 -0700202 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700203 false,
Eric Newberry42602412016-08-27 09:33:18 -0700204 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700205
Eric Newberry42602412016-08-27 09:33:18 -0700206 createFace(factory,
207 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000208 {},
Eric Newberry42602412016-08-27 09:33:18 -0700209 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700210 false,
Eric Newberry42602412016-08-27 09:33:18 -0700211 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700212
Eric Newberry42602412016-08-27 09:33:18 -0700213 createFace(factory,
214 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000215 {},
Eric Newberry42602412016-08-27 09:33:18 -0700216 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700217 false,
Eric Newberry42602412016-08-27 09:33:18 -0700218 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700219}
220
221BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
222BOOST_AUTO_TEST_SUITE_END() // Face
223
224} // namespace tests
Junxiao Shi3409cd32017-01-18 15:31:27 +0000225} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700226} // namespace nfd