blob: 47b1a0f01becdc3f1aeceb84a45e63d1969fce62 [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/*
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
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
Junxiao Shi3409cd32017-01-18 15:31:27 +000035namespace ip = boost::asio::ip;
36
Junxiao Shi0ba6d642017-07-17 00:53:22 +000037using WebSocketFactoryFixture = FaceSystemFactoryFixture<WebSocketFactory>;
Junxiao Shicde37ad2015-12-24 01:02:05 -070038
Junxiao Shi0ba6d642017-07-17 00:53:22 +000039BOOST_AUTO_TEST_SUITE(Face)
40BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture)
41
42BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi3409cd32017-01-18 15:31:27 +000043
44BOOST_AUTO_TEST_CASE(Normal)
45{
46 const std::string CONFIG = R"CONFIG(
47 face_system
48 {
49 websocket
50 {
51 listen yes
52 port 9696
53 enable_v4 yes
54 enable_v6 yes
55 }
56 }
57 )CONFIG";
58
Junxiao Shi1b65ca12017-01-21 23:04:41 +000059 parseConfig(CONFIG, true);
60 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000061
Junxiao Shi3409cd32017-01-18 15:31:27 +000062 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
Junxiao Shi3409cd32017-01-18 15:31:27 +000083 checkChannelListEqual(factory, {"ws://0.0.0.0:9696"});
84}
85
86BOOST_AUTO_TEST_CASE(UnsupportedIpv6Only)
87{
88 const std::string CONFIG = R"CONFIG(
89 face_system
90 {
91 websocket
92 {
93 listen yes
94 port 9696
95 enable_v4 no
96 enable_v6 yes
97 }
98 }
99 )CONFIG";
100
101 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
102 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
103}
104
105BOOST_AUTO_TEST_CASE(ChannelsDisabled)
106{
107 const std::string CONFIG = R"CONFIG(
108 face_system
109 {
110 websocket
111 {
112 listen yes
113 port 9696
114 enable_v4 no
115 enable_v6 no
116 }
117 }
118 )CONFIG";
119
120 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
121 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
122}
123
124BOOST_AUTO_TEST_CASE(NoListen)
125{
126 const std::string CONFIG = R"CONFIG(
127 face_system
128 {
129 websocket
130 {
131 listen no
132 port 9696
133 enable_v4 yes
134 enable_v6 yes
135 }
136 }
137 )CONFIG";
138
Junxiao Shi3409cd32017-01-18 15:31:27 +0000139 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
140}
141
142BOOST_AUTO_TEST_CASE(ChangeEndpoint)
143{
144 const std::string CONFIG1 = R"CONFIG(
145 face_system
146 {
147 websocket
148 {
149 port 9001
150 }
151 }
152 )CONFIG";
153
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000154 parseConfig(CONFIG1, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000155 checkChannelListEqual(factory, {"ws://[::]:9001"});
156
157 const std::string CONFIG2 = R"CONFIG(
158 face_system
159 {
160 websocket
161 {
162 port 9002
163 }
164 }
165 )CONFIG";
166
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000167 parseConfig(CONFIG2, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000168 checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"});
169}
170
171BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700172
173BOOST_AUTO_TEST_CASE(GetChannels)
174{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400175 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700176
Davide Pesaventob15276f2017-07-15 16:27:13 -0400177 std::set<std::string> expected;
178 expected.insert(factory.createChannel("127.0.0.1", "20070")->getUri().toString());
179 expected.insert(factory.createChannel("127.0.0.1", "20071")->getUri().toString());
180 expected.insert(factory.createChannel("::1", "20071")->getUri().toString());
181 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700182}
183
Davide Pesaventob15276f2017-07-15 16:27:13 -0400184BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700185{
Eric Newberry42602412016-08-27 09:33:18 -0700186 createFace(factory,
187 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000188 {},
Eric Newberry42602412016-08-27 09:33:18 -0700189 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700190 false,
Eric Newberry42602412016-08-27 09:33:18 -0700191 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192
Eric Newberry42602412016-08-27 09:33:18 -0700193 createFace(factory,
194 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000195 {},
Eric Newberry42602412016-08-27 09:33:18 -0700196 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700197 false,
Eric Newberry42602412016-08-27 09:33:18 -0700198 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700199
Eric Newberry42602412016-08-27 09:33:18 -0700200 createFace(factory,
201 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000202 {},
Eric Newberry42602412016-08-27 09:33:18 -0700203 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700204 false,
Eric Newberry42602412016-08-27 09:33:18 -0700205 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700206}
207
208BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory
209BOOST_AUTO_TEST_SUITE_END() // Face
210
211} // namespace tests
Junxiao Shi3409cd32017-01-18 15:31:27 +0000212} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213} // namespace nfd