blob: 9c13653e96381a42895751d8e239d4322fbaad65 [file] [log] [blame]
Eric Newberryf68f3782015-12-11 00:31:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento6d1c1c32017-10-08 21:44:05 -04002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberryf68f3782015-12-11 00:31:32 -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
26#include "tcp-transport-fixture.hpp"
27#include "unix-stream-transport-fixture.hpp"
28
Davide Pesavento22fba352017-10-17 15:53:51 -040029#include "transport-test-common.hpp"
30
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/asio/read.hpp>
Eric Newberry65caf202015-12-17 15:08:16 -070032#include <boost/mpl/vector.hpp>
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::tests {
35
36using namespace nfd::face;
Eric Newberryf68f3782015-12-11 00:31:32 -070037
38BOOST_AUTO_TEST_SUITE(Face)
39BOOST_AUTO_TEST_SUITE(TestStreamTransport)
40
Davide Pesavento22fba352017-10-17 15:53:51 -040041using StreamTransportFixtures = boost::mpl::vector<
42 GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(TcpTransportFixture),
43 UnixStreamTransportFixture
44>;
Eric Newberryf68f3782015-12-11 00:31:32 -070045
46BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
47{
Davide Pesavento22fba352017-10-17 15:53:51 -040048 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -070049
50 auto block1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040051 this->transport->send(block1);
Eric Newberryf68f3782015-12-11 00:31:32 -070052 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
53 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
54
55 auto block2 = ndn::encoding::makeStringBlock(301, "world");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040056 this->transport->send(block2);
Eric Newberryf68f3782015-12-11 00:31:32 -070057 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2);
58 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size());
59
60 std::vector<uint8_t> readBuf(block1.size() + block2.size());
61 boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf),
62 [this] (const boost::system::error_code& error, size_t) {
63 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
64 this->limitedIo.afterOp();
65 });
66
Davide Pesavento14e71f02019-03-28 17:35:25 -040067 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -070068
69 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end());
70 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end());
71 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
72}
73
74BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T)
75{
Davide Pesavento22fba352017-10-17 15:53:51 -040076 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -070077
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040078 auto pkt1 = ndn::encoding::makeStringBlock(300, "hello");
79 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
80 this->remoteWrite(buf1);
Eric Newberryf68f3782015-12-11 00:31:32 -070081
82 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040083 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size());
84
85 auto pkt2 = ndn::encoding::makeStringBlock(301, "world!");
86 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
87 this->remoteWrite(buf2);
88
89 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
90 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size() + pkt2.size());
Eric Newberryf68f3782015-12-11 00:31:32 -070091 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040092
93 BOOST_REQUIRE_EQUAL(this->receivedPackets->size(), 2);
94 BOOST_CHECK(this->receivedPackets->at(0).packet == pkt1);
95 BOOST_CHECK(this->receivedPackets->at(1).packet == pkt2);
96 BOOST_CHECK_EQUAL(this->receivedPackets->at(0).endpoint, 0);
97 BOOST_CHECK_EQUAL(this->receivedPackets->at(1).endpoint, 0);
Eric Newberryf68f3782015-12-11 00:31:32 -070098}
99
100BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
101{
Davide Pesavento22fba352017-10-17 15:53:51 -0400102 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700103
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400104 auto pkt = ndn::encoding::makeStringBlock(300, "hello");
Eric Newberryf68f3782015-12-11 00:31:32 -0700105 ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
106 ndn::Buffer buf2(pkt.end() - 2, pkt.end());
107
108 this->remoteWrite(buf1);
109
110 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
111 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
112 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
113 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
114
115 this->remoteWrite(buf2);
116
117 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
118 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
119 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
120 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
121}
122
123BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
124{
Davide Pesavento22fba352017-10-17 15:53:51 -0400125 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700126
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400127 auto pkt1 = ndn::encoding::makeStringBlock(300, "hello");
128 auto pkt2 = ndn::encoding::makeStringBlock(301, "world");
Eric Newberryf68f3782015-12-11 00:31:32 -0700129 ndn::Buffer buf(pkt1.size() + pkt2.size());
Davide Pesavento6d1c1c32017-10-08 21:44:05 -0400130 std::copy(pkt1.begin(), pkt1.end(), buf.begin());
131 std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size());
Eric Newberryf68f3782015-12-11 00:31:32 -0700132
133 this->remoteWrite(buf);
134
135 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
136 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
137 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
138 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
139}
140
141BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
142{
Davide Pesavento22fba352017-10-17 15:53:51 -0400143 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700144
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500145 const std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
146 auto pkt1 = ndn::encoding::makeBinaryBlock(300, ndn::make_span(bytes).subspan(6));
Eric Newberry65caf202015-12-17 15:08:16 -0700147 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
148 BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700149
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500150 auto pkt2 = ndn::encoding::makeBinaryBlock(301, bytes);
Eric Newberry65caf202015-12-17 15:08:16 -0700151 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
152 BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700153
Eric Newberry65caf202015-12-17 15:08:16 -0700154 this->remoteWrite(buf1); // this should succeed
155
156 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
157 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
158 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
159 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
160
161 int nStateChanges = 0;
162 this->transport->afterStateChange.connect(
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400163 [&nStateChanges] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700164 switch (nStateChanges) {
165 case 0:
166 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
167 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
168 break;
169 case 1:
170 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
171 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
172 break;
173 default:
174 BOOST_CHECK(false);
175 }
176 nStateChanges++;
177 });
178
179 this->remoteWrite(buf2, false); // this should fail
180
181 BOOST_CHECK_EQUAL(nStateChanges, 2);
182
183 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
184 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
185 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
Eric Newberryf68f3782015-12-11 00:31:32 -0700186}
187
188BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
189{
Davide Pesavento22fba352017-10-17 15:53:51 -0400190 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700191
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400192 this->transport->afterStateChange.connectSingleShot([] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700193 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
194 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
195 });
Eric Newberryf68f3782015-12-11 00:31:32 -0700196
Eric Newberry65caf202015-12-17 15:08:16 -0700197 this->transport->close();
198
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400199 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700200 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
201 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
202 this->limitedIo.afterOp();
203 });
204
Davide Pesavento14e71f02019-03-28 17:35:25 -0400205 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700206}
207
208BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
209{
Davide Pesavento22fba352017-10-17 15:53:51 -0400210 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700211
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400212 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberryf68f3782015-12-11 00:31:32 -0700213 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400214 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
Eric Newberryf68f3782015-12-11 00:31:32 -0700215 this->limitedIo.afterOp();
216 });
217
218 this->remoteSocket.close();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400219 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700220
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400221 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400222 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
Eric Newberry65caf202015-12-17 15:08:16 -0700223 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
224 this->limitedIo.afterOp();
225 });
226
Davide Pesavento14e71f02019-03-28 17:35:25 -0400227 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700228}
229
Eric Newberryb49313d2017-12-24 20:22:27 -0700230BOOST_FIXTURE_TEST_CASE_TEMPLATE(SendQueueLength, T, StreamTransportFixtures, T)
231{
232 TRANSPORT_TEST_INIT();
233
234 BOOST_CHECK_EQUAL(this->transport->getSendQueueLength(), 0);
235}
236
Eric Newberryf68f3782015-12-11 00:31:32 -0700237BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
238BOOST_AUTO_TEST_SUITE_END() // Face
239
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400240} // namespace nfd::tests