Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 28 | #include "factory-test-common.hpp" |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 29 | #include "face-system-fixture.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "tests/limited-io.hpp" |
| 31 | |
| 32 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 33 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 34 | namespace tests { |
| 35 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 36 | using namespace nfd::tests; |
| 37 | namespace ip = boost::asio::ip; |
| 38 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Face) |
| 40 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, BaseFixture) |
| 41 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 42 | BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture) |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(Normal) |
| 45 | { |
| 46 | const std::string CONFIG = R"CONFIG( |
| 47 | face_system |
| 48 | { |
| 49 | websocket |
| 50 | { |
| 51 | listen yes |
| 52 | port 9696 |
| 53 | enable_v4 yes |
| 54 | enable_v6 yes |
| 55 | } |
| 56 | } |
| 57 | )CONFIG"; |
| 58 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame^] | 59 | parseConfig(CONFIG, true); |
| 60 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 61 | |
| 62 | auto& factory = this->getFactoryById<WebSocketFactory>("websocket"); |
| 63 | checkChannelListEqual(factory, {"ws://[::]:9696"}); |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(EnableIpv4Only) |
| 67 | { |
| 68 | const std::string CONFIG = R"CONFIG( |
| 69 | face_system |
| 70 | { |
| 71 | websocket |
| 72 | { |
| 73 | listen yes |
| 74 | port 9696 |
| 75 | enable_v4 yes |
| 76 | enable_v6 no |
| 77 | } |
| 78 | } |
| 79 | )CONFIG"; |
| 80 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame^] | 81 | parseConfig(CONFIG, true); |
| 82 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 83 | |
| 84 | auto& factory = this->getFactoryById<WebSocketFactory>("websocket"); |
| 85 | checkChannelListEqual(factory, {"ws://0.0.0.0:9696"}); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(UnsupportedIpv6Only) |
| 89 | { |
| 90 | const std::string CONFIG = R"CONFIG( |
| 91 | face_system |
| 92 | { |
| 93 | websocket |
| 94 | { |
| 95 | listen yes |
| 96 | port 9696 |
| 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(ChannelsDisabled) |
| 108 | { |
| 109 | const std::string CONFIG = R"CONFIG( |
| 110 | face_system |
| 111 | { |
| 112 | websocket |
| 113 | { |
| 114 | listen yes |
| 115 | port 9696 |
| 116 | enable_v4 no |
| 117 | enable_v6 no |
| 118 | } |
| 119 | } |
| 120 | )CONFIG"; |
| 121 | |
| 122 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 123 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(NoListen) |
| 127 | { |
| 128 | const std::string CONFIG = R"CONFIG( |
| 129 | face_system |
| 130 | { |
| 131 | websocket |
| 132 | { |
| 133 | listen no |
| 134 | port 9696 |
| 135 | enable_v4 yes |
| 136 | enable_v6 yes |
| 137 | } |
| 138 | } |
| 139 | )CONFIG"; |
| 140 | |
| 141 | auto& factory = this->getFactoryById<WebSocketFactory>("websocket"); |
| 142 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_CASE(ChangeEndpoint) |
| 146 | { |
| 147 | const std::string CONFIG1 = R"CONFIG( |
| 148 | face_system |
| 149 | { |
| 150 | websocket |
| 151 | { |
| 152 | port 9001 |
| 153 | } |
| 154 | } |
| 155 | )CONFIG"; |
| 156 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame^] | 157 | parseConfig(CONFIG1, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 158 | auto& factory = this->getFactoryById<WebSocketFactory>("websocket"); |
| 159 | checkChannelListEqual(factory, {"ws://[::]:9001"}); |
| 160 | |
| 161 | const std::string CONFIG2 = R"CONFIG( |
| 162 | face_system |
| 163 | { |
| 164 | websocket |
| 165 | { |
| 166 | port 9002 |
| 167 | } |
| 168 | } |
| 169 | )CONFIG"; |
| 170 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame^] | 171 | parseConfig(CONFIG2, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 172 | checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"}); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 176 | |
| 177 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 178 | { |
| 179 | WebSocketFactory factory; |
| 180 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 181 | |
| 182 | std::vector<shared_ptr<const Channel>> expectedChannels; |
| 183 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 184 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 185 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 186 | |
| 187 | for (const auto& i : factory.getChannels()) { |
| 188 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i); |
| 189 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 190 | expectedChannels.erase(pos); |
| 191 | } |
| 192 | |
| 193 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 197 | { |
| 198 | WebSocketFactory factory; |
| 199 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 200 | createFace(factory, |
| 201 | FaceUri("ws://127.0.0.1:20070"), |
| 202 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 203 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 204 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 205 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 206 | createFace(factory, |
| 207 | FaceUri("ws://127.0.0.1:20070"), |
| 208 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [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"), |
| 214 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 215 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 216 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 220 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 221 | |
| 222 | } // namespace tests |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 223 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 224 | } // namespace nfd |