blob: 98167295aa4383c4684d288ae71d46bd37bdd975 [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
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;
Davide Pesavento292e5e12015-03-13 02:08:33 +010062 BOOST_CHECK(factory.getChannels().empty());
Steve DiBenedettoef04f272014-06-04 14:28:31 -060063
Davide Pesavento292e5e12015-03-13 02:08:33 +010064 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060065 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH1));
66 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH2));
67
Davide Pesavento292e5e12015-03-13 02:08:33 +010068 for (const auto& channel : factory.getChannels()) {
69 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), channel);
70 BOOST_REQUIRE(pos != expectedChannels.end());
71 expectedChannels.erase(pos);
72 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060073
74 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
75}
76
Junxiao Shid9ee45c2014-02-27 15:38:11 -070077class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010078{
79public:
80 void
81 client_onConnect(const boost::system::error_code& error)
82 {
83 BOOST_CHECK_MESSAGE(!error, error.message());
84
Junxiao Shi79494162014-04-02 18:25:11 -070085 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010086 }
87
88 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -070089 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010090 {
Junxiao Shi79494162014-04-02 18:25:11 -070091 BOOST_CHECK(!static_cast<bool>(face1));
92 face1 = static_pointer_cast<UnixStreamFace>(newFace);
Junxiao Shic099ddb2014-12-25 20:53:20 -070093 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
94 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010095
Junxiao Shi79494162014-04-02 18:25:11 -070096 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010097 }
98
99 void
100 channel1_onConnectFailed(const std::string& reason)
101 {
102 BOOST_CHECK_MESSAGE(false, reason);
103
Junxiao Shi79494162014-04-02 18:25:11 -0700104 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100105 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800106
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100107 void
108 face1_onReceiveInterest(const Interest& interest)
109 {
Junxiao Shi79494162014-04-02 18:25:11 -0700110 face1_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100111
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_onReceiveData(const Data& data)
117 {
Junxiao Shi79494162014-04-02 18:25:11 -0700118 face1_receivedDatas.push_back(data);
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 }
122
123 void
124 face2_onReceiveInterest(const Interest& interest)
125 {
Junxiao Shi79494162014-04-02 18:25:11 -0700126 face2_receivedInterests.push_back(interest);
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 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800130
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100131 void
132 face2_onReceiveData(const Data& data)
133 {
Junxiao Shi79494162014-04-02 18:25:11 -0700134 face2_receivedDatas.push_back(data);
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 }
138
139 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700140 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100141 {
Junxiao Shi79494162014-04-02 18:25:11 -0700142 faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
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
148 channel_onConnectFailed(const std::string& reason)
149 {
150 BOOST_CHECK_MESSAGE(false, reason);
151
Junxiao Shi79494162014-04-02 18:25:11 -0700152 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100153 }
154
Davide Pesavento292e5e12015-03-13 02:08:33 +0100155 shared_ptr<UnixStreamFace>
156 makeFace(UnixStreamFace::protocol::socket socket)
157 {
158 auto remoteUri = FaceUri::fromFd(socket.native_handle());
159 auto localUri = FaceUri(socket.local_endpoint());
160 return make_shared<UnixStreamFace>(remoteUri, localUri, std::move(socket));
161 }
162
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100163protected:
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
Davide Pesavento292e5e12015-03-13 02:08:33 +0100173 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
Davide Pesavento292e5e12015-03-13 02:08:33 +0100185 UnixStreamFace::protocol::socket client(g_io);
186 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
187 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100188
Davide Pesavento292e5e12015-03-13 02:08:33 +0100189 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100190
Junxiao Shi79494162014-04-02 18:25:11 -0700191 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesavento94279412015-02-27 01:29:32 +0100192 BOOST_CHECK_EQUAL(face1->isLocal(), true);
193 BOOST_CHECK_EQUAL(face1->isOnDemand(), true);
194 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700195 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
Davide Pesavento292e5e12015-03-13 02:08:33 +0100196 BOOST_CHECK_NO_THROW(std::stoi(face1->getRemoteUri().getHost()));
Junxiao Shi79494162014-04-02 18:25:11 -0700197 std::string face1localUri = face1->getLocalUri().toString();
198 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
199 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
200 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000201
Davide Pesavento292e5e12015-03-13 02:08:33 +0100202 face2 = makeFace(std::move(client));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700203 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
204 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100205
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700206 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
207 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
208 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
209 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100210
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700211 face1->sendInterest(*interest1);
212 face1->sendInterest(*interest1);
213 face1->sendInterest(*interest1);
214 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700215 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700216 face2->sendInterest(*interest2);
217 face2->sendData (*data2 );
218 face2->sendData (*data2 );
219 face2->sendData (*data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700220 size_t nBytesSent2 = interest2->wireEncode().size() + data2->wireEncode().size() * 3;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100221
Davide Pesavento292e5e12015-03-13 02:08:33 +0100222 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100223
Junxiao Shi79494162014-04-02 18:25:11 -0700224 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
225 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
226 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
227 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100228
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700229 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
230 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
231 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
232 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000233
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700234 // needed to ensure NOutBytes counters are accurate
235 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
236
Junxiao Shi79494162014-04-02 18:25:11 -0700237 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700238 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
239 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
240 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
241 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700242 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
243 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000244
Junxiao Shi79494162014-04-02 18:25:11 -0700245 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700246 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
247 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
248 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
249 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100250}
251
252BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
253{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800254 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100255
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700256 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100257 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
258 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
259
Davide Pesavento292e5e12015-03-13 02:08:33 +0100260 UnixStreamFace::protocol::socket client1(g_io);
261 client1.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
262 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100263
Davide Pesavento292e5e12015-03-13 02:08:33 +0100264 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "First connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100265
Junxiao Shi79494162014-04-02 18:25:11 -0700266 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100267
Davide Pesavento292e5e12015-03-13 02:08:33 +0100268 UnixStreamFace::protocol::socket client2(g_io);
269 client2.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
270 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100271
Davide Pesavento292e5e12015-03-13 02:08:33 +0100272 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Second connect");
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
Davide Pesavento292e5e12015-03-13 02:08:33 +0100284 face2 = makeFace(std::move(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
Davide Pesavento292e5e12015-03-13 02:08:33 +0100298 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100299
Junxiao Shi79494162014-04-02 18:25:11 -0700300 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
301 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
302 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
303 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100304
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700305 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
306 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
307 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
308 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100309}
310
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800311BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
312{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800313 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800314
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700315 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800316 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
317 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
318
Davide Pesavento292e5e12015-03-13 02:08:33 +0100319 UnixStreamFace::protocol::socket client(g_io);
320 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
321 bind(&EndToEndFixture::client_onConnect, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800322
Davide Pesavento292e5e12015-03-13 02:08:33 +0100323 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800324
Junxiao Shi79494162014-04-02 18:25:11 -0700325 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800326
Davide Pesavento292e5e12015-03-13 02:08:33 +0100327 face2 = makeFace(std::move(client));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700328 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
329 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800330
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700331 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
332 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800333
Junxiao Shi79494162014-04-02 18:25:11 -0700334 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
335 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800336
Junxiao Shi79494162014-04-02 18:25:11 -0700337 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
338 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800339
Junxiao Shi79494162014-04-02 18:25:11 -0700340 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
341 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800342
Junxiao Shi79494162014-04-02 18:25:11 -0700343 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
344 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800345
346 ////////////////////////////////////////////////////////
347
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700348 interest1->setIncomingFaceId(11);
349 interest1->setNextHopFaceId(111);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700350 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800351
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700352 data1->setIncomingFaceId(22);
353 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700354 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800355
Junxiao Shi79494162014-04-02 18:25:11 -0700356 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100357 "Regular send/receive");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800358
Junxiao Shi79494162014-04-02 18:25:11 -0700359 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
360 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800361
362 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700363 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
364 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800365
Junxiao Shi79494162014-04-02 18:25:11 -0700366 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
367 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800368
Davide Pesavento292e5e12015-03-13 02:08:33 +0100369 face1->close();
370 face1.reset();
371
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800372 ////////////////////////////////////////////////////////
373
Davide Pesavento292e5e12015-03-13 02:08:33 +0100374 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
375 bind(&EndToEndFixture::client_onConnect, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800376
Davide Pesavento292e5e12015-03-13 02:08:33 +0100377 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
378
379 BOOST_REQUIRE(static_cast<bool>(face1));
380 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
381 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
382
383 Block iHeader = interest1->getLocalControlHeader()
384 .wireEncode(*interest1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
385 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700386 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800387
Davide Pesavento292e5e12015-03-13 02:08:33 +0100388 Block dHeader = data1->getLocalControlHeader()
389 .wireEncode(*data1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
390 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700391 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800392
Davide Pesavento292e5e12015-03-13 02:08:33 +0100393 client.async_send(std::vector<boost::asio::const_buffer>{iHeader, iPayload},
394 [] (const boost::system::error_code& error, size_t nBytesSent) {
395 BOOST_CHECK_MESSAGE(!error, error.message());
396 });
397 client.async_send(std::vector<boost::asio::const_buffer>{dHeader, dPayload},
398 [] (const boost::system::error_code& error, size_t nBytesSent) {
399 BOOST_CHECK_MESSAGE(!error, error.message());
400 });
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800401
Junxiao Shi79494162014-04-02 18:25:11 -0700402 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100403 "Send/receive with LocalControlHeader");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800404
Junxiao Shi79494162014-04-02 18:25:11 -0700405 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
406 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800407
Junxiao Shi79494162014-04-02 18:25:11 -0700408 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
409 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
410 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800411
Junxiao Shi79494162014-04-02 18:25:11 -0700412 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
413 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800414}
415
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700416
417class SimpleEndToEndFixture : protected BaseFixture
418{
419public:
420 void
421 onFaceCreated(const shared_ptr<Face>& face)
422 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700423 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
424 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
425 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700426
427 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
428 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
429 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
430
431 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
432 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
433 }
434
435 limitedIo.afterOp();
436 }
437
438 void
439 onConnectFailed(const std::string& reason)
440 {
441 BOOST_CHECK_MESSAGE(false, reason);
442
443 limitedIo.afterOp();
444 }
445
446 void
447 onReceiveInterest(const Interest& interest)
448 {
449 receivedInterests.push_back(interest);
450
451 limitedIo.afterOp();
452 }
453
454 void
455 onReceiveData(const Data& data)
456 {
457 receivedDatas.push_back(data);
458
459 limitedIo.afterOp();
460 }
461
462 void
463 onFail(const shared_ptr<Face>& face)
464 {
465 limitedIo.afterOp();
466 }
467
468public:
469 LimitedIo limitedIo;
470
471 std::vector<Interest> receivedInterests;
472 std::vector<Data> receivedDatas;
473};
474
475
476BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
477 CorruptedPackets, SimpleEndToEndFixture)
478{
479 UnixStreamFactory factory;
480
481 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
482 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
483 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
484
Davide Pesavento292e5e12015-03-13 02:08:33 +0100485 DummyStreamSender<UnixStreamFace::protocol, Dataset> sender;
486 sender.start(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700487
Davide Pesavento292e5e12015-03-13 02:08:33 +0100488 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)) == LimitedIo::EXCEED_TIME,
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700489 "Exception thrown for " + Dataset::getName());
490}
491
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100492BOOST_AUTO_TEST_SUITE_END()
493
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700494} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100495} // namespace nfd