blob: de48540911e1e8cfab2705de21ae064916583520 [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
109 abortTestCase(const std::string& message)
110 {
111 m_ioService.stop();
112 BOOST_FAIL(message);
113 }
114
115private:
116 void
117 afterIo()
118 {
119 if (--m_ioRemaining <= 0) m_ioService.stop();
120 }
121
122public:
123 boost::asio::io_service m_ioService;
124
125 int m_ioRemaining;
126
127 shared_ptr<TcpFace> m_face1;
128 std::vector<Interest> m_face1_receivedInterests;
129 std::vector<Data> m_face1_receivedDatas;
130 shared_ptr<TcpFace> m_face2;
131 std::vector<Interest> m_face2_receivedInterests;
132 std::vector<Data> m_face2_receivedDatas;
133};
134
135
136BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
137{
138 TcpChannelFactory factory(m_ioService);
139 Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
140
141 EventId abortEvent =
142 scheduler.scheduleEvent(time::seconds(1),
143 bind(&EndToEndFixture::abortTestCase, this,
144 "TcpChannel error: cannot connect or cannot accept connection"));
145
146 shared_ptr<TcpChannel> channel1 = factory.create("127.0.0.1", "20070");
147 shared_ptr<TcpChannel> channel2 = factory.create("127.0.0.1", "20071");
148
149 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
150 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
151
152 channel2->connect("127.0.0.1", "20070",
153 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
154 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
155 time::milliseconds(100)); // very short timeout
156
157 m_ioRemaining = 2;
158 m_ioService.run();
159 m_ioService.reset();
160 scheduler.cancelEvent(abortEvent);
161
162 BOOST_REQUIRE(static_cast<bool>(m_face1));
163 BOOST_REQUIRE(static_cast<bool>(m_face2));
164
165 abortEvent =
166 scheduler.scheduleEvent(time::seconds(1),
167 bind(&EndToEndFixture::abortTestCase, this,
168 "TcpChannel error: cannot send or receive Interest/Data packets"));
169
170 Interest interest1("ndn:/TpnzGvW9R");
171 Data data1 ("ndn:/KfczhUqVix");
172 data1.setContent(0, 0);
173 Interest interest2("ndn:/QWiIMfj5sL");
174 Data data2 ("ndn:/XNBV796f");
175 data2.setContent(0, 0);
176
177 ndn::SignatureSha256WithRsa fakeSignature;
178 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
179
180 // set fake signature on data1 and data2
181 data1.setSignature(fakeSignature);
182 data2.setSignature(fakeSignature);
183
184 m_face1->sendInterest(interest1);
185 m_face1->sendData (data1 );
186 m_face2->sendInterest(interest2);
187 m_face2->sendData (data2 );
188
189 m_ioRemaining = 4;
190 m_ioService.run();
191 m_ioService.reset();
192
193 BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
194 BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1);
195 BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
196 BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1);
197
198 BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
199 BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName());
200 BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
201 BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName());
202}
203
204BOOST_AUTO_TEST_SUITE_END()
205
206} // namespace nfd