blob: a74b760dae9d4216c64c4f2d9695c934583a122c [file] [log] [blame]
Junxiao Shi13546112015-10-14 19:33:07 -07001/* -*- 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 "face/unix-stream-transport.hpp"
Eric Newberry8717e872015-11-23 12:41:50 -070027#include "transport-test-common.hpp"
Junxiao Shi13546112015-10-14 19:33:07 -070028
Junxiao Shi13546112015-10-14 19:33:07 -070029#include <boost/filesystem.hpp>
30
31namespace nfd {
32namespace face {
33namespace tests {
34
35using namespace nfd::tests;
36typedef boost::asio::local::stream_protocol unix_stream;
37
38BOOST_AUTO_TEST_SUITE(Face)
39BOOST_FIXTURE_TEST_SUITE(TestUnixStreamTransport, BaseFixture)
40
41/** \brief automatically unlinks the socket file of a Unix stream acceptor
42 */
43class AcceptorWithCleanup : public unix_stream::acceptor
44{
45public:
46 AcceptorWithCleanup(const std::string& path = "")
47 : unix_stream::acceptor(getGlobalIoService())
48 {
49 this->open();
50 if (path.empty()) {
51 this->bind("unix-stream-acceptor." + to_string(time::system_clock::now().time_since_epoch().count()) + ".sock");
52 }
53 else {
54 this->bind(path);
55 }
56 this->listen(1);
57 }
58
59 ~AcceptorWithCleanup()
60 {
61 std::string path = this->local_endpoint().path();
62
63 boost::system::error_code ec;
64 this->close(ec);
65 boost::filesystem::remove(path, ec);
66 }
67};
68
69bool
70connectToAcceptor(unix_stream::acceptor& acceptor, unix_stream::socket& sock1, unix_stream::socket& sock2)
71{
72 bool isAccepted = false;
73 acceptor.async_accept(sock1, bind([&isAccepted] { isAccepted = true; }));
74
75 bool isConnected = false;
76 sock2.async_connect(acceptor.local_endpoint(), bind([&isConnected] { isConnected = true; }));
77
78 getGlobalIoService().poll();
79
80 return isAccepted && isConnected;
81}
82
83BOOST_AUTO_TEST_CASE(StaticProperties)
84{
85 AcceptorWithCleanup acceptor1;
86 unix_stream::socket sock1(getGlobalIoService());
87 unix_stream::socket sock2(getGlobalIoService());
88 BOOST_CHECK(connectToAcceptor(acceptor1, sock1, sock2));
89
90 UnixStreamTransport transport(std::move(sock1));
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070091 checkStaticPropertiesInitialized(transport);
Junxiao Shi13546112015-10-14 19:33:07 -070092
93 BOOST_CHECK_EQUAL(transport.getLocalUri().getScheme(), "unix");
94 BOOST_CHECK_EQUAL(transport.getLocalUri().getHost(), "");
95 BOOST_CHECK_EQUAL(transport.getLocalUri().getPath(), acceptor1.local_endpoint().path());
96 BOOST_CHECK_EQUAL(transport.getRemoteUri().getScheme(), "fd");
97 BOOST_CHECK_EQUAL(transport.getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
98 BOOST_CHECK_EQUAL(transport.getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
99 BOOST_CHECK_EQUAL(transport.getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
100 BOOST_CHECK_EQUAL(transport.getMtu(), MTU_UNLIMITED);
101}
102
103BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamTransport
104BOOST_AUTO_TEST_SUITE_END() // Face
105
106} // namespace tests
107} // namespace face
108} // namespace nfd