blob: 776d9a7ee55cdb4e0520ae715ba7ba420f090049 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Giulio Grassi624f6c62014-02-18 19:42:14 +010024
25#include "face/udp-factory.hpp"
Junxiao Shi7e2413b2014-03-02 11:15:09 -070026
27#include "tests/test-common.hpp"
28#include "tests/core/limited-io.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029
30namespace nfd {
31namespace tests {
32
33BOOST_FIXTURE_TEST_SUITE(FaceUdp, BaseFixture)
34
35class FactoryErrorCheck : protected BaseFixture
36{
37public:
38 bool isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
39 return strcmp(e.what(),
40 "Cannot create the requested UDP unicast channel, local "
41 "endpoint is already allocated for a UDP multicast face") == 0;
42 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070043
Giulio Grassi624f6c62014-02-18 19:42:14 +010044 bool isNotMulticastAddress(const UdpFactory::Error& e) {
45 return strcmp(e.what(),
46 "Cannot create the requested UDP multicast face, "
47 "the multicast group given as input is not a multicast address") == 0;
48 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070049
Giulio Grassi624f6c62014-02-18 19:42:14 +010050 bool isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
51 return strcmp(e.what(),
52 "Cannot create the requested UDP multicast face, local "
53 "endpoint is already allocated for a UDP unicast channel") == 0;
54 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070055
Giulio Grassi624f6c62014-02-18 19:42:14 +010056 bool isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
57 return strcmp(e.what(),
58 "Cannot create the requested UDP multicast face, local "
59 "endpoint is already allocated for a UDP multicast face "
60 "on a different multicast group") == 0;
61 }
62};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070063
Giulio Grassi624f6c62014-02-18 19:42:14 +010064BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
65{
66 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070067
Giulio Grassi624f6c62014-02-18 19:42:14 +010068 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070069
Giulio Grassi624f6c62014-02-18 19:42:14 +010070 //to instantiate multicast face on a specific ip address, change interfaceIp
71 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -070072
Giulio Grassi624f6c62014-02-18 19:42:14 +010073 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
74 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
75 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070076 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070077
Giulio Grassi624f6c62014-02-18 19:42:14 +010078 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
79 BOOST_CHECK_NE(channel1, channel2);
80
81 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070082
Junxiao Shi61e3cc52014-03-03 20:40:28 -070083 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
84 BOOST_CHECK_NE(channel2, channel4);
85 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
86
Giulio Grassi624f6c62014-02-18 19:42:14 +010087 //same endpoint of a unicast channel
88 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
89 "224.0.0.1",
90 "20070"),
91 UdpFactory::Error,
92 isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070093
94
Giulio Grassi624f6c62014-02-18 19:42:14 +010095 shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp,
96 "224.0.0.1",
97 "20072");
98 shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp,
99 "224.0.0.1",
100 "20072");
101 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700102
103
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 //same endpoint of a multicast face
105 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
106 UdpFactory::Error,
107 isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700108
Giulio Grassi624f6c62014-02-18 19:42:14 +0100109 //same multicast endpoint, different group
110 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
111 "224.0.0.42",
112 "20072"),
113 UdpFactory::Error,
114 isLocalEndpointOnDifferentGroup);
115
116 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
117 "192.168.10.15",
118 "20025"),
119 UdpFactory::Error,
120 isNotMulticastAddress);
121
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700122
Giulio Grassi624f6c62014-02-18 19:42:14 +0100123// //Test commented because it required to be run in a machine that can resolve ipv6 query
124// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
125// "fe80::5e96:9dff:fe7d:9c8d%en1",
126// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
127// "20070");
128//
129// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
130// //an available network interface (different from the first one used)
131// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
132// "224.0.0.1",
133// "20073");
134// BOOST_CHECK_NE(multicastFace1, multicastFace2);
135//
136//
137// //ipv6 - work in progress
138// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
139// "FF01:0:0:0:0:0:0:2",
140// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700141//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
143// "FF01:0:0:0:0:0:0:2",
144// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700145//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
147//
148// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
149// "FF01:0:0:0:0:0:0:2",
150// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700151//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100152// BOOST_CHECK_NE(multicastFace3, multicastFace5);
153//
154// //same local ipv6 endpoint for a different multicast group
155// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
156// "FE01:0:0:0:0:0:0:2",
157// "20073"),
158// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700159//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100160// //same local ipv6 (expect for th port number) endpoint for a different multicast group
161// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
162// "FE01:0:0:0:0:0:0:2",
163// "20075"),
164// UdpFactory::Error);
165//
166// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
167// "FE12:0:0:0:0:0:0:2",
168// "20075"),
169// UdpFactory::Error);
170//
171// //not a multicast ipv6
172// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
173// "A112:0:0:0:0:0:0:2",
174// "20075"),
175// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100176}
177
178class EndToEndFixture : protected BaseFixture
179{
180public:
181 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700182 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100183 {
Junxiao Shi79494162014-04-02 18:25:11 -0700184 BOOST_CHECK(!static_cast<bool>(face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100185 channel1_onFaceCreatedNoCheck(newFace);
186 }
Junxiao Shi79494162014-04-02 18:25:11 -0700187
Giulio Grassi69871f02014-03-09 16:14:44 +0100188 void
189 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
190 {
Junxiao Shi79494162014-04-02 18:25:11 -0700191 face1 = newFace;
192 face1->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100193 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700194 face1->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100195 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700196 face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100197 bind(&EndToEndFixture::face1_onFail, this);
198 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
199
Junxiao Shi79494162014-04-02 18:25:11 -0700200 faces.push_back(face1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100201
Junxiao Shi79494162014-04-02 18:25:11 -0700202 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100203 }
204
205 void
206 channel1_onConnectFailed(const std::string& reason)
207 {
208 BOOST_CHECK_MESSAGE(false, reason);
209
Junxiao Shi79494162014-04-02 18:25:11 -0700210 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100211 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700212
Giulio Grassi624f6c62014-02-18 19:42:14 +0100213 void
214 face1_onReceiveInterest(const Interest& interest)
215 {
Junxiao Shi79494162014-04-02 18:25:11 -0700216 face1_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100217
Junxiao Shi79494162014-04-02 18:25:11 -0700218 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100219 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700220
Giulio Grassi624f6c62014-02-18 19:42:14 +0100221 void
222 face1_onReceiveData(const Data& data)
223 {
Junxiao Shi79494162014-04-02 18:25:11 -0700224 face1_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100225
Junxiao Shi79494162014-04-02 18:25:11 -0700226 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100227 }
228
229 void
230 face1_onFail()
231 {
Junxiao Shi79494162014-04-02 18:25:11 -0700232 face1.reset();
233 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100234 }
235
236 void
237 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
238 {
Junxiao Shi79494162014-04-02 18:25:11 -0700239 BOOST_CHECK(!static_cast<bool>(face2));
240 face2 = newFace;
241 face2->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100242 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700243 face2->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100244 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700245 face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100246 bind(&EndToEndFixture::face2_onFail, this);
247
Junxiao Shi79494162014-04-02 18:25:11 -0700248 faces.push_back(face2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100249
250 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi79494162014-04-02 18:25:11 -0700251 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100252 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700253
Giulio Grassi624f6c62014-02-18 19:42:14 +0100254 void
255 channel2_onConnectFailed(const std::string& reason)
256 {
257 BOOST_CHECK_MESSAGE(false, reason);
258
Junxiao Shi79494162014-04-02 18:25:11 -0700259 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100260 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700261
Giulio Grassi624f6c62014-02-18 19:42:14 +0100262 void
263 face2_onReceiveInterest(const Interest& interest)
264 {
Junxiao Shi79494162014-04-02 18:25:11 -0700265 face2_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100266
Junxiao Shi79494162014-04-02 18:25:11 -0700267 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100268 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700269
Giulio Grassi624f6c62014-02-18 19:42:14 +0100270 void
271 face2_onReceiveData(const Data& data)
272 {
Junxiao Shi79494162014-04-02 18:25:11 -0700273 face2_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100274
Junxiao Shi79494162014-04-02 18:25:11 -0700275 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100276 }
277
278 void
279 face2_onFail()
280 {
Junxiao Shi79494162014-04-02 18:25:11 -0700281 face2.reset();
282 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100283 }
284
285 void
286 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
287 {
Junxiao Shi79494162014-04-02 18:25:11 -0700288 BOOST_CHECK(!static_cast<bool>(face1));
289 face3 = newFace;
290 faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700291
Junxiao Shi79494162014-04-02 18:25:11 -0700292 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100293 }
294
Giulio Grassi624f6c62014-02-18 19:42:14 +0100295 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700296 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100297 {
Junxiao Shi79494162014-04-02 18:25:11 -0700298 faces.push_back(newFace);
299 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100300 }
301
302 void
303 channel_onConnectFailed(const std::string& reason)
304 {
305 BOOST_CHECK_MESSAGE(false, reason);
306
Junxiao Shi79494162014-04-02 18:25:11 -0700307 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100308 }
309
310 void
311 channel_onConnectFailedOk(const std::string& reason)
312 {
313 //it's ok, it was supposed to fail
Junxiao Shi79494162014-04-02 18:25:11 -0700314 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100315 }
316
317 void
318 checkFaceList(size_t shouldBe)
319 {
Junxiao Shi79494162014-04-02 18:25:11 -0700320 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100321 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100322
323public:
Junxiao Shi79494162014-04-02 18:25:11 -0700324 LimitedIo limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100325
Junxiao Shi79494162014-04-02 18:25:11 -0700326 shared_ptr<Face> face1;
327 std::vector<Interest> face1_receivedInterests;
328 std::vector<Data> face1_receivedDatas;
329 shared_ptr<Face> face2;
330 std::vector<Interest> face2_receivedInterests;
331 std::vector<Data> face2_receivedDatas;
332 shared_ptr<Face> face3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100333
Junxiao Shi79494162014-04-02 18:25:11 -0700334 std::list< shared_ptr<Face> > faces;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335};
336
337
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000338BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100339{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700340 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100341
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000342 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700343
Giulio Grassi624f6c62014-02-18 19:42:14 +0100344 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
345 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
346 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700347
348
Junxiao Shi79494162014-04-02 18:25:11 -0700349 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700350 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100351
Junxiao Shi79494162014-04-02 18:25:11 -0700352 BOOST_REQUIRE(static_cast<bool>(face2));
353 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp4://127.0.0.1:20070");
354 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp4://127.0.0.1:20071");
355 BOOST_CHECK_EQUAL(face2->isLocal(), false);
356 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700357
Giulio Grassi624f6c62014-02-18 19:42:14 +0100358 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
359 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
360 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700361
Giulio Grassi624f6c62014-02-18 19:42:14 +0100362 Interest interest1("ndn:/TpnzGvW9R");
363 Data data1 ("ndn:/KfczhUqVix");
364 data1.setContent(0, 0);
365 Interest interest2("ndn:/QWiIMfj5sL");
366 Data data2 ("ndn:/XNBV796f");
367 data2.setContent(0, 0);
368 Interest interest3("ndn:/QWiIhjgkj5sL");
369 Data data3 ("ndn:/XNBV794f");
370 data3.setContent(0, 0);
371
372
373 ndn::SignatureSha256WithRsa fakeSignature;
374 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
375 reinterpret_cast<const uint8_t*>(0),
376 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700377
Giulio Grassi624f6c62014-02-18 19:42:14 +0100378 // set fake signature on data1 and data2
379 data1.setSignature(fakeSignature);
380 data2.setSignature(fakeSignature);
381 data3.setSignature(fakeSignature);
382
Junxiao Shi79494162014-04-02 18:25:11 -0700383 face2->sendInterest(interest2);
384 face2->sendData (data2 );
385 face2->sendData (data2 );
386 face2->sendData (data2 );
Giulio Grassi624f6c62014-02-18 19:42:14 +0100387
Junxiao Shi79494162014-04-02 18:25:11 -0700388 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000389 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700390 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100391
Junxiao Shi79494162014-04-02 18:25:11 -0700392 BOOST_REQUIRE(static_cast<bool>(face1));
393 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
394 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
395 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700396
Junxiao Shi79494162014-04-02 18:25:11 -0700397 face1->sendInterest(interest1);
398 face1->sendInterest(interest1);
399 face1->sendInterest(interest1);
400 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700401
Junxiao Shi79494162014-04-02 18:25:11 -0700402 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700403 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100404
405
Junxiao Shi79494162014-04-02 18:25:11 -0700406 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
407 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
408 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
409 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700410
Junxiao Shi79494162014-04-02 18:25:11 -0700411 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
412 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
413 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
414 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700415
416
417
Giulio Grassi624f6c62014-02-18 19:42:14 +0100418 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700419
Junxiao Shi79494162014-04-02 18:25:11 -0700420 face2->sendData (data3 );
421 face2->sendInterest(interest3);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100422
Junxiao Shi79494162014-04-02 18:25:11 -0700423 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700424 "UdpChannel error: cannot send or receive Interest/Data packets");
425
Junxiao Shi79494162014-04-02 18:25:11 -0700426 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
427 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700428
Junxiao Shi79494162014-04-02 18:25:11 -0700429 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
430 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000431
Junxiao Shi79494162014-04-02 18:25:11 -0700432 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700433 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 2);
434 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 4);
435 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
436 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000437
Junxiao Shi79494162014-04-02 18:25:11 -0700438 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700439 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
440 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
441 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 2);
442 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 4);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000443}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100444
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000445BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
446{
447 UdpFactory factory;
448
449 factory.createChannel("::1", "20071");
450
451 factory.createFace(FaceUri("udp://[::1]:20070"),
452 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
453 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
454
455
Junxiao Shi79494162014-04-02 18:25:11 -0700456 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000457 "UdpChannel error: cannot connect or cannot accept connection");
458
Junxiao Shi79494162014-04-02 18:25:11 -0700459 BOOST_REQUIRE(static_cast<bool>(face2));
460 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
461 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
462 BOOST_CHECK_EQUAL(face2->isLocal(), false);
463 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000464
465 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
466 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
467 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
468
469 Interest interest1("ndn:/TpnzGvW9R");
470 Data data1 ("ndn:/KfczhUqVix");
471 data1.setContent(0, 0);
472 Interest interest2("ndn:/QWiIMfj5sL");
473 Data data2 ("ndn:/XNBV796f");
474 data2.setContent(0, 0);
475 Interest interest3("ndn:/QWiIhjgkj5sL");
476 Data data3 ("ndn:/XNBV794f");
477 data3.setContent(0, 0);
478
479
480 ndn::SignatureSha256WithRsa fakeSignature;
481 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
482 reinterpret_cast<const uint8_t*>(0),
483 0));
484
485 // set fake signature on data1 and data2
486 data1.setSignature(fakeSignature);
487 data2.setSignature(fakeSignature);
488 data3.setSignature(fakeSignature);
489
Junxiao Shi79494162014-04-02 18:25:11 -0700490 face2->sendInterest(interest2);
491 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000492
Junxiao Shi79494162014-04-02 18:25:11 -0700493 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000494 time::seconds(1)) == LimitedIo::EXCEED_OPS,
495 "UdpChannel error: cannot send or receive Interest/Data packets");
496
Junxiao Shi79494162014-04-02 18:25:11 -0700497 BOOST_REQUIRE(static_cast<bool>(face1));
498 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
499 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
500 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000501
Junxiao Shi79494162014-04-02 18:25:11 -0700502 face1->sendInterest(interest1);
503 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000504
Junxiao Shi79494162014-04-02 18:25:11 -0700505 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000506 "UdpChannel error: cannot send or receive Interest/Data packets");
507
508
Junxiao Shi79494162014-04-02 18:25:11 -0700509 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
510 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
511 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
512 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000513
Junxiao Shi79494162014-04-02 18:25:11 -0700514 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
515 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
516 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
517 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000518
519
520
521 //checking if the connection accepting mechanism works properly.
522
Junxiao Shi79494162014-04-02 18:25:11 -0700523 face2->sendData (data3 );
524 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000525
Junxiao Shi79494162014-04-02 18:25:11 -0700526 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000527 "UdpChannel error: cannot send or receive Interest/Data packets");
528
Junxiao Shi79494162014-04-02 18:25:11 -0700529 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
530 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000531
Junxiao Shi79494162014-04-02 18:25:11 -0700532 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
533 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100534}
535
536BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
537{
538 Interest interest1("ndn:/TpnzGvW9R");
539 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100540
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700541 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100542
Giulio Grassi624f6c62014-02-18 19:42:14 +0100543 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
544 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700545
Giulio Grassi624f6c62014-02-18 19:42:14 +0100546 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
547 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700548
Giulio Grassi624f6c62014-02-18 19:42:14 +0100549 channel2->connect("127.0.0.1", "20070",
550 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
551 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700552
553
Junxiao Shi79494162014-04-02 18:25:11 -0700554 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700555 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100556
Junxiao Shi79494162014-04-02 18:25:11 -0700557 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700558
Giulio Grassi624f6c62014-02-18 19:42:14 +0100559 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
560 channel3->connect("127.0.0.1", "20070",
561 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
562 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700563
Giulio Grassi624f6c62014-02-18 19:42:14 +0100564 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
565
566 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100567
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700568 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700569 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
570 // does not work without static_cast
571 static_cast<UdpChannel::FaceCreatedCallback>(
572 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
573 static_cast<UdpChannel::ConnectFailedCallback>(
574 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
575
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700576 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700577
Junxiao Shi79494162014-04-02 18:25:11 -0700578 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700579 time::seconds(4)) == LimitedIo::EXCEED_OPS,
580 "UdpChannel error: cannot connect or cannot accept multiple connections");
581
Junxiao Shi79494162014-04-02 18:25:11 -0700582 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700583
584
Junxiao Shi79494162014-04-02 18:25:11 -0700585 face2->sendInterest(interest1);
586 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700587 "UdpChannel error: cannot send or receive Interest/Data packets");
588
Junxiao Shi79494162014-04-02 18:25:11 -0700589 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700590
Junxiao Shi79494162014-04-02 18:25:11 -0700591 face3->sendInterest(interest2);
592 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700593 "UdpChannel error: cannot send or receive Interest/Data packets");
594
Junxiao Shi79494162014-04-02 18:25:11 -0700595 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
596 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100597 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
598 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100599 UdpChannel::Error);
600}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700601
Giulio Grassi624f6c62014-02-18 19:42:14 +0100602//Test commented because it required to be run in a machine that can resolve ipv6 query
603//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
604//{
605// UdpFactory factory = UdpFactory();
606// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700607//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100608// EventId abortEvent =
609// scheduler.scheduleEvent(time::seconds(1),
610// bind(&EndToEndFixture::abortTestCase, this,
611// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700612//
Junxiao Shi79494162014-04-02 18:25:11 -0700613// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700614//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100615// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
616// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700617//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100618// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
619// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700620//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100621// channel2->connect("::1", "20070",
622// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
623// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
624// g_io.run();
625// g_io.reset();
626// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700627//
Junxiao Shi79494162014-04-02 18:25:11 -0700628// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700629//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100630// abortEvent =
631// scheduler.scheduleEvent(time::seconds(1),
632// bind(&EndToEndFixture::abortTestCase, this,
633// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700634//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100635// Interest interest1("ndn:/TpnzGvW9R");
636// Data data1 ("ndn:/KfczhUqVix");
637// data1.setContent(0, 0);
638// Interest interest2("ndn:/QWiIMfj5sL");
639// Data data2 ("ndn:/XNBV796f");
640// data2.setContent(0, 0);
641// Interest interest3("ndn:/QWiIhjgkj5sL");
642// Data data3 ("ndn:/XNBV794f");
643// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700644//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100645// ndn::SignatureSha256WithRsa fakeSignature;
646// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700647//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100648// // set fake signature on data1 and data2
649// data1.setSignature(fakeSignature);
650// data2.setSignature(fakeSignature);
651// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700652//
Junxiao Shi79494162014-04-02 18:25:11 -0700653// face2->sendInterest(interest2);
654// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700655//
Junxiao Shi79494162014-04-02 18:25:11 -0700656// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100657// g_io.run();
658// g_io.reset();
659// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700660//
Junxiao Shi79494162014-04-02 18:25:11 -0700661// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700662//
Junxiao Shi79494162014-04-02 18:25:11 -0700663// face1->sendInterest(interest1);
664// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700665//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100666// abortEvent =
667// scheduler.scheduleEvent(time::seconds(1),
668// bind(&EndToEndFixture::abortTestCase, this,
669// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700670// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100671// g_io.run();
672// g_io.reset();
673// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700674//
Junxiao Shi79494162014-04-02 18:25:11 -0700675// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
676// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
677// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
678// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700679//
Junxiao Shi79494162014-04-02 18:25:11 -0700680// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
681// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
682// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
683// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700684//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100685// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Junxiao Shi79494162014-04-02 18:25:11 -0700687// face2->sendData (data3 );
688// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700689//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100690// abortEvent =
691// scheduler.scheduleEvent(time::seconds(1),
692// bind(&EndToEndFixture::abortTestCase, this,
693// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700694// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100695// g_io.run();
696// g_io.reset();
697// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700698//
Junxiao Shi79494162014-04-02 18:25:11 -0700699// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
700// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700701//
Junxiao Shi79494162014-04-02 18:25:11 -0700702// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
703// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700704//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100705//}
706
707
708//Test commented because it required to be run in a machine that can resolve ipv6 query
709//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
710//{
711// Interest interest1("ndn:/TpnzGvW9R");
712// Interest interest2("ndn:/QWiIMfj5sL");
713// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700714//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100715// UdpFactory factory = UdpFactory();
716// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700717//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100718// EventId abortEvent =
719// scheduler.scheduleEvent(time::seconds(4),
720// bind(&EndToEndFixture::abortTestCase, this,
721// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700722//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100723// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
724// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700725//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100726// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
727// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700728//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100729// channel2->connect("::1", "20070",
730// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
731// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700732//
Junxiao Shi79494162014-04-02 18:25:11 -0700733// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100734// g_io.run();
735// g_io.reset();
736// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700737//
Junxiao Shi79494162014-04-02 18:25:11 -0700738// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700739//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100740// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
741// channel3->connect("::1", "20070",
742// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
743// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700744//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100745// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700746//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100747// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700748//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100749// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700750// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100751// bind(&UdpChannel::connect, channel4,
752// "::1", "20070",
753// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
754// channel_onFaceCreated, this, _1)),
755// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
756// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700757//
Junxiao Shi79494162014-04-02 18:25:11 -0700758// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100759// abortEvent =
760// scheduler.scheduleEvent(time::seconds(4),
761// bind(&EndToEndFixture::abortTestCase, this,
762// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100764// scheduler.scheduleEvent(time::seconds(0.4),
765// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700766//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100767// g_io.run();
768// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700769//
Junxiao Shi79494162014-04-02 18:25:11 -0700770// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700771//
772//
Junxiao Shi79494162014-04-02 18:25:11 -0700773// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100774// abortEvent =
775// scheduler.scheduleEvent(time::seconds(1),
776// bind(&EndToEndFixture::abortTestCase, this,
777// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700778// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100779// g_io.run();
780// g_io.reset();
781// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700782//
Junxiao Shi79494162014-04-02 18:25:11 -0700783// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700784//
Junxiao Shi79494162014-04-02 18:25:11 -0700785// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100786// abortEvent =
787// scheduler.scheduleEvent(time::seconds(1),
788// bind(&EndToEndFixture::abortTestCase, this,
789// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700790// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100791// g_io.run();
792// g_io.reset();
793// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700794//
Junxiao Shi79494162014-04-02 18:25:11 -0700795// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
796// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100797// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
798// this,
799// _1),
800// bind(&EndToEndFixture::channel_onConnectFailedOk,
801// this,
802// _1)),
803// UdpChannel::Error);
804//}
805
806
807BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
808{
809 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100810
Giulio Grassi624f6c62014-02-18 19:42:14 +0100811 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
812 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700813
Giulio Grassi624f6c62014-02-18 19:42:14 +0100814 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
815 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700816
Giulio Grassi624f6c62014-02-18 19:42:14 +0100817 channel2->connect("127.0.0.1", "20070",
818 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
819 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700820
Junxiao Shi79494162014-04-02 18:25:11 -0700821 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700822 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100823
824 BOOST_CHECK_EQUAL(channel2->size(), 1);
825
Junxiao Shi79494162014-04-02 18:25:11 -0700826 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100827
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700828 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700829 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700830
Junxiao Shi79494162014-04-02 18:25:11 -0700831 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700832 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100833
Junxiao Shi79494162014-04-02 18:25:11 -0700834 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700835
Giulio Grassi624f6c62014-02-18 19:42:14 +0100836 BOOST_CHECK_EQUAL(channel2->size(), 0);
837}
Junxiao Shi79494162014-04-02 18:25:11 -0700838
Giulio Grassi69871f02014-03-09 16:14:44 +0100839BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
840{
841 Interest interest1("ndn:/TpnzGvW9R");
842 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100843
Giulio Grassi69871f02014-03-09 16:14:44 +0100844 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100845
846 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100847 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100848 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100849 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100850
Giulio Grassi69871f02014-03-09 16:14:44 +0100851 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100852 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100853
854 channel2->connect("127.0.0.1", "20070",
855 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
856 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700857
858 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100859 "UdpChannel error: cannot connect or cannot accept connection");
860
Junxiao Shi79494162014-04-02 18:25:11 -0700861 face2->sendInterest(interest1);
862 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100863
Junxiao Shi79494162014-04-02 18:25:11 -0700864 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100865 time::seconds(1)) == LimitedIo::EXCEED_OPS,
866 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700867
868 BOOST_CHECK_EQUAL(faces.size(), 2);
869 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100870 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700871 BOOST_CHECK_EQUAL(faces.size(), 2);
872 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100873 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700874
Giulio Grassi69871f02014-03-09 16:14:44 +0100875 //the face on listen should be closed now
876 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700877 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100878 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700879 BOOST_REQUIRE(static_cast<bool>(face2));
880
881 face2->sendInterest(interest2);
882 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100883 time::seconds(1)) == LimitedIo::EXCEED_OPS,
884 "UdpChannel error: cannot send or receive Interest/Data packets");
885 //channel1 should have created a new face by now
886 BOOST_CHECK_EQUAL(channel1->size(), 1);
887 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700888 BOOST_REQUIRE(static_cast<bool>(face1));
889 BOOST_CHECK(face1->isOnDemand());
890
Giulio Grassi69871f02014-03-09 16:14:44 +0100891 channel1->connect("127.0.0.1", "20071",
892 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
893 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
894
Junxiao Shi79494162014-04-02 18:25:11 -0700895 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100896 time::seconds(1)) == LimitedIo::EXCEED_OPS,
897 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700898
Junxiao Shi79494162014-04-02 18:25:11 -0700899 BOOST_CHECK(!face1->isOnDemand());
900
901 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100902 //but it shouln't have created any additional faces
903 BOOST_CHECK_EQUAL(channel1->size(), 1);
904 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700905 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100906 "Idle face should be still open because it's permanent now");
907 //both faces are permanent, nothing should have changed
908 BOOST_CHECK_EQUAL(channel1->size(), 1);
909 BOOST_CHECK_EQUAL(channel2->size(), 1);
910}
911
Giulio Grassi624f6c62014-02-18 19:42:14 +0100912BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700913
Giulio Grassi624f6c62014-02-18 19:42:14 +0100914} // namespace tests
915} // namespace nfd