blob: a8390fbc8364dcea5842d977d64713e91a3cb963 [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
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);
Junxiao Shic099ddb2014-12-25 20:53:20 -070099 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
100 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100101
Junxiao Shi79494162014-04-02 18:25:11 -0700102 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100103 }
104
105 void
106 channel1_onConnectFailed(const std::string& reason)
107 {
108 BOOST_CHECK_MESSAGE(false, reason);
109
Junxiao Shi79494162014-04-02 18:25:11 -0700110 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100111 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800112
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100113 void
114 face1_onReceiveInterest(const Interest& interest)
115 {
Junxiao Shi79494162014-04-02 18:25:11 -0700116 face1_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100117
Junxiao Shi79494162014-04-02 18:25:11 -0700118 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100119 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800120
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100121 void
122 face1_onReceiveData(const Data& data)
123 {
Junxiao Shi79494162014-04-02 18:25:11 -0700124 face1_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100125
Junxiao Shi79494162014-04-02 18:25:11 -0700126 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100127 }
128
129 void
130 face2_onReceiveInterest(const Interest& interest)
131 {
Junxiao Shi79494162014-04-02 18:25:11 -0700132 face2_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100133
Junxiao Shi79494162014-04-02 18:25:11 -0700134 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100135 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800136
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100137 void
138 face2_onReceiveData(const Data& data)
139 {
Junxiao Shi79494162014-04-02 18:25:11 -0700140 face2_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100141
Junxiao Shi79494162014-04-02 18:25:11 -0700142 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100143 }
144
145 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700146 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100147 {
Junxiao Shi79494162014-04-02 18:25:11 -0700148 faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100149
Junxiao Shi79494162014-04-02 18:25:11 -0700150 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100151 }
152
153 void
154 channel_onConnectFailed(const std::string& reason)
155 {
156 BOOST_CHECK_MESSAGE(false, reason);
157
Junxiao Shi79494162014-04-02 18:25:11 -0700158 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100159 }
160
161protected:
Junxiao Shi79494162014-04-02 18:25:11 -0700162 LimitedIo limitedIo;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100163
Junxiao Shi79494162014-04-02 18:25:11 -0700164 shared_ptr<UnixStreamFace> face1;
165 std::vector<Interest> face1_receivedInterests;
166 std::vector<Data> face1_receivedDatas;
167 shared_ptr<UnixStreamFace> face2;
168 std::vector<Interest> face2_receivedInterests;
169 std::vector<Data> face2_receivedDatas;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100170
Junxiao Shi79494162014-04-02 18:25:11 -0700171 std::list< shared_ptr<UnixStreamFace> > faces;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100172};
173
174
175BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
176{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800177 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100178
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700179 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100180 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
181 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
182
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700183 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700184 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100185 bind(&EndToEndFixture::client_onConnect, this, _1));
186
Junxiao Shi79494162014-04-02 18:25:11 -0700187 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700188 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100189
Junxiao Shi79494162014-04-02 18:25:11 -0700190 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100191
Junxiao Shi79494162014-04-02 18:25:11 -0700192 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
193 BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost()));
194 std::string face1localUri = face1->getLocalUri().toString();
195 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
196 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
197 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000198
Junxiao Shi79494162014-04-02 18:25:11 -0700199 face2 = make_shared<UnixStreamFace>(client);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700200 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
201 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100202
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700203 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
204 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
205 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
206 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100207
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700208 face1->sendInterest(*interest1);
209 face1->sendInterest(*interest1);
210 face1->sendInterest(*interest1);
211 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700212 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700213 face2->sendInterest(*interest2);
214 face2->sendData (*data2 );
215 face2->sendData (*data2 );
216 face2->sendData (*data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700217 size_t nBytesSent2 = interest2->wireEncode().size() + data2->wireEncode().size() * 3;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100218
Junxiao Shi79494162014-04-02 18:25:11 -0700219 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700220 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100221
Junxiao Shi79494162014-04-02 18:25:11 -0700222 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
223 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
224 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
225 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100226
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700227 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
228 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
229 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
230 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000231
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700232 // needed to ensure NOutBytes counters are accurate
233 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
234
Junxiao Shi79494162014-04-02 18:25:11 -0700235 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700236 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
237 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
238 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
239 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700240 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
241 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000242
Junxiao Shi79494162014-04-02 18:25:11 -0700243 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700244 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
245 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
246 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
247 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100248}
249
250BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
251{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800252 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100253
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700254 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100255 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
256 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
257
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700258 shared_ptr<stream_protocol::socket> client1 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700259 client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100260 bind(&EndToEndFixture::client_onConnect, this, _1));
261
Junxiao Shi79494162014-04-02 18:25:11 -0700262 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700263 "UnixStreamChannel error: cannot connect or cannot accept connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100264
Junxiao Shi79494162014-04-02 18:25:11 -0700265 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100266
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700267 shared_ptr<stream_protocol::socket> client2 = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700268 client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100269 bind(&EndToEndFixture::client_onConnect, this, _1));
270
Junxiao Shi79494162014-04-02 18:25:11 -0700271 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700272 "UnixStreamChannel error: cannot accept multiple connections");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100273
Junxiao Shi79494162014-04-02 18:25:11 -0700274 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100275
276 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700277 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100278
279 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700280 face1 = faces.back();
Junxiao Shic099ddb2014-12-25 20:53:20 -0700281 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
282 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100283
Junxiao Shi79494162014-04-02 18:25:11 -0700284 face2 = make_shared<UnixStreamFace>(client2);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700285 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
286 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100287
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700288 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
289 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
290 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
291 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100292
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700293 face1->sendInterest(*interest1);
294 face1->sendData (*data1 );
295 face2->sendInterest(*interest2);
296 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100297
Junxiao Shi79494162014-04-02 18:25:11 -0700298 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700299 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100300
Junxiao Shi79494162014-04-02 18:25:11 -0700301 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
302 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
303 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
304 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100305
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700306 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
307 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
308 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
309 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100310}
311
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800312static inline void
313noOp()
314{
315}
316
317BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
318{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800319 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800320
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700321 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800322 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
323 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
324
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700325 shared_ptr<stream_protocol::socket> client = make_shared<stream_protocol::socket>(ref(g_io));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700326 client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800327 bind(&EndToEndFixture::client_onConnect, this, _1));
328
Junxiao Shi79494162014-04-02 18:25:11 -0700329 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700330 "UnixStreamChannel error: cannot connect or cannot accept connection");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800331
Junxiao Shi79494162014-04-02 18:25:11 -0700332 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800333
Junxiao Shi79494162014-04-02 18:25:11 -0700334 face2 = make_shared<UnixStreamFace>(client);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700335 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
336 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800337
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700338 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
339 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800340
Junxiao Shi79494162014-04-02 18:25:11 -0700341 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
342 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800343
Junxiao Shi79494162014-04-02 18:25:11 -0700344 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
345 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800346
Junxiao Shi79494162014-04-02 18:25:11 -0700347 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
348 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800349
Junxiao Shi79494162014-04-02 18:25:11 -0700350 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
351 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800352
353 ////////////////////////////////////////////////////////
354
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700355 interest1->setIncomingFaceId(11);
356 interest1->setNextHopFaceId(111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800357
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700358 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800359
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700360 data1->setIncomingFaceId(22);
361 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800362
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700363 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800364
365 //
366
Junxiao Shi79494162014-04-02 18:25:11 -0700367 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700368 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800369
Junxiao Shi79494162014-04-02 18:25:11 -0700370 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
371 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800372
373 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700374 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
375 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800376
Junxiao Shi79494162014-04-02 18:25:11 -0700377 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
378 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800379
380 ////////////////////////////////////////////////////////
381
382 using namespace boost::asio;
383
384 std::vector<const_buffer> interestWithHeader;
Alexander Afanasyeva4ff44c2015-02-13 14:11:37 -0800385 Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1,
386 ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
387 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700388 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800389 interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size()));
390 interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));
391
392 std::vector<const_buffer> dataWithHeader;
Alexander Afanasyeva4ff44c2015-02-13 14:11:37 -0800393 Block dHeader = data1->getLocalControlHeader().wireEncode(*data1,
394 ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
395 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700396 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800397 dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size()));
398 dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));
399
400 //
401
402 client->async_send(interestWithHeader, bind(&noOp));
403 client->async_send(dataWithHeader, bind(&noOp));
404
Junxiao Shi79494162014-04-02 18:25:11 -0700405 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700406 "UnixStreamChannel error: cannot send or receive Interest/Data packets");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800407
Junxiao Shi79494162014-04-02 18:25:11 -0700408 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
409 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800410
Junxiao Shi79494162014-04-02 18:25:11 -0700411 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
412 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
413 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800414
Junxiao Shi79494162014-04-02 18:25:11 -0700415 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
416 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800417}
418
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700419
420class SimpleEndToEndFixture : protected BaseFixture
421{
422public:
423 void
424 onFaceCreated(const shared_ptr<Face>& face)
425 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700426 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
427 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
428 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700429
430 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
431 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
432 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
433
434 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
435 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
436 }
437
438 limitedIo.afterOp();
439 }
440
441 void
442 onConnectFailed(const std::string& reason)
443 {
444 BOOST_CHECK_MESSAGE(false, reason);
445
446 limitedIo.afterOp();
447 }
448
449 void
450 onReceiveInterest(const Interest& interest)
451 {
452 receivedInterests.push_back(interest);
453
454 limitedIo.afterOp();
455 }
456
457 void
458 onReceiveData(const Data& data)
459 {
460 receivedDatas.push_back(data);
461
462 limitedIo.afterOp();
463 }
464
465 void
466 onFail(const shared_ptr<Face>& face)
467 {
468 limitedIo.afterOp();
469 }
470
471public:
472 LimitedIo limitedIo;
473
474 std::vector<Interest> receivedInterests;
475 std::vector<Data> receivedDatas;
476};
477
478
479BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
480 CorruptedPackets, SimpleEndToEndFixture)
481{
482 UnixStreamFactory factory;
483
484 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
485 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
486 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
487
488 DummyStreamSender<stream_protocol, Dataset> sender;
489 sender.start(stream_protocol::endpoint(CHANNEL_PATH1));
490
491 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
492 time::seconds(1)) == LimitedIo::EXCEED_TIME,
493 "Exception thrown for " + Dataset::getName());
494}
495
496
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100497BOOST_AUTO_TEST_SUITE_END()
498
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700499} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100500} // namespace nfd