tests: UnixStreamTransport test suite improvements
refs #3305
Change-Id: I6c6ca52a5e80266920ded811ff45fc568c3e59b9
diff --git a/tests/daemon/face/stream-transport.t.cpp b/tests/daemon/face/stream-transport.t.cpp
new file mode 100644
index 0000000..24b8b43
--- /dev/null
+++ b/tests/daemon/face/stream-transport.t.cpp
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tcp-transport-fixture.hpp"
+#include "unix-stream-transport-fixture.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Face)
+BOOST_AUTO_TEST_SUITE(TestStreamTransport)
+
+typedef boost::mpl::vector<TcpTransportFixture, UnixStreamTransportFixture> StreamTransportFixtures;
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ auto block1 = ndn::encoding::makeStringBlock(300, "hello");
+ this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
+
+ auto block2 = ndn::encoding::makeStringBlock(301, "world");
+ this->transport->send(Transport::Packet{Block{block2}}); // make a copy of the block
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 2);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size() + block2.size());
+
+ std::vector<uint8_t> readBuf(block1.size() + block2.size());
+ boost::asio::async_read(this->remoteSocket, boost::asio::buffer(readBuf),
+ [this] (const boost::system::error_code& error, size_t) {
+ BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+ this->limitedIo.afterOp();
+ });
+
+ BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+ BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.begin() + block1.size(), block1.begin(), block1.end());
+ BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin() + block1.size(), readBuf.end(), block2.begin(), block2.end());
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ Block pkt = ndn::encoding::makeStringBlock(300, "hello");
+ ndn::Buffer buf(pkt.begin(), pkt.end());
+ this->remoteWrite(buf);
+
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
+ BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleSegments, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ Block pkt = ndn::encoding::makeStringBlock(300, "hello");
+ ndn::Buffer buf1(pkt.begin(), pkt.end() - 2);
+ ndn::Buffer buf2(pkt.end() - 2, pkt.end());
+
+ this->remoteWrite(buf1);
+
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
+ BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+
+ this->remoteWrite(buf2);
+
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
+ BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveMultipleBlocks, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
+ Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
+ ndn::Buffer buf(pkt1.size() + pkt2.size());
+ std::copy(pkt1.begin(), pkt1.end(), buf.buf());
+ std::copy(pkt2.begin(), pkt2.end(), buf.buf() + pkt1.size());
+
+ this->remoteWrite(buf);
+
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 2);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf.size());
+ BOOST_CHECK_EQUAL(this->receivedPackets->size(), 2);
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE + 1, 0);
+ Block pkt = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size());
+ ndn::Buffer buf(pkt.begin(), pkt.end());
+
+ this->remoteWrite(buf, false);
+
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
+ BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
+ BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ this->transport->close();
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING);
+
+ this->g_io.poll();
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
+{
+ this->initialize();
+
+ this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+ BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+ BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
+ this->limitedIo.afterOp();
+ });
+
+ this->remoteSocket.close();
+ BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+ this->g_io.poll();
+ BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
+BOOST_AUTO_TEST_SUITE_END() // Face
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
\ No newline at end of file