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 | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 31 | namespace nfd::tests { |
| 32 | |
| 33 | using face::WebSocketChannel; |
| 34 | using face::WebSocketFactory; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 36 | class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory> |
| 37 | { |
| 38 | protected: |
| 39 | shared_ptr<WebSocketChannel> |
| 40 | createChannel(const std::string& localIp, const std::string& localPort) |
| 41 | { |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame] | 42 | websocket::Endpoint endpoint(boost::asio::ip::address::from_string(localIp), |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 43 | boost::lexical_cast<uint16_t>(localPort)); |
| 44 | return factory.createChannel(endpoint); |
| 45 | } |
| 46 | }; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 48 | BOOST_AUTO_TEST_SUITE(Face) |
| 49 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture) |
| 50 | |
| 51 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 52 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(Defaults) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 54 | { |
| 55 | const std::string CONFIG = R"CONFIG( |
| 56 | face_system |
| 57 | { |
| 58 | websocket |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 59 | } |
| 60 | )CONFIG"; |
| 61 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 62 | parseConfig(CONFIG, true); |
| 63 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 64 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 65 | checkChannelListEqual(factory, {"ws://0.0.0.0:9696", "ws://[::]:9696"}); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 66 | auto channels = factory.getChannels(); |
| 67 | BOOST_CHECK(std::all_of(channels.begin(), channels.end(), |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 68 | [] (const auto& ch) { return ch->isListening(); })); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(DisableListen) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 72 | { |
| 73 | const std::string CONFIG = R"CONFIG( |
| 74 | face_system |
| 75 | { |
| 76 | websocket |
| 77 | { |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 78 | listen no |
| 79 | port 7001 |
| 80 | } |
| 81 | } |
| 82 | )CONFIG"; |
| 83 | |
| 84 | parseConfig(CONFIG, true); |
| 85 | parseConfig(CONFIG, false); |
| 86 | |
| 87 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(DisableV4) |
| 91 | { |
| 92 | const std::string CONFIG = R"CONFIG( |
| 93 | face_system |
| 94 | { |
| 95 | websocket |
| 96 | { |
| 97 | port 7001 |
| 98 | enable_v4 no |
| 99 | enable_v6 yes |
| 100 | } |
| 101 | } |
| 102 | )CONFIG"; |
| 103 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 104 | parseConfig(CONFIG, true); |
| 105 | parseConfig(CONFIG, false); |
| 106 | |
| 107 | checkChannelListEqual(factory, {"ws://[::]:7001"}); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(DisableV6) |
| 111 | { |
| 112 | const std::string CONFIG = R"CONFIG( |
| 113 | face_system |
| 114 | { |
| 115 | websocket |
| 116 | { |
| 117 | port 7001 |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 118 | enable_v4 yes |
| 119 | enable_v6 no |
| 120 | } |
| 121 | } |
| 122 | )CONFIG"; |
| 123 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 124 | parseConfig(CONFIG, true); |
| 125 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 126 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 127 | checkChannelListEqual(factory, {"ws://0.0.0.0:7001"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 130 | BOOST_AUTO_TEST_CASE(ChangePort) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 131 | { |
| 132 | const std::string CONFIG1 = R"CONFIG( |
| 133 | face_system |
| 134 | { |
| 135 | websocket |
| 136 | { |
| 137 | port 9001 |
| 138 | } |
| 139 | } |
| 140 | )CONFIG"; |
| 141 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 142 | parseConfig(CONFIG1, false); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 143 | checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 144 | |
| 145 | const std::string CONFIG2 = R"CONFIG( |
| 146 | face_system |
| 147 | { |
| 148 | websocket |
| 149 | { |
| 150 | port 9002 |
| 151 | } |
| 152 | } |
| 153 | )CONFIG"; |
| 154 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 155 | parseConfig(CONFIG2, false); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 156 | checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001", |
| 157 | "ws://0.0.0.0:9002", "ws://[::]:9002"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 160 | BOOST_AUTO_TEST_CASE(Omitted) |
| 161 | { |
| 162 | const std::string CONFIG = R"CONFIG( |
| 163 | face_system |
| 164 | { |
| 165 | } |
| 166 | )CONFIG"; |
| 167 | |
| 168 | parseConfig(CONFIG, true); |
| 169 | parseConfig(CONFIG, false); |
| 170 | |
| 171 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(AllDisabled) |
| 175 | { |
| 176 | const std::string CONFIG = R"CONFIG( |
| 177 | face_system |
| 178 | { |
| 179 | websocket |
| 180 | { |
| 181 | enable_v4 no |
| 182 | enable_v6 no |
| 183 | } |
| 184 | } |
| 185 | )CONFIG"; |
| 186 | |
| 187 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 188 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 189 | } |
| 190 | |
| 191 | BOOST_AUTO_TEST_CASE(BadListen) |
| 192 | { |
| 193 | const std::string CONFIG = R"CONFIG( |
| 194 | face_system |
| 195 | { |
| 196 | websocket |
| 197 | { |
| 198 | listen hello |
| 199 | } |
| 200 | } |
| 201 | )CONFIG"; |
| 202 | |
| 203 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 204 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 205 | } |
| 206 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 207 | BOOST_AUTO_TEST_CASE(BadPort) |
| 208 | { |
| 209 | // not a number |
| 210 | const std::string CONFIG1 = R"CONFIG( |
| 211 | face_system |
| 212 | { |
| 213 | websocket |
| 214 | { |
| 215 | port hello |
| 216 | } |
| 217 | } |
| 218 | )CONFIG"; |
| 219 | |
| 220 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 221 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
| 222 | |
| 223 | // negative number |
| 224 | const std::string CONFIG2 = R"CONFIG( |
| 225 | face_system |
| 226 | { |
| 227 | websocket |
| 228 | { |
| 229 | port -1 |
| 230 | } |
| 231 | } |
| 232 | )CONFIG"; |
| 233 | |
| 234 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 235 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
| 236 | |
| 237 | // out of range |
| 238 | const std::string CONFIG3 = R"CONFIG( |
| 239 | face_system |
| 240 | { |
| 241 | websocket |
| 242 | { |
| 243 | port 65536 |
| 244 | } |
| 245 | } |
| 246 | )CONFIG"; |
| 247 | |
| 248 | BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error); |
| 249 | BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 253 | { |
| 254 | const std::string CONFIG = R"CONFIG( |
| 255 | face_system |
| 256 | { |
| 257 | websocket |
| 258 | { |
| 259 | hello |
| 260 | } |
| 261 | } |
| 262 | )CONFIG"; |
| 263 | |
| 264 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 265 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 266 | } |
| 267 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 268 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 269 | |
| 270 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 271 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 272 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 273 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 274 | std::set<std::string> expected; |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 275 | expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString()); |
| 276 | expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString()); |
| 277 | expected.insert(createChannel("::1", "20071")->getUri().toString()); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 278 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 281 | BOOST_AUTO_TEST_CASE(CreateChannel) |
| 282 | { |
| 283 | auto channel1 = createChannel("127.0.0.1", "20070"); |
| 284 | auto channel1a = createChannel("127.0.0.1", "20070"); |
| 285 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 286 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "ws://127.0.0.1:20070"); |
| 287 | |
| 288 | auto channel2 = createChannel("127.0.0.1", "20071"); |
| 289 | BOOST_CHECK_NE(channel1, channel2); |
| 290 | |
| 291 | auto channel3 = createChannel("::1", "20071"); |
| 292 | BOOST_CHECK_NE(channel2, channel3); |
| 293 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "ws://[::1]:20071"); |
| 294 | } |
| 295 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 296 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 297 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 298 | createFace(factory, |
| 299 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 300 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 301 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 302 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 303 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 304 | createFace(factory, |
| 305 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 306 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 307 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 308 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 309 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 310 | createFace(factory, |
| 311 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 312 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 313 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 314 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 318 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 319 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 320 | } // namespace nfd::tests |