blob: 89470de28e887a37521aa3576b281f9a9b987951 [file] [log] [blame]
Eric Newberryf68f3782015-12-11 00:31:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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
38typedef boost::mpl::vector<TcpTransportFixture, UnixStreamTransportFixture> StreamTransportFixtures;
39
40BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
41{
42 this->initialize();
43
44 auto block1 = ndn::encoding::makeStringBlock(300, "hello");
45 this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
46 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
47 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
48
49 auto block2 = ndn::encoding::makeStringBlock(301, "world");
50 this->transport->send(Transport::Packet{Block{block2}}); // make a copy of the block
51 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2);
52 BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size());
53
54 std::vector<uint8_t> readBuf(block1.size() + block2.size());
55 boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf),
56 [this] (const boost::system::error_code& error, size_t) {
57 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
58 this->limitedIo.afterOp();
59 });
60
61 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
62
63 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end());
64 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end());
65 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
66}
67
68BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T)
69{
70 this->initialize();
71
72 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
73 ndn::Buffer buf(pkt.begin(), pkt.end());
74 this->remoteWrite(buf);
75
76 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
77 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
78 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
79 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
80}
81
82BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
83{
84 this->initialize();
85
86 Block pkt = ndn::encoding::makeStringBlock(300, "hello");
87 ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
88 ndn::Buffer buf2(pkt.end() - 2, pkt.end());
89
90 this->remoteWrite(buf1);
91
92 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
93 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
94 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
95 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
96
97 this->remoteWrite(buf2);
98
99 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
100 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
101 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
102 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
103}
104
105BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
106{
107 this->initialize();
108
109 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
110 Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
111 ndn::Buffer buf(pkt1.size() + pkt2.size());
112 std::copy(pkt1.begin(), pkt1.end(), buf.buf());
113 std::copy(pkt2.begin(), pkt2.end(), buf.buf() + pkt1.size());
114
115 this->remoteWrite(buf);
116
117 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
118 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
119 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
120 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
121}
122
123BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
124{
125 this->initialize();
126
Eric Newberry65caf202015-12-17 15:08:16 -0700127 std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
128 Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
129 ndn::Buffer buf1(pkt1.begin(), pkt1.end());
130 BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700131
Eric Newberry65caf202015-12-17 15:08:16 -0700132 Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
133 ndn::Buffer buf2(pkt2.begin(), pkt2.end());
134 BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
Eric Newberryf68f3782015-12-11 00:31:32 -0700135
Eric Newberry65caf202015-12-17 15:08:16 -0700136 this->remoteWrite(buf1); // this should succeed
137
138 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
139 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
140 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
141 BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
142
143 int nStateChanges = 0;
144 this->transport->afterStateChange.connect(
145 [this, &nStateChanges] (TransportState oldState, TransportState newState) {
146 switch (nStateChanges) {
147 case 0:
148 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
149 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
150 break;
151 case 1:
152 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
153 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
154 break;
155 default:
156 BOOST_CHECK(false);
157 }
158 nStateChanges++;
159 });
160
161 this->remoteWrite(buf2, false); // this should fail
162
163 BOOST_CHECK_EQUAL(nStateChanges, 2);
164
165 BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
166 BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
167 BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
Eric Newberryf68f3782015-12-11 00:31:32 -0700168}
169
170BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
171{
172 this->initialize();
173
Eric Newberry65caf202015-12-17 15:08:16 -0700174 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
175 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
176 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
177 });
Eric Newberryf68f3782015-12-11 00:31:32 -0700178
Eric Newberry65caf202015-12-17 15:08:16 -0700179 this->transport->close();
180
181 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
182 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
183 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
184 this->limitedIo.afterOp();
185 });
186
187 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700188}
189
190BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
191{
192 this->initialize();
193
194 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
195 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
196 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
197 this->limitedIo.afterOp();
198 });
199
200 this->remoteSocket.close();
201 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
202
Eric Newberry65caf202015-12-17 15:08:16 -0700203 this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
204 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
205 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
206 this->limitedIo.afterOp();
207 });
208
209 BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700210}
211
212BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
213BOOST_AUTO_TEST_SUITE_END() // Face
214
215} // namespace tests
216} // namespace face
Eric Newberry65caf202015-12-17 15:08:16 -0700217} // namespace nfd