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