blob: dc4965a5382782f6fd4067f08e1da3b9ae21763c [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoac238f22017-09-12 15:19:40 -04002/*
Alexander Afanasyev4400e422021-02-17 11:17:33 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +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
Junxiao Shi331ade72016-08-19 14:07:19 +000026#include "nfdc/strategy-choice-module.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000027
Junxiao Shi5d3e4812017-04-05 16:52:59 +000028#include "execute-command-fixture.hpp"
Junxiao Shi1f481fa2017-01-26 15:14:43 +000029#include "status-fixture.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000030
31namespace nfd {
32namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000033namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000034namespace tests {
35
Junxiao Shi331ade72016-08-19 14:07:19 +000036BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shi5d3e4812017-04-05 16:52:59 +000037BOOST_AUTO_TEST_SUITE(TestStrategyChoiceModule)
38
39class StrategyListFixture : public ExecuteCommandFixture
40{
41protected:
42 bool
43 respondStrategyChoiceDataset(const Interest& interest)
44 {
45 if (!Name("/localhost/nfd/strategy-choice/list").isPrefixOf(interest.getName())) {
46 return false;
47 }
48
49 StrategyChoice entry1;
50 entry1.setName("/");
51 entry1.setStrategy("/strategyP/%FD%01");
52
53 StrategyChoice entry2;
54 entry2.setName("/52VRvpL9/Yqfut4TNHv");
55 entry2.setStrategy("/strategyQ/%FD%02");
56
57 this->sendDataset(interest.getName(), entry1, entry2);
58 return true;
59 }
60};
61
62BOOST_FIXTURE_TEST_SUITE(ListCommand, StrategyListFixture)
63
64BOOST_AUTO_TEST_CASE(Normal)
65{
66 this->processInterest = [this] (const Interest& interest) {
67 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
68 };
69
70 this->execute("strategy list");
71 BOOST_CHECK_EQUAL(exitCode, 0);
72 BOOST_CHECK(out.is_equal("prefix=/ strategy=/strategyP/%FD%01\n"
73 "prefix=/52VRvpL9/Yqfut4TNHv strategy=/strategyQ/%FD%02\n"));
74 BOOST_CHECK(err.is_empty());
75}
76
77BOOST_AUTO_TEST_CASE(ErrorDataset)
78{
79 this->processInterest = nullptr; // no response to dataset or command
80
81 this->execute("strategy list");
82 BOOST_CHECK_EQUAL(exitCode, 1);
83 BOOST_CHECK(out.is_empty());
Eric Newberry359135c2018-06-26 21:02:12 -070084 BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000085}
86
87BOOST_AUTO_TEST_SUITE_END() // ListCommand
88
89BOOST_FIXTURE_TEST_SUITE(ShowCommand, StrategyListFixture)
90
91BOOST_AUTO_TEST_CASE(NormalDefaultStrategy)
92{
93 this->processInterest = [this] (const Interest& interest) {
94 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
95 };
96
97 this->execute("strategy show /I1Ixgg0X");
98 BOOST_CHECK_EQUAL(exitCode, 0);
99 BOOST_CHECK(out.is_equal(" prefix=/\n"
100 "strategy=/strategyP/%FD%01\n"));
101 BOOST_CHECK(err.is_empty());
102}
103
104BOOST_AUTO_TEST_CASE(NormalNonDefaultStrategy)
105{
106 this->processInterest = [this] (const Interest& interest) {
107 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
108 };
109
110 this->execute("strategy show /52VRvpL9/Yqfut4TNHv/Y5gY7gom");
111 BOOST_CHECK_EQUAL(exitCode, 0);
112 BOOST_CHECK(out.is_equal(" prefix=/52VRvpL9/Yqfut4TNHv\n"
113 "strategy=/strategyQ/%FD%02\n"));
114 BOOST_CHECK(err.is_empty());
115}
116
117BOOST_AUTO_TEST_CASE(ErrorDataset)
118{
119 this->processInterest = nullptr; // no response to dataset or command
120
121 this->execute("strategy show /xVoIhNsJ");
122 BOOST_CHECK_EQUAL(exitCode, 1);
123 BOOST_CHECK(out.is_empty());
Eric Newberry359135c2018-06-26 21:02:12 -0700124 BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000125}
126
127BOOST_AUTO_TEST_SUITE_END() // ShowCommand
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000128
Junxiao Shib283f522017-04-06 20:46:15 +0000129BOOST_FIXTURE_TEST_SUITE(SetCommand, ExecuteCommandFixture)
130
131BOOST_AUTO_TEST_CASE(Normal)
132{
133 this->processInterest = [this] (const Interest& interest) {
134 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set");
135 BOOST_REQUIRE(req.hasName());
136 BOOST_CHECK_EQUAL(req.getName(), "/VBXSJg3m/XYs81ARNhx");
137 BOOST_REQUIRE(req.hasStrategy());
138 BOOST_CHECK_EQUAL(req.getStrategy(), "/strategyP");
139
140 ControlParameters resp;
141 resp.setName("/VBXSJg3m/XYs81ARNhx");
142 resp.setStrategy("/strategyP/%FD%05");
143 this->succeedCommand(interest, resp);
144 };
145
146 this->execute("strategy set /VBXSJg3m/XYs81ARNhx /strategyP");
147 BOOST_CHECK_EQUAL(exitCode, 0);
148 BOOST_CHECK(out.is_equal("strategy-set prefix=/VBXSJg3m/XYs81ARNhx strategy=/strategyP/%FD%05\n"));
149 BOOST_CHECK(err.is_empty());
150}
151
152BOOST_AUTO_TEST_CASE(UnknownStrategy)
153{
154 this->processInterest = [this] (const Interest& interest) {
155 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set");
156
157 ControlParameters resp;
158 this->failCommand(interest, 404, "strategy not found");
159 };
160
161 this->execute("strategy set /zezRSP1I /strategyQ");
162 BOOST_CHECK_EQUAL(exitCode, 7);
163 BOOST_CHECK(out.is_empty());
164 BOOST_CHECK(err.is_equal("Unknown strategy: /strategyQ\n"));
165}
166
167BOOST_AUTO_TEST_CASE(ErrorCommand)
168{
169 this->processInterest = nullptr; // no response to command
170
171 this->execute("strategy set /LJdNFfQJ8 /strategyP");
172 BOOST_CHECK_EQUAL(exitCode, 1);
173 BOOST_CHECK(out.is_empty());
174 BOOST_CHECK(err.is_equal("Error 10060 when setting strategy: request timed out\n"));
175}
176
177BOOST_AUTO_TEST_SUITE_END() // SetCommand
178
179BOOST_FIXTURE_TEST_SUITE(UnsetCommand, ExecuteCommandFixture)
180
181BOOST_AUTO_TEST_CASE(Normal)
182{
183 this->processInterest = [this] (const Interest& interest) {
184 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/unset");
185 BOOST_REQUIRE(req.hasName());
186 BOOST_CHECK_EQUAL(req.getName(), "/CFVecryP");
187
188 ControlParameters resp;
189 resp.setName("/CFVecryP");
190 this->succeedCommand(interest, resp);
191 };
192
193 this->execute("strategy unset /CFVecryP");
194 BOOST_CHECK_EQUAL(exitCode, 0);
195 BOOST_CHECK(out.is_equal("strategy-unset prefix=/CFVecryP\n"));
196 BOOST_CHECK(err.is_empty());
197}
198
199BOOST_AUTO_TEST_CASE(CannotUnsetDefault)
200{
Davide Pesaventoac238f22017-09-12 15:19:40 -0400201 this->processInterest = [] (const Interest&) {
Junxiao Shib283f522017-04-06 20:46:15 +0000202 BOOST_ERROR("unexpected command");
203 };
204
205 this->execute("strategy unset /");
206 BOOST_CHECK_EQUAL(exitCode, 2);
207 BOOST_CHECK(out.is_empty());
208 BOOST_CHECK(err.is_equal("Unsetting default strategy is prohibited\n"));
209}
210
211BOOST_AUTO_TEST_CASE(ErrorCommand)
212{
213 this->processInterest = nullptr; // no response to command
214
215 this->execute("strategy unset /GQcEsG96");
216 BOOST_CHECK_EQUAL(exitCode, 1);
217 BOOST_CHECK(out.is_empty());
218 BOOST_CHECK(err.is_equal("Error 10060 when unsetting strategy: request timed out\n"));
219}
220
221BOOST_AUTO_TEST_SUITE_END() // UnsetCommand
222
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000223const std::string STATUS_XML = stripXmlSpaces(R"XML(
224 <strategyChoices>
225 <strategyChoice>
226 <namespace>/</namespace>
227 <strategy>
228 <name>/localhost/nfd/strategy/best-route/%FD%04</name>
229 </strategy>
230 </strategyChoice>
231 <strategyChoice>
232 <namespace>/localhost</namespace>
233 <strategy>
Alexander Afanasyev4400e422021-02-17 11:17:33 -0500234 <name>/localhost/nfd/strategy/multicast/%FD%04</name>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000235 </strategy>
236 </strategyChoice>
237 </strategyChoices>
238)XML");
239
240const std::string STATUS_TEXT = std::string(R"TEXT(
241Strategy choices:
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000242 prefix=/ strategy=/localhost/nfd/strategy/best-route/%FD%04
Alexander Afanasyev4400e422021-02-17 11:17:33 -0500243 prefix=/localhost strategy=/localhost/nfd/strategy/multicast/%FD%04
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000244)TEXT").substr(1);
245
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000246BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<StrategyChoiceModule>)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000247{
248 this->fetchStatus();
249 StrategyChoice payload1;
250 payload1.setName("/")
251 .setStrategy("/localhost/nfd/strategy/best-route/%FD%04");
252 StrategyChoice payload2;
253 payload2.setName("/localhost")
Alexander Afanasyev4400e422021-02-17 11:17:33 -0500254 .setStrategy("/localhost/nfd/strategy/multicast/%FD%04");
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000255 this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2);
256 this->prepareStatusOutput();
257
258 BOOST_CHECK(statusXml.is_equal(STATUS_XML));
259 BOOST_CHECK(statusText.is_equal(STATUS_TEXT));
260}
261
262BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceModule
Junxiao Shi331ade72016-08-19 14:07:19 +0000263BOOST_AUTO_TEST_SUITE_END() // Nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000264
265} // namespace tests
Junxiao Shi331ade72016-08-19 14:07:19 +0000266} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000267} // namespace tools
268} // namespace nfd