blob: 2f85e37863fec04ba85275776ace7d9e0db58ffe [file] [log] [blame]
Junxiao Shi96dc0c42014-01-30 23:51:59 -07001/* -*- 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 */
Junxiao Shi96dc0c42014-01-30 23:51:59 -070025
Davide Pesavento6ad890a2015-03-09 03:43:17 +010026#include "face/tcp-channel.hpp"
27#include "face/tcp-face.hpp"
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080028#include "face/tcp-factory.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070029
Davide Pesavento6ad890a2015-03-09 03:43:17 +010030#include "core/network-interface.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070032#include "tests/limited-io.hpp"
Alexander Afanasyev650028d2014-04-25 18:39:10 -070033#include "dummy-stream-sender.hpp"
34#include "packet-datasets.hpp"
Junxiao Shi96dc0c42014-01-30 23:51:59 -070035
Davide Pesavento6ad890a2015-03-09 03:43:17 +010036#include <ndn-cxx/security/key-chain.hpp>
Davide Pesavento6ad890a2015-03-09 03:43:17 +010037
Junxiao Shi96dc0c42014-01-30 23:51:59 -070038namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070040
Junxiao Shid9ee45c2014-02-27 15:38:11 -070041BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070042
43BOOST_AUTO_TEST_CASE(ChannelMap)
44{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080045 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080046
Alexander Afanasyevd6655302014-02-28 08:41:28 -080047 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
48 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070049 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080051
Alexander Afanasyevd6655302014-02-28 08:41:28 -080052 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070053 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070054
55 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
56 BOOST_CHECK_NE(channel2, channel3);
57 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070058}
59
Steve DiBenedettoef04f272014-06-04 14:28:31 -060060BOOST_AUTO_TEST_CASE(GetChannels)
61{
62 TcpFactory factory;
63 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
64
Davide Pesaventob499a602014-11-18 22:36:56 +010065 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060066 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
67 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
68 expectedChannels.push_back(factory.createChannel("::1", "20071"));
69
Davide Pesaventob499a602014-11-18 22:36:56 +010070 for (const auto& ch : factory.getChannels()) {
71 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
72 BOOST_REQUIRE(pos != expectedChannels.end());
73 expectedChannels.erase(pos);
74 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060075 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
76}
77
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070078class FaceCreateFixture : protected BaseFixture
79{
80public:
81 void
82 ignore()
83 {
84 }
85
86 void
87 checkError(const std::string& errorActual, const std::string& errorExpected)
88 {
89 BOOST_CHECK_EQUAL(errorActual, errorExpected);
90 }
91
92 void
93 failIfError(const std::string& errorActual)
94 {
95 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
96 }
97};
98
99BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
100{
101 TcpFactory factory = TcpFactory();
102
Chengyu Fan4381fb62015-01-14 11:37:04 -0700103 factory.createFace(FaceUri("tcp4://127.0.0.1:6363"),
104 bind(&FaceCreateFixture::ignore, this),
105 bind(&FaceCreateFixture::checkError, this, _1,
106 "No channels available to connect to 127.0.0.1:6363"));
107
108 factory.createChannel("127.0.0.1", "20071");
109
110 factory.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700111 bind(&FaceCreateFixture::ignore, this),
112 bind(&FaceCreateFixture::failIfError, this, _1));
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700113}
114
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700115class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700116{
117public:
118 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800119 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700120 {
Junxiao Shi79494162014-04-02 18:25:11 -0700121 BOOST_CHECK(!static_cast<bool>(face1));
122 face1 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700123 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
124 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
125 face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700126
Junxiao Shi79494162014-04-02 18:25:11 -0700127 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700128 }
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();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700136 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800137
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700138 void
139 face1_onReceiveInterest(const Interest& interest)
140 {
Junxiao Shi79494162014-04-02 18:25:11 -0700141 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700142
Junxiao Shi79494162014-04-02 18:25:11 -0700143 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700144 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800145
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700146 void
147 face1_onReceiveData(const Data& data)
148 {
Junxiao Shi79494162014-04-02 18:25:11 -0700149 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700150
Junxiao Shi79494162014-04-02 18:25:11 -0700151 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700152 }
153
154 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800155 face1_onFail()
156 {
Junxiao Shi79494162014-04-02 18:25:11 -0700157 face1.reset();
158 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800159 }
160
161 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800162 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700163 {
Junxiao Shi79494162014-04-02 18:25:11 -0700164 BOOST_CHECK(!static_cast<bool>(face2));
165 face2 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700166 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
167 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
168 face2->onFail.connect(bind(&EndToEndFixture::face2_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700169
Junxiao Shi79494162014-04-02 18:25:11 -0700170 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700171 }
172
173 void
174 channel2_onConnectFailed(const std::string& reason)
175 {
176 BOOST_CHECK_MESSAGE(false, reason);
177
Junxiao Shi79494162014-04-02 18:25:11 -0700178 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700179 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800180
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700181 void
182 face2_onReceiveInterest(const Interest& interest)
183 {
Junxiao Shi79494162014-04-02 18:25:11 -0700184 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700185
Junxiao Shi79494162014-04-02 18:25:11 -0700186 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700187 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800188
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700189 void
190 face2_onReceiveData(const Data& data)
191 {
Junxiao Shi79494162014-04-02 18:25:11 -0700192 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700193
Junxiao Shi79494162014-04-02 18:25:11 -0700194 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700195 }
196
197 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800198 face2_onFail()
199 {
Junxiao Shi79494162014-04-02 18:25:11 -0700200 face2.reset();
201 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800202 }
203
204 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800205 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800206 {
Junxiao Shi79494162014-04-02 18:25:11 -0700207 faces.push_back(newFace);
208 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800209 }
210
211 void
212 channel_onConnectFailed(const std::string& reason)
213 {
214 BOOST_CHECK_MESSAGE(false, reason);
215
Junxiao Shi79494162014-04-02 18:25:11 -0700216 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800217 }
218
219 void
220 checkFaceList(size_t shouldBe)
221 {
Junxiao Shi79494162014-04-02 18:25:11 -0700222 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800223 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800224
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200225 void
226 connect(const shared_ptr<TcpChannel>& channel,
227 const std::string& remoteHost,
228 const std::string& remotePort)
229 {
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800230 channel->connect(tcp::Endpoint(boost::asio::ip::address::from_string(remoteHost),
231 boost::lexical_cast<uint16_t>(remotePort)),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200232 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
233 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
234 }
235
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700236public:
Junxiao Shi79494162014-04-02 18:25:11 -0700237 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700238
Junxiao Shi79494162014-04-02 18:25:11 -0700239 shared_ptr<Face> face1;
240 std::vector<Interest> face1_receivedInterests;
241 std::vector<Data> face1_receivedDatas;
242 shared_ptr<Face> face2;
243 std::vector<Interest> face2_receivedInterests;
244 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800245
Davide Pesaventob499a602014-11-18 22:36:56 +0100246 std::list<shared_ptr<Face>> faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700247};
248
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000249BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700250{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600251 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700252
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600253 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
254 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800255
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700256 BOOST_CHECK_EQUAL(channel1->isListening(), false);
257
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700258 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
259 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800260
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700261 BOOST_CHECK_EQUAL(channel1->isListening(), true);
262
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600263 TcpFactory factory2;
264
265 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
266 factory2.createChannel("127.0.0.2", "20071");
267
Chengyu Fan4381fb62015-01-14 11:37:04 -0700268 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600269 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
270 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700271
Junxiao Shi79494162014-04-02 18:25:11 -0700272 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700273 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700274
Junxiao Shi79494162014-04-02 18:25:11 -0700275 BOOST_REQUIRE(static_cast<bool>(face1));
276 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800277
Yukai Tu731f0d72015-07-04 11:14:44 +0800278 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
279 BOOST_CHECK_EQUAL(face2->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento94279412015-02-27 01:29:32 +0100280 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
281 BOOST_CHECK_EQUAL(face2->isMultiAccess(), false);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700282
Junxiao Shi79494162014-04-02 18:25:11 -0700283 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
284 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
285 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000286
Junxiao Shi79494162014-04-02 18:25:11 -0700287 BOOST_CHECK_EQUAL(face1->isLocal(), true);
288 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000289
Junxiao Shi79494162014-04-02 18:25:11 -0700290 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
291 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000292
293 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
294
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700295 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
296 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
297 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
298 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000299
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700300 face1->sendInterest(*interest1);
301 face1->sendInterest(*interest1);
302 face1->sendInterest(*interest1);
303 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700304 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700305 face2->sendInterest(*interest2);
306 face2->sendData (*data2 );
307 face2->sendData (*data2 );
308 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000309
Junxiao Shi79494162014-04-02 18:25:11 -0700310 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000311 "TcpChannel error: cannot send or receive Interest/Data packets");
312
Junxiao Shi79494162014-04-02 18:25:11 -0700313 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
314 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
315 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
316 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000317
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700318 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
319 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
320 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
321 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000322
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700323 // needed to ensure NOutBytes counters are accurate
324 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
325
Junxiao Shi79494162014-04-02 18:25:11 -0700326 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700327 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
328 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
329 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
330 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700331 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000332
Junxiao Shi79494162014-04-02 18:25:11 -0700333 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700334 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
335 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
336 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
337 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700338 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000339}
340
341BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
342{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600343 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000344
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600345 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
346 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000347
348 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
349 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
350
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600351 TcpFactory factory2;
352
353 factory2.createChannel("::2", "20070");
354
Chengyu Fan4381fb62015-01-14 11:37:04 -0700355 factory2.createFace(FaceUri("tcp6://[::1]:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600356 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
357 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000358
Junxiao Shi79494162014-04-02 18:25:11 -0700359 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000360 "TcpChannel error: cannot connect or cannot accept connection");
361
Junxiao Shi79494162014-04-02 18:25:11 -0700362 BOOST_REQUIRE(static_cast<bool>(face1));
363 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000364
Junxiao Shi79494162014-04-02 18:25:11 -0700365 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
366 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
367 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000368
Junxiao Shi79494162014-04-02 18:25:11 -0700369 BOOST_CHECK_EQUAL(face1->isLocal(), true);
370 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800371
Junxiao Shi79494162014-04-02 18:25:11 -0700372 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
373 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800374
375 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800376
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700377 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
378 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
379 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
380 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700381
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700382 face1->sendInterest(*interest1);
383 face1->sendData (*data1 );
384 face2->sendInterest(*interest2);
385 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700386
Junxiao Shi79494162014-04-02 18:25:11 -0700387 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700388 "TcpChannel error: cannot send or receive Interest/Data packets");
389
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700390
Junxiao Shi79494162014-04-02 18:25:11 -0700391 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
392 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
393 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
394 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800395
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700396 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
397 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
398 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
399 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700400}
401
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800402BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
403{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800404 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800405
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800406 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
407 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800408
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800409 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
410 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800411
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800412 using namespace boost::asio;
413 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800414 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
415 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800416 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800417
Junxiao Shi79494162014-04-02 18:25:11 -0700418 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700419 "TcpChannel error: cannot connect or cannot accept connection");
420
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800421
Junxiao Shi79494162014-04-02 18:25:11 -0700422 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800423
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800424 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800425 channel3->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800426 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
427 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800428 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800429
430
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800431 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800432
433 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800434
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700435 scheduler::schedule(time::seconds(1),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200436 bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070"));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800437
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700438 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800439 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800440
Junxiao Shi79494162014-04-02 18:25:11 -0700441 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700442 time::seconds(10)) == LimitedIo::EXCEED_OPS,
443 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800444
Junxiao Shi79494162014-04-02 18:25:11 -0700445 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800446}
447
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800448
449BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
450{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800451 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800452
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800453 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
454 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800455
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800456 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
457 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800458
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800459 using namespace boost::asio;
460 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800461 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
462 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800463 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800464
Junxiao Shi79494162014-04-02 18:25:11 -0700465 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700466 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800467
468 BOOST_CHECK_EQUAL(channel1->size(), 1);
469 BOOST_CHECK_EQUAL(channel2->size(), 1);
470
Junxiao Shi79494162014-04-02 18:25:11 -0700471 BOOST_REQUIRE(static_cast<bool>(face1));
472 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800473
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700474 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700475 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800476
Junxiao Shi79494162014-04-02 18:25:11 -0700477 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700478 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800479
480 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700481 BOOST_CHECK(!static_cast<bool>(face1));
482 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800483
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800484 BOOST_CHECK_EQUAL(channel1->size(), 0);
485 BOOST_CHECK_EQUAL(channel2->size(), 0);
486}
487
488
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700489class SimpleEndToEndFixture : protected BaseFixture
490{
491public:
492 void
493 onFaceCreated(const shared_ptr<Face>& face)
494 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700495 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
496 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
497 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700498
499 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
500 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
501 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
502
503 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
504 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
505 }
506
507 limitedIo.afterOp();
508 }
509
510 void
511 onConnectFailed(const std::string& reason)
512 {
513 BOOST_CHECK_MESSAGE(false, reason);
514
515 limitedIo.afterOp();
516 }
517
518 void
519 onReceiveInterest(const Interest& interest)
520 {
521 receivedInterests.push_back(interest);
522
523 limitedIo.afterOp();
524 }
525
526 void
527 onReceiveData(const Data& data)
528 {
529 receivedDatas.push_back(data);
530
531 limitedIo.afterOp();
532 }
533
534 void
535 onFail(const shared_ptr<Face>& face)
536 {
537 limitedIo.afterOp();
538 }
539
540public:
541 LimitedIo limitedIo;
542
543 std::vector<Interest> receivedInterests;
544 std::vector<Data> receivedDatas;
545};
546
547
548BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset,
549 CorruptedPackets, SimpleEndToEndFixture)
550{
551 TcpFactory factory;
Davide Pesavento2231ab52015-03-16 00:15:13 +0100552 tcp::Endpoint endpoint(boost::asio::ip::address_v4::from_string("127.0.0.1"), 20070);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700553
Davide Pesavento2231ab52015-03-16 00:15:13 +0100554 shared_ptr<TcpChannel> channel = factory.createChannel(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700555 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
556 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
557 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
558
559 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700560 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700561
562 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
563 time::seconds(1)) == LimitedIo::EXCEED_TIME,
564 "Exception thrown for " + Dataset::getName());
565}
566
567BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset,
568 CorruptedPackets, SimpleEndToEndFixture)
569{
Davide Pesavento2231ab52015-03-16 00:15:13 +0100570 // test with non-local Face
571 boost::asio::ip::address_v4 someIpv4Address;
Davide Pesaventob499a602014-11-18 22:36:56 +0100572 for (const auto& netif : listNetworkInterfaces()) {
573 if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) {
Davide Pesavento2231ab52015-03-16 00:15:13 +0100574 someIpv4Address = netif.ipv4Addresses.front();
Davide Pesaventob499a602014-11-18 22:36:56 +0100575 break;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700576 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100577 }
Davide Pesavento2231ab52015-03-16 00:15:13 +0100578 if (someIpv4Address.is_unspecified()) {
Davide Pesaventob499a602014-11-18 22:36:56 +0100579 BOOST_TEST_MESSAGE("Test with non-local Face cannot be run "
Davide Pesavento2231ab52015-03-16 00:15:13 +0100580 "(no non-loopback interface with IPv4 address found)");
Davide Pesaventob499a602014-11-18 22:36:56 +0100581 return;
582 }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700583
584 TcpFactory factory;
Davide Pesavento2231ab52015-03-16 00:15:13 +0100585 tcp::Endpoint endpoint(someIpv4Address, 20070);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700586
Davide Pesavento2231ab52015-03-16 00:15:13 +0100587 shared_ptr<TcpChannel> channel = factory.createChannel(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700588 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
589 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
590 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
591
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700592 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700593 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700594
595 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
596 time::seconds(1)) == LimitedIo::EXCEED_TIME,
597 "Exception thrown for " + Dataset::getName());
598}
599
Alexander Afanasyev18861802014-06-13 17:49:03 -0700600class FaceCreateTimeoutFixture : protected BaseFixture
601{
602public:
603 void
604 onFaceCreated(const shared_ptr<Face>& newFace)
605 {
606 BOOST_CHECK_MESSAGE(false, "Timeout expected");
607 BOOST_CHECK(!static_cast<bool>(face1));
608 face1 = newFace;
609
610 limitedIo.afterOp();
611 }
612
613 void
614 onConnectFailed(const std::string& reason)
615 {
616 BOOST_CHECK_MESSAGE(true, reason);
617
618 limitedIo.afterOp();
619 }
620
621public:
622 LimitedIo limitedIo;
623
624 shared_ptr<Face> face1;
625};
626
Alexander Afanasyev18861802014-06-13 17:49:03 -0700627BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
628{
629 TcpFactory factory;
630 shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070");
631
Chengyu Fan4381fb62015-01-14 11:37:04 -0700632 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Alexander Afanasyev18861802014-06-13 17:49:03 -0700633 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
634 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1));
635
636 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS,
637 "TcpChannel error: cannot connect or cannot accept connection");
638
639 BOOST_CHECK_EQUAL(static_cast<bool>(face1), false);
640}
641
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200642BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture)
643{
644 TcpFactory factory1;
645
646 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
647 factory1.createChannel("127.0.0.1", "20071");
648
649 BOOST_CHECK_EQUAL(channel1->isListening(), false);
650
651 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
652 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
653
654 BOOST_CHECK_EQUAL(channel1->isListening(), true);
655
656 TcpFactory factory2;
657
658 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
659 factory2.createChannel("127.0.0.2", "20071");
660
Chengyu Fan4381fb62015-01-14 11:37:04 -0700661 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200662 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
663 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
664
665 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
666 "TcpChannel error: cannot connect or cannot accept connection");
667
668 BOOST_REQUIRE(static_cast<bool>(face1));
669 BOOST_REQUIRE(static_cast<bool>(face2));
670
671 std::ostringstream hugeName;
672 hugeName << "/huge-name/";
Junxiao Shi39cd6332014-11-06 21:53:18 -0700673 for (size_t i = 0; i < ndn::MAX_NDN_PACKET_SIZE; i++)
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200674 hugeName << 'a';
675
676 shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix");
677 shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str());
678
679 face1->sendInterest(*hugeInterest);
680 face2->sendInterest(*interest);
681 face2->sendInterest(*interest);
682
683 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
684 BOOST_TEST_MESSAGE("Unexpected assertion test passed");
685}
Alexander Afanasyev18861802014-06-13 17:49:03 -0700686
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800687class FakeNetworkInterfaceFixture : public BaseFixture
688{
689public:
690 FakeNetworkInterfaceFixture()
691 {
692 using namespace boost::asio::ip;
693
694 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
695
696 fakeInterfaces->push_back(
697 NetworkInterfaceInfo {0, "eth0",
698 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
699 {address_v4::from_string("0.0.0.0")},
700 {address_v6::from_string("::")},
701 address_v4(),
702 IFF_UP});
703 fakeInterfaces->push_back(
704 NetworkInterfaceInfo {1, "eth0",
705 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
706 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
707 {},
708 address_v4::from_string("192.168.2.255"),
709 0});
710 fakeInterfaces->push_back(
711 NetworkInterfaceInfo {2, "eth1",
712 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
713 {address_v4::from_string("198.51.100.1")},
714 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
715 address_v4::from_string("198.51.100.255"),
716 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
717
718 setDebugNetworkInterfaces(fakeInterfaces);
719 }
720
721 ~FakeNetworkInterfaceFixture()
722 {
723 setDebugNetworkInterfaces(nullptr);
724 }
725};
726
727BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
728{
729 using namespace boost::asio::ip;
730
731 TcpFactory factory;
732 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
733 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
734 BOOST_CHECK((factory.m_prohibitedEndpoints ==
735 std::set<tcp::Endpoint> {
736 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
737 }));
738
739 factory.m_prohibitedEndpoints.clear();
740 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
741 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
742 BOOST_CHECK((factory.m_prohibitedEndpoints ==
743 std::set<tcp::Endpoint> {
744 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
745 }));
746
747 factory.m_prohibitedEndpoints.clear();
748 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
749 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
750 BOOST_CHECK((factory.m_prohibitedEndpoints ==
751 std::set<tcp::Endpoint> {
752 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
753 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
754 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
755 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
756 }));
757
758 factory.m_prohibitedEndpoints.clear();
759 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
760 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
761 BOOST_CHECK((factory.m_prohibitedEndpoints ==
762 std::set<tcp::Endpoint> {
763 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
764 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
765 tcp::Endpoint(address_v6::from_string("::"), 2048)
766 }));
767}
768
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700769BOOST_AUTO_TEST_SUITE_END()
770
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700771} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700772} // namespace nfd