blob: d813931de5a2891464d261484fbe3251a581b340 [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
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040031#include <ndn-cxx/net/address-converter.hpp>
32
Junxiao Shicde37ad2015-12-24 01:02:05 -070033namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
36
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040037class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory>
38{
39protected:
40 shared_ptr<WebSocketChannel>
41 createChannel(const std::string& localIp, const std::string& localPort)
42 {
43 websocket::Endpoint endpoint(ndn::ip::addressFromString(localIp),
44 boost::lexical_cast<uint16_t>(localPort));
45 return factory.createChannel(endpoint);
46 }
47};
Junxiao Shicde37ad2015-12-24 01:02:05 -070048
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049BOOST_AUTO_TEST_SUITE(Face)
50BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture)
51
52BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi3409cd32017-01-18 15:31:27 +000053
54BOOST_AUTO_TEST_CASE(Normal)
55{
56 const std::string CONFIG = R"CONFIG(
57 face_system
58 {
59 websocket
60 {
61 listen yes
62 port 9696
63 enable_v4 yes
64 enable_v6 yes
65 }
66 }
67 )CONFIG";
68
Junxiao Shi1b65ca12017-01-21 23:04:41 +000069 parseConfig(CONFIG, true);
70 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000071
Junxiao Shi3409cd32017-01-18 15:31:27 +000072 checkChannelListEqual(factory, {"ws://[::]:9696"});
73}
74
75BOOST_AUTO_TEST_CASE(EnableIpv4Only)
76{
77 const std::string CONFIG = R"CONFIG(
78 face_system
79 {
80 websocket
81 {
82 listen yes
83 port 9696
84 enable_v4 yes
85 enable_v6 no
86 }
87 }
88 )CONFIG";
89
Junxiao Shi1b65ca12017-01-21 23:04:41 +000090 parseConfig(CONFIG, true);
91 parseConfig(CONFIG, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +000092
Junxiao Shi3409cd32017-01-18 15:31:27 +000093 checkChannelListEqual(factory, {"ws://0.0.0.0:9696"});
94}
95
96BOOST_AUTO_TEST_CASE(UnsupportedIpv6Only)
97{
98 const std::string CONFIG = R"CONFIG(
99 face_system
100 {
101 websocket
102 {
103 listen yes
104 port 9696
105 enable_v4 no
106 enable_v6 yes
107 }
108 }
109 )CONFIG";
110
111 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
112 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
113}
114
115BOOST_AUTO_TEST_CASE(ChannelsDisabled)
116{
117 const std::string CONFIG = R"CONFIG(
118 face_system
119 {
120 websocket
121 {
122 listen yes
123 port 9696
124 enable_v4 no
125 enable_v6 no
126 }
127 }
128 )CONFIG";
129
130 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
131 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
132}
133
134BOOST_AUTO_TEST_CASE(NoListen)
135{
136 const std::string CONFIG = R"CONFIG(
137 face_system
138 {
139 websocket
140 {
141 listen no
142 port 9696
143 enable_v4 yes
144 enable_v6 yes
145 }
146 }
147 )CONFIG";
148
Junxiao Shi3409cd32017-01-18 15:31:27 +0000149 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
150}
151
152BOOST_AUTO_TEST_CASE(ChangeEndpoint)
153{
154 const std::string CONFIG1 = R"CONFIG(
155 face_system
156 {
157 websocket
158 {
159 port 9001
160 }
161 }
162 )CONFIG";
163
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000164 parseConfig(CONFIG1, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000165 checkChannelListEqual(factory, {"ws://[::]:9001"});
166
167 const std::string CONFIG2 = R"CONFIG(
168 face_system
169 {
170 websocket
171 {
172 port 9002
173 }
174 }
175 )CONFIG";
176
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000177 parseConfig(CONFIG2, false);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000178 checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"});
179}
180
181BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
Junxiao Shicde37ad2015-12-24 01:02:05 -0700182
183BOOST_AUTO_TEST_CASE(GetChannels)
184{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400185 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700186
Davide Pesaventob15276f2017-07-15 16:27:13 -0400187 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400188 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
189 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
190 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400191 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192}
193
Davide Pesaventob15276f2017-07-15 16:27:13 -0400194BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700195{
Eric Newberry42602412016-08-27 09:33:18 -0700196 createFace(factory,
197 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000198 {},
Eric Newberry42602412016-08-27 09:33:18 -0700199 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700200 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400201 false,
Eric Newberry42602412016-08-27 09:33:18 -0700202 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700203
Eric Newberry42602412016-08-27 09:33:18 -0700204 createFace(factory,
205 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000206 {},
Eric Newberry42602412016-08-27 09:33:18 -0700207 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700208 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400209 false,
Eric Newberry42602412016-08-27 09:33:18 -0700210 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700211
Eric Newberry42602412016-08-27 09:33:18 -0700212 createFace(factory,
213 FaceUri("ws://127.0.0.1:20070"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000214 {},
Eric Newberry42602412016-08-27 09:33:18 -0700215 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700216 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400217 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