blob: 332e28cc2e54445fe07efcdf4b557f1849fb8c44 [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 {
41 BOOST_CHECK(!static_cast<bool>(m_face1));
42 m_face1 = newFace;
43 m_face1->onReceiveInterest +=
44 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
45 m_face1->onReceiveData +=
46 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080047 m_face1->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080048 bind(&EndToEndFixture::face1_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070049
Junxiao Shi7e2413b2014-03-02 11:15:09 -070050 m_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 Shi7e2413b2014-03-02 11:15:09 -070058 m_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 {
64 m_face1_receivedInterests.push_back(interest);
65
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066 m_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 {
72 m_face1_receivedDatas.push_back(data);
73
Junxiao Shi7e2413b2014-03-02 11:15:09 -070074 m_limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070075 }
76
77 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080078 face1_onFail()
79 {
80 m_face1.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070081 m_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 {
87 BOOST_CHECK(!static_cast<bool>(m_face2));
88 m_face2 = newFace;
89 m_face2->onReceiveInterest +=
90 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
91 m_face2->onReceiveData +=
92 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080093 m_face2->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080094 bind(&EndToEndFixture::face2_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070095
Junxiao Shi7e2413b2014-03-02 11:15:09 -070096 m_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 Shi7e2413b2014-03-02 11:15:09 -0700104 m_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 {
110 m_face2_receivedInterests.push_back(interest);
111
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700112 m_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 {
118 m_face2_receivedDatas.push_back(data);
119
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700120 m_limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700121 }
122
123 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800124 face2_onFail()
125 {
126 m_face2.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700127 m_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 {
133 m_faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700134 m_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 Shi7e2413b2014-03-02 11:15:09 -0700142 m_limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800143 }
144
145 void
146 checkFaceList(size_t shouldBe)
147 {
148 BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
149 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800150
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700151public:
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700152 LimitedIo m_limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700153
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800154 shared_ptr<Face> m_face1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700155 std::vector<Interest> m_face1_receivedInterests;
156 std::vector<Data> m_face1_receivedDatas;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800157 shared_ptr<Face> m_face2;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700158 std::vector<Interest> m_face2_receivedInterests;
159 std::vector<Data> m_face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800160
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800161 std::list< shared_ptr<Face> > m_faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700162};
163
164
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000165BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700166{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800167 TcpFactory factory;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700168
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800169 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000170 factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800171
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700172 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
173 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800174
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000175 factory.createFace(FaceUri("tcp://127.0.0.1:20070"),
176 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
177 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700178
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700179 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
180 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700181
182 BOOST_REQUIRE(static_cast<bool>(m_face1));
183 BOOST_REQUIRE(static_cast<bool>(m_face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800184
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000185 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp4://127.0.0.1:20070");
186 // face1 has an unknown URI, since the source port is automatically chosen by OS
187
188 BOOST_CHECK_EQUAL(m_face1->isLocal(), true);
189 BOOST_CHECK_EQUAL(m_face2->isLocal(), true);
190
191 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true);
192 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true);
193
194 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
195
196 Interest interest1("ndn:/TpnzGvW9R");
197 Data data1 ("ndn:/KfczhUqVix");
198 data1.setContent(0, 0);
199 Interest interest2("ndn:/QWiIMfj5sL");
200 Data data2 ("ndn:/XNBV796f");
201 data2.setContent(0, 0);
202
203 ndn::SignatureSha256WithRsa fakeSignature;
204 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
205
206 // set fake signature on data1 and data2
207 data1.setSignature(fakeSignature);
208 data2.setSignature(fakeSignature);
209
210 m_face1->sendInterest(interest1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000211 m_face1->sendInterest(interest1);
212 m_face1->sendInterest(interest1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000213 m_face1->sendData (data1 );
214 m_face2->sendInterest(interest2);
215 m_face2->sendData (data2 );
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000216 m_face2->sendData (data2 );
217 m_face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000218
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000219 BOOST_CHECK_MESSAGE(m_limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000220 "TcpChannel error: cannot send or receive Interest/Data packets");
221
222
223 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000224 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 3);
225 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000226 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
227
228 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
229 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
230 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
231 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000232
233 const FaceCounters& counters1 = m_face1->getCounters();
234 BOOST_CHECK_EQUAL(counters1.getInInterest() , 1);
235 BOOST_CHECK_EQUAL(counters1.getInData() , 3);
236 BOOST_CHECK_EQUAL(counters1.getOutInterest(), 3);
237 BOOST_CHECK_EQUAL(counters1.getOutData() , 1);
238
239 const FaceCounters& counters2 = m_face2->getCounters();
240 BOOST_CHECK_EQUAL(counters2.getInInterest() , 3);
241 BOOST_CHECK_EQUAL(counters2.getInData() , 1);
242 BOOST_CHECK_EQUAL(counters2.getOutInterest(), 1);
243 BOOST_CHECK_EQUAL(counters2.getOutData() , 3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000244}
245
246BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
247{
248 TcpFactory factory;
249
250 shared_ptr<TcpChannel> channel1 = factory.createChannel("::1", "20070");
251 shared_ptr<TcpChannel> channel2 = factory.createChannel("::1", "20071");
252
253 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
254 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
255
256 factory.createFace(FaceUri("tcp://[::1]:20070"),
257 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
258 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
259
260 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
261 "TcpChannel error: cannot connect or cannot accept connection");
262
263 BOOST_REQUIRE(static_cast<bool>(m_face1));
264 BOOST_REQUIRE(static_cast<bool>(m_face2));
265
266 BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp6://[::1]:20070");
267 // face1 has an unknown URI, since the source port is automatically chosen by OS
268
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800269 BOOST_CHECK_EQUAL(m_face1->isLocal(), true);
270 BOOST_CHECK_EQUAL(m_face2->isLocal(), true);
271
272 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true);
273 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true);
274
275 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800276
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700277 Interest interest1("ndn:/TpnzGvW9R");
278 Data data1 ("ndn:/KfczhUqVix");
279 data1.setContent(0, 0);
280 Interest interest2("ndn:/QWiIMfj5sL");
281 Data data2 ("ndn:/XNBV796f");
282 data2.setContent(0, 0);
283
284 ndn::SignatureSha256WithRsa fakeSignature;
285 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800286
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700287 // set fake signature on data1 and data2
288 data1.setSignature(fakeSignature);
289 data2.setSignature(fakeSignature);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800290
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700291 m_face1->sendInterest(interest1);
292 m_face1->sendData (data1 );
293 m_face2->sendInterest(interest2);
294 m_face2->sendData (data2 );
295
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700296 BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
297 "TcpChannel error: cannot send or receive Interest/Data packets");
298
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700299
300 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
301 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
302 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
303 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800304
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700305 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
306 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
307 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
308 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
309}
310
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800311BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
312{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800313 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800314
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800315 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
316 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800317
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800318 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
319 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800320
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800321 channel2->connect("127.0.0.1", "20070",
322 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
323 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800324 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800325
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700326 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
327 "TcpChannel error: cannot connect or cannot accept connection");
328
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800329
330 BOOST_CHECK_EQUAL(m_faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800331
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800332 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800333 channel3->connect("127.0.0.1", "20070",
334 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
335 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800336 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800337
338
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800339 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800340
341 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800342
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700343 scheduler::schedule(time::seconds(1),
344 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800345 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700346 static_cast<TcpChannel::FaceCreatedCallback>(
347 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
348 static_cast<TcpChannel::ConnectFailedCallback>(
349 bind(&EndToEndFixture::channel_onConnectFailed, this, _1)),
350 time::seconds(4)));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800351
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800352 scheduler::schedule(time::seconds(0.5),
353 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800354
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700355 BOOST_CHECK_MESSAGE(m_limitedIo.run(4,// 2 connects and 2 accepts
356 time::seconds(10)) == LimitedIo::EXCEED_OPS,
357 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800358
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800359 BOOST_CHECK_EQUAL(m_faces.size(), 6);
360}
361
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800362
363BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
364{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800365 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800366
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800367 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
368 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800369
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800370 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
371 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800372
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800373 channel2->connect("127.0.0.1", "20070",
374 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
375 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800376 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800377
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700378 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
379 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800380
381 BOOST_CHECK_EQUAL(channel1->size(), 1);
382 BOOST_CHECK_EQUAL(channel2->size(), 1);
383
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800384 BOOST_REQUIRE(static_cast<bool>(m_face1));
385 BOOST_CHECK(static_cast<bool>(m_face2));
386
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700387 // Face::close must be invoked during io run to be counted as an op
388 scheduler::schedule(time::seconds(0.1), bind(&Face::close, m_face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800389
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700390 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
391 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800392
393 // both faces should get closed
394 BOOST_CHECK(!static_cast<bool>(m_face1));
395 BOOST_CHECK(!static_cast<bool>(m_face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800396
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800397 BOOST_CHECK_EQUAL(channel1->size(), 0);
398 BOOST_CHECK_EQUAL(channel2->size(), 0);
399}
400
401
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700402BOOST_AUTO_TEST_SUITE_END()
403
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700404} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700405} // namespace nfd