blob: 06ff5d375cda73acd73fbd99f8299345d1e8a54f [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- 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 **/
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010024
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080025#include "face/unix-stream-factory.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010026
Junxiao Shid9ee45c2014-02-27 15:38:11 -070027#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070028#include "tests/limited-io.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070029#include "dummy-stream-sender.hpp"
30#include "packet-datasets.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031
32namespace nfd {
33namespace tests {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010034
35using namespace boost::asio::local;
36
Junxiao Shi61e3cc52014-03-03 20:40:28 -070037#define CHANNEL_PATH1 "unix-stream-test.1.sock"
38#define CHANNEL_PATH2 "unix-stream-test.2.sock"
39
Junxiao Shid9ee45c2014-02-27 15:38:11 -070040BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010041
42BOOST_AUTO_TEST_CASE(ChannelMap)
43{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080044 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010045
Junxiao Shi61e3cc52014-03-03 20:40:28 -070046 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
47 shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010048 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070049 std::string channel1uri = channel1->getUri().toString();
50 BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
51 BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
52 channel1uri.size() - std::string(CHANNEL_PATH1).size());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010053
Junxiao Shi61e3cc52014-03-03 20:40:28 -070054 shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010055 BOOST_CHECK_NE(channel1, channel2);
56}
57
Junxiao Shid9ee45c2014-02-27 15:38:11 -070058class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010059{
60public:
61 void
62 client_onConnect(const boost::system::error_code& error)
63 {
64 BOOST_CHECK_MESSAGE(!error, error.message());
65
Junxiao Shi79494162014-04-02 18:25:11 -070066 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010067 }
68
69 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -070070 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010071 {
Junxiao Shi79494162014-04-02 18:25:11 -070072 BOOST_CHECK(!static_cast<bool>(face1));
73 face1 = static_pointer_cast<UnixStreamFace>(newFace);
74 face1->onReceiveInterest +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010075 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -070076 face1->onReceiveData +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010077 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
78
Junxiao Shi79494162014-04-02 18:25:11 -070079 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010080 }
81
82 void
83 channel1_onConnectFailed(const std::string& reason)
84 {
85 BOOST_CHECK_MESSAGE(false, reason);
86
Junxiao Shi79494162014-04-02 18:25:11 -070087 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010088 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080089
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010090 void
91 face1_onReceiveInterest(const Interest& interest)
92 {
Junxiao Shi79494162014-04-02 18:25:11 -070093 face1_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010094
Junxiao Shi79494162014-04-02 18:25:11 -070095 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010096 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080097
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010098 void
99 face1_onReceiveData(const Data& data)
100 {
Junxiao Shi79494162014-04-02 18:25:11 -0700101 face1_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100102
Junxiao Shi79494162014-04-02 18:25:11 -0700103 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100104 }
105
106 void
107 face2_onReceiveInterest(const Interest& interest)
108 {
Junxiao Shi79494162014-04-02 18:25:11 -0700109 face2_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100110
Junxiao Shi79494162014-04-02 18:25:11 -0700111 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100112 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800113
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100114 void
115 face2_onReceiveData(const Data& data)
116 {
Junxiao Shi79494162014-04-02 18:25:11 -0700117 face2_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100118
Junxiao Shi79494162014-04-02 18:25:11 -0700119 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100120 }
121
122 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700123 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100124 {
Junxiao Shi79494162014-04-02 18:25:11 -0700125 faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100126
Junxiao Shi79494162014-04-02 18:25:11 -0700127 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100128 }
129
130 void
131 channel_onConnectFailed(const std::string& reason)
132 {
133 BOOST_CHECK_MESSAGE(false, reason);
134
Junxiao Shi79494162014-04-02 18:25:11 -0700135 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100136 }
137
138protected:
Junxiao Shi79494162014-04-02 18:25:11 -0700139 LimitedIo limitedIo;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100140
Junxiao Shi79494162014-04-02 18:25:11 -0700141 shared_ptr<UnixStreamFace> face1;
142 std::vector<Interest> face1_receivedInterests;
143 std::vector<Data> face1_receivedDatas;
144 shared_ptr<UnixStreamFace> face2;
145 std::vector<Interest> face2_receivedInterests;
146 std::vector<Data> face2_receivedDatas;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100147
Junxiao Shi79494162014-04-02 18:25:11 -0700148 std::list< shared_ptr<UnixStreamFace> > faces;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100149};
150
151
152BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
153{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800154 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100155
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700156 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100157 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
158 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
159
160 shared_ptr<stream_protocol::socket> client =
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800161 make_shared<stream_protocol::socket>(boost::ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700162 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100163 bind(&EndToEndFixture::client_onConnect, this, _1));
164
Junxiao Shi79494162014-04-02 18:25:11 -0700165 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700166 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100167
Junxiao Shi79494162014-04-02 18:25:11 -0700168 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100169
Junxiao Shi79494162014-04-02 18:25:11 -0700170 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
171 BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost()));
172 std::string face1localUri = face1->getLocalUri().toString();
173 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
174 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
175 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000176
Junxiao Shi79494162014-04-02 18:25:11 -0700177 face2 = make_shared<UnixStreamFace>(client);
178 face2->onReceiveInterest +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100179 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700180 face2->onReceiveData +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100181 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
182
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700183 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
184 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
185 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
186 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100187
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700188 face1->sendInterest(*interest1);
189 face1->sendInterest(*interest1);
190 face1->sendInterest(*interest1);
191 face1->sendData (*data1 );
192 face2->sendInterest(*interest2);
193 face2->sendData (*data2 );
194 face2->sendData (*data2 );
195 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100196
Junxiao Shi79494162014-04-02 18:25:11 -0700197 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700198 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100199
Junxiao Shi79494162014-04-02 18:25:11 -0700200 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
201 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
202 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
203 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100204
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700205 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
206 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
207 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
208 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000209
Junxiao Shi79494162014-04-02 18:25:11 -0700210 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700211 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
212 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
213 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
214 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000215
Junxiao Shi79494162014-04-02 18:25:11 -0700216 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700217 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
218 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
219 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
220 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100221}
222
223BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
224{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800225 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100226
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700227 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100228 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
229 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
230
231 shared_ptr<stream_protocol::socket> client1 =
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800232 make_shared<stream_protocol::socket>(boost::ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700233 client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100234 bind(&EndToEndFixture::client_onConnect, this, _1));
235
Junxiao Shi79494162014-04-02 18:25:11 -0700236 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700237 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100238
Junxiao Shi79494162014-04-02 18:25:11 -0700239 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100240
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100241 shared_ptr<stream_protocol::socket> client2 =
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800242 make_shared<stream_protocol::socket>(boost::ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700243 client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100244 bind(&EndToEndFixture::client_onConnect, this, _1));
245
Junxiao Shi79494162014-04-02 18:25:11 -0700246 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700247 "UnixStreamChannel error: cannot accept multiple connections");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100248
Junxiao Shi79494162014-04-02 18:25:11 -0700249 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100250
251 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700252 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100253
254 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700255 face1 = faces.back();
256 face1->onReceiveInterest += bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
257 face1->onReceiveData += bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100258
Junxiao Shi79494162014-04-02 18:25:11 -0700259 face2 = make_shared<UnixStreamFace>(client2);
260 face2->onReceiveInterest += bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
261 face2->onReceiveData += bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100262
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700263 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
264 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
265 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
266 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100267
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700268 face1->sendInterest(*interest1);
269 face1->sendData (*data1 );
270 face2->sendInterest(*interest2);
271 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700274 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100275
Junxiao Shi79494162014-04-02 18:25:11 -0700276 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
277 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
278 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
279 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100280
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700281 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
282 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
283 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
284 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100285}
286
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800287static inline void
288noOp()
289{
290}
291
292BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
293{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800294 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800295
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700296 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800297 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
298 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
299
300 shared_ptr<stream_protocol::socket> client =
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800301 make_shared<stream_protocol::socket>(boost::ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700302 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800303 bind(&EndToEndFixture::client_onConnect, this, _1));
304
Junxiao Shi79494162014-04-02 18:25:11 -0700305 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700306 "UnixStreamChannel error: cannot connect or cannot accept connection");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800307
Junxiao Shi79494162014-04-02 18:25:11 -0700308 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800309
Junxiao Shi79494162014-04-02 18:25:11 -0700310 face2 = make_shared<UnixStreamFace>(client);
311 face2->onReceiveInterest +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800312 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700313 face2->onReceiveData +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800314 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
315
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700316 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
317 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800318
Junxiao Shi79494162014-04-02 18:25:11 -0700319 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
320 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800321
Junxiao Shi79494162014-04-02 18:25:11 -0700322 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
323 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800324
Junxiao Shi79494162014-04-02 18:25:11 -0700325 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
326 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800327
Junxiao Shi79494162014-04-02 18:25:11 -0700328 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
329 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800330
331 ////////////////////////////////////////////////////////
332
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700333 interest1->setIncomingFaceId(11);
334 interest1->setNextHopFaceId(111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800335
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700336 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800337
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700338 data1->setIncomingFaceId(22);
339 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800340
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700341 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800342
343 //
344
Junxiao Shi79494162014-04-02 18:25:11 -0700345 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700346 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800347
Junxiao Shi79494162014-04-02 18:25:11 -0700348 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
349 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800350
351 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700352 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
353 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800354
Junxiao Shi79494162014-04-02 18:25:11 -0700355 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
356 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800357
358 ////////////////////////////////////////////////////////
359
360 using namespace boost::asio;
361
362 std::vector<const_buffer> interestWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700363 Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1, true, true);
364 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800365 interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size()));
366 interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));
367
368 std::vector<const_buffer> dataWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700369 Block dHeader = data1->getLocalControlHeader().wireEncode(*data1, true, true);
370 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800371 dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size()));
372 dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));
373
374 //
375
376 client->async_send(interestWithHeader, bind(&noOp));
377 client->async_send(dataWithHeader, bind(&noOp));
378
Junxiao Shi79494162014-04-02 18:25:11 -0700379 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700380 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800381
Junxiao Shi79494162014-04-02 18:25:11 -0700382 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
383 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800384
Junxiao Shi79494162014-04-02 18:25:11 -0700385 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
386 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
387 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800388
Junxiao Shi79494162014-04-02 18:25:11 -0700389 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
390 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800391}
392
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700393
394class SimpleEndToEndFixture : protected BaseFixture
395{
396public:
397 void
398 onFaceCreated(const shared_ptr<Face>& face)
399 {
400 face->onReceiveInterest +=
401 bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1);
402 face->onReceiveData +=
403 bind(&SimpleEndToEndFixture::onReceiveData, this, _1);
404 face->onFail +=
405 bind(&SimpleEndToEndFixture::onFail, this, face);
406
407 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
408 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
409 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
410
411 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
412 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
413 }
414
415 limitedIo.afterOp();
416 }
417
418 void
419 onConnectFailed(const std::string& reason)
420 {
421 BOOST_CHECK_MESSAGE(false, reason);
422
423 limitedIo.afterOp();
424 }
425
426 void
427 onReceiveInterest(const Interest& interest)
428 {
429 receivedInterests.push_back(interest);
430
431 limitedIo.afterOp();
432 }
433
434 void
435 onReceiveData(const Data& data)
436 {
437 receivedDatas.push_back(data);
438
439 limitedIo.afterOp();
440 }
441
442 void
443 onFail(const shared_ptr<Face>& face)
444 {
445 limitedIo.afterOp();
446 }
447
448public:
449 LimitedIo limitedIo;
450
451 std::vector<Interest> receivedInterests;
452 std::vector<Data> receivedDatas;
453};
454
455
456BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
457 CorruptedPackets, SimpleEndToEndFixture)
458{
459 UnixStreamFactory factory;
460
461 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
462 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
463 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
464
465 DummyStreamSender<stream_protocol, Dataset> sender;
466 sender.start(stream_protocol::endpoint(CHANNEL_PATH1));
467
468 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
469 time::seconds(1)) == LimitedIo::EXCEED_TIME,
470 "Exception thrown for " + Dataset::getName());
471}
472
473
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100474BOOST_AUTO_TEST_SUITE_END()
475
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700476} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100477} // namespace nfd