Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [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/face-system.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 27 | #include "face-system-fixture.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 28 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace nfd::tests; |
| 36 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Face) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestFaceSystem, FaceSystemFixture) |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
| 41 | |
| 42 | class DummyProtocolFactory : public ProtocolFactory |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 43 | { |
| 44 | public: |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 45 | void |
| 46 | processConfig(OptionalConfigSection configSection, |
| 47 | FaceSystem::ConfigContext& context) override |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 48 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 49 | processConfigHistory.push_back({configSection, context.isDryRun}); |
| 50 | if (!context.isDryRun) { |
| 51 | this->providedSchemes = this->newProvidedSchemes; |
| 52 | } |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 56 | createFace(const FaceUri& remoteUri, |
| 57 | const ndn::optional<FaceUri>& localUri, |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 58 | ndn::nfd::FacePersistency persistency, |
| 59 | bool wantLocalFieldsEnabled, |
| 60 | const FaceCreatedCallback& onCreated, |
| 61 | const FaceCreationFailedCallback& onFailure) override |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 62 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 63 | BOOST_FAIL("createFace should not be called"); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 66 | std::vector<shared_ptr<const Channel>> |
| 67 | getChannels() const override |
| 68 | { |
| 69 | BOOST_FAIL("getChannels should not be called"); |
| 70 | return {}; |
| 71 | } |
| 72 | |
| 73 | public: |
| 74 | struct ProcessConfigArgs |
| 75 | { |
| 76 | OptionalConfigSection configSection; |
| 77 | bool isDryRun; |
| 78 | }; |
| 79 | std::vector<ProcessConfigArgs> processConfigHistory; |
| 80 | |
| 81 | std::set<std::string> newProvidedSchemes; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(Normal) |
| 85 | { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 86 | faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(); |
| 87 | faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(); |
| 88 | auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1")); |
| 89 | auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2")); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 90 | |
| 91 | const std::string CONFIG = R"CONFIG( |
| 92 | face_system |
| 93 | { |
| 94 | f1 |
| 95 | { |
| 96 | key v1 |
| 97 | } |
| 98 | f2 |
| 99 | { |
| 100 | key v2 |
| 101 | } |
| 102 | } |
| 103 | )CONFIG"; |
| 104 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 105 | parseConfig(CONFIG, true); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 106 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1); |
| 107 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true); |
| 108 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1"); |
| 109 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1); |
| 110 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true); |
| 111 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2"); |
| 112 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 113 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 114 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2); |
| 115 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false); |
| 116 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1"); |
| 117 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2); |
| 118 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false); |
| 119 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2"); |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(OmittedSection) |
| 123 | { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 124 | faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(); |
| 125 | faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(); |
| 126 | auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1")); |
| 127 | auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2")); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 128 | |
| 129 | const std::string CONFIG = R"CONFIG( |
| 130 | face_system |
| 131 | { |
| 132 | f1 |
| 133 | { |
| 134 | } |
| 135 | } |
| 136 | )CONFIG"; |
| 137 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 138 | parseConfig(CONFIG, true); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 139 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1); |
| 140 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true); |
| 141 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1); |
| 142 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true); |
| 143 | BOOST_CHECK(!f2->processConfigHistory.back().configSection); |
| 144 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 145 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 146 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2); |
| 147 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false); |
| 148 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2); |
| 149 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false); |
| 150 | BOOST_CHECK(!f2->processConfigHistory.back().configSection); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(UnknownSection) |
| 154 | { |
| 155 | const std::string CONFIG = R"CONFIG( |
| 156 | face_system |
| 157 | { |
| 158 | f0 |
| 159 | { |
| 160 | } |
| 161 | } |
| 162 | )CONFIG"; |
| 163 | |
| 164 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 165 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(ChangeProvidedSchemes) |
| 169 | { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 170 | faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(); |
| 171 | auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1")); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 172 | |
| 173 | const std::string CONFIG = R"CONFIG( |
| 174 | face_system |
| 175 | { |
| 176 | f1 |
| 177 | { |
| 178 | } |
| 179 | } |
| 180 | )CONFIG"; |
| 181 | |
| 182 | f1->newProvidedSchemes.insert("s1"); |
| 183 | f1->newProvidedSchemes.insert("s2"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 184 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 185 | BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1); |
| 187 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s2"), f1); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 188 | |
| 189 | f1->newProvidedSchemes.erase("s2"); |
| 190 | f1->newProvidedSchemes.insert("s3"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 191 | parseConfig(CONFIG, false); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 192 | BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 194 | BOOST_CHECK(faceSystem.getFactoryByScheme("s2") == nullptr); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s3"), f1); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 199 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 200 | BOOST_AUTO_TEST_SUITE_END() // TestFaceSystem |
| 201 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 202 | |
| 203 | } // namespace tests |
| 204 | } // namespace face |
| 205 | } // namespace nfd |