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