Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 6d1c1c3 | 2017-10-08 21:44:05 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 29 | #include "transport-test-common.hpp" |
| 30 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 31 | #include <boost/mpl/vector.hpp> |
| 32 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 33 | namespace nfd { |
| 34 | namespace face { |
| 35 | namespace tests { |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Face) |
| 38 | BOOST_AUTO_TEST_SUITE(TestStreamTransport) |
| 39 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 40 | using StreamTransportFixtures = boost::mpl::vector< |
| 41 | GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(TcpTransportFixture), |
| 42 | UnixStreamTransportFixture |
| 43 | >; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 44 | |
| 45 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T) |
| 46 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 47 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 48 | |
| 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 | |
| 66 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 67 | |
| 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 | |
| 73 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T) |
| 74 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 75 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 76 | |
| 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 | |
| 87 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T) |
| 88 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 89 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 90 | |
| 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 | |
| 110 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T) |
| 111 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 112 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 113 | |
| 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 Pesavento | 6d1c1c3 | 2017-10-08 21:44:05 -0400 | [diff] [blame] | 117 | std::copy(pkt1.begin(), pkt1.end(), buf.begin()); |
| 118 | std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size()); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 119 | |
| 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 | |
| 128 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T) |
| 129 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 130 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 131 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 132 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 136 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 137 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 140 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 141 | 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 Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 150 | [&nStateChanges] (TransportState oldState, TransportState newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 151 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T) |
| 176 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 177 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 178 | |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 179 | this->transport->afterStateChange.connectSingleShot([] (TransportState oldState, TransportState newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 181 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
| 182 | }); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 183 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 184 | 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 | |
| 192 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T) |
| 196 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 197 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 198 | |
| 199 | this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 200 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 202 | this->limitedIo.afterOp(); |
| 203 | }); |
| 204 | |
| 205 | this->remoteSocket.close(); |
| 206 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 207 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 208 | this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 211 | this->limitedIo.afterOp(); |
| 212 | }); |
| 213 | |
| 214 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport |
| 218 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 219 | |
| 220 | } // namespace tests |
| 221 | } // namespace face |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 222 | } // namespace nfd |