blob: ee2eb415da1071e3ffdadff40064de874f5fdc85 [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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "face/tcp-factory.hpp"
Chengyu Fan4381fb62015-01-14 11:37:04 -070027#include <ndn-cxx/util/dns.hpp>
Alexander Afanasyev650028d2014-04-25 18:39:10 -070028#include "core/network-interface.hpp"
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shi96dc0c42014-01-30 23:51:59 -070030
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
36namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070037namespace tests {
Junxiao Shi96dc0c42014-01-30 23:51:59 -070038
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -070040
41BOOST_AUTO_TEST_CASE(ChannelMap)
42{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080043 TcpFactory factory;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080044
Alexander Afanasyevd6655302014-02-28 08:41:28 -080045 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
46 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070047 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070048 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080049
Alexander Afanasyevd6655302014-02-28 08:41:28 -080050 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070051 BOOST_CHECK_NE(channel1, channel2);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070052
53 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
54 BOOST_CHECK_NE(channel2, channel3);
55 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
Junxiao Shi96dc0c42014-01-30 23:51:59 -070056}
57
Steve DiBenedettoef04f272014-06-04 14:28:31 -060058BOOST_AUTO_TEST_CASE(GetChannels)
59{
60 TcpFactory factory;
61 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
62
Davide Pesaventob499a602014-11-18 22:36:56 +010063 std::vector<shared_ptr<const Channel>> expectedChannels;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060064 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
65 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
66 expectedChannels.push_back(factory.createChannel("::1", "20071"));
67
Davide Pesaventob499a602014-11-18 22:36:56 +010068 for (const auto& ch : factory.getChannels()) {
69 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
70 BOOST_REQUIRE(pos != expectedChannels.end());
71 expectedChannels.erase(pos);
72 }
Steve DiBenedettoef04f272014-06-04 14:28:31 -060073 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
74}
75
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070076class FaceCreateFixture : protected BaseFixture
77{
78public:
79 void
80 ignore()
81 {
82 }
83
84 void
85 checkError(const std::string& errorActual, const std::string& errorExpected)
86 {
87 BOOST_CHECK_EQUAL(errorActual, errorExpected);
88 }
89
90 void
91 failIfError(const std::string& errorActual)
92 {
93 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
94 }
95};
96
97BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
98{
99 TcpFactory factory = TcpFactory();
100
Chengyu Fan4381fb62015-01-14 11:37:04 -0700101 factory.createFace(FaceUri("tcp4://127.0.0.1:6363"),
102 bind(&FaceCreateFixture::ignore, this),
103 bind(&FaceCreateFixture::checkError, this, _1,
104 "No channels available to connect to 127.0.0.1:6363"));
105
106 factory.createChannel("127.0.0.1", "20071");
107
108 factory.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700109 bind(&FaceCreateFixture::ignore, this),
110 bind(&FaceCreateFixture::failIfError, this, _1));
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700111}
112
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700113class EndToEndFixture : protected BaseFixture
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700114{
115public:
116 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800117 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700118 {
Junxiao Shi79494162014-04-02 18:25:11 -0700119 BOOST_CHECK(!static_cast<bool>(face1));
120 face1 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700121 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
122 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
123 face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700124
Junxiao Shi79494162014-04-02 18:25:11 -0700125 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700126 }
127
128 void
129 channel1_onConnectFailed(const std::string& reason)
130 {
131 BOOST_CHECK_MESSAGE(false, reason);
132
Junxiao Shi79494162014-04-02 18:25:11 -0700133 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700134 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800135
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700136 void
137 face1_onReceiveInterest(const Interest& interest)
138 {
Junxiao Shi79494162014-04-02 18:25:11 -0700139 face1_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700140
Junxiao Shi79494162014-04-02 18:25:11 -0700141 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700142 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800143
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700144 void
145 face1_onReceiveData(const Data& data)
146 {
Junxiao Shi79494162014-04-02 18:25:11 -0700147 face1_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700148
Junxiao Shi79494162014-04-02 18:25:11 -0700149 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700150 }
151
152 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800153 face1_onFail()
154 {
Junxiao Shi79494162014-04-02 18:25:11 -0700155 face1.reset();
156 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800157 }
158
159 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800160 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700161 {
Junxiao Shi79494162014-04-02 18:25:11 -0700162 BOOST_CHECK(!static_cast<bool>(face2));
163 face2 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700164 face2->onReceiveInterest.connect(bind(&EndToEndFixture::face2_onReceiveInterest, this, _1));
165 face2->onReceiveData.connect(bind(&EndToEndFixture::face2_onReceiveData, this, _1));
166 face2->onFail.connect(bind(&EndToEndFixture::face2_onFail, this));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700167
Junxiao Shi79494162014-04-02 18:25:11 -0700168 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700169 }
170
171 void
172 channel2_onConnectFailed(const std::string& reason)
173 {
174 BOOST_CHECK_MESSAGE(false, reason);
175
Junxiao Shi79494162014-04-02 18:25:11 -0700176 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700177 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800178
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700179 void
180 face2_onReceiveInterest(const Interest& interest)
181 {
Junxiao Shi79494162014-04-02 18:25:11 -0700182 face2_receivedInterests.push_back(interest);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700183
Junxiao Shi79494162014-04-02 18:25:11 -0700184 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700185 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800186
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700187 void
188 face2_onReceiveData(const Data& data)
189 {
Junxiao Shi79494162014-04-02 18:25:11 -0700190 face2_receivedDatas.push_back(data);
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700191
Junxiao Shi79494162014-04-02 18:25:11 -0700192 limitedIo.afterOp();
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700193 }
194
195 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800196 face2_onFail()
197 {
Junxiao Shi79494162014-04-02 18:25:11 -0700198 face2.reset();
199 limitedIo.afterOp();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800200 }
201
202 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800203 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800204 {
Junxiao Shi79494162014-04-02 18:25:11 -0700205 faces.push_back(newFace);
206 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800207 }
208
209 void
210 channel_onConnectFailed(const std::string& reason)
211 {
212 BOOST_CHECK_MESSAGE(false, reason);
213
Junxiao Shi79494162014-04-02 18:25:11 -0700214 limitedIo.afterOp();
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800215 }
216
217 void
218 checkFaceList(size_t shouldBe)
219 {
Junxiao Shi79494162014-04-02 18:25:11 -0700220 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800221 }
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800222
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200223 void
224 connect(const shared_ptr<TcpChannel>& channel,
225 const std::string& remoteHost,
226 const std::string& remotePort)
227 {
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800228 channel->connect(tcp::Endpoint(boost::asio::ip::address::from_string(remoteHost),
229 boost::lexical_cast<uint16_t>(remotePort)),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200230 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
231 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
232 }
233
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700234public:
Junxiao Shi79494162014-04-02 18:25:11 -0700235 LimitedIo limitedIo;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700236
Junxiao Shi79494162014-04-02 18:25:11 -0700237 shared_ptr<Face> face1;
238 std::vector<Interest> face1_receivedInterests;
239 std::vector<Data> face1_receivedDatas;
240 shared_ptr<Face> face2;
241 std::vector<Interest> face2_receivedInterests;
242 std::vector<Data> face2_receivedDatas;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800243
Davide Pesaventob499a602014-11-18 22:36:56 +0100244 std::list<shared_ptr<Face>> faces;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700245};
246
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000247BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700248{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600249 TcpFactory factory1;
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700250
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600251 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
252 factory1.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800253
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700254 BOOST_CHECK_EQUAL(channel1->isListening(), false);
255
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700256 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
257 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800258
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700259 BOOST_CHECK_EQUAL(channel1->isListening(), true);
260
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600261 TcpFactory factory2;
262
263 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
264 factory2.createChannel("127.0.0.2", "20071");
265
Chengyu Fan4381fb62015-01-14 11:37:04 -0700266 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600267 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
268 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700269
Junxiao Shi79494162014-04-02 18:25:11 -0700270 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700271 "TcpChannel error: cannot connect or cannot accept connection");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700272
Junxiao Shi79494162014-04-02 18:25:11 -0700273 BOOST_REQUIRE(static_cast<bool>(face1));
274 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800275
Davide Pesavento94279412015-02-27 01:29:32 +0100276 BOOST_CHECK_EQUAL(face1->isOnDemand(), true);
277 BOOST_CHECK_EQUAL(face2->isOnDemand(), false);
278 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
279 BOOST_CHECK_EQUAL(face2->isMultiAccess(), false);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700280
Junxiao Shi79494162014-04-02 18:25:11 -0700281 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp4://127.0.0.1:20070");
282 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp4://127.0.0.1:20070");
283 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000284
Junxiao Shi79494162014-04-02 18:25:11 -0700285 BOOST_CHECK_EQUAL(face1->isLocal(), true);
286 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000287
Junxiao Shi79494162014-04-02 18:25:11 -0700288 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
289 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000290
291 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
292
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700293 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
294 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
295 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
296 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000297
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700298 face1->sendInterest(*interest1);
299 face1->sendInterest(*interest1);
300 face1->sendInterest(*interest1);
301 face1->sendData (*data1 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700302 size_t nBytesSent1 = interest1->wireEncode().size() * 3 + data1->wireEncode().size();
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700303 face2->sendInterest(*interest2);
304 face2->sendData (*data2 );
305 face2->sendData (*data2 );
306 face2->sendData (*data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000307
Junxiao Shi79494162014-04-02 18:25:11 -0700308 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000309 "TcpChannel error: cannot send or receive Interest/Data packets");
310
Junxiao Shi79494162014-04-02 18:25:11 -0700311 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
312 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
313 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
314 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000315
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700316 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
317 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
318 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
319 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000320
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700321 // needed to ensure NOutBytes counters are accurate
322 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
323
Junxiao Shi79494162014-04-02 18:25:11 -0700324 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700325 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1);
326 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
327 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
328 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700329 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000330
Junxiao Shi79494162014-04-02 18:25:11 -0700331 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700332 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
333 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
334 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1);
335 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700336 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000337}
338
339BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
340{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600341 TcpFactory factory1;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000342
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600343 shared_ptr<TcpChannel> channel1 = factory1.createChannel("::1", "20070");
344 shared_ptr<TcpChannel> channel2 = factory1.createChannel("::1", "20071");
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000345
346 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
347 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
348
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600349 TcpFactory factory2;
350
351 factory2.createChannel("::2", "20070");
352
Chengyu Fan4381fb62015-01-14 11:37:04 -0700353 factory2.createFace(FaceUri("tcp6://[::1]:20070"),
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600354 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
355 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000356
Junxiao Shi79494162014-04-02 18:25:11 -0700357 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000358 "TcpChannel error: cannot connect or cannot accept connection");
359
Junxiao Shi79494162014-04-02 18:25:11 -0700360 BOOST_REQUIRE(static_cast<bool>(face1));
361 BOOST_REQUIRE(static_cast<bool>(face2));
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000362
Junxiao Shi79494162014-04-02 18:25:11 -0700363 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "tcp6://[::1]:20070");
364 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "tcp6://[::1]:20070");
365 // face1 has an unknown remoteUri, since the source port is automatically chosen by OS
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000366
Junxiao Shi79494162014-04-02 18:25:11 -0700367 BOOST_CHECK_EQUAL(face1->isLocal(), true);
368 BOOST_CHECK_EQUAL(face2->isLocal(), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800369
Junxiao Shi79494162014-04-02 18:25:11 -0700370 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), true);
371 BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face2)), true);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800372
373 // integrated tests needs to check that TcpFace for non-loopback fails these tests...
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800374
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700375 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
376 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
377 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
378 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700379
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700380 face1->sendInterest(*interest1);
381 face1->sendData (*data1 );
382 face2->sendInterest(*interest2);
383 face2->sendData (*data2 );
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700384
Junxiao Shi79494162014-04-02 18:25:11 -0700385 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700386 "TcpChannel error: cannot send or receive Interest/Data packets");
387
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700388
Junxiao Shi79494162014-04-02 18:25:11 -0700389 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
390 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
391 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
392 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800393
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700394 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName());
395 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
396 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName());
397 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName());
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700398}
399
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800400BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
401{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800402 TcpFactory factory;
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800403
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800404 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
405 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800406
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800407 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
408 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800409
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800410 using namespace boost::asio;
411 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800412 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
413 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800414 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800415
Junxiao Shi79494162014-04-02 18:25:11 -0700416 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700417 "TcpChannel error: cannot connect or cannot accept connection");
418
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800419
Junxiao Shi79494162014-04-02 18:25:11 -0700420 BOOST_CHECK_EQUAL(faces.size(), 2);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800421
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800422 shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800423 channel3->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800424 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
425 bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800426 time::seconds(4)); // very short timeout
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800427
428
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800429 shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800430
431 BOOST_CHECK_NE(channel3, channel4);
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800432
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700433 scheduler::schedule(time::seconds(1),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200434 bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070"));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800435
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700436 scheduler::schedule(time::milliseconds(500),
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800437 bind(&EndToEndFixture::checkFaceList, this, 4));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800438
Junxiao Shi79494162014-04-02 18:25:11 -0700439 BOOST_CHECK_MESSAGE(limitedIo.run(4,// 2 connects and 2 accepts
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700440 time::seconds(10)) == LimitedIo::EXCEED_OPS,
441 "TcpChannel error: cannot connect or cannot accept multiple connections");
Alexander Afanasyev7329e022014-02-27 14:47:22 -0800442
Junxiao Shi79494162014-04-02 18:25:11 -0700443 BOOST_CHECK_EQUAL(faces.size(), 6);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800444}
445
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800446
447BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
448{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -0800449 TcpFactory factory;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800450
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800451 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
452 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800453
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800454 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
455 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800456
Alexander Afanasyevbaba2532015-02-13 18:27:33 -0800457 using namespace boost::asio;
458 channel2->connect(tcp::Endpoint(ip::address::from_string("127.0.0.1"), 20070),
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800459 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
460 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -0800461 time::seconds(4)); // very short timeout
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800462
Junxiao Shi79494162014-04-02 18:25:11 -0700463 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700464 "TcpChannel error: cannot connect or cannot accept connection");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800465
466 BOOST_CHECK_EQUAL(channel1->size(), 1);
467 BOOST_CHECK_EQUAL(channel2->size(), 1);
468
Junxiao Shi79494162014-04-02 18:25:11 -0700469 BOOST_REQUIRE(static_cast<bool>(face1));
470 BOOST_CHECK(static_cast<bool>(face2));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800471
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700472 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700473 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face1));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800474
Junxiao Shi79494162014-04-02 18:25:11 -0700475 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700476 "FaceClosing error: cannot properly close faces");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800477
478 // both faces should get closed
Junxiao Shi79494162014-04-02 18:25:11 -0700479 BOOST_CHECK(!static_cast<bool>(face1));
480 BOOST_CHECK(!static_cast<bool>(face2));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800481
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800482 BOOST_CHECK_EQUAL(channel1->size(), 0);
483 BOOST_CHECK_EQUAL(channel2->size(), 0);
484}
485
486
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700487class SimpleEndToEndFixture : protected BaseFixture
488{
489public:
490 void
491 onFaceCreated(const shared_ptr<Face>& face)
492 {
Junxiao Shic099ddb2014-12-25 20:53:20 -0700493 face->onReceiveInterest.connect(bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1));
494 face->onReceiveData.connect(bind(&SimpleEndToEndFixture::onReceiveData, this, _1));
495 face->onFail.connect(bind(&SimpleEndToEndFixture::onFail, this, face));
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700496
497 if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) {
498 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
499 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
500
501 static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature(
502 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
503 }
504
505 limitedIo.afterOp();
506 }
507
508 void
509 onConnectFailed(const std::string& reason)
510 {
511 BOOST_CHECK_MESSAGE(false, reason);
512
513 limitedIo.afterOp();
514 }
515
516 void
517 onReceiveInterest(const Interest& interest)
518 {
519 receivedInterests.push_back(interest);
520
521 limitedIo.afterOp();
522 }
523
524 void
525 onReceiveData(const Data& data)
526 {
527 receivedDatas.push_back(data);
528
529 limitedIo.afterOp();
530 }
531
532 void
533 onFail(const shared_ptr<Face>& face)
534 {
535 limitedIo.afterOp();
536 }
537
538public:
539 LimitedIo limitedIo;
540
541 std::vector<Interest> receivedInterests;
542 std::vector<Data> receivedDatas;
543};
544
545
546BOOST_FIXTURE_TEST_CASE_TEMPLATE(LocalFaceCorruptedInput, Dataset,
547 CorruptedPackets, SimpleEndToEndFixture)
548{
549 TcpFactory factory;
550
551 shared_ptr<TcpChannel> channel = factory.createChannel("127.0.0.1", "20070");
552 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
553 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
554 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
555
556 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700557 tcp::Endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 20070);
558 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700559
560 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
561 time::seconds(1)) == LimitedIo::EXCEED_TIME,
562 "Exception thrown for " + Dataset::getName());
563}
564
565BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceCorruptedInput, Dataset,
566 CorruptedPackets, SimpleEndToEndFixture)
567{
568 // tests with non-local Face
569 std::string someIpv4Address;
Davide Pesaventob499a602014-11-18 22:36:56 +0100570 for (const auto& netif : listNetworkInterfaces()) {
571 if (!netif.isLoopback() && netif.isUp() && !netif.ipv4Addresses.empty()) {
572 someIpv4Address = netif.ipv4Addresses[0].to_string();
573 break;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700574 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100575 }
576 if (someIpv4Address.empty()) {
577 BOOST_TEST_MESSAGE("Test with non-local Face cannot be run "
578 "(no non-local interface with IPv4 address available)");
579 return;
580 }
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700581
582 TcpFactory factory;
583
584 shared_ptr<TcpChannel> channel = factory.createChannel(someIpv4Address, "20070");
585 channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1),
586 bind(&SimpleEndToEndFixture::onConnectFailed, this, _1));
587 BOOST_REQUIRE_EQUAL(channel->isListening(), true);
588
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700589 DummyStreamSender<boost::asio::ip::tcp, Dataset> sender;
Chengyu Fan4381fb62015-01-14 11:37:04 -0700590 tcp::Endpoint endpoint(ndn::dns::syncResolve(someIpv4Address, getGlobalIoService()), 20070);
591 sender.start(endpoint);
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700592
593 BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS,
594 time::seconds(1)) == LimitedIo::EXCEED_TIME,
595 "Exception thrown for " + Dataset::getName());
596}
597
Alexander Afanasyev18861802014-06-13 17:49:03 -0700598class FaceCreateTimeoutFixture : protected BaseFixture
599{
600public:
601 void
602 onFaceCreated(const shared_ptr<Face>& newFace)
603 {
604 BOOST_CHECK_MESSAGE(false, "Timeout expected");
605 BOOST_CHECK(!static_cast<bool>(face1));
606 face1 = newFace;
607
608 limitedIo.afterOp();
609 }
610
611 void
612 onConnectFailed(const std::string& reason)
613 {
614 BOOST_CHECK_MESSAGE(true, reason);
615
616 limitedIo.afterOp();
617 }
618
619public:
620 LimitedIo limitedIo;
621
622 shared_ptr<Face> face1;
623};
624
Alexander Afanasyev18861802014-06-13 17:49:03 -0700625BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
626{
627 TcpFactory factory;
628 shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070");
629
Chengyu Fan4381fb62015-01-14 11:37:04 -0700630 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
Alexander Afanasyev18861802014-06-13 17:49:03 -0700631 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
632 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _1));
633
634 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS,
635 "TcpChannel error: cannot connect or cannot accept connection");
636
637 BOOST_CHECK_EQUAL(static_cast<bool>(face1), false);
638}
639
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200640BOOST_FIXTURE_TEST_CASE(Bug1856, EndToEndFixture)
641{
642 TcpFactory factory1;
643
644 shared_ptr<TcpChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
645 factory1.createChannel("127.0.0.1", "20071");
646
647 BOOST_CHECK_EQUAL(channel1->isListening(), false);
648
649 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
650 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
651
652 BOOST_CHECK_EQUAL(channel1->isListening(), true);
653
654 TcpFactory factory2;
655
656 shared_ptr<TcpChannel> channel2 = factory2.createChannel("127.0.0.2", "20070");
657 factory2.createChannel("127.0.0.2", "20071");
658
Chengyu Fan4381fb62015-01-14 11:37:04 -0700659 factory2.createFace(FaceUri("tcp4://127.0.0.1:20070"),
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200660 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
661 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
662
663 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
664 "TcpChannel error: cannot connect or cannot accept connection");
665
666 BOOST_REQUIRE(static_cast<bool>(face1));
667 BOOST_REQUIRE(static_cast<bool>(face2));
668
669 std::ostringstream hugeName;
670 hugeName << "/huge-name/";
Junxiao Shi39cd6332014-11-06 21:53:18 -0700671 for (size_t i = 0; i < ndn::MAX_NDN_PACKET_SIZE; i++)
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200672 hugeName << 'a';
673
674 shared_ptr<Interest> interest = makeInterest("ndn:/KfczhUqVix");
675 shared_ptr<Interest> hugeInterest = makeInterest(hugeName.str());
676
677 face1->sendInterest(*hugeInterest);
678 face2->sendInterest(*interest);
679 face2->sendInterest(*interest);
680
681 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(1));
682 BOOST_TEST_MESSAGE("Unexpected assertion test passed");
683}
Alexander Afanasyev18861802014-06-13 17:49:03 -0700684
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800685class FakeNetworkInterfaceFixture : public BaseFixture
686{
687public:
688 FakeNetworkInterfaceFixture()
689 {
690 using namespace boost::asio::ip;
691
692 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
693
694 fakeInterfaces->push_back(
695 NetworkInterfaceInfo {0, "eth0",
696 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
697 {address_v4::from_string("0.0.0.0")},
698 {address_v6::from_string("::")},
699 address_v4(),
700 IFF_UP});
701 fakeInterfaces->push_back(
702 NetworkInterfaceInfo {1, "eth0",
703 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
704 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
705 {},
706 address_v4::from_string("192.168.2.255"),
707 0});
708 fakeInterfaces->push_back(
709 NetworkInterfaceInfo {2, "eth1",
710 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
711 {address_v4::from_string("198.51.100.1")},
712 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
713 address_v4::from_string("198.51.100.255"),
714 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
715
716 setDebugNetworkInterfaces(fakeInterfaces);
717 }
718
719 ~FakeNetworkInterfaceFixture()
720 {
721 setDebugNetworkInterfaces(nullptr);
722 }
723};
724
725BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
726{
727 using namespace boost::asio::ip;
728
729 TcpFactory factory;
730 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
731 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
732 BOOST_CHECK((factory.m_prohibitedEndpoints ==
733 std::set<tcp::Endpoint> {
734 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
735 }));
736
737 factory.m_prohibitedEndpoints.clear();
738 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
739 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
740 BOOST_CHECK((factory.m_prohibitedEndpoints ==
741 std::set<tcp::Endpoint> {
742 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
743 }));
744
745 factory.m_prohibitedEndpoints.clear();
746 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
747 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
748 BOOST_CHECK((factory.m_prohibitedEndpoints ==
749 std::set<tcp::Endpoint> {
750 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
751 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
752 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
753 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
754 }));
755
756 factory.m_prohibitedEndpoints.clear();
757 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
758 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
759 BOOST_CHECK((factory.m_prohibitedEndpoints ==
760 std::set<tcp::Endpoint> {
761 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
762 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
763 tcp::Endpoint(address_v6::from_string("::"), 2048)
764 }));
765}
766
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700767BOOST_AUTO_TEST_SUITE_END()
768
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700769} // namespace tests
Junxiao Shi96dc0c42014-01-30 23:51:59 -0700770} // namespace nfd