blob: 47610af4b981200de7c1e24ab83f218b735c74dd [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"
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080027#include "face/unix-stream-factory.hpp"
Yukai Tu74c895d2015-09-21 01:11:51 -070028#include "face/unix-stream-transport.hpp"
29
30#include "face/generic-link-service.hpp"
31#include "face/lp-face-wrapper.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010032
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070034#include "tests/limited-io.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070035#include "dummy-stream-sender.hpp"
36#include "packet-datasets.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070037
38namespace nfd {
39namespace tests {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010040
Junxiao Shi61e3cc52014-03-03 20:40:28 -070041#define CHANNEL_PATH1 "unix-stream-test.1.sock"
42#define CHANNEL_PATH2 "unix-stream-test.2.sock"
43
Yukai Tu74c895d2015-09-21 01:11:51 -070044BOOST_AUTO_TEST_SUITE(Face)
45BOOST_FIXTURE_TEST_SUITE(TestUnixStream, BaseFixture)
46
47using nfd::Face;
48using face::LpFaceWrapper;
49using face::UnixStreamTransport;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010050
51BOOST_AUTO_TEST_CASE(ChannelMap)
52{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080053 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010054
Junxiao Shi61e3cc52014-03-03 20:40:28 -070055 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
56 shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010057 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070058 std::string channel1uri = channel1->getUri().toString();
59 BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
60 BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
61 channel1uri.size() - std::string(CHANNEL_PATH1).size());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010062
Junxiao Shi61e3cc52014-03-03 20:40:28 -070063 shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010064 BOOST_CHECK_NE(channel1, channel2);
65}
66
Steve DiBenedettoef04f272014-06-04 14:28:31 -060067BOOST_AUTO_TEST_CASE(GetChannels)
68{
69 UnixStreamFactory factory;
Davide Pesavento292e5e12015-03-13 02:08:33 +010070 BOOST_CHECK(factory.getChannels().empty());
Steve DiBenedettoef04f272014-06-04 14:28:31 -060071
Davide Pesavento292e5e12015-03-13 02:08:33 +010072 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060073 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH1));
74 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH2));
75
Davide Pesavento292e5e12015-03-13 02:08:33 +010076 for (const auto& channel : factory.getChannels()) {
77 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), channel);
78 BOOST_REQUIRE(pos != expectedChannels.end());
79 expectedChannels.erase(pos);
80 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060081
82 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
83}
84
Yukai Tu7c90e6d2015-07-11 12:21:46 +080085BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
86{
87 UnixStreamFactory factory;
88
89 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
90 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
91 bind([]{}),
92 bind([]{})),
93 ProtocolFactory::Error);
94
95 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
96 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
97 bind([]{}),
98 bind([]{})),
99 ProtocolFactory::Error);
100
101 BOOST_CHECK_THROW(factory.createFace(FaceUri("unix:///var/run/nfd.sock"),
102 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
103 bind([]{}),
104 bind([]{})),
105 ProtocolFactory::Error);
106}
107
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700108class EndToEndFixture : protected BaseFixture
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100109{
110public:
111 void
112 client_onConnect(const boost::system::error_code& error)
113 {
114 BOOST_CHECK_MESSAGE(!error, error.message());
115
Junxiao Shi79494162014-04-02 18:25:11 -0700116 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100117 }
118
119 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700120 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100121 {
Junxiao Shi79494162014-04-02 18:25:11 -0700122 BOOST_CHECK(!static_cast<bool>(face1));
Yukai Tu74c895d2015-09-21 01:11:51 -0700123 face1 = static_pointer_cast<LpFaceWrapper>(newFace);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700124 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
125 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100126
Junxiao Shi79494162014-04-02 18:25:11 -0700127 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100128 }
129
130 void
131 channel1_onConnectFailed(const std::string& reason)
132 {
133 BOOST_CHECK_MESSAGE(false, reason);
134
Junxiao Shi79494162014-04-02 18:25:11 -0700135 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100136 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800137
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100138 void
139 face1_onReceiveInterest(const Interest& interest)
140 {
Junxiao Shi79494162014-04-02 18:25:11 -0700141 face1_receivedInterests.push_back(interest);
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 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800145
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100146 void
147 face1_onReceiveData(const Data& data)
148 {
Junxiao Shi79494162014-04-02 18:25:11 -0700149 face1_receivedDatas.push_back(data);
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 }
153
154 void
155 face2_onReceiveInterest(const Interest& interest)
156 {
Junxiao Shi79494162014-04-02 18:25:11 -0700157 face2_receivedInterests.push_back(interest);
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 }
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800161
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100162 void
163 face2_onReceiveData(const Data& data)
164 {
Junxiao Shi79494162014-04-02 18:25:11 -0700165 face2_receivedDatas.push_back(data);
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
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700171 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100172 {
Yukai Tu74c895d2015-09-21 01:11:51 -0700173 faces.push_back(static_pointer_cast<LpFaceWrapper>(newFace));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100174
Junxiao Shi79494162014-04-02 18:25:11 -0700175 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100176 }
177
178 void
179 channel_onConnectFailed(const std::string& reason)
180 {
181 BOOST_CHECK_MESSAGE(false, reason);
182
Junxiao Shi79494162014-04-02 18:25:11 -0700183 limitedIo.afterOp();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100184 }
185
Yukai Tu74c895d2015-09-21 01:11:51 -0700186 shared_ptr<LpFaceWrapper>
187 makeFace(UnixStreamTransport::protocol::socket&& socket)
Davide Pesavento292e5e12015-03-13 02:08:33 +0100188 {
Yukai Tu74c895d2015-09-21 01:11:51 -0700189 auto linkService = make_unique<face::GenericLinkService>();
190 auto transport = make_unique<UnixStreamTransport>(std::move(socket));
191 auto lpFace = make_unique<LpFace>(std::move(linkService), std::move(transport));
192 return make_shared<LpFaceWrapper>(std::move(lpFace));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100193 }
194
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100195protected:
Junxiao Shi79494162014-04-02 18:25:11 -0700196 LimitedIo limitedIo;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100197
Yukai Tu74c895d2015-09-21 01:11:51 -0700198 shared_ptr<LpFaceWrapper> face1;
Junxiao Shi79494162014-04-02 18:25:11 -0700199 std::vector<Interest> face1_receivedInterests;
200 std::vector<Data> face1_receivedDatas;
Yukai Tu74c895d2015-09-21 01:11:51 -0700201 shared_ptr<LpFaceWrapper> face2;
Junxiao Shi79494162014-04-02 18:25:11 -0700202 std::vector<Interest> face2_receivedInterests;
203 std::vector<Data> face2_receivedDatas;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100204
Yukai Tu74c895d2015-09-21 01:11:51 -0700205 std::list<shared_ptr<LpFaceWrapper>> faces;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100206};
207
208
209BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
210{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800211 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100212
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700213 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100214 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
215 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
216
Yukai Tu74c895d2015-09-21 01:11:51 -0700217 UnixStreamTransport::protocol::socket client(g_io);
218 client.async_connect(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1),
Davide Pesavento292e5e12015-03-13 02:08:33 +0100219 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100220
Davide Pesavento292e5e12015-03-13 02:08:33 +0100221 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100222
Junxiao Shi79494162014-04-02 18:25:11 -0700223 BOOST_REQUIRE(static_cast<bool>(face1));
Davide Pesavento94279412015-02-27 01:29:32 +0100224 BOOST_CHECK_EQUAL(face1->isLocal(), true);
Yukai Tu731f0d72015-07-04 11:14:44 +0800225 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesavento94279412015-02-27 01:29:32 +0100226 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700227 BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd");
Davide Pesavento292e5e12015-03-13 02:08:33 +0100228 BOOST_CHECK_NO_THROW(std::stoi(face1->getRemoteUri().getHost()));
Junxiao Shi79494162014-04-02 18:25:11 -0700229 std::string face1localUri = face1->getLocalUri().toString();
230 BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator
231 BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1),
232 face1localUri.size() - std::string(CHANNEL_PATH1).size());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000233
Davide Pesavento292e5e12015-03-13 02:08:33 +0100234 face2 = makeFace(std::move(client));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700235 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
236 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100237
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700238 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
239 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
240 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
241 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100242
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700243 face1->sendInterest(*interest1);
244 face1->sendInterest(*interest1);
245 face1->sendInterest(*interest1);
246 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700247 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700248 face2->sendInterest(*interest2);
249 face2->sendData (*data2 );
250 face2->sendData (*data2 );
251 face2->sendData (*data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700252 size_t nBytesSent2 = interest2->wireEncode().size() + data2->wireEncode().size() * 3;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100253
Davide Pesavento292e5e12015-03-13 02:08:33 +0100254 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100255
Junxiao Shi79494162014-04-02 18:25:11 -0700256 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
257 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
258 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
259 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100260
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700261 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
262 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
263 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
264 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000265
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700266 // needed to ensure NOutBytes counters are accurate
267 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
268
Junxiao Shida93f1f2015-11-11 06:13:16 -0700269 const face::FaceCounters& counters1 = face1->getCounters();
270 BOOST_CHECK_EQUAL(counters1.nInInterests, 1);
271 BOOST_CHECK_EQUAL(counters1.nInData, 3);
272 BOOST_CHECK_EQUAL(counters1.nOutInterests, 3);
273 BOOST_CHECK_EQUAL(counters1.nOutData, 1);
274 BOOST_CHECK_EQUAL(counters1.nInBytes, nBytesSent2);
275 BOOST_CHECK_EQUAL(counters1.nOutBytes, nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000276
Junxiao Shida93f1f2015-11-11 06:13:16 -0700277 const face::FaceCounters& counters2 = face2->getCounters();
278 BOOST_CHECK_EQUAL(counters2.nInInterests, 3);
279 BOOST_CHECK_EQUAL(counters2.nInData, 1);
280 BOOST_CHECK_EQUAL(counters2.nOutInterests, 1);
281 BOOST_CHECK_EQUAL(counters2.nOutData, 3);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100282}
283
284BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
285{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800286 UnixStreamFactory factory;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100287
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700288 shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100289 channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
290 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
291
Yukai Tu74c895d2015-09-21 01:11:51 -0700292 UnixStreamTransport::protocol::socket client1(g_io);
293 client1.async_connect(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1),
Davide Pesavento292e5e12015-03-13 02:08:33 +0100294 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100295
Davide Pesavento292e5e12015-03-13 02:08:33 +0100296 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "First connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100297
Junxiao Shi79494162014-04-02 18:25:11 -0700298 BOOST_CHECK_EQUAL(faces.size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100299
Yukai Tu74c895d2015-09-21 01:11:51 -0700300 UnixStreamTransport::protocol::socket client2(g_io);
301 client2.async_connect(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1),
Davide Pesavento292e5e12015-03-13 02:08:33 +0100302 bind(&EndToEndFixture::client_onConnect, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100303
Davide Pesavento292e5e12015-03-13 02:08:33 +0100304 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Second connect");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100305
Junxiao Shi79494162014-04-02 18:25:11 -0700306 BOOST_CHECK_EQUAL(faces.size(), 2);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100307
308 // now close one of the faces
Junxiao Shi79494162014-04-02 18:25:11 -0700309 faces.front()->close();
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100310
311 // we should still be able to send/receive with the other one
Junxiao Shi79494162014-04-02 18:25:11 -0700312 face1 = faces.back();
Junxiao Shic099ddb2014-12-25 20:53:20 -0700313 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
314 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100315
Davide Pesavento292e5e12015-03-13 02:08:33 +0100316 face2 = makeFace(std::move(client2));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700317 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
318 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100319
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700320 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
321 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
322 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
323 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100324
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700325 face1->sendInterest(*interest1);
326 face1->sendData (*data1 );
327 face2->sendInterest(*interest2);
328 face2->sendData (*data2 );
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100329
Davide Pesavento292e5e12015-03-13 02:08:33 +0100330 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Send/receive");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100331
Junxiao Shi79494162014-04-02 18:25:11 -0700332 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
333 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
334 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
335 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100336
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700337 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
338 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
339 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
340 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100341}
342
Yukai Tu74c895d2015-09-21 01:11:51 -0700343//BOOST_FIXTURE_TEST_CASE(UnixStreamTransportLocalControlHeader, EndToEndFixture)
344//{
345// UnixStreamFactory factory;
346//
347// shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
348// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
349// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
350//
351// UnixStreamTransport::protocol::socket client(g_io);
352// client.async_connect(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1),
353// bind(&EndToEndFixture::client_onConnect, this, _1));
354//
355// BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
356//
357// BOOST_REQUIRE(static_cast<bool>(face1));
358//
359// face2 = makeFace(std::move(client));
360// face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
361// face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
362//
363// shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
364// shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800365
Yukai Tu74c895d2015-09-21 01:11:51 -0700366// face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
367// face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
368//
369// BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
370// BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800371
Yukai Tu74c895d2015-09-21 01:11:51 -0700372// face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
373// face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800374
Yukai Tu74c895d2015-09-21 01:11:51 -0700375// BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
376// BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800377
378 ////////////////////////////////////////////////////////
379
Yukai Tu74c895d2015-09-21 01:11:51 -0700380// interest1->setIncomingFaceId(11);
381// interest1->setNextHopFaceId(111);
382// face1->sendInterest(*interest1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800383
Yukai Tu74c895d2015-09-21 01:11:51 -0700384// data1->setIncomingFaceId(22);
385// data1->getLocalControlHeader().setNextHopFaceId(222);
386// face1->sendData(*data1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800387
Yukai Tu74c895d2015-09-21 01:11:51 -0700388// BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
389// "Regular send/receive");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800390
Yukai Tu74c895d2015-09-21 01:11:51 -0700391// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
392// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800393
Yukai Tu74c895d2015-09-21 01:11:51 -0700394// sending allows only IncomingFaceId, receiving allows only NextHopFaceId
395// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
396// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800397
Yukai Tu74c895d2015-09-21 01:11:51 -0700398// BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
399// BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800400
Yukai Tu74c895d2015-09-21 01:11:51 -0700401// face1->close();
402// face1.reset();
Davide Pesavento292e5e12015-03-13 02:08:33 +0100403
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800404 ////////////////////////////////////////////////////////
405
Yukai Tu74c895d2015-09-21 01:11:51 -0700406// client.async_connect(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1),
407// bind(&EndToEndFixture::client_onConnect, this, _1));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800408
Yukai Tu74c895d2015-09-21 01:11:51 -0700409// BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, "Connect");
Davide Pesavento292e5e12015-03-13 02:08:33 +0100410
Yukai Tu74c895d2015-09-21 01:11:51 -0700411// BOOST_REQUIRE(static_cast<bool>(face1));
412// face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
413// face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100414
Yukai Tu74c895d2015-09-21 01:11:51 -0700415// Block iHeader = interest1->getLocalControlHeader()
416// .wireEncode(*interest1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
417// ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
418// Block iPayload = interest1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800419
Yukai Tu74c895d2015-09-21 01:11:51 -0700420// Block dHeader = data1->getLocalControlHeader()
421// .wireEncode(*data1, ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
422// ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP);
423// Block dPayload = data1->wireEncode();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800424
Yukai Tu74c895d2015-09-21 01:11:51 -0700425// client.async_send(std::vector<boost::asio::const_buffer>{iHeader, iPayload},
426// [] (const boost::system::error_code& error, size_t nBytesSent) {
427// BOOST_CHECK_MESSAGE(!error, error.message());
428// });
429// client.async_send(std::vector<boost::asio::const_buffer>{dHeader, dPayload},
430// [] (const boost::system::error_code& error, size_t nBytesSent) {
431// BOOST_CHECK_MESSAGE(!error, error.message());
432// });
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800433
Yukai Tu74c895d2015-09-21 01:11:51 -0700434// BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
435// "Send/receive with LocalControlHeader");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800436
Yukai Tu74c895d2015-09-21 01:11:51 -0700437// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
438// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800439
Yukai Tu74c895d2015-09-21 01:11:51 -0700440// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
441// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
442// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800443
Yukai Tu74c895d2015-09-21 01:11:51 -0700444// BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
445// BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
446//}
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800447
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700448
Yukai Tu74c895d2015-09-21 01:11:51 -0700449//class SimpleEndToEndFixture : protected BaseFixture
450//{
451//public:
452// void
453// onFaceCreated(const shared_ptr<Face>& face)
454// {
455// face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
456// face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
457// face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700458
Yukai Tu74c895d2015-09-21 01:11:51 -0700459// if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
460// static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
461// LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700462
Yukai Tu74c895d2015-09-21 01:11:51 -0700463// static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
464// LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
465// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700466
Yukai Tu74c895d2015-09-21 01:11:51 -0700467// limitedIo.afterOp();
468// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700469
Yukai Tu74c895d2015-09-21 01:11:51 -0700470// void
471// onConnectFailed(const std::string& reason)
472// {
473// BOOST_CHECK_MESSAGE(false, reason);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700474
Yukai Tu74c895d2015-09-21 01:11:51 -0700475// limitedIo.afterOp();
476// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700477
Yukai Tu74c895d2015-09-21 01:11:51 -0700478// void
479// onReceiveInterest(const Interest& interest)
480// {
481// receivedInterests.push_back(interest);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700482
Yukai Tu74c895d2015-09-21 01:11:51 -0700483// limitedIo.afterOp();
484// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700485
Yukai Tu74c895d2015-09-21 01:11:51 -0700486// void
487// onReceiveData(const Data& data)
488// {
489// receivedDatas.push_back(data);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700490
Yukai Tu74c895d2015-09-21 01:11:51 -0700491// limitedIo.afterOp();
492// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700493
Yukai Tu74c895d2015-09-21 01:11:51 -0700494// void
495// onFail(const shared_ptr<Face>& face)
496// {
497// limitedIo.afterOp();
498// }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700499
Yukai Tu74c895d2015-09-21 01:11:51 -0700500//public:
501// LimitedIo limitedIo;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700502
Yukai Tu74c895d2015-09-21 01:11:51 -0700503// std::vector<Interest> receivedInterests;
504// std::vector<Data> receivedDatas;
505//};
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700506
507
Yukai Tu74c895d2015-09-21 01:11:51 -0700508//BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset,
509// CorruptedPackets, SimpleEndToEndFixture)
510//{
511// UnixStreamFactory factory;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700512
Yukai Tu74c895d2015-09-21 01:11:51 -0700513// shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
514// channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
515// bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700516
Yukai Tu74c895d2015-09-21 01:11:51 -0700517// DummyStreamSender<UnixStreamTransport::protocol, Dataset> sender;
518// sender.start(UnixStreamTransport::protocol::endpoint(CHANNEL_PATH1));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700519
Yukai Tu74c895d2015-09-21 01:11:51 -0700520// BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1)) == LimitedIo::EXCEED_TIME,
521// "Exception thrown for " + Dataset::getName());
522//}
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700523
Yukai Tu74c895d2015-09-21 01:11:51 -0700524BOOST_AUTO_TEST_SUITE_END() // TestUnixStream
525BOOST_AUTO_TEST_SUITE_END() // Face
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100526
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700527} // namespace tests
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100528} // namespace nfd