blob: c1ee2cb35764d676896e9a429de979a975307727 [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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
Davide Pesavento6ad890a2015-03-09 03:43:17 +010026#include "face/unix-stream-channel.hpp"
27#include "face/unix-stream-face.hpp"
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080028#include "face/unix-stream-factory.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070031#include "tests/limited-io.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070032#include "dummy-stream-sender.hpp"
33#include "packet-datasets.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034
35namespace nfd {
36namespace tests {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037
38using namespace boost::asio::local;
39
Junxiao Shi61e3cc52014-03-03 20:40:28 -070040#define CHANNEL_PATH1 "unix-stream-test.1.sock"
41#define CHANNEL_PATH2 "unix-stream-test.2.sock"
42
Junxiao Shid9ee45c2014-02-27 15:38:11 -070043BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010044
45BOOST_AUTO_TEST_CASE(ChannelMap)
46{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080047 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010048
Junxiao Shi61e3cc52014-03-03 20:40:28 -070049 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
50 shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010051 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070052 std::string channel1uri = channel1->getUri().toString();
53 BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
54 BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
55 channel1uri.size() - std::string(CHANNEL_PATH1).size());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010056
Junxiao Shi61e3cc52014-03-03 20:40:28 -070057 shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010058 BOOST_CHECK_NE(channel1, channel2);
59}
60
Steve DiBenedettoef04f272014-06-04 14:28:31 -060061BOOST_AUTO_TEST_CASE(GetChannels)
62{
63 UnixStreamFactory factory;
64 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
65
66 std::vector<shared_ptr<const Channel> > expectedChannels;
67
68 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH1));
69 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH2));
70
71 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
72 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
73 i != channels.end(); ++i)
74 {
75 std::vector<shared_ptr<const Channel> >::iterator pos =
76 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
77
78 BOOST_REQUIRE(pos != expectedChannels.end());
79 expectedChannels.erase(pos);
80 }
81
82 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
83}
84
Junxiao Shid9ee45c2014-02-27 15:38:11 -070085class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010086{
87public:
88 void
89 client_onConnect(const boost::system::error_code& error)
90 {
91 BOOST_CHECK_MESSAGE(!error, error.message());
92
Junxiao Shi79494162014-04-02 18:25:11 -070093 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010094 }
95
96 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -070097 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010098 {
Junxiao Shi79494162014-04-02 18:25:11 -070099 BOOST_CHECK(!static_cast<bool>(face1));
100 face1 = static_pointer_cast<UnixStreamFace>(newFace);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700101 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
102 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100103
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 Pesavento94279412015-02-27 01:29:32 +0100193 BOOST_CHECK_EQUAL(face1->isLocal(), true);
194 BOOST_CHECK_EQUAL(face1->isOnDemand(), true);
195 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700196 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
197 BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost()));
198 std::string face1localUri = face1->getLocalUri().toString();
199 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
200 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
201 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000202
Junxiao Shi79494162014-04-02 18:25:11 -0700203 face2 = make_shared<UnixStreamFace>(client);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700204 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
205 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100206
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 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700216 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700217 face2->sendInterest(*interest2);
218 face2->sendData (*data2 );
219 face2->sendData (*data2 );
220 face2->sendData (*data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700221 size_t nBytesSent2 = interest2->wireEncode().size() + data2->wireEncode().size() * 3;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100222
Junxiao Shi79494162014-04-02 18:25:11 -0700223 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700224 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100225
Junxiao Shi79494162014-04-02 18:25:11 -0700226 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
227 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
228 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
229 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100230
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700231 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
232 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
233 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
234 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000235
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700236 // needed to ensure NOutBytes counters are accurate
237 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
238
Junxiao Shi79494162014-04-02 18:25:11 -0700239 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700240 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
241 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
242 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
243 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700244 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
245 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000246
Junxiao Shi79494162014-04-02 18:25:11 -0700247 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700248 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
249 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
250 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
251 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100252}
253
254BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
255{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800256 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100257
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700258 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100259 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
260 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
261
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700262 shared_ptr<stream_protocol::socket> client1 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700263 client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100264 bind(&EndToEndFixture::client_onConnect, this, _1));
265
Junxiao Shi79494162014-04-02 18:25:11 -0700266 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700267 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100268
Junxiao Shi79494162014-04-02 18:25:11 -0700269 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100270
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700271 shared_ptr<stream_protocol::socket> client2 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700272 client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100273 bind(&EndToEndFixture::client_onConnect, this, _1));
274
Junxiao Shi79494162014-04-02 18:25:11 -0700275 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700276 "UnixStreamChannel error: cannot accept multiple connections");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100277
Junxiao Shi79494162014-04-02 18:25:11 -0700278 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100279
280 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700281 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100282
283 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700284 face1 = faces.back();
Junxiao Shic099ddb2014-12-25 20:53:20 -0700285 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
286 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100287
Junxiao Shi79494162014-04-02 18:25:11 -0700288 face2 = make_shared<UnixStreamFace>(client2);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700289 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
290 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100291
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700292 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
293 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
294 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
295 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100296
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700297 face1->sendInterest(*interest1);
298 face1->sendData (*data1 );
299 face2->sendInterest(*interest2);
300 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100301
Junxiao Shi79494162014-04-02 18:25:11 -0700302 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700303 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100304
Junxiao Shi79494162014-04-02 18:25:11 -0700305 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
306 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
307 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
308 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100309
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700310 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
311 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
312 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
313 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100314}
315
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800316static inline void
317noOp()
318{
319}
320
321BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
322{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800323 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800324
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700325 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800326 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
327 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
328
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700329 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700330 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800331 bind(&EndToEndFixture::client_onConnect, this, _1));
332
Junxiao Shi79494162014-04-02 18:25:11 -0700333 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700334 "UnixStreamChannel error: cannot connect or cannot accept connection");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800335
Junxiao Shi79494162014-04-02 18:25:11 -0700336 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800337
Junxiao Shi79494162014-04-02 18:25:11 -0700338 face2 = make_shared<UnixStreamFace>(client);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700339 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
340 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800341
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700342 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
343 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800344
Junxiao Shi79494162014-04-02 18:25:11 -0700345 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
346 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800347
Junxiao Shi79494162014-04-02 18:25:11 -0700348 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
349 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800350
Junxiao Shi79494162014-04-02 18:25:11 -0700351 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
352 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800353
Junxiao Shi79494162014-04-02 18:25:11 -0700354 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
355 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800356
357 ////////////////////////////////////////////////////////
358
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700359 interest1->setIncomingFaceId(11);
360 interest1->setNextHopFaceId(111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800361
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700362 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800363
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700364 data1->setIncomingFaceId(22);
365 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800366
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700367 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800368
369 //
370
Junxiao Shi79494162014-04-02 18:25:11 -0700371 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700372 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800373
Junxiao Shi79494162014-04-02 18:25:11 -0700374 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
375 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800376
377 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700378 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
379 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800380
Junxiao Shi79494162014-04-02 18:25:11 -0700381 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
382 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800383
384 ////////////////////////////////////////////////////////
385
386 using namespace boost::asio;
387
388 std::vector<const_buffer> interestWithHeader;
Alexander Afanasyeva4ff44c2015-02-13 14:11:37 -0800389 Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1,
390 ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
391 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700392 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800393 interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size()));
394 interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));
395
396 std::vector<const_buffer> dataWithHeader;
Alexander Afanasyeva4ff44c2015-02-13 14:11:37 -0800397 Block dHeader = data1->getLocalControlHeader().wireEncode(*data1,
398 ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
399 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700400 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800401 dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size()));
402 dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));
403
404 //
405
406 client->async_send(interestWithHeader, bind(&noOp));
407 client->async_send(dataWithHeader, bind(&noOp));
408
Junxiao Shi79494162014-04-02 18:25:11 -0700409 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700410 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800411
Junxiao Shi79494162014-04-02 18:25:11 -0700412 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
413 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800414
Junxiao Shi79494162014-04-02 18:25:11 -0700415 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
416 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
417 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800418
Junxiao Shi79494162014-04-02 18:25:11 -0700419 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
420 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800421}
422
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700423
424class SimpleEndToEndFixture : protected BaseFixture
425{
426public:
427 void
428 onFaceCreated(const shared_ptr<Face>& face)
429 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700430 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
431 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
432 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700433
434 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
435 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
436 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
437
438 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
439 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
440 }
441
442 limitedIo.afterOp();
443 }
444
445 void
446 onConnectFailed(const std::string& reason)
447 {
448 BOOST_CHECK_MESSAGE(false, reason);
449
450 limitedIo.afterOp();
451 }
452
453 void
454 onReceiveInterest(const Interest& interest)
455 {
456 receivedInterests.push_back(interest);
457
458 limitedIo.afterOp();
459 }
460
461 void
462 onReceiveData(const Data& data)
463 {
464 receivedDatas.push_back(data);
465
466 limitedIo.afterOp();
467 }
468
469 void
470 onFail(const shared_ptr<Face>& face)
471 {
472 limitedIo.afterOp();
473 }
474
475public:
476 LimitedIo limitedIo;
477
478 std::vector<Interest> receivedInterests;
479 std::vector<Data> receivedDatas;
480};
481
482
483BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
484 CorruptedPackets, SimpleEndToEndFixture)
485{
486 UnixStreamFactory factory;
487
488 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
489 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
490 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
491
492 DummyStreamSender<stream_protocol, Dataset> sender;
493 sender.start(stream_protocol::endpoint(CHANNEL_PATH1));
494
495 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
496 time::seconds(1)) == LimitedIo::EXCEED_TIME,
497 "Exception thrown for " + Dataset::getName());
498}
499
500
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100501BOOST_AUTO_TEST_SUITE_END()
502
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700503} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100504} // namespace nfd