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 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | namespace tests { |
| 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(Face) |
| 34 | BOOST_AUTO_TEST_SUITE(TestStreamTransport) |
| 35 | |
| 36 | typedef boost::mpl::vector<TcpTransportFixture, UnixStreamTransportFixture> StreamTransportFixtures; |
| 37 | |
| 38 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T) |
| 39 | { |
| 40 | this->initialize(); |
| 41 | |
| 42 | auto block1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 43 | this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block |
| 44 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1); |
| 45 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size()); |
| 46 | |
| 47 | auto block2 = ndn::encoding::makeStringBlock(301, "world"); |
| 48 | this->transport->send(Transport::Packet{Block{block2}}); // make a copy of the block |
| 49 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2); |
| 50 | BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size()); |
| 51 | |
| 52 | std::vector<uint8_t> readBuf(block1.size() + block2.size()); |
| 53 | boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf), |
| 54 | [this] (const boost::system::error_code& error, size_t) { |
| 55 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 56 | this->limitedIo.afterOp(); |
| 57 | }); |
| 58 | |
| 59 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 60 | |
| 61 | BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end()); |
| 62 | BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end()); |
| 63 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 64 | } |
| 65 | |
| 66 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T) |
| 67 | { |
| 68 | this->initialize(); |
| 69 | |
| 70 | Block pkt = ndn::encoding::makeStringBlock(300, "hello"); |
| 71 | ndn::Buffer buf(pkt.begin(), pkt.end()); |
| 72 | this->remoteWrite(buf); |
| 73 | |
| 74 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
| 75 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size()); |
| 76 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1); |
| 77 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 78 | } |
| 79 | |
| 80 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T) |
| 81 | { |
| 82 | this->initialize(); |
| 83 | |
| 84 | Block pkt = ndn::encoding::makeStringBlock(300, "hello"); |
| 85 | ndn::Buffer buf1(pkt.begin(), pkt.end() - 2); |
| 86 | ndn::Buffer buf2(pkt.end() - 2, pkt.end()); |
| 87 | |
| 88 | this->remoteWrite(buf1); |
| 89 | |
| 90 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0); |
| 91 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0); |
| 92 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0); |
| 93 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 94 | |
| 95 | this->remoteWrite(buf2); |
| 96 | |
| 97 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1); |
| 98 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size()); |
| 99 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1); |
| 100 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 101 | } |
| 102 | |
| 103 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T) |
| 104 | { |
| 105 | this->initialize(); |
| 106 | |
| 107 | Block pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 108 | Block pkt2 = ndn::encoding::makeStringBlock(301, "world"); |
| 109 | ndn::Buffer buf(pkt1.size() + pkt2.size()); |
| 110 | std::copy(pkt1.begin(), pkt1.end(), buf.buf()); |
| 111 | std::copy(pkt2.begin(), pkt2.end(), buf.buf() + pkt1.size()); |
| 112 | |
| 113 | this->remoteWrite(buf); |
| 114 | |
| 115 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2); |
| 116 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size()); |
| 117 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2); |
| 118 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP); |
| 119 | } |
| 120 | |
| 121 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T) |
| 122 | { |
| 123 | this->initialize(); |
| 124 | |
| 125 | std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE + 1, 0); |
| 126 | Block pkt = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size()); |
| 127 | ndn::Buffer buf(pkt.begin(), pkt.end()); |
| 128 | |
| 129 | this->remoteWrite(buf, false); |
| 130 | |
| 131 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0); |
| 132 | BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0); |
| 133 | BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0); |
| 134 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED); |
| 135 | } |
| 136 | |
| 137 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T) |
| 138 | { |
| 139 | this->initialize(); |
| 140 | |
| 141 | this->transport->close(); |
| 142 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING); |
| 143 | |
| 144 | this->g_io.poll(); |
| 145 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED); |
| 146 | } |
| 147 | |
| 148 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T) |
| 149 | { |
| 150 | this->initialize(); |
| 151 | |
| 152 | this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 153 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 154 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 155 | this->limitedIo.afterOp(); |
| 156 | }); |
| 157 | |
| 158 | this->remoteSocket.close(); |
| 159 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 160 | |
| 161 | this->g_io.poll(); |
| 162 | BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport |
| 166 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 167 | |
| 168 | } // namespace tests |
| 169 | } // namespace face |
| 170 | } // namespace nfd |