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