Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
| 28 | #include "core/network-interface.hpp" |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [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 { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Face) |
| 36 | BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, BaseFixture) |
| 37 | |
| 38 | using nfd::Face; |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 41 | { |
| 42 | TcpFactory factory; |
| 43 | |
| 44 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 45 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
| 46 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 47 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
| 48 | |
| 49 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 50 | BOOST_CHECK_NE(channel1, channel2); |
| 51 | |
| 52 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 53 | BOOST_CHECK_NE(channel2, channel3); |
| 54 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
| 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 58 | { |
| 59 | TcpFactory factory; |
| 60 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 61 | |
| 62 | std::vector<shared_ptr<const Channel>> expectedChannels; |
| 63 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 64 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 65 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 66 | |
| 67 | for (const auto& ch : factory.getChannels()) { |
| 68 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch); |
| 69 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 70 | expectedChannels.erase(pos); |
| 71 | } |
| 72 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 73 | } |
| 74 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 75 | BOOST_AUTO_TEST_CASE(FaceCreate) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 76 | { |
| 77 | TcpFactory factory; |
| 78 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 79 | createFace(factory, |
| 80 | FaceUri("tcp4://127.0.0.1:6363"), |
| 81 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 82 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 83 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 84 | |
| 85 | factory.createChannel("127.0.0.1", "20071"); |
| 86 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 87 | createFace(factory, |
| 88 | FaceUri("tcp4://127.0.0.1:20070"), |
| 89 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 90 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 91 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 95 | { |
| 96 | TcpFactory factory; |
| 97 | |
| 98 | factory.createChannel("127.0.0.1", "20070"); |
| 99 | factory.createChannel("127.0.0.1", "20071"); |
| 100 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 101 | createFace(factory, |
| 102 | FaceUri("tcp4://127.0.0.1:20070"), |
| 103 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 104 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 105 | {CreateFaceExpectedResult::FAILURE, 406, |
| 106 | "Outgoing TCP faces only support persistent persistency"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 107 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 108 | createFace(factory, |
| 109 | FaceUri("tcp4://127.0.0.1:20071"), |
| 110 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 111 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 112 | {CreateFaceExpectedResult::FAILURE, 406, |
| 113 | "Outgoing TCP faces only support persistent persistency"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | class FaceCreateTimeoutFixture : public BaseFixture |
| 117 | { |
| 118 | public: |
| 119 | void |
| 120 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 121 | { |
| 122 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
| 123 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 124 | face1 = newFace; |
| 125 | |
| 126 | limitedIo.afterOp(); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | onConnectFailed(const std::string& reason) |
| 131 | { |
| 132 | BOOST_CHECK_MESSAGE(true, reason); |
| 133 | |
| 134 | limitedIo.afterOp(); |
| 135 | } |
| 136 | |
| 137 | public: |
| 138 | LimitedIo limitedIo; |
| 139 | |
| 140 | shared_ptr<Face> face1; |
| 141 | }; |
| 142 | |
| 143 | BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture) |
| 144 | { |
| 145 | TcpFactory factory; |
| 146 | shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070"); |
| 147 | |
| 148 | factory.createFace(FaceUri("tcp4://192.0.2.1:20070"), |
| 149 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 150 | false, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 151 | bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 152 | bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _2)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 153 | |
| 154 | BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 155 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 156 | |
| 157 | BOOST_CHECK_EQUAL(static_cast<bool>(face1), false); |
| 158 | } |
| 159 | |
| 160 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 161 | { |
| 162 | public: |
| 163 | FakeNetworkInterfaceFixture() |
| 164 | { |
| 165 | using namespace boost::asio::ip; |
| 166 | |
| 167 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 168 | |
| 169 | fakeInterfaces->push_back( |
| 170 | NetworkInterfaceInfo {0, "eth0", |
| 171 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 172 | {address_v4::from_string("0.0.0.0")}, |
| 173 | {address_v6::from_string("::")}, |
| 174 | address_v4(), |
| 175 | IFF_UP}); |
| 176 | fakeInterfaces->push_back( |
| 177 | NetworkInterfaceInfo {1, "eth0", |
| 178 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 179 | {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")}, |
| 180 | {}, |
| 181 | address_v4::from_string("192.168.2.255"), |
| 182 | 0}); |
| 183 | fakeInterfaces->push_back( |
| 184 | NetworkInterfaceInfo {2, "eth1", |
| 185 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 186 | {address_v4::from_string("198.51.100.1")}, |
| 187 | {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")}, |
| 188 | address_v4::from_string("198.51.100.255"), |
| 189 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 190 | |
| 191 | setDebugNetworkInterfaces(fakeInterfaces); |
| 192 | } |
| 193 | |
| 194 | ~FakeNetworkInterfaceFixture() |
| 195 | { |
| 196 | setDebugNetworkInterfaces(nullptr); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture) |
| 201 | { |
| 202 | using namespace boost::asio::ip; |
| 203 | |
| 204 | TcpFactory factory; |
| 205 | factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024)); |
| 206 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 207 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 208 | std::set<tcp::Endpoint> { |
| 209 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 210 | })); |
| 211 | |
| 212 | factory.m_prohibitedEndpoints.clear(); |
| 213 | factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)); |
| 214 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 215 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 216 | std::set<tcp::Endpoint> { |
| 217 | tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048) |
| 218 | })); |
| 219 | |
| 220 | factory.m_prohibitedEndpoints.clear(); |
| 221 | factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024)); |
| 222 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4); |
| 223 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 224 | std::set<tcp::Endpoint> { |
| 225 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 226 | tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024), |
| 227 | tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024), |
| 228 | tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024) |
| 229 | })); |
| 230 | |
| 231 | factory.m_prohibitedEndpoints.clear(); |
| 232 | factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048)); |
| 233 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3); |
| 234 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 235 | std::set<tcp::Endpoint> { |
| 236 | tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048), |
| 237 | tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048), |
| 238 | tcp::Endpoint(address_v6::from_string("::"), 2048) |
| 239 | })); |
| 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory |
| 243 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 244 | |
| 245 | } // namespace tests |
| 246 | } // namespace nfd |