| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /** |
| * Copyright (c) 2014-2016, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "face/udp-channel.hpp" |
| #include "face/transport.hpp" |
| |
| #include "test-ip.hpp" |
| #include "tests/limited-io.hpp" |
| #include "factory-test-common.hpp" |
| |
| #include <boost/mpl/vector.hpp> |
| |
| namespace nfd { |
| namespace tests { |
| |
| BOOST_AUTO_TEST_SUITE(Face) |
| |
| using nfd::Face; |
| namespace ip = boost::asio::ip; |
| |
| typedef boost::mpl::vector<ip::address_v4, |
| ip::address_v6> AddressFamilies; |
| |
| class UdpChannelFixture : public BaseFixture |
| { |
| protected: |
| UdpChannelFixture() |
| : nextPort(7050) |
| { |
| } |
| |
| unique_ptr<UdpChannel> |
| makeChannel(const ip::address& addr, uint16_t port = 0) |
| { |
| if (port == 0) |
| port = nextPort++; |
| |
| return make_unique<UdpChannel>(udp::Endpoint(addr, port), time::seconds(2)); |
| } |
| |
| void |
| listen(const ip::address& addr) |
| { |
| listenerEp = udp::Endpoint(addr, 7030); |
| listenerChannel = makeChannel(addr, 7030); |
| listenerChannel->listen( |
| [this] (const shared_ptr<Face>& newFace) { |
| BOOST_REQUIRE(newFace != nullptr); |
| connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| listenerFaces.push_back(newFace); |
| limitedIo.afterOp(); |
| }, |
| &failIfError); |
| } |
| |
| void |
| connect(UdpChannel& channel) |
| { |
| g_io.post([&] { |
| channel.connect(listenerEp, ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| [this] (const shared_ptr<Face>& newFace) { |
| BOOST_REQUIRE(newFace != nullptr); |
| connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| clientFaces.push_back(newFace); |
| face::Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello")); |
| newFace->getTransport()->send(std::move(pkt)); |
| limitedIo.afterOp(); |
| }, |
| &failIfError); |
| }); |
| } |
| |
| protected: |
| LimitedIo limitedIo; |
| udp::Endpoint listenerEp; |
| unique_ptr<UdpChannel> listenerChannel; |
| std::vector<shared_ptr<Face>> listenerFaces; |
| std::vector<shared_ptr<Face>> clientFaces; |
| |
| private: |
| uint16_t nextPort; |
| }; |
| |
| BOOST_FIXTURE_TEST_SUITE(TestUdpChannel, UdpChannelFixture) |
| |
| BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, A, AddressFamilies) |
| { |
| udp::Endpoint ep(A::loopback(), 7050); |
| auto channel = makeChannel(ep.address(), ep.port()); |
| BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep)); |
| } |
| |
| BOOST_AUTO_TEST_CASE(Listen) |
| { |
| auto channel = makeChannel(ip::address_v4()); |
| BOOST_CHECK_EQUAL(channel->isListening(), false); |
| |
| channel->listen(nullptr, nullptr); |
| BOOST_CHECK_EQUAL(channel->isListening(), true); |
| |
| // listen() is idempotent |
| BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr)); |
| BOOST_CHECK_EQUAL(channel->isListening(), true); |
| } |
| |
| BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, A, AddressFamilies) |
| { |
| auto address = getTestIp<A>(LoopbackAddress::Yes); |
| SKIP_IF_IP_UNAVAILABLE(address); |
| this->listen(address); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| |
| auto ch1 = makeChannel(A()); |
| this->connect(*ch1); |
| |
| BOOST_CHECK(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| BOOST_CHECK_EQUAL(ch1->size(), 1); |
| BOOST_CHECK_EQUAL(ch1->isListening(), false); |
| |
| auto ch2 = makeChannel(A()); |
| auto ch3 = makeChannel(A()); |
| this->connect(*ch2); |
| this->connect(*ch3); |
| |
| BOOST_CHECK(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| BOOST_CHECK_EQUAL(ch1->size(), 1); |
| BOOST_CHECK_EQUAL(ch2->size(), 1); |
| BOOST_CHECK_EQUAL(ch3->size(), 1); |
| BOOST_CHECK_EQUAL(clientFaces.size(), 3); |
| |
| // check face persistency |
| for (const auto& face : listenerFaces) { |
| BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| } |
| for (const auto& face : clientFaces) { |
| BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| } |
| |
| // connect twice to the same endpoint |
| this->connect(*ch3); |
| |
| BOOST_CHECK(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| BOOST_CHECK_EQUAL(ch1->size(), 1); |
| BOOST_CHECK_EQUAL(ch2->size(), 1); |
| BOOST_CHECK_EQUAL(ch3->size(), 1); |
| BOOST_CHECK_EQUAL(clientFaces.size(), 4); |
| BOOST_CHECK_EQUAL(clientFaces.at(2), clientFaces.at(3)); |
| } |
| |
| BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, A, AddressFamilies) |
| { |
| auto address = getTestIp<A>(LoopbackAddress::Yes); |
| SKIP_IF_IP_UNAVAILABLE(address); |
| this->listen(address); |
| |
| auto clientChannel = makeChannel(A()); |
| this->connect(*clientChannel); |
| |
| BOOST_CHECK(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| BOOST_CHECK_EQUAL(clientChannel->size(), 1); |
| |
| clientFaces.at(0)->close(); |
| |
| BOOST_CHECK(limitedIo.run(2, time::seconds(5)) == LimitedIo::EXCEED_OPS); |
| |
| BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| BOOST_CHECK_EQUAL(clientChannel->size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // TestUdpChannel |
| BOOST_AUTO_TEST_SUITE_END() // Face |
| |
| } // namespace tests |
| } // namespace nfd |