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 |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 56 | createFace(const FaceUri& uri, |
| 57 | ndn::nfd::FacePersistency persistency, |
| 58 | bool wantLocalFieldsEnabled, |
| 59 | const FaceCreatedCallback& onCreated, |
| 60 | const FaceCreationFailedCallback& onFailure) override |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 61 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 62 | BOOST_FAIL("createFace should not be called"); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 65 | std::vector<shared_ptr<const Channel>> |
| 66 | getChannels() const override |
| 67 | { |
| 68 | BOOST_FAIL("getChannels should not be called"); |
| 69 | return {}; |
| 70 | } |
| 71 | |
| 72 | public: |
| 73 | struct ProcessConfigArgs |
| 74 | { |
| 75 | OptionalConfigSection configSection; |
| 76 | bool isDryRun; |
| 77 | }; |
| 78 | std::vector<ProcessConfigArgs> processConfigHistory; |
| 79 | |
| 80 | std::set<std::string> newProvidedSchemes; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(Normal) |
| 84 | { |
| 85 | auto f1 = make_shared<DummyProtocolFactory>(); |
| 86 | auto f2 = make_shared<DummyProtocolFactory>(); |
| 87 | faceSystem.m_factories["f1"] = f1; |
| 88 | faceSystem.m_factories["f2"] = f2; |
| 89 | |
| 90 | const std::string CONFIG = R"CONFIG( |
| 91 | face_system |
| 92 | { |
| 93 | f1 |
| 94 | { |
| 95 | key v1 |
| 96 | } |
| 97 | f2 |
| 98 | { |
| 99 | key v2 |
| 100 | } |
| 101 | } |
| 102 | )CONFIG"; |
| 103 | |
| 104 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 105 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1); |
| 106 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true); |
| 107 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1"); |
| 108 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1); |
| 109 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true); |
| 110 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2"); |
| 111 | |
| 112 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 113 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2); |
| 114 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false); |
| 115 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1"); |
| 116 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2); |
| 117 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false); |
| 118 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2"); |
| 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(OmittedSection) |
| 122 | { |
| 123 | auto f1 = make_shared<DummyProtocolFactory>(); |
| 124 | auto f2 = make_shared<DummyProtocolFactory>(); |
| 125 | faceSystem.m_factories["f1"] = f1; |
| 126 | faceSystem.m_factories["f2"] = f2; |
| 127 | |
| 128 | const std::string CONFIG = R"CONFIG( |
| 129 | face_system |
| 130 | { |
| 131 | f1 |
| 132 | { |
| 133 | } |
| 134 | } |
| 135 | )CONFIG"; |
| 136 | |
| 137 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 138 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1); |
| 139 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true); |
| 140 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1); |
| 141 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true); |
| 142 | BOOST_CHECK(!f2->processConfigHistory.back().configSection); |
| 143 | |
| 144 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 145 | BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2); |
| 146 | BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false); |
| 147 | BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2); |
| 148 | BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false); |
| 149 | BOOST_CHECK(!f2->processConfigHistory.back().configSection); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(UnknownSection) |
| 153 | { |
| 154 | const std::string CONFIG = R"CONFIG( |
| 155 | face_system |
| 156 | { |
| 157 | f0 |
| 158 | { |
| 159 | } |
| 160 | } |
| 161 | )CONFIG"; |
| 162 | |
| 163 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 164 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(ChangeProvidedSchemes) |
| 168 | { |
| 169 | auto f1 = make_shared<DummyProtocolFactory>(); |
| 170 | faceSystem.m_factories["f1"] = f1; |
| 171 | |
| 172 | const std::string CONFIG = R"CONFIG( |
| 173 | face_system |
| 174 | { |
| 175 | f1 |
| 176 | { |
| 177 | } |
| 178 | } |
| 179 | )CONFIG"; |
| 180 | |
| 181 | f1->newProvidedSchemes.insert("s1"); |
| 182 | f1->newProvidedSchemes.insert("s2"); |
| 183 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 184 | BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr); |
| 185 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1.get()); |
| 186 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s2"), f1.get()); |
| 187 | |
| 188 | f1->newProvidedSchemes.erase("s2"); |
| 189 | f1->newProvidedSchemes.insert("s3"); |
| 190 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 191 | BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr); |
| 192 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1.get()); |
| 193 | BOOST_CHECK(faceSystem.getFactoryByScheme("s2") == nullptr); |
| 194 | BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s3"), f1.get()); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 198 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 199 | BOOST_AUTO_TEST_SUITE_END() // TestFaceSystem |
| 200 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 201 | |
| 202 | } // namespace tests |
| 203 | } // namespace face |
| 204 | } // namespace nfd |