blob: 5b86e9d74f4dcac4b0cec8876f61111c08d9da1f [file] [log] [blame]
Davide Pesavento9a00fab2016-09-27 11:22:46 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Davide Pesavento9a00fab2016-09-27 11:22:46 +02004 * 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 "tcp-channel-fixture.hpp"
27#include "udp-channel-fixture.hpp"
28
Davide Pesavento22fba352017-10-17 15:53:51 -040029#include "test-ip.hpp"
Davide Pesavento9a00fab2016-09-27 11:22:46 +020030#include <boost/mpl/vector.hpp>
31
32namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Davide Pesavento9a00fab2016-09-27 11:22:46 +020034namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Face)
37BOOST_AUTO_TEST_SUITE(TestTcpUdpChannel)
38
Junxiao Shi84d62cb2017-07-12 16:15:18 +000039template<typename F, AddressFamily AF>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020040struct FixtureAndAddress
41{
42 using Fixture = F;
Junxiao Shi84d62cb2017-07-12 16:15:18 +000043 static constexpr AddressFamily ADDRESS_FAMILY = AF;
44 using Address = typename IpAddressFromFamily<AF>::type;
Davide Pesavento9a00fab2016-09-27 11:22:46 +020045};
46
47using FixtureAndAddressList = boost::mpl::vector<
Junxiao Shi84d62cb2017-07-12 16:15:18 +000048 FixtureAndAddress<TcpChannelFixture, AddressFamily::V4>,
49 FixtureAndAddress<TcpChannelFixture, AddressFamily::V6>,
50 FixtureAndAddress<UdpChannelFixture, AddressFamily::V4>,
51 FixtureAndAddress<UdpChannelFixture, AddressFamily::V6>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020052>;
53
54BOOST_FIXTURE_TEST_CASE_TEMPLATE(Uri, T, FixtureAndAddressList, T::Fixture)
55{
56 decltype(this->listenerEp) ep(T::Address::loopback(), 7050);
57 auto channel = this->makeChannel(ep.address(), ep.port());
58 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep));
59}
60
61BOOST_FIXTURE_TEST_CASE_TEMPLATE(Listen, T, FixtureAndAddressList, T::Fixture)
62{
63 auto channel = this->makeChannel(typename T::Address());
64 BOOST_CHECK_EQUAL(channel->isListening(), false);
65
66 channel->listen(nullptr, nullptr);
67 BOOST_CHECK_EQUAL(channel->isListening(), true);
68
69 // listen() is idempotent
70 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
71 BOOST_CHECK_EQUAL(channel->isListening(), true);
72}
73
74BOOST_FIXTURE_TEST_CASE_TEMPLATE(MultipleAccepts, T, FixtureAndAddressList, T::Fixture)
75{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040076 auto address = getTestIp(T::ADDRESS_FAMILY, AddressScope::Loopback);
Davide Pesavento9a00fab2016-09-27 11:22:46 +020077 SKIP_IF_IP_UNAVAILABLE(address);
78 this->listen(address);
79
80 BOOST_CHECK_EQUAL(this->listenerChannel->isListening(), true);
81 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 0);
82
83 auto ch1 = this->makeChannel(typename T::Address());
84 this->connect(*ch1);
85
Davide Pesavento14e71f02019-03-28 17:35:25 -040086 BOOST_CHECK_EQUAL(this->limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesavento9a00fab2016-09-27 11:22:46 +020087
88 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 1);
89 BOOST_CHECK_EQUAL(ch1->size(), 1);
90 BOOST_CHECK_EQUAL(ch1->isListening(), false);
91
92 auto ch2 = this->makeChannel(typename T::Address());
93 auto ch3 = this->makeChannel(typename T::Address());
94 this->connect(*ch2);
95 this->connect(*ch3);
96
Davide Pesavento14e71f02019-03-28 17:35:25 -040097 BOOST_CHECK_EQUAL(this->limitedIo.run(4, 2_s), LimitedIo::EXCEED_OPS);
Davide Pesavento9a00fab2016-09-27 11:22:46 +020098
99 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 3);
100 BOOST_CHECK_EQUAL(ch1->size(), 1);
101 BOOST_CHECK_EQUAL(ch2->size(), 1);
102 BOOST_CHECK_EQUAL(ch3->size(), 1);
103 BOOST_CHECK_EQUAL(this->clientFaces.size(), 3);
104
105 // check face persistency
106 for (const auto& face : this->listenerFaces) {
107 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
108 }
109 for (const auto& face : this->clientFaces) {
110 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
111 }
112
113 // connect twice to the same endpoint
114 this->connect(*ch3);
115
Davide Pesavento14e71f02019-03-28 17:35:25 -0400116 BOOST_CHECK_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200117
118 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 3);
119 BOOST_CHECK_EQUAL(ch1->size(), 1);
120 BOOST_CHECK_EQUAL(ch2->size(), 1);
121 BOOST_CHECK_EQUAL(ch3->size(), 1);
122 BOOST_CHECK_EQUAL(this->clientFaces.size(), 4);
123 BOOST_CHECK_EQUAL(this->clientFaces.at(2), this->clientFaces.at(3));
124}
125
126BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceClosure, T, FixtureAndAddressList, T::Fixture)
127{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400128 auto address = getTestIp(T::ADDRESS_FAMILY, AddressScope::Loopback);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200129 SKIP_IF_IP_UNAVAILABLE(address);
130 this->listen(address);
131
132 auto clientChannel = this->makeChannel(typename T::Address());
133 this->connect(*clientChannel);
134
Davide Pesavento14e71f02019-03-28 17:35:25 -0400135 BOOST_CHECK_EQUAL(this->limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200136
137 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 1);
138 BOOST_CHECK_EQUAL(clientChannel->size(), 1);
139
140 this->clientFaces.at(0)->close();
141
Davide Pesavento14e71f02019-03-28 17:35:25 -0400142 BOOST_CHECK_EQUAL(this->limitedIo.run(2, 5_s), LimitedIo::EXCEED_OPS);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200143
144 BOOST_CHECK_EQUAL(this->listenerChannel->size(), 0);
145 BOOST_CHECK_EQUAL(clientChannel->size(), 0);
146}
147
148BOOST_AUTO_TEST_SUITE_END() // TestTcpUdpChannel
149BOOST_AUTO_TEST_SUITE_END() // Face
150
151} // namespace tests
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400152} // namespace face
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200153} // namespace nfd