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 | /* |
Eric Newberry | cb27d91 | 2020-04-08 19:38:18 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 64 | checkChannelListEqual(factory, {"ws://0.0.0.0:9696", "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(), |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 67 | [] (const auto& 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 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 103 | parseConfig(CONFIG, true); |
| 104 | parseConfig(CONFIG, false); |
| 105 | |
| 106 | checkChannelListEqual(factory, {"ws://[::]:7001"}); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(DisableV6) |
| 110 | { |
| 111 | const std::string CONFIG = R"CONFIG( |
| 112 | face_system |
| 113 | { |
| 114 | websocket |
| 115 | { |
| 116 | port 7001 |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 117 | enable_v4 yes |
| 118 | enable_v6 no |
| 119 | } |
| 120 | } |
| 121 | )CONFIG"; |
| 122 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 123 | parseConfig(CONFIG, true); |
| 124 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 125 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 126 | checkChannelListEqual(factory, {"ws://0.0.0.0:7001"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(ChangePort) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 130 | { |
| 131 | const std::string CONFIG1 = R"CONFIG( |
| 132 | face_system |
| 133 | { |
| 134 | websocket |
| 135 | { |
| 136 | port 9001 |
| 137 | } |
| 138 | } |
| 139 | )CONFIG"; |
| 140 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 141 | parseConfig(CONFIG1, false); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 142 | checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 143 | |
| 144 | const std::string CONFIG2 = R"CONFIG( |
| 145 | face_system |
| 146 | { |
| 147 | websocket |
| 148 | { |
| 149 | port 9002 |
| 150 | } |
| 151 | } |
| 152 | )CONFIG"; |
| 153 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 154 | parseConfig(CONFIG2, false); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 155 | checkChannelListEqual(factory, {"ws://0.0.0.0:9001", "ws://[::]:9001", |
| 156 | "ws://0.0.0.0:9002", "ws://[::]:9002"}); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 159 | BOOST_AUTO_TEST_CASE(Omitted) |
| 160 | { |
| 161 | const std::string CONFIG = R"CONFIG( |
| 162 | face_system |
| 163 | { |
| 164 | } |
| 165 | )CONFIG"; |
| 166 | |
| 167 | parseConfig(CONFIG, true); |
| 168 | parseConfig(CONFIG, false); |
| 169 | |
| 170 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(AllDisabled) |
| 174 | { |
| 175 | const std::string CONFIG = R"CONFIG( |
| 176 | face_system |
| 177 | { |
| 178 | websocket |
| 179 | { |
| 180 | enable_v4 no |
| 181 | enable_v6 no |
| 182 | } |
| 183 | } |
| 184 | )CONFIG"; |
| 185 | |
| 186 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 187 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(BadListen) |
| 191 | { |
| 192 | const std::string CONFIG = R"CONFIG( |
| 193 | face_system |
| 194 | { |
| 195 | websocket |
| 196 | { |
| 197 | listen hello |
| 198 | } |
| 199 | } |
| 200 | )CONFIG"; |
| 201 | |
| 202 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 203 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 204 | } |
| 205 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 206 | BOOST_AUTO_TEST_CASE(BadPort) |
| 207 | { |
| 208 | // not a number |
| 209 | const std::string CONFIG1 = R"CONFIG( |
| 210 | face_system |
| 211 | { |
| 212 | websocket |
| 213 | { |
| 214 | port hello |
| 215 | } |
| 216 | } |
| 217 | )CONFIG"; |
| 218 | |
| 219 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 220 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
| 221 | |
| 222 | // negative number |
| 223 | const std::string CONFIG2 = R"CONFIG( |
| 224 | face_system |
| 225 | { |
| 226 | websocket |
| 227 | { |
| 228 | port -1 |
| 229 | } |
| 230 | } |
| 231 | )CONFIG"; |
| 232 | |
| 233 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 234 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
| 235 | |
| 236 | // out of range |
| 237 | const std::string CONFIG3 = R"CONFIG( |
| 238 | face_system |
| 239 | { |
| 240 | websocket |
| 241 | { |
| 242 | port 65536 |
| 243 | } |
| 244 | } |
| 245 | )CONFIG"; |
| 246 | |
| 247 | BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error); |
| 248 | BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error); |
| 249 | } |
| 250 | |
| 251 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 252 | { |
| 253 | const std::string CONFIG = R"CONFIG( |
| 254 | face_system |
| 255 | { |
| 256 | websocket |
| 257 | { |
| 258 | hello |
| 259 | } |
| 260 | } |
| 261 | )CONFIG"; |
| 262 | |
| 263 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 264 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 265 | } |
| 266 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 267 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 268 | |
| 269 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 270 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 272 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 273 | std::set<std::string> expected; |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 274 | expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString()); |
| 275 | expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString()); |
| 276 | expected.insert(createChannel("::1", "20071")->getUri().toString()); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 277 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 280 | BOOST_AUTO_TEST_CASE(CreateChannel) |
| 281 | { |
| 282 | auto channel1 = createChannel("127.0.0.1", "20070"); |
| 283 | auto channel1a = createChannel("127.0.0.1", "20070"); |
| 284 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 285 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "ws://127.0.0.1:20070"); |
| 286 | |
| 287 | auto channel2 = createChannel("127.0.0.1", "20071"); |
| 288 | BOOST_CHECK_NE(channel1, channel2); |
| 289 | |
| 290 | auto channel3 = createChannel("::1", "20071"); |
| 291 | BOOST_CHECK_NE(channel2, channel3); |
| 292 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "ws://[::1]:20071"); |
| 293 | } |
| 294 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 295 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 296 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 297 | createFace(factory, |
| 298 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 299 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 300 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 301 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 302 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 303 | createFace(factory, |
| 304 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 305 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 306 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 307 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 308 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 309 | createFace(factory, |
| 310 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 311 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 312 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 313 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 317 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 318 | |
| 319 | } // namespace tests |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 320 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 321 | } // namespace nfd |