Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 26 | #include "face/tcp-factory.hpp" |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 27 | #include "core/resolver.hpp" |
| 28 | #include "core/network-interface.hpp" |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 32 | #include "tests/limited-io.hpp" |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 33 | #include "dummy-stream-sender.hpp" |
| 34 | #include "packet-datasets.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 37 | namespace tests { |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 42 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 43 | TcpFactory factory; |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 45 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 46 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 50 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 51 | BOOST_CHECK_NE(channel1, channel2); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 52 | |
| 53 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 54 | BOOST_CHECK_NE(channel2, channel3); |
| 55 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 59 | { |
| 60 | TcpFactory factory; |
| 61 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 62 | |
| 63 | std::vector<shared_ptr<const Channel> > expectedChannels; |
| 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 | |
| 68 | std::list<shared_ptr<const Channel> > channels = factory.getChannels(); |
| 69 | for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin(); |
| 70 | i != channels.end(); ++i) |
| 71 | { |
| 72 | std::vector<shared_ptr<const Channel> >::iterator pos = |
| 73 | std::find(expectedChannels.begin(), expectedChannels.end(), *i); |
| 74 | |
| 75 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 76 | expectedChannels.erase(pos); |
| 77 | } |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 80 | } |
| 81 | |
Alexander Afanasyev | 86bc91a | 2014-08-28 22:29:16 -0700 | [diff] [blame] | 82 | class FaceCreateFixture : protected BaseFixture |
| 83 | { |
| 84 | public: |
| 85 | void |
| 86 | ignore() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | checkError(const std::string& errorActual, const std::string& errorExpected) |
| 92 | { |
| 93 | BOOST_CHECK_EQUAL(errorActual, errorExpected); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | failIfError(const std::string& errorActual) |
| 98 | { |
| 99 | BOOST_FAIL("No error expected, but got: [" << errorActual << "]"); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture) |
| 104 | { |
| 105 | TcpFactory factory = TcpFactory(); |
| 106 | |
| 107 | factory.createFace(FaceUri("tcp4://127.0.0.1"), |
| 108 | bind(&FaceCreateFixture::ignore, this), |
| 109 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 110 | |
| 111 | factory.createFace(FaceUri("tcp4://127.0.0.1/"), |
| 112 | bind(&FaceCreateFixture::ignore, this), |
| 113 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 114 | |
| 115 | factory.createFace(FaceUri("tcp4://127.0.0.1/path"), |
| 116 | bind(&FaceCreateFixture::ignore, this), |
| 117 | bind(&FaceCreateFixture::checkError, this, _1, "Invalid URI")); |
| 118 | |
| 119 | } |
| 120 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 121 | class EndToEndFixture : protected BaseFixture |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 122 | { |
| 123 | public: |
| 124 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 125 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 126 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 127 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 128 | face1 = newFace; |
| 129 | face1->onReceiveInterest += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 130 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 131 | face1->onReceiveData += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 132 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 133 | face1->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 134 | bind(&EndToEndFixture::face1_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 136 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | channel1_onConnectFailed(const std::string& reason) |
| 141 | { |
| 142 | BOOST_CHECK_MESSAGE(false, reason); |
| 143 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 144 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 145 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 146 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 147 | void |
| 148 | face1_onReceiveInterest(const Interest& interest) |
| 149 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 150 | face1_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 151 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 152 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 154 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 155 | void |
| 156 | face1_onReceiveData(const Data& data) |
| 157 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 158 | face1_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 160 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 164 | face1_onFail() |
| 165 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 166 | face1.reset(); |
| 167 | limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 171 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 172 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 173 | BOOST_CHECK(!static_cast<bool>(face2)); |
| 174 | face2 = newFace; |
| 175 | face2->onReceiveInterest += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 176 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 177 | face2->onReceiveData += |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 178 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 179 | face2->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 180 | bind(&EndToEndFixture::face2_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 182 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | channel2_onConnectFailed(const std::string& reason) |
| 187 | { |
| 188 | BOOST_CHECK_MESSAGE(false, reason); |
| 189 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 190 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 191 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 192 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 193 | void |
| 194 | face2_onReceiveInterest(const Interest& interest) |
| 195 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 196 | face2_receivedInterests.push_back(interest); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 198 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 199 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 200 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 201 | void |
| 202 | face2_onReceiveData(const Data& data) |
| 203 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 204 | face2_receivedDatas.push_back(data); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 205 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 206 | limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 210 | face2_onFail() |
| 211 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 212 | face2.reset(); |
| 213 | limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 217 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 218 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 219 | faces.push_back(newFace); |
| 220 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void |
| 224 | channel_onConnectFailed(const std::string& reason) |
| 225 | { |
| 226 | BOOST_CHECK_MESSAGE(false, reason); |
| 227 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 228 | limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
| 232 | checkFaceList(size_t shouldBe) |
| 233 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(faces.size(), shouldBe); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 235 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 236 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 237 | public: |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 238 | LimitedIo limitedIo; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 239 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 240 | shared_ptr<Face> face1; |
| 241 | std::vector<Interest> face1_receivedInterests; |
| 242 | std::vector<Data> face1_receivedDatas; |
| 243 | shared_ptr<Face> face2; |
| 244 | std::vector<Interest> face2_receivedInterests; |
| 245 | std::vector<Data> face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 246 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 247 | std::list< shared_ptr<Face> > faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 250 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 251 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 252 | TcpFactory factory1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 253 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 254 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 255 | factory1.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 256 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 258 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 259 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 260 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 261 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 263 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 264 | TcpFactory factory2; |
| 265 | |
| 266 | shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070"); |
| 267 | factory2.createChannel("127.0.0.2", "20071"); |
| 268 | |
| 269 | factory2.createFace(FaceUri("tcp://127.0.0.1:20070"), |
| 270 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 271 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 272 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 273 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 274 | "TcpChannel error: cannot connect or cannot accept connection"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 275 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 276 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 277 | BOOST_REQUIRE(static_cast<bool>(face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 278 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 279 | BOOST_CHECK(face1->isOnDemand()); |
| 280 | BOOST_CHECK(!face2->isOnDemand()); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 281 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070"); |
| 283 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070"); |
| 284 | // 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] | 285 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 287 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 288 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 289 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true); |
| 290 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 291 | |
| 292 | // integrated tests needs to check that TcpFace for non-loopback fails these tests... |
| 293 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 294 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 295 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 296 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 297 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 298 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 299 | face1->sendInterest(*interest1); |
| 300 | face1->sendInterest(*interest1); |
| 301 | face1->sendInterest(*interest1); |
| 302 | face1->sendData (*data1 ); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 303 | size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size(); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 304 | face2->sendInterest(*interest2); |
| 305 | face2->sendData (*data2 ); |
| 306 | face2->sendData (*data2 ); |
| 307 | face2->sendData (*data2 ); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 308 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 309 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 310 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 311 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 312 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 313 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 314 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3); |
| 315 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 316 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 317 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 318 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 319 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 320 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 321 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 322 | // needed to ensure NOutBytes counters are accurate |
| 323 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 324 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 325 | const FaceCounters& counters1 = face1->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 326 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1); |
| 327 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 328 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3); |
| 329 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 330 | BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 331 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 332 | const FaceCounters& counters2 = face2->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 333 | BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3); |
| 334 | BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1); |
| 335 | BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1); |
| 336 | BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 337 | BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) |
| 341 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 342 | TcpFactory factory1; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 343 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 344 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070"); |
| 345 | shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 346 | |
| 347 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 348 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 349 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 350 | TcpFactory factory2; |
| 351 | |
| 352 | factory2.createChannel("::2", "20070"); |
| 353 | |
| 354 | factory2.createFace(FaceUri("tcp://[::1]:20070"), |
| 355 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 356 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 357 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 358 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 359 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 360 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 361 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 362 | BOOST_REQUIRE(static_cast<bool>(face2)); |
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(face2->getRemoteUri().toString(), "tcp6://[::1]:20070"); |
| 365 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070"); |
| 366 | // 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] | 367 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 368 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 369 | BOOST_CHECK_EQUAL(face2->isLocal(), true); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 370 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 371 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true); |
| 372 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 373 | |
| 374 | // 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] | 375 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 376 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 377 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 378 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 379 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 380 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 381 | face1->sendInterest(*interest1); |
| 382 | face1->sendData (*data1 ); |
| 383 | face2->sendInterest(*interest2); |
| 384 | face2->sendData (*data2 ); |
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_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 387 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 388 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 389 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 390 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 391 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1); |
| 392 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1); |
| 393 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 394 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 395 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 396 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 397 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 398 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 401 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 402 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 403 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 404 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 405 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 406 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
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 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 409 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 410 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 411 | channel2->connect("127.0.0.1", "20070", |
| 412 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 413 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 414 | time::seconds(4)); // very short timeout |
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_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 417 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 418 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 419 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 420 | BOOST_CHECK_EQUAL(faces.size(), 2); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 421 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 422 | shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 423 | channel3->connect("127.0.0.1", "20070", |
| 424 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 425 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 426 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 427 | |
| 428 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 429 | shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 430 | |
| 431 | BOOST_CHECK_NE(channel3, channel4); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 432 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 433 | scheduler::schedule(time::seconds(1), |
| 434 | bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070", |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 435 | // does not work without static_cast |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 436 | static_cast<TcpChannel::FaceCreatedCallback>( |
| 437 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1)), |
| 438 | static_cast<TcpChannel::ConnectFailedCallback>( |
| 439 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)), |
| 440 | time::seconds(4))); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 441 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 442 | scheduler::schedule(time::milliseconds(500), |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 443 | bind(&EndToEndFixture::checkFaceList, this, 4)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 444 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 445 | BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 446 | time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 447 | "TcpChannel error: cannot connect or cannot accept multiple connections"); |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 448 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 449 | BOOST_CHECK_EQUAL(faces.size(), 6); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 452 | |
| 453 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 454 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 455 | TcpFactory factory; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 456 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 457 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 458 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 459 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 460 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 461 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 462 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 463 | channel2->connect("127.0.0.1", "20070", |
| 464 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 465 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 466 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 467 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 468 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 469 | "TcpChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 470 | |
| 471 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 472 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 473 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 474 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 475 | BOOST_CHECK(static_cast<bool>(face2)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 476 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 477 | // 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] | 478 | scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 479 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 480 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 481 | "FaceClosing error: cannot properly close faces"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 482 | |
| 483 | // both faces should get closed |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 484 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 485 | BOOST_CHECK(!static_cast<bool>(face2)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 486 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 487 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 488 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 489 | } |
| 490 | |
| 491 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 492 | |
| 493 | |
| 494 | class SimpleEndToEndFixture : protected BaseFixture |
| 495 | { |
| 496 | public: |
| 497 | void |
| 498 | onFaceCreated(const shared_ptr<Face>& face) |
| 499 | { |
| 500 | face->onReceiveInterest += |
| 501 | bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1); |
| 502 | face->onReceiveData += |
| 503 | bind(&SimpleEndToEndFixture::onReceiveData, this, _1); |
| 504 | face->onFail += |
| 505 | bind(&SimpleEndToEndFixture::onFail, this, face); |
| 506 | |
| 507 | if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) { |
| 508 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 509 | LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID); |
| 510 | |
| 511 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 512 | LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID); |
| 513 | } |
| 514 | |
| 515 | limitedIo.afterOp(); |
| 516 | } |
| 517 | |
| 518 | void |
| 519 | onConnectFailed(const std::string& reason) |
| 520 | { |
| 521 | BOOST_CHECK_MESSAGE(false, reason); |
| 522 | |
| 523 | limitedIo.afterOp(); |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | onReceiveInterest(const Interest& interest) |
| 528 | { |
| 529 | receivedInterests.push_back(interest); |
| 530 | |
| 531 | limitedIo.afterOp(); |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | onReceiveData(const Data& data) |
| 536 | { |
| 537 | receivedDatas.push_back(data); |
| 538 | |
| 539 | limitedIo.afterOp(); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | onFail(const shared_ptr<Face>& face) |
| 544 | { |
| 545 | limitedIo.afterOp(); |
| 546 | } |
| 547 | |
| 548 | public: |
| 549 | LimitedIo limitedIo; |
| 550 | |
| 551 | std::vector<Interest> receivedInterests; |
| 552 | std::vector<Data> receivedDatas; |
| 553 | }; |
| 554 | |
| 555 | |
| 556 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset, |
| 557 | CorruptedPackets, SimpleEndToEndFixture) |
| 558 | { |
| 559 | TcpFactory factory; |
| 560 | |
| 561 | shared_ptr<TcpChannel> channel = factory.createChannel("127.0.0.1", "20070"); |
| 562 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 563 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 564 | BOOST_REQUIRE_EQUAL(channel->isListening(), true); |
| 565 | |
| 566 | DummyStreamSender<boost::asio::ip::tcp, Dataset> sender; |
| 567 | sender.start(Resolver<boost::asio::ip::tcp>::syncResolve("127.0.0.1", "20070")); |
| 568 | |
| 569 | BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, |
| 570 | time::seconds(1)) == LimitedIo::EXCEED_TIME, |
| 571 | "Exception thrown for " + Dataset::getName()); |
| 572 | } |
| 573 | |
| 574 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset, |
| 575 | CorruptedPackets, SimpleEndToEndFixture) |
| 576 | { |
| 577 | // tests with non-local Face |
| 578 | std::string someIpv4Address; |
| 579 | std::list< shared_ptr<NetworkInterfaceInfo> > ifs = listNetworkInterfaces(); |
| 580 | for (std::list< shared_ptr<NetworkInterfaceInfo> >::const_iterator i = ifs.begin(); |
| 581 | i != ifs.end(); |
| 582 | ++i) |
| 583 | { |
| 584 | if (!(*i)->isLoopback() && (*i)->isUp() && !(*i)->ipv4Addresses.empty()) |
| 585 | { |
| 586 | someIpv4Address = (*i)->ipv4Addresses[0].to_string(); |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | if (someIpv4Address.empty()) |
| 591 | { |
| 592 | BOOST_TEST_MESSAGE("Test with non-local Face cannot be run " |
| 593 | "(no non-local interface with IPv4 address available)"); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | TcpFactory factory; |
| 598 | |
| 599 | shared_ptr<TcpChannel> channel = factory.createChannel(someIpv4Address, "20070"); |
| 600 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 601 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 602 | BOOST_REQUIRE_EQUAL(channel->isListening(), true); |
| 603 | |
| 604 | |
| 605 | DummyStreamSender<boost::asio::ip::tcp, Dataset> sender; |
| 606 | sender.start(Resolver<boost::asio::ip::tcp>::syncResolve(someIpv4Address, "20070")); |
| 607 | |
| 608 | BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, |
| 609 | time::seconds(1)) == LimitedIo::EXCEED_TIME, |
| 610 | "Exception thrown for " + Dataset::getName()); |
| 611 | } |
| 612 | |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 613 | class FaceCreateTimeoutFixture : protected BaseFixture |
| 614 | { |
| 615 | public: |
| 616 | void |
| 617 | onFaceCreated(const shared_ptr<Face>& newFace) |
| 618 | { |
| 619 | BOOST_CHECK_MESSAGE(false, "Timeout expected"); |
| 620 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 621 | face1 = newFace; |
| 622 | |
| 623 | limitedIo.afterOp(); |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | onConnectFailed(const std::string& reason) |
| 628 | { |
| 629 | BOOST_CHECK_MESSAGE(true, reason); |
| 630 | |
| 631 | limitedIo.afterOp(); |
| 632 | } |
| 633 | |
| 634 | public: |
| 635 | LimitedIo limitedIo; |
| 636 | |
| 637 | shared_ptr<Face> face1; |
| 638 | }; |
| 639 | |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 640 | BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture) |
| 641 | { |
| 642 | TcpFactory factory; |
| 643 | shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070"); |
| 644 | |
| 645 | factory.createFace(FaceUri("tcp://192.0.2.1:20070"), |
| 646 | bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1), |
| 647 | bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1)); |
| 648 | |
| 649 | BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 650 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 651 | |
| 652 | BOOST_CHECK_EQUAL(static_cast<bool>(face1), false); |
| 653 | } |
| 654 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame^] | 655 | BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture) |
| 656 | { |
| 657 | TcpFactory factory1; |
| 658 | |
| 659 | shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 660 | factory1.createChannel("127.0.0.1", "20071"); |
| 661 | |
| 662 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 663 | |
| 664 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 665 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 666 | |
| 667 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 668 | |
| 669 | TcpFactory factory2; |
| 670 | |
| 671 | shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070"); |
| 672 | factory2.createChannel("127.0.0.2", "20071"); |
| 673 | |
| 674 | factory2.createFace(FaceUri("tcp://127.0.0.1:20070"), |
| 675 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 676 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 677 | |
| 678 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 679 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 680 | |
| 681 | BOOST_REQUIRE(static_cast<bool>(face1)); |
| 682 | BOOST_REQUIRE(static_cast<bool>(face2)); |
| 683 | |
| 684 | std::ostringstream hugeName; |
| 685 | hugeName << "/huge-name/"; |
| 686 | for (size_t i = 0; i < MAX_NDN_PACKET_SIZE; i++) |
| 687 | hugeName << 'a'; |
| 688 | |
| 689 | shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix"); |
| 690 | shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str()); |
| 691 | |
| 692 | face1->sendInterest(*hugeInterest); |
| 693 | face2->sendInterest(*interest); |
| 694 | face2->sendInterest(*interest); |
| 695 | |
| 696 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 697 | BOOST_TEST_MESSAGE("Unexpected assertion test passed"); |
| 698 | } |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 699 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 700 | BOOST_AUTO_TEST_SUITE_END() |
| 701 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 702 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 703 | } // namespace nfd |