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 | /* |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 175 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 176 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 177 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 178 | createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 179 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 180 | createFace(factory, |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 181 | FaceUri("tcp4://127.0.0.1:6363"), |
| 182 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 183 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 184 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 185 | |
| 186 | createFace(factory, |
| 187 | FaceUri("tcp4://127.0.0.1:6363"), |
| 188 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 189 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false}, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 190 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 191 | |
| 192 | createFace(factory, |
| 193 | FaceUri("tcp4://127.0.0.1:20072"), |
| 194 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 195 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false}, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 196 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 197 | |
| 198 | createFace(factory, |
| 199 | FaceUri("tcp4://127.0.0.1:20073"), |
| 200 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 201 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, true, false}, |
| 202 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 203 | |
| 204 | createFace(factory, |
| 205 | FaceUri("tcp4://127.0.0.1:20073"), |
| 206 | {}, |
| 207 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, true}, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 208 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 211 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 212 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 213 | createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 214 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 215 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 216 | FaceUri("tcp4://127.0.0.1:20072"), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 217 | FaceUri("tcp4://127.0.0.1:20071"), |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 218 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 219 | {CreateFaceExpectedResult::FAILURE, 406, |
| 220 | "Unicast TCP faces cannot be created with a LocalUri"}); |
| 221 | |
| 222 | createFace(factory, |
| 223 | FaceUri("tcp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 224 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 225 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 226 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 227 | "Outgoing TCP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 228 | |
| 229 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 230 | FaceUri("tcp4://198.51.100.100:6363"), |
| 231 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 232 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, true, false, false}, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 233 | {CreateFaceExpectedResult::FAILURE, 406, |
| 234 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 237 | class CreateFaceTimeoutFixture : public TcpFactoryFixture |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 238 | { |
| 239 | public: |
| 240 | void |
| 241 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 242 | { |
| 243 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 244 | face = newFace; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 245 | |
| 246 | limitedIo.afterOp(); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | onConnectFailed(const std::string& reason) |
| 251 | { |
| 252 | BOOST_CHECK_MESSAGE(true, reason); |
| 253 | |
| 254 | limitedIo.afterOp(); |
| 255 | } |
| 256 | |
| 257 | public: |
| 258 | LimitedIo limitedIo; |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 259 | shared_ptr<Face> face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 262 | BOOST_FIXTURE_TEST_CASE(CreateFaceTimeout, CreateFaceTimeoutFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 263 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 264 | createChannel("0.0.0.0", "20070"); |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 265 | factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {}, {}}, |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 266 | bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1), |
| 267 | bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 268 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 269 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 10_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 270 | BOOST_CHECK(face == nullptr); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 273 | BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory |
| 274 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 275 | |
| 276 | } // namespace tests |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 277 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 278 | } // namespace nfd |