blob: 7b0a77d98531af05b21b90717932f06b7cbe9c6c [file] [log] [blame]
Junxiao Shi96dc0c42014-01-30 23:51:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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 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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "face/tcp-factory.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070027#include "core/resolver.hpp"
28#include "core/network-interface.hpp"
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shi96dc0c42014-01-30 23:51:59 -070030
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
36namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070037namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070038
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070040
41BOOST_AUTO_TEST_CASE(ChannelMap)
42{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080043 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080044
Alexander Afanasyevd6655302014-02-28 08:41:28 -080045 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
46 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070047 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070048 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080049
Alexander Afanasyevd6655302014-02-28 08:41:28 -080050 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070051 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070052
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 Shi96dc0c42014-01-30 23:51:59 -070056}
57
Steve DiBenedettoef04f272014-06-04 14:28:31 -060058BOOST_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 Afanasyev86bc91a2014-08-28 22:29:16 -070082class FaceCreateFixture : protected BaseFixture
83{
84public:
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
103BOOST_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 Shid9ee45c2014-02-27 15:38:11 -0700121class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700122{
123public:
124 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800125 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700126 {
Junxiao Shi79494162014-04-02 18:25:11 -0700127 BOOST_CHECK(!static_cast<bool>(face1));
128 face1 = newFace;
129 face1->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700130 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700131 face1->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700132 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700133 face1->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800134 bind(&EndToEndFixture::face1_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700135
Junxiao Shi79494162014-04-02 18:25:11 -0700136 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700137 }
138
139 void
140 channel1_onConnectFailed(const std::string& reason)
141 {
142 BOOST_CHECK_MESSAGE(false, reason);
143
Junxiao Shi79494162014-04-02 18:25:11 -0700144 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700145 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800146
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700147 void
148 face1_onReceiveInterest(const Interest& interest)
149 {
Junxiao Shi79494162014-04-02 18:25:11 -0700150 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700151
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_onReceiveData(const Data& data)
157 {
Junxiao Shi79494162014-04-02 18:25:11 -0700158 face1_receivedDatas.push_back(data);
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 }
162
163 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800164 face1_onFail()
165 {
Junxiao Shi79494162014-04-02 18:25:11 -0700166 face1.reset();
167 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800168 }
169
170 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800171 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700172 {
Junxiao Shi79494162014-04-02 18:25:11 -0700173 BOOST_CHECK(!static_cast<bool>(face2));
174 face2 = newFace;
175 face2->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700176 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700177 face2->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700178 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700179 face2->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800180 bind(&EndToEndFixture::face2_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700181
Junxiao Shi79494162014-04-02 18:25:11 -0700182 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700183 }
184
185 void
186 channel2_onConnectFailed(const std::string& reason)
187 {
188 BOOST_CHECK_MESSAGE(false, reason);
189
Junxiao Shi79494162014-04-02 18:25:11 -0700190 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700191 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800192
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700193 void
194 face2_onReceiveInterest(const Interest& interest)
195 {
Junxiao Shi79494162014-04-02 18:25:11 -0700196 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700197
Junxiao Shi79494162014-04-02 18:25:11 -0700198 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700199 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800200
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700201 void
202 face2_onReceiveData(const Data& data)
203 {
Junxiao Shi79494162014-04-02 18:25:11 -0700204 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700205
Junxiao Shi79494162014-04-02 18:25:11 -0700206 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700207 }
208
209 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800210 face2_onFail()
211 {
Junxiao Shi79494162014-04-02 18:25:11 -0700212 face2.reset();
213 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800214 }
215
216 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800217 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800218 {
Junxiao Shi79494162014-04-02 18:25:11 -0700219 faces.push_back(newFace);
220 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800221 }
222
223 void
224 channel_onConnectFailed(const std::string& reason)
225 {
226 BOOST_CHECK_MESSAGE(false, reason);
227
Junxiao Shi79494162014-04-02 18:25:11 -0700228 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800229 }
230
231 void
232 checkFaceList(size_t shouldBe)
233 {
Junxiao Shi79494162014-04-02 18:25:11 -0700234 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800235 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800236
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700237public:
Junxiao Shi79494162014-04-02 18:25:11 -0700238 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 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 Afanasyeva16abd22014-01-31 18:12:29 -0800246
Junxiao Shi79494162014-04-02 18:25:11 -0700247 std::list< shared_ptr<Face> > faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700248};
249
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000250BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700251{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600252 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700253
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600254 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
255 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800256
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700257 BOOST_CHECK_EQUAL(channel1->isListening(), false);
258
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700259 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
260 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800261
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700262 BOOST_CHECK_EQUAL(channel1->isListening(), true);
263
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600264 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 Shi96dc0c42014-01-30 23:51:59 -0700272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700274 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700275
Junxiao Shi79494162014-04-02 18:25:11 -0700276 BOOST_REQUIRE(static_cast<bool>(face1));
277 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800278
Junxiao Shi79494162014-04-02 18:25:11 -0700279 BOOST_CHECK(face1->isOnDemand());
280 BOOST_CHECK(!face2->isOnDemand());
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700281
Junxiao Shi79494162014-04-02 18:25:11 -0700282 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000285
Junxiao Shi79494162014-04-02 18:25:11 -0700286 BOOST_CHECK_EQUAL(face1->isLocal(), true);
287 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000288
Junxiao Shi79494162014-04-02 18:25:11 -0700289 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000291
292 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
293
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700294 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000298
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700299 face1->sendInterest(*interest1);
300 face1->sendInterest(*interest1);
301 face1->sendInterest(*interest1);
302 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700303 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700304 face2->sendInterest(*interest2);
305 face2->sendData (*data2 );
306 face2->sendData (*data2 );
307 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000308
Junxiao Shi79494162014-04-02 18:25:11 -0700309 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000310 "TcpChannel error: cannot send or receive Interest/Data packets");
311
Junxiao Shi79494162014-04-02 18:25:11 -0700312 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000316
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700317 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 Afanasyev7e698e62014-03-07 16:48:35 +0000321
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700322 // needed to ensure NOutBytes counters are accurate
323 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
324
Junxiao Shi79494162014-04-02 18:25:11 -0700325 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700326 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 Shi5dd26c32014-07-20 23:15:14 -0700330 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000331
Junxiao Shi79494162014-04-02 18:25:11 -0700332 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700333 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 Shi5dd26c32014-07-20 23:15:14 -0700337 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000338}
339
340BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
341{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600342 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000343
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600344 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
345 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000346
347 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
348 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
349
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600350 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000357
Junxiao Shi79494162014-04-02 18:25:11 -0700358 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000359 "TcpChannel error: cannot connect or cannot accept connection");
360
Junxiao Shi79494162014-04-02 18:25:11 -0700361 BOOST_REQUIRE(static_cast<bool>(face1));
362 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000363
Junxiao Shi79494162014-04-02 18:25:11 -0700364 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 Afanasyeva39b90b2014-03-05 15:31:00 +0000367
Junxiao Shi79494162014-04-02 18:25:11 -0700368 BOOST_CHECK_EQUAL(face1->isLocal(), true);
369 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800370
Junxiao Shi79494162014-04-02 18:25:11 -0700371 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 Afanasyevbd220a02014-02-20 00:29:56 -0800373
374 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800375
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700376 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 Shi96dc0c42014-01-30 23:51:59 -0700380
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700381 face1->sendInterest(*interest1);
382 face1->sendData (*data1 );
383 face2->sendInterest(*interest2);
384 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700385
Junxiao Shi79494162014-04-02 18:25:11 -0700386 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700387 "TcpChannel error: cannot send or receive Interest/Data packets");
388
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700389
Junxiao Shi79494162014-04-02 18:25:11 -0700390 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 Afanasyev5f1ec252014-02-28 10:59:17 -0800394
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700395 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 Shi96dc0c42014-01-30 23:51:59 -0700399}
400
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800401BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
402{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800403 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800404
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800405 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
406 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800407
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800408 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
409 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800410
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800411 channel2->connect("127.0.0.1", "20070",
412 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
413 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800414 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800415
Junxiao Shi79494162014-04-02 18:25:11 -0700416 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700417 "TcpChannel error: cannot connect or cannot accept connection");
418
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800419
Junxiao Shi79494162014-04-02 18:25:11 -0700420 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800421
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800422 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800423 channel3->connect("127.0.0.1", "20070",
424 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
425 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800426 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800427
428
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800429 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800430
431 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800432
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700433 scheduler::schedule(time::seconds(1),
434 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800435 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700436 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 Afanasyeva16abd22014-01-31 18:12:29 -0800441
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700442 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800443 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800444
Junxiao Shi79494162014-04-02 18:25:11 -0700445 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700446 time::seconds(10)) == LimitedIo::EXCEED_OPS,
447 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800448
Junxiao Shi79494162014-04-02 18:25:11 -0700449 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800450}
451
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800452
453BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
454{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800455 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800456
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800457 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
458 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800459
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800460 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
461 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800462
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800463 channel2->connect("127.0.0.1", "20070",
464 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
465 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800466 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800467
Junxiao Shi79494162014-04-02 18:25:11 -0700468 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700469 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800470
471 BOOST_CHECK_EQUAL(channel1->size(), 1);
472 BOOST_CHECK_EQUAL(channel2->size(), 1);
473
Junxiao Shi79494162014-04-02 18:25:11 -0700474 BOOST_REQUIRE(static_cast<bool>(face1));
475 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800476
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700477 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700478 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800479
Junxiao Shi79494162014-04-02 18:25:11 -0700480 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700481 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800482
483 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700484 BOOST_CHECK(!static_cast<bool>(face1));
485 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800486
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800487 BOOST_CHECK_EQUAL(channel1->size(), 0);
488 BOOST_CHECK_EQUAL(channel2->size(), 0);
489}
490
491
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700492
493
494class SimpleEndToEndFixture : protected BaseFixture
495{
496public:
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
548public:
549 LimitedIo limitedIo;
550
551 std::vector<Interest> receivedInterests;
552 std::vector<Data> receivedDatas;
553};
554
555
556BOOST_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
574BOOST_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 Afanasyev18861802014-06-13 17:49:03 -0700613class FaceCreateTimeoutFixture : protected BaseFixture
614{
615public:
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
634public:
635 LimitedIo limitedIo;
636
637 shared_ptr<Face> face1;
638};
639
Alexander Afanasyev18861802014-06-13 17:49:03 -0700640BOOST_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 Pesavento3f5655f2014-08-30 21:38:59 +0200655BOOST_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 Afanasyev18861802014-06-13 17:49:03 -0700699
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700700BOOST_AUTO_TEST_SUITE_END()
701
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700702} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700703} // namespace nfd