blob: 157602dd25f144c4914a9346f69c591d06af13b7 [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"
Junxiao Shi7e2413b2014-03-02 11:15:09 -07008
9#include "tests/test-common.hpp"
10#include "tests/core/limited-io.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010011
12namespace nfd {
13namespace tests {
14
15BOOST_FIXTURE_TEST_SUITE(FaceUdp, BaseFixture)
16
17class FactoryErrorCheck : protected BaseFixture
18{
19public:
20 bool isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
21 return strcmp(e.what(),
22 "Cannot create the requested UDP unicast channel, local "
23 "endpoint is already allocated for a UDP multicast face") == 0;
24 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070025
Giulio Grassi624f6c62014-02-18 19:42:14 +010026 bool isNotMulticastAddress(const UdpFactory::Error& e) {
27 return strcmp(e.what(),
28 "Cannot create the requested UDP multicast face, "
29 "the multicast group given as input is not a multicast address") == 0;
30 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070031
Giulio Grassi624f6c62014-02-18 19:42:14 +010032 bool isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
33 return strcmp(e.what(),
34 "Cannot create the requested UDP multicast face, local "
35 "endpoint is already allocated for a UDP unicast channel") == 0;
36 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070037
Giulio Grassi624f6c62014-02-18 19:42:14 +010038 bool isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
39 return strcmp(e.what(),
40 "Cannot create the requested UDP multicast face, local "
41 "endpoint is already allocated for a UDP multicast face "
42 "on a different multicast group") == 0;
43 }
44};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070045
Giulio Grassi624f6c62014-02-18 19:42:14 +010046BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
47{
48 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070049
Giulio Grassi624f6c62014-02-18 19:42:14 +010050 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070051
Giulio Grassi624f6c62014-02-18 19:42:14 +010052 //to instantiate multicast face on a specific ip address, change interfaceIp
53 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -070054
Giulio Grassi624f6c62014-02-18 19:42:14 +010055 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
56 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
57 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070058 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070059
Giulio Grassi624f6c62014-02-18 19:42:14 +010060 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
61 BOOST_CHECK_NE(channel1, channel2);
62
63 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -070064
Junxiao Shi61e3cc52014-03-03 20:40:28 -070065 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
66 BOOST_CHECK_NE(channel2, channel4);
67 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
68
Giulio Grassi624f6c62014-02-18 19:42:14 +010069 //same endpoint of a unicast channel
70 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
71 "224.0.0.1",
72 "20070"),
73 UdpFactory::Error,
74 isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070075
76
Giulio Grassi624f6c62014-02-18 19:42:14 +010077 shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp,
78 "224.0.0.1",
79 "20072");
80 shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp,
81 "224.0.0.1",
82 "20072");
83 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070084
85
Giulio Grassi624f6c62014-02-18 19:42:14 +010086 //same endpoint of a multicast face
87 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
88 UdpFactory::Error,
89 isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070090
Giulio Grassi624f6c62014-02-18 19:42:14 +010091 //same multicast endpoint, different group
92 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
93 "224.0.0.42",
94 "20072"),
95 UdpFactory::Error,
96 isLocalEndpointOnDifferentGroup);
97
98 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
99 "192.168.10.15",
100 "20025"),
101 UdpFactory::Error,
102 isNotMulticastAddress);
103
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700104
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105// //Test commented because it required to be run in a machine that can resolve ipv6 query
106// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
107// "fe80::5e96:9dff:fe7d:9c8d%en1",
108// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
109// "20070");
110//
111// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
112// //an available network interface (different from the first one used)
113// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
114// "224.0.0.1",
115// "20073");
116// BOOST_CHECK_NE(multicastFace1, multicastFace2);
117//
118//
119// //ipv6 - work in progress
120// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
121// "FF01:0:0:0:0:0:0:2",
122// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700123//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100124// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
125// "FF01:0:0:0:0:0:0:2",
126// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700127//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
129//
130// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
131// "FF01:0:0:0:0:0:0:2",
132// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700133//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100134// BOOST_CHECK_NE(multicastFace3, multicastFace5);
135//
136// //same local ipv6 endpoint for a different multicast group
137// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
138// "FE01:0:0:0:0:0:0:2",
139// "20073"),
140// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700141//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142// //same local ipv6 (expect for th port number) endpoint for a different multicast group
143// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
144// "FE01:0:0:0:0:0:0:2",
145// "20075"),
146// UdpFactory::Error);
147//
148// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
149// "FE12:0:0:0:0:0:0:2",
150// "20075"),
151// UdpFactory::Error);
152//
153// //not a multicast ipv6
154// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
155// "A112:0:0:0:0:0:0:2",
156// "20075"),
157// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100158}
159
160class EndToEndFixture : protected BaseFixture
161{
162public:
163 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700164 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100165 {
Junxiao Shi79494162014-04-02 18:25:11 -0700166 BOOST_CHECK(!static_cast<bool>(face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100167 channel1_onFaceCreatedNoCheck(newFace);
168 }
Junxiao Shi79494162014-04-02 18:25:11 -0700169
Giulio Grassi69871f02014-03-09 16:14:44 +0100170 void
171 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
172 {
Junxiao Shi79494162014-04-02 18:25:11 -0700173 face1 = newFace;
174 face1->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700176 face1->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100177 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700178 face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100179 bind(&EndToEndFixture::face1_onFail, this);
180 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
181
Junxiao Shi79494162014-04-02 18:25:11 -0700182 faces.push_back(face1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100183
Junxiao Shi79494162014-04-02 18:25:11 -0700184 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185 }
186
187 void
188 channel1_onConnectFailed(const std::string& reason)
189 {
190 BOOST_CHECK_MESSAGE(false, reason);
191
Junxiao Shi79494162014-04-02 18:25:11 -0700192 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100193 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700194
Giulio Grassi624f6c62014-02-18 19:42:14 +0100195 void
196 face1_onReceiveInterest(const Interest& interest)
197 {
Junxiao Shi79494162014-04-02 18:25:11 -0700198 face1_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100199
Junxiao Shi79494162014-04-02 18:25:11 -0700200 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100201 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700202
Giulio Grassi624f6c62014-02-18 19:42:14 +0100203 void
204 face1_onReceiveData(const Data& data)
205 {
Junxiao Shi79494162014-04-02 18:25:11 -0700206 face1_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100207
Junxiao Shi79494162014-04-02 18:25:11 -0700208 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 }
210
211 void
212 face1_onFail()
213 {
Junxiao Shi79494162014-04-02 18:25:11 -0700214 face1.reset();
215 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100216 }
217
218 void
219 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
220 {
Junxiao Shi79494162014-04-02 18:25:11 -0700221 BOOST_CHECK(!static_cast<bool>(face2));
222 face2 = newFace;
223 face2->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100224 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700225 face2->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100226 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700227 face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100228 bind(&EndToEndFixture::face2_onFail, this);
229
Junxiao Shi79494162014-04-02 18:25:11 -0700230 faces.push_back(face2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100231
232 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi79494162014-04-02 18:25:11 -0700233 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100234 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700235
Giulio Grassi624f6c62014-02-18 19:42:14 +0100236 void
237 channel2_onConnectFailed(const std::string& reason)
238 {
239 BOOST_CHECK_MESSAGE(false, reason);
240
Junxiao Shi79494162014-04-02 18:25:11 -0700241 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100242 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700243
Giulio Grassi624f6c62014-02-18 19:42:14 +0100244 void
245 face2_onReceiveInterest(const Interest& interest)
246 {
Junxiao Shi79494162014-04-02 18:25:11 -0700247 face2_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100248
Junxiao Shi79494162014-04-02 18:25:11 -0700249 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100250 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700251
Giulio Grassi624f6c62014-02-18 19:42:14 +0100252 void
253 face2_onReceiveData(const Data& data)
254 {
Junxiao Shi79494162014-04-02 18:25:11 -0700255 face2_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100256
Junxiao Shi79494162014-04-02 18:25:11 -0700257 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100258 }
259
260 void
261 face2_onFail()
262 {
Junxiao Shi79494162014-04-02 18:25:11 -0700263 face2.reset();
264 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100265 }
266
267 void
268 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
269 {
Junxiao Shi79494162014-04-02 18:25:11 -0700270 BOOST_CHECK(!static_cast<bool>(face1));
271 face3 = newFace;
272 faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700273
Junxiao Shi79494162014-04-02 18:25:11 -0700274 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100275 }
276
Giulio Grassi624f6c62014-02-18 19:42:14 +0100277 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700278 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100279 {
Junxiao Shi79494162014-04-02 18:25:11 -0700280 faces.push_back(newFace);
281 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100282 }
283
284 void
285 channel_onConnectFailed(const std::string& reason)
286 {
287 BOOST_CHECK_MESSAGE(false, reason);
288
Junxiao Shi79494162014-04-02 18:25:11 -0700289 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100290 }
291
292 void
293 channel_onConnectFailedOk(const std::string& reason)
294 {
295 //it's ok, it was supposed to fail
Junxiao Shi79494162014-04-02 18:25:11 -0700296 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100297 }
298
299 void
300 checkFaceList(size_t shouldBe)
301 {
Junxiao Shi79494162014-04-02 18:25:11 -0700302 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100303 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100304
305public:
Junxiao Shi79494162014-04-02 18:25:11 -0700306 LimitedIo limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100307
Junxiao Shi79494162014-04-02 18:25:11 -0700308 shared_ptr<Face> face1;
309 std::vector<Interest> face1_receivedInterests;
310 std::vector<Data> face1_receivedDatas;
311 shared_ptr<Face> face2;
312 std::vector<Interest> face2_receivedInterests;
313 std::vector<Data> face2_receivedDatas;
314 shared_ptr<Face> face3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100315
Junxiao Shi79494162014-04-02 18:25:11 -0700316 std::list< shared_ptr<Face> > faces;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100317};
318
319
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000320BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100321{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700322 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100323
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000324 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700325
Giulio Grassi624f6c62014-02-18 19:42:14 +0100326 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
327 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
328 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700329
330
Junxiao Shi79494162014-04-02 18:25:11 -0700331 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700332 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100333
Junxiao Shi79494162014-04-02 18:25:11 -0700334 BOOST_REQUIRE(static_cast<bool>(face2));
335 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp4://127.0.0.1:20070");
336 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp4://127.0.0.1:20071");
337 BOOST_CHECK_EQUAL(face2->isLocal(), false);
338 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700339
Giulio Grassi624f6c62014-02-18 19:42:14 +0100340 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
341 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
342 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700343
Giulio Grassi624f6c62014-02-18 19:42:14 +0100344 Interest interest1("ndn:/TpnzGvW9R");
345 Data data1 ("ndn:/KfczhUqVix");
346 data1.setContent(0, 0);
347 Interest interest2("ndn:/QWiIMfj5sL");
348 Data data2 ("ndn:/XNBV796f");
349 data2.setContent(0, 0);
350 Interest interest3("ndn:/QWiIhjgkj5sL");
351 Data data3 ("ndn:/XNBV794f");
352 data3.setContent(0, 0);
353
354
355 ndn::SignatureSha256WithRsa fakeSignature;
356 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
357 reinterpret_cast<const uint8_t*>(0),
358 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700359
Giulio Grassi624f6c62014-02-18 19:42:14 +0100360 // set fake signature on data1 and data2
361 data1.setSignature(fakeSignature);
362 data2.setSignature(fakeSignature);
363 data3.setSignature(fakeSignature);
364
Junxiao Shi79494162014-04-02 18:25:11 -0700365 face2->sendInterest(interest2);
366 face2->sendData (data2 );
367 face2->sendData (data2 );
368 face2->sendData (data2 );
Giulio Grassi624f6c62014-02-18 19:42:14 +0100369
Junxiao Shi79494162014-04-02 18:25:11 -0700370 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000371 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700372 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100373
Junxiao Shi79494162014-04-02 18:25:11 -0700374 BOOST_REQUIRE(static_cast<bool>(face1));
375 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
376 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
377 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700378
Junxiao Shi79494162014-04-02 18:25:11 -0700379 face1->sendInterest(interest1);
380 face1->sendInterest(interest1);
381 face1->sendInterest(interest1);
382 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700383
Junxiao Shi79494162014-04-02 18:25:11 -0700384 BOOST_CHECK_MESSAGE(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
Junxiao Shi79494162014-04-02 18:25:11 -0700388 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
389 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
390 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
391 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700392
Junxiao Shi79494162014-04-02 18:25:11 -0700393 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
394 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
395 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
396 BOOST_CHECK_EQUAL(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
Junxiao Shi79494162014-04-02 18:25:11 -0700402 face2->sendData (data3 );
403 face2->sendInterest(interest3);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100404
Junxiao Shi79494162014-04-02 18:25:11 -0700405 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700406 "UdpChannel error: cannot send or receive Interest/Data packets");
407
Junxiao Shi79494162014-04-02 18:25:11 -0700408 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
409 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700410
Junxiao Shi79494162014-04-02 18:25:11 -0700411 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
412 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000413
Junxiao Shi79494162014-04-02 18:25:11 -0700414 const FaceCounters& counters1 = face1->getCounters();
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000415 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
Junxiao Shi79494162014-04-02 18:25:11 -0700420 const FaceCounters& counters2 = face2->getCounters();
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000421 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
Junxiao Shi79494162014-04-02 18:25:11 -0700438 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000439 "UdpChannel error: cannot connect or cannot accept connection");
440
Junxiao Shi79494162014-04-02 18:25:11 -0700441 BOOST_REQUIRE(static_cast<bool>(face2));
442 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
443 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
444 BOOST_CHECK_EQUAL(face2->isLocal(), false);
445 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000446
447 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
448 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
449 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
450
451 Interest interest1("ndn:/TpnzGvW9R");
452 Data data1 ("ndn:/KfczhUqVix");
453 data1.setContent(0, 0);
454 Interest interest2("ndn:/QWiIMfj5sL");
455 Data data2 ("ndn:/XNBV796f");
456 data2.setContent(0, 0);
457 Interest interest3("ndn:/QWiIhjgkj5sL");
458 Data data3 ("ndn:/XNBV794f");
459 data3.setContent(0, 0);
460
461
462 ndn::SignatureSha256WithRsa fakeSignature;
463 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
464 reinterpret_cast<const uint8_t*>(0),
465 0));
466
467 // set fake signature on data1 and data2
468 data1.setSignature(fakeSignature);
469 data2.setSignature(fakeSignature);
470 data3.setSignature(fakeSignature);
471
Junxiao Shi79494162014-04-02 18:25:11 -0700472 face2->sendInterest(interest2);
473 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000474
Junxiao Shi79494162014-04-02 18:25:11 -0700475 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000476 time::seconds(1)) == LimitedIo::EXCEED_OPS,
477 "UdpChannel error: cannot send or receive Interest/Data packets");
478
Junxiao Shi79494162014-04-02 18:25:11 -0700479 BOOST_REQUIRE(static_cast<bool>(face1));
480 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
481 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
482 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000483
Junxiao Shi79494162014-04-02 18:25:11 -0700484 face1->sendInterest(interest1);
485 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000486
Junxiao Shi79494162014-04-02 18:25:11 -0700487 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000488 "UdpChannel error: cannot send or receive Interest/Data packets");
489
490
Junxiao Shi79494162014-04-02 18:25:11 -0700491 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
492 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
493 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
494 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000495
Junxiao Shi79494162014-04-02 18:25:11 -0700496 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
497 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
498 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
499 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000500
501
502
503 //checking if the connection accepting mechanism works properly.
504
Junxiao Shi79494162014-04-02 18:25:11 -0700505 face2->sendData (data3 );
506 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000507
Junxiao Shi79494162014-04-02 18:25:11 -0700508 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000509 "UdpChannel error: cannot send or receive Interest/Data packets");
510
Junxiao Shi79494162014-04-02 18:25:11 -0700511 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
512 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000513
Junxiao Shi79494162014-04-02 18:25:11 -0700514 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
515 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100516}
517
518BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
519{
520 Interest interest1("ndn:/TpnzGvW9R");
521 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100522
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700523 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100524
Giulio Grassi624f6c62014-02-18 19:42:14 +0100525 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
526 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700527
Giulio Grassi624f6c62014-02-18 19:42:14 +0100528 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
529 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700530
Giulio Grassi624f6c62014-02-18 19:42:14 +0100531 channel2->connect("127.0.0.1", "20070",
532 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
533 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700534
535
Junxiao Shi79494162014-04-02 18:25:11 -0700536 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700537 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100538
Junxiao Shi79494162014-04-02 18:25:11 -0700539 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700540
Giulio Grassi624f6c62014-02-18 19:42:14 +0100541 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
542 channel3->connect("127.0.0.1", "20070",
543 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
544 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700545
Giulio Grassi624f6c62014-02-18 19:42:14 +0100546 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
547
548 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100549
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700550 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700551 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
552 // does not work without static_cast
553 static_cast<UdpChannel::FaceCreatedCallback>(
554 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
555 static_cast<UdpChannel::ConnectFailedCallback>(
556 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
557
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700558 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700559
Junxiao Shi79494162014-04-02 18:25:11 -0700560 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700561 time::seconds(4)) == LimitedIo::EXCEED_OPS,
562 "UdpChannel error: cannot connect or cannot accept multiple connections");
563
Junxiao Shi79494162014-04-02 18:25:11 -0700564 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700565
566
Junxiao Shi79494162014-04-02 18:25:11 -0700567 face2->sendInterest(interest1);
568 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700569 "UdpChannel error: cannot send or receive Interest/Data packets");
570
Junxiao Shi79494162014-04-02 18:25:11 -0700571 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700572
Junxiao Shi79494162014-04-02 18:25:11 -0700573 face3->sendInterest(interest2);
574 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700575 "UdpChannel error: cannot send or receive Interest/Data packets");
576
Junxiao Shi79494162014-04-02 18:25:11 -0700577 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
578 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100579 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
580 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100581 UdpChannel::Error);
582}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700583
Giulio Grassi624f6c62014-02-18 19:42:14 +0100584//Test commented because it required to be run in a machine that can resolve ipv6 query
585//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
586//{
587// UdpFactory factory = UdpFactory();
588// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700589//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100590// EventId abortEvent =
591// scheduler.scheduleEvent(time::seconds(1),
592// bind(&EndToEndFixture::abortTestCase, this,
593// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700594//
Junxiao Shi79494162014-04-02 18:25:11 -0700595// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700596//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100597// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
598// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700599//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100600// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
601// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700602//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100603// channel2->connect("::1", "20070",
604// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
605// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
606// g_io.run();
607// g_io.reset();
608// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700609//
Junxiao Shi79494162014-04-02 18:25:11 -0700610// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700611//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100612// abortEvent =
613// scheduler.scheduleEvent(time::seconds(1),
614// bind(&EndToEndFixture::abortTestCase, this,
615// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700616//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100617// Interest interest1("ndn:/TpnzGvW9R");
618// Data data1 ("ndn:/KfczhUqVix");
619// data1.setContent(0, 0);
620// Interest interest2("ndn:/QWiIMfj5sL");
621// Data data2 ("ndn:/XNBV796f");
622// data2.setContent(0, 0);
623// Interest interest3("ndn:/QWiIhjgkj5sL");
624// Data data3 ("ndn:/XNBV794f");
625// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700626//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100627// ndn::SignatureSha256WithRsa fakeSignature;
628// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700629//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100630// // set fake signature on data1 and data2
631// data1.setSignature(fakeSignature);
632// data2.setSignature(fakeSignature);
633// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700634//
Junxiao Shi79494162014-04-02 18:25:11 -0700635// face2->sendInterest(interest2);
636// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700637//
Junxiao Shi79494162014-04-02 18:25:11 -0700638// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100639// g_io.run();
640// g_io.reset();
641// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700642//
Junxiao Shi79494162014-04-02 18:25:11 -0700643// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700644//
Junxiao Shi79494162014-04-02 18:25:11 -0700645// face1->sendInterest(interest1);
646// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700647//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100648// abortEvent =
649// scheduler.scheduleEvent(time::seconds(1),
650// bind(&EndToEndFixture::abortTestCase, this,
651// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700652// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100653// g_io.run();
654// g_io.reset();
655// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700656//
Junxiao Shi79494162014-04-02 18:25:11 -0700657// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
658// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
659// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
660// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700661//
Junxiao Shi79494162014-04-02 18:25:11 -0700662// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
663// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
664// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
665// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700666//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100667// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700668//
Junxiao Shi79494162014-04-02 18:25:11 -0700669// face2->sendData (data3 );
670// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700671//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100672// abortEvent =
673// scheduler.scheduleEvent(time::seconds(1),
674// bind(&EndToEndFixture::abortTestCase, this,
675// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700676// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100677// g_io.run();
678// g_io.reset();
679// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700680//
Junxiao Shi79494162014-04-02 18:25:11 -0700681// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
682// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700683//
Junxiao Shi79494162014-04-02 18:25:11 -0700684// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
685// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100687//}
688
689
690//Test commented because it required to be run in a machine that can resolve ipv6 query
691//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
692//{
693// Interest interest1("ndn:/TpnzGvW9R");
694// Interest interest2("ndn:/QWiIMfj5sL");
695// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700696//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100697// UdpFactory factory = UdpFactory();
698// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700699//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100700// EventId abortEvent =
701// scheduler.scheduleEvent(time::seconds(4),
702// bind(&EndToEndFixture::abortTestCase, this,
703// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700704//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100705// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
706// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700707//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100708// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
709// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700710//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100711// channel2->connect("::1", "20070",
712// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
713// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700714//
Junxiao Shi79494162014-04-02 18:25:11 -0700715// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100716// g_io.run();
717// g_io.reset();
718// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700719//
Junxiao Shi79494162014-04-02 18:25:11 -0700720// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700721//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100722// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
723// channel3->connect("::1", "20070",
724// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
725// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700726//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100727// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700728//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100729// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700730//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100731// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700732// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100733// bind(&UdpChannel::connect, channel4,
734// "::1", "20070",
735// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
736// channel_onFaceCreated, this, _1)),
737// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
738// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700739//
Junxiao Shi79494162014-04-02 18:25:11 -0700740// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100741// abortEvent =
742// scheduler.scheduleEvent(time::seconds(4),
743// bind(&EndToEndFixture::abortTestCase, this,
744// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700745//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100746// scheduler.scheduleEvent(time::seconds(0.4),
747// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700748//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100749// g_io.run();
750// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700751//
Junxiao Shi79494162014-04-02 18:25:11 -0700752// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700753//
754//
Junxiao Shi79494162014-04-02 18:25:11 -0700755// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100756// abortEvent =
757// scheduler.scheduleEvent(time::seconds(1),
758// bind(&EndToEndFixture::abortTestCase, this,
759// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700760// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100761// g_io.run();
762// g_io.reset();
763// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700764//
Junxiao Shi79494162014-04-02 18:25:11 -0700765// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700766//
Junxiao Shi79494162014-04-02 18:25:11 -0700767// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100768// abortEvent =
769// scheduler.scheduleEvent(time::seconds(1),
770// bind(&EndToEndFixture::abortTestCase, this,
771// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700772// 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//
Junxiao Shi79494162014-04-02 18:25:11 -0700777// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
778// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100779// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
780// this,
781// _1),
782// bind(&EndToEndFixture::channel_onConnectFailedOk,
783// this,
784// _1)),
785// UdpChannel::Error);
786//}
787
788
789BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
790{
791 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100792
Giulio Grassi624f6c62014-02-18 19:42:14 +0100793 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
794 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700795
Giulio Grassi624f6c62014-02-18 19:42:14 +0100796 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
797 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700798
Giulio Grassi624f6c62014-02-18 19:42:14 +0100799 channel2->connect("127.0.0.1", "20070",
800 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
801 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700802
Junxiao Shi79494162014-04-02 18:25:11 -0700803 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700804 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100805
806 BOOST_CHECK_EQUAL(channel2->size(), 1);
807
Junxiao Shi79494162014-04-02 18:25:11 -0700808 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100809
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700810 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700811 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700812
Junxiao Shi79494162014-04-02 18:25:11 -0700813 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700814 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100815
Junxiao Shi79494162014-04-02 18:25:11 -0700816 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700817
Giulio Grassi624f6c62014-02-18 19:42:14 +0100818 BOOST_CHECK_EQUAL(channel2->size(), 0);
819}
Junxiao Shi79494162014-04-02 18:25:11 -0700820
Giulio Grassi69871f02014-03-09 16:14:44 +0100821BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
822{
823 Interest interest1("ndn:/TpnzGvW9R");
824 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100825
Giulio Grassi69871f02014-03-09 16:14:44 +0100826 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100827
828 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100829 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100830 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100831 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100832
Giulio Grassi69871f02014-03-09 16:14:44 +0100833 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100834 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100835
836 channel2->connect("127.0.0.1", "20070",
837 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
838 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700839
840 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100841 "UdpChannel error: cannot connect or cannot accept connection");
842
Junxiao Shi79494162014-04-02 18:25:11 -0700843 face2->sendInterest(interest1);
844 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100845
Junxiao Shi79494162014-04-02 18:25:11 -0700846 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100847 time::seconds(1)) == LimitedIo::EXCEED_OPS,
848 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700849
850 BOOST_CHECK_EQUAL(faces.size(), 2);
851 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100852 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700853 BOOST_CHECK_EQUAL(faces.size(), 2);
854 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100855 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700856
Giulio Grassi69871f02014-03-09 16:14:44 +0100857 //the face on listen should be closed now
858 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700859 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100860 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700861 BOOST_REQUIRE(static_cast<bool>(face2));
862
863 face2->sendInterest(interest2);
864 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100865 time::seconds(1)) == LimitedIo::EXCEED_OPS,
866 "UdpChannel error: cannot send or receive Interest/Data packets");
867 //channel1 should have created a new face by now
868 BOOST_CHECK_EQUAL(channel1->size(), 1);
869 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700870 BOOST_REQUIRE(static_cast<bool>(face1));
871 BOOST_CHECK(face1->isOnDemand());
872
Giulio Grassi69871f02014-03-09 16:14:44 +0100873 channel1->connect("127.0.0.1", "20071",
874 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
875 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
876
Junxiao Shi79494162014-04-02 18:25:11 -0700877 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100878 time::seconds(1)) == LimitedIo::EXCEED_OPS,
879 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700880
Junxiao Shi79494162014-04-02 18:25:11 -0700881 BOOST_CHECK(!face1->isOnDemand());
882
883 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100884 //but it shouln't have created any additional faces
885 BOOST_CHECK_EQUAL(channel1->size(), 1);
886 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700887 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100888 "Idle face should be still open because it's permanent now");
889 //both faces are permanent, nothing should have changed
890 BOOST_CHECK_EQUAL(channel1->size(), 1);
891 BOOST_CHECK_EQUAL(channel2->size(), 1);
892}
893
Giulio Grassi624f6c62014-02-18 19:42:14 +0100894BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700895
Giulio Grassi624f6c62014-02-18 19:42:14 +0100896} // namespace tests
897} // namespace nfd