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