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 | 38b24c7 | 2017-01-05 02:59:31 +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/tcp-factory.hpp" |
| 27 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +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 | #include "tests/limited-io.hpp" |
| 31 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 32 | #include <ndn-cxx/net/address-converter.hpp> |
| 33 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 35 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 36 | namespace tests { |
| 37 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 38 | class TcpFactoryFixture : public FaceSystemFactoryFixture<TcpFactory> |
| 39 | { |
| 40 | protected: |
| 41 | shared_ptr<TcpChannel> |
| 42 | createChannel(const std::string& localIp, const std::string& localPort) |
| 43 | { |
| 44 | tcp::Endpoint endpoint(ndn::ip::addressFromString(localIp), |
| 45 | boost::lexical_cast<uint16_t>(localPort)); |
| 46 | return factory.createChannel(endpoint); |
| 47 | } |
| 48 | }; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 49 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 50 | BOOST_AUTO_TEST_SUITE(Face) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 51 | BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, TcpFactoryFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 52 | |
| 53 | using nfd::Face; |
| 54 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 55 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 56 | |
| 57 | BOOST_AUTO_TEST_CASE(Normal) |
| 58 | { |
| 59 | const std::string CONFIG = R"CONFIG( |
| 60 | face_system |
| 61 | { |
| 62 | tcp |
| 63 | { |
| 64 | listen yes |
| 65 | port 16363 |
| 66 | enable_v4 yes |
| 67 | enable_v6 yes |
| 68 | } |
| 69 | } |
| 70 | )CONFIG"; |
| 71 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 72 | parseConfig(CONFIG, true); |
| 73 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 74 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 2); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE(Omitted) |
| 79 | { |
| 80 | const std::string CONFIG = R"CONFIG( |
| 81 | face_system |
| 82 | { |
| 83 | } |
| 84 | )CONFIG"; |
| 85 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 86 | parseConfig(CONFIG, true); |
| 87 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 88 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(BadListen) |
| 93 | { |
| 94 | const std::string CONFIG = R"CONFIG( |
| 95 | face_system |
| 96 | { |
| 97 | tcp |
| 98 | { |
| 99 | listen hello |
| 100 | } |
| 101 | } |
| 102 | )CONFIG"; |
| 103 | |
| 104 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 105 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(ChannelsDisabled) |
| 109 | { |
| 110 | const std::string CONFIG = R"CONFIG( |
| 111 | face_system |
| 112 | { |
| 113 | tcp |
| 114 | { |
| 115 | port 6363 |
| 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(UnknownOption) |
| 127 | { |
| 128 | const std::string CONFIG = R"CONFIG( |
| 129 | face_system |
| 130 | { |
| 131 | tcp |
| 132 | { |
| 133 | hello |
| 134 | } |
| 135 | } |
| 136 | )CONFIG"; |
| 137 | |
| 138 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 139 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 143 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 144 | BOOST_AUTO_TEST_CASE(GetChannels) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 145 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
| 147 | |
| 148 | std::set<std::string> expected; |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 149 | expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString()); |
| 150 | expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString()); |
| 151 | expected.insert(createChannel("::1", "20071")->getUri().toString()); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 152 | checkChannelListEqual(factory, expected); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(CreateChannel) |
| 156 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 157 | auto channel1 = createChannel("127.0.0.1", "20070"); |
| 158 | auto channel1a = createChannel("127.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 160 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
| 161 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 162 | auto channel2 = createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 163 | BOOST_CHECK_NE(channel1, channel2); |
| 164 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 165 | auto channel3 = createChannel("::1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 166 | BOOST_CHECK_NE(channel2, channel3); |
| 167 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
| 168 | } |
| 169 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 170 | BOOST_AUTO_TEST_CASE(CreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 171 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 172 | createFace(factory, |
| 173 | FaceUri("tcp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 174 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 175 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 176 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 177 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 178 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 179 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 180 | createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 181 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 182 | createFace(factory, |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 183 | FaceUri("tcp4://127.0.0.1:6363"), |
| 184 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 185 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 186 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 187 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 188 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 189 | |
| 190 | createFace(factory, |
| 191 | FaceUri("tcp4://127.0.0.1:6363"), |
| 192 | {}, |
| 193 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 194 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 195 | false, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 196 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 197 | |
| 198 | createFace(factory, |
| 199 | FaceUri("tcp4://127.0.0.1:20072"), |
| 200 | {}, |
| 201 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 202 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 203 | false, |
| 204 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 205 | |
| 206 | createFace(factory, |
| 207 | FaceUri("tcp4://127.0.0.1:20073"), |
| 208 | {}, |
| 209 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 210 | false, |
| 211 | true, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 212 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 215 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 216 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 217 | createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 218 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 219 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 220 | FaceUri("tcp4://127.0.0.1:20072"), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 221 | FaceUri("tcp4://127.0.0.1:20071"), |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 222 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 223 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 224 | false, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 225 | {CreateFaceExpectedResult::FAILURE, 406, |
| 226 | "Unicast TCP faces cannot be created with a LocalUri"}); |
| 227 | |
| 228 | createFace(factory, |
| 229 | FaceUri("tcp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 230 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 231 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 232 | false, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 233 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 234 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 235 | "Outgoing TCP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 236 | |
| 237 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 238 | FaceUri("tcp4://198.51.100.100:6363"), |
| 239 | {}, |
| 240 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 241 | true, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 242 | false, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 243 | {CreateFaceExpectedResult::FAILURE, 406, |
| 244 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 247 | class CreateFaceTimeoutFixture : public TcpFactoryFixture |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 248 | { |
| 249 | public: |
| 250 | void |
| 251 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 252 | { |
| 253 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 254 | face = newFace; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 255 | |
| 256 | limitedIo.afterOp(); |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | onConnectFailed(const std::string& reason) |
| 261 | { |
| 262 | BOOST_CHECK_MESSAGE(true, reason); |
| 263 | |
| 264 | limitedIo.afterOp(); |
| 265 | } |
| 266 | |
| 267 | public: |
| 268 | LimitedIo limitedIo; |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 269 | shared_ptr<Face> face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 272 | BOOST_FIXTURE_TEST_CASE(CreateFaceTimeout, CreateFaceTimeoutFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 273 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame^] | 274 | createChannel("0.0.0.0", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 275 | |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 276 | factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {}, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 277 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, false, false}, |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 278 | bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1), |
| 279 | bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 280 | |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 281 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS); |
| 282 | BOOST_CHECK(face == nullptr); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 285 | BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory |
| 286 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 287 | |
| 288 | } // namespace tests |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 289 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 290 | } // namespace nfd |