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