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 | |
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 | namespace ip = boost::asio::ip; |
| 37 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 38 | using WebSocketFactoryFixture = FaceSystemFactoryFixture<WebSocketFactory>; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 40 | BOOST_AUTO_TEST_SUITE(Face) |
| 41 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketFactory, WebSocketFactoryFixture) |
| 42 | |
| 43 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 44 | |
| 45 | BOOST_AUTO_TEST_CASE(Normal) |
| 46 | { |
| 47 | const std::string CONFIG = R"CONFIG( |
| 48 | face_system |
| 49 | { |
| 50 | websocket |
| 51 | { |
| 52 | listen yes |
| 53 | port 9696 |
| 54 | enable_v4 yes |
| 55 | enable_v6 yes |
| 56 | } |
| 57 | } |
| 58 | )CONFIG"; |
| 59 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 60 | parseConfig(CONFIG, true); |
| 61 | parseConfig(CONFIG, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 62 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 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 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 84 | checkChannelListEqual(factory, {"ws://0.0.0.0:9696"}); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(UnsupportedIpv6Only) |
| 88 | { |
| 89 | const std::string CONFIG = R"CONFIG( |
| 90 | face_system |
| 91 | { |
| 92 | websocket |
| 93 | { |
| 94 | listen yes |
| 95 | port 9696 |
| 96 | enable_v4 no |
| 97 | enable_v6 yes |
| 98 | } |
| 99 | } |
| 100 | )CONFIG"; |
| 101 | |
| 102 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 103 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_CASE(ChannelsDisabled) |
| 107 | { |
| 108 | const std::string CONFIG = R"CONFIG( |
| 109 | face_system |
| 110 | { |
| 111 | websocket |
| 112 | { |
| 113 | listen yes |
| 114 | port 9696 |
| 115 | enable_v4 no |
| 116 | enable_v6 no |
| 117 | } |
| 118 | } |
| 119 | )CONFIG"; |
| 120 | |
| 121 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 122 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(NoListen) |
| 126 | { |
| 127 | const std::string CONFIG = R"CONFIG( |
| 128 | face_system |
| 129 | { |
| 130 | websocket |
| 131 | { |
| 132 | listen no |
| 133 | port 9696 |
| 134 | enable_v4 yes |
| 135 | enable_v6 yes |
| 136 | } |
| 137 | } |
| 138 | )CONFIG"; |
| 139 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(ChangeEndpoint) |
| 144 | { |
| 145 | const std::string CONFIG1 = R"CONFIG( |
| 146 | face_system |
| 147 | { |
| 148 | websocket |
| 149 | { |
| 150 | port 9001 |
| 151 | } |
| 152 | } |
| 153 | )CONFIG"; |
| 154 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 155 | parseConfig(CONFIG1, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 156 | checkChannelListEqual(factory, {"ws://[::]:9001"}); |
| 157 | |
| 158 | const std::string CONFIG2 = R"CONFIG( |
| 159 | face_system |
| 160 | { |
| 161 | websocket |
| 162 | { |
| 163 | port 9002 |
| 164 | } |
| 165 | } |
| 166 | )CONFIG"; |
| 167 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 168 | parseConfig(CONFIG2, false); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 169 | checkChannelListEqual(factory, {"ws://[::]:9001", "ws://[::]:9002"}); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 173 | |
| 174 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 175 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 177 | |
| 178 | std::vector<shared_ptr<const Channel>> expectedChannels; |
| 179 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 180 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 181 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 182 | |
| 183 | for (const auto& i : factory.getChannels()) { |
| 184 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i); |
| 185 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 186 | expectedChannels.erase(pos); |
| 187 | } |
| 188 | |
| 189 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 193 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 194 | createFace(factory, |
| 195 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 196 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 197 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 198 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 199 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 200 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 201 | createFace(factory, |
| 202 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 203 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 204 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 205 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 206 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 207 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 208 | createFace(factory, |
| 209 | FaceUri("ws://127.0.0.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 210 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 211 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 212 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 213 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketFactory |
| 217 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 218 | |
| 219 | } // namespace tests |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 220 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 221 | } // namespace nfd |