Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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. |
| 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 | |
| 28 | #include "core/network-interface.hpp" |
| 29 | #include "tests/test-common.hpp" |
| 30 | #include "tests/limited-io.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Face) |
| 36 | BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, BaseFixture) |
| 37 | |
| 38 | using nfd::Face; |
| 39 | |
| 40 | BOOST_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 Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 59 | BOOST_AUTO_TEST_CASE(CreateChannel) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 60 | { |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 61 | UdpFactory factory; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 62 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 63 | auto channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 64 | auto channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 66 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070"); |
| 67 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 68 | auto channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 69 | BOOST_CHECK_NE(channel1, channel2); |
| 70 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 71 | auto channel3 = factory.createChannel("::1", "20071"); |
| 72 | BOOST_CHECK_NE(channel2, channel3); |
| 73 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 74 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 75 | // 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 82 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 83 | // 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 92 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 93 | BOOST_AUTO_TEST_CASE(CreateMulticastFace) |
| 94 | { |
| 95 | using boost::asio::ip::udp; |
| 96 | |
| 97 | UdpFactory factory; |
| 98 | |
| 99 | auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070"); |
| 100 | auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
| 102 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 103 | // createMulticastFace with a local endpoint that has already been allocated for a UDP unicast channel |
| 104 | auto channel = factory.createChannel("127.0.0.1", "20071"); |
| 105 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error, |
| 106 | [] (const UdpFactory::Error& e) { |
| 107 | return strcmp(e.what(), |
| 108 | "Cannot create the requested UDP multicast face, local " |
| 109 | "endpoint is already allocated for a UDP unicast channel") == 0; |
| 110 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 111 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 112 | // createMulticastFace with a local endpoint that has already been allocated |
| 113 | // for a UDP multicast face on a different multicast group |
| 114 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error, |
| 115 | [] (const UdpFactory::Error& e) { |
| 116 | return strcmp(e.what(), |
| 117 | "Cannot create the requested UDP multicast face, local " |
| 118 | "endpoint is already allocated for a UDP multicast face " |
| 119 | "on a different multicast group") == 0; |
| 120 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 121 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 122 | // createMulticastFace with an IPv4 unicast address |
| 123 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error, |
| 124 | [] (const UdpFactory::Error& e) { |
| 125 | return strcmp(e.what(), |
| 126 | "Cannot create the requested UDP multicast face, " |
| 127 | "the multicast group given as input is not a multicast address") == 0; |
| 128 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 129 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 130 | // createMulticastFace with an IPv6 multicast address |
| 131 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error, |
| 132 | [] (const UdpFactory::Error& e) { |
| 133 | return strcmp(e.what(), |
| 134 | "IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 135 | "address") == 0; |
| 136 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 137 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame^] | 138 | // createMulticastFace with different local and remote port numbers |
| 139 | udp::endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074); |
| 140 | udp::endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075); |
| 141 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error, |
| 142 | [] (const UdpFactory::Error& e) { |
| 143 | return strcmp(e.what(), |
| 144 | "Cannot create the requested UDP multicast face, " |
| 145 | "both endpoints should have the same port number. ") == 0; |
| 146 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | class FaceCreateFixture : protected BaseFixture |
| 150 | { |
| 151 | public: |
| 152 | void |
| 153 | checkError(const std::string& errorActual, const std::string& errorExpected) |
| 154 | { |
| 155 | BOOST_CHECK_EQUAL(errorActual, errorExpected); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | failIfError(const std::string& errorActual) |
| 160 | { |
| 161 | BOOST_FAIL("No error expected, but got: [" << errorActual << "]"); |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture) |
| 166 | { |
| 167 | UdpFactory factory = UdpFactory(); |
| 168 | |
| 169 | factory.createFace(FaceUri("udp4://127.0.0.1:6363"), |
| 170 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 171 | bind([]{}), |
| 172 | bind(&FaceCreateFixture::checkError, this, _1, |
| 173 | "No channels available to connect to 127.0.0.1:6363")); |
| 174 | |
| 175 | factory.createChannel("127.0.0.1", "20071"); |
| 176 | |
| 177 | factory.createFace(FaceUri("udp4://127.0.0.1:20070"), |
| 178 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 179 | bind([]{}), |
| 180 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 181 | //test the upgrade |
| 182 | factory.createFace(FaceUri("udp4://127.0.0.1:20070"), |
| 183 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 184 | bind([]{}), |
| 185 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 186 | |
| 187 | factory.createFace(FaceUri("udp4://127.0.0.1:20072"), |
| 188 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 189 | bind([]{}), |
| 190 | bind(&FaceCreateFixture::failIfError, this, _1)); |
| 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 194 | { |
| 195 | UdpFactory factory; |
| 196 | |
| 197 | factory.createChannel("127.0.0.1", "20070"); |
| 198 | |
| 199 | BOOST_CHECK_THROW(factory.createFace(FaceUri("udp4://127.0.0.1:20070"), |
| 200 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
| 201 | bind([]{}), |
| 202 | bind([]{})), |
| 203 | ProtocolFactory::Error); |
| 204 | } |
| 205 | |
| 206 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 207 | { |
| 208 | public: |
| 209 | FakeNetworkInterfaceFixture() |
| 210 | { |
| 211 | using namespace boost::asio::ip; |
| 212 | |
| 213 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 214 | |
| 215 | fakeInterfaces->push_back( |
| 216 | NetworkInterfaceInfo {0, "eth0", |
| 217 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 218 | {address_v4::from_string("0.0.0.0")}, |
| 219 | {address_v6::from_string("::")}, |
| 220 | address_v4(), |
| 221 | IFF_UP}); |
| 222 | fakeInterfaces->push_back( |
| 223 | NetworkInterfaceInfo {1, "eth0", |
| 224 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 225 | {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")}, |
| 226 | {}, |
| 227 | address_v4::from_string("192.168.2.255"), |
| 228 | 0}); |
| 229 | fakeInterfaces->push_back( |
| 230 | NetworkInterfaceInfo {2, "eth1", |
| 231 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 232 | {address_v4::from_string("198.51.100.1")}, |
| 233 | {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")}, |
| 234 | address_v4::from_string("198.51.100.255"), |
| 235 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 236 | |
| 237 | setDebugNetworkInterfaces(fakeInterfaces); |
| 238 | } |
| 239 | |
| 240 | ~FakeNetworkInterfaceFixture() |
| 241 | { |
| 242 | setDebugNetworkInterfaces(nullptr); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture) |
| 247 | { |
| 248 | using namespace boost::asio::ip; |
| 249 | |
| 250 | UdpFactory factory; |
| 251 | factory.prohibitEndpoint(udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024)); |
| 252 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 253 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 254 | std::set<udp::Endpoint> { |
| 255 | udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 256 | })); |
| 257 | |
| 258 | factory.m_prohibitedEndpoints.clear(); |
| 259 | factory.prohibitEndpoint(udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048)); |
| 260 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1); |
| 261 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 262 | std::set<udp::Endpoint> { |
| 263 | udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048), |
| 264 | })); |
| 265 | |
| 266 | factory.m_prohibitedEndpoints.clear(); |
| 267 | factory.prohibitEndpoint(udp::Endpoint(address_v4(), 1024)); |
| 268 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 6); |
| 269 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 270 | std::set<udp::Endpoint> { |
| 271 | udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024), |
| 272 | udp::Endpoint(address_v4::from_string("192.168.2.2"), 1024), |
| 273 | udp::Endpoint(address_v4::from_string("198.51.100.1"), 1024), |
| 274 | udp::Endpoint(address_v4::from_string("198.51.100.255"), 1024), |
| 275 | udp::Endpoint(address_v4::from_string("255.255.255.255"), 1024), |
| 276 | udp::Endpoint(address_v4::from_string("0.0.0.0"), 1024) |
| 277 | })); |
| 278 | |
| 279 | factory.m_prohibitedEndpoints.clear(); |
| 280 | factory.prohibitEndpoint(udp::Endpoint(address_v6(), 2048)); |
| 281 | BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3); |
| 282 | BOOST_CHECK((factory.m_prohibitedEndpoints == |
| 283 | std::set<udp::Endpoint> { |
| 284 | udp::Endpoint(address_v6::from_string("2001:db8::2"), 2048), |
| 285 | udp::Endpoint(address_v6::from_string("2001:db8::3"), 2048), |
| 286 | udp::Endpoint(address_v6::from_string("::"), 2048), |
| 287 | })); |
| 288 | } |
| 289 | |
| 290 | BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory |
| 291 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 292 | |
| 293 | } // namespace tests |
| 294 | } // namespace nfd |