blob: 1438b883c453208c5cf705b46a72ee25ae992bcc [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spencer Leebb9102f2016-02-28 19:53:50 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -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
Junxiao Shicde37ad2015-12-24 01:02:05 -070026#include "face/udp-channel.hpp"
Spencer Leebb9102f2016-02-28 19:53:50 -070027#include "face/transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070028
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020029#include "test-ip.hpp"
Spencer Leebb9102f2016-02-28 19:53:50 -070030#include "tests/limited-io.hpp"
Eric Newberry42602412016-08-27 09:33:18 -070031#include "factory-test-common.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032
Spencer Leebb9102f2016-02-28 19:53:50 -070033#include <boost/mpl/vector.hpp>
34
Eric Newberrya98bf932015-09-21 00:58:47 -070035namespace nfd {
Junxiao Shicde37ad2015-12-24 01:02:05 -070036namespace tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070037
Junxiao Shicde37ad2015-12-24 01:02:05 -070038BOOST_AUTO_TEST_SUITE(Face)
Eric Newberrya98bf932015-09-21 00:58:47 -070039
Junxiao Shicde37ad2015-12-24 01:02:05 -070040using nfd::Face;
Spencer Leebb9102f2016-02-28 19:53:50 -070041namespace ip = boost::asio::ip;
Junxiao Shicde37ad2015-12-24 01:02:05 -070042
Spencer Leebb9102f2016-02-28 19:53:50 -070043typedef boost::mpl::vector<ip::address_v4,
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020044 ip::address_v6> AddressFamilies;
Spencer Leebb9102f2016-02-28 19:53:50 -070045
46class UdpChannelFixture : public BaseFixture
47{
48protected:
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 Newberry42602412016-08-27 09:33:18 -070075 &failIfError);
Spencer Leebb9102f2016-02-28 19:53:50 -070076 }
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 Newberry42602412016-08-27 09:33:18 -070091 &failIfError);
Spencer Leebb9102f2016-02-28 19:53:50 -070092 });
93 }
94
95protected:
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
102private:
103 uint16_t nextPort;
104};
105
106BOOST_FIXTURE_TEST_SUITE(TestUdpChannel, UdpChannelFixture)
107
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200108BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, A, AddressFamilies)
Spencer Leebb9102f2016-02-28 19:53:50 -0700109{
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
115BOOST_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 Pesaventoeee53aa2016-04-11 17:20:21 +0200128BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, A, AddressFamilies)
Spencer Leebb9102f2016-02-28 19:53:50 -0700129{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200130 auto address = getTestIp<A>(LoopbackAddress::Yes);
131 SKIP_IF_IP_UNAVAILABLE(address);
132 this->listen(address);
Spencer Leebb9102f2016-02-28 19:53:50 -0700133
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 Pesaventoeee53aa2016-04-11 17:20:21 +0200180BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, A, AddressFamilies)
Spencer Leebb9102f2016-02-28 19:53:50 -0700181{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200182 auto address = getTestIp<A>(LoopbackAddress::Yes);
183 SKIP_IF_IP_UNAVAILABLE(address);
184 this->listen(address);
Spencer Leebb9102f2016-02-28 19:53:50 -0700185
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200186 auto clientChannel = makeChannel(A());
Spencer Leebb9102f2016-02-28 19:53:50 -0700187 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 Shicde37ad2015-12-24 01:02:05 -0700201
202BOOST_AUTO_TEST_SUITE_END() // TestUdpChannel
203BOOST_AUTO_TEST_SUITE_END() // Face
204
205} // namespace tests
Eric Newberrya98bf932015-09-21 00:58:47 -0700206} // namespace nfd