blob: 9223283a77158ddf26dde9cffeefea8c8f40e96a [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
165BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
166{
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");
170 shared_ptr<TcpChannel> channel2 = 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
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700175 channel2->connect("127.0.0.1", "20070",
176 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
177 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800178 time::seconds(4));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700179
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700180 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
181 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700182
183 BOOST_REQUIRE(static_cast<bool>(m_face1));
184 BOOST_REQUIRE(static_cast<bool>(m_face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800185
186 BOOST_CHECK_EQUAL(m_face1->isLocal(), true);
187 BOOST_CHECK_EQUAL(m_face2->isLocal(), true);
188
189 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true);
190 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true);
191
192 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800193
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700194 Interest interest1("ndn:/TpnzGvW9R");
195 Data data1 ("ndn:/KfczhUqVix");
196 data1.setContent(0, 0);
197 Interest interest2("ndn:/QWiIMfj5sL");
198 Data data2 ("ndn:/XNBV796f");
199 data2.setContent(0, 0);
200
201 ndn::SignatureSha256WithRsa fakeSignature;
202 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800203
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700204 // set fake signature on data1 and data2
205 data1.setSignature(fakeSignature);
206 data2.setSignature(fakeSignature);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800207
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700208 m_face1->sendInterest(interest1);
209 m_face1->sendData (data1 );
210 m_face2->sendInterest(interest2);
211 m_face2->sendData (data2 );
212
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700213 BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
214 "TcpChannel error: cannot send or receive Interest/Data packets");
215
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700216
217 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
218 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
219 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
220 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800221
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700222 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
223 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
224 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
225 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
226}
227
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800228BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
229{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800230 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800231
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800232 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
233 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800234
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800235 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
236 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800237
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800238 channel2->connect("127.0.0.1", "20070",
239 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
240 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800241 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800242
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700243 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
244 "TcpChannel error: cannot connect or cannot accept connection");
245
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800246
247 BOOST_CHECK_EQUAL(m_faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800248
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800249 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800250 channel3->connect("127.0.0.1", "20070",
251 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
252 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800253 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800254
255
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800256 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800257
258 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800259
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700260 scheduler::schedule(time::seconds(1),
261 bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070",
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800262 // does not work without static_cast
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700263 static_cast<TcpChannel::FaceCreatedCallback>(
264 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
265 static_cast<TcpChannel::ConnectFailedCallback>(
266 bind(&EndToEndFixture::channel_onConnectFailed, this, _1)),
267 time::seconds(4)));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800268
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800269 scheduler::schedule(time::seconds(0.5),
270 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800271
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700272 BOOST_CHECK_MESSAGE(m_limitedIo.run(4,// 2 connects and 2 accepts
273 time::seconds(10)) == LimitedIo::EXCEED_OPS,
274 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800275
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800276 BOOST_CHECK_EQUAL(m_faces.size(), 6);
277}
278
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800279
280BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
281{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800282 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800283
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800284 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
285 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800286
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800287 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
288 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800289
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800290 channel2->connect("127.0.0.1", "20070",
291 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
292 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800293 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800294
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700295 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
296 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800297
298 BOOST_CHECK_EQUAL(channel1->size(), 1);
299 BOOST_CHECK_EQUAL(channel2->size(), 1);
300
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800301 BOOST_REQUIRE(static_cast<bool>(m_face1));
302 BOOST_CHECK(static_cast<bool>(m_face2));
303
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700304 // Face::close must be invoked during io run to be counted as an op
305 scheduler::schedule(time::seconds(0.1), bind(&Face::close, m_face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800306
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700307 BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
308 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800309
310 // both faces should get closed
311 BOOST_CHECK(!static_cast<bool>(m_face1));
312 BOOST_CHECK(!static_cast<bool>(m_face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800313
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800314 BOOST_CHECK_EQUAL(channel1->size(), 0);
315 BOOST_CHECK_EQUAL(channel2->size(), 0);
316}
317
318
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700319BOOST_AUTO_TEST_SUITE_END()
320
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700321} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700322} // namespace nfd