blob: dc5b5ac4cd89f906deed5701942d6d30c750c08a [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi406deb52017-01-05 20:07:44 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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-factory.hpp"
27
Eric Newberry42602412016-08-27 09:33:18 -070028#include "factory-test-common.hpp"
Junxiao Shi406deb52017-01-05 20:07:44 +000029#include "face-system-fixture.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
31
32namespace nfd {
Junxiao Shi406deb52017-01-05 20:07:44 +000033namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace tests {
35
36#define CHANNEL_PATH1 "unix-stream-test.1.sock"
37#define CHANNEL_PATH2 "unix-stream-test.2.sock"
38
39BOOST_AUTO_TEST_SUITE(Face)
40BOOST_FIXTURE_TEST_SUITE(TestUnixStreamFactory, BaseFixture)
41
42using nfd::Face;
43
Junxiao Shi406deb52017-01-05 20:07:44 +000044BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture)
45
46BOOST_AUTO_TEST_CASE(Normal)
47{
48 const std::string CONFIG = R"CONFIG(
49 face_system
50 {
51 unix
52 {
53 path /tmp/nfd.sock
54 }
55 }
56 )CONFIG";
57
Junxiao Shi1b65ca12017-01-21 23:04:41 +000058 parseConfig(CONFIG, true);
59 parseConfig(CONFIG, false);
Junxiao Shi406deb52017-01-05 20:07:44 +000060
61 auto& factory = this->getFactoryById<UnixStreamFactory>("unix");
62 BOOST_CHECK_EQUAL(factory.getChannels().size(), 1);
63}
64
65BOOST_AUTO_TEST_CASE(Omitted)
66{
67 const std::string CONFIG = R"CONFIG(
68 face_system
69 {
70 }
71 )CONFIG";
72
Junxiao Shi1b65ca12017-01-21 23:04:41 +000073 parseConfig(CONFIG, true);
74 parseConfig(CONFIG, false);
Junxiao Shi406deb52017-01-05 20:07:44 +000075
76 auto& factory = this->getFactoryById<UnixStreamFactory>("unix");
77 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
78}
79
80BOOST_AUTO_TEST_CASE(UnknownOption)
81{
82 const std::string CONFIG = R"CONFIG(
83 face_system
84 {
85 unix
86 {
87 hello
88 }
89 }
90 )CONFIG";
91
92 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
93 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
94}
95
96BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
97
Junxiao Shicde37ad2015-12-24 01:02:05 -070098BOOST_AUTO_TEST_CASE(ChannelMap)
99{
100 UnixStreamFactory factory;
101
102 shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
103 shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
104 BOOST_CHECK_EQUAL(channel1, channel1a);
105 std::string channel1uri = channel1->getUri().toString();
106 BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
107 BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
108 channel1uri.size() - std::string(CHANNEL_PATH1).size());
109
110 shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
111 BOOST_CHECK_NE(channel1, channel2);
112}
113
114BOOST_AUTO_TEST_CASE(GetChannels)
115{
116 UnixStreamFactory factory;
117 BOOST_CHECK(factory.getChannels().empty());
118
119 std::vector<shared_ptr<const Channel>> expectedChannels;
120 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH1));
121 expectedChannels.push_back(factory.createChannel(CHANNEL_PATH2));
122
123 for (const auto& channel : factory.getChannels()) {
124 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), channel);
125 BOOST_REQUIRE(pos != expectedChannels.end());
126 expectedChannels.erase(pos);
127 }
128
129 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
130}
131
132BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
133{
134 UnixStreamFactory factory;
135
Eric Newberry42602412016-08-27 09:33:18 -0700136 createFace(factory,
137 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000138 {},
Eric Newberry42602412016-08-27 09:33:18 -0700139 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700140 false,
Eric Newberry42602412016-08-27 09:33:18 -0700141 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700142
Eric Newberry42602412016-08-27 09:33:18 -0700143 createFace(factory,
144 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000145 {},
Eric Newberry42602412016-08-27 09:33:18 -0700146 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700147 false,
Eric Newberry42602412016-08-27 09:33:18 -0700148 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700149
Eric Newberry42602412016-08-27 09:33:18 -0700150 createFace(factory,
151 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000152 {},
Eric Newberry42602412016-08-27 09:33:18 -0700153 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700154 false,
Eric Newberry42602412016-08-27 09:33:18 -0700155 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700156}
157
158BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamFactory
159BOOST_AUTO_TEST_SUITE_END() // Face
160
161} // namespace tests
Junxiao Shi406deb52017-01-05 20:07:44 +0000162} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700163} // namespace nfd