Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 26 | #include "face/tcp-channel.hpp" |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 27 | #include "face/tcp-factory.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 29 | #include "core/network-interface.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 31 | #include "tests/limited-io.hpp" |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 32 | #include "dummy-stream-sender.hpp" |
| 33 | #include "packet-datasets.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | namespace tests { |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 37 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Face) |
| 39 | BOOST_FIXTURE_TEST_SUITE(TestTcp, BaseFixture) |
| 40 | |
| 41 | using nfd::Face; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 42 | |
| 43 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 44 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 45 | TcpFactory factory; |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 47 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 48 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 52 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 53 | BOOST_CHECK_NE(channel1, channel2); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 54 | |
| 55 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 56 | BOOST_CHECK_NE(channel2, channel3); |
| 57 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 61 | { |
| 62 | TcpFactory factory; |
| 63 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 64 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 65 | std::vector<shared_ptr<const Channel>> expectedChannels; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 66 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 67 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 68 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 69 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 70 | for (const auto& ch : factory.getChannels()) { |
| 71 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch); |
| 72 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 73 | expectedChannels.erase(pos); |
| 74 | } |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 76 | } |
| 77 | |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 78 | class FaceCreateFixture : protected BaseFixture |
| 79 | { |
| 80 | public: |
| 81 | void |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 82 | checkError(const std::string& errorActual, const std::string& errorExpected) |
| 83 | { |
| 84 | BOOST_CHECK_EQUAL(errorActual, errorExpected); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | failIfError(const std::string& errorActual) |
| 89 | { |
| 90 | BOOST_FAIL("No error expected, but got: [" << errorActual << "]"); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture) |
| 95 | { |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 96 | TcpFactory factory; |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 97 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 98 | factory.createFace(FaceUri("tcp4://127.0.0.1:6363"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 99 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 100 | bind([]{}), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 101 | bind(&FaceCreateFixture::checkError, this, _1, |
| 102 | "No channels available to connect to 127.0.0.1:6363")); |
| 103 | |
| 104 | factory.createChannel("127.0.0.1", "20071"); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 105 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 106 | factory.createFace(FaceUri("tcp4://127.0.0.1:20070"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 107 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 108 | bind([]{}), |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 109 | bind(&FaceCreateFixture::failIfError, this, _1)); |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 112 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 113 | { |
| 114 | TcpFactory factory; |
| 115 | |
| 116 | factory.createChannel("127.0.0.1", "20070"); |
| 117 | factory.createChannel("127.0.0.1", "20071"); |
| 118 | |
| 119 | BOOST_CHECK_THROW(factory.createFace(FaceUri("tcp4://127.0.0.1:20070"), |
| 120 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 121 | bind([]{}), |
| 122 | bind([]{})), |
| 123 | ProtocolFactory::Error); |
| 124 | |
| 125 | BOOST_CHECK_THROW(factory.createFace(FaceUri("tcp4://127.0.0.1:20071"), |
| 126 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
| 127 | bind([]{}), |
| 128 | bind([]{})), |
| 129 | ProtocolFactory::Error); |
| 130 | } |
| 131 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 132 | class EndToEndFixture : protected BaseFixture |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 133 | { |
| 134 | public: |
| 135 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 136 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 137 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 138 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 139 | face1 = newFace; |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 140 | face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1)); |
| 141 | face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1)); |
| 142 | face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 144 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | channel1_onConnectFailed(const std::string& reason) |
| 149 | { |
| 150 | BOOST_CHECK_MESSAGE(false, reason); |
| 151 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 152 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 154 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 155 | void |
| 156 | face1_onReceiveInterest(const Interest& interest) |
| 157 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 158 | face1_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 160 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 161 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 162 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 163 | void |
| 164 | face1_onReceiveData(const Data& data) |
| 165 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 166 | face1_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 168 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 172 | face1_onFail() |
| 173 | { |
Junxiao Shi | d5134a7 | 2015-11-08 07:53:24 -0700 | [diff] [blame] | 174 | g_io.post([this] { |
| 175 | face1.reset(); |
| 176 | limitedIo.afterOp(); |
| 177 | }); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 181 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 182 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 183 | BOOST_CHECK(!static_cast<bool>(face2)); |
| 184 | face2 = newFace; |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 185 | face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1)); |
| 186 | face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1)); |
| 187 | face2->onFail.connect(bind(&EndToEndFixture::face2_onFail, this)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 188 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 189 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void |
| 193 | channel2_onConnectFailed(const std::string& reason) |
| 194 | { |
| 195 | BOOST_CHECK_MESSAGE(false, reason); |
| 196 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 197 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 198 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 199 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 200 | void |
| 201 | face2_onReceiveInterest(const Interest& interest) |
| 202 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 203 | face2_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 205 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 206 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 207 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 208 | void |
| 209 | face2_onReceiveData(const Data& data) |
| 210 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 211 | face2_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 212 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 213 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 217 | face2_onFail() |
| 218 | { |
Junxiao Shi | d5134a7 | 2015-11-08 07:53:24 -0700 | [diff] [blame] | 219 | g_io.post([this] { |
| 220 | face2.reset(); |
| 221 | limitedIo.afterOp(); |
| 222 | }); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 226 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 227 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 228 | faces.push_back(newFace); |
| 229 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void |
| 233 | channel_onConnectFailed(const std::string& reason) |
| 234 | { |
| 235 | BOOST_CHECK_MESSAGE(false, reason); |
| 236 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 237 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
| 241 | checkFaceList(size_t shouldBe) |
| 242 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 243 | BOOST_CHECK_EQUAL(faces.size(), shouldBe); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 244 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 245 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 246 | void |
| 247 | connect(const shared_ptr<TcpChannel>& channel, |
| 248 | const std::string& remoteHost, |
| 249 | const std::string& remotePort) |
| 250 | { |
Alexander Afanasyev | baba253 | 2015-02-13 18:27:33 -0800 | [diff] [blame] | 251 | channel->connect(tcp::Endpoint(boost::asio::ip::address::from_string(remoteHost), |
| 252 | boost::lexical_cast<uint16_t>(remotePort)), |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 253 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 254 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 255 | } |
| 256 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 257 | public: |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 258 | LimitedIo limitedIo; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 259 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 260 | shared_ptr<Face> face1; |
| 261 | std::vector<Interest> face1_receivedInterests; |
| 262 | std::vector<Data> face1_receivedDatas; |
| 263 | shared_ptr<Face> face2; |
| 264 | std::vector<Interest> face2_receivedInterests; |
| 265 | std::vector<Data> face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 266 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 267 | std::list<shared_ptr<Face>> faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 270 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 271 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 272 | TcpFactory factory1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 273 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 274 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 275 | factory1.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 276 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 278 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 279 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 280 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 281 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 283 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 284 | TcpFactory factory2; |
| 285 | |
| 286 | shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070"); |
| 287 | factory2.createChannel("127.0.0.2", "20071"); |
| 288 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 289 | factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 290 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 291 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 292 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 293 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 294 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 295 | "TcpChannel error: cannot connect or cannot accept connection"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 296 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 297 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 298 | BOOST_REQUIRE(static_cast<bool>(face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 299 | |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 301 | BOOST_CHECK_EQUAL(face2->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL(face1->isMultiAccess(), false); |
| 303 | BOOST_CHECK_EQUAL(face2->isMultiAccess(), false); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 304 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 305 | BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070"); |
| 306 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070"); |
| 307 | // face1 has an unknown remoteUri, since the source port is automatically chosen by OS |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 308 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 310 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 311 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 312 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 313 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 314 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 315 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 316 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 317 | face1->sendInterest(*interest1); |
| 318 | face1->sendInterest(*interest1); |
| 319 | face1->sendInterest(*interest1); |
| 320 | face1->sendData (*data1 ); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 321 | size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size(); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 322 | face2->sendInterest(*interest2); |
| 323 | face2->sendData (*data2 ); |
| 324 | face2->sendData (*data2 ); |
| 325 | face2->sendData (*data2 ); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 326 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 327 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 328 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 329 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 330 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 331 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 332 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3); |
| 333 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 334 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 335 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 336 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 337 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 338 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 339 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 340 | // needed to ensure NOutBytes counters are accurate |
| 341 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 342 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 343 | const FaceCounters& counters1 = face1->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 344 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1); |
| 345 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 346 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3); |
| 347 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 348 | BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 349 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 350 | const FaceCounters& counters2 = face2->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 351 | BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3); |
| 352 | BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1); |
| 353 | BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1); |
| 354 | BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 355 | BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) |
| 359 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 360 | TcpFactory factory1; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 361 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 362 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070"); |
| 363 | shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 364 | |
| 365 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 366 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 367 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 368 | TcpFactory factory2; |
| 369 | |
| 370 | factory2.createChannel("::2", "20070"); |
| 371 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 372 | factory2.createFace(FaceUri("tcp6://[::1]:20070"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 373 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 374 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 375 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 376 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 377 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 378 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 379 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 380 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 381 | BOOST_REQUIRE(static_cast<bool>(face2)); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 382 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 383 | BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070"); |
| 384 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070"); |
| 385 | // face1 has an unknown remoteUri, since the source port is automatically chosen by OS |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 386 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 387 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 388 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 389 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 390 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 391 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 392 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 393 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 394 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 395 | face1->sendInterest(*interest1); |
| 396 | face1->sendData (*data1 ); |
| 397 | face2->sendInterest(*interest2); |
| 398 | face2->sendData (*data2 ); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 399 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 400 | BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 401 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 402 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 403 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 404 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 405 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1); |
| 406 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1); |
| 407 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 408 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 410 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 411 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 412 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 415 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 416 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 417 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 418 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 419 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 420 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 421 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 422 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 423 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 424 | |
Alexander Afanasyev | baba253 | 2015-02-13 18:27:33 -0800 | [diff] [blame] | 425 | using namespace boost::asio; |
| 426 | channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070), |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 427 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 428 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 429 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 430 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 431 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 432 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 433 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 434 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(faces.size(), 2); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 436 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 437 | shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
Alexander Afanasyev | baba253 | 2015-02-13 18:27:33 -0800 | [diff] [blame] | 438 | channel3->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070), |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 439 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 440 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 441 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 442 | |
| 443 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 444 | shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 445 | |
| 446 | BOOST_CHECK_NE(channel3, channel4); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 447 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 448 | scheduler::schedule(time::seconds(1), |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 449 | bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070")); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 450 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 451 | scheduler::schedule(time::milliseconds(500), |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 452 | bind(&EndToEndFixture::checkFaceList, this, 4)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 453 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 454 | BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 455 | time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 456 | "TcpChannel error: cannot connect or cannot accept multiple connections"); |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 457 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 458 | BOOST_CHECK_EQUAL(faces.size(), 6); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 461 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 462 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 463 | TcpFactory factory; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 464 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 465 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 466 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 467 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 468 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 469 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 470 | |
Alexander Afanasyev | baba253 | 2015-02-13 18:27:33 -0800 | [diff] [blame] | 471 | using namespace boost::asio; |
| 472 | channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070), |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 473 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 474 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 475 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 476 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 477 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 478 | "TcpChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 479 | |
| 480 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 481 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 482 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 483 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 484 | BOOST_CHECK(static_cast<bool>(face2)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 485 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 486 | // Face::close must be invoked during io run to be counted as an op |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 487 | scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 488 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 489 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 490 | "FaceClosing error: cannot properly close faces"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 491 | |
| 492 | // both faces should get closed |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 493 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 494 | BOOST_CHECK(!static_cast<bool>(face2)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 495 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 496 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 497 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 498 | } |
| 499 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 500 | class SimpleEndToEndFixture : protected BaseFixture |
| 501 | { |
| 502 | public: |
| 503 | void |
| 504 | onFaceCreated(const shared_ptr<Face>& face) |
| 505 | { |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 506 | face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1)); |
| 507 | face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1)); |
| 508 | face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face)); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 509 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 510 | limitedIo.afterOp(); |
| 511 | } |
| 512 | |
| 513 | void |
| 514 | onConnectFailed(const std::string& reason) |
| 515 | { |
| 516 | BOOST_CHECK_MESSAGE(false, reason); |
| 517 | |
| 518 | limitedIo.afterOp(); |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | onReceiveInterest(const Interest& interest) |
| 523 | { |
| 524 | receivedInterests.push_back(interest); |
| 525 | |
| 526 | limitedIo.afterOp(); |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | onReceiveData(const Data& data) |
| 531 | { |
| 532 | receivedDatas.push_back(data); |
| 533 | |
| 534 | limitedIo.afterOp(); |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | onFail(const shared_ptr<Face>& face) |
| 539 | { |
| 540 | limitedIo.afterOp(); |
| 541 | } |
| 542 | |
| 543 | public: |
| 544 | LimitedIo limitedIo; |
| 545 | |
| 546 | std::vector<Interest> receivedInterests; |
| 547 | std::vector<Data> receivedDatas; |
| 548 | }; |
| 549 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 550 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset, |
| 551 | CorruptedPackets, SimpleEndToEndFixture) |
| 552 | { |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 553 | // test with non-local Face |
| 554 | boost::asio::ip::address_v4 someIpv4Address; |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 555 | for (const auto& netif : listNetworkInterfaces()) { |
| 556 | if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) { |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 557 | someIpv4Address = netif.ipv4Addresses.front(); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 558 | break; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 559 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 560 | } |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 561 | if (someIpv4Address.is_unspecified()) { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 562 | BOOST_TEST_MESSAGE("Test with non-local Face cannot be run " |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 563 | "(no non-loopback interface with IPv4 address found)"); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 564 | return; |
| 565 | } |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 566 | |
| 567 | TcpFactory factory; |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 568 | tcp::Endpoint endpoint(someIpv4Address, 20070); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 569 | |
Davide Pesavento | 2231ab5 | 2015-03-16 00:15:13 +0100 | [diff] [blame] | 570 | shared_ptr<TcpChannel> channel = factory.createChannel(endpoint); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 571 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 572 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 573 | BOOST_REQUIRE_EQUAL(channel->isListening(), true); |
| 574 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 575 | DummyStreamSender<boost::asio::ip::tcp, Dataset> sender; |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 576 | sender.start(endpoint); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 577 | |
| 578 | BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, |
| 579 | time::seconds(1)) == LimitedIo::EXCEED_TIME, |
| 580 | "Exception thrown for " + Dataset::getName()); |
| 581 | } |
| 582 | |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 583 | class FaceCreateTimeoutFixture : protected BaseFixture |
| 584 | { |
| 585 | public: |
| 586 | void |
| 587 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 588 | { |
| 589 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
| 590 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 591 | face1 = newFace; |
| 592 | |
| 593 | limitedIo.afterOp(); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | onConnectFailed(const std::string& reason) |
| 598 | { |
| 599 | BOOST_CHECK_MESSAGE(true, reason); |
| 600 | |
| 601 | limitedIo.afterOp(); |
| 602 | } |
| 603 | |
| 604 | public: |
| 605 | LimitedIo limitedIo; |
| 606 | |
| 607 | shared_ptr<Face> face1; |
| 608 | }; |
| 609 | |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 610 | BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture) |
| 611 | { |
| 612 | TcpFactory factory; |
| 613 | shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070"); |
| 614 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 615 | factory.createFace(FaceUri("tcp4://192.0.2.1:20070"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 616 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 617 | bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1), |
| 618 | bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1)); |
| 619 | |
| 620 | BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 621 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 622 | |
| 623 | BOOST_CHECK_EQUAL(static_cast<bool>(face1), false); |
| 624 | } |
| 625 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 626 | BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture) |
| 627 | { |
| 628 | TcpFactory factory1; |
| 629 | |
| 630 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 631 | factory1.createChannel("127.0.0.1", "20071"); |
| 632 | |
| 633 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 634 | |
| 635 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 636 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 637 | |
| 638 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 639 | |
| 640 | TcpFactory factory2; |
| 641 | |
| 642 | shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070"); |
| 643 | factory2.createChannel("127.0.0.2", "20071"); |
| 644 | |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 645 | factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"), |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 646 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 647 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 648 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 649 | |
| 650 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 651 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 652 | |
| 653 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 654 | BOOST_REQUIRE(static_cast<bool>(face2)); |
| 655 | |
| 656 | std::ostringstream hugeName; |
| 657 | hugeName << "/huge-name/"; |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 658 | for (size_t i = 0; i < ndn::MAX_NDN_PACKET_SIZE; i++) |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 659 | hugeName << 'a'; |
| 660 | |
| 661 | shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix"); |
| 662 | shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str()); |
| 663 | |
| 664 | face1->sendInterest(*hugeInterest); |
| 665 | face2->sendInterest(*interest); |
| 666 | face2->sendInterest(*interest); |
| 667 | |
| 668 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 669 | BOOST_TEST_MESSAGE("Unexpected assertion test passed"); |
| 670 | } |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 671 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 672 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 673 | { |
| 674 | public: |
| 675 | FakeNetworkInterfaceFixture() |
| 676 | { |
| 677 | using namespace boost::asio::ip; |
| 678 | |
| 679 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 680 | |
| 681 | fakeInterfaces->push_back( |
| 682 | NetworkInterfaceInfo {0, "eth0", |
| 683 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 684 | {address_v4::from_string("0.0.0.0")}, |
| 685 | {address_v6::from_string("::")}, |
| 686 | address_v4(), |
| 687 | IFF_UP}); |
| 688 | fakeInterfaces->push_back( |
| 689 | NetworkInterfaceInfo {1, "eth0", |
| 690 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 691 | {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")}, |
| 692 | {}, |
| 693 | address_v4::from_string("192.168.2.255"), |
| 694 | 0}); |
| 695 | fakeInterfaces->push_back( |
| 696 | NetworkInterfaceInfo {2, "eth1", |
| 697 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 698 | {address_v4::from_string("198.51.100.1")}, |
| 699 | {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")}, |
| 700 | address_v4::from_string("198.51.100.255"), |
| 701 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 702 | |
| 703 | setDebugNetworkInterfaces(fakeInterfaces); |
| 704 | } |
| 705 | |
| 706 | ~FakeNetworkInterfaceFixture() |
| 707 | { |
| 708 | setDebugNetworkInterfaces(nullptr); |
| 709 | } |
| 710 | }; |
| 711 | |
| 712 | BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture) |
| 713 | { |
| 714 | using namespace boost::asio::ip; |
| 715 | |
| 716 | TcpFactory factory; |
| 717 | factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024)); |
| 718 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 719 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 720 | std::set<tcp::Endpoint> { |
| 721 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 722 | })); |
| 723 | |
| 724 | factory.m_prohibitedEndpoints.clear(); |
| 725 | factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)); |
| 726 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 727 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 728 | std::set<tcp::Endpoint> { |
| 729 | tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048) |
| 730 | })); |
| 731 | |
| 732 | factory.m_prohibitedEndpoints.clear(); |
| 733 | factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024)); |
| 734 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4); |
| 735 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 736 | std::set<tcp::Endpoint> { |
| 737 | tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 738 | tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024), |
| 739 | tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024), |
| 740 | tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024) |
| 741 | })); |
| 742 | |
| 743 | factory.m_prohibitedEndpoints.clear(); |
| 744 | factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048)); |
| 745 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3); |
| 746 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 747 | std::set<tcp::Endpoint> { |
| 748 | tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048), |
| 749 | tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048), |
| 750 | tcp::Endpoint(address_v6::from_string("::"), 2048) |
| 751 | })); |
| 752 | } |
| 753 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 754 | BOOST_AUTO_TEST_SUITE_END() // TestTcp |
| 755 | BOOST_AUTO_TEST_SUITE_END() // Face |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 756 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 757 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 758 | } // namespace nfd |