Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 4 | * 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 Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 28 | #include "face-system-fixture.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 29 | #include "factory-test-common.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 32 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 35 | class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory> |
| 36 | { |
| 37 | protected: |
| 38 | shared_ptr<WebSocketChannel> |
| 39 | createChannel(const std::string& localIp, const std::string& localPort) |
| 40 | { |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame^] | 41 | websocket::Endpoint endpoint(boost::asio::ip::address::from_string(localIp), |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 42 | boost::lexical_cast<uint16_t>(localPort)); |
| 43 | return factory.createChannel(endpoint); |
| 44 | } |
| 45 | }; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 46 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 47 | BOOST_AUTO_TEST_SUITE(Face) |
| 48 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture) |
| 49 | |
| 50 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 51 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 52 | BOOST_AUTO_TEST_CASE(Defaults) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 53 | { |
| 54 | const std::string CONFIG = R"CONFIG( |
| 55 | face_system |
| 56 | { |
| 57 | websocket |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 58 | } |
| 59 | )CONFIG"; |
| 60 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 61 | parseConfig(CONFIG, true); |
| 62 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 63 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 64 | checkChannelListEqual(factory, {"ws://[::]:9696"}); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 65 | 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 Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 70 | BOOST_AUTO_TEST_CASE(DisableListen) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 71 | { |
| 72 | const std::string CONFIG = R"CONFIG( |
| 73 | face_system |
| 74 | { |
| 75 | websocket |
| 76 | { |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 77 | 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 | |
| 89 | BOOST_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 | |
| 107 | BOOST_AUTO_TEST_CASE(DisableV6) |
| 108 | { |
| 109 | const std::string CONFIG = R"CONFIG( |
| 110 | face_system |
| 111 | { |
| 112 | websocket |
| 113 | { |
| 114 | port 7001 |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 115 | enable_v4 yes |
| 116 | enable_v6 no |
| 117 | } |
| 118 | } |
| 119 | )CONFIG"; |
| 120 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 121 | parseConfig(CONFIG, true); |
| 122 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 123 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 124 | checkChannelListEqual(factory, {"ws://0.0.0.0:7001"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | BOOST_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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 139 | parseConfig(CONFIG1, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 140 | 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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 152 | parseConfig(CONFIG2, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 153 | checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"}); |
| 154 | } |
| 155 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 156 | BOOST_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 | |
| 170 | BOOST_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 | |
| 187 | BOOST_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 | |
| 203 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489 |
| 204 | BOOST_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 | |
| 249 | BOOST_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 Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 265 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 266 | |
| 267 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 268 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 270 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 271 | std::set<std::string> expected; |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 272 | 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 Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 275 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 278 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 279 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 280 | createFace(factory, |
| 281 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 282 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 283 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 284 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 285 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 286 | createFace(factory, |
| 287 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 288 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 289 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 290 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 291 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 292 | createFace(factory, |
| 293 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 294 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 295 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 296 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 300 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 301 | |
| 302 | } // namespace tests |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 303 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 304 | } // namespace nfd |