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