Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 26 | #include "face/tcp-factory.hpp" |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 27 | #include "core/resolver.hpp" |
| 28 | #include "core/network-interface.hpp" |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 30 | |
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 | |
| 36 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 37 | namespace tests { |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 42 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 43 | TcpFactory factory; |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 45 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 46 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 50 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 51 | BOOST_CHECK_NE(channel1, channel2); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 52 | |
| 53 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 54 | BOOST_CHECK_NE(channel2, channel3); |
| 55 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 59 | { |
| 60 | TcpFactory factory; |
| 61 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 62 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 63 | std::vector<shared_ptr<const Channel>> expectedChannels; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 64 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 65 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 66 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 67 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 68 | for (const auto& ch : factory.getChannels()) { |
| 69 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch); |
| 70 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 71 | expectedChannels.erase(pos); |
| 72 | } |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 74 | } |
| 75 | |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 76 | class FaceCreateFixture : protected BaseFixture |
| 77 | { |
| 78 | public: |
| 79 | void |
| 80 | ignore() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | checkError(const std::string& errorActual, const std::string& errorExpected) |
| 86 | { |
| 87 | BOOST_CHECK_EQUAL(errorActual, errorExpected); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | failIfError(const std::string& errorActual) |
| 92 | { |
| 93 | BOOST_FAIL("No error expected, but got: [" << errorActual << "]"); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture) |
| 98 | { |
| 99 | TcpFactory factory = TcpFactory(); |
| 100 | |
| 101 | factory.createFace(FaceUri("tcp4://127.0.0.1"), |
| 102 | bind(&FaceCreateFixture::ignore, this), |
| 103 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 104 | |
| 105 | factory.createFace(FaceUri("tcp4://127.0.0.1/"), |
| 106 | bind(&FaceCreateFixture::ignore, this), |
| 107 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 108 | |
| 109 | factory.createFace(FaceUri("tcp4://127.0.0.1/path"), |
| 110 | bind(&FaceCreateFixture::ignore, this), |
| 111 | bind(&FaceCreateFixture::checkError, this, _1, "Invalid URI")); |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 114 | class EndToEndFixture : protected BaseFixture |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 115 | { |
| 116 | public: |
| 117 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 118 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 119 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 120 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 121 | face1 = newFace; |
| 122 | face1->onReceiveInterest += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 123 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 124 | face1->onReceiveData += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 125 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 126 | face1->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 127 | bind(&EndToEndFixture::face1_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 129 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
| 133 | channel1_onConnectFailed(const std::string& reason) |
| 134 | { |
| 135 | BOOST_CHECK_MESSAGE(false, reason); |
| 136 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 137 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 138 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 139 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 140 | void |
| 141 | face1_onReceiveInterest(const Interest& interest) |
| 142 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 143 | face1_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 145 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 146 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 147 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 148 | void |
| 149 | face1_onReceiveData(const Data& data) |
| 150 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 151 | face1_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 153 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 157 | face1_onFail() |
| 158 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 159 | face1.reset(); |
| 160 | limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 164 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 165 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 166 | BOOST_CHECK(!static_cast<bool>(face2)); |
| 167 | face2 = newFace; |
| 168 | face2->onReceiveInterest += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 169 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 170 | face2->onReceiveData += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 171 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 172 | face2->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 173 | bind(&EndToEndFixture::face2_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 174 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 175 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void |
| 179 | channel2_onConnectFailed(const std::string& reason) |
| 180 | { |
| 181 | BOOST_CHECK_MESSAGE(false, reason); |
| 182 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 183 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 184 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 185 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 186 | void |
| 187 | face2_onReceiveInterest(const Interest& interest) |
| 188 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 189 | face2_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 190 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 191 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 192 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 193 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 194 | void |
| 195 | face2_onReceiveData(const Data& data) |
| 196 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 197 | face2_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 198 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 199 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 203 | face2_onFail() |
| 204 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 205 | face2.reset(); |
| 206 | limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 210 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 211 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 212 | faces.push_back(newFace); |
| 213 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
| 217 | channel_onConnectFailed(const std::string& reason) |
| 218 | { |
| 219 | BOOST_CHECK_MESSAGE(false, reason); |
| 220 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 221 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void |
| 225 | checkFaceList(size_t shouldBe) |
| 226 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 227 | BOOST_CHECK_EQUAL(faces.size(), shouldBe); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 228 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 229 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 230 | void |
| 231 | connect(const shared_ptr<TcpChannel>& channel, |
| 232 | const std::string& remoteHost, |
| 233 | const std::string& remotePort) |
| 234 | { |
| 235 | channel->connect(remoteHost, remotePort, |
| 236 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 237 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 238 | } |
| 239 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 240 | public: |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 241 | LimitedIo limitedIo; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 243 | shared_ptr<Face> face1; |
| 244 | std::vector<Interest> face1_receivedInterests; |
| 245 | std::vector<Data> face1_receivedDatas; |
| 246 | shared_ptr<Face> face2; |
| 247 | std::vector<Interest> face2_receivedInterests; |
| 248 | std::vector<Data> face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 249 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 250 | std::list<shared_ptr<Face>> faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 253 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 254 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 255 | TcpFactory factory1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 256 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 257 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 258 | factory1.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 261 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 262 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 263 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 264 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 265 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 266 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 267 | TcpFactory factory2; |
| 268 | |
| 269 | shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070"); |
| 270 | factory2.createChannel("127.0.0.2", "20071"); |
| 271 | |
| 272 | factory2.createFace(FaceUri("tcp://127.0.0.1:20070"), |
| 273 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 274 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
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_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 277 | "TcpChannel error: cannot connect or cannot accept connection"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 278 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 279 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 280 | BOOST_REQUIRE(static_cast<bool>(face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 281 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 282 | BOOST_CHECK(face1->isOnDemand()); |
| 283 | BOOST_CHECK(!face2->isOnDemand()); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 284 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 285 | BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070"); |
| 286 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070"); |
| 287 | // 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] | 288 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 289 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 290 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 291 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 292 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true); |
| 293 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 294 | |
| 295 | // integrated tests needs to check that TcpFace for non-loopback fails these tests... |
| 296 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 297 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 298 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 299 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 300 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 301 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 302 | face1->sendInterest(*interest1); |
| 303 | face1->sendInterest(*interest1); |
| 304 | face1->sendInterest(*interest1); |
| 305 | face1->sendData (*data1 ); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 306 | size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size(); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 307 | face2->sendInterest(*interest2); |
| 308 | face2->sendData (*data2 ); |
| 309 | face2->sendData (*data2 ); |
| 310 | face2->sendData (*data2 ); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 311 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 312 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 313 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 314 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 315 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 316 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 317 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3); |
| 318 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 319 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 320 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 321 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 322 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 323 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 324 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 325 | // needed to ensure NOutBytes counters are accurate |
| 326 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 327 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 328 | const FaceCounters& counters1 = face1->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 329 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1); |
| 330 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 331 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3); |
| 332 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 333 | BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 334 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 335 | const FaceCounters& counters2 = face2->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 336 | BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3); |
| 337 | BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1); |
| 338 | BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1); |
| 339 | BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 340 | BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) |
| 344 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 345 | TcpFactory factory1; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 346 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 347 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070"); |
| 348 | shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 349 | |
| 350 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 351 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 352 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 353 | TcpFactory factory2; |
| 354 | |
| 355 | factory2.createChannel("::2", "20070"); |
| 356 | |
| 357 | factory2.createFace(FaceUri("tcp://[::1]:20070"), |
| 358 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 359 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 360 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 361 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 362 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 363 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 364 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 365 | BOOST_REQUIRE(static_cast<bool>(face2)); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 366 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070"); |
| 368 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070"); |
| 369 | // 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] | 370 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 371 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 372 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 373 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 374 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true); |
| 375 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 376 | |
| 377 | // 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] | 378 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 379 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 380 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 381 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 382 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 383 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 384 | face1->sendInterest(*interest1); |
| 385 | face1->sendData (*data1 ); |
| 386 | face2->sendInterest(*interest2); |
| 387 | face2->sendData (*data2 ); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 388 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 389 | BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 390 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 391 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 392 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 393 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 394 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1); |
| 395 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1); |
| 396 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 397 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 398 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 399 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 400 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 401 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 404 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 405 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 406 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 407 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 408 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 409 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 410 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 411 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 412 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 413 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 414 | channel2->connect("127.0.0.1", "20070", |
| 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 | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 426 | channel3->connect("127.0.0.1", "20070", |
| 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 | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 460 | channel2->connect("127.0.0.1", "20070", |
| 461 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 462 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 463 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 464 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 465 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 466 | "TcpChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 467 | |
| 468 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 469 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 470 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 471 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 472 | BOOST_CHECK(static_cast<bool>(face2)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 473 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 474 | // 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] | 475 | scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -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 | "FaceClosing error: cannot properly close faces"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 479 | |
| 480 | // both faces should get closed |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 481 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 482 | BOOST_CHECK(!static_cast<bool>(face2)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 483 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 484 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 485 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 486 | } |
| 487 | |
| 488 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 489 | class SimpleEndToEndFixture : protected BaseFixture |
| 490 | { |
| 491 | public: |
| 492 | void |
| 493 | onFaceCreated(const shared_ptr<Face>& face) |
| 494 | { |
| 495 | face->onReceiveInterest += |
| 496 | bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1); |
| 497 | face->onReceiveData += |
| 498 | bind(&SimpleEndToEndFixture::onReceiveData, this, _1); |
| 499 | face->onFail += |
| 500 | bind(&SimpleEndToEndFixture::onFail, this, face); |
| 501 | |
| 502 | if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) { |
| 503 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 504 | LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID); |
| 505 | |
| 506 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 507 | LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID); |
| 508 | } |
| 509 | |
| 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 | |
| 550 | |
| 551 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset, |
| 552 | CorruptedPackets, SimpleEndToEndFixture) |
| 553 | { |
| 554 | TcpFactory factory; |
| 555 | |
| 556 | shared_ptr<TcpChannel> channel = factory.createChannel("127.0.0.1", "20070"); |
| 557 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 558 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 559 | BOOST_REQUIRE_EQUAL(channel->isListening(), true); |
| 560 | |
| 561 | DummyStreamSender<boost::asio::ip::tcp, Dataset> sender; |
| 562 | sender.start(Resolver<boost::asio::ip::tcp>::syncResolve("127.0.0.1", "20070")); |
| 563 | |
| 564 | BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, |
| 565 | time::seconds(1)) == LimitedIo::EXCEED_TIME, |
| 566 | "Exception thrown for " + Dataset::getName()); |
| 567 | } |
| 568 | |
| 569 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset, |
| 570 | CorruptedPackets, SimpleEndToEndFixture) |
| 571 | { |
| 572 | // tests with non-local Face |
| 573 | std::string someIpv4Address; |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 574 | for (const auto& netif : listNetworkInterfaces()) { |
| 575 | if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) { |
| 576 | someIpv4Address = netif.ipv4Addresses[0].to_string(); |
| 577 | break; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 578 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 579 | } |
| 580 | if (someIpv4Address.empty()) { |
| 581 | BOOST_TEST_MESSAGE("Test with non-local Face cannot be run " |
| 582 | "(no non-local interface with IPv4 address available)"); |
| 583 | return; |
| 584 | } |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 585 | |
| 586 | TcpFactory factory; |
| 587 | |
| 588 | shared_ptr<TcpChannel> channel = factory.createChannel(someIpv4Address, "20070"); |
| 589 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 590 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 591 | BOOST_REQUIRE_EQUAL(channel->isListening(), true); |
| 592 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 593 | DummyStreamSender<boost::asio::ip::tcp, Dataset> sender; |
| 594 | sender.start(Resolver<boost::asio::ip::tcp>::syncResolve(someIpv4Address, "20070")); |
| 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 | |
| 633 | factory.createFace(FaceUri("tcp://192.0.2.1:20070"), |
| 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 | |
| 662 | factory2.createFace(FaceUri("tcp://127.0.0.1:20070"), |
| 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 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 688 | BOOST_AUTO_TEST_SUITE_END() |
| 689 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 690 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 691 | } // namespace nfd |