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 | 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 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 28 | #include "factory-test-common.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +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 | 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 | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 36 | using namespace nfd::tests; |
| 37 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Face) |
| 39 | BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, BaseFixture) |
| 40 | |
| 41 | using nfd::Face; |
| 42 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 43 | BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture) |
| 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 | |
| 63 | auto& factory = this->getFactoryById<TcpFactory>("tcp"); |
| 64 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 2); |
| 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(Omitted) |
| 68 | { |
| 69 | const std::string CONFIG = R"CONFIG( |
| 70 | face_system |
| 71 | { |
| 72 | } |
| 73 | )CONFIG"; |
| 74 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 75 | parseConfig(CONFIG, true); |
| 76 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 77 | |
| 78 | auto& factory = this->getFactoryById<TcpFactory>("tcp"); |
| 79 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(BadListen) |
| 83 | { |
| 84 | const std::string CONFIG = R"CONFIG( |
| 85 | face_system |
| 86 | { |
| 87 | tcp |
| 88 | { |
| 89 | listen hello |
| 90 | } |
| 91 | } |
| 92 | )CONFIG"; |
| 93 | |
| 94 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 95 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(ChannelsDisabled) |
| 99 | { |
| 100 | const std::string CONFIG = R"CONFIG( |
| 101 | face_system |
| 102 | { |
| 103 | tcp |
| 104 | { |
| 105 | port 6363 |
| 106 | enable_v4 no |
| 107 | enable_v6 no |
| 108 | } |
| 109 | } |
| 110 | )CONFIG"; |
| 111 | |
| 112 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 113 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 117 | { |
| 118 | const std::string CONFIG = R"CONFIG( |
| 119 | face_system |
| 120 | { |
| 121 | tcp |
| 122 | { |
| 123 | hello |
| 124 | } |
| 125 | } |
| 126 | )CONFIG"; |
| 127 | |
| 128 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 129 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 133 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 134 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 135 | { |
| 136 | TcpFactory factory; |
| 137 | |
| 138 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 139 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
| 140 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 141 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
| 142 | |
| 143 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 144 | BOOST_CHECK_NE(channel1, channel2); |
| 145 | |
| 146 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 147 | BOOST_CHECK_NE(channel2, channel3); |
| 148 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 152 | { |
| 153 | TcpFactory factory; |
| 154 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 155 | |
| 156 | std::vector<shared_ptr<const Channel>> expectedChannels; |
| 157 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 158 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 159 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 160 | |
| 161 | for (const auto& ch : factory.getChannels()) { |
| 162 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch); |
| 163 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 164 | expectedChannels.erase(pos); |
| 165 | } |
| 166 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 167 | } |
| 168 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 169 | BOOST_AUTO_TEST_CASE(FaceCreate) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 170 | { |
| 171 | TcpFactory factory; |
| 172 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 173 | createFace(factory, |
| 174 | FaceUri("tcp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 175 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 176 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [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 | |
| 180 | factory.createChannel("127.0.0.1", "20071"); |
| 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 | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 187 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 188 | |
| 189 | createFace(factory, |
| 190 | FaceUri("tcp4://127.0.0.1:6363"), |
| 191 | {}, |
| 192 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 193 | false, |
| 194 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 195 | |
| 196 | createFace(factory, |
| 197 | FaceUri("tcp4://127.0.0.1:20072"), |
| 198 | {}, |
| 199 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 200 | false, |
| 201 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 205 | { |
| 206 | TcpFactory factory; |
| 207 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 208 | factory.createChannel("127.0.0.1", "20071"); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 209 | factory.createChannel("127.0.0.1", "20072"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 210 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 211 | createFace(factory, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 212 | FaceUri("tcp4://127.0.0.1:20071"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 213 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 214 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
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, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 217 | "Outgoing TCP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 218 | |
| 219 | createFace(factory, |
| 220 | FaceUri("tcp4://127.0.0.1:20072"), |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 221 | FaceUri("tcp4://127.0.0.1:20073"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 222 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 223 | false, |
| 224 | {CreateFaceExpectedResult::FAILURE, 406, |
| 225 | "Unicast TCP faces cannot be created with a LocalUri"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | class FaceCreateTimeoutFixture : public BaseFixture |
| 229 | { |
| 230 | public: |
| 231 | void |
| 232 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 233 | { |
| 234 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 235 | face = newFace; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 236 | |
| 237 | limitedIo.afterOp(); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | onConnectFailed(const std::string& reason) |
| 242 | { |
| 243 | BOOST_CHECK_MESSAGE(true, reason); |
| 244 | |
| 245 | limitedIo.afterOp(); |
| 246 | } |
| 247 | |
| 248 | public: |
| 249 | LimitedIo limitedIo; |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 250 | shared_ptr<Face> face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture) |
| 254 | { |
| 255 | TcpFactory factory; |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 256 | factory.createChannel("0.0.0.0", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 257 | |
| 258 | factory.createFace(FaceUri("tcp4://192.0.2.1:20070"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 259 | {}, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 260 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 261 | false, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 262 | bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 263 | bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _2)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 264 | |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 265 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(10)), LimitedIo::EXCEED_OPS); |
| 266 | BOOST_CHECK(face == nullptr); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 270 | { |
| 271 | public: |
| 272 | FakeNetworkInterfaceFixture() |
| 273 | { |
| 274 | using namespace boost::asio::ip; |
| 275 | |
| 276 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 277 | |
| 278 | fakeInterfaces->push_back( |
| 279 | NetworkInterfaceInfo {0, "eth0", |
| 280 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 281 | {address_v4::from_string("0.0.0.0")}, |
| 282 | {address_v6::from_string("::")}, |
| 283 | address_v4(), |
| 284 | IFF_UP}); |
| 285 | fakeInterfaces->push_back( |
| 286 | NetworkInterfaceInfo {1, "eth0", |
| 287 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 288 | {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")}, |
| 289 | {}, |
| 290 | address_v4::from_string("192.168.2.255"), |
| 291 | 0}); |
| 292 | fakeInterfaces->push_back( |
| 293 | NetworkInterfaceInfo {2, "eth1", |
| 294 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 295 | {address_v4::from_string("198.51.100.1")}, |
| 296 | {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")}, |
| 297 | address_v4::from_string("198.51.100.255"), |
| 298 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 299 | |
| 300 | setDebugNetworkInterfaces(fakeInterfaces); |
| 301 | } |
| 302 | |
| 303 | ~FakeNetworkInterfaceFixture() |
| 304 | { |
| 305 | setDebugNetworkInterfaces(nullptr); |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture) |
| 310 | { |
| 311 | using namespace boost::asio::ip; |
| 312 | |
| 313 | TcpFactory factory; |
| 314 | factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024)); |
| 315 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 316 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 317 | std::set<tcp::Endpoint> { |
| 318 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 319 | })); |
| 320 | |
| 321 | factory.m_prohibitedEndpoints.clear(); |
| 322 | factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)); |
| 323 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 324 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 325 | std::set<tcp::Endpoint> { |
| 326 | tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048) |
| 327 | })); |
| 328 | |
| 329 | factory.m_prohibitedEndpoints.clear(); |
| 330 | factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024)); |
| 331 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4); |
| 332 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 333 | std::set<tcp::Endpoint> { |
| 334 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 335 | tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024), |
| 336 | tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024), |
| 337 | tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024) |
| 338 | })); |
| 339 | |
| 340 | factory.m_prohibitedEndpoints.clear(); |
| 341 | factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048)); |
| 342 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3); |
| 343 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 344 | std::set<tcp::Endpoint> { |
| 345 | tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048), |
| 346 | tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048), |
| 347 | tcp::Endpoint(address_v6::from_string("::"), 2048) |
| 348 | })); |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory |
| 352 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 353 | |
| 354 | } // namespace tests |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 355 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 356 | } // namespace nfd |