blob: d4983bf57e454e034360ca1bd0665b43b76be432 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "face/udp-factory.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +01008#include "core/face-uri.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +01009#include <ndn-cpp-dev/security/key-chain.hpp>
Junxiao Shi7e2413b2014-03-02 11:15:09 -070010
11#include "tests/test-common.hpp"
12#include "tests/core/limited-io.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010013
14namespace nfd {
15namespace tests {
16
17BOOST_FIXTURE_TEST_SUITE(FaceUdp, BaseFixture)
18
19class FactoryErrorCheck : protected BaseFixture
20{
21public:
22 bool isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
23 return strcmp(e.what(),
24 "Cannot create the requested UDP unicast channel, local "
25 "endpoint is already allocated for a UDP multicast face") == 0;
26 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070027
Giulio Grassi624f6c62014-02-18 19:42:14 +010028 bool isNotMulticastAddress(const UdpFactory::Error& e) {
29 return strcmp(e.what(),
30 "Cannot create the requested UDP multicast face, "
31 "the multicast group given as input is not a multicast address") == 0;
32 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070033
Giulio Grassi624f6c62014-02-18 19:42:14 +010034 bool isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
35 return strcmp(e.what(),
36 "Cannot create the requested UDP multicast face, local "
37 "endpoint is already allocated for a UDP unicast channel") == 0;
38 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070039
Giulio Grassi624f6c62014-02-18 19:42:14 +010040 bool isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
41 return strcmp(e.what(),
42 "Cannot create the requested UDP multicast face, local "
43 "endpoint is already allocated for a UDP multicast face "
44 "on a different multicast group") == 0;
45 }
46};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070047
Giulio Grassi624f6c62014-02-18 19:42:14 +010048BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
49{
50 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070051
Giulio Grassi624f6c62014-02-18 19:42:14 +010052 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070053
Giulio Grassi624f6c62014-02-18 19:42:14 +010054 //to instantiate multicast face on a specific ip address, change interfaceIp
55 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -070056
Giulio Grassi624f6c62014-02-18 19:42:14 +010057 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
58 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
59 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070060 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070061
Giulio Grassi624f6c62014-02-18 19:42:14 +010062 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
63 BOOST_CHECK_NE(channel1, channel2);
64
65 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066
Junxiao Shi61e3cc52014-03-03 20:40:28 -070067 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
68 BOOST_CHECK_NE(channel2, channel4);
69 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
70
Giulio Grassi624f6c62014-02-18 19:42:14 +010071 //same endpoint of a unicast channel
72 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
73 "224.0.0.1",
74 "20070"),
75 UdpFactory::Error,
76 isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070077
78
Giulio Grassi624f6c62014-02-18 19:42:14 +010079 shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp,
80 "224.0.0.1",
81 "20072");
82 shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp,
83 "224.0.0.1",
84 "20072");
85 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070086
87
Giulio Grassi624f6c62014-02-18 19:42:14 +010088 //same endpoint of a multicast face
89 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
90 UdpFactory::Error,
91 isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070092
Giulio Grassi624f6c62014-02-18 19:42:14 +010093 //same multicast endpoint, different group
94 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
95 "224.0.0.42",
96 "20072"),
97 UdpFactory::Error,
98 isLocalEndpointOnDifferentGroup);
99
100 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
101 "192.168.10.15",
102 "20025"),
103 UdpFactory::Error,
104 isNotMulticastAddress);
105
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700106
Giulio Grassi624f6c62014-02-18 19:42:14 +0100107// //Test commented because it required to be run in a machine that can resolve ipv6 query
108// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
109// "fe80::5e96:9dff:fe7d:9c8d%en1",
110// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
111// "20070");
112//
113// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
114// //an available network interface (different from the first one used)
115// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
116// "224.0.0.1",
117// "20073");
118// BOOST_CHECK_NE(multicastFace1, multicastFace2);
119//
120//
121// //ipv6 - work in progress
122// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
123// "FF01:0:0:0:0:0:0:2",
124// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700125//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100126// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
127// "FF01:0:0:0:0:0:0:2",
128// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700129//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100130// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
131//
132// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
133// "FF01:0:0:0:0:0:0:2",
134// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700135//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100136// BOOST_CHECK_NE(multicastFace3, multicastFace5);
137//
138// //same local ipv6 endpoint for a different multicast group
139// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
140// "FE01:0:0:0:0:0:0:2",
141// "20073"),
142// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700143//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100144// //same local ipv6 (expect for th port number) endpoint for a different multicast group
145// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
146// "FE01:0:0:0:0:0:0:2",
147// "20075"),
148// UdpFactory::Error);
149//
150// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
151// "FE12:0:0:0:0:0:0:2",
152// "20075"),
153// UdpFactory::Error);
154//
155// //not a multicast ipv6
156// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
157// "A112:0:0:0:0:0:0:2",
158// "20075"),
159// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100160}
161
162class EndToEndFixture : protected BaseFixture
163{
164public:
165 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700166 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100167 {
168 BOOST_CHECK(!static_cast<bool>(m_face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100169 channel1_onFaceCreatedNoCheck(newFace);
170 }
171
172 void
173 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
174 {
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175 m_face1 = newFace;
176 m_face1->onReceiveInterest +=
177 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
178 m_face1->onReceiveData +=
179 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700180 m_face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100181 bind(&EndToEndFixture::face1_onFail, this);
182 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
183
184 m_faces.push_back(m_face1);
185
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700186 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100187 }
188
189 void
190 channel1_onConnectFailed(const std::string& reason)
191 {
192 BOOST_CHECK_MESSAGE(false, reason);
193
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700194 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100195 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700196
Giulio Grassi624f6c62014-02-18 19:42:14 +0100197 void
198 face1_onReceiveInterest(const Interest& interest)
199 {
200 m_face1_receivedInterests.push_back(interest);
201
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700202 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100203 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700204
Giulio Grassi624f6c62014-02-18 19:42:14 +0100205 void
206 face1_onReceiveData(const Data& data)
207 {
208 m_face1_receivedDatas.push_back(data);
209
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700210 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100211 }
212
213 void
214 face1_onFail()
215 {
216 m_face1.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700217 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100218 }
219
220 void
221 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
222 {
223 BOOST_CHECK(!static_cast<bool>(m_face2));
224 m_face2 = newFace;
225 m_face2->onReceiveInterest +=
226 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
227 m_face2->onReceiveData +=
228 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700229 m_face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100230 bind(&EndToEndFixture::face2_onFail, this);
231
232 m_faces.push_back(m_face2);
233
234 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700235 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100236 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700237
Giulio Grassi624f6c62014-02-18 19:42:14 +0100238 void
239 channel2_onConnectFailed(const std::string& reason)
240 {
241 BOOST_CHECK_MESSAGE(false, reason);
242
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700243 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100244 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700245
Giulio Grassi624f6c62014-02-18 19:42:14 +0100246 void
247 face2_onReceiveInterest(const Interest& interest)
248 {
249 m_face2_receivedInterests.push_back(interest);
250
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700251 m_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 face2_onReceiveData(const Data& data)
256 {
257 m_face2_receivedDatas.push_back(data);
258
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700259 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100260 }
261
262 void
263 face2_onFail()
264 {
265 m_face2.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700266 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100267 }
268
269 void
270 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
271 {
272 BOOST_CHECK(!static_cast<bool>(m_face1));
273 m_face3 = newFace;
274 m_faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700275
276 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100277 }
278
Giulio Grassi624f6c62014-02-18 19:42:14 +0100279 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700280 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100281 {
282 m_faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700283 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100284 }
285
286 void
287 channel_onConnectFailed(const std::string& reason)
288 {
289 BOOST_CHECK_MESSAGE(false, reason);
290
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700291 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100292 }
293
294 void
295 channel_onConnectFailedOk(const std::string& reason)
296 {
297 //it's ok, it was supposed to fail
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700298 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100299 }
300
301 void
302 checkFaceList(size_t shouldBe)
303 {
304 BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
305 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100306
307public:
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700308 LimitedIo m_limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100309
310 shared_ptr<Face> m_face1;
311 std::vector<Interest> m_face1_receivedInterests;
312 std::vector<Data> m_face1_receivedDatas;
313 shared_ptr<Face> m_face2;
314 std::vector<Interest> m_face2_receivedInterests;
315 std::vector<Data> m_face2_receivedDatas;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100316 shared_ptr<Face> m_face3;
317
Giulio Grassi624f6c62014-02-18 19:42:14 +0100318 std::list< shared_ptr<Face> > m_faces;
319};
320
321
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000322BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100323{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700324 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100325
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000326 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700327
Giulio Grassi624f6c62014-02-18 19:42:14 +0100328 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
329 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
330 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700331
332
333 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
334 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335
336 BOOST_REQUIRE(static_cast<bool>(m_face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000337 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp4://127.0.0.1:20070");
338 BOOST_CHECK_EQUAL(m_face2->isLocal(), false);
339 // m_face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700340
Giulio Grassi624f6c62014-02-18 19:42:14 +0100341 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
342 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
343 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700344
Giulio Grassi624f6c62014-02-18 19:42:14 +0100345 Interest interest1("ndn:/TpnzGvW9R");
346 Data data1 ("ndn:/KfczhUqVix");
347 data1.setContent(0, 0);
348 Interest interest2("ndn:/QWiIMfj5sL");
349 Data data2 ("ndn:/XNBV796f");
350 data2.setContent(0, 0);
351 Interest interest3("ndn:/QWiIhjgkj5sL");
352 Data data3 ("ndn:/XNBV794f");
353 data3.setContent(0, 0);
354
355
356 ndn::SignatureSha256WithRsa fakeSignature;
357 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
358 reinterpret_cast<const uint8_t*>(0),
359 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700360
Giulio Grassi624f6c62014-02-18 19:42:14 +0100361 // set fake signature on data1 and data2
362 data1.setSignature(fakeSignature);
363 data2.setSignature(fakeSignature);
364 data3.setSignature(fakeSignature);
365
366 m_face2->sendInterest(interest2);
367 m_face2->sendData (data2 );
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000368 m_face2->sendData (data2 );
369 m_face2->sendData (data2 );
Giulio Grassi624f6c62014-02-18 19:42:14 +0100370
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000371 BOOST_CHECK_MESSAGE(m_limitedIo.run(5,//4 send + 1 listen return
372 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700373 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100374
375 BOOST_REQUIRE(static_cast<bool>(m_face1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000376 BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp4://127.0.0.1:20071");
377 BOOST_CHECK_EQUAL(m_face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700378
Giulio Grassi624f6c62014-02-18 19:42:14 +0100379 m_face1->sendInterest(interest1);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000380 m_face1->sendInterest(interest1);
381 m_face1->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100382 m_face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700383
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000384 BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700385 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386
387
388 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000389 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 3);
390 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 3);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100391 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700392
Giulio Grassi624f6c62014-02-18 19:42:14 +0100393 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
394 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
395 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
396 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700397
398
399
Giulio Grassi624f6c62014-02-18 19:42:14 +0100400 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700401
Giulio Grassi624f6c62014-02-18 19:42:14 +0100402 m_face2->sendData (data3 );
403 m_face2->sendInterest(interest3);
404
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700405 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
406 "UdpChannel error: cannot send or receive Interest/Data packets");
407
Giulio Grassi624f6c62014-02-18 19:42:14 +0100408 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000409 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700410
Giulio Grassi624f6c62014-02-18 19:42:14 +0100411 BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000412 BOOST_CHECK_EQUAL(m_face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000413
414 const FaceCounters& counters1 = m_face1->getCounters();
415 BOOST_CHECK_EQUAL(counters1.getInInterest() , 2);
416 BOOST_CHECK_EQUAL(counters1.getInData() , 4);
417 BOOST_CHECK_EQUAL(counters1.getOutInterest(), 3);
418 BOOST_CHECK_EQUAL(counters1.getOutData() , 1);
419
420 const FaceCounters& counters2 = m_face2->getCounters();
421 BOOST_CHECK_EQUAL(counters2.getInInterest() , 3);
422 BOOST_CHECK_EQUAL(counters2.getInData() , 1);
423 BOOST_CHECK_EQUAL(counters2.getOutInterest(), 2);
424 BOOST_CHECK_EQUAL(counters2.getOutData() , 4);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000425}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100426
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000427BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
428{
429 UdpFactory factory;
430
431 factory.createChannel("::1", "20071");
432
433 factory.createFace(FaceUri("udp://[::1]:20070"),
434 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
435 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
436
437
438 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
439 "UdpChannel error: cannot connect or cannot accept connection");
440
441 BOOST_REQUIRE(static_cast<bool>(m_face2));
442 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp6://[::1]:20070");
443 BOOST_CHECK_EQUAL(m_face2->isLocal(), false);
444 // m_face1 is not created yet
445
446 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
447 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
448 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
449
450 Interest interest1("ndn:/TpnzGvW9R");
451 Data data1 ("ndn:/KfczhUqVix");
452 data1.setContent(0, 0);
453 Interest interest2("ndn:/QWiIMfj5sL");
454 Data data2 ("ndn:/XNBV796f");
455 data2.setContent(0, 0);
456 Interest interest3("ndn:/QWiIhjgkj5sL");
457 Data data3 ("ndn:/XNBV794f");
458 data3.setContent(0, 0);
459
460
461 ndn::SignatureSha256WithRsa fakeSignature;
462 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
463 reinterpret_cast<const uint8_t*>(0),
464 0));
465
466 // set fake signature on data1 and data2
467 data1.setSignature(fakeSignature);
468 data2.setSignature(fakeSignature);
469 data3.setSignature(fakeSignature);
470
471 m_face2->sendInterest(interest2);
472 m_face2->sendData (data2 );
473
474 BOOST_CHECK_MESSAGE(m_limitedIo.run(3,//2 send + 1 listen return
475 time::seconds(1)) == LimitedIo::EXCEED_OPS,
476 "UdpChannel error: cannot send or receive Interest/Data packets");
477
478 BOOST_REQUIRE(static_cast<bool>(m_face1));
479 BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp6://[::1]:20071");
480 BOOST_CHECK_EQUAL(m_face1->isLocal(), false);
481
482 m_face1->sendInterest(interest1);
483 m_face1->sendData (data1 );
484
485 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
486 "UdpChannel error: cannot send or receive Interest/Data packets");
487
488
489 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
490 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
491 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
492 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
493
494 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
495 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
496 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
497 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
498
499
500
501 //checking if the connection accepting mechanism works properly.
502
503 m_face2->sendData (data3 );
504 m_face2->sendInterest(interest3);
505
506 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
507 "UdpChannel error: cannot send or receive Interest/Data packets");
508
509 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
510 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2);
511
512 BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
513 BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100514}
515
516BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
517{
518 Interest interest1("ndn:/TpnzGvW9R");
519 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100520
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700521 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100522
Giulio Grassi624f6c62014-02-18 19:42:14 +0100523 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
524 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700525
Giulio Grassi624f6c62014-02-18 19:42:14 +0100526 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
527 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700528
Giulio Grassi624f6c62014-02-18 19:42:14 +0100529 channel2->connect("127.0.0.1", "20070",
530 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
531 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700532
533
534 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
535 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100536
537 BOOST_CHECK_EQUAL(m_faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700538
Giulio Grassi624f6c62014-02-18 19:42:14 +0100539 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
540 channel3->connect("127.0.0.1", "20070",
541 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
542 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700543
Giulio Grassi624f6c62014-02-18 19:42:14 +0100544 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
545
546 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100547
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700548 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700549 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
550 // does not work without static_cast
551 static_cast<UdpChannel::FaceCreatedCallback>(
552 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
553 static_cast<UdpChannel::ConnectFailedCallback>(
554 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
555
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700556 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700557
558 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,// 2 connects
559 time::seconds(4)) == LimitedIo::EXCEED_OPS,
560 "UdpChannel error: cannot connect or cannot accept multiple connections");
561
Giulio Grassi624f6c62014-02-18 19:42:14 +0100562 BOOST_CHECK_EQUAL(m_faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700563
564
Giulio Grassi624f6c62014-02-18 19:42:14 +0100565 m_face2->sendInterest(interest1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700566 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
567 "UdpChannel error: cannot send or receive Interest/Data packets");
568
Giulio Grassi624f6c62014-02-18 19:42:14 +0100569 BOOST_CHECK_EQUAL(m_faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700570
Giulio Grassi624f6c62014-02-18 19:42:14 +0100571 m_face3->sendInterest(interest2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700572 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
573 "UdpChannel error: cannot send or receive Interest/Data packets");
574
Giulio Grassi624f6c62014-02-18 19:42:14 +0100575 //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest
576 BOOST_CHECK_EQUAL(m_faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100577 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
578 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100579 UdpChannel::Error);
580}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700581
Giulio Grassi624f6c62014-02-18 19:42:14 +0100582//Test commented because it required to be run in a machine that can resolve ipv6 query
583//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
584//{
585// UdpFactory factory = UdpFactory();
586// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700587//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100588// EventId abortEvent =
589// scheduler.scheduleEvent(time::seconds(1),
590// bind(&EndToEndFixture::abortTestCase, this,
591// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700592//
593// m_limitedIoRemaining = 1;
594//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100595// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
596// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700597//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100598// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
599// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700600//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100601// channel2->connect("::1", "20070",
602// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
603// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
604// g_io.run();
605// g_io.reset();
606// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700607//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100608// BOOST_REQUIRE(static_cast<bool>(m_face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700609//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100610// abortEvent =
611// scheduler.scheduleEvent(time::seconds(1),
612// bind(&EndToEndFixture::abortTestCase, this,
613// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700614//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100615// Interest interest1("ndn:/TpnzGvW9R");
616// Data data1 ("ndn:/KfczhUqVix");
617// data1.setContent(0, 0);
618// Interest interest2("ndn:/QWiIMfj5sL");
619// Data data2 ("ndn:/XNBV796f");
620// data2.setContent(0, 0);
621// Interest interest3("ndn:/QWiIhjgkj5sL");
622// Data data3 ("ndn:/XNBV794f");
623// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700624//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100625// ndn::SignatureSha256WithRsa fakeSignature;
626// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700627//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100628// // set fake signature on data1 and data2
629// data1.setSignature(fakeSignature);
630// data2.setSignature(fakeSignature);
631// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700632//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100633// m_face2->sendInterest(interest2);
634// m_face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700635//
636// m_limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100637// g_io.run();
638// g_io.reset();
639// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700640//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100641// BOOST_REQUIRE(static_cast<bool>(m_face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700642//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100643// m_face1->sendInterest(interest1);
644// m_face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700645//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100646// abortEvent =
647// scheduler.scheduleEvent(time::seconds(1),
648// bind(&EndToEndFixture::abortTestCase, this,
649// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700650// m_limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100651// g_io.run();
652// g_io.reset();
653// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700654//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100655// BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
656// BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
657// BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
658// BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700659//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100660// BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
661// BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
662// BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
663// BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700664//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100665// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700666//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100667// m_face2->sendData (data3 );
668// m_face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700669//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100670// abortEvent =
671// scheduler.scheduleEvent(time::seconds(1),
672// bind(&EndToEndFixture::abortTestCase, this,
673// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700674// m_limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100675// g_io.run();
676// g_io.reset();
677// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700678//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100679// BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
680// BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700681//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100682// BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
683// BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700684//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100685//}
686
687
688//Test commented because it required to be run in a machine that can resolve ipv6 query
689//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
690//{
691// Interest interest1("ndn:/TpnzGvW9R");
692// Interest interest2("ndn:/QWiIMfj5sL");
693// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700694//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100695// UdpFactory factory = UdpFactory();
696// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700697//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100698// EventId abortEvent =
699// scheduler.scheduleEvent(time::seconds(4),
700// bind(&EndToEndFixture::abortTestCase, this,
701// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700702//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100703// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
704// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700705//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100706// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
707// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700708//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100709// channel2->connect("::1", "20070",
710// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
711// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700712//
713// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100714// g_io.run();
715// g_io.reset();
716// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700717//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100718// BOOST_CHECK_EQUAL(m_faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700719//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100720// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
721// channel3->connect("::1", "20070",
722// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
723// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700724//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100725// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700726//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100727// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700728//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100729// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700730// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100731// bind(&UdpChannel::connect, channel4,
732// "::1", "20070",
733// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
734// channel_onFaceCreated, this, _1)),
735// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
736// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700737//
738// m_limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100739// abortEvent =
740// scheduler.scheduleEvent(time::seconds(4),
741// bind(&EndToEndFixture::abortTestCase, this,
742// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700743//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100744// scheduler.scheduleEvent(time::seconds(0.4),
745// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700746//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100747// g_io.run();
748// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700749//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100750// BOOST_CHECK_EQUAL(m_faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700751//
752//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100753// m_face2->sendInterest(interest1);
754// abortEvent =
755// scheduler.scheduleEvent(time::seconds(1),
756// bind(&EndToEndFixture::abortTestCase, this,
757// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700758// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100759// g_io.run();
760// g_io.reset();
761// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700762//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100763// BOOST_CHECK_EQUAL(m_faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700764//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100765// m_face3->sendInterest(interest2);
766// abortEvent =
767// scheduler.scheduleEvent(time::seconds(1),
768// bind(&EndToEndFixture::abortTestCase, this,
769// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700770// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100771// g_io.run();
772// g_io.reset();
773// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700774//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100775// //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest
776// BOOST_CHECK_EQUAL(m_faces.size(), 5);
777// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
778// this,
779// _1),
780// bind(&EndToEndFixture::channel_onConnectFailedOk,
781// this,
782// _1)),
783// UdpChannel::Error);
784//}
785
786
787BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
788{
789 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100790
Giulio Grassi624f6c62014-02-18 19:42:14 +0100791 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
792 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700793
Giulio Grassi624f6c62014-02-18 19:42:14 +0100794 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
795 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700796
Giulio Grassi624f6c62014-02-18 19:42:14 +0100797 channel2->connect("127.0.0.1", "20070",
798 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
799 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700800
801 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
802 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100803
804 BOOST_CHECK_EQUAL(channel2->size(), 1);
805
Giulio Grassi624f6c62014-02-18 19:42:14 +0100806 BOOST_CHECK(static_cast<bool>(m_face2));
807
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700808 // Face::close must be invoked during io run to be counted as an op
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700809 scheduler::schedule(time::milliseconds(100), bind(&Face::close, m_face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700810
811 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
812 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100813
814 BOOST_CHECK(!static_cast<bool>(m_face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700815
Giulio Grassi624f6c62014-02-18 19:42:14 +0100816 BOOST_CHECK_EQUAL(channel2->size(), 0);
817}
Giulio Grassi69871f02014-03-09 16:14:44 +0100818
819BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
820{
821 Interest interest1("ndn:/TpnzGvW9R");
822 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100823
Giulio Grassi69871f02014-03-09 16:14:44 +0100824 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100825
826 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100827 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100828 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100829 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100830
Giulio Grassi69871f02014-03-09 16:14:44 +0100831 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100832 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100833
834 channel2->connect("127.0.0.1", "20070",
835 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
836 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
837
838 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
839 "UdpChannel error: cannot connect or cannot accept connection");
840
841 m_face2->sendInterest(interest1);
842
843 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,//1 send + 1 listen return
844 time::seconds(1)) == LimitedIo::EXCEED_OPS,
845 "UdpChannel error: cannot send or receive Interest/Data packets");
846
847 BOOST_CHECK_EQUAL(m_faces.size(), 2);
848 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
849 "Idle face should be still open because has been used recently");
850 BOOST_CHECK_EQUAL(m_faces.size(), 2);
851 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
852 "Closing idle face error: face should be closed by now");
853
854 //the face on listen should be closed now
855 BOOST_CHECK_EQUAL(channel1->size(), 0);
856 //checking that m_face2 has not been closed
857 BOOST_CHECK_EQUAL(channel2->size(), 1);
858 BOOST_REQUIRE(static_cast<bool>(m_face2));
859
860 m_face2->sendInterest(interest2);
861 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,//1 send + 1 listen return
862 time::seconds(1)) == LimitedIo::EXCEED_OPS,
863 "UdpChannel error: cannot send or receive Interest/Data packets");
864 //channel1 should have created a new face by now
865 BOOST_CHECK_EQUAL(channel1->size(), 1);
866 BOOST_CHECK_EQUAL(channel2->size(), 1);
867 BOOST_REQUIRE(static_cast<bool>(m_face1));
868
869 channel1->connect("127.0.0.1", "20071",
870 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
871 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
872
873 BOOST_CHECK_MESSAGE(m_limitedIo.run(1,//1 connect
874 time::seconds(1)) == LimitedIo::EXCEED_OPS,
875 "UdpChannel error: cannot connect");
876
877 //the connect should have set m_face1 as permanent face,
878 //but it shouln't have created any additional faces
879 BOOST_CHECK_EQUAL(channel1->size(), 1);
880 BOOST_CHECK_EQUAL(channel2->size(), 1);
881 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
882 "Idle face should be still open because it's permanent now");
883 //both faces are permanent, nothing should have changed
884 BOOST_CHECK_EQUAL(channel1->size(), 1);
885 BOOST_CHECK_EQUAL(channel2->size(), 1);
886}
887
Giulio Grassi624f6c62014-02-18 19:42:14 +0100888BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700889
Giulio Grassi624f6c62014-02-18 19:42:14 +0100890} // namespace tests
891} // namespace nfd