Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -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 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 26 | #include "face/udp-channel.hpp" |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 27 | #include "face/transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 29 | #include "test-ip.hpp" |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 30 | #include "tests/limited-io.hpp" |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 31 | #include "factory-test-common.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 32 | |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 33 | #include <boost/mpl/vector.hpp> |
| 34 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 35 | namespace nfd { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 36 | namespace tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 40 | using nfd::Face; |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 41 | namespace ip = boost::asio::ip; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 42 | |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 43 | typedef boost::mpl::vector<ip::address_v4, |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 44 | ip::address_v6> AddressFamilies; |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 45 | |
| 46 | class UdpChannelFixture : public BaseFixture |
| 47 | { |
| 48 | protected: |
| 49 | UdpChannelFixture() |
| 50 | : nextPort(7050) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | unique_ptr<UdpChannel> |
| 55 | makeChannel(const ip::address& addr, uint16_t port = 0) |
| 56 | { |
| 57 | if (port == 0) |
| 58 | port = nextPort++; |
| 59 | |
| 60 | return make_unique<UdpChannel>(udp::Endpoint(addr, port), time::seconds(2)); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | listen(const ip::address& addr) |
| 65 | { |
| 66 | listenerEp = udp::Endpoint(addr, 7030); |
| 67 | listenerChannel = makeChannel(addr, 7030); |
| 68 | listenerChannel->listen( |
| 69 | [this] (const shared_ptr<Face>& newFace) { |
| 70 | BOOST_REQUIRE(newFace != nullptr); |
| 71 | connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| 72 | listenerFaces.push_back(newFace); |
| 73 | limitedIo.afterOp(); |
| 74 | }, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 75 | &failIfError); |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | connect(UdpChannel& channel) |
| 80 | { |
| 81 | g_io.post([&] { |
| 82 | channel.connect(listenerEp, ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 83 | [this] (const shared_ptr<Face>& newFace) { |
| 84 | BOOST_REQUIRE(newFace != nullptr); |
| 85 | connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| 86 | clientFaces.push_back(newFace); |
| 87 | face::Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello")); |
| 88 | newFace->getTransport()->send(std::move(pkt)); |
| 89 | limitedIo.afterOp(); |
| 90 | }, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 91 | &failIfError); |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 92 | }); |
| 93 | } |
| 94 | |
| 95 | protected: |
| 96 | LimitedIo limitedIo; |
| 97 | udp::Endpoint listenerEp; |
| 98 | unique_ptr<UdpChannel> listenerChannel; |
| 99 | std::vector<shared_ptr<Face>> listenerFaces; |
| 100 | std::vector<shared_ptr<Face>> clientFaces; |
| 101 | |
| 102 | private: |
| 103 | uint16_t nextPort; |
| 104 | }; |
| 105 | |
| 106 | BOOST_FIXTURE_TEST_SUITE(TestUdpChannel, UdpChannelFixture) |
| 107 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, A, AddressFamilies) |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 109 | { |
| 110 | udp::Endpoint ep(A::loopback(), 7050); |
| 111 | auto channel = makeChannel(ep.address(), ep.port()); |
| 112 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep)); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(Listen) |
| 116 | { |
| 117 | auto channel = makeChannel(ip::address_v4()); |
| 118 | BOOST_CHECK_EQUAL(channel->isListening(), false); |
| 119 | |
| 120 | channel->listen(nullptr, nullptr); |
| 121 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 122 | |
| 123 | // listen() is idempotent |
| 124 | BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr)); |
| 125 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 126 | } |
| 127 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, A, AddressFamilies) |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 129 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 130 | auto address = getTestIp<A>(LoopbackAddress::Yes); |
| 131 | SKIP_IF_IP_UNAVAILABLE(address); |
| 132 | this->listen(address); |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 133 | |
| 134 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| 135 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 136 | |
| 137 | auto ch1 = makeChannel(A()); |
| 138 | this->connect(*ch1); |
| 139 | |
| 140 | BOOST_CHECK(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| 141 | |
| 142 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| 143 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 144 | BOOST_CHECK_EQUAL(ch1->isListening(), false); |
| 145 | |
| 146 | auto ch2 = makeChannel(A()); |
| 147 | auto ch3 = makeChannel(A()); |
| 148 | this->connect(*ch2); |
| 149 | this->connect(*ch3); |
| 150 | |
| 151 | BOOST_CHECK(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| 152 | |
| 153 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| 154 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 155 | BOOST_CHECK_EQUAL(ch2->size(), 1); |
| 156 | BOOST_CHECK_EQUAL(ch3->size(), 1); |
| 157 | BOOST_CHECK_EQUAL(clientFaces.size(), 3); |
| 158 | |
| 159 | // check face persistency |
| 160 | for (const auto& face : listenerFaces) { |
| 161 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 162 | } |
| 163 | for (const auto& face : clientFaces) { |
| 164 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 165 | } |
| 166 | |
| 167 | // connect twice to the same endpoint |
| 168 | this->connect(*ch3); |
| 169 | |
| 170 | BOOST_CHECK(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| 173 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 174 | BOOST_CHECK_EQUAL(ch2->size(), 1); |
| 175 | BOOST_CHECK_EQUAL(ch3->size(), 1); |
| 176 | BOOST_CHECK_EQUAL(clientFaces.size(), 4); |
| 177 | BOOST_CHECK_EQUAL(clientFaces.at(2), clientFaces.at(3)); |
| 178 | } |
| 179 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 180 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, A, AddressFamilies) |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 181 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 182 | auto address = getTestIp<A>(LoopbackAddress::Yes); |
| 183 | SKIP_IF_IP_UNAVAILABLE(address); |
| 184 | this->listen(address); |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 185 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 186 | auto clientChannel = makeChannel(A()); |
Spencer Lee | bb9102f | 2016-02-28 19:53:50 -0700 | [diff] [blame] | 187 | this->connect(*clientChannel); |
| 188 | |
| 189 | BOOST_CHECK(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS); |
| 190 | |
| 191 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| 192 | BOOST_CHECK_EQUAL(clientChannel->size(), 1); |
| 193 | |
| 194 | clientFaces.at(0)->close(); |
| 195 | |
| 196 | BOOST_CHECK(limitedIo.run(2, time::seconds(5)) == LimitedIo::EXCEED_OPS); |
| 197 | |
| 198 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 199 | BOOST_CHECK_EQUAL(clientChannel->size(), 0); |
| 200 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 201 | |
| 202 | BOOST_AUTO_TEST_SUITE_END() // TestUdpChannel |
| 203 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 204 | |
| 205 | } // namespace tests |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 206 | } // namespace nfd |