Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 1 | /* -*- 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 Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 29 | #include <boost/mpl/vector.hpp> |
| 30 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Face) |
| 36 | BOOST_AUTO_TEST_SUITE(TestStreamTransport) |
| 37 | |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 38 | typedef boost::mpl::vector<TcpTransportFixture, |
| 39 | UnixStreamTransportFixture |
| 40 | > StreamTransportFixtures; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 41 | |
| 42 | BOOST_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 | |
| 70 | BOOST_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 | |
| 84 | BOOST_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 | |
| 107 | BOOST_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()); |
| 114 | std::copy(pkt1.begin(), pkt1.end(), buf.buf()); |
| 115 | std::copy(pkt2.begin(), pkt2.end(), buf.buf() + pkt1.size()); |
| 116 | |
| 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 | |
| 125 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T) |
| 126 | { |
| 127 | this->initialize(); |
| 128 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 129 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 133 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 134 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 137 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 138 | 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( |
| 147 | [this, &nStateChanges] (TransportState oldState, TransportState newState) { |
| 148 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T) |
| 173 | { |
| 174 | this->initialize(); |
| 175 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 176 | this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 177 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 178 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
| 179 | }); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 180 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 181 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | BOOST_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); |
| 198 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 199 | 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 Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 205 | this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 206 | BOOST_CHECK_EQUAL(oldState, TransportState::FAILED); |
| 207 | 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 Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport |
| 215 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 216 | |
| 217 | } // namespace tests |
| 218 | } // namespace face |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 219 | } // namespace nfd |