blob: 1f0a62fd0998be8ed47ce50535add83f5ad3e72b [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0ba6d642017-07-17 00:53:22 +00002/*
Davide Pesavento4c957712024-01-01 15:40:06 -05003 * Copyright (c) 2014-2024, 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
Junxiao Shi406deb52017-01-05 20:07:44 +000028#include "face-system-fixture.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040029#include "factory-test-common.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030
Davide Pesavento4c957712024-01-01 15:40:06 -050031#include <boost/filesystem.hpp>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
34
Davide Pesavento4c957712024-01-01 15:40:06 -050035using UnixStreamFactoryFixture = FaceSystemFactoryFixture<face::UnixStreamFactory>;
Junxiao Shi0ba6d642017-07-17 00:53:22 +000036
Junxiao Shicde37ad2015-12-24 01:02:05 -070037BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000038BOOST_FIXTURE_TEST_SUITE(TestUnixStreamFactory, UnixStreamFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -070039
Junxiao Shi0ba6d642017-07-17 00:53:22 +000040BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi406deb52017-01-05 20:07:44 +000041
Davide Pesavento4c957712024-01-01 15:40:06 -050042BOOST_AUTO_TEST_CASE(AbsolutePath)
Junxiao Shi406deb52017-01-05 20:07:44 +000043{
44 const std::string CONFIG = R"CONFIG(
45 face_system
46 {
47 unix
48 {
Davide Pesavento494a9552018-02-04 22:16:05 -050049 path /tmp/nfd-test.sock
Junxiao Shi406deb52017-01-05 20:07:44 +000050 }
51 }
52 )CONFIG";
53
Junxiao Shi1b65ca12017-01-21 23:04:41 +000054 parseConfig(CONFIG, true);
55 parseConfig(CONFIG, false);
Junxiao Shi406deb52017-01-05 20:07:44 +000056
Davide Pesavento494a9552018-02-04 22:16:05 -050057 BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 1);
Davide Pesavento4c957712024-01-01 15:40:06 -050058 BOOST_TEST(factory.getChannels().front()->isListening());
59
Davide Pesavento494a9552018-02-04 22:16:05 -050060 const auto& uri = factory.getChannels().front()->getUri();
Davide Pesavento4c957712024-01-01 15:40:06 -050061 BOOST_TEST(uri.getScheme() == "unix");
62 boost::filesystem::path path(uri.getPath());
63 BOOST_TEST(path.filename() == "nfd-test.sock");
64 BOOST_TEST(boost::filesystem::canonical(path) == path); // path should already be in canonical form
65 BOOST_TEST(boost::filesystem::equivalent(path, "/tmp/nfd-test.sock"));
66}
67
68BOOST_AUTO_TEST_CASE(RelativePath)
69{
70 const std::string CONFIG = R"CONFIG(
71 face_system
72 {
73 unix
74 {
75 path nfd-test.sock
76 }
77 }
78 )CONFIG";
79
80 parseConfig(CONFIG, true);
81 parseConfig(CONFIG, false);
82
83 BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 1);
84 BOOST_TEST(factory.getChannels().front()->isListening());
85
86 const auto& uri = factory.getChannels().front()->getUri();
87 BOOST_TEST(uri.getScheme() == "unix");
88 boost::filesystem::path path(uri.getPath());
89 BOOST_TEST(path.filename() == "nfd-test.sock");
90 BOOST_TEST(boost::filesystem::canonical(path) == path); // path should already be in canonical form
91 BOOST_TEST(boost::filesystem::equivalent(path, "nfd-test.sock"));
Junxiao Shi406deb52017-01-05 20:07:44 +000092}
93
94BOOST_AUTO_TEST_CASE(Omitted)
95{
96 const std::string CONFIG = R"CONFIG(
97 face_system
98 {
99 }
100 )CONFIG";
101
Junxiao Shi1b65ca12017-01-21 23:04:41 +0000102 parseConfig(CONFIG, true);
103 parseConfig(CONFIG, false);
Junxiao Shi406deb52017-01-05 20:07:44 +0000104
Junxiao Shi406deb52017-01-05 20:07:44 +0000105 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
106}
107
108BOOST_AUTO_TEST_CASE(UnknownOption)
109{
110 const std::string CONFIG = R"CONFIG(
111 face_system
112 {
113 unix
114 {
115 hello
116 }
117 }
118 )CONFIG";
119
120 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
121 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
122}
123
124BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
125
Davide Pesavento4c957712024-01-01 15:40:06 -0500126const std::string CHANNEL_PATH1("unix-stream-test.1.sock");
127const std::string CHANNEL_PATH2("unix-stream-test.2.sock");
128
Davide Pesaventob15276f2017-07-15 16:27:13 -0400129BOOST_AUTO_TEST_CASE(GetChannels)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700130{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400131 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700132
Davide Pesaventob15276f2017-07-15 16:27:13 -0400133 std::set<std::string> expected;
134 expected.insert(factory.createChannel(CHANNEL_PATH1)->getUri().toString());
135 expected.insert(factory.createChannel(CHANNEL_PATH2)->getUri().toString());
136 checkChannelListEqual(factory, expected);
137}
138
139BOOST_AUTO_TEST_CASE(CreateChannel)
140{
Davide Pesavento4c957712024-01-01 15:40:06 -0500141 auto channel1 = factory.createChannel("./" + CHANNEL_PATH1); // test path normalization
Davide Pesaventob15276f2017-07-15 16:27:13 -0400142 auto channel1a = factory.createChannel(CHANNEL_PATH1);
Davide Pesavento4c957712024-01-01 15:40:06 -0500143 auto channel1b = factory.createChannel(boost::filesystem::absolute(CHANNEL_PATH1).string());
144 auto channel1c = factory.createChannel("foo//../" + CHANNEL_PATH1);
Davide Pesaventob15276f2017-07-15 16:27:13 -0400145 BOOST_CHECK_EQUAL(channel1, channel1a);
Davide Pesavento4c957712024-01-01 15:40:06 -0500146 BOOST_CHECK_EQUAL(channel1, channel1b);
147 BOOST_CHECK_EQUAL(channel1, channel1c);
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400148 BOOST_CHECK_EQUAL(factory.getChannels().size(), 1);
Davide Pesavento494a9552018-02-04 22:16:05 -0500149
150 const auto& uri = channel1->getUri();
Davide Pesavento4c957712024-01-01 15:40:06 -0500151 BOOST_TEST_INFO_SCOPE(uri);
Davide Pesavento494a9552018-02-04 22:16:05 -0500152 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
153 BOOST_CHECK_EQUAL(uri.getHost(), "");
154 BOOST_CHECK_EQUAL(uri.getPort(), "");
Davide Pesavento4c957712024-01-01 15:40:06 -0500155 boost::filesystem::path path1(uri.getPath());
156 BOOST_TEST(path1.filename() == CHANNEL_PATH1);
157 BOOST_TEST(path1.is_absolute()); // path should always be absolute
158 BOOST_TEST(path1.lexically_normal() == path1); // path should be in normal form
Davide Pesaventob15276f2017-07-15 16:27:13 -0400159
160 auto channel2 = factory.createChannel(CHANNEL_PATH2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700161 BOOST_CHECK_NE(channel1, channel2);
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400162 BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700163}
164
Davide Pesaventoa9ec67c2023-10-19 20:04:04 -0400165BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700166{
Eric Newberry42602412016-08-27 09:33:18 -0700167 createFace(factory,
168 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000169 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700170 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700171 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700172
Eric Newberry42602412016-08-27 09:33:18 -0700173 createFace(factory,
174 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000175 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700176 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700177 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178
Eric Newberry42602412016-08-27 09:33:18 -0700179 createFace(factory,
180 FaceUri("unix:///var/run/nfd.sock"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000181 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700182 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700183 {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700184}
185
186BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamFactory
187BOOST_AUTO_TEST_SUITE_END() // Face
188
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400189} // namespace nfd::tests