blob: 590c0634ea16750bfb945647f443d601223623f1 [file] [log] [blame]
Junxiao Shib8590312016-12-29 21:22:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Shi38b24c72017-01-05 02:59:31 +000043 void
44 processConfig(OptionalConfigSection configSection,
45 FaceSystem::ConfigContext& context) override
Junxiao Shib8590312016-12-29 21:22:25 +000046 {
Junxiao Shi38b24c72017-01-05 02:59:31 +000047 processConfigHistory.push_back({configSection, context.isDryRun});
48 if (!context.isDryRun) {
49 this->providedSchemes = this->newProvidedSchemes;
50 }
Junxiao Shib8590312016-12-29 21:22:25 +000051 }
52
53 void
Eric Newberry78e32b02017-04-01 14:34:44 +000054 createFace(const FaceUri& remoteUri,
55 const ndn::optional<FaceUri>& localUri,
Junxiao Shi38b24c72017-01-05 02:59:31 +000056 ndn::nfd::FacePersistency persistency,
57 bool wantLocalFieldsEnabled,
58 const FaceCreatedCallback& onCreated,
59 const FaceCreationFailedCallback& onFailure) override
Junxiao Shib8590312016-12-29 21:22:25 +000060 {
Junxiao Shi38b24c72017-01-05 02:59:31 +000061 BOOST_FAIL("createFace should not be called");
Junxiao Shib8590312016-12-29 21:22:25 +000062 }
63
Junxiao Shi38b24c72017-01-05 02:59:31 +000064 std::vector<shared_ptr<const Channel>>
65 getChannels() const override
66 {
67 BOOST_FAIL("getChannels should not be called");
68 return {};
69 }
70
71public:
72 struct ProcessConfigArgs
73 {
74 OptionalConfigSection configSection;
75 bool isDryRun;
76 };
77 std::vector<ProcessConfigArgs> processConfigHistory;
78
79 std::set<std::string> newProvidedSchemes;
Junxiao Shib8590312016-12-29 21:22:25 +000080};
81
Junxiao Shi38b24c72017-01-05 02:59:31 +000082BOOST_AUTO_TEST_CASE(Normal)
83{
Junxiao Shib47247d2017-01-24 15:09:16 +000084 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>();
85 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>();
86 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
87 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +000088
89 const std::string CONFIG = R"CONFIG(
90 face_system
91 {
92 f1
93 {
94 key v1
95 }
96 f2
97 {
98 key v2
99 }
100 }
101 )CONFIG";
102
Junxiao Shib47247d2017-01-24 15:09:16 +0000103 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000104 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1);
105 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true);
106 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1");
107 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1);
108 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true);
109 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2");
110
Junxiao Shib47247d2017-01-24 15:09:16 +0000111 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000112 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2);
113 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false);
114 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().configSection->get<std::string>("key"), "v1");
115 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2);
116 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false);
117 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().configSection->get<std::string>("key"), "v2");
118}
119
120BOOST_AUTO_TEST_CASE(OmittedSection)
121{
Junxiao Shib47247d2017-01-24 15:09:16 +0000122 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>();
123 faceSystem.m_factories["f2"] = make_unique<DummyProtocolFactory>();
124 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
125 auto f2 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f2"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000126
127 const std::string CONFIG = R"CONFIG(
128 face_system
129 {
130 f1
131 {
132 }
133 }
134 )CONFIG";
135
Junxiao Shib47247d2017-01-24 15:09:16 +0000136 parseConfig(CONFIG, true);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000137 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 1);
138 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, true);
139 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 1);
140 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, true);
141 BOOST_CHECK(!f2->processConfigHistory.back().configSection);
142
Junxiao Shib47247d2017-01-24 15:09:16 +0000143 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000144 BOOST_REQUIRE_EQUAL(f1->processConfigHistory.size(), 2);
145 BOOST_CHECK_EQUAL(f1->processConfigHistory.back().isDryRun, false);
146 BOOST_REQUIRE_EQUAL(f2->processConfigHistory.size(), 2);
147 BOOST_CHECK_EQUAL(f2->processConfigHistory.back().isDryRun, false);
148 BOOST_CHECK(!f2->processConfigHistory.back().configSection);
149}
150
151BOOST_AUTO_TEST_CASE(UnknownSection)
152{
153 const std::string CONFIG = R"CONFIG(
154 face_system
155 {
156 f0
157 {
158 }
159 }
160 )CONFIG";
161
162 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
163 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
164}
165
166BOOST_AUTO_TEST_CASE(ChangeProvidedSchemes)
167{
Junxiao Shib47247d2017-01-24 15:09:16 +0000168 faceSystem.m_factories["f1"] = make_unique<DummyProtocolFactory>();
169 auto f1 = static_cast<DummyProtocolFactory*>(faceSystem.getFactoryById("f1"));
Junxiao Shi38b24c72017-01-05 02:59:31 +0000170
171 const std::string CONFIG = R"CONFIG(
172 face_system
173 {
174 f1
175 {
176 }
177 }
178 )CONFIG";
179
180 f1->newProvidedSchemes.insert("s1");
181 f1->newProvidedSchemes.insert("s2");
Junxiao Shib47247d2017-01-24 15:09:16 +0000182 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000183 BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000184 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1);
185 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s2"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000186
187 f1->newProvidedSchemes.erase("s2");
188 f1->newProvidedSchemes.insert("s3");
Junxiao Shib47247d2017-01-24 15:09:16 +0000189 parseConfig(CONFIG, false);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000190 BOOST_CHECK(faceSystem.getFactoryByScheme("f1") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000191 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s1"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000192 BOOST_CHECK(faceSystem.getFactoryByScheme("s2") == nullptr);
Junxiao Shib47247d2017-01-24 15:09:16 +0000193 BOOST_CHECK_EQUAL(faceSystem.getFactoryByScheme("s3"), f1);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000194}
195
196BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
197
Junxiao Shib8590312016-12-29 21:22:25 +0000198BOOST_AUTO_TEST_SUITE_END() // TestFaceSystem
199BOOST_AUTO_TEST_SUITE_END() // Mgmt
200
201} // namespace tests
202} // namespace face
203} // namespace nfd