blob: 1ecf33a5e0fce3c67c5c3b2898ce81c274fc15b2 [file] [log] [blame]
Junxiao Shib8590312016-12-29 21:22:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0ba6d642017-07-17 00:53:22 +00002/*
Eric Newberry0c841642018-01-17 15:01:00 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shib8590312016-12-29 21:22:25 +00004 * 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 Shi38b24c72017-01-05 02:59:31 +000027#include "face-system-fixture.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000028
Junxiao Shib8590312016-12-29 21:22:25 +000029#include "tests/test-common.hpp"
30
31namespace nfd {
32namespace face {
33namespace tests {
34
Junxiao Shi38b24c72017-01-05 02:59:31 +000035BOOST_AUTO_TEST_SUITE(Face)
36BOOST_FIXTURE_TEST_SUITE(TestFaceSystem, FaceSystemFixture)
37
38BOOST_AUTO_TEST_SUITE(ProcessConfig)
39
40class DummyProtocolFactory : public ProtocolFactory
Junxiao Shib8590312016-12-29 21:22:25 +000041{
42public:
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043 DummyProtocolFactory(const CtorParams& params)
44 : ProtocolFactory(params)
45 {
46 }
47
Junxiao Shi38b24c72017-01-05 02:59:31 +000048 void
49 processConfig(OptionalConfigSection configSection,
50 FaceSystem::ConfigContext& context) override
Junxiao Shib8590312016-12-29 21:22:25 +000051 {
Eric Newberry0c841642018-01-17 15:01:00 -070052 processConfigHistory.push_back({configSection, context.isDryRun,
53 context.generalConfig.wantCongestionMarking});
Junxiao Shi38b24c72017-01-05 02:59:31 +000054 if (!context.isDryRun) {
55 this->providedSchemes = this->newProvidedSchemes;
56 }
Junxiao Shib8590312016-12-29 21:22:25 +000057 }
58
59 void
Eric Newberry944f38b2017-07-20 20:54:22 -040060 createFace(const CreateFaceParams& params,
Junxiao Shi38b24c72017-01-05 02:59:31 +000061 const FaceCreatedCallback& onCreated,
62 const FaceCreationFailedCallback& onFailure) override
Junxiao Shib8590312016-12-29 21:22:25 +000063 {
Junxiao Shi38b24c72017-01-05 02:59:31 +000064 BOOST_FAIL("createFace should not be called");
Junxiao Shib8590312016-12-29 21:22:25 +000065 }
66
Junxiao Shi38b24c72017-01-05 02:59:31 +000067 std::vector<shared_ptr<const Channel>>
68 getChannels() const override
69 {
70 BOOST_FAIL("getChannels should not be called");
71 return {};
72 }
73
74public:
75 struct ProcessConfigArgs
76 {
77 OptionalConfigSection configSection;
78 bool isDryRun;
Eric Newberry0c841642018-01-17 15:01:00 -070079 bool wantCongestionMarking;
Junxiao Shi38b24c72017-01-05 02:59:31 +000080 };
81 std::vector<ProcessConfigArgs> processConfigHistory;
82
83 std::set<std::string> newProvidedSchemes;
Junxiao Shib8590312016-12-29 21:22:25 +000084};
85
Junxiao Shi38b24c72017-01-05 02:59:31 +000086BOOST_AUTO_TEST_CASE(Normal)
87{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000088 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
89 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +000090 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
91 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +000092
93 const std::string CONFIG = R"CONFIG(
94 face_system
95 {
Eric Newberry0c841642018-01-17 15:01:00 -070096 general
97 {
98 enable_congestion_marking yes
99 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000100 f1
101 {
102 key v1
103 }
104 f2
105 {
106 key v2
107 }
108 }
109 )CONFIG";
110
Junxiao Shib47247d2017-01-24 15:09:16 +0000111 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000112 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1);
Eric Newberry0c841642018-01-17 15:01:00 -0700113 BOOST_CHECK(f1->processConfigHistory.back().isDryRun);
114 BOOST_CHECK(f1->processConfigHistory.back().wantCongestionMarking);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000115 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1");
116 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1);
Eric Newberry0c841642018-01-17 15:01:00 -0700117 BOOST_CHECK(f2->processConfigHistory.back().isDryRun);
118 BOOST_CHECK(f2->processConfigHistory.back().wantCongestionMarking);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000119 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2");
120
Junxiao Shib47247d2017-01-24 15:09:16 +0000121 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000122 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2);
Eric Newberry0c841642018-01-17 15:01:00 -0700123 BOOST_CHECK(!f1->processConfigHistory.back().isDryRun);
124 BOOST_CHECK(f1->processConfigHistory.back().wantCongestionMarking);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000125 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1");
126 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2);
Eric Newberry0c841642018-01-17 15:01:00 -0700127 BOOST_CHECK(!f2->processConfigHistory.back().isDryRun);
128 BOOST_CHECK(f2->processConfigHistory.back().wantCongestionMarking);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000129 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2");
130}
131
132BOOST_AUTO_TEST_CASE(OmittedSection)
133{
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000134 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
135 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +0000136 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
137 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000138
139 const std::string CONFIG = R"CONFIG(
140 face_system
141 {
142 f1
143 {
144 }
145 }
146 )CONFIG";
147
Junxiao Shib47247d2017-01-24 15:09:16 +0000148 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000149 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 Shib47247d2017-01-24 15:09:16 +0000155 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000156 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
163BOOST_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
178BOOST_AUTO_TEST_CASE(ChangeProvidedSchemes)
179{
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000180 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +0000181 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000182
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 Shib47247d2017-01-24 15:09:16 +0000194 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000195 BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000196 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1);
197 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s2"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000198
199 f1->newProvidedSchemes.erase("s2");
200 f1->newProvidedSchemes.insert("s3");
Junxiao Shib47247d2017-01-24 15:09:16 +0000201 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000202 BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000203 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000204 BOOST_CHECK(faceSystem.getFactoryByScheme("s2") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000205 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s3"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000206}
207
208BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
209
Junxiao Shib8590312016-12-29 21:22:25 +0000210BOOST_AUTO_TEST_SUITE_END() // TestFaceSystem
211BOOST_AUTO_TEST_SUITE_END() // Mgmt
212
213} // namespace tests
214} // namespace face
215} // namespace nfd