blob: 9ee9881fb41927d55d519daffe8d3fd266f7e729 [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>
37#include <ndn-cxx/util/dns.hpp>
38
Junxiao Shi96dc0c42014-01-30 23:51:59 -070039namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070040namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070041
Junxiao Shid9ee45c2014-02-27 15:38:11 -070042BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070043
44BOOST_AUTO_TEST_CASE(ChannelMap)
45{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080046 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080047
Alexander Afanasyevd6655302014-02-28 08:41:28 -080048 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
49 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070050 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070051 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080052
Alexander Afanasyevd6655302014-02-28 08:41:28 -080053 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070054 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070055
56 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
57 BOOST_CHECK_NE(channel2, channel3);
58 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070059}
60
Steve DiBenedettoef04f272014-06-04 14:28:31 -060061BOOST_AUTO_TEST_CASE(GetChannels)
62{
63 TcpFactory factory;
64 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
65
Davide Pesaventob499a602014-11-18 22:36:56 +010066 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060067 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
68 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
69 expectedChannels.push_back(factory.createChannel("::1", "20071"));
70
Davide Pesaventob499a602014-11-18 22:36:56 +010071 for (const auto& ch : factory.getChannels()) {
72 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
73 BOOST_REQUIRE(pos != expectedChannels.end());
74 expectedChannels.erase(pos);
75 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060076 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
77}
78
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070079class FaceCreateFixture : protected BaseFixture
80{
81public:
82 void
83 ignore()
84 {
85 }
86
87 void
88 checkError(const std::string& errorActual, const std::string& errorExpected)
89 {
90 BOOST_CHECK_EQUAL(errorActual, errorExpected);
91 }
92
93 void
94 failIfError(const std::string& errorActual)
95 {
96 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
97 }
98};
99
100BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
101{
102 TcpFactory factory = TcpFactory();
103
Chengyu Fan4381fb62015-01-14 11:37:04 -0700104 factory.createFace(FaceUri("tcp4://127.0.0.1:6363"),
105 bind(&FaceCreateFixture::ignore, this),
106 bind(&FaceCreateFixture::checkError, this, _1,
107 "No channels available to connect to 127.0.0.1:6363"));
108
109 factory.createChannel("127.0.0.1", "20071");
110
111 factory.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700112 bind(&FaceCreateFixture::ignore, this),
113 bind(&FaceCreateFixture::failIfError, this, _1));
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700114}
115
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700116class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700117{
118public:
119 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800120 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700121 {
Junxiao Shi79494162014-04-02 18:25:11 -0700122 BOOST_CHECK(!static_cast<bool>(face1));
123 face1 = 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));
126 face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700127
Junxiao Shi79494162014-04-02 18:25:11 -0700128 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700129 }
130
131 void
132 channel1_onConnectFailed(const std::string& reason)
133 {
134 BOOST_CHECK_MESSAGE(false, reason);
135
Junxiao Shi79494162014-04-02 18:25:11 -0700136 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700137 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800138
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700139 void
140 face1_onReceiveInterest(const Interest& interest)
141 {
Junxiao Shi79494162014-04-02 18:25:11 -0700142 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700143
Junxiao Shi79494162014-04-02 18:25:11 -0700144 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700145 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800146
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700147 void
148 face1_onReceiveData(const Data& data)
149 {
Junxiao Shi79494162014-04-02 18:25:11 -0700150 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700151
Junxiao Shi79494162014-04-02 18:25:11 -0700152 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700153 }
154
155 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800156 face1_onFail()
157 {
Junxiao Shi79494162014-04-02 18:25:11 -0700158 face1.reset();
159 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800160 }
161
162 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800163 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700164 {
Junxiao Shi79494162014-04-02 18:25:11 -0700165 BOOST_CHECK(!static_cast<bool>(face2));
166 face2 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700167 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
168 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
169 face2->onFail.connect(bind(&EndToEndFixture::face2_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700170
Junxiao Shi79494162014-04-02 18:25:11 -0700171 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700172 }
173
174 void
175 channel2_onConnectFailed(const std::string& reason)
176 {
177 BOOST_CHECK_MESSAGE(false, reason);
178
Junxiao Shi79494162014-04-02 18:25:11 -0700179 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700180 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800181
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700182 void
183 face2_onReceiveInterest(const Interest& interest)
184 {
Junxiao Shi79494162014-04-02 18:25:11 -0700185 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700186
Junxiao Shi79494162014-04-02 18:25:11 -0700187 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700188 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800189
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700190 void
191 face2_onReceiveData(const Data& data)
192 {
Junxiao Shi79494162014-04-02 18:25:11 -0700193 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700194
Junxiao Shi79494162014-04-02 18:25:11 -0700195 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700196 }
197
198 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800199 face2_onFail()
200 {
Junxiao Shi79494162014-04-02 18:25:11 -0700201 face2.reset();
202 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800203 }
204
205 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800206 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800207 {
Junxiao Shi79494162014-04-02 18:25:11 -0700208 faces.push_back(newFace);
209 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800210 }
211
212 void
213 channel_onConnectFailed(const std::string& reason)
214 {
215 BOOST_CHECK_MESSAGE(false, reason);
216
Junxiao Shi79494162014-04-02 18:25:11 -0700217 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800218 }
219
220 void
221 checkFaceList(size_t shouldBe)
222 {
Junxiao Shi79494162014-04-02 18:25:11 -0700223 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800224 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800225
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200226 void
227 connect(const shared_ptr<TcpChannel>& channel,
228 const std::string& remoteHost,
229 const std::string& remotePort)
230 {
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800231 channel->connect(tcp::Endpoint(boost::asio::ip::address::from_string(remoteHost),
232 boost::lexical_cast<uint16_t>(remotePort)),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200233 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
234 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
235 }
236
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700237public:
Junxiao Shi79494162014-04-02 18:25:11 -0700238 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700239
Junxiao Shi79494162014-04-02 18:25:11 -0700240 shared_ptr<Face> face1;
241 std::vector<Interest> face1_receivedInterests;
242 std::vector<Data> face1_receivedDatas;
243 shared_ptr<Face> face2;
244 std::vector<Interest> face2_receivedInterests;
245 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800246
Davide Pesaventob499a602014-11-18 22:36:56 +0100247 std::list<shared_ptr<Face>> faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700248};
249
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000250BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700251{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600252 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700253
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600254 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
255 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800256
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700257 BOOST_CHECK_EQUAL(channel1->isListening(), false);
258
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700259 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
260 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800261
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700262 BOOST_CHECK_EQUAL(channel1->isListening(), true);
263
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600264 TcpFactory factory2;
265
266 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
267 factory2.createChannel("127.0.0.2", "20071");
268
Chengyu Fan4381fb62015-01-14 11:37:04 -0700269 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600270 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
271 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700274 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700275
Junxiao Shi79494162014-04-02 18:25:11 -0700276 BOOST_REQUIRE(static_cast<bool>(face1));
277 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800278
Davide Pesavento94279412015-02-27 01:29:32 +0100279 BOOST_CHECK_EQUAL(face1->isOnDemand(), true);
280 BOOST_CHECK_EQUAL(face2->isOnDemand(), false);
281 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
282 BOOST_CHECK_EQUAL(face2->isMultiAccess(), false);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700283
Junxiao Shi79494162014-04-02 18:25:11 -0700284 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
285 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
286 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000287
Junxiao Shi79494162014-04-02 18:25:11 -0700288 BOOST_CHECK_EQUAL(face1->isLocal(), true);
289 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000290
Junxiao Shi79494162014-04-02 18:25:11 -0700291 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
292 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000293
294 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
295
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700296 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
297 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
298 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
299 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000300
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700301 face1->sendInterest(*interest1);
302 face1->sendInterest(*interest1);
303 face1->sendInterest(*interest1);
304 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700305 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700306 face2->sendInterest(*interest2);
307 face2->sendData (*data2 );
308 face2->sendData (*data2 );
309 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000310
Junxiao Shi79494162014-04-02 18:25:11 -0700311 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000312 "TcpChannel error: cannot send or receive Interest/Data packets");
313
Junxiao Shi79494162014-04-02 18:25:11 -0700314 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
315 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
316 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
317 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000318
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700319 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
320 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
321 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
322 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000323
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700324 // needed to ensure NOutBytes counters are accurate
325 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
326
Junxiao Shi79494162014-04-02 18:25:11 -0700327 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700328 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
329 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
330 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
331 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700332 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000333
Junxiao Shi79494162014-04-02 18:25:11 -0700334 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700335 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
336 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
337 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
338 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700339 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000340}
341
342BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
343{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600344 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000345
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600346 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
347 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000348
349 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
350 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
351
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600352 TcpFactory factory2;
353
354 factory2.createChannel("::2", "20070");
355
Chengyu Fan4381fb62015-01-14 11:37:04 -0700356 factory2.createFace(FaceUri("tcp6://[::1]:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600357 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
358 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000359
Junxiao Shi79494162014-04-02 18:25:11 -0700360 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000361 "TcpChannel error: cannot connect or cannot accept connection");
362
Junxiao Shi79494162014-04-02 18:25:11 -0700363 BOOST_REQUIRE(static_cast<bool>(face1));
364 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000365
Junxiao Shi79494162014-04-02 18:25:11 -0700366 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
367 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
368 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000369
Junxiao Shi79494162014-04-02 18:25:11 -0700370 BOOST_CHECK_EQUAL(face1->isLocal(), true);
371 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800372
Junxiao Shi79494162014-04-02 18:25:11 -0700373 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
374 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800375
376 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800377
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700378 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
379 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
380 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
381 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700382
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700383 face1->sendInterest(*interest1);
384 face1->sendData (*data1 );
385 face2->sendInterest(*interest2);
386 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700387
Junxiao Shi79494162014-04-02 18:25:11 -0700388 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700389 "TcpChannel error: cannot send or receive Interest/Data packets");
390
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700391
Junxiao Shi79494162014-04-02 18:25:11 -0700392 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
393 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
394 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
395 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800396
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700397 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
398 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
399 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
400 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700401}
402
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800403BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
404{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800405 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800406
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800407 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
408 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800409
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800410 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
411 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800412
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800413 using namespace boost::asio;
414 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800415 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
416 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800417 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800418
Junxiao Shi79494162014-04-02 18:25:11 -0700419 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700420 "TcpChannel error: cannot connect or cannot accept connection");
421
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800422
Junxiao Shi79494162014-04-02 18:25:11 -0700423 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800424
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800425 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800426 channel3->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800427 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
428 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800429 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800430
431
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800432 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800433
434 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800435
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700436 scheduler::schedule(time::seconds(1),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200437 bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070"));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800438
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700439 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800440 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800441
Junxiao Shi79494162014-04-02 18:25:11 -0700442 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700443 time::seconds(10)) == LimitedIo::EXCEED_OPS,
444 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800445
Junxiao Shi79494162014-04-02 18:25:11 -0700446 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800447}
448
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800449
450BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
451{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800452 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800453
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800454 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
455 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800456
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800457 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
458 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800459
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800460 using namespace boost::asio;
461 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800462 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
463 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800464 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800465
Junxiao Shi79494162014-04-02 18:25:11 -0700466 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700467 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800468
469 BOOST_CHECK_EQUAL(channel1->size(), 1);
470 BOOST_CHECK_EQUAL(channel2->size(), 1);
471
Junxiao Shi79494162014-04-02 18:25:11 -0700472 BOOST_REQUIRE(static_cast<bool>(face1));
473 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800474
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700475 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700476 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800477
Junxiao Shi79494162014-04-02 18:25:11 -0700478 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700479 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800480
481 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700482 BOOST_CHECK(!static_cast<bool>(face1));
483 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800484
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800485 BOOST_CHECK_EQUAL(channel1->size(), 0);
486 BOOST_CHECK_EQUAL(channel2->size(), 0);
487}
488
489
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700490class SimpleEndToEndFixture : protected BaseFixture
491{
492public:
493 void
494 onFaceCreated(const shared_ptr<Face>& face)
495 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700496 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
497 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
498 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700499
500 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
501 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
502 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
503
504 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
505 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
506 }
507
508 limitedIo.afterOp();
509 }
510
511 void
512 onConnectFailed(const std::string& reason)
513 {
514 BOOST_CHECK_MESSAGE(false, reason);
515
516 limitedIo.afterOp();
517 }
518
519 void
520 onReceiveInterest(const Interest& interest)
521 {
522 receivedInterests.push_back(interest);
523
524 limitedIo.afterOp();
525 }
526
527 void
528 onReceiveData(const Data& data)
529 {
530 receivedDatas.push_back(data);
531
532 limitedIo.afterOp();
533 }
534
535 void
536 onFail(const shared_ptr<Face>& face)
537 {
538 limitedIo.afterOp();
539 }
540
541public:
542 LimitedIo limitedIo;
543
544 std::vector<Interest> receivedInterests;
545 std::vector<Data> receivedDatas;
546};
547
548
549BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset,
550 CorruptedPackets, SimpleEndToEndFixture)
551{
552 TcpFactory factory;
553
554 shared_ptr<TcpChannel> channel = factory.createChannel("127.0.0.1", "20070");
555 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 tcp::Endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 20070);
561 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700562
563 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
564 time::seconds(1)) == LimitedIo::EXCEED_TIME,
565 "Exception thrown for " + Dataset::getName());
566}
567
568BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset,
569 CorruptedPackets, SimpleEndToEndFixture)
570{
571 // tests with non-local Face
572 std::string someIpv4Address;
Davide Pesaventob499a602014-11-18 22:36:56 +0100573 for (const auto& netif : listNetworkInterfaces()) {
574 if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) {
575 someIpv4Address = netif.ipv4Addresses[0].to_string();
576 break;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700577 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100578 }
579 if (someIpv4Address.empty()) {
580 BOOST_TEST_MESSAGE("Test with non-local Face cannot be run "
581 "(no non-local interface with IPv4 address available)");
582 return;
583 }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700584
585 TcpFactory factory;
586
587 shared_ptr<TcpChannel> channel = factory.createChannel(someIpv4Address, "20070");
588 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 tcp::Endpoint endpoint(ndn::dns::syncResolve(someIpv4Address, getGlobalIoService()), 20070);
594 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700595
596 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
597 time::seconds(1)) == LimitedIo::EXCEED_TIME,
598 "Exception thrown for " + Dataset::getName());
599}
600
Alexander Afanasyev18861802014-06-13 17:49:03 -0700601class FaceCreateTimeoutFixture : protected BaseFixture
602{
603public:
604 void
605 onFaceCreated(const shared_ptr<Face>& newFace)
606 {
607 BOOST_CHECK_MESSAGE(false, "Timeout expected");
608 BOOST_CHECK(!static_cast<bool>(face1));
609 face1 = newFace;
610
611 limitedIo.afterOp();
612 }
613
614 void
615 onConnectFailed(const std::string& reason)
616 {
617 BOOST_CHECK_MESSAGE(true, reason);
618
619 limitedIo.afterOp();
620 }
621
622public:
623 LimitedIo limitedIo;
624
625 shared_ptr<Face> face1;
626};
627
Alexander Afanasyev18861802014-06-13 17:49:03 -0700628BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
629{
630 TcpFactory factory;
631 shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070");
632
Chengyu Fan4381fb62015-01-14 11:37:04 -0700633 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Alexander Afanasyev18861802014-06-13 17:49:03 -0700634 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
635 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1));
636
637 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS,
638 "TcpChannel error: cannot connect or cannot accept connection");
639
640 BOOST_CHECK_EQUAL(static_cast<bool>(face1), false);
641}
642
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200643BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture)
644{
645 TcpFactory factory1;
646
647 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
648 factory1.createChannel("127.0.0.1", "20071");
649
650 BOOST_CHECK_EQUAL(channel1->isListening(), false);
651
652 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
653 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
654
655 BOOST_CHECK_EQUAL(channel1->isListening(), true);
656
657 TcpFactory factory2;
658
659 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
660 factory2.createChannel("127.0.0.2", "20071");
661
Chengyu Fan4381fb62015-01-14 11:37:04 -0700662 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200663 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
664 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
665
666 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
667 "TcpChannel error: cannot connect or cannot accept connection");
668
669 BOOST_REQUIRE(static_cast<bool>(face1));
670 BOOST_REQUIRE(static_cast<bool>(face2));
671
672 std::ostringstream hugeName;
673 hugeName << "/huge-name/";
Junxiao Shi39cd6332014-11-06 21:53:18 -0700674 for (size_t i = 0; i < ndn::MAX_NDN_PACKET_SIZE; i++)
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200675 hugeName << 'a';
676
677 shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix");
678 shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str());
679
680 face1->sendInterest(*hugeInterest);
681 face2->sendInterest(*interest);
682 face2->sendInterest(*interest);
683
684 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
685 BOOST_TEST_MESSAGE("Unexpected assertion test passed");
686}
Alexander Afanasyev18861802014-06-13 17:49:03 -0700687
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800688class FakeNetworkInterfaceFixture : public BaseFixture
689{
690public:
691 FakeNetworkInterfaceFixture()
692 {
693 using namespace boost::asio::ip;
694
695 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
696
697 fakeInterfaces->push_back(
698 NetworkInterfaceInfo {0, "eth0",
699 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
700 {address_v4::from_string("0.0.0.0")},
701 {address_v6::from_string("::")},
702 address_v4(),
703 IFF_UP});
704 fakeInterfaces->push_back(
705 NetworkInterfaceInfo {1, "eth0",
706 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
707 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
708 {},
709 address_v4::from_string("192.168.2.255"),
710 0});
711 fakeInterfaces->push_back(
712 NetworkInterfaceInfo {2, "eth1",
713 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
714 {address_v4::from_string("198.51.100.1")},
715 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
716 address_v4::from_string("198.51.100.255"),
717 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
718
719 setDebugNetworkInterfaces(fakeInterfaces);
720 }
721
722 ~FakeNetworkInterfaceFixture()
723 {
724 setDebugNetworkInterfaces(nullptr);
725 }
726};
727
728BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
729{
730 using namespace boost::asio::ip;
731
732 TcpFactory factory;
733 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
734 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
735 BOOST_CHECK((factory.m_prohibitedEndpoints ==
736 std::set<tcp::Endpoint> {
737 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
738 }));
739
740 factory.m_prohibitedEndpoints.clear();
741 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
742 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
743 BOOST_CHECK((factory.m_prohibitedEndpoints ==
744 std::set<tcp::Endpoint> {
745 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
746 }));
747
748 factory.m_prohibitedEndpoints.clear();
749 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
750 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
751 BOOST_CHECK((factory.m_prohibitedEndpoints ==
752 std::set<tcp::Endpoint> {
753 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
754 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
755 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
756 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
757 }));
758
759 factory.m_prohibitedEndpoints.clear();
760 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
761 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
762 BOOST_CHECK((factory.m_prohibitedEndpoints ==
763 std::set<tcp::Endpoint> {
764 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
765 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
766 tcp::Endpoint(address_v6::from_string("::"), 2048)
767 }));
768}
769
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700770BOOST_AUTO_TEST_SUITE_END()
771
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700772} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700773} // namespace nfd