blob: 6ec274c465d8e70fb5add6d2cc5bca2ece3fccec [file] [log] [blame]
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
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 "mgmt/tables-config-section.hpp"
27#include "fw/forwarder.hpp"
28
29
30#include "tests/test-common.hpp"
31
32namespace nfd {
33namespace tests {
34
35class TablesConfigSectionFixture : protected BaseFixture
36{
37public:
38
39 TablesConfigSectionFixture()
40 : m_cs(m_forwarder.getCs())
41 , m_pit(m_forwarder.getPit())
42 , m_fib(m_forwarder.getFib())
43 , m_strategyChoice(m_forwarder.getStrategyChoice())
44 , m_measurements(m_forwarder.getMeasurements())
45 , m_tablesConfig(m_cs, m_pit, m_fib, m_strategyChoice, m_measurements)
46 {
47 m_tablesConfig.setConfigFile(m_config);
48 }
49
50 void
51 runConfig(const std::string& CONFIG, bool isDryRun)
52 {
53 m_config.parse(CONFIG, isDryRun, "dummy-config");
54 }
55
56 bool
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060057 validateException(const std::runtime_error& exception, const std::string& expectedMsg)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060058 {
59 return exception.what() == expectedMsg;
60 }
61
62protected:
63 Forwarder m_forwarder;
64
65 Cs& m_cs;
66 Pit& m_pit;
67 Fib& m_fib;
68 StrategyChoice& m_strategyChoice;
69 Measurements& m_measurements;
70
71 TablesConfigSection m_tablesConfig;
72 ConfigFile m_config;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060073};
74
75BOOST_FIXTURE_TEST_SUITE(TestTableConfigSection, TablesConfigSectionFixture)
76
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060077BOOST_AUTO_TEST_CASE(ConfigureTablesWithDefaults)
78{
79 const size_t initialLimit = m_cs.getLimit();
80
81 m_tablesConfig.ensureTablesAreConfigured();
82 BOOST_CHECK_NE(initialLimit, m_cs.getLimit());
83}
84
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060085BOOST_AUTO_TEST_CASE(EmptyTablesSection)
86{
87 const std::string CONFIG =
88 "tables\n"
89 "{\n"
90 "}\n";
91
92 const size_t nCsMaxPackets = m_cs.getLimit();
93
94 BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, true));
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060095 BOOST_CHECK_EQUAL(m_cs.getLimit(), nCsMaxPackets);
96
97 BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, false));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060098 BOOST_CHECK_NE(m_cs.getLimit(), nCsMaxPackets);
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060099
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600100 const size_t defaultLimit = m_cs.getLimit();
101
102 m_tablesConfig.ensureTablesAreConfigured();
103 BOOST_CHECK_EQUAL(defaultLimit, m_cs.getLimit());
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600104}
105
106BOOST_AUTO_TEST_CASE(ValidCsMaxPackets)
107{
108 const std::string CONFIG =
109 "tables\n"
110 "{\n"
111 " cs_max_packets 101\n"
112 "}\n";
113
114 BOOST_REQUIRE_NE(m_cs.getLimit(), 101);
115
116 BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, true));
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600117 BOOST_CHECK_NE(m_cs.getLimit(), 101);
118
119 BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, false));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600120 BOOST_CHECK_EQUAL(m_cs.getLimit(), 101);
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600121
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600122 m_tablesConfig.ensureTablesAreConfigured();
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600123 BOOST_CHECK_EQUAL(m_cs.getLimit(), 101);
124}
125
126BOOST_AUTO_TEST_CASE(MissingValueCsMaxPackets)
127{
128 const std::string CONFIG =
129 "tables\n"
130 "{\n"
131 " cs_max_packets\n"
132 "}\n";
133
134 const std::string expectedMsg =
135 "Invalid value for option \"cs_max_packets\" in \"tables\" section";
136
137 BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true),
138 ConfigFile::Error,
139 bind(&TablesConfigSectionFixture::validateException,
140 this, _1, expectedMsg));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600141
142 BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false),
143 ConfigFile::Error,
144 bind(&TablesConfigSectionFixture::validateException,
145 this, _1, expectedMsg));
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600146}
147
148BOOST_AUTO_TEST_CASE(InvalidValueCsMaxPackets)
149{
150 const std::string CONFIG =
151 "tables\n"
152 "{\n"
153 " cs_max_packets invalid\n"
154 "}\n";
155
156 const std::string expectedMsg =
157 "Invalid value for option \"cs_max_packets\" in \"tables\" section";
158
159 BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true),
160 ConfigFile::Error,
161 bind(&TablesConfigSectionFixture::validateException,
162 this, _1, expectedMsg));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600163
164
165 BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false),
166 ConfigFile::Error,
167 bind(&TablesConfigSectionFixture::validateException,
168 this, _1, expectedMsg));
169}
170
171class IgnoreNotTablesSection
172{
173public:
174 void
175 operator()(const std::string& filename,
176 const std::string& sectionName,
177 const ConfigSection& section,
178 bool isDryRun)
179
180 {
181 // Ignore "not_tables" section
182 if (sectionName == "not_tables")
183 {
184 // do nothing
185 }
186 }
187};
188
189BOOST_AUTO_TEST_CASE(MissingTablesSection)
190{
191 const std::string CONFIG =
192 "not_tables\n"
193 "{\n"
194 " some_other_field 0\n"
195 "}\n";
196
197 ConfigFile passiveConfig((IgnoreNotTablesSection()));
198
199 const size_t initialLimit = m_cs.getLimit();
200
201 passiveConfig.parse(CONFIG, true, "dummy-config");
202 BOOST_REQUIRE_EQUAL(initialLimit, m_cs.getLimit());
203
204 passiveConfig.parse(CONFIG, false, "dummy-config");
205 BOOST_REQUIRE_EQUAL(initialLimit, m_cs.getLimit());
206
207 m_tablesConfig.ensureTablesAreConfigured();
208 BOOST_CHECK_NE(initialLimit, m_cs.getLimit());
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600209}
210
211BOOST_AUTO_TEST_SUITE_END()
212
213} // namespace tests
214} // namespace nfd