blob: bec87e1f2fbb9d29f619247fd716517708dcea9f [file] [log] [blame]
Junxiao Shi96dc0c42014-01-30 23:51:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Junxiao Shi96dc0c42014-01-30 23:51:59 -070025
Davide Pesavento6ad890a2015-03-09 03:43:17 +010026#include "face/tcp-channel.hpp"
27#include "face/tcp-face.hpp"
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080028#include "face/tcp-factory.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070029
Davide Pesavento6ad890a2015-03-09 03:43:17 +010030#include "core/network-interface.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070032#include "tests/limited-io.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070033#include "dummy-stream-sender.hpp"
34#include "packet-datasets.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070035
Davide Pesavento6ad890a2015-03-09 03:43:17 +010036#include <ndn-cxx/security/key-chain.hpp>
Davide Pesavento6ad890a2015-03-09 03:43:17 +010037
Junxiao Shi96dc0c42014-01-30 23:51:59 -070038namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070040
Junxiao Shid9ee45c2014-02-27 15:38:11 -070041BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070042
43BOOST_AUTO_TEST_CASE(ChannelMap)
44{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080045 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080046
Alexander Afanasyevd6655302014-02-28 08:41:28 -080047 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
48 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070049 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080051
Alexander Afanasyevd6655302014-02-28 08:41:28 -080052 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070053 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070054
55 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
56 BOOST_CHECK_NE(channel2, channel3);
57 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070058}
59
Steve DiBenedettoef04f272014-06-04 14:28:31 -060060BOOST_AUTO_TEST_CASE(GetChannels)
61{
62 TcpFactory factory;
63 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
64
Davide Pesaventob499a602014-11-18 22:36:56 +010065 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060066 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
67 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
68 expectedChannels.push_back(factory.createChannel("::1", "20071"));
69
Davide Pesaventob499a602014-11-18 22:36:56 +010070 for (const auto& ch : factory.getChannels()) {
71 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
72 BOOST_REQUIRE(pos != expectedChannels.end());
73 expectedChannels.erase(pos);
74 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060075 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
76}
77
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070078class FaceCreateFixture : protected BaseFixture
79{
80public:
81 void
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070082 checkError(const std::string& errorActual, const std::string& errorExpected)
83 {
84 BOOST_CHECK_EQUAL(errorActual, errorExpected);
85 }
86
87 void
88 failIfError(const std::string& errorActual)
89 {
90 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
91 }
92};
93
94BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
95{
Yukai Tu7c90e6d2015-07-11 12:21:46 +080096 TcpFactory factory;
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070097
Chengyu Fan4381fb62015-01-14 11:37:04 -070098 factory.createFace(FaceUri("tcp4://127.0.0.1:6363"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +080099 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
100 bind([]{}),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700101 bind(&FaceCreateFixture::checkError, this, _1,
102 "No channels available to connect to 127.0.0.1:6363"));
103
104 factory.createChannel("127.0.0.1", "20071");
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800105
Chengyu Fan4381fb62015-01-14 11:37:04 -0700106 factory.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800107 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
108 bind([]{}),
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700109 bind(&FaceCreateFixture::failIfError, this, _1));
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700110}
111
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800112BOOST_FIXTURE_TEST_CASE(UnsupportedFaceCreate, FaceCreateFixture)
113{
114 TcpFactory factory;
115
116 factory.createChannel("127.0.0.1", "20070");
117 factory.createChannel("127.0.0.1", "20071");
118
119 BOOST_CHECK_THROW(factory.createFace(FaceUri("tcp4://127.0.0.1:20070"),
120 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
121 bind([]{}),
122 bind([]{})),
123 ProtocolFactory::Error);
124
125 BOOST_CHECK_THROW(factory.createFace(FaceUri("tcp4://127.0.0.1:20071"),
126 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
127 bind([]{}),
128 bind([]{})),
129 ProtocolFactory::Error);
130}
131
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700132class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700133{
134public:
135 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800136 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700137 {
Junxiao Shi79494162014-04-02 18:25:11 -0700138 BOOST_CHECK(!static_cast<bool>(face1));
139 face1 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700140 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
141 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
142 face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700143
Junxiao Shi79494162014-04-02 18:25:11 -0700144 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700145 }
146
147 void
148 channel1_onConnectFailed(const std::string& reason)
149 {
150 BOOST_CHECK_MESSAGE(false, reason);
151
Junxiao Shi79494162014-04-02 18:25:11 -0700152 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700153 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800154
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700155 void
156 face1_onReceiveInterest(const Interest& interest)
157 {
Junxiao Shi79494162014-04-02 18:25:11 -0700158 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700159
Junxiao Shi79494162014-04-02 18:25:11 -0700160 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700161 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800162
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700163 void
164 face1_onReceiveData(const Data& data)
165 {
Junxiao Shi79494162014-04-02 18:25:11 -0700166 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700167
Junxiao Shi79494162014-04-02 18:25:11 -0700168 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700169 }
170
171 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800172 face1_onFail()
173 {
Junxiao Shi79494162014-04-02 18:25:11 -0700174 face1.reset();
175 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800176 }
177
178 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800179 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700180 {
Junxiao Shi79494162014-04-02 18:25:11 -0700181 BOOST_CHECK(!static_cast<bool>(face2));
182 face2 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700183 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
184 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
185 face2->onFail.connect(bind(&EndToEndFixture::face2_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700186
Junxiao Shi79494162014-04-02 18:25:11 -0700187 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700188 }
189
190 void
191 channel2_onConnectFailed(const std::string& reason)
192 {
193 BOOST_CHECK_MESSAGE(false, reason);
194
Junxiao Shi79494162014-04-02 18:25:11 -0700195 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700196 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800197
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700198 void
199 face2_onReceiveInterest(const Interest& interest)
200 {
Junxiao Shi79494162014-04-02 18:25:11 -0700201 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700202
Junxiao Shi79494162014-04-02 18:25:11 -0700203 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700204 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800205
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700206 void
207 face2_onReceiveData(const Data& data)
208 {
Junxiao Shi79494162014-04-02 18:25:11 -0700209 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700210
Junxiao Shi79494162014-04-02 18:25:11 -0700211 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700212 }
213
214 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800215 face2_onFail()
216 {
Junxiao Shi79494162014-04-02 18:25:11 -0700217 face2.reset();
218 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800219 }
220
221 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800222 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800223 {
Junxiao Shi79494162014-04-02 18:25:11 -0700224 faces.push_back(newFace);
225 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800226 }
227
228 void
229 channel_onConnectFailed(const std::string& reason)
230 {
231 BOOST_CHECK_MESSAGE(false, reason);
232
Junxiao Shi79494162014-04-02 18:25:11 -0700233 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800234 }
235
236 void
237 checkFaceList(size_t shouldBe)
238 {
Junxiao Shi79494162014-04-02 18:25:11 -0700239 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800240 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800241
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200242 void
243 connect(const shared_ptr<TcpChannel>& channel,
244 const std::string& remoteHost,
245 const std::string& remotePort)
246 {
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800247 channel->connect(tcp::Endpoint(boost::asio::ip::address::from_string(remoteHost),
248 boost::lexical_cast<uint16_t>(remotePort)),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200249 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
250 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
251 }
252
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700253public:
Junxiao Shi79494162014-04-02 18:25:11 -0700254 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700255
Junxiao Shi79494162014-04-02 18:25:11 -0700256 shared_ptr<Face> face1;
257 std::vector<Interest> face1_receivedInterests;
258 std::vector<Data> face1_receivedDatas;
259 shared_ptr<Face> face2;
260 std::vector<Interest> face2_receivedInterests;
261 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800262
Davide Pesaventob499a602014-11-18 22:36:56 +0100263 std::list<shared_ptr<Face>> faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700264};
265
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000266BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700267{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600268 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700269
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600270 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
271 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800272
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700273 BOOST_CHECK_EQUAL(channel1->isListening(), false);
274
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700275 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
276 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800277
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700278 BOOST_CHECK_EQUAL(channel1->isListening(), true);
279
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600280 TcpFactory factory2;
281
282 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
283 factory2.createChannel("127.0.0.2", "20071");
284
Chengyu Fan4381fb62015-01-14 11:37:04 -0700285 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800286 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600287 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
288 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700289
Junxiao Shi79494162014-04-02 18:25:11 -0700290 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700291 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700292
Junxiao Shi79494162014-04-02 18:25:11 -0700293 BOOST_REQUIRE(static_cast<bool>(face1));
294 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800295
Yukai Tu731f0d72015-07-04 11:14:44 +0800296 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
297 BOOST_CHECK_EQUAL(face2->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento94279412015-02-27 01:29:32 +0100298 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
299 BOOST_CHECK_EQUAL(face2->isMultiAccess(), false);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700300
Junxiao Shi79494162014-04-02 18:25:11 -0700301 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
302 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
303 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000304
Junxiao Shi79494162014-04-02 18:25:11 -0700305 BOOST_CHECK_EQUAL(face1->isLocal(), true);
306 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000307
Junxiao Shi79494162014-04-02 18:25:11 -0700308 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
309 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000310
311 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
312
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700313 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
314 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
315 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
316 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000317
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700318 face1->sendInterest(*interest1);
319 face1->sendInterest(*interest1);
320 face1->sendInterest(*interest1);
321 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700322 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700323 face2->sendInterest(*interest2);
324 face2->sendData (*data2 );
325 face2->sendData (*data2 );
326 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000327
Junxiao Shi79494162014-04-02 18:25:11 -0700328 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000329 "TcpChannel error: cannot send or receive Interest/Data packets");
330
Junxiao Shi79494162014-04-02 18:25:11 -0700331 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
332 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
333 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
334 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000335
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700336 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
337 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
338 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
339 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000340
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700341 // needed to ensure NOutBytes counters are accurate
342 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
343
Junxiao Shi79494162014-04-02 18:25:11 -0700344 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700345 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
346 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
347 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
348 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700349 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000350
Junxiao Shi79494162014-04-02 18:25:11 -0700351 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700352 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
353 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
354 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
355 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700356 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000357}
358
359BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
360{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600361 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000362
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600363 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
364 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000365
366 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
367 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
368
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600369 TcpFactory factory2;
370
371 factory2.createChannel("::2", "20070");
372
Chengyu Fan4381fb62015-01-14 11:37:04 -0700373 factory2.createFace(FaceUri("tcp6://[::1]:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800374 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600375 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
376 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000377
Junxiao Shi79494162014-04-02 18:25:11 -0700378 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000379 "TcpChannel error: cannot connect or cannot accept connection");
380
Junxiao Shi79494162014-04-02 18:25:11 -0700381 BOOST_REQUIRE(static_cast<bool>(face1));
382 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000383
Junxiao Shi79494162014-04-02 18:25:11 -0700384 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
385 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
386 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000387
Junxiao Shi79494162014-04-02 18:25:11 -0700388 BOOST_CHECK_EQUAL(face1->isLocal(), true);
389 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800390
Junxiao Shi79494162014-04-02 18:25:11 -0700391 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
392 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800393
394 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800395
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700396 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
397 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
398 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
399 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700400
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700401 face1->sendInterest(*interest1);
402 face1->sendData (*data1 );
403 face2->sendInterest(*interest2);
404 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700405
Junxiao Shi79494162014-04-02 18:25:11 -0700406 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700407 "TcpChannel error: cannot send or receive Interest/Data packets");
408
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700409
Junxiao Shi79494162014-04-02 18:25:11 -0700410 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
411 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
412 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
413 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800414
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700415 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
416 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
417 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
418 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700419}
420
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800421BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
422{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800423 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800424
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800425 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
426 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800427
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800428 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
429 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800430
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800431 using namespace boost::asio;
432 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800433 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
434 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800435 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800436
Junxiao Shi79494162014-04-02 18:25:11 -0700437 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700438 "TcpChannel error: cannot connect or cannot accept connection");
439
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800440
Junxiao Shi79494162014-04-02 18:25:11 -0700441 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800442
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800443 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800444 channel3->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800445 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
446 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800447 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800448
449
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800450 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800451
452 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800453
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700454 scheduler::schedule(time::seconds(1),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200455 bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070"));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800456
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700457 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800458 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800459
Junxiao Shi79494162014-04-02 18:25:11 -0700460 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700461 time::seconds(10)) == LimitedIo::EXCEED_OPS,
462 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800463
Junxiao Shi79494162014-04-02 18:25:11 -0700464 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800465}
466
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800467
468BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
469{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800470 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800471
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800472 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
473 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800474
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800475 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
476 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800477
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800478 using namespace boost::asio;
479 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800480 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
481 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800482 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800483
Junxiao Shi79494162014-04-02 18:25:11 -0700484 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700485 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800486
487 BOOST_CHECK_EQUAL(channel1->size(), 1);
488 BOOST_CHECK_EQUAL(channel2->size(), 1);
489
Junxiao Shi79494162014-04-02 18:25:11 -0700490 BOOST_REQUIRE(static_cast<bool>(face1));
491 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800492
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700493 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700494 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800495
Junxiao Shi79494162014-04-02 18:25:11 -0700496 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700497 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800498
499 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700500 BOOST_CHECK(!static_cast<bool>(face1));
501 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800502
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800503 BOOST_CHECK_EQUAL(channel1->size(), 0);
504 BOOST_CHECK_EQUAL(channel2->size(), 0);
505}
506
507
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700508class SimpleEndToEndFixture : protected BaseFixture
509{
510public:
511 void
512 onFaceCreated(const shared_ptr<Face>& face)
513 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700514 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
515 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
516 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700517
518 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
519 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
520 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
521
522 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
523 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
524 }
525
526 limitedIo.afterOp();
527 }
528
529 void
530 onConnectFailed(const std::string& reason)
531 {
532 BOOST_CHECK_MESSAGE(false, reason);
533
534 limitedIo.afterOp();
535 }
536
537 void
538 onReceiveInterest(const Interest& interest)
539 {
540 receivedInterests.push_back(interest);
541
542 limitedIo.afterOp();
543 }
544
545 void
546 onReceiveData(const Data& data)
547 {
548 receivedDatas.push_back(data);
549
550 limitedIo.afterOp();
551 }
552
553 void
554 onFail(const shared_ptr<Face>& face)
555 {
556 limitedIo.afterOp();
557 }
558
559public:
560 LimitedIo limitedIo;
561
562 std::vector<Interest> receivedInterests;
563 std::vector<Data> receivedDatas;
564};
565
566
567BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset,
568 CorruptedPackets, SimpleEndToEndFixture)
569{
570 TcpFactory factory;
Davide Pesavento2231ab52015-03-16 00:15:13 +0100571 tcp::Endpoint endpoint(boost::asio::ip::address_v4::from_string("127.0.0.1"), 20070);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700572
Davide Pesavento2231ab52015-03-16 00:15:13 +0100573 shared_ptr<TcpChannel> channel = factory.createChannel(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700574 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
575 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
576 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
577
578 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700579 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700580
581 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
582 time::seconds(1)) == LimitedIo::EXCEED_TIME,
583 "Exception thrown for " + Dataset::getName());
584}
585
586BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset,
587 CorruptedPackets, SimpleEndToEndFixture)
588{
Davide Pesavento2231ab52015-03-16 00:15:13 +0100589 // test with non-local Face
590 boost::asio::ip::address_v4 someIpv4Address;
Davide Pesaventob499a602014-11-18 22:36:56 +0100591 for (const auto& netif : listNetworkInterfaces()) {
592 if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) {
Davide Pesavento2231ab52015-03-16 00:15:13 +0100593 someIpv4Address = netif.ipv4Addresses.front();
Davide Pesaventob499a602014-11-18 22:36:56 +0100594 break;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700595 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100596 }
Davide Pesavento2231ab52015-03-16 00:15:13 +0100597 if (someIpv4Address.is_unspecified()) {
Davide Pesaventob499a602014-11-18 22:36:56 +0100598 BOOST_TEST_MESSAGE("Test with non-local Face cannot be run "
Davide Pesavento2231ab52015-03-16 00:15:13 +0100599 "(no non-loopback interface with IPv4 address found)");
Davide Pesaventob499a602014-11-18 22:36:56 +0100600 return;
601 }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700602
603 TcpFactory factory;
Davide Pesavento2231ab52015-03-16 00:15:13 +0100604 tcp::Endpoint endpoint(someIpv4Address, 20070);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700605
Davide Pesavento2231ab52015-03-16 00:15:13 +0100606 shared_ptr<TcpChannel> channel = factory.createChannel(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700607 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
608 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
609 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
610
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700611 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700612 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700613
614 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
615 time::seconds(1)) == LimitedIo::EXCEED_TIME,
616 "Exception thrown for " + Dataset::getName());
617}
618
Alexander Afanasyev18861802014-06-13 17:49:03 -0700619class FaceCreateTimeoutFixture : protected BaseFixture
620{
621public:
622 void
623 onFaceCreated(const shared_ptr<Face>& newFace)
624 {
625 BOOST_CHECK_MESSAGE(false, "Timeout expected");
626 BOOST_CHECK(!static_cast<bool>(face1));
627 face1 = newFace;
628
629 limitedIo.afterOp();
630 }
631
632 void
633 onConnectFailed(const std::string& reason)
634 {
635 BOOST_CHECK_MESSAGE(true, reason);
636
637 limitedIo.afterOp();
638 }
639
640public:
641 LimitedIo limitedIo;
642
643 shared_ptr<Face> face1;
644};
645
Alexander Afanasyev18861802014-06-13 17:49:03 -0700646BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
647{
648 TcpFactory factory;
649 shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070");
650
Chengyu Fan4381fb62015-01-14 11:37:04 -0700651 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800652 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Alexander Afanasyev18861802014-06-13 17:49:03 -0700653 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
654 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1));
655
656 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS,
657 "TcpChannel error: cannot connect or cannot accept connection");
658
659 BOOST_CHECK_EQUAL(static_cast<bool>(face1), false);
660}
661
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200662BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture)
663{
664 TcpFactory factory1;
665
666 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
667 factory1.createChannel("127.0.0.1", "20071");
668
669 BOOST_CHECK_EQUAL(channel1->isListening(), false);
670
671 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
672 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
673
674 BOOST_CHECK_EQUAL(channel1->isListening(), true);
675
676 TcpFactory factory2;
677
678 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
679 factory2.createChannel("127.0.0.2", "20071");
680
Chengyu Fan4381fb62015-01-14 11:37:04 -0700681 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800682 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200683 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
684 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
685
686 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
687 "TcpChannel error: cannot connect or cannot accept connection");
688
689 BOOST_REQUIRE(static_cast<bool>(face1));
690 BOOST_REQUIRE(static_cast<bool>(face2));
691
692 std::ostringstream hugeName;
693 hugeName << "/huge-name/";
Junxiao Shi39cd6332014-11-06 21:53:18 -0700694 for (size_t i = 0; i < ndn::MAX_NDN_PACKET_SIZE; i++)
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200695 hugeName << 'a';
696
697 shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix");
698 shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str());
699
700 face1->sendInterest(*hugeInterest);
701 face2->sendInterest(*interest);
702 face2->sendInterest(*interest);
703
704 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
705 BOOST_TEST_MESSAGE("Unexpected assertion test passed");
706}
Alexander Afanasyev18861802014-06-13 17:49:03 -0700707
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800708class FakeNetworkInterfaceFixture : public BaseFixture
709{
710public:
711 FakeNetworkInterfaceFixture()
712 {
713 using namespace boost::asio::ip;
714
715 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
716
717 fakeInterfaces->push_back(
718 NetworkInterfaceInfo {0, "eth0",
719 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
720 {address_v4::from_string("0.0.0.0")},
721 {address_v6::from_string("::")},
722 address_v4(),
723 IFF_UP});
724 fakeInterfaces->push_back(
725 NetworkInterfaceInfo {1, "eth0",
726 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
727 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
728 {},
729 address_v4::from_string("192.168.2.255"),
730 0});
731 fakeInterfaces->push_back(
732 NetworkInterfaceInfo {2, "eth1",
733 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
734 {address_v4::from_string("198.51.100.1")},
735 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
736 address_v4::from_string("198.51.100.255"),
737 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
738
739 setDebugNetworkInterfaces(fakeInterfaces);
740 }
741
742 ~FakeNetworkInterfaceFixture()
743 {
744 setDebugNetworkInterfaces(nullptr);
745 }
746};
747
748BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
749{
750 using namespace boost::asio::ip;
751
752 TcpFactory factory;
753 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
754 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
755 BOOST_CHECK((factory.m_prohibitedEndpoints ==
756 std::set<tcp::Endpoint> {
757 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
758 }));
759
760 factory.m_prohibitedEndpoints.clear();
761 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
762 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
763 BOOST_CHECK((factory.m_prohibitedEndpoints ==
764 std::set<tcp::Endpoint> {
765 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
766 }));
767
768 factory.m_prohibitedEndpoints.clear();
769 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
770 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
771 BOOST_CHECK((factory.m_prohibitedEndpoints ==
772 std::set<tcp::Endpoint> {
773 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
774 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
775 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
776 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
777 }));
778
779 factory.m_prohibitedEndpoints.clear();
780 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
781 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
782 BOOST_CHECK((factory.m_prohibitedEndpoints ==
783 std::set<tcp::Endpoint> {
784 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
785 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
786 tcp::Endpoint(address_v6::from_string("::"), 2048)
787 }));
788}
789
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700790BOOST_AUTO_TEST_SUITE_END()
791
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700792} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700793} // namespace nfd