blob: 81044d1f02119efb311d4537724ccf1a93caf128 [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 Pesavento68ab43d2017-07-02 13:37:35 -04003 * Copyright (c) 2014-2017, 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
Eric Newberry65caf202015-12-17 15:08:16 -070029#include <boost/mpl/vector.hpp>
30
Eric Newberryf68f3782015-12-11 00:31:32 -070031namespace nfd {
32namespace face {
33namespace tests {
34
35BOOST_AUTO_TEST_SUITE(Face)
36BOOST_AUTO_TEST_SUITE(TestStreamTransport)
37
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070038typedef boost::mpl::vector<TcpTransportFixture,
39 UnixStreamTransportFixture
40 > StreamTransportFixtures;
Eric Newberryf68f3782015-12-11 00:31:32 -070041
42BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
43{
44 this->initialize();
45
46 auto block1 = ndn::encoding::makeStringBlock(300, "hello");
47 this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
48 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
49 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
50
51 auto block2 = ndn::encoding::makeStringBlock(301, "world");
52 this->transport->send(Transport::Packet{Block{block2}}); // make a copy of the block
53 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2);
54 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size());
55
56 std::vector<uint8_t> readBuf(block1.size() + block2.size());
57 boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf),
58 [this] (const boost::system::error_code& error, size_t) {
59 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
60 this->limitedIo.afterOp();
61 });
62
63 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
64
65 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end());
66 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end());
67 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
68}
69
70BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T)
71{
72 this->initialize();
73
74 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
75 ndn::Buffer buf(pkt.begin(), pkt.end());
76 this->remoteWrite(buf);
77
78 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
79 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
80 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
81 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
82}
83
84BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
85{
86 this->initialize();
87
88 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
89 ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
90 ndn::Buffer buf2(pkt.end() - 2, pkt.end());
91
92 this->remoteWrite(buf1);
93
94 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
95 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
96 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
97 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
98
99 this->remoteWrite(buf2);
100
101 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
102 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
103 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
104 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
105}
106
107BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
108{
109 this->initialize();
110
111 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
112 Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
113 ndn::Buffer buf(pkt1.size() + pkt2.size());
Davide Pesavento6d1c1c32017-10-08 21:44:05 -0400114 std::copy(pkt1.begin(), pkt1.end(), buf.begin());
115 std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size());
Eric Newberryf68f3782015-12-11 00:31:32 -0700116
117 this->remoteWrite(buf);
118
119 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
120 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
121 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
122 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
123}
124
125BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
126{
127 this->initialize();
128
Eric Newberry65caf202015-12-17 15:08:16 -0700129 std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
130 Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
131 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
132 BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700133
Eric Newberry65caf202015-12-17 15:08:16 -0700134 Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
135 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
136 BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700137
Eric Newberry65caf202015-12-17 15:08:16 -0700138 this->remoteWrite(buf1); // this should succeed
139
140 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
141 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
142 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
143 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
144
145 int nStateChanges = 0;
146 this->transport->afterStateChange.connect(
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400147 [&nStateChanges] (TransportState oldState, TransportState newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700148 switch (nStateChanges) {
149 case 0:
150 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
151 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
152 break;
153 case 1:
154 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
155 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
156 break;
157 default:
158 BOOST_CHECK(false);
159 }
160 nStateChanges++;
161 });
162
163 this->remoteWrite(buf2, false); // this should fail
164
165 BOOST_CHECK_EQUAL(nStateChanges, 2);
166
167 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
168 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
169 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
Eric Newberryf68f3782015-12-11 00:31:32 -0700170}
171
172BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
173{
174 this->initialize();
175
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400176 this->transport->afterStateChange.connectSingleShot([] (TransportState oldState, TransportState newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700177 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
178 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
179 });
Eric Newberryf68f3782015-12-11 00:31:32 -0700180
Eric Newberry65caf202015-12-17 15:08:16 -0700181 this->transport->close();
182
183 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
184 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
185 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
186 this->limitedIo.afterOp();
187 });
188
189 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700190}
191
192BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
193{
194 this->initialize();
195
196 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
197 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400198 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
Eric Newberryf68f3782015-12-11 00:31:32 -0700199 this->limitedIo.afterOp();
200 });
201
202 this->remoteSocket.close();
203 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
204
Eric Newberry65caf202015-12-17 15:08:16 -0700205 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400206 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
Eric Newberry65caf202015-12-17 15:08:16 -0700207 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
208 this->limitedIo.afterOp();
209 });
210
211 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700212}
213
214BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
215BOOST_AUTO_TEST_SUITE_END() // Face
216
217} // namespace tests
218} // namespace face
Eric Newberry65caf202015-12-17 15:08:16 -0700219} // namespace nfd