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 | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 29 | #include <boost/asio/read.hpp> |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 30 | #include <boost/mp11/list.hpp> |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
| 33 | |
| 34 | using namespace nfd::face; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Face) |
| 37 | BOOST_AUTO_TEST_SUITE(TestStreamTransport) |
| 38 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 39 | using StreamTransportFixtures = boost::mp11::mp_list< |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 40 | GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(TcpTransportFixture), |
| 41 | UnixStreamTransportFixture |
| 42 | >; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 43 | |
| 44 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T) |
| 45 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 46 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 47 | |
| 48 | auto block1 = ndn::encoding::makeStringBlock(300, "hello"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 49 | this->transport->send(block1); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1); |
| 51 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size()); |
| 52 | |
| 53 | auto block2 = ndn::encoding::makeStringBlock(301, "world"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 54 | this->transport->send(block2); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2); |
| 56 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size()); |
| 57 | |
| 58 | std::vector<uint8_t> readBuf(block1.size() + block2.size()); |
| 59 | boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf), |
| 60 | [this] (const boost::system::error_code& error, size_t) { |
| 61 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 62 | this->limitedIo.afterOp(); |
| 63 | }); |
| 64 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 65 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 66 | |
| 67 | BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end()); |
| 68 | BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end()); |
| 69 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 70 | } |
| 71 | |
| 72 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T) |
| 73 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 74 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 76 | auto pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 77 | ndn::Buffer buf1(pkt1.begin(), pkt1.end()); |
| 78 | this->remoteWrite(buf1); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 79 | |
| 80 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size()); |
| 82 | |
| 83 | auto pkt2 = ndn::encoding::makeStringBlock(301, "world!"); |
| 84 | ndn::Buffer buf2(pkt2.begin(), pkt2.end()); |
| 85 | this->remoteWrite(buf2); |
| 86 | |
| 87 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2); |
| 88 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt1.size() + pkt2.size()); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 90 | |
| 91 | BOOST_REQUIRE_EQUAL(this->receivedPackets->size(), 2); |
| 92 | BOOST_CHECK(this->receivedPackets->at(0).packet == pkt1); |
| 93 | BOOST_CHECK(this->receivedPackets->at(1).packet == pkt2); |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 94 | BOOST_CHECK(this->receivedPackets->at(0).endpoint == EndpointId{}); |
| 95 | BOOST_CHECK(this->receivedPackets->at(1).endpoint == EndpointId{}); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T) |
| 99 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 100 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 102 | auto pkt = ndn::encoding::makeStringBlock(300, "hello"); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 103 | ndn::Buffer buf1(pkt.begin(), pkt.end() - 2); |
| 104 | ndn::Buffer buf2(pkt.end() - 2, pkt.end()); |
| 105 | |
| 106 | this->remoteWrite(buf1); |
| 107 | |
| 108 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0); |
| 109 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0); |
| 110 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0); |
| 111 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 112 | |
| 113 | this->remoteWrite(buf2); |
| 114 | |
| 115 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
| 116 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size()); |
| 117 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1); |
| 118 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 119 | } |
| 120 | |
| 121 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T) |
| 122 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 123 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 124 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 125 | auto pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 126 | auto pkt2 = ndn::encoding::makeStringBlock(301, "world"); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 127 | ndn::Buffer buf(pkt1.size() + pkt2.size()); |
Davide Pesavento | 6d1c1c3 | 2017-10-08 21:44:05 -0400 | [diff] [blame] | 128 | std::copy(pkt1.begin(), pkt1.end(), buf.begin()); |
| 129 | std::copy(pkt2.begin(), pkt2.end(), buf.begin() + pkt1.size()); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 130 | |
| 131 | this->remoteWrite(buf); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2); |
| 134 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size()); |
| 135 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2); |
| 136 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 137 | } |
| 138 | |
| 139 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T) |
| 140 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 141 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 143 | const std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0); |
| 144 | auto pkt1 = ndn::encoding::makeBinaryBlock(300, ndn::make_span(bytes).subspan(6)); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 145 | ndn::Buffer buf1(pkt1.begin(), pkt1.end()); |
| 146 | BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 147 | |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 148 | auto pkt2 = ndn::encoding::makeBinaryBlock(301, bytes); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 149 | ndn::Buffer buf2(pkt2.begin(), pkt2.end()); |
| 150 | BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 151 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 152 | this->remoteWrite(buf1); // this should succeed |
| 153 | |
| 154 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
| 155 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size()); |
| 156 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1); |
| 157 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 158 | |
| 159 | int nStateChanges = 0; |
| 160 | this->transport->afterStateChange.connect( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 161 | [&nStateChanges] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 162 | switch (nStateChanges) { |
| 163 | case 0: |
| 164 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 165 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 166 | break; |
| 167 | case 1: |
| 168 | BOOST_CHECK_EQUAL(oldState, TransportState::FAILED); |
| 169 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 170 | break; |
| 171 | default: |
| 172 | BOOST_CHECK(false); |
| 173 | } |
| 174 | nStateChanges++; |
| 175 | }); |
| 176 | |
| 177 | this->remoteWrite(buf2, false); // this should fail |
| 178 | |
| 179 | BOOST_CHECK_EQUAL(nStateChanges, 2); |
| 180 | |
| 181 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
| 182 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size()); |
| 183 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T) |
| 187 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 188 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 189 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 190 | this->transport->afterStateChange.connectSingleShot([] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 192 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
| 193 | }); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 194 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 195 | this->transport->close(); |
| 196 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 197 | this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING); |
| 199 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 200 | this->limitedIo.afterOp(); |
| 201 | }); |
| 202 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 203 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T) |
| 207 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 208 | TRANSPORT_TEST_INIT(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 209 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 210 | this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 212 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 213 | this->limitedIo.afterOp(); |
| 214 | }); |
| 215 | |
| 216 | this->remoteSocket.close(); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 217 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 218 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 219 | this->transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) { |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 220 | BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 221 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 222 | this->limitedIo.afterOp(); |
| 223 | }); |
| 224 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 225 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 228 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(SendQueueLength, T, StreamTransportFixtures, T) |
| 229 | { |
| 230 | TRANSPORT_TEST_INIT(); |
| 231 | |
| 232 | BOOST_CHECK_EQUAL(this->transport->getSendQueueLength(), 0); |
| 233 | } |
| 234 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 235 | BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport |
| 236 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 237 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 238 | } // namespace nfd::tests |