blob: 7d86f395bcf5e29365aa767f1f708b0a57247761 [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
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700160 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700161 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100162 bind(&EndToEndFixture::client_onConnect, this, _1));
163
Junxiao Shi79494162014-04-02 18:25:11 -0700164 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700165 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100166
Junxiao Shi79494162014-04-02 18:25:11 -0700167 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100168
Junxiao Shi79494162014-04-02 18:25:11 -0700169 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
170 BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost()));
171 std::string face1localUri = face1->getLocalUri().toString();
172 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
173 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
174 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000175
Junxiao Shi79494162014-04-02 18:25:11 -0700176 face2 = make_shared<UnixStreamFace>(client);
177 face2->onReceiveInterest +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100178 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700179 face2->onReceiveData +=
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100180 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
181
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700182 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
183 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
184 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
185 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100186
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700187 face1->sendInterest(*interest1);
188 face1->sendInterest(*interest1);
189 face1->sendInterest(*interest1);
190 face1->sendData (*data1 );
191 face2->sendInterest(*interest2);
192 face2->sendData (*data2 );
193 face2->sendData (*data2 );
194 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100195
Junxiao Shi79494162014-04-02 18:25:11 -0700196 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700197 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100198
Junxiao Shi79494162014-04-02 18:25:11 -0700199 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
200 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
201 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
202 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100203
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700204 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
205 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
206 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
207 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000208
Junxiao Shi79494162014-04-02 18:25:11 -0700209 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700210 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
211 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
212 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
213 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000214
Junxiao Shi79494162014-04-02 18:25:11 -0700215 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700216 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
217 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
218 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
219 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100220}
221
222BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
223{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800224 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100225
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700226 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100227 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
228 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
229
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700230 shared_ptr<stream_protocol::socket> client1 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700231 client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100232 bind(&EndToEndFixture::client_onConnect, this, _1));
233
Junxiao Shi79494162014-04-02 18:25:11 -0700234 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700235 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100236
Junxiao Shi79494162014-04-02 18:25:11 -0700237 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100238
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700239 shared_ptr<stream_protocol::socket> client2 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700240 client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100241 bind(&EndToEndFixture::client_onConnect, this, _1));
242
Junxiao Shi79494162014-04-02 18:25:11 -0700243 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700244 "UnixStreamChannel error: cannot accept multiple connections");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100245
Junxiao Shi79494162014-04-02 18:25:11 -0700246 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100247
248 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700249 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100250
251 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700252 face1 = faces.back();
253 face1->onReceiveInterest += bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
254 face1->onReceiveData += bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100255
Junxiao Shi79494162014-04-02 18:25:11 -0700256 face2 = make_shared<UnixStreamFace>(client2);
257 face2->onReceiveInterest += bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
258 face2->onReceiveData += bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100259
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700260 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
261 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
262 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
263 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100264
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700265 face1->sendInterest(*interest1);
266 face1->sendData (*data1 );
267 face2->sendInterest(*interest2);
268 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100269
Junxiao Shi79494162014-04-02 18:25:11 -0700270 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700271 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
274 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
275 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
276 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100277
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700278 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
279 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
280 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
281 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100282}
283
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800284static inline void
285noOp()
286{
287}
288
289BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
290{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800291 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800292
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700293 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800294 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
295 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
296
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700297 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700298 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800299 bind(&EndToEndFixture::client_onConnect, this, _1));
300
Junxiao Shi79494162014-04-02 18:25:11 -0700301 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700302 "UnixStreamChannel error: cannot connect or cannot accept connection");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800303
Junxiao Shi79494162014-04-02 18:25:11 -0700304 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800305
Junxiao Shi79494162014-04-02 18:25:11 -0700306 face2 = make_shared<UnixStreamFace>(client);
307 face2->onReceiveInterest +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800308 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700309 face2->onReceiveData +=
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800310 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
311
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700312 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
313 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800314
Junxiao Shi79494162014-04-02 18:25:11 -0700315 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
316 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800317
Junxiao Shi79494162014-04-02 18:25:11 -0700318 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
319 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800320
Junxiao Shi79494162014-04-02 18:25:11 -0700321 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
322 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800323
Junxiao Shi79494162014-04-02 18:25:11 -0700324 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
325 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800326
327 ////////////////////////////////////////////////////////
328
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700329 interest1->setIncomingFaceId(11);
330 interest1->setNextHopFaceId(111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800331
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700332 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800333
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700334 data1->setIncomingFaceId(22);
335 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800336
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700337 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800338
339 //
340
Junxiao Shi79494162014-04-02 18:25:11 -0700341 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700342 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800343
Junxiao Shi79494162014-04-02 18:25:11 -0700344 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
345 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800346
347 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700348 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
349 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800350
Junxiao Shi79494162014-04-02 18:25:11 -0700351 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
352 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800353
354 ////////////////////////////////////////////////////////
355
356 using namespace boost::asio;
357
358 std::vector<const_buffer> interestWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700359 Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1, true, true);
360 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800361 interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size()));
362 interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));
363
364 std::vector<const_buffer> dataWithHeader;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700365 Block dHeader = data1->getLocalControlHeader().wireEncode(*data1, true, true);
366 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800367 dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size()));
368 dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));
369
370 //
371
372 client->async_send(interestWithHeader, bind(&noOp));
373 client->async_send(dataWithHeader, bind(&noOp));
374
Junxiao Shi79494162014-04-02 18:25:11 -0700375 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700376 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800377
Junxiao Shi79494162014-04-02 18:25:11 -0700378 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
379 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800380
Junxiao Shi79494162014-04-02 18:25:11 -0700381 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
382 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
383 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800384
Junxiao Shi79494162014-04-02 18:25:11 -0700385 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
386 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800387}
388
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700389
390class SimpleEndToEndFixture : protected BaseFixture
391{
392public:
393 void
394 onFaceCreated(const shared_ptr<Face>& face)
395 {
396 face->onReceiveInterest +=
397 bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1);
398 face->onReceiveData +=
399 bind(&SimpleEndToEndFixture::onReceiveData, this, _1);
400 face->onFail +=
401 bind(&SimpleEndToEndFixture::onFail, this, face);
402
403 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
404 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
405 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
406
407 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
408 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
409 }
410
411 limitedIo.afterOp();
412 }
413
414 void
415 onConnectFailed(const std::string& reason)
416 {
417 BOOST_CHECK_MESSAGE(false, reason);
418
419 limitedIo.afterOp();
420 }
421
422 void
423 onReceiveInterest(const Interest& interest)
424 {
425 receivedInterests.push_back(interest);
426
427 limitedIo.afterOp();
428 }
429
430 void
431 onReceiveData(const Data& data)
432 {
433 receivedDatas.push_back(data);
434
435 limitedIo.afterOp();
436 }
437
438 void
439 onFail(const shared_ptr<Face>& face)
440 {
441 limitedIo.afterOp();
442 }
443
444public:
445 LimitedIo limitedIo;
446
447 std::vector<Interest> receivedInterests;
448 std::vector<Data> receivedDatas;
449};
450
451
452BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
453 CorruptedPackets, SimpleEndToEndFixture)
454{
455 UnixStreamFactory factory;
456
457 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
458 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
459 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
460
461 DummyStreamSender<stream_protocol, Dataset> sender;
462 sender.start(stream_protocol::endpoint(CHANNEL_PATH1));
463
464 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
465 time::seconds(1)) == LimitedIo::EXCEED_TIME,
466 "Exception thrown for " + Dataset::getName());
467}
468
469
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100470BOOST_AUTO_TEST_SUITE_END()
471
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700472} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100473} // namespace nfd