blob: de32a4d29dcddde33964a28afae714fe55f39e07 [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 Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, 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");
50 this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
51 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");
55 this->transport->send(Transport::Packet{Block{block2}}); // make a copy of the block
56 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
77 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
78 ndn::Buffer buf(pkt.begin(), pkt.end());
79 this->remoteWrite(buf);
80
81 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
82 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
83 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
84 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
85}
86
87BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
88{
Davide Pesavento22fba352017-10-17 15:53:51 -040089 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -070090
91 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
92 ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
93 ndn::Buffer buf2(pkt.end() - 2, pkt.end());
94
95 this->remoteWrite(buf1);
96
97 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
98 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
99 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
100 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
101
102 this->remoteWrite(buf2);
103
104 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
105 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
106 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
107 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
108}
109
110BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
111{
Davide Pesavento22fba352017-10-17 15:53:51 -0400112 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700113
114 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
115 Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
116 ndn::Buffer buf(pkt1.size() + pkt2.size());
Davide Pesavento6d1c1c32017-10-08 21:44:05 -0400117 std::copy(pkt1.begin(), pkt1.end(), buf.begin());
118 std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size());
Eric Newberryf68f3782015-12-11 00:31:32 -0700119
120 this->remoteWrite(buf);
121
122 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
123 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
124 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
125 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
126}
127
128BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
129{
Davide Pesavento22fba352017-10-17 15:53:51 -0400130 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700131
Eric Newberry65caf202015-12-17 15:08:16 -0700132 std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
133 Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
134 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
135 BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700136
Eric Newberry65caf202015-12-17 15:08:16 -0700137 Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
138 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
139 BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700140
Eric Newberry65caf202015-12-17 15:08:16 -0700141 this->remoteWrite(buf1); // this should succeed
142
143 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
144 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
145 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
146 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
147
148 int nStateChanges = 0;
149 this->transport->afterStateChange.connect(
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400150 [&nStateChanges] (TransportState oldState, TransportState newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700151 switch (nStateChanges) {
152 case 0:
153 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
154 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
155 break;
156 case 1:
157 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
158 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
159 break;
160 default:
161 BOOST_CHECK(false);
162 }
163 nStateChanges++;
164 });
165
166 this->remoteWrite(buf2, false); // this should fail
167
168 BOOST_CHECK_EQUAL(nStateChanges, 2);
169
170 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
171 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
172 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
Eric Newberryf68f3782015-12-11 00:31:32 -0700173}
174
175BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
176{
Davide Pesavento22fba352017-10-17 15:53:51 -0400177 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700178
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400179 this->transport->afterStateChange.connectSingleShot([] (TransportState oldState, TransportState newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700180 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
181 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
182 });
Eric Newberryf68f3782015-12-11 00:31:32 -0700183
Eric Newberry65caf202015-12-17 15:08:16 -0700184 this->transport->close();
185
186 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
187 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
188 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
189 this->limitedIo.afterOp();
190 });
191
Davide Pesavento14e71f02019-03-28 17:35:25 -0400192 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700193}
194
195BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
196{
Davide Pesavento22fba352017-10-17 15:53:51 -0400197 TRANSPORT_TEST_INIT();
Eric Newberryf68f3782015-12-11 00:31:32 -0700198
199 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
200 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400201 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
Eric Newberryf68f3782015-12-11 00:31:32 -0700202 this->limitedIo.afterOp();
203 });
204
205 this->remoteSocket.close();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400206 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700207
Eric Newberry65caf202015-12-17 15:08:16 -0700208 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400209 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
Eric Newberry65caf202015-12-17 15:08:16 -0700210 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
211 this->limitedIo.afterOp();
212 });
213
Davide Pesavento14e71f02019-03-28 17:35:25 -0400214 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700215}
216
Eric Newberryb49313d2017-12-24 20:22:27 -0700217BOOST_FIXTURE_TEST_CASE_TEMPLATE(SendQueueLength, T, StreamTransportFixtures, T)
218{
219 TRANSPORT_TEST_INIT();
220
221 BOOST_CHECK_EQUAL(this->transport->getSendQueueLength(), 0);
222}
223
Eric Newberryf68f3782015-12-11 00:31:32 -0700224BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
225BOOST_AUTO_TEST_SUITE_END() // Face
226
227} // namespace tests
228} // namespace face
Eric Newberry65caf202015-12-17 15:08:16 -0700229} // namespace nfd