blob: e161cfa9c9fac53315515bccc66f3952955d7a8a [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
107
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108// //Test commented because it required to be run in a machine that can resolve ipv6 query
109// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
110// "fe80::5e96:9dff:fe7d:9c8d%en1",
111// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
112// "20070");
113//
114// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
115// //an available network interface (different from the first one used)
116// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
117// "224.0.0.1",
118// "20073");
119// BOOST_CHECK_NE(multicastFace1, multicastFace2);
120//
121//
122// //ipv6 - work in progress
123// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
124// "FF01:0:0:0:0:0:0:2",
125// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700126//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100127// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
128// "FF01:0:0:0:0:0:0:2",
129// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700130//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100131// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
132//
133// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
134// "FF01:0:0:0:0:0:0:2",
135// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700136//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100137// BOOST_CHECK_NE(multicastFace3, multicastFace5);
138//
139// //same local ipv6 endpoint for a different multicast group
140// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
141// "FE01:0:0:0:0:0:0:2",
142// "20073"),
143// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700144//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100145// //same local ipv6 (expect for th port number) endpoint for a different multicast group
146// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
147// "FE01:0:0:0:0:0:0:2",
148// "20075"),
149// UdpFactory::Error);
150//
151// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
152// "FE12:0:0:0:0:0:0:2",
153// "20075"),
154// UdpFactory::Error);
155//
156// //not a multicast ipv6
157// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
158// "A112:0:0:0:0:0:0:2",
159// "20075"),
160// UdpFactory::Error);
161
162
163
164}
165
166class EndToEndFixture : protected BaseFixture
167{
168public:
169 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700170 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100171 {
172 BOOST_CHECK(!static_cast<bool>(m_face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100173 channel1_onFaceCreatedNoCheck(newFace);
174 }
175
176 void
177 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
178 {
Giulio Grassi624f6c62014-02-18 19:42:14 +0100179 m_face1 = newFace;
180 m_face1->onReceiveInterest +=
181 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
182 m_face1->onReceiveData +=
183 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700184 m_face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185 bind(&EndToEndFixture::face1_onFail, this);
186 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
187
188 m_faces.push_back(m_face1);
189
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700190 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100191 }
192
193 void
194 channel1_onConnectFailed(const std::string& reason)
195 {
196 BOOST_CHECK_MESSAGE(false, reason);
197
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700198 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100199 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700200
Giulio Grassi624f6c62014-02-18 19:42:14 +0100201 void
202 face1_onReceiveInterest(const Interest& interest)
203 {
204 m_face1_receivedInterests.push_back(interest);
205
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700206 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100207 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700208
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 void
210 face1_onReceiveData(const Data& data)
211 {
212 m_face1_receivedDatas.push_back(data);
213
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700214 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100215 }
216
217 void
218 face1_onFail()
219 {
220 m_face1.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700221 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100222 }
223
224 void
225 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
226 {
227 BOOST_CHECK(!static_cast<bool>(m_face2));
228 m_face2 = newFace;
229 m_face2->onReceiveInterest +=
230 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
231 m_face2->onReceiveData +=
232 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700233 m_face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100234 bind(&EndToEndFixture::face2_onFail, this);
235
236 m_faces.push_back(m_face2);
237
238 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700239 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100240 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700241
Giulio Grassi624f6c62014-02-18 19:42:14 +0100242 void
243 channel2_onConnectFailed(const std::string& reason)
244 {
245 BOOST_CHECK_MESSAGE(false, reason);
246
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700247 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100248 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700249
Giulio Grassi624f6c62014-02-18 19:42:14 +0100250 void
251 face2_onReceiveInterest(const Interest& interest)
252 {
253 m_face2_receivedInterests.push_back(interest);
254
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700255 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100256 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700257
Giulio Grassi624f6c62014-02-18 19:42:14 +0100258 void
259 face2_onReceiveData(const Data& data)
260 {
261 m_face2_receivedDatas.push_back(data);
262
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700263 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100264 }
265
266 void
267 face2_onFail()
268 {
269 m_face2.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700270 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100271 }
272
273 void
274 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
275 {
276 BOOST_CHECK(!static_cast<bool>(m_face1));
277 m_face3 = newFace;
278 m_faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700279
280 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100281 }
282
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700283
Giulio Grassi624f6c62014-02-18 19:42:14 +0100284 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700285 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100286 {
287 m_faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700288 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100289 }
290
291 void
292 channel_onConnectFailed(const std::string& reason)
293 {
294 BOOST_CHECK_MESSAGE(false, reason);
295
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700296 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100297 }
298
299 void
300 channel_onConnectFailedOk(const std::string& reason)
301 {
302 //it's ok, it was supposed to fail
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700303 m_limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100304 }
305
306 void
307 checkFaceList(size_t shouldBe)
308 {
309 BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
310 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100311
312public:
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700313 LimitedIo m_limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100314
315 shared_ptr<Face> m_face1;
316 std::vector<Interest> m_face1_receivedInterests;
317 std::vector<Data> m_face1_receivedDatas;
318 shared_ptr<Face> m_face2;
319 std::vector<Interest> m_face2_receivedInterests;
320 std::vector<Data> m_face2_receivedDatas;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700321
Giulio Grassi624f6c62014-02-18 19:42:14 +0100322 shared_ptr<Face> m_face3;
323
324
325 std::list< shared_ptr<Face> > m_faces;
326};
327
328
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000329BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100330{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700331 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100332
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000333 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700334
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
336 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
337 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700338
339
340 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
341 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100342
343 BOOST_REQUIRE(static_cast<bool>(m_face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000344 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp4://127.0.0.1:20070");
345 BOOST_CHECK_EQUAL(m_face2->isLocal(), false);
346 // m_face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700347
Giulio Grassi624f6c62014-02-18 19:42:14 +0100348 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
349 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
350 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700351
Giulio Grassi624f6c62014-02-18 19:42:14 +0100352 Interest interest1("ndn:/TpnzGvW9R");
353 Data data1 ("ndn:/KfczhUqVix");
354 data1.setContent(0, 0);
355 Interest interest2("ndn:/QWiIMfj5sL");
356 Data data2 ("ndn:/XNBV796f");
357 data2.setContent(0, 0);
358 Interest interest3("ndn:/QWiIhjgkj5sL");
359 Data data3 ("ndn:/XNBV794f");
360 data3.setContent(0, 0);
361
362
363 ndn::SignatureSha256WithRsa fakeSignature;
364 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
365 reinterpret_cast<const uint8_t*>(0),
366 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700367
Giulio Grassi624f6c62014-02-18 19:42:14 +0100368 // set fake signature on data1 and data2
369 data1.setSignature(fakeSignature);
370 data2.setSignature(fakeSignature);
371 data3.setSignature(fakeSignature);
372
373 m_face2->sendInterest(interest2);
374 m_face2->sendData (data2 );
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000375 m_face2->sendData (data2 );
376 m_face2->sendData (data2 );
Giulio Grassi624f6c62014-02-18 19:42:14 +0100377
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000378 BOOST_CHECK_MESSAGE(m_limitedIo.run(5,//4 send + 1 listen return
379 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700380 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100381
382 BOOST_REQUIRE(static_cast<bool>(m_face1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000383 BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp4://127.0.0.1:20071");
384 BOOST_CHECK_EQUAL(m_face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700385
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386 m_face1->sendInterest(interest1);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000387 m_face1->sendInterest(interest1);
388 m_face1->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100389 m_face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700390
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000391 BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700392 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100393
394
395 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000396 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 3);
397 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 3);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100398 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700399
Giulio Grassi624f6c62014-02-18 19:42:14 +0100400 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
401 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
402 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
403 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700404
405
406
Giulio Grassi624f6c62014-02-18 19:42:14 +0100407 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700408
Giulio Grassi624f6c62014-02-18 19:42:14 +0100409 m_face2->sendData (data3 );
410 m_face2->sendInterest(interest3);
411
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700412 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
413 "UdpChannel error: cannot send or receive Interest/Data packets");
414
Giulio Grassi624f6c62014-02-18 19:42:14 +0100415 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000416 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700417
Giulio Grassi624f6c62014-02-18 19:42:14 +0100418 BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000419 BOOST_CHECK_EQUAL(m_face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000420
421 const FaceCounters& counters1 = m_face1->getCounters();
422 BOOST_CHECK_EQUAL(counters1.getInInterest() , 2);
423 BOOST_CHECK_EQUAL(counters1.getInData() , 4);
424 BOOST_CHECK_EQUAL(counters1.getOutInterest(), 3);
425 BOOST_CHECK_EQUAL(counters1.getOutData() , 1);
426
427 const FaceCounters& counters2 = m_face2->getCounters();
428 BOOST_CHECK_EQUAL(counters2.getInInterest() , 3);
429 BOOST_CHECK_EQUAL(counters2.getInData() , 1);
430 BOOST_CHECK_EQUAL(counters2.getOutInterest(), 2);
431 BOOST_CHECK_EQUAL(counters2.getOutData() , 4);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000432}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100433
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000434BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
435{
436 UdpFactory factory;
437
438 factory.createChannel("::1", "20071");
439
440 factory.createFace(FaceUri("udp://[::1]:20070"),
441 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
442 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
443
444
445 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
446 "UdpChannel error: cannot connect or cannot accept connection");
447
448 BOOST_REQUIRE(static_cast<bool>(m_face2));
449 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp6://[::1]:20070");
450 BOOST_CHECK_EQUAL(m_face2->isLocal(), false);
451 // m_face1 is not created yet
452
453 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
454 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
455 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
456
457 Interest interest1("ndn:/TpnzGvW9R");
458 Data data1 ("ndn:/KfczhUqVix");
459 data1.setContent(0, 0);
460 Interest interest2("ndn:/QWiIMfj5sL");
461 Data data2 ("ndn:/XNBV796f");
462 data2.setContent(0, 0);
463 Interest interest3("ndn:/QWiIhjgkj5sL");
464 Data data3 ("ndn:/XNBV794f");
465 data3.setContent(0, 0);
466
467
468 ndn::SignatureSha256WithRsa fakeSignature;
469 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
470 reinterpret_cast<const uint8_t*>(0),
471 0));
472
473 // set fake signature on data1 and data2
474 data1.setSignature(fakeSignature);
475 data2.setSignature(fakeSignature);
476 data3.setSignature(fakeSignature);
477
478 m_face2->sendInterest(interest2);
479 m_face2->sendData (data2 );
480
481 BOOST_CHECK_MESSAGE(m_limitedIo.run(3,//2 send + 1 listen return
482 time::seconds(1)) == LimitedIo::EXCEED_OPS,
483 "UdpChannel error: cannot send or receive Interest/Data packets");
484
485 BOOST_REQUIRE(static_cast<bool>(m_face1));
486 BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp6://[::1]:20071");
487 BOOST_CHECK_EQUAL(m_face1->isLocal(), false);
488
489 m_face1->sendInterest(interest1);
490 m_face1->sendData (data1 );
491
492 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
493 "UdpChannel error: cannot send or receive Interest/Data packets");
494
495
496 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
497 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
498 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
499 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
500
501 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
502 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
503 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
504 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
505
506
507
508 //checking if the connection accepting mechanism works properly.
509
510 m_face2->sendData (data3 );
511 m_face2->sendInterest(interest3);
512
513 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
514 "UdpChannel error: cannot send or receive Interest/Data packets");
515
516 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
517 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2);
518
519 BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
520 BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100521}
522
523BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
524{
525 Interest interest1("ndn:/TpnzGvW9R");
526 Interest interest2("ndn:/QWiIMfj5sL");
527 Interest interest3("ndn:/QWiIhjgkj5sL");
528
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700529 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100530
Giulio Grassi624f6c62014-02-18 19:42:14 +0100531 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
532 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700533
534
535
Giulio Grassi624f6c62014-02-18 19:42:14 +0100536 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
537 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700538
Giulio Grassi624f6c62014-02-18 19:42:14 +0100539 channel2->connect("127.0.0.1", "20070",
540 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
541 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700542
543
544 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
545 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100546
547 BOOST_CHECK_EQUAL(m_faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700548
Giulio Grassi624f6c62014-02-18 19:42:14 +0100549 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
550 channel3->connect("127.0.0.1", "20070",
551 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
552 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700553
Giulio Grassi624f6c62014-02-18 19:42:14 +0100554 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
555
556 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100557
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700558 scheduler::schedule(time::seconds(0.5),
559 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
560 // does not work without static_cast
561 static_cast<UdpChannel::FaceCreatedCallback>(
562 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
563 static_cast<UdpChannel::ConnectFailedCallback>(
564 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
565
566 scheduler::schedule(time::seconds(0.4), bind(&EndToEndFixture::checkFaceList, this, 2));
567
568 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,// 2 connects
569 time::seconds(4)) == LimitedIo::EXCEED_OPS,
570 "UdpChannel error: cannot connect or cannot accept multiple connections");
571
Giulio Grassi624f6c62014-02-18 19:42:14 +0100572 BOOST_CHECK_EQUAL(m_faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700573
574
Giulio Grassi624f6c62014-02-18 19:42:14 +0100575 m_face2->sendInterest(interest1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700576 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
577 "UdpChannel error: cannot send or receive Interest/Data packets");
578
Giulio Grassi624f6c62014-02-18 19:42:14 +0100579 BOOST_CHECK_EQUAL(m_faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700580
Giulio Grassi624f6c62014-02-18 19:42:14 +0100581 m_face3->sendInterest(interest2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700582 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
583 "UdpChannel error: cannot send or receive Interest/Data packets");
584
Giulio Grassi624f6c62014-02-18 19:42:14 +0100585 //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest
586 BOOST_CHECK_EQUAL(m_faces.size(), 5);
587 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
588 this,
589 _1),
590 bind(&EndToEndFixture::channel_onConnectFailedOk,
591 this,
592 _1)),
593 UdpChannel::Error);
594}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700595
Giulio Grassi624f6c62014-02-18 19:42:14 +0100596//Test commented because it required to be run in a machine that can resolve ipv6 query
597//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
598//{
599// UdpFactory factory = UdpFactory();
600// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700601//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100602// EventId abortEvent =
603// scheduler.scheduleEvent(time::seconds(1),
604// bind(&EndToEndFixture::abortTestCase, this,
605// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700606//
607// m_limitedIoRemaining = 1;
608//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100609// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
610// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700611//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100612// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
613// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700614//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100615// channel2->connect("::1", "20070",
616// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
617// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
618// g_io.run();
619// g_io.reset();
620// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700621//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100622// BOOST_REQUIRE(static_cast<bool>(m_face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700623//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100624// abortEvent =
625// scheduler.scheduleEvent(time::seconds(1),
626// bind(&EndToEndFixture::abortTestCase, this,
627// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700628//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100629// Interest interest1("ndn:/TpnzGvW9R");
630// Data data1 ("ndn:/KfczhUqVix");
631// data1.setContent(0, 0);
632// Interest interest2("ndn:/QWiIMfj5sL");
633// Data data2 ("ndn:/XNBV796f");
634// data2.setContent(0, 0);
635// Interest interest3("ndn:/QWiIhjgkj5sL");
636// Data data3 ("ndn:/XNBV794f");
637// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700638//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100639// ndn::SignatureSha256WithRsa fakeSignature;
640// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700641//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100642// // set fake signature on data1 and data2
643// data1.setSignature(fakeSignature);
644// data2.setSignature(fakeSignature);
645// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700646//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100647// m_face2->sendInterest(interest2);
648// m_face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700649//
650// m_limitedIoRemaining = 3; //2 send + 1 listen return
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(static_cast<bool>(m_face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700656//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100657// m_face1->sendInterest(interest1);
658// m_face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700659//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100660// abortEvent =
661// scheduler.scheduleEvent(time::seconds(1),
662// bind(&EndToEndFixture::abortTestCase, this,
663// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700664// m_limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100665// g_io.run();
666// g_io.reset();
667// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700668//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100669// BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
670// BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
671// BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
672// BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700673//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100674// BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
675// BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
676// BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
677// BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700678//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100679// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700680//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100681// m_face2->sendData (data3 );
682// m_face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700683//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100684// abortEvent =
685// scheduler.scheduleEvent(time::seconds(1),
686// bind(&EndToEndFixture::abortTestCase, this,
687// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700688// m_limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100689// g_io.run();
690// g_io.reset();
691// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700692//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100693// BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2);
694// BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700695//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100696// BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName());
697// BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700698//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100699//}
700
701
702//Test commented because it required to be run in a machine that can resolve ipv6 query
703//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
704//{
705// Interest interest1("ndn:/TpnzGvW9R");
706// Interest interest2("ndn:/QWiIMfj5sL");
707// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700708//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100709// UdpFactory factory = UdpFactory();
710// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700711//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100712// EventId abortEvent =
713// scheduler.scheduleEvent(time::seconds(4),
714// bind(&EndToEndFixture::abortTestCase, this,
715// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700716//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100717// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
718// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700719//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100720// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
721// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700722//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100723// channel2->connect("::1", "20070",
724// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
725// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700726//
727// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100728// g_io.run();
729// g_io.reset();
730// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700731//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100732// BOOST_CHECK_EQUAL(m_faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700733//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100734// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
735// channel3->connect("::1", "20070",
736// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
737// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700738//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100739// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700740//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100741// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700742//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100743// scheduler
744// .scheduleEvent(time::seconds(0.5),
745// bind(&UdpChannel::connect, channel4,
746// "::1", "20070",
747// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
748// channel_onFaceCreated, this, _1)),
749// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
750// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700751//
752// m_limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100753// abortEvent =
754// scheduler.scheduleEvent(time::seconds(4),
755// bind(&EndToEndFixture::abortTestCase, this,
756// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700757//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100758// scheduler.scheduleEvent(time::seconds(0.4),
759// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700760//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100761// g_io.run();
762// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100764// BOOST_CHECK_EQUAL(m_faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700765//
766//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100767// m_face2->sendInterest(interest1);
768// abortEvent =
769// scheduler.scheduleEvent(time::seconds(1),
770// bind(&EndToEndFixture::abortTestCase, this,
771// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700772// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100773// g_io.run();
774// g_io.reset();
775// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700776//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100777// BOOST_CHECK_EQUAL(m_faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700778//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100779// m_face3->sendInterest(interest2);
780// abortEvent =
781// scheduler.scheduleEvent(time::seconds(1),
782// bind(&EndToEndFixture::abortTestCase, this,
783// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700784// m_limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100785// g_io.run();
786// g_io.reset();
787// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700788//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100789// //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest
790// BOOST_CHECK_EQUAL(m_faces.size(), 5);
791// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
792// this,
793// _1),
794// bind(&EndToEndFixture::channel_onConnectFailedOk,
795// this,
796// _1)),
797// UdpChannel::Error);
798//}
799
800
801BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
802{
803 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100804
Giulio Grassi624f6c62014-02-18 19:42:14 +0100805 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
806 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700807
Giulio Grassi624f6c62014-02-18 19:42:14 +0100808 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
809 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700810
Giulio Grassi624f6c62014-02-18 19:42:14 +0100811 channel2->connect("127.0.0.1", "20070",
812 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
813 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700814
815 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
816 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100817
818 BOOST_CHECK_EQUAL(channel2->size(), 1);
819
Giulio Grassi624f6c62014-02-18 19:42:14 +0100820 BOOST_CHECK(static_cast<bool>(m_face2));
821
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700822 // Face::close must be invoked during io run to be counted as an op
823 scheduler::schedule(time::seconds(0.1), bind(&Face::close, m_face2));
824
825 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
826 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100827
828 BOOST_CHECK(!static_cast<bool>(m_face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700829
Giulio Grassi624f6c62014-02-18 19:42:14 +0100830 BOOST_CHECK_EQUAL(channel2->size(), 0);
831}
Giulio Grassi69871f02014-03-09 16:14:44 +0100832
833BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
834{
835 Interest interest1("ndn:/TpnzGvW9R");
836 Interest interest2("ndn:/QWiIMfj5sL");
837
838 UdpFactory factory;
839 time::Duration idleTimeout = time::seconds(2);
840
841 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1",
842 "20070",
843 time::seconds(2));
844 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1",
845 "20071",
846 time::seconds(2));
847 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
848 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
849
850 channel2->connect("127.0.0.1", "20070",
851 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
852 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
853
854 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
855 "UdpChannel error: cannot connect or cannot accept connection");
856
857 m_face2->sendInterest(interest1);
858
859 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,//1 send + 1 listen return
860 time::seconds(1)) == LimitedIo::EXCEED_OPS,
861 "UdpChannel error: cannot send or receive Interest/Data packets");
862
863 BOOST_CHECK_EQUAL(m_faces.size(), 2);
864 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
865 "Idle face should be still open because has been used recently");
866 BOOST_CHECK_EQUAL(m_faces.size(), 2);
867 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
868 "Closing idle face error: face should be closed by now");
869
870 //the face on listen should be closed now
871 BOOST_CHECK_EQUAL(channel1->size(), 0);
872 //checking that m_face2 has not been closed
873 BOOST_CHECK_EQUAL(channel2->size(), 1);
874 BOOST_REQUIRE(static_cast<bool>(m_face2));
875
876 m_face2->sendInterest(interest2);
877 BOOST_CHECK_MESSAGE(m_limitedIo.run(2,//1 send + 1 listen return
878 time::seconds(1)) == LimitedIo::EXCEED_OPS,
879 "UdpChannel error: cannot send or receive Interest/Data packets");
880 //channel1 should have created a new face by now
881 BOOST_CHECK_EQUAL(channel1->size(), 1);
882 BOOST_CHECK_EQUAL(channel2->size(), 1);
883 BOOST_REQUIRE(static_cast<bool>(m_face1));
884
885 channel1->connect("127.0.0.1", "20071",
886 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
887 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
888
889 BOOST_CHECK_MESSAGE(m_limitedIo.run(1,//1 connect
890 time::seconds(1)) == LimitedIo::EXCEED_OPS,
891 "UdpChannel error: cannot connect");
892
893 //the connect should have set m_face1 as permanent face,
894 //but it shouln't have created any additional faces
895 BOOST_CHECK_EQUAL(channel1->size(), 1);
896 BOOST_CHECK_EQUAL(channel2->size(), 1);
897 BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
898 "Idle face should be still open because it's permanent now");
899 //both faces are permanent, nothing should have changed
900 BOOST_CHECK_EQUAL(channel1->size(), 1);
901 BOOST_CHECK_EQUAL(channel2->size(), 1);
902}
903
Giulio Grassi624f6c62014-02-18 19:42:14 +0100904
905//BOOST_FIXTURE_TEST_CASE(MulticastFace, EndToEndFixture)
906//{
907// //to instantiate multicast face on a specific ip address, change interfaceIp
908// std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700909//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100910// UdpFactory factory = UdpFactory();
911// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700912//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100913// shared_ptr<MulticastUdpFace> multicastFace1 =
914// factory.createMulticastFace(interfaceIp, "224.0.0.1", "20072");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700915//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100916// BOOST_REQUIRE(static_cast<bool>(multicastFace1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700917//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100918// channel1_onFaceCreated(multicastFace1);
919//
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700920//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100921// EventId abortEvent =
922// scheduler.scheduleEvent(time::seconds(10),
923// bind(&EndToEndFixture::abortTestCase, this,
924// "MulticastFace error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700925//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100926// Interest interest1("ndn:/TpnzGvW9R");
927// Data data1 ("ndn:/KfczhUqVix");
928// data1.setContent(0, 0);
929// Interest interest2("ndn:/QWiIMfj5sL");
930// Data data2 ("ndn:/XNBV796f");
931// data2.setContent(0, 0);
932// Interest interest3("ndn:/QWiIhjgkj5sL");
933// Data data3 ("ndn:/XNBV794f");
934// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700935//
936//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100937// ndn::SignatureSha256WithRsa fakeSignature;
938// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700939//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100940// // set fake signature on data1 and data2
941// data1.setSignature(fakeSignature);
942// data2.setSignature(fakeSignature);
943// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700944//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100945// multicastFace1->sendInterest(interest1);
946// multicastFace1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700947//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100948// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700949// m_limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100950// g_io.run();
951// g_io.reset();
952// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700953//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100954// BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
955// BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700956//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100957// BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest1.getName());
958// BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data1.getName());
959//}
960
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700961
962
Giulio Grassi624f6c62014-02-18 19:42:14 +0100963BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700964
Giulio Grassi624f6c62014-02-18 19:42:14 +0100965} // namespace tests
966} // namespace nfd