blob: cb48db43062256a50b67a625d356f54990186bbb [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010025
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "face/unix-stream-factory.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010027
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"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070030#include "dummy-stream-sender.hpp"
31#include "packet-datasets.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032
33namespace nfd {
34namespace tests {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010035
36using namespace boost::asio::local;
37
Junxiao Shi61e3cc52014-03-03 20:40:28 -070038#define CHANNEL_PATH1 "unix-stream-test.1.sock"
39#define CHANNEL_PATH2 "unix-stream-test.2.sock"
40
Junxiao Shid9ee45c2014-02-27 15:38:11 -070041BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010042
43BOOST_AUTO_TEST_CASE(ChannelMap)
44{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080045 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046
Junxiao Shi61e3cc52014-03-03 20:40:28 -070047 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
48 shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010049 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050 std::string channel1uri = channel1->getUri().toString();
51 BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
52 BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
53 channel1uri.size() - std::string(CHANNEL_PATH1).size());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010054
Junxiao Shi61e3cc52014-03-03 20:40:28 -070055 shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010056 BOOST_CHECK_NE(channel1, channel2);
57}
58
Steve DiBenedettoef04f272014-06-04 14:28:31 -060059BOOST_AUTO_TEST_CASE(GetChannels)
60{
61 UnixStreamFactory factory;
62 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
63
64 std::vector<shared_ptr<const Channel> > expectedChannels;
65
66 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH1));
67 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH2));
68
69 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
70 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
71 i != channels.end(); ++i)
72 {
73 std::vector<shared_ptr<const Channel> >::iterator pos =
74 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
75
76 BOOST_REQUIRE(pos != expectedChannels.end());
77 expectedChannels.erase(pos);
78 }
79
80 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
81}
82
Junxiao Shid9ee45c2014-02-27 15:38:11 -070083class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010084{
85public:
86 void
87 client_onConnect(const boost::system::error_code& error)
88 {
89 BOOST_CHECK_MESSAGE(!error, error.message());
90
Junxiao Shi79494162014-04-02 18:25:11 -070091 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010092 }
93
94 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -070095 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010096 {
Junxiao Shi79494162014-04-02 18:25:11 -070097 BOOST_CHECK(!static_cast<bool>(face1));
98 face1 = static_pointer_cast<UnixStreamFace>(newFace);
99 face1->onReceiveInterest +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100100 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700101 face1->onReceiveData +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100102 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
103
Junxiao Shi79494162014-04-02 18:25:11 -0700104 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100105 }
106
107 void
108 channel1_onConnectFailed(const std::string& reason)
109 {
110 BOOST_CHECK_MESSAGE(false, reason);
111
Junxiao Shi79494162014-04-02 18:25:11 -0700112 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100113 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800114
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100115 void
116 face1_onReceiveInterest(const Interest& interest)
117 {
Junxiao Shi79494162014-04-02 18:25:11 -0700118 face1_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100119
Junxiao Shi79494162014-04-02 18:25:11 -0700120 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100121 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800122
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100123 void
124 face1_onReceiveData(const Data& data)
125 {
Junxiao Shi79494162014-04-02 18:25:11 -0700126 face1_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100127
Junxiao Shi79494162014-04-02 18:25:11 -0700128 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100129 }
130
131 void
132 face2_onReceiveInterest(const Interest& interest)
133 {
Junxiao Shi79494162014-04-02 18:25:11 -0700134 face2_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100135
Junxiao Shi79494162014-04-02 18:25:11 -0700136 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100137 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800138
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100139 void
140 face2_onReceiveData(const Data& data)
141 {
Junxiao Shi79494162014-04-02 18:25:11 -0700142 face2_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100143
Junxiao Shi79494162014-04-02 18:25:11 -0700144 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100145 }
146
147 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700148 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100149 {
Junxiao Shi79494162014-04-02 18:25:11 -0700150 faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100151
Junxiao Shi79494162014-04-02 18:25:11 -0700152 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100153 }
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();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100161 }
162
163protected:
Junxiao Shi79494162014-04-02 18:25:11 -0700164 LimitedIo limitedIo;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100165
Junxiao Shi79494162014-04-02 18:25:11 -0700166 shared_ptr<UnixStreamFace> face1;
167 std::vector<Interest> face1_receivedInterests;
168 std::vector<Data> face1_receivedDatas;
169 shared_ptr<UnixStreamFace> face2;
170 std::vector<Interest> face2_receivedInterests;
171 std::vector<Data> face2_receivedDatas;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100172
Junxiao Shi79494162014-04-02 18:25:11 -0700173 std::list< shared_ptr<UnixStreamFace> > faces;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100174};
175
176
177BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
178{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800179 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100180
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700181 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100182 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
183 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
184
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700185 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700186 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100187 bind(&EndToEndFixture::client_onConnect, this, _1));
188
Junxiao Shi79494162014-04-02 18:25:11 -0700189 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700190 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100191
Junxiao Shi79494162014-04-02 18:25:11 -0700192 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100193
Junxiao Shi79494162014-04-02 18:25:11 -0700194 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
195 BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost()));
196 std::string face1localUri = face1->getLocalUri().toString();
197 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
198 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
199 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000200
Junxiao Shi79494162014-04-02 18:25:11 -0700201 face2 = make_shared<UnixStreamFace>(client);
202 face2->onReceiveInterest +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100203 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700204 face2->onReceiveData +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100205 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
206
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700207 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
208 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
209 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
210 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100211
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700212 face1->sendInterest(*interest1);
213 face1->sendInterest(*interest1);
214 face1->sendInterest(*interest1);
215 face1->sendData (*data1 );
216 face2->sendInterest(*interest2);
217 face2->sendData (*data2 );
218 face2->sendData (*data2 );
219 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100220
Junxiao Shi79494162014-04-02 18:25:11 -0700221 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700222 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100223
Junxiao Shi79494162014-04-02 18:25:11 -0700224 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
225 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
226 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
227 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100228
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700229 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
230 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
231 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
232 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000233
Junxiao Shi79494162014-04-02 18:25:11 -0700234 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700235 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
236 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
237 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
238 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700241 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
242 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
243 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
244 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100245}
246
247BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
248{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800249 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100250
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700251 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100252 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
253 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
254
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700255 shared_ptr<stream_protocol::socket> client1 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700256 client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100257 bind(&EndToEndFixture::client_onConnect, this, _1));
258
Junxiao Shi79494162014-04-02 18:25:11 -0700259 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700260 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100261
Junxiao Shi79494162014-04-02 18:25:11 -0700262 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100263
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700264 shared_ptr<stream_protocol::socket> client2 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700265 client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100266 bind(&EndToEndFixture::client_onConnect, this, _1));
267
Junxiao Shi79494162014-04-02 18:25:11 -0700268 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700269 "UnixStreamChannel error: cannot accept multiple connections");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100270
Junxiao Shi79494162014-04-02 18:25:11 -0700271 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100272
273 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700274 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100275
276 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700277 face1 = faces.back();
278 face1->onReceiveInterest += bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
279 face1->onReceiveData += bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100280
Junxiao Shi79494162014-04-02 18:25:11 -0700281 face2 = make_shared<UnixStreamFace>(client2);
282 face2->onReceiveInterest += bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
283 face2->onReceiveData += bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100284
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700285 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
286 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
287 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
288 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100289
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700290 face1->sendInterest(*interest1);
291 face1->sendData (*data1 );
292 face2->sendInterest(*interest2);
293 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100294
Junxiao Shi79494162014-04-02 18:25:11 -0700295 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700296 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100297
Junxiao Shi79494162014-04-02 18:25:11 -0700298 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
299 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
300 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
301 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100302
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700303 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
304 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
305 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
306 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100307}
308
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800309static inline void
310noOp()
311{
312}
313
314BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
315{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800316 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800317
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700318 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800319 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
320 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
321
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700322 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700323 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800324 bind(&EndToEndFixture::client_onConnect, this, _1));
325
Junxiao Shi79494162014-04-02 18:25:11 -0700326 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700327 "UnixStreamChannel error: cannot connect or cannot accept connection");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800328
Junxiao Shi79494162014-04-02 18:25:11 -0700329 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800330
Junxiao Shi79494162014-04-02 18:25:11 -0700331 face2 = make_shared<UnixStreamFace>(client);
332 face2->onReceiveInterest +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800333 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700334 face2->onReceiveData +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800335 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
336
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700337 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
338 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800339
Junxiao Shi79494162014-04-02 18:25:11 -0700340 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
341 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800342
Junxiao Shi79494162014-04-02 18:25:11 -0700343 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
344 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800345
Junxiao Shi79494162014-04-02 18:25:11 -0700346 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
347 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800348
Junxiao Shi79494162014-04-02 18:25:11 -0700349 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
350 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800351
352 ////////////////////////////////////////////////////////
353
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700354 interest1->setIncomingFaceId(11);
355 interest1->setNextHopFaceId(111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800356
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700357 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800358
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700359 data1->setIncomingFaceId(22);
360 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800361
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700362 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800363
364 //
365
Junxiao Shi79494162014-04-02 18:25:11 -0700366 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700367 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800368
Junxiao Shi79494162014-04-02 18:25:11 -0700369 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
370 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800371
372 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700373 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
374 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800375
Junxiao Shi79494162014-04-02 18:25:11 -0700376 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
377 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800378
379 ////////////////////////////////////////////////////////
380
381 using namespace boost::asio;
382
383 std::vector<const_buffer> interestWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700384 Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1, true, true);
385 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800386 interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size()));
387 interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));
388
389 std::vector<const_buffer> dataWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700390 Block dHeader = data1->getLocalControlHeader().wireEncode(*data1, true, true);
391 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800392 dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size()));
393 dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));
394
395 //
396
397 client->async_send(interestWithHeader, bind(&noOp));
398 client->async_send(dataWithHeader, bind(&noOp));
399
Junxiao Shi79494162014-04-02 18:25:11 -0700400 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700401 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800402
Junxiao Shi79494162014-04-02 18:25:11 -0700403 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
404 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800405
Junxiao Shi79494162014-04-02 18:25:11 -0700406 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
407 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
408 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800409
Junxiao Shi79494162014-04-02 18:25:11 -0700410 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
411 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800412}
413
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700414
415class SimpleEndToEndFixture : protected BaseFixture
416{
417public:
418 void
419 onFaceCreated(const shared_ptr<Face>& face)
420 {
421 face->onReceiveInterest +=
422 bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1);
423 face->onReceiveData +=
424 bind(&SimpleEndToEndFixture::onReceiveData, this, _1);
425 face->onFail +=
426 bind(&SimpleEndToEndFixture::onFail, this, face);
427
428 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
429 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
430 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
431
432 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
433 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
434 }
435
436 limitedIo.afterOp();
437 }
438
439 void
440 onConnectFailed(const std::string& reason)
441 {
442 BOOST_CHECK_MESSAGE(false, reason);
443
444 limitedIo.afterOp();
445 }
446
447 void
448 onReceiveInterest(const Interest& interest)
449 {
450 receivedInterests.push_back(interest);
451
452 limitedIo.afterOp();
453 }
454
455 void
456 onReceiveData(const Data& data)
457 {
458 receivedDatas.push_back(data);
459
460 limitedIo.afterOp();
461 }
462
463 void
464 onFail(const shared_ptr<Face>& face)
465 {
466 limitedIo.afterOp();
467 }
468
469public:
470 LimitedIo limitedIo;
471
472 std::vector<Interest> receivedInterests;
473 std::vector<Data> receivedDatas;
474};
475
476
477BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
478 CorruptedPackets, SimpleEndToEndFixture)
479{
480 UnixStreamFactory factory;
481
482 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
483 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
484 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
485
486 DummyStreamSender<stream_protocol, Dataset> sender;
487 sender.start(stream_protocol::endpoint(CHANNEL_PATH1));
488
489 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
490 time::seconds(1)) == LimitedIo::EXCEED_TIME,
491 "Exception thrown for " + Dataset::getName());
492}
493
494
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100495BOOST_AUTO_TEST_SUITE_END()
496
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700497} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100498} // namespace nfd