blob: b71de487e8356ba8e65b46c5578bed660b88a793 [file] [log] [blame]
Junxiao Shi96dc0c42014-01-30 23:51:59 -07001/* -*- 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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -08007#include "face/tcp-factory.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -07008#include <ndn-cpp-dev/security/key-chain.hpp>
9
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010#include "tests/test-common.hpp"
Junxiao Shi7e2413b2014-03-02 11:15:09 -070011#include "tests/core/limited-io.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070012
13namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070014namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070015
Junxiao Shid9ee45c2014-02-27 15:38:11 -070016BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070017
18BOOST_AUTO_TEST_CASE(ChannelMap)
19{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080020 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080021
Alexander Afanasyevd6655302014-02-28 08:41:28 -080022 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
23 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070024 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070025 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080026
Alexander Afanasyevd6655302014-02-28 08:41:28 -080027 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070028 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029
30 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
31 BOOST_CHECK_NE(channel2, channel3);
32 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070033}
34
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -070036{
37public:
38 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080039 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070040 {
Junxiao Shi79494162014-04-02 18:25:11 -070041 BOOST_CHECK(!static_cast<bool>(face1));
42 face1 = newFace;
43 face1->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070044 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070045 face1->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070046 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070047 face1->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080048 bind(&EndToEndFixture::face1_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070049
Junxiao Shi79494162014-04-02 18:25:11 -070050 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070051 }
52
53 void
54 channel1_onConnectFailed(const std::string& reason)
55 {
56 BOOST_CHECK_MESSAGE(false, reason);
57
Junxiao Shi79494162014-04-02 18:25:11 -070058 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070059 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080060
Junxiao Shi96dc0c42014-01-30 23:51:59 -070061 void
62 face1_onReceiveInterest(const Interest& interest)
63 {
Junxiao Shi79494162014-04-02 18:25:11 -070064 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070065
Junxiao Shi79494162014-04-02 18:25:11 -070066 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070067 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080068
Junxiao Shi96dc0c42014-01-30 23:51:59 -070069 void
70 face1_onReceiveData(const Data& data)
71 {
Junxiao Shi79494162014-04-02 18:25:11 -070072 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070073
Junxiao Shi79494162014-04-02 18:25:11 -070074 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070075 }
76
77 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080078 face1_onFail()
79 {
Junxiao Shi79494162014-04-02 18:25:11 -070080 face1.reset();
81 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080082 }
83
84 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080085 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070086 {
Junxiao Shi79494162014-04-02 18:25:11 -070087 BOOST_CHECK(!static_cast<bool>(face2));
88 face2 = newFace;
89 face2->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070090 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070091 face2->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070092 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070093 face2->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080094 bind(&EndToEndFixture::face2_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070095
Junxiao Shi79494162014-04-02 18:25:11 -070096 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070097 }
98
99 void
100 channel2_onConnectFailed(const std::string& reason)
101 {
102 BOOST_CHECK_MESSAGE(false, reason);
103
Junxiao Shi79494162014-04-02 18:25:11 -0700104 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700105 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800106
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700107 void
108 face2_onReceiveInterest(const Interest& interest)
109 {
Junxiao Shi79494162014-04-02 18:25:11 -0700110 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700111
Junxiao Shi79494162014-04-02 18:25:11 -0700112 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700113 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800114
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700115 void
116 face2_onReceiveData(const Data& data)
117 {
Junxiao Shi79494162014-04-02 18:25:11 -0700118 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700119
Junxiao Shi79494162014-04-02 18:25:11 -0700120 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700121 }
122
123 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800124 face2_onFail()
125 {
Junxiao Shi79494162014-04-02 18:25:11 -0700126 face2.reset();
127 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800128 }
129
130 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800131 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800132 {
Junxiao Shi79494162014-04-02 18:25:11 -0700133 faces.push_back(newFace);
134 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800135 }
136
137 void
138 channel_onConnectFailed(const std::string& reason)
139 {
140 BOOST_CHECK_MESSAGE(false, reason);
141
Junxiao Shi79494162014-04-02 18:25:11 -0700142 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800143 }
144
145 void
146 checkFaceList(size_t shouldBe)
147 {
Junxiao Shi79494162014-04-02 18:25:11 -0700148 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800149 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800150
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700151public:
Junxiao Shi79494162014-04-02 18:25:11 -0700152 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700153
Junxiao Shi79494162014-04-02 18:25:11 -0700154 shared_ptr<Face> face1;
155 std::vector<Interest> face1_receivedInterests;
156 std::vector<Data> face1_receivedDatas;
157 shared_ptr<Face> face2;
158 std::vector<Interest> face2_receivedInterests;
159 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800160
Junxiao Shi79494162014-04-02 18:25:11 -0700161 std::list< shared_ptr<Face> > faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700162};
163
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000164BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700165{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600166 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700167
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600168 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
169 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800170
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700171 BOOST_CHECK_EQUAL(channel1->isListening(), false);
172
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700173 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
174 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800175
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700176 BOOST_CHECK_EQUAL(channel1->isListening(), true);
177
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600178 TcpFactory factory2;
179
180 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
181 factory2.createChannel("127.0.0.2", "20071");
182
183 factory2.createFace(FaceUri("tcp://127.0.0.1:20070"),
184 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
185 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700186
Junxiao Shi79494162014-04-02 18:25:11 -0700187 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700188 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700189
Junxiao Shi79494162014-04-02 18:25:11 -0700190 BOOST_REQUIRE(static_cast<bool>(face1));
191 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800192
Junxiao Shi79494162014-04-02 18:25:11 -0700193 BOOST_CHECK(face1->isOnDemand());
194 BOOST_CHECK(!face2->isOnDemand());
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700195
Junxiao Shi79494162014-04-02 18:25:11 -0700196 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
197 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
198 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000199
Junxiao Shi79494162014-04-02 18:25:11 -0700200 BOOST_CHECK_EQUAL(face1->isLocal(), true);
201 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000202
Junxiao Shi79494162014-04-02 18:25:11 -0700203 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
204 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000205
206 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
207
208 Interest interest1("ndn:/TpnzGvW9R");
209 Data data1 ("ndn:/KfczhUqVix");
210 data1.setContent(0, 0);
211 Interest interest2("ndn:/QWiIMfj5sL");
212 Data data2 ("ndn:/XNBV796f");
213 data2.setContent(0, 0);
214
215 ndn::SignatureSha256WithRsa fakeSignature;
216 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
217
218 // set fake signature on data1 and data2
219 data1.setSignature(fakeSignature);
220 data2.setSignature(fakeSignature);
221
Junxiao Shi79494162014-04-02 18:25:11 -0700222 face1->sendInterest(interest1);
223 face1->sendInterest(interest1);
224 face1->sendInterest(interest1);
225 face1->sendData (data1 );
226 face2->sendInterest(interest2);
227 face2->sendData (data2 );
228 face2->sendData (data2 );
229 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000230
Junxiao Shi79494162014-04-02 18:25:11 -0700231 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000232 "TcpChannel error: cannot send or receive Interest/Data packets");
233
234
Junxiao Shi79494162014-04-02 18:25:11 -0700235 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
236 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
237 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
238 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
241 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
242 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
243 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000244
Junxiao Shi79494162014-04-02 18:25:11 -0700245 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700246 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
247 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
248 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
249 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000250
Junxiao Shi79494162014-04-02 18:25:11 -0700251 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700252 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
253 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
254 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
255 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000256}
257
258BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
259{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600260 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000261
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600262 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
263 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000264
265 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
266 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
267
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600268 TcpFactory factory2;
269
270 factory2.createChannel("::2", "20070");
271
272 factory2.createFace(FaceUri("tcp://[::1]:20070"),
273 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
274 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000275
Junxiao Shi79494162014-04-02 18:25:11 -0700276 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000277 "TcpChannel error: cannot connect or cannot accept connection");
278
Junxiao Shi79494162014-04-02 18:25:11 -0700279 BOOST_REQUIRE(static_cast<bool>(face1));
280 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000281
Junxiao Shi79494162014-04-02 18:25:11 -0700282 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
283 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
284 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000285
Junxiao Shi79494162014-04-02 18:25:11 -0700286 BOOST_CHECK_EQUAL(face1->isLocal(), true);
287 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800288
Junxiao Shi79494162014-04-02 18:25:11 -0700289 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
290 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800291
292 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800293
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700294 Interest interest1("ndn:/TpnzGvW9R");
295 Data data1 ("ndn:/KfczhUqVix");
296 data1.setContent(0, 0);
297 Interest interest2("ndn:/QWiIMfj5sL");
298 Data data2 ("ndn:/XNBV796f");
299 data2.setContent(0, 0);
300
301 ndn::SignatureSha256WithRsa fakeSignature;
302 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800303
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700304 // set fake signature on data1 and data2
305 data1.setSignature(fakeSignature);
306 data2.setSignature(fakeSignature);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800307
Junxiao Shi79494162014-04-02 18:25:11 -0700308 face1->sendInterest(interest1);
309 face1->sendData (data1 );
310 face2->sendInterest(interest2);
311 face2->sendData (data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700312
Junxiao Shi79494162014-04-02 18:25:11 -0700313 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700314 "TcpChannel error: cannot send or receive Interest/Data packets");
315
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700316
Junxiao Shi79494162014-04-02 18:25:11 -0700317 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
318 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
319 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
320 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800321
Junxiao Shi79494162014-04-02 18:25:11 -0700322 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
323 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
324 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
325 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700326}
327
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800328BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
329{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800330 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800331
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800332 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
333 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800334
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800335 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
336 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800337
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800338 channel2->connect("127.0.0.1", "20070",
339 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
340 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800341 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800342
Junxiao Shi79494162014-04-02 18:25:11 -0700343 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700344 "TcpChannel error: cannot connect or cannot accept connection");
345
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800346
Junxiao Shi79494162014-04-02 18:25:11 -0700347 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800348
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800349 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800350 channel3->connect("127.0.0.1", "20070",
351 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
352 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800353 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800354
355
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800356 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800357
358 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800359
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700360 scheduler::schedule(time::seconds(1),
361 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800362 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700363 static_cast<TcpChannel::FaceCreatedCallback>(
364 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
365 static_cast<TcpChannel::ConnectFailedCallback>(
366 bind(&EndToEndFixture::channel_onConnectFailed, this, _1)),
367 time::seconds(4)));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800368
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700369 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800370 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800371
Junxiao Shi79494162014-04-02 18:25:11 -0700372 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700373 time::seconds(10)) == LimitedIo::EXCEED_OPS,
374 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800375
Junxiao Shi79494162014-04-02 18:25:11 -0700376 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800377}
378
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800379
380BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
381{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800382 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800383
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800384 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
385 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800386
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800387 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
388 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800389
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800390 channel2->connect("127.0.0.1", "20070",
391 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
392 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800393 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800394
Junxiao Shi79494162014-04-02 18:25:11 -0700395 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700396 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800397
398 BOOST_CHECK_EQUAL(channel1->size(), 1);
399 BOOST_CHECK_EQUAL(channel2->size(), 1);
400
Junxiao Shi79494162014-04-02 18:25:11 -0700401 BOOST_REQUIRE(static_cast<bool>(face1));
402 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800403
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700404 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700405 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800406
Junxiao Shi79494162014-04-02 18:25:11 -0700407 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700408 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800409
410 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700411 BOOST_CHECK(!static_cast<bool>(face1));
412 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800413
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800414 BOOST_CHECK_EQUAL(channel1->size(), 0);
415 BOOST_CHECK_EQUAL(channel2->size(), 0);
416}
417
418
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700419BOOST_AUTO_TEST_SUITE_END()
420
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700421} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700422} // namespace nfd