blob: eb27f3a0e69f2a9d0f6db5957b04388ef7b998fa [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Weiwei Liu72cee942016-02-04 16:49:19 -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/udp-factory.hpp"
27
Eric Newberry42602412016-08-27 09:33:18 -070028#include "factory-test-common.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070029#include "core/network-interface.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(TestUdpFactory, BaseFixture)
37
38using nfd::Face;
39
40BOOST_AUTO_TEST_CASE(GetChannels)
41{
42 UdpFactory factory;
43 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
44
45 std::vector<shared_ptr<const Channel>> expectedChannels;
46 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
47 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
48 expectedChannels.push_back(factory.createChannel("::1", "20071"));
49
50 for (const auto& i : factory.getChannels()) {
51 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
52 BOOST_REQUIRE(pos != expectedChannels.end());
53 expectedChannels.erase(pos);
54 }
55
56 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
57}
58
Weiwei Liu72cee942016-02-04 16:49:19 -070059BOOST_AUTO_TEST_CASE(CreateChannel)
Junxiao Shicde37ad2015-12-24 01:02:05 -070060{
Weiwei Liu72cee942016-02-04 16:49:19 -070061 UdpFactory factory;
Junxiao Shicde37ad2015-12-24 01:02:05 -070062
Weiwei Liu72cee942016-02-04 16:49:19 -070063 auto channel1 = factory.createChannel("127.0.0.1", "20070");
64 auto channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -070065 BOOST_CHECK_EQUAL(channel1, channel1a);
66 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
67
Weiwei Liu72cee942016-02-04 16:49:19 -070068 auto channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -070069 BOOST_CHECK_NE(channel1, channel2);
70
Weiwei Liu72cee942016-02-04 16:49:19 -070071 auto channel3 = factory.createChannel("::1", "20071");
72 BOOST_CHECK_NE(channel2, channel3);
73 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -070074
Weiwei Liu72cee942016-02-04 16:49:19 -070075 // createChannel with multicast address
76 BOOST_CHECK_EXCEPTION(factory.createChannel("224.0.0.1", "20070"), UdpFactory::Error,
77 [] (const UdpFactory::Error& e) {
78 return strcmp(e.what(),
79 "createChannel is only for unicast channels. The provided endpoint "
80 "is multicast. Use createMulticastFace to create a multicast face") == 0;
81 });
Junxiao Shicde37ad2015-12-24 01:02:05 -070082
Weiwei Liu72cee942016-02-04 16:49:19 -070083 // createChannel with a local endpoint that has already been allocated for a UDP multicast face
84 auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072");
85 BOOST_CHECK_EXCEPTION(factory.createChannel("127.0.0.1", "20072"), UdpFactory::Error,
86 [] (const UdpFactory::Error& e) {
87 return strcmp(e.what(),
88 "Cannot create the requested UDP unicast channel, local "
89 "endpoint is already allocated for a UDP multicast face") == 0;
90 });
91}
Junxiao Shicde37ad2015-12-24 01:02:05 -070092
Weiwei Liu72cee942016-02-04 16:49:19 -070093BOOST_AUTO_TEST_CASE(CreateMulticastFace)
94{
Weiwei Liu72cee942016-02-04 16:49:19 -070095 UdpFactory factory;
96
97 auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
98 auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -070099 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
100
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200101 // createMulticastFace with a local endpoint that is already used by a channel
Weiwei Liu72cee942016-02-04 16:49:19 -0700102 auto channel = factory.createChannel("127.0.0.1", "20071");
103 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error,
104 [] (const UdpFactory::Error& e) {
105 return strcmp(e.what(),
106 "Cannot create the requested UDP multicast face, local "
107 "endpoint is already allocated for a UDP unicast channel") == 0;
108 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700109
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200110 // createMulticastFace with a local endpoint that is already
111 // used by a multicast face on a different multicast group
Weiwei Liu72cee942016-02-04 16:49:19 -0700112 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error,
113 [] (const UdpFactory::Error& e) {
114 return strcmp(e.what(),
115 "Cannot create the requested UDP multicast face, local "
116 "endpoint is already allocated for a UDP multicast face "
117 "on a different multicast group") == 0;
118 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119
Weiwei Liu72cee942016-02-04 16:49:19 -0700120 // createMulticastFace with an IPv4 unicast address
121 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error,
122 [] (const UdpFactory::Error& e) {
123 return strcmp(e.what(),
124 "Cannot create the requested UDP multicast face, "
125 "the multicast group given as input is not a multicast address") == 0;
126 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700127
Weiwei Liu72cee942016-02-04 16:49:19 -0700128 // createMulticastFace with an IPv6 multicast address
129 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error,
130 [] (const UdpFactory::Error& e) {
131 return strcmp(e.what(),
132 "IPv6 multicast is not supported yet. Please provide an IPv4 "
133 "address") == 0;
134 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700135
Weiwei Liu72cee942016-02-04 16:49:19 -0700136 // createMulticastFace with different local and remote port numbers
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200137 udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074);
138 udp::Endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075);
Weiwei Liu72cee942016-02-04 16:49:19 -0700139 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error,
140 [] (const UdpFactory::Error& e) {
141 return strcmp(e.what(),
142 "Cannot create the requested UDP multicast face, "
143 "both endpoints should have the same port number. ") == 0;
144 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700145}
146
Eric Newberry42602412016-08-27 09:33:18 -0700147BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700148{
149 UdpFactory factory = UdpFactory();
150
Eric Newberry42602412016-08-27 09:33:18 -0700151 createFace(factory,
152 FaceUri("udp4://127.0.0.1:6363"),
153 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
154 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700155
156 factory.createChannel("127.0.0.1", "20071");
157
Eric Newberry42602412016-08-27 09:33:18 -0700158 createFace(factory,
159 FaceUri("udp4://127.0.0.1:20070"),
160 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
161 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700162 //test the upgrade
Eric Newberry42602412016-08-27 09:33:18 -0700163 createFace(factory,
164 FaceUri("udp4://127.0.0.1:20070"),
165 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
166 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700167
Eric Newberry42602412016-08-27 09:33:18 -0700168 createFace(factory,
169 FaceUri("udp4://127.0.0.1:20072"),
170 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
171 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700172}
173
174BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
175{
Eric Newberry42602412016-08-27 09:33:18 -0700176 UdpFactory factory = UdpFactory();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700177
178 factory.createChannel("127.0.0.1", "20070");
179
Eric Newberry42602412016-08-27 09:33:18 -0700180 createFace(factory,
181 FaceUri("udp4://127.0.0.1:20070"),
182 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
183 {CreateFaceExpectedResult::FAILURE, 406,
184 "Outgoing unicast UDP faces do not support on-demand persistency"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700185}
186
187class FakeNetworkInterfaceFixture : public BaseFixture
188{
189public:
190 FakeNetworkInterfaceFixture()
191 {
192 using namespace boost::asio::ip;
193
194 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
195
196 fakeInterfaces->push_back(
197 NetworkInterfaceInfo {0, "eth0",
198 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
199 {address_v4::from_string("0.0.0.0")},
200 {address_v6::from_string("::")},
201 address_v4(),
202 IFF_UP});
203 fakeInterfaces->push_back(
204 NetworkInterfaceInfo {1, "eth0",
205 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
206 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
207 {},
208 address_v4::from_string("192.168.2.255"),
209 0});
210 fakeInterfaces->push_back(
211 NetworkInterfaceInfo {2, "eth1",
212 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
213 {address_v4::from_string("198.51.100.1")},
214 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
215 address_v4::from_string("198.51.100.255"),
216 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
217
218 setDebugNetworkInterfaces(fakeInterfaces);
219 }
220
221 ~FakeNetworkInterfaceFixture()
222 {
223 setDebugNetworkInterfaces(nullptr);
224 }
225};
226
227BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
228{
229 using namespace boost::asio::ip;
230
231 UdpFactory factory;
232 factory.prohibitEndpoint(udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
233 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
234 BOOST_CHECK((factory.m_prohibitedEndpoints ==
235 std::set<udp::Endpoint> {
236 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
237 }));
238
239 factory.m_prohibitedEndpoints.clear();
240 factory.prohibitEndpoint(udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
241 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
242 BOOST_CHECK((factory.m_prohibitedEndpoints ==
243 std::set<udp::Endpoint> {
244 udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048),
245 }));
246
247 factory.m_prohibitedEndpoints.clear();
248 factory.prohibitEndpoint(udp::Endpoint(address_v4(), 1024));
249 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 6);
250 BOOST_CHECK((factory.m_prohibitedEndpoints ==
251 std::set<udp::Endpoint> {
252 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
253 udp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
254 udp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
255 udp::Endpoint(address_v4::from_string("198.51.100.255"), 1024),
256 udp::Endpoint(address_v4::from_string("255.255.255.255"), 1024),
257 udp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
258 }));
259
260 factory.m_prohibitedEndpoints.clear();
261 factory.prohibitEndpoint(udp::Endpoint(address_v6(), 2048));
262 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
263 BOOST_CHECK((factory.m_prohibitedEndpoints ==
264 std::set<udp::Endpoint> {
265 udp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
266 udp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
267 udp::Endpoint(address_v6::from_string("::"), 2048),
268 }));
269}
270
271BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
272BOOST_AUTO_TEST_SUITE_END() // Face
273
274} // namespace tests
275} // namespace nfd