blob: 66c0a69eb3403093260d3438cc5db74a9fc4ae62 [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
7#include "face/tcp-channel-factory.hpp"
8#include "core/scheduler.hpp"
9
10#include <ndn-cpp-dev/security/key-chain.hpp>
11
12#include <boost/test/unit_test.hpp>
13
14namespace nfd {
15
16BOOST_AUTO_TEST_SUITE(FaceTcp)
17
18BOOST_AUTO_TEST_CASE(ChannelMap)
19{
20 boost::asio::io_service io;
21 TcpChannelFactory factory(io);
22
23 shared_ptr<TcpChannel> channel1 = factory.create("127.0.0.1", "20070");
24 shared_ptr<TcpChannel> channel1a = factory.create("127.0.0.1", "20070");
25 BOOST_CHECK_EQUAL(channel1, channel1a);
26
27 shared_ptr<TcpChannel> channel2 = factory.create("127.0.0.1", "20071");
28 BOOST_CHECK_NE(channel1, channel2);
29}
30
31class EndToEndFixture
32{
33public:
34 void
35 channel1_onFaceCreated(const shared_ptr<TcpFace>& newFace)
36 {
37 BOOST_CHECK(!static_cast<bool>(m_face1));
38 m_face1 = newFace;
39 m_face1->onReceiveInterest +=
40 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
41 m_face1->onReceiveData +=
42 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
43
44 this->afterIo();
45 }
46
47 void
48 channel1_onConnectFailed(const std::string& reason)
49 {
50 BOOST_CHECK_MESSAGE(false, reason);
51
52 this->afterIo();
53 }
54
55 void
56 face1_onReceiveInterest(const Interest& interest)
57 {
58 m_face1_receivedInterests.push_back(interest);
59
60 this->afterIo();
61 }
62
63 void
64 face1_onReceiveData(const Data& data)
65 {
66 m_face1_receivedDatas.push_back(data);
67
68 this->afterIo();
69 }
70
71 void
72 channel2_onFaceCreated(const shared_ptr<TcpFace>& newFace)
73 {
74 BOOST_CHECK(!static_cast<bool>(m_face2));
75 m_face2 = newFace;
76 m_face2->onReceiveInterest +=
77 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
78 m_face2->onReceiveData +=
79 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
80
81 this->afterIo();
82 }
83
84 void
85 channel2_onConnectFailed(const std::string& reason)
86 {
87 BOOST_CHECK_MESSAGE(false, reason);
88
89 this->afterIo();
90 }
91
92 void
93 face2_onReceiveInterest(const Interest& interest)
94 {
95 m_face2_receivedInterests.push_back(interest);
96
97 this->afterIo();
98 }
99
100 void
101 face2_onReceiveData(const Data& data)
102 {
103 m_face2_receivedDatas.push_back(data);
104
105 this->afterIo();
106 }
107
108 void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800109 channel_onFaceCreated(const shared_ptr<TcpFace>& newFace)
110 {
111 m_faces.push_back(newFace);
112 this->afterIo();
113 }
114
115 void
116 channel_onConnectFailed(const std::string& reason)
117 {
118 BOOST_CHECK_MESSAGE(false, reason);
119
120 this->afterIo();
121 }
122
123 void
124 checkFaceList(size_t shouldBe)
125 {
126 BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
127 }
128
129 void
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700130 abortTestCase(const std::string& message)
131 {
132 m_ioService.stop();
133 BOOST_FAIL(message);
134 }
135
136private:
137 void
138 afterIo()
139 {
140 if (--m_ioRemaining <= 0) m_ioService.stop();
141 }
142
143public:
144 boost::asio::io_service m_ioService;
145
146 int m_ioRemaining;
147
148 shared_ptr<TcpFace> m_face1;
149 std::vector<Interest> m_face1_receivedInterests;
150 std::vector<Data> m_face1_receivedDatas;
151 shared_ptr<TcpFace> m_face2;
152 std::vector<Interest> m_face2_receivedInterests;
153 std::vector<Data> m_face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800154
155 std::list< shared_ptr<TcpFace> > m_faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700156};
157
158
159BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
160{
161 TcpChannelFactory factory(m_ioService);
162 Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
163
164 EventId abortEvent =
165 scheduler.scheduleEvent(time::seconds(1),
166 bind(&EndToEndFixture::abortTestCase, this,
167 "TcpChannel error: cannot connect or cannot accept connection"));
168
169 shared_ptr<TcpChannel> channel1 = factory.create("127.0.0.1", "20070");
170 shared_ptr<TcpChannel> channel2 = factory.create("127.0.0.1", "20071");
171
172 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
173 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
174
175 channel2->connect("127.0.0.1", "20070",
176 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
177 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
178 time::milliseconds(100)); // very short timeout
179
180 m_ioRemaining = 2;
181 m_ioService.run();
182 m_ioService.reset();
183 scheduler.cancelEvent(abortEvent);
184
185 BOOST_REQUIRE(static_cast<bool>(m_face1));
186 BOOST_REQUIRE(static_cast<bool>(m_face2));
187
188 abortEvent =
189 scheduler.scheduleEvent(time::seconds(1),
190 bind(&EndToEndFixture::abortTestCase, this,
191 "TcpChannel error: cannot send or receive Interest/Data packets"));
192
193 Interest interest1("ndn:/TpnzGvW9R");
194 Data data1 ("ndn:/KfczhUqVix");
195 data1.setContent(0, 0);
196 Interest interest2("ndn:/QWiIMfj5sL");
197 Data data2 ("ndn:/XNBV796f");
198 data2.setContent(0, 0);
199
200 ndn::SignatureSha256WithRsa fakeSignature;
201 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
202
203 // set fake signature on data1 and data2
204 data1.setSignature(fakeSignature);
205 data2.setSignature(fakeSignature);
206
207 m_face1->sendInterest(interest1);
208 m_face1->sendData (data1 );
209 m_face2->sendInterest(interest2);
210 m_face2->sendData (data2 );
211
212 m_ioRemaining = 4;
213 m_ioService.run();
214 m_ioService.reset();
215
216 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
217 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
218 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
219 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
220
221 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
222 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
223 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
224 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
225}
226
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800227BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
228{
229 TcpChannelFactory factory(m_ioService);
230 Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
231
232 EventId abortEvent =
233 scheduler.scheduleEvent(time::seconds(1),
234 bind(&EndToEndFixture::abortTestCase, this,
235 "TcpChannel error: cannot connect or cannot accept connection"));
236
237 shared_ptr<TcpChannel> channel1 = factory.create("127.0.0.1", "20070");
238 shared_ptr<TcpChannel> channel2 = factory.create("127.0.0.1", "20071");
239
240 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
241 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
242
243 channel2->connect("127.0.0.1", "20070",
244 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
245 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
246 time::milliseconds(100)); // very short timeout
247
248 m_ioRemaining = 2;
249 m_ioService.run();
250 m_ioService.reset();
251 scheduler.cancelEvent(abortEvent);
252
253 BOOST_CHECK_EQUAL(m_faces.size(), 2);
254
255 shared_ptr<TcpChannel> channel3 = factory.create("127.0.0.1", "20072");
256 channel3->connect("127.0.0.1", "20070",
257 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
258 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
259 time::milliseconds(100)); // very short timeout
260
261
262 shared_ptr<TcpChannel> channel4 = factory.create("127.0.0.1", "20073");
263
264 BOOST_CHECK_NE(channel3, channel4);
265
266 scheduler
267 .scheduleEvent
268 (time::seconds(0.5),
269 bind(&TcpChannel::connect, channel4,
270 "127.0.0.1", "20070",
271 // does not work without static_cast
272 static_cast<TcpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
273 channel_onFaceCreated, this, _1)),
274 static_cast<TcpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
275 channel_onConnectFailed, this, _1)),
276 time::milliseconds(100)));
277
278 m_ioRemaining = 4; // 2 connects and 2 accepts
279 abortEvent =
280 scheduler.scheduleEvent(time::seconds(1),
281 bind(&EndToEndFixture::abortTestCase, this,
282 "TcpChannel error: cannot connect or cannot accept multiple connections"));
283
284 scheduler.scheduleEvent(time::seconds(0.4),
285 bind(&EndToEndFixture::checkFaceList, this, 4));
286
287 m_ioService.run();
288 m_ioService.reset();
289
290 BOOST_CHECK_EQUAL(m_faces.size(), 6);
291}
292
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700293BOOST_AUTO_TEST_SUITE_END()
294
295} // namespace nfd