blob: c5f52217c7421fe899d7520c5ff2e97f50a32bc5 [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
Yukai Tu7c90e6d2015-07-11 12:21:46 +080077BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
78{
79 UnixStreamFactory factory;
80
81 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
82 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
83 bind([]{}),
84 bind([]{})),
85 ProtocolFactory::Error);
86
87 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
88 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
89 bind([]{}),
90 bind([]{})),
91 ProtocolFactory::Error);
92
93 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
94 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
95 bind([]{}),
96 bind([]{})),
97 ProtocolFactory::Error);
98}
99
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700100class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100101{
102public:
103 void
104 client_onConnect(const boost::system::error_code& error)
105 {
106 BOOST_CHECK_MESSAGE(!error, error.message());
107
Junxiao Shi79494162014-04-02 18:25:11 -0700108 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100109 }
110
111 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700112 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100113 {
Junxiao Shi79494162014-04-02 18:25:11 -0700114 BOOST_CHECK(!static_cast<bool>(face1));
115 face1 = static_pointer_cast<UnixStreamFace>(newFace);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700116 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
117 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
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
123 channel1_onConnectFailed(const std::string& reason)
124 {
125 BOOST_CHECK_MESSAGE(false, reason);
126
Junxiao Shi79494162014-04-02 18:25:11 -0700127 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100128 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800129
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100130 void
131 face1_onReceiveInterest(const Interest& interest)
132 {
Junxiao Shi79494162014-04-02 18:25:11 -0700133 face1_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100134
Junxiao Shi79494162014-04-02 18:25:11 -0700135 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100136 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800137
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100138 void
139 face1_onReceiveData(const Data& data)
140 {
Junxiao Shi79494162014-04-02 18:25:11 -0700141 face1_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100142
Junxiao Shi79494162014-04-02 18:25:11 -0700143 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100144 }
145
146 void
147 face2_onReceiveInterest(const Interest& interest)
148 {
Junxiao Shi79494162014-04-02 18:25:11 -0700149 face2_receivedInterests.push_back(interest);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100150
Junxiao Shi79494162014-04-02 18:25:11 -0700151 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100152 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800153
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100154 void
155 face2_onReceiveData(const Data& data)
156 {
Junxiao Shi79494162014-04-02 18:25:11 -0700157 face2_receivedDatas.push_back(data);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100158
Junxiao Shi79494162014-04-02 18:25:11 -0700159 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100160 }
161
162 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700163 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100164 {
Junxiao Shi79494162014-04-02 18:25:11 -0700165 faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100166
Junxiao Shi79494162014-04-02 18:25:11 -0700167 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100168 }
169
170 void
171 channel_onConnectFailed(const std::string& reason)
172 {
173 BOOST_CHECK_MESSAGE(false, reason);
174
Junxiao Shi79494162014-04-02 18:25:11 -0700175 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100176 }
177
Davide Pesavento292e5e12015-03-13 02:08:33 +0100178 shared_ptr<UnixStreamFace>
179 makeFace(UnixStreamFace::protocol::socket socket)
180 {
181 auto remoteUri = FaceUri::fromFd(socket.native_handle());
182 auto localUri = FaceUri(socket.local_endpoint());
183 return make_shared<UnixStreamFace>(remoteUri, localUri, std::move(socket));
184 }
185
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100186protected:
Junxiao Shi79494162014-04-02 18:25:11 -0700187 LimitedIo limitedIo;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100188
Junxiao Shi79494162014-04-02 18:25:11 -0700189 shared_ptr<UnixStreamFace> face1;
190 std::vector<Interest> face1_receivedInterests;
191 std::vector<Data> face1_receivedDatas;
192 shared_ptr<UnixStreamFace> face2;
193 std::vector<Interest> face2_receivedInterests;
194 std::vector<Data> face2_receivedDatas;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100195
Davide Pesavento292e5e12015-03-13 02:08:33 +0100196 std::list<shared_ptr<UnixStreamFace>> faces;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100197};
198
199
200BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
201{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800202 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100203
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700204 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100205 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
206 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
207
Davide Pesavento292e5e12015-03-13 02:08:33 +0100208 UnixStreamFace::protocol::socket client(g_io);
209 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
210 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100211
Davide Pesavento292e5e12015-03-13 02:08:33 +0100212 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100213
Junxiao Shi79494162014-04-02 18:25:11 -0700214 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesavento94279412015-02-27 01:29:32 +0100215 BOOST_CHECK_EQUAL(face1->isLocal(), true);
Yukai Tu731f0d72015-07-04 11:14:44 +0800216 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesavento94279412015-02-27 01:29:32 +0100217 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700218 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
Davide Pesavento292e5e12015-03-13 02:08:33 +0100219 BOOST_CHECK_NO_THROW(std::stoi(face1->getRemoteUri().getHost()));
Junxiao Shi79494162014-04-02 18:25:11 -0700220 std::string face1localUri = face1->getLocalUri().toString();
221 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
222 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
223 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000224
Davide Pesavento292e5e12015-03-13 02:08:33 +0100225 face2 = makeFace(std::move(client));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700226 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
227 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100228
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700229 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
230 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
231 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
232 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100233
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700234 face1->sendInterest(*interest1);
235 face1->sendInterest(*interest1);
236 face1->sendInterest(*interest1);
237 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700238 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700239 face2->sendInterest(*interest2);
240 face2->sendData (*data2 );
241 face2->sendData (*data2 );
242 face2->sendData (*data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700243 size_t nBytesSent2 = interest2->wireEncode().size() + data2->wireEncode().size() * 3;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100244
Davide Pesavento292e5e12015-03-13 02:08:33 +0100245 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100246
Junxiao Shi79494162014-04-02 18:25:11 -0700247 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
248 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
249 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
250 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100251
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700252 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
253 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
254 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
255 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000256
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700257 // needed to ensure NOutBytes counters are accurate
258 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
259
Junxiao Shi79494162014-04-02 18:25:11 -0700260 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700261 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
262 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
263 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
264 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700265 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
266 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000267
Junxiao Shi79494162014-04-02 18:25:11 -0700268 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700269 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
270 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
271 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
272 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100273}
274
275BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
276{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800277 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100278
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700279 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100280 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
281 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
282
Davide Pesavento292e5e12015-03-13 02:08:33 +0100283 UnixStreamFace::protocol::socket client1(g_io);
284 client1.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
285 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100286
Davide Pesavento292e5e12015-03-13 02:08:33 +0100287 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "First connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100288
Junxiao Shi79494162014-04-02 18:25:11 -0700289 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100290
Davide Pesavento292e5e12015-03-13 02:08:33 +0100291 UnixStreamFace::protocol::socket client2(g_io);
292 client2.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
293 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100294
Davide Pesavento292e5e12015-03-13 02:08:33 +0100295 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Second connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100296
Junxiao Shi79494162014-04-02 18:25:11 -0700297 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100298
299 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700300 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100301
302 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700303 face1 = faces.back();
Junxiao Shic099ddb2014-12-25 20:53:20 -0700304 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
305 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100306
Davide Pesavento292e5e12015-03-13 02:08:33 +0100307 face2 = makeFace(std::move(client2));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700308 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
309 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100310
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700311 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
312 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
313 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
314 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100315
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700316 face1->sendInterest(*interest1);
317 face1->sendData (*data1 );
318 face2->sendInterest(*interest2);
319 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100320
Davide Pesavento292e5e12015-03-13 02:08:33 +0100321 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100322
Junxiao Shi79494162014-04-02 18:25:11 -0700323 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
324 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
325 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
326 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100327
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700328 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
329 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
330 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
331 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100332}
333
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800334BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
335{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800336 UnixStreamFactory factory;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800337
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700338 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800339 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
340 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
341
Davide Pesavento292e5e12015-03-13 02:08:33 +0100342 UnixStreamFace::protocol::socket client(g_io);
343 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
344 bind(&EndToEndFixture::client_onConnect, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800345
Davide Pesavento292e5e12015-03-13 02:08:33 +0100346 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800347
Junxiao Shi79494162014-04-02 18:25:11 -0700348 BOOST_REQUIRE(static_cast<bool>(face1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800349
Davide Pesavento292e5e12015-03-13 02:08:33 +0100350 face2 = makeFace(std::move(client));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700351 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
352 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800353
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700354 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
355 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800356
Junxiao Shi79494162014-04-02 18:25:11 -0700357 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
358 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800359
Junxiao Shi79494162014-04-02 18:25:11 -0700360 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
361 BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800362
Junxiao Shi79494162014-04-02 18:25:11 -0700363 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
364 face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800365
Junxiao Shi79494162014-04-02 18:25:11 -0700366 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
367 BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800368
369 ////////////////////////////////////////////////////////
370
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700371 interest1->setIncomingFaceId(11);
372 interest1->setNextHopFaceId(111);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700373 face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800374
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700375 data1->setIncomingFaceId(22);
376 data1->getLocalControlHeader().setNextHopFaceId(222);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700377 face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800378
Junxiao Shi79494162014-04-02 18:25:11 -0700379 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100380 "Regular send/receive");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800381
Junxiao Shi79494162014-04-02 18:25:11 -0700382 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
383 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800384
385 // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
Junxiao Shi79494162014-04-02 18:25:11 -0700386 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
387 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800388
Junxiao Shi79494162014-04-02 18:25:11 -0700389 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
390 BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800391
Davide Pesavento292e5e12015-03-13 02:08:33 +0100392 face1->close();
393 face1.reset();
394
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800395 ////////////////////////////////////////////////////////
396
Davide Pesavento292e5e12015-03-13 02:08:33 +0100397 client.async_connect(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1),
398 bind(&EndToEndFixture::client_onConnect, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800399
Davide Pesavento292e5e12015-03-13 02:08:33 +0100400 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
401
402 BOOST_REQUIRE(static_cast<bool>(face1));
403 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
404 face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
405
406 Block iHeader = interest1->getLocalControlHeader()
407 .wireEncode(*interest1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
408 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700409 Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800410
Davide Pesavento292e5e12015-03-13 02:08:33 +0100411 Block dHeader = data1->getLocalControlHeader()
412 .wireEncode(*data1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
413 ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700414 Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800415
Davide Pesavento292e5e12015-03-13 02:08:33 +0100416 client.async_send(std::vector<boost::asio::const_buffer>{iHeader, iPayload},
417 [] (const boost::system::error_code& error, size_t nBytesSent) {
418 BOOST_CHECK_MESSAGE(!error, error.message());
419 });
420 client.async_send(std::vector<boost::asio::const_buffer>{dHeader, dPayload},
421 [] (const boost::system::error_code& error, size_t nBytesSent) {
422 BOOST_CHECK_MESSAGE(!error, error.message());
423 });
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800424
Junxiao Shi79494162014-04-02 18:25:11 -0700425 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100426 "Send/receive with LocalControlHeader");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800427
Junxiao Shi79494162014-04-02 18:25:11 -0700428 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
429 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800430
Junxiao Shi79494162014-04-02 18:25:11 -0700431 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
432 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
433 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800434
Junxiao Shi79494162014-04-02 18:25:11 -0700435 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
436 BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800437}
438
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700439
440class SimpleEndToEndFixture : protected BaseFixture
441{
442public:
443 void
444 onFaceCreated(const shared_ptr<Face>& face)
445 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700446 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
447 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
448 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700449
450 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
451 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
452 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
453
454 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
455 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
456 }
457
458 limitedIo.afterOp();
459 }
460
461 void
462 onConnectFailed(const std::string& reason)
463 {
464 BOOST_CHECK_MESSAGE(false, reason);
465
466 limitedIo.afterOp();
467 }
468
469 void
470 onReceiveInterest(const Interest& interest)
471 {
472 receivedInterests.push_back(interest);
473
474 limitedIo.afterOp();
475 }
476
477 void
478 onReceiveData(const Data& data)
479 {
480 receivedDatas.push_back(data);
481
482 limitedIo.afterOp();
483 }
484
485 void
486 onFail(const shared_ptr<Face>& face)
487 {
488 limitedIo.afterOp();
489 }
490
491public:
492 LimitedIo limitedIo;
493
494 std::vector<Interest> receivedInterests;
495 std::vector<Data> receivedDatas;
496};
497
498
499BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
500 CorruptedPackets, SimpleEndToEndFixture)
501{
502 UnixStreamFactory factory;
503
504 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
505 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
506 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
507
Davide Pesavento292e5e12015-03-13 02:08:33 +0100508 DummyStreamSender<UnixStreamFace::protocol, Dataset> sender;
509 sender.start(UnixStreamFace::protocol::endpoint(CHANNEL_PATH1));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700510
Davide Pesavento292e5e12015-03-13 02:08:33 +0100511 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)) == LimitedIo::EXCEED_TIME,
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700512 "Exception thrown for " + Dataset::getName());
513}
514
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100515BOOST_AUTO_TEST_SUITE_END()
516
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700517} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100518} // namespace nfd