blob: 0f6409934a632634875427125a6bd0ee8c6ae365 [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
Junxiao Shid9ee45c2014-02-27 15:38:11 -070082class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -070083{
84public:
85 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080086 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070087 {
Junxiao Shi79494162014-04-02 18:25:11 -070088 BOOST_CHECK(!static_cast<bool>(face1));
89 face1 = newFace;
90 face1->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070091 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070092 face1->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070093 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070094 face1->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080095 bind(&EndToEndFixture::face1_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070096
Junxiao Shi79494162014-04-02 18:25:11 -070097 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070098 }
99
100 void
101 channel1_onConnectFailed(const std::string& reason)
102 {
103 BOOST_CHECK_MESSAGE(false, reason);
104
Junxiao Shi79494162014-04-02 18:25:11 -0700105 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700106 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800107
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700108 void
109 face1_onReceiveInterest(const Interest& interest)
110 {
Junxiao Shi79494162014-04-02 18:25:11 -0700111 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700112
Junxiao Shi79494162014-04-02 18:25:11 -0700113 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700114 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800115
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700116 void
117 face1_onReceiveData(const Data& data)
118 {
Junxiao Shi79494162014-04-02 18:25:11 -0700119 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700120
Junxiao Shi79494162014-04-02 18:25:11 -0700121 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700122 }
123
124 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800125 face1_onFail()
126 {
Junxiao Shi79494162014-04-02 18:25:11 -0700127 face1.reset();
128 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800129 }
130
131 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800132 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700133 {
Junxiao Shi79494162014-04-02 18:25:11 -0700134 BOOST_CHECK(!static_cast<bool>(face2));
135 face2 = newFace;
136 face2->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700137 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700138 face2->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700139 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700140 face2->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800141 bind(&EndToEndFixture::face2_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700142
Junxiao Shi79494162014-04-02 18:25:11 -0700143 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700144 }
145
146 void
147 channel2_onConnectFailed(const std::string& reason)
148 {
149 BOOST_CHECK_MESSAGE(false, reason);
150
Junxiao Shi79494162014-04-02 18:25:11 -0700151 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700152 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800153
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700154 void
155 face2_onReceiveInterest(const Interest& interest)
156 {
Junxiao Shi79494162014-04-02 18:25:11 -0700157 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700158
Junxiao Shi79494162014-04-02 18:25:11 -0700159 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700160 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800161
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700162 void
163 face2_onReceiveData(const Data& data)
164 {
Junxiao Shi79494162014-04-02 18:25:11 -0700165 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700166
Junxiao Shi79494162014-04-02 18:25:11 -0700167 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700168 }
169
170 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800171 face2_onFail()
172 {
Junxiao Shi79494162014-04-02 18:25:11 -0700173 face2.reset();
174 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800175 }
176
177 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800178 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800179 {
Junxiao Shi79494162014-04-02 18:25:11 -0700180 faces.push_back(newFace);
181 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800182 }
183
184 void
185 channel_onConnectFailed(const std::string& reason)
186 {
187 BOOST_CHECK_MESSAGE(false, reason);
188
Junxiao Shi79494162014-04-02 18:25:11 -0700189 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800190 }
191
192 void
193 checkFaceList(size_t shouldBe)
194 {
Junxiao Shi79494162014-04-02 18:25:11 -0700195 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800196 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800197
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700198public:
Junxiao Shi79494162014-04-02 18:25:11 -0700199 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700200
Junxiao Shi79494162014-04-02 18:25:11 -0700201 shared_ptr<Face> face1;
202 std::vector<Interest> face1_receivedInterests;
203 std::vector<Data> face1_receivedDatas;
204 shared_ptr<Face> face2;
205 std::vector<Interest> face2_receivedInterests;
206 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800207
Junxiao Shi79494162014-04-02 18:25:11 -0700208 std::list< shared_ptr<Face> > faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700209};
210
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000211BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700212{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600213 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700214
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600215 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
216 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800217
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700218 BOOST_CHECK_EQUAL(channel1->isListening(), false);
219
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700220 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
221 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800222
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700223 BOOST_CHECK_EQUAL(channel1->isListening(), true);
224
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600225 TcpFactory factory2;
226
227 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
228 factory2.createChannel("127.0.0.2", "20071");
229
230 factory2.createFace(FaceUri("tcp://127.0.0.1:20070"),
231 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
232 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700233
Junxiao Shi79494162014-04-02 18:25:11 -0700234 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700235 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700236
Junxiao Shi79494162014-04-02 18:25:11 -0700237 BOOST_REQUIRE(static_cast<bool>(face1));
238 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 BOOST_CHECK(face1->isOnDemand());
241 BOOST_CHECK(!face2->isOnDemand());
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700242
Junxiao Shi79494162014-04-02 18:25:11 -0700243 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
244 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
245 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000246
Junxiao Shi79494162014-04-02 18:25:11 -0700247 BOOST_CHECK_EQUAL(face1->isLocal(), true);
248 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000249
Junxiao Shi79494162014-04-02 18:25:11 -0700250 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
251 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000252
253 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
254
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700255 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
256 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
257 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
258 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000259
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700260 face1->sendInterest(*interest1);
261 face1->sendInterest(*interest1);
262 face1->sendInterest(*interest1);
263 face1->sendData (*data1 );
264 face2->sendInterest(*interest2);
265 face2->sendData (*data2 );
266 face2->sendData (*data2 );
267 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000268
Junxiao Shi79494162014-04-02 18:25:11 -0700269 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000270 "TcpChannel error: cannot send or receive Interest/Data packets");
271
272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
274 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
275 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
276 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000277
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700278 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
279 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
280 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
281 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000282
Junxiao Shi79494162014-04-02 18:25:11 -0700283 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700284 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
285 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
286 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
287 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000288
Junxiao Shi79494162014-04-02 18:25:11 -0700289 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700290 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
291 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
292 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
293 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000294}
295
296BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
297{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600298 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000299
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600300 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
301 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000302
303 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
304 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
305
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600306 TcpFactory factory2;
307
308 factory2.createChannel("::2", "20070");
309
310 factory2.createFace(FaceUri("tcp://[::1]:20070"),
311 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
312 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000313
Junxiao Shi79494162014-04-02 18:25:11 -0700314 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000315 "TcpChannel error: cannot connect or cannot accept connection");
316
Junxiao Shi79494162014-04-02 18:25:11 -0700317 BOOST_REQUIRE(static_cast<bool>(face1));
318 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000319
Junxiao Shi79494162014-04-02 18:25:11 -0700320 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
321 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
322 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000323
Junxiao Shi79494162014-04-02 18:25:11 -0700324 BOOST_CHECK_EQUAL(face1->isLocal(), true);
325 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800326
Junxiao Shi79494162014-04-02 18:25:11 -0700327 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
328 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800329
330 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800331
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700332 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
333 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
334 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
335 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700336
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700337 face1->sendInterest(*interest1);
338 face1->sendData (*data1 );
339 face2->sendInterest(*interest2);
340 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700341
Junxiao Shi79494162014-04-02 18:25:11 -0700342 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700343 "TcpChannel error: cannot send or receive Interest/Data packets");
344
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700345
Junxiao Shi79494162014-04-02 18:25:11 -0700346 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
347 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
348 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
349 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800350
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700351 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
352 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
353 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
354 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700355}
356
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800357BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
358{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800359 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800360
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800361 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
362 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800363
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800364 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
365 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800366
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800367 channel2->connect("127.0.0.1", "20070",
368 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
369 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800370 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800371
Junxiao Shi79494162014-04-02 18:25:11 -0700372 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700373 "TcpChannel error: cannot connect or cannot accept connection");
374
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800375
Junxiao Shi79494162014-04-02 18:25:11 -0700376 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800377
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800378 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800379 channel3->connect("127.0.0.1", "20070",
380 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
381 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800382 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800383
384
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800385 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800386
387 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800388
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700389 scheduler::schedule(time::seconds(1),
390 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800391 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700392 static_cast<TcpChannel::FaceCreatedCallback>(
393 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
394 static_cast<TcpChannel::ConnectFailedCallback>(
395 bind(&EndToEndFixture::channel_onConnectFailed, this, _1)),
396 time::seconds(4)));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800397
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700398 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800399 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800400
Junxiao Shi79494162014-04-02 18:25:11 -0700401 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700402 time::seconds(10)) == LimitedIo::EXCEED_OPS,
403 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800404
Junxiao Shi79494162014-04-02 18:25:11 -0700405 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800406}
407
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800408
409BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
410{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800411 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800412
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800413 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
414 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800415
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800416 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
417 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800418
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800419 channel2->connect("127.0.0.1", "20070",
420 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
421 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800422 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800423
Junxiao Shi79494162014-04-02 18:25:11 -0700424 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700425 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800426
427 BOOST_CHECK_EQUAL(channel1->size(), 1);
428 BOOST_CHECK_EQUAL(channel2->size(), 1);
429
Junxiao Shi79494162014-04-02 18:25:11 -0700430 BOOST_REQUIRE(static_cast<bool>(face1));
431 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800432
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700433 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700434 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800435
Junxiao Shi79494162014-04-02 18:25:11 -0700436 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700437 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800438
439 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700440 BOOST_CHECK(!static_cast<bool>(face1));
441 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800442
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800443 BOOST_CHECK_EQUAL(channel1->size(), 0);
444 BOOST_CHECK_EQUAL(channel2->size(), 0);
445}
446
447
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700448
449
450class SimpleEndToEndFixture : protected BaseFixture
451{
452public:
453 void
454 onFaceCreated(const shared_ptr<Face>& face)
455 {
456 face->onReceiveInterest +=
457 bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1);
458 face->onReceiveData +=
459 bind(&SimpleEndToEndFixture::onReceiveData, this, _1);
460 face->onFail +=
461 bind(&SimpleEndToEndFixture::onFail, this, face);
462
463 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
464 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
465 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
466
467 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
468 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
469 }
470
471 limitedIo.afterOp();
472 }
473
474 void
475 onConnectFailed(const std::string& reason)
476 {
477 BOOST_CHECK_MESSAGE(false, reason);
478
479 limitedIo.afterOp();
480 }
481
482 void
483 onReceiveInterest(const Interest& interest)
484 {
485 receivedInterests.push_back(interest);
486
487 limitedIo.afterOp();
488 }
489
490 void
491 onReceiveData(const Data& data)
492 {
493 receivedDatas.push_back(data);
494
495 limitedIo.afterOp();
496 }
497
498 void
499 onFail(const shared_ptr<Face>& face)
500 {
501 limitedIo.afterOp();
502 }
503
504public:
505 LimitedIo limitedIo;
506
507 std::vector<Interest> receivedInterests;
508 std::vector<Data> receivedDatas;
509};
510
511
512BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset,
513 CorruptedPackets, SimpleEndToEndFixture)
514{
515 TcpFactory factory;
516
517 shared_ptr<TcpChannel> channel = factory.createChannel("127.0.0.1", "20070");
518 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
519 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
520 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
521
522 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
523 sender.start(Resolver<boost::asio::ip::tcp>::syncResolve("127.0.0.1", "20070"));
524
525 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
526 time::seconds(1)) == LimitedIo::EXCEED_TIME,
527 "Exception thrown for " + Dataset::getName());
528}
529
530BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset,
531 CorruptedPackets, SimpleEndToEndFixture)
532{
533 // tests with non-local Face
534 std::string someIpv4Address;
535 std::list< shared_ptr<NetworkInterfaceInfo> > ifs = listNetworkInterfaces();
536 for (std::list< shared_ptr<NetworkInterfaceInfo> >::const_iterator i = ifs.begin();
537 i != ifs.end();
538 ++i)
539 {
540 if (!(*i)->isLoopback() && (*i)->isUp() && !(*i)->ipv4Addresses.empty())
541 {
542 someIpv4Address = (*i)->ipv4Addresses[0].to_string();
543 break;
544 }
545 }
546 if (someIpv4Address.empty())
547 {
548 BOOST_TEST_MESSAGE("Test with non-local Face cannot be run "
549 "(no non-local interface with IPv4 address available)");
550 return;
551 }
552
553 TcpFactory factory;
554
555 shared_ptr<TcpChannel> channel = factory.createChannel(someIpv4Address, "20070");
556 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
557 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
558 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
559
560
561 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
562 sender.start(Resolver<boost::asio::ip::tcp>::syncResolve(someIpv4Address, "20070"));
563
564 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
565 time::seconds(1)) == LimitedIo::EXCEED_TIME,
566 "Exception thrown for " + Dataset::getName());
567}
568
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700569BOOST_AUTO_TEST_SUITE_END()
570
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700571} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700572} // namespace nfd