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