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