blob: c8fa82b897f368dd7205f184defb069229a5895b [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
Eric Newberry65caf202015-12-17 15:08:16 -070031#include <boost/mpl/vector.hpp>
32
Eric Newberryf68f3782015-12-11 00:31:32 -070033namespace nfd {
34namespace face {
35namespace tests {
36
37BOOST_AUTO_TEST_SUITE(Face)
38BOOST_AUTO_TEST_SUITE(TestStreamTransport)
39
Davide Pesavento22fba352017-10-17 15:53:51 -040040using StreamTransportFixtures = boost::mpl::vector<
41 GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(TcpTransportFixture),
42 UnixStreamTransportFixture
43>;
Eric Newberryf68f3782015-12-11 00:31:32 -070044
45BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
46{
Davide Pesavento22fba352017-10-17 15:53:51 -040047 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -070048
49 auto block1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040050 this->transport->send(block1);
Eric Newberryf68f3782015-12-11 00:31:32 -070051 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
52 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
53
54 auto block2 = ndn::encoding::makeStringBlock(301, "world");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040055 this->transport->send(block2);
Eric Newberryf68f3782015-12-11 00:31:32 -070056 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2);
57 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size());
58
59 std::vector<uint8_t> readBuf(block1.size() + block2.size());
60 boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf),
61 [this] (const boost::system::error_code& error, size_t) {
62 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
63 this->limitedIo.afterOp();
64 });
65
Davide Pesavento14e71f02019-03-28 17:35:25 -040066 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -070067
68 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end());
69 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end());
70 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
71}
72
73BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T)
74{
Davide Pesavento22fba352017-10-17 15:53:51 -040075 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -070076
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040077 auto pkt1 = ndn::encoding::makeStringBlock(300, "hello");
78 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
79 this->remoteWrite(buf1);
Eric Newberryf68f3782015-12-11 00:31:32 -070080
81 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040082 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size());
83
84 auto pkt2 = ndn::encoding::makeStringBlock(301, "world!");
85 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
86 this->remoteWrite(buf2);
87
88 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
89 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size() + pkt2.size());
Eric Newberryf68f3782015-12-11 00:31:32 -070090 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040091
92 BOOST_REQUIRE_EQUAL(this->receivedPackets->size(), 2);
93 BOOST_CHECK(this->receivedPackets->at(0).packet == pkt1);
94 BOOST_CHECK(this->receivedPackets->at(1).packet == pkt2);
95 BOOST_CHECK_EQUAL(this->receivedPackets->at(0).endpoint, 0);
96 BOOST_CHECK_EQUAL(this->receivedPackets->at(1).endpoint, 0);
Eric Newberryf68f3782015-12-11 00:31:32 -070097}
98
99BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
100{
Davide Pesavento22fba352017-10-17 15:53:51 -0400101 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700102
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400103 auto pkt = ndn::encoding::makeStringBlock(300, "hello");
Eric Newberryf68f3782015-12-11 00:31:32 -0700104 ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
105 ndn::Buffer buf2(pkt.end() - 2, pkt.end());
106
107 this->remoteWrite(buf1);
108
109 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
110 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
111 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
112 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
113
114 this->remoteWrite(buf2);
115
116 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
117 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
118 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
119 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
120}
121
122BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
123{
Davide Pesavento22fba352017-10-17 15:53:51 -0400124 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700125
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400126 auto pkt1 = ndn::encoding::makeStringBlock(300, "hello");
127 auto pkt2 = ndn::encoding::makeStringBlock(301, "world");
Eric Newberryf68f3782015-12-11 00:31:32 -0700128 ndn::Buffer buf(pkt1.size() + pkt2.size());
Davide Pesavento6d1c1c32017-10-08 21:44:05 -0400129 std::copy(pkt1.begin(), pkt1.end(), buf.begin());
130 std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size());
Eric Newberryf68f3782015-12-11 00:31:32 -0700131
132 this->remoteWrite(buf);
133
134 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
135 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
136 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
137 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
138}
139
140BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
141{
Davide Pesavento22fba352017-10-17 15:53:51 -0400142 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700143
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500144 const std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
145 auto pkt1 = ndn::encoding::makeBinaryBlock(300, ndn::make_span(bytes).subspan(6));
Eric Newberry65caf202015-12-17 15:08:16 -0700146 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
147 BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700148
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500149 auto pkt2 = ndn::encoding::makeBinaryBlock(301, bytes);
Eric Newberry65caf202015-12-17 15:08:16 -0700150 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
151 BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700152
Eric Newberry65caf202015-12-17 15:08:16 -0700153 this->remoteWrite(buf1); // this should succeed
154
155 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
156 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
157 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
158 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
159
160 int nStateChanges = 0;
161 this->transport->afterStateChange.connect(
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400162 [&nStateChanges] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700163 switch (nStateChanges) {
164 case 0:
165 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
166 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
167 break;
168 case 1:
169 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
170 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
171 break;
172 default:
173 BOOST_CHECK(false);
174 }
175 nStateChanges++;
176 });
177
178 this->remoteWrite(buf2, false); // this should fail
179
180 BOOST_CHECK_EQUAL(nStateChanges, 2);
181
182 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
183 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
184 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
Eric Newberryf68f3782015-12-11 00:31:32 -0700185}
186
187BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
188{
Davide Pesavento22fba352017-10-17 15:53:51 -0400189 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700190
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400191 this->transport->afterStateChange.connectSingleShot([] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700192 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
193 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
194 });
Eric Newberryf68f3782015-12-11 00:31:32 -0700195
Eric Newberry65caf202015-12-17 15:08:16 -0700196 this->transport->close();
197
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400198 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700199 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
200 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
201 this->limitedIo.afterOp();
202 });
203
Davide Pesavento14e71f02019-03-28 17:35:25 -0400204 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700205}
206
207BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
208{
Davide Pesavento22fba352017-10-17 15:53:51 -0400209 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700210
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400211 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberryf68f3782015-12-11 00:31:32 -0700212 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400213 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
Eric Newberryf68f3782015-12-11 00:31:32 -0700214 this->limitedIo.afterOp();
215 });
216
217 this->remoteSocket.close();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400218 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700219
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400220 this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400221 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
Eric Newberry65caf202015-12-17 15:08:16 -0700222 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
223 this->limitedIo.afterOp();
224 });
225
Davide Pesavento14e71f02019-03-28 17:35:25 -0400226 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700227}
228
Eric Newberryb49313d2017-12-24 20:22:27 -0700229BOOST_FIXTURE_TEST_CASE_TEMPLATE(SendQueueLength, T, StreamTransportFixtures, T)
230{
231 TRANSPORT_TEST_INIT();
232
233 BOOST_CHECK_EQUAL(this->transport->getSendQueueLength(), 0);
234}
235
Eric Newberryf68f3782015-12-11 00:31:32 -0700236BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
237BOOST_AUTO_TEST_SUITE_END() // Face
238
239} // namespace tests
240} // namespace face
Eric Newberry65caf202015-12-17 15:08:16 -0700241} // namespace nfd