blob: bc3ac30f58f5f41851a65cf5453515160feff492 [file] [log] [blame]
Junxiao Shi96dc0c42014-01-30 23:51:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi96dc0c42014-01-30 23:51:59 -070024
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080025#include "face/tcp-factory.hpp"
Alexander Afanasyev4a771362014-04-24 21:29:33 -070026#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shi96dc0c42014-01-30 23:51:59 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070029#include "tests/limited-io.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070033
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070035
36BOOST_AUTO_TEST_CASE(ChannelMap)
37{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080038 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080039
Alexander Afanasyevd6655302014-02-28 08:41:28 -080040 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
41 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070042 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070043 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080044
Alexander Afanasyevd6655302014-02-28 08:41:28 -080045 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070046 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070047
48 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
49 BOOST_CHECK_NE(channel2, channel3);
50 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070051}
52
Junxiao Shid9ee45c2014-02-27 15:38:11 -070053class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -070054{
55public:
56 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080057 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070058 {
Junxiao Shi79494162014-04-02 18:25:11 -070059 BOOST_CHECK(!static_cast<bool>(face1));
60 face1 = newFace;
61 face1->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070062 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070063 face1->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -070064 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070065 face1->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080066 bind(&EndToEndFixture::face1_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070067
Junxiao Shi79494162014-04-02 18:25:11 -070068 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070069 }
70
71 void
72 channel1_onConnectFailed(const std::string& reason)
73 {
74 BOOST_CHECK_MESSAGE(false, reason);
75
Junxiao Shi79494162014-04-02 18:25:11 -070076 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070077 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080078
Junxiao Shi96dc0c42014-01-30 23:51:59 -070079 void
80 face1_onReceiveInterest(const Interest& interest)
81 {
Junxiao Shi79494162014-04-02 18:25:11 -070082 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070083
Junxiao Shi79494162014-04-02 18:25:11 -070084 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070085 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080086
Junxiao Shi96dc0c42014-01-30 23:51:59 -070087 void
88 face1_onReceiveData(const Data& data)
89 {
Junxiao Shi79494162014-04-02 18:25:11 -070090 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -070091
Junxiao Shi79494162014-04-02 18:25:11 -070092 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -070093 }
94
95 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080096 face1_onFail()
97 {
Junxiao Shi79494162014-04-02 18:25:11 -070098 face1.reset();
99 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800100 }
101
102 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800103 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700104 {
Junxiao Shi79494162014-04-02 18:25:11 -0700105 BOOST_CHECK(!static_cast<bool>(face2));
106 face2 = newFace;
107 face2->onReceiveInterest +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700108 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700109 face2->onReceiveData +=
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700110 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700111 face2->onFail +=
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800112 bind(&EndToEndFixture::face2_onFail, this);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700113
Junxiao Shi79494162014-04-02 18:25:11 -0700114 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700115 }
116
117 void
118 channel2_onConnectFailed(const std::string& reason)
119 {
120 BOOST_CHECK_MESSAGE(false, reason);
121
Junxiao Shi79494162014-04-02 18:25:11 -0700122 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700123 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800124
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700125 void
126 face2_onReceiveInterest(const Interest& interest)
127 {
Junxiao Shi79494162014-04-02 18:25:11 -0700128 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700129
Junxiao Shi79494162014-04-02 18:25:11 -0700130 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700131 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800132
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700133 void
134 face2_onReceiveData(const Data& data)
135 {
Junxiao Shi79494162014-04-02 18:25:11 -0700136 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700137
Junxiao Shi79494162014-04-02 18:25:11 -0700138 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700139 }
140
141 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800142 face2_onFail()
143 {
Junxiao Shi79494162014-04-02 18:25:11 -0700144 face2.reset();
145 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800146 }
147
148 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800149 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800150 {
Junxiao Shi79494162014-04-02 18:25:11 -0700151 faces.push_back(newFace);
152 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800153 }
154
155 void
156 channel_onConnectFailed(const std::string& reason)
157 {
158 BOOST_CHECK_MESSAGE(false, reason);
159
Junxiao Shi79494162014-04-02 18:25:11 -0700160 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800161 }
162
163 void
164 checkFaceList(size_t shouldBe)
165 {
Junxiao Shi79494162014-04-02 18:25:11 -0700166 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800167 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800168
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700169public:
Junxiao Shi79494162014-04-02 18:25:11 -0700170 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700171
Junxiao Shi79494162014-04-02 18:25:11 -0700172 shared_ptr<Face> face1;
173 std::vector<Interest> face1_receivedInterests;
174 std::vector<Data> face1_receivedDatas;
175 shared_ptr<Face> face2;
176 std::vector<Interest> face2_receivedInterests;
177 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800178
Junxiao Shi79494162014-04-02 18:25:11 -0700179 std::list< shared_ptr<Face> > faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700180};
181
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000182BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700183{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600184 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700185
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600186 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
187 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800188
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700189 BOOST_CHECK_EQUAL(channel1->isListening(), false);
190
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700191 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
192 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800193
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700194 BOOST_CHECK_EQUAL(channel1->isListening(), true);
195
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600196 TcpFactory factory2;
197
198 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
199 factory2.createChannel("127.0.0.2", "20071");
200
201 factory2.createFace(FaceUri("tcp://127.0.0.1:20070"),
202 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
203 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700204
Junxiao Shi79494162014-04-02 18:25:11 -0700205 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700206 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700207
Junxiao Shi79494162014-04-02 18:25:11 -0700208 BOOST_REQUIRE(static_cast<bool>(face1));
209 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800210
Junxiao Shi79494162014-04-02 18:25:11 -0700211 BOOST_CHECK(face1->isOnDemand());
212 BOOST_CHECK(!face2->isOnDemand());
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700213
Junxiao Shi79494162014-04-02 18:25:11 -0700214 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
215 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
216 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000217
Junxiao Shi79494162014-04-02 18:25:11 -0700218 BOOST_CHECK_EQUAL(face1->isLocal(), true);
219 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000220
Junxiao Shi79494162014-04-02 18:25:11 -0700221 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
222 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000223
224 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
225
226 Interest interest1("ndn:/TpnzGvW9R");
227 Data data1 ("ndn:/KfczhUqVix");
228 data1.setContent(0, 0);
229 Interest interest2("ndn:/QWiIMfj5sL");
230 Data data2 ("ndn:/XNBV796f");
231 data2.setContent(0, 0);
232
233 ndn::SignatureSha256WithRsa fakeSignature;
234 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
235
236 // set fake signature on data1 and data2
237 data1.setSignature(fakeSignature);
238 data2.setSignature(fakeSignature);
239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 face1->sendInterest(interest1);
241 face1->sendInterest(interest1);
242 face1->sendInterest(interest1);
243 face1->sendData (data1 );
244 face2->sendInterest(interest2);
245 face2->sendData (data2 );
246 face2->sendData (data2 );
247 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000248
Junxiao Shi79494162014-04-02 18:25:11 -0700249 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000250 "TcpChannel error: cannot send or receive Interest/Data packets");
251
252
Junxiao Shi79494162014-04-02 18:25:11 -0700253 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
254 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
255 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
256 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000257
Junxiao Shi79494162014-04-02 18:25:11 -0700258 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
259 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
260 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
261 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000262
Junxiao Shi79494162014-04-02 18:25:11 -0700263 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700264 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
265 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
266 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
267 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000268
Junxiao Shi79494162014-04-02 18:25:11 -0700269 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700270 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
271 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
272 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
273 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000274}
275
276BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
277{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600278 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000279
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600280 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
281 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000282
283 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
284 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
285
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600286 TcpFactory factory2;
287
288 factory2.createChannel("::2", "20070");
289
290 factory2.createFace(FaceUri("tcp://[::1]:20070"),
291 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
292 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000293
Junxiao Shi79494162014-04-02 18:25:11 -0700294 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000295 "TcpChannel error: cannot connect or cannot accept connection");
296
Junxiao Shi79494162014-04-02 18:25:11 -0700297 BOOST_REQUIRE(static_cast<bool>(face1));
298 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000299
Junxiao Shi79494162014-04-02 18:25:11 -0700300 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
301 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
302 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000303
Junxiao Shi79494162014-04-02 18:25:11 -0700304 BOOST_CHECK_EQUAL(face1->isLocal(), true);
305 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800306
Junxiao Shi79494162014-04-02 18:25:11 -0700307 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
308 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800309
310 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800311
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700312 Interest interest1("ndn:/TpnzGvW9R");
313 Data data1 ("ndn:/KfczhUqVix");
314 data1.setContent(0, 0);
315 Interest interest2("ndn:/QWiIMfj5sL");
316 Data data2 ("ndn:/XNBV796f");
317 data2.setContent(0, 0);
318
319 ndn::SignatureSha256WithRsa fakeSignature;
320 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800321
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700322 // set fake signature on data1 and data2
323 data1.setSignature(fakeSignature);
324 data2.setSignature(fakeSignature);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800325
Junxiao Shi79494162014-04-02 18:25:11 -0700326 face1->sendInterest(interest1);
327 face1->sendData (data1 );
328 face2->sendInterest(interest2);
329 face2->sendData (data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700330
Junxiao Shi79494162014-04-02 18:25:11 -0700331 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700332 "TcpChannel error: cannot send or receive Interest/Data packets");
333
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700334
Junxiao Shi79494162014-04-02 18:25:11 -0700335 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
336 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
337 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
338 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800339
Junxiao Shi79494162014-04-02 18:25:11 -0700340 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
341 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
342 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
343 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700344}
345
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800346BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
347{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800348 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800349
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800350 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
351 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800352
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800353 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
354 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800355
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800356 channel2->connect("127.0.0.1", "20070",
357 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
358 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800359 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800360
Junxiao Shi79494162014-04-02 18:25:11 -0700361 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700362 "TcpChannel error: cannot connect or cannot accept connection");
363
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800364
Junxiao Shi79494162014-04-02 18:25:11 -0700365 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800366
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800367 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800368 channel3->connect("127.0.0.1", "20070",
369 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
370 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800371 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800372
373
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800374 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800375
376 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800377
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700378 scheduler::schedule(time::seconds(1),
379 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800380 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700381 static_cast<TcpChannel::FaceCreatedCallback>(
382 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
383 static_cast<TcpChannel::ConnectFailedCallback>(
384 bind(&EndToEndFixture::channel_onConnectFailed, this, _1)),
385 time::seconds(4)));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800386
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700387 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800388 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800389
Junxiao Shi79494162014-04-02 18:25:11 -0700390 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700391 time::seconds(10)) == LimitedIo::EXCEED_OPS,
392 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800393
Junxiao Shi79494162014-04-02 18:25:11 -0700394 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800395}
396
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800397
398BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
399{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800400 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800401
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800402 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
403 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800404
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800405 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
406 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800407
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800408 channel2->connect("127.0.0.1", "20070",
409 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
410 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800411 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800412
Junxiao Shi79494162014-04-02 18:25:11 -0700413 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700414 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800415
416 BOOST_CHECK_EQUAL(channel1->size(), 1);
417 BOOST_CHECK_EQUAL(channel2->size(), 1);
418
Junxiao Shi79494162014-04-02 18:25:11 -0700419 BOOST_REQUIRE(static_cast<bool>(face1));
420 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800421
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700422 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700423 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800424
Junxiao Shi79494162014-04-02 18:25:11 -0700425 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700426 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800427
428 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700429 BOOST_CHECK(!static_cast<bool>(face1));
430 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800431
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800432 BOOST_CHECK_EQUAL(channel1->size(), 0);
433 BOOST_CHECK_EQUAL(channel2->size(), 0);
434}
435
436
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700437BOOST_AUTO_TEST_SUITE_END()
438
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700439} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700440} // namespace nfd