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 | /* |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/net/address-converter.hpp> |
| 32 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 34 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 35 | namespace tests { |
| 36 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 37 | class WebSocketFactoryFixture : public FaceSystemFactoryFixture<WebSocketFactory> |
| 38 | { |
| 39 | protected: |
| 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 49 | BOOST_AUTO_TEST_SUITE(Face) |
| 50 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture) |
| 51 | |
| 52 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 53 | |
| 54 | BOOST_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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 69 | parseConfig(CONFIG, true); |
| 70 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 71 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 72 | checkChannelListEqual(factory, {"ws://[::]:9696"}); |
| 73 | } |
| 74 | |
| 75 | BOOST_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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 90 | parseConfig(CONFIG, true); |
| 91 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 92 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 93 | checkChannelListEqual(factory, {"ws://0.0.0.0:9696"}); |
| 94 | } |
| 95 | |
| 96 | BOOST_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 | |
| 115 | BOOST_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 | |
| 134 | BOOST_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 Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 150 | } |
| 151 | |
| 152 | BOOST_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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 164 | parseConfig(CONFIG1, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 165 | 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 Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 177 | parseConfig(CONFIG2, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 178 | checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"}); |
| 179 | } |
| 180 | |
| 181 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 182 | |
| 183 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 184 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 186 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 187 | std::set<std::string> expected; |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 188 | 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 Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 191 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 194 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 195 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 196 | createFace(factory, |
| 197 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 198 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 199 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 200 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 201 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 202 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 203 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 204 | createFace(factory, |
| 205 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 206 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 207 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 208 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 209 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 210 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 211 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 212 | createFace(factory, |
| 213 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 214 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 215 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 216 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 217 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 218 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 222 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 223 | |
| 224 | } // namespace tests |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 225 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 226 | } // namespace nfd |