Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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-factory.hpp" |
| 27 | |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 28 | #include "face-system-fixture.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 29 | #include "factory-test-common.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame] | 31 | #include <filesystem> |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
| 34 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 35 | using UnixStreamFactoryFixture = FaceSystemFactoryFixture<face::UnixStreamFactory>; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 36 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Face) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestUnixStreamFactory, UnixStreamFactoryFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 41 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(AbsolutePath) |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 43 | { |
| 44 | const std::string CONFIG = R"CONFIG( |
| 45 | face_system |
| 46 | { |
| 47 | unix |
| 48 | { |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 49 | path /tmp/nfd-test.sock |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | )CONFIG"; |
| 53 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 54 | parseConfig(CONFIG, true); |
| 55 | parseConfig(CONFIG, false); |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 56 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 57 | BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 1); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 58 | BOOST_TEST(factory.getChannels().front()->isListening()); |
| 59 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 60 | const auto& uri = factory.getChannels().front()->getUri(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 61 | BOOST_TEST(uri.getScheme() == "unix"); |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame] | 62 | BOOST_TEST(uri.getPath() == std::filesystem::canonical("/tmp/nfd-test.sock")); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(RelativePath) |
| 66 | { |
| 67 | const std::string CONFIG = R"CONFIG( |
| 68 | face_system |
| 69 | { |
| 70 | unix |
| 71 | { |
| 72 | path nfd-test.sock |
| 73 | } |
| 74 | } |
| 75 | )CONFIG"; |
| 76 | |
| 77 | parseConfig(CONFIG, true); |
| 78 | parseConfig(CONFIG, false); |
| 79 | |
| 80 | BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 1); |
| 81 | BOOST_TEST(factory.getChannels().front()->isListening()); |
| 82 | |
| 83 | const auto& uri = factory.getChannels().front()->getUri(); |
| 84 | BOOST_TEST(uri.getScheme() == "unix"); |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame] | 85 | BOOST_TEST(uri.getPath() == std::filesystem::canonical("nfd-test.sock")); |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(Omitted) |
| 89 | { |
| 90 | const std::string CONFIG = R"CONFIG( |
| 91 | face_system |
| 92 | { |
| 93 | } |
| 94 | )CONFIG"; |
| 95 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 96 | parseConfig(CONFIG, true); |
| 97 | parseConfig(CONFIG, false); |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 98 | |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 103 | { |
| 104 | const std::string CONFIG = R"CONFIG( |
| 105 | face_system |
| 106 | { |
| 107 | unix |
| 108 | { |
| 109 | hello |
| 110 | } |
| 111 | } |
| 112 | )CONFIG"; |
| 113 | |
| 114 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 115 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 119 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 120 | const std::string CHANNEL_PATH1("unix-stream-test.1.sock"); |
| 121 | const std::string CHANNEL_PATH2("unix-stream-test.2.sock"); |
| 122 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(GetChannels) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 124 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 126 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 127 | std::set<std::string> expected; |
| 128 | expected.insert(factory.createChannel(CHANNEL_PATH1)->getUri().toString()); |
| 129 | expected.insert(factory.createChannel(CHANNEL_PATH2)->getUri().toString()); |
| 130 | checkChannelListEqual(factory, expected); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(CreateChannel) |
| 134 | { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 135 | auto channel1 = factory.createChannel("./" + CHANNEL_PATH1); // test path normalization |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 136 | auto channel1a = factory.createChannel(CHANNEL_PATH1); |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame] | 137 | auto channel1b = factory.createChannel(std::filesystem::absolute(CHANNEL_PATH1)); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 138 | auto channel1c = factory.createChannel("foo//../" + CHANNEL_PATH1); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(channel1, channel1b); |
| 141 | BOOST_CHECK_EQUAL(channel1, channel1c); |
Davide Pesavento | a9ec67c | 2023-10-19 20:04:04 -0400 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 1); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 143 | |
| 144 | const auto& uri = channel1->getUri(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 145 | BOOST_TEST_INFO_SCOPE(uri); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 147 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 148 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame] | 149 | std::filesystem::path path1(uri.getPath()); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 150 | BOOST_TEST(path1.filename() == CHANNEL_PATH1); |
| 151 | BOOST_TEST(path1.is_absolute()); // path should always be absolute |
| 152 | BOOST_TEST(path1.lexically_normal() == path1); // path should be in normal form |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 153 | |
| 154 | auto channel2 = factory.createChannel(CHANNEL_PATH2); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 155 | BOOST_CHECK_NE(channel1, channel2); |
Davide Pesavento | a9ec67c | 2023-10-19 20:04:04 -0400 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 2); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Davide Pesavento | a9ec67c | 2023-10-19 20:04:04 -0400 | [diff] [blame] | 159 | BOOST_AUTO_TEST_CASE(CreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 160 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 161 | createFace(factory, |
| 162 | FaceUri("unix:///var/run/nfd.sock"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 163 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 164 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 165 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 166 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 167 | createFace(factory, |
| 168 | FaceUri("unix:///var/run/nfd.sock"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 169 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 170 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 171 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 172 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 173 | createFace(factory, |
| 174 | FaceUri("unix:///var/run/nfd.sock"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 175 | {}, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 176 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 177 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamFactory |
| 181 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 182 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 183 | } // namespace nfd::tests |