blob: 1e4624046c346593dbe76c7d8abde37355299234 [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/*
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, 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 {
Junxiao Shi38b24c72017-01-05 02:59:31 +000052 processConfigHistory.push_back({configSection, context.isDryRun});
53 if (!context.isDryRun) {
54 this->providedSchemes = this->newProvidedSchemes;
55 }
Junxiao Shib8590312016-12-29 21:22:25 +000056 }
57
58 void
Eric Newberry78e32b02017-04-01 14:34:44 +000059 createFace(const FaceUri& remoteUri,
60 const ndn::optional<FaceUri>& localUri,
Junxiao Shi38b24c72017-01-05 02:59:31 +000061 ndn::nfd::FacePersistency persistency,
62 bool wantLocalFieldsEnabled,
63 const FaceCreatedCallback& onCreated,
64 const FaceCreationFailedCallback& onFailure) override
Junxiao Shib8590312016-12-29 21:22:25 +000065 {
Junxiao Shi38b24c72017-01-05 02:59:31 +000066 BOOST_FAIL("createFace should not be called");
Junxiao Shib8590312016-12-29 21:22:25 +000067 }
68
Junxiao Shi38b24c72017-01-05 02:59:31 +000069 std::vector<shared_ptr<const Channel>>
70 getChannels() const override
71 {
72 BOOST_FAIL("getChannels should not be called");
73 return {};
74 }
75
76public:
77 struct ProcessConfigArgs
78 {
79 OptionalConfigSection configSection;
80 bool isDryRun;
81 };
82 std::vector<ProcessConfigArgs> processConfigHistory;
83
84 std::set<std::string> newProvidedSchemes;
Junxiao Shib8590312016-12-29 21:22:25 +000085};
86
Junxiao Shi38b24c72017-01-05 02:59:31 +000087BOOST_AUTO_TEST_CASE(Normal)
88{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000089 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
90 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +000091 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
92 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +000093
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 Shib47247d2017-01-24 15:09:16 +0000108 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000109 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 Shib47247d2017-01-24 15:09:16 +0000116 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000117 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
125BOOST_AUTO_TEST_CASE(OmittedSection)
126{
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000127 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
128 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +0000129 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
130 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000131
132 const std::string CONFIG = R"CONFIG(
133 face_system
134 {
135 f1
136 {
137 }
138 }
139 )CONFIG";
140
Junxiao Shib47247d2017-01-24 15:09:16 +0000141 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000142 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 Shib47247d2017-01-24 15:09:16 +0000148 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000149 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
156BOOST_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
171BOOST_AUTO_TEST_CASE(ChangeProvidedSchemes)
172{
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000173 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>(faceSystem.makePFCtorParams());
Junxiao Shib47247d2017-01-24 15:09:16 +0000174 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000175
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 Shib47247d2017-01-24 15:09:16 +0000187 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000188 BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000189 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1);
190 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s2"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000191
192 f1->newProvidedSchemes.erase("s2");
193 f1->newProvidedSchemes.insert("s3");
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);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000197 BOOST_CHECK(faceSystem.getFactoryByScheme("s2") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000198 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s3"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000199}
200
201BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
202
Junxiao Shib8590312016-12-29 21:22:25 +0000203BOOST_AUTO_TEST_SUITE_END() // TestFaceSystem
204BOOST_AUTO_TEST_SUITE_END() // Mgmt
205
206} // namespace tests
207} // namespace face
208} // namespace nfd