blob: 7418b264a110a05f4291ac814a409ccdcedf5f94 [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry42602412016-08-27 09:33:18 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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.
10 *
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/>.
24 */
25
26#include "face/tcp-factory.hpp"
27
28#include "core/network-interface.hpp"
Eric Newberry42602412016-08-27 09:33:18 -070029#include "factory-test-common.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
31
32namespace nfd {
33namespace tests {
34
35BOOST_AUTO_TEST_SUITE(Face)
36BOOST_FIXTURE_TEST_SUITE(TestTcpFactory, BaseFixture)
37
38using nfd::Face;
39
40BOOST_AUTO_TEST_CASE(ChannelMap)
41{
42 TcpFactory factory;
43
44 shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
45 shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
46 BOOST_CHECK_EQUAL(channel1, channel1a);
47 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
48
49 shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
50 BOOST_CHECK_NE(channel1, channel2);
51
52 shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
53 BOOST_CHECK_NE(channel2, channel3);
54 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
55}
56
57BOOST_AUTO_TEST_CASE(GetChannels)
58{
59 TcpFactory factory;
60 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
61
62 std::vector<shared_ptr<const Channel>> expectedChannels;
63 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
64 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
65 expectedChannels.push_back(factory.createChannel("::1", "20071"));
66
67 for (const auto& ch : factory.getChannels()) {
68 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), ch);
69 BOOST_REQUIRE(pos != expectedChannels.end());
70 expectedChannels.erase(pos);
71 }
72 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
73}
74
Eric Newberry42602412016-08-27 09:33:18 -070075BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -070076{
77 TcpFactory factory;
78
Eric Newberry42602412016-08-27 09:33:18 -070079 createFace(factory,
80 FaceUri("tcp4://127.0.0.1:6363"),
81 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
82 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -070083
84 factory.createChannel("127.0.0.1", "20071");
85
Eric Newberry42602412016-08-27 09:33:18 -070086 createFace(factory,
87 FaceUri("tcp4://127.0.0.1:20070"),
88 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
89 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -070090}
91
92BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
93{
94 TcpFactory factory;
95
96 factory.createChannel("127.0.0.1", "20070");
97 factory.createChannel("127.0.0.1", "20071");
98
Eric Newberry42602412016-08-27 09:33:18 -070099 createFace(factory,
100 FaceUri("tcp4://127.0.0.1:20070"),
101 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
102 {CreateFaceExpectedResult::FAILURE, 406,
103 "Outgoing TCP faces only support persistent persistency"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700104
Eric Newberry42602412016-08-27 09:33:18 -0700105 createFace(factory,
106 FaceUri("tcp4://127.0.0.1:20071"),
107 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
108 {CreateFaceExpectedResult::FAILURE, 406,
109 "Outgoing TCP faces only support persistent persistency"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700110}
111
112class FaceCreateTimeoutFixture : public BaseFixture
113{
114public:
115 void
116 onFaceCreated(const shared_ptr<Face>& newFace)
117 {
118 BOOST_CHECK_MESSAGE(false, "Timeout expected");
119 BOOST_CHECK(!static_cast<bool>(face1));
120 face1 = newFace;
121
122 limitedIo.afterOp();
123 }
124
125 void
126 onConnectFailed(const std::string& reason)
127 {
128 BOOST_CHECK_MESSAGE(true, reason);
129
130 limitedIo.afterOp();
131 }
132
133public:
134 LimitedIo limitedIo;
135
136 shared_ptr<Face> face1;
137};
138
139BOOST_FIXTURE_TEST_CASE(FaceCreateTimeout, FaceCreateTimeoutFixture)
140{
141 TcpFactory factory;
142 shared_ptr<TcpChannel> channel = factory.createChannel("0.0.0.0", "20070");
143
144 factory.createFace(FaceUri("tcp4://192.0.2.1:20070"),
145 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
146 bind(&FaceCreateTimeoutFixture::onFaceCreated, this, _1),
Eric Newberry42602412016-08-27 09:33:18 -0700147 bind(&FaceCreateTimeoutFixture::onConnectFailed, this, _2));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700148
149 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(10)) == LimitedIo::EXCEED_OPS,
150 "TcpChannel error: cannot connect or cannot accept connection");
151
152 BOOST_CHECK_EQUAL(static_cast<bool>(face1), false);
153}
154
155class FakeNetworkInterfaceFixture : public BaseFixture
156{
157public:
158 FakeNetworkInterfaceFixture()
159 {
160 using namespace boost::asio::ip;
161
162 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
163
164 fakeInterfaces->push_back(
165 NetworkInterfaceInfo {0, "eth0",
166 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
167 {address_v4::from_string("0.0.0.0")},
168 {address_v6::from_string("::")},
169 address_v4(),
170 IFF_UP});
171 fakeInterfaces->push_back(
172 NetworkInterfaceInfo {1, "eth0",
173 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
174 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
175 {},
176 address_v4::from_string("192.168.2.255"),
177 0});
178 fakeInterfaces->push_back(
179 NetworkInterfaceInfo {2, "eth1",
180 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
181 {address_v4::from_string("198.51.100.1")},
182 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
183 address_v4::from_string("198.51.100.255"),
184 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
185
186 setDebugNetworkInterfaces(fakeInterfaces);
187 }
188
189 ~FakeNetworkInterfaceFixture()
190 {
191 setDebugNetworkInterfaces(nullptr);
192 }
193};
194
195BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
196{
197 using namespace boost::asio::ip;
198
199 TcpFactory factory;
200 factory.prohibitEndpoint(tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
201 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
202 BOOST_CHECK((factory.m_prohibitedEndpoints ==
203 std::set<tcp::Endpoint> {
204 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
205 }));
206
207 factory.m_prohibitedEndpoints.clear();
208 factory.prohibitEndpoint(tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
209 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
210 BOOST_CHECK((factory.m_prohibitedEndpoints ==
211 std::set<tcp::Endpoint> {
212 tcp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)
213 }));
214
215 factory.m_prohibitedEndpoints.clear();
216 factory.prohibitEndpoint(tcp::Endpoint(address_v4(), 1024));
217 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 4);
218 BOOST_CHECK((factory.m_prohibitedEndpoints ==
219 std::set<tcp::Endpoint> {
220 tcp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
221 tcp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
222 tcp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
223 tcp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
224 }));
225
226 factory.m_prohibitedEndpoints.clear();
227 factory.prohibitEndpoint(tcp::Endpoint(address_v6(), 2048));
228 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
229 BOOST_CHECK((factory.m_prohibitedEndpoints ==
230 std::set<tcp::Endpoint> {
231 tcp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
232 tcp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
233 tcp::Endpoint(address_v6::from_string("::"), 2048)
234 }));
235}
236
237BOOST_AUTO_TEST_SUITE_END() // TestTcpFactory
238BOOST_AUTO_TEST_SUITE_END() // Face
239
240} // namespace tests
241} // namespace nfd