blob: 6ff8abbc7fbe0e8b46da64b028b3813beffcecfb [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc::tests {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
Junxiao Shi331ade72016-08-19 14:07:19 +000033BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shi5d3e4812017-04-05 16:52:59 +000034BOOST_AUTO_TEST_SUITE(TestStrategyChoiceModule)
35
36class StrategyListFixture : public ExecuteCommandFixture
37{
38protected:
39 bool
40 respondStrategyChoiceDataset(const Interest& interest)
41 {
42 if (!Name("/localhost/nfd/strategy-choice/list").isPrefixOf(interest.getName())) {
43 return false;
44 }
45
46 StrategyChoice entry1;
47 entry1.setName("/");
Eric Newberry358414d2021-03-21 20:56:42 -070048 entry1.setStrategy(Name("/strategyP").appendVersion(1));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000049
50 StrategyChoice entry2;
51 entry2.setName("/52VRvpL9/Yqfut4TNHv");
Eric Newberry358414d2021-03-21 20:56:42 -070052 entry2.setStrategy(Name("/strategyQ").appendVersion(2));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000053
54 this->sendDataset(interest.getName(), entry1, entry2);
55 return true;
56 }
57};
58
59BOOST_FIXTURE_TEST_SUITE(ListCommand, StrategyListFixture)
60
61BOOST_AUTO_TEST_CASE(Normal)
62{
63 this->processInterest = [this] (const Interest& interest) {
64 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
65 };
66
67 this->execute("strategy list");
68 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry7249fb42021-04-04 21:09:32 -070069 BOOST_CHECK(out.is_equal("prefix=/ strategy=/strategyP/v=1\n"
70 "prefix=/52VRvpL9/Yqfut4TNHv strategy=/strategyQ/v=2\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000071 BOOST_CHECK(err.is_empty());
72}
73
74BOOST_AUTO_TEST_CASE(ErrorDataset)
75{
76 this->processInterest = nullptr; // no response to dataset or command
77
78 this->execute("strategy list");
79 BOOST_CHECK_EQUAL(exitCode, 1);
80 BOOST_CHECK(out.is_empty());
Eric Newberry359135c2018-06-26 21:02:12 -070081 BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000082}
83
84BOOST_AUTO_TEST_SUITE_END() // ListCommand
85
86BOOST_FIXTURE_TEST_SUITE(ShowCommand, StrategyListFixture)
87
88BOOST_AUTO_TEST_CASE(NormalDefaultStrategy)
89{
90 this->processInterest = [this] (const Interest& interest) {
91 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
92 };
93
94 this->execute("strategy show /I1Ixgg0X");
95 BOOST_CHECK_EQUAL(exitCode, 0);
96 BOOST_CHECK(out.is_equal(" prefix=/\n"
Eric Newberry7249fb42021-04-04 21:09:32 -070097 "strategy=/strategyP/v=1\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +000098 BOOST_CHECK(err.is_empty());
99}
100
101BOOST_AUTO_TEST_CASE(NormalNonDefaultStrategy)
102{
103 this->processInterest = [this] (const Interest& interest) {
104 BOOST_CHECK(this->respondStrategyChoiceDataset(interest));
105 };
106
107 this->execute("strategy show /52VRvpL9/Yqfut4TNHv/Y5gY7gom");
108 BOOST_CHECK_EQUAL(exitCode, 0);
109 BOOST_CHECK(out.is_equal(" prefix=/52VRvpL9/Yqfut4TNHv\n"
Eric Newberry7249fb42021-04-04 21:09:32 -0700110 "strategy=/strategyQ/v=2\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000111 BOOST_CHECK(err.is_empty());
112}
113
114BOOST_AUTO_TEST_CASE(ErrorDataset)
115{
116 this->processInterest = nullptr; // no response to dataset or command
117
118 this->execute("strategy show /xVoIhNsJ");
119 BOOST_CHECK_EQUAL(exitCode, 1);
120 BOOST_CHECK(out.is_empty());
Eric Newberry359135c2018-06-26 21:02:12 -0700121 BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n"));
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000122}
123
124BOOST_AUTO_TEST_SUITE_END() // ShowCommand
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000125
Junxiao Shib283f522017-04-06 20:46:15 +0000126BOOST_FIXTURE_TEST_SUITE(SetCommand, ExecuteCommandFixture)
127
128BOOST_AUTO_TEST_CASE(Normal)
129{
130 this->processInterest = [this] (const Interest& interest) {
131 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set");
132 BOOST_REQUIRE(req.hasName());
133 BOOST_CHECK_EQUAL(req.getName(), "/VBXSJg3m/XYs81ARNhx");
134 BOOST_REQUIRE(req.hasStrategy());
135 BOOST_CHECK_EQUAL(req.getStrategy(), "/strategyP");
136
137 ControlParameters resp;
138 resp.setName("/VBXSJg3m/XYs81ARNhx");
Eric Newberry358414d2021-03-21 20:56:42 -0700139 resp.setStrategy(Name("/strategyP").appendVersion(5));
Junxiao Shib283f522017-04-06 20:46:15 +0000140 this->succeedCommand(interest, resp);
141 };
142
143 this->execute("strategy set /VBXSJg3m/XYs81ARNhx /strategyP");
144 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry7249fb42021-04-04 21:09:32 -0700145 BOOST_CHECK(out.is_equal("strategy-set prefix=/VBXSJg3m/XYs81ARNhx strategy=/strategyP/v=5\n"));
Junxiao Shib283f522017-04-06 20:46:15 +0000146 BOOST_CHECK(err.is_empty());
147}
148
149BOOST_AUTO_TEST_CASE(UnknownStrategy)
150{
151 this->processInterest = [this] (const Interest& interest) {
152 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set");
153
154 ControlParameters resp;
155 this->failCommand(interest, 404, "strategy not found");
156 };
157
158 this->execute("strategy set /zezRSP1I /strategyQ");
159 BOOST_CHECK_EQUAL(exitCode, 7);
160 BOOST_CHECK(out.is_empty());
161 BOOST_CHECK(err.is_equal("Unknown strategy: /strategyQ\n"));
162}
163
164BOOST_AUTO_TEST_CASE(ErrorCommand)
165{
166 this->processInterest = nullptr; // no response to command
167
168 this->execute("strategy set /LJdNFfQJ8 /strategyP");
169 BOOST_CHECK_EQUAL(exitCode, 1);
170 BOOST_CHECK(out.is_empty());
171 BOOST_CHECK(err.is_equal("Error 10060 when setting strategy: request timed out\n"));
172}
173
174BOOST_AUTO_TEST_SUITE_END() // SetCommand
175
176BOOST_FIXTURE_TEST_SUITE(UnsetCommand, ExecuteCommandFixture)
177
178BOOST_AUTO_TEST_CASE(Normal)
179{
180 this->processInterest = [this] (const Interest& interest) {
181 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/unset");
182 BOOST_REQUIRE(req.hasName());
183 BOOST_CHECK_EQUAL(req.getName(), "/CFVecryP");
184
185 ControlParameters resp;
186 resp.setName("/CFVecryP");
187 this->succeedCommand(interest, resp);
188 };
189
190 this->execute("strategy unset /CFVecryP");
191 BOOST_CHECK_EQUAL(exitCode, 0);
192 BOOST_CHECK(out.is_equal("strategy-unset prefix=/CFVecryP\n"));
193 BOOST_CHECK(err.is_empty());
194}
195
196BOOST_AUTO_TEST_CASE(CannotUnsetDefault)
197{
Davide Pesaventoac238f22017-09-12 15:19:40 -0400198 this->processInterest = [] (const Interest&) {
Junxiao Shib283f522017-04-06 20:46:15 +0000199 BOOST_ERROR("unexpected command");
200 };
201
202 this->execute("strategy unset /");
203 BOOST_CHECK_EQUAL(exitCode, 2);
204 BOOST_CHECK(out.is_empty());
205 BOOST_CHECK(err.is_equal("Unsetting default strategy is prohibited\n"));
206}
207
208BOOST_AUTO_TEST_CASE(ErrorCommand)
209{
210 this->processInterest = nullptr; // no response to command
211
212 this->execute("strategy unset /GQcEsG96");
213 BOOST_CHECK_EQUAL(exitCode, 1);
214 BOOST_CHECK(out.is_empty());
215 BOOST_CHECK(err.is_equal("Error 10060 when unsetting strategy: request timed out\n"));
216}
217
218BOOST_AUTO_TEST_SUITE_END() // UnsetCommand
219
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000220const std::string STATUS_XML = stripXmlSpaces(R"XML(
221 <strategyChoices>
222 <strategyChoice>
223 <namespace>/</namespace>
224 <strategy>
Eric Newberry7249fb42021-04-04 21:09:32 -0700225 <name>/localhost/nfd/strategy/best-route/v=4</name>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000226 </strategy>
227 </strategyChoice>
228 <strategyChoice>
229 <namespace>/localhost</namespace>
230 <strategy>
Eric Newberry7249fb42021-04-04 21:09:32 -0700231 <name>/localhost/nfd/strategy/multicast/v=4</name>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000232 </strategy>
233 </strategyChoice>
234 </strategyChoices>
235)XML");
236
237const std::string STATUS_TEXT = std::string(R"TEXT(
238Strategy choices:
Eric Newberry7249fb42021-04-04 21:09:32 -0700239 prefix=/ strategy=/localhost/nfd/strategy/best-route/v=4
240 prefix=/localhost strategy=/localhost/nfd/strategy/multicast/v=4
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000241)TEXT").substr(1);
242
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000243BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<StrategyChoiceModule>)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000244{
245 this->fetchStatus();
246 StrategyChoice payload1;
247 payload1.setName("/")
Eric Newberry358414d2021-03-21 20:56:42 -0700248 .setStrategy(Name("/localhost/nfd/strategy/best-route").appendVersion(4));
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000249 StrategyChoice payload2;
250 payload2.setName("/localhost")
Eric Newberry358414d2021-03-21 20:56:42 -0700251 .setStrategy(Name("/localhost/nfd/strategy/multicast").appendVersion(4));
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000252 this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2);
253 this->prepareStatusOutput();
254
255 BOOST_CHECK(statusXml.is_equal(STATUS_XML));
256 BOOST_CHECK(statusText.is_equal(STATUS_TEXT));
257}
258
259BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceModule
Junxiao Shi331ade72016-08-19 14:07:19 +0000260BOOST_AUTO_TEST_SUITE_END() // Nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000261
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400262} // namespace nfd::tools::nfdc::tests