blob: 374e36de71ba915bd6f55a9626829b6682e94bcc [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod2147442018-02-19 23:58:17 -05002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi64567bb2016-09-04 16:00:27 +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 "nfdc/command-definition.hpp"
Junxiao Shi64567bb2016-09-04 16:00:27 +000027
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace tools {
32namespace nfdc {
33namespace tests {
34
35using namespace nfd::tests;
36
Junxiao Shi64567bb2016-09-04 16:00:27 +000037BOOST_AUTO_TEST_SUITE(Nfdc)
38BOOST_FIXTURE_TEST_SUITE(TestCommandDefinition, BaseFixture)
39
40BOOST_AUTO_TEST_SUITE(Arguments)
41
42BOOST_AUTO_TEST_CASE(NoArg)
43{
44 CommandDefinition cs("noun", "verb");
45
46 CommandArguments ca;
47
48 ca = cs.parse(std::vector<std::string>{});
49 BOOST_CHECK_EQUAL(ca.size(), 0);
50
51 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x"}), CommandDefinition::Error);
52 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x", "y"}), CommandDefinition::Error);
53}
54
55BOOST_AUTO_TEST_CASE(NamedArgs)
56{
57 CommandDefinition cs("noun", "verb");
58 cs.addArg("a", ArgValueType::UNSIGNED, Required::NO, Positional::NO, "int")
59 .addArg("b", ArgValueType::NAME, Required::NO, Positional::NO, "name");
60
61 CommandArguments ca;
62
63 ca = cs.parse(std::vector<std::string>{});
64 BOOST_CHECK_EQUAL(ca.size(), 0);
65
66 ca = cs.parse(std::vector<std::string>{"a", "1"});
67 BOOST_CHECK_EQUAL(ca.size(), 1);
68 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
69
70 ca = cs.parse(std::vector<std::string>{"a", "1", "b", "/n"});
71 BOOST_CHECK_EQUAL(ca.size(), 2);
72 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
73 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
74
75 ca = cs.parse(std::vector<std::string>{"b", "/n", "a", "1"});
76 BOOST_CHECK_EQUAL(ca.size(), 2);
77 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
78 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
79
80 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"1"}), CommandDefinition::Error);
81 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"c", "1"}), CommandDefinition::Error);
82}
83
84BOOST_AUTO_TEST_CASE(PositionalArgs)
85{
86 CommandDefinition cs("face", "create");
87 cs.addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
88 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES);
89
90 CommandArguments ca;
91
92 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com", "persistent"});
93 BOOST_CHECK_EQUAL(ca.size(), 2);
94 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
95 FaceUri("udp4://router.example.com"));
96 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
97 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
98
99 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com"});
100 BOOST_CHECK_EQUAL(ca.size(), 1);
101 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
102 FaceUri("udp4://router.example.com"));
103
104 ca = cs.parse(std::vector<std::string>{"remote", "udp4://router.example.com"});
105 BOOST_CHECK_EQUAL(ca.size(), 1);
106 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
107 FaceUri("udp4://router.example.com"));
108
109 ca = cs.parse(std::vector<std::string>{
110 "udp4://router.example.com", "persistency", "persistent"});
111 BOOST_CHECK_EQUAL(ca.size(), 2);
112 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
113 FaceUri("udp4://router.example.com"));
114 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
115 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
116
117 ca = cs.parse(std::vector<std::string>{
118 "remote", "udp4://router.example.com", "persistency", "persistent"});
119 BOOST_CHECK_EQUAL(ca.size(), 2);
120 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
121 FaceUri("udp4://router.example.com"));
122 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
123 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
124
125 ca = cs.parse(std::vector<std::string>{
126 "persistency", "persistent", "remote", "udp4://router.example.com"});
127 BOOST_CHECK_EQUAL(ca.size(), 2);
128 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
129 FaceUri("udp4://router.example.com"));
130 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
131 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
132
133 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
134 "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
135 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
136 "persistency", "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
137 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
138 "remote", "udp4://router.example.com", "persistent"}), CommandDefinition::Error);
139}
140
141BOOST_AUTO_TEST_SUITE_END() // Arguments
142
143BOOST_AUTO_TEST_SUITE(ParseValue)
144
145BOOST_AUTO_TEST_CASE(NoneType)
146{
Davide Pesaventod2147442018-02-19 23:58:17 -0500147 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::NONE), "none");
148
Junxiao Shi64567bb2016-09-04 16:00:27 +0000149 CommandDefinition cs("noun", "verb");
150 cs.addArg("a", ArgValueType::NONE, Required::YES, Positional::NO);
151
152 CommandArguments ca;
153
154 ca = cs.parse(std::vector<std::string>{"a"});
155 BOOST_CHECK_EQUAL(ca.size(), 1);
156 BOOST_CHECK(ca.at("a").type() == typeid(bool));
157 BOOST_CHECK_EQUAL(ca.get<bool>("a"), true);
158
159 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "value"}), CommandDefinition::Error);
160}
161
162BOOST_AUTO_TEST_CASE(AnyType)
163{
Davide Pesaventod2147442018-02-19 23:58:17 -0500164 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::ANY), "any");
165
Junxiao Shi64567bb2016-09-04 16:00:27 +0000166 CommandDefinition cs("noun", "verb");
167 cs.addArg("a", ArgValueType::ANY, Required::NO, Positional::YES);
168
169 CommandArguments ca;
170
171 ca = cs.parse(std::vector<std::string>{});
172 BOOST_CHECK_EQUAL(ca.size(), 0);
173
174 ca = cs.parse(std::vector<std::string>{"a"});
175 BOOST_CHECK_EQUAL(ca.size(), 1);
176 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
177 std::vector<std::string> values = ca.get<std::vector<std::string>>("a");
178 BOOST_CHECK_EQUAL(values.size(), 1);
179 BOOST_CHECK_EQUAL(values.at(0), "a");
180
181 ca = cs.parse(std::vector<std::string>{"b", "c"});
182 BOOST_CHECK_EQUAL(ca.size(), 1);
183 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
184 values = ca.get<std::vector<std::string>>("a");
185 BOOST_CHECK_EQUAL(values.size(), 2);
186 BOOST_CHECK_EQUAL(values.at(0), "b");
187 BOOST_CHECK_EQUAL(values.at(1), "c");
188}
189
Davide Pesaventod2147442018-02-19 23:58:17 -0500190BOOST_AUTO_TEST_CASE(BooleanType)
191{
192 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::BOOLEAN), "boolean");
193
194 CommandDefinition cs("noun", "verb");
195 cs.addArg("a", ArgValueType::BOOLEAN, Required::YES);
196
197 CommandArguments ca;
198
199 ca = cs.parse({"a", "on"});
200 BOOST_CHECK_EQUAL(ca.size(), 1);
201 BOOST_CHECK(ca.at("a").type() == typeid(bool));
202 BOOST_CHECK_EQUAL(ca.get<bool>("a"), true);
203
204 ca = cs.parse({"a", "off"});
205 BOOST_CHECK_EQUAL(ca.size(), 1);
206 BOOST_CHECK(ca.at("a").type() == typeid(bool));
207 BOOST_CHECK_EQUAL(ca.get<bool>("a"), false);
208
209 BOOST_CHECK_EQUAL(cs.parse({"a", "true"}).get<bool>("a"), true);
210 BOOST_CHECK_EQUAL(cs.parse({"a", "enabled"}).get<bool>("a"), true);
211 BOOST_CHECK_EQUAL(cs.parse({"a", "yes"}).get<bool>("a"), true);
212 BOOST_CHECK_EQUAL(cs.parse({"a", "1"}).get<bool>("a"), true);
213 BOOST_CHECK_EQUAL(cs.parse({"a", "false"}).get<bool>("a"), false);
214 BOOST_CHECK_EQUAL(cs.parse({"a", "disabled"}).get<bool>("a"), false);
215 BOOST_CHECK_EQUAL(cs.parse({"a", "no"}).get<bool>("a"), false);
216 BOOST_CHECK_EQUAL(cs.parse({"a", "0"}).get<bool>("a"), false);
217
218 BOOST_CHECK_THROW(cs.parse({"a", "42"}), CommandDefinition::Error);
219 BOOST_CHECK_THROW(cs.parse({"a", "not-bool"}), CommandDefinition::Error);
220}
221
Junxiao Shi64567bb2016-09-04 16:00:27 +0000222BOOST_AUTO_TEST_CASE(UnsignedType)
223{
Davide Pesaventod2147442018-02-19 23:58:17 -0500224 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::UNSIGNED), "non-negative integer");
225
Junxiao Shi64567bb2016-09-04 16:00:27 +0000226 CommandDefinition cs("noun", "verb");
227 cs.addArg("a", ArgValueType::UNSIGNED, Required::YES);
228
229 CommandArguments ca;
230
231 ca = cs.parse(std::vector<std::string>{"a", "0"});
232 BOOST_CHECK_EQUAL(ca.size(), 1);
233 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
234 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 0);
235
236 ca = cs.parse(std::vector<std::string>{"a", "12923"});
237 BOOST_CHECK_EQUAL(ca.size(), 1);
238 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
239 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 12923);
240
241 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "-25705"}), CommandDefinition::Error);
242 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-uint"}), CommandDefinition::Error);
243}
244
245BOOST_AUTO_TEST_CASE(StringType)
246{
Davide Pesaventod2147442018-02-19 23:58:17 -0500247 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::STRING), "string");
248
Junxiao Shi64567bb2016-09-04 16:00:27 +0000249 CommandDefinition cs("noun", "verb");
250 cs.addArg("a", ArgValueType::STRING, Required::YES);
251
252 CommandArguments ca;
253
254 ca = cs.parse(std::vector<std::string>{"a", "hello"});
255 BOOST_CHECK_EQUAL(ca.size(), 1);
256 BOOST_CHECK(ca.at("a").type() == typeid(std::string));
257 BOOST_CHECK_EQUAL(ca.get<std::string>("a"), "hello");
258}
259
260BOOST_AUTO_TEST_CASE(ReportFormatType)
261{
Davide Pesaventod2147442018-02-19 23:58:17 -0500262 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::REPORT_FORMAT), "ReportFormat");
263
Junxiao Shi64567bb2016-09-04 16:00:27 +0000264 CommandDefinition cs("noun", "verb");
265 cs.addArg("a", ArgValueType::REPORT_FORMAT, Required::YES);
266
267 CommandArguments ca;
268
269 ca = cs.parse(std::vector<std::string>{"a", "xml"});
270 BOOST_CHECK_EQUAL(ca.size(), 1);
271 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
272 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::XML);
273
274 ca = cs.parse(std::vector<std::string>{"a", "text"});
275 BOOST_CHECK_EQUAL(ca.size(), 1);
276 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
277 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::TEXT);
278
279 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-fmt"}), CommandDefinition::Error);
280}
281
282BOOST_AUTO_TEST_CASE(NameType)
283{
Davide Pesaventod2147442018-02-19 23:58:17 -0500284 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::NAME), "Name");
285
Junxiao Shi64567bb2016-09-04 16:00:27 +0000286 CommandDefinition cs("noun", "verb");
287 cs.addArg("a", ArgValueType::NAME, Required::YES);
288
289 CommandArguments ca;
290
291 ca = cs.parse(std::vector<std::string>{"a", "/n"});
292 BOOST_CHECK_EQUAL(ca.size(), 1);
293 BOOST_CHECK(ca.at("a").type() == typeid(Name));
294 BOOST_CHECK_EQUAL(ca.get<Name>("a"), "/n");
295}
296
297BOOST_AUTO_TEST_CASE(FaceUriType)
298{
Davide Pesaventod2147442018-02-19 23:58:17 -0500299 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_URI), "FaceUri");
300
Junxiao Shi64567bb2016-09-04 16:00:27 +0000301 CommandDefinition cs("noun", "verb");
302 cs.addArg("a", ArgValueType::FACE_URI, Required::YES);
303
304 CommandArguments ca;
305
306 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
307 BOOST_CHECK_EQUAL(ca.size(), 1);
308 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
309 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
310
311 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "208"}), CommandDefinition::Error);
312 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
313}
314
315BOOST_AUTO_TEST_CASE(FaceIdOrUriType)
316{
Davide Pesaventod2147442018-02-19 23:58:17 -0500317 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_ID_OR_URI), "FaceId or FaceUri");
318
Junxiao Shi64567bb2016-09-04 16:00:27 +0000319 CommandDefinition cs("noun", "verb");
320 cs.addArg("a", ArgValueType::FACE_ID_OR_URI, Required::YES);
321
322 CommandArguments ca;
323
324 ca = cs.parse(std::vector<std::string>{"a", "208"});
325 BOOST_CHECK_EQUAL(ca.size(), 1);
326 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
327 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 208);
328
329 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
330 BOOST_CHECK_EQUAL(ca.size(), 1);
331 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
332 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
333
334 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
335}
336
337BOOST_AUTO_TEST_CASE(FacePersistencyType)
338{
Davide Pesaventod2147442018-02-19 23:58:17 -0500339 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_PERSISTENCY), "FacePersistency");
340
Junxiao Shi64567bb2016-09-04 16:00:27 +0000341 CommandDefinition cs("noun", "verb");
342 cs.addArg("a", ArgValueType::FACE_PERSISTENCY, Required::YES);
343
344 CommandArguments ca;
345
346 ca = cs.parse(std::vector<std::string>{"a", "persistent"});
347 BOOST_CHECK_EQUAL(ca.size(), 1);
348 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
Davide Pesaventod2147442018-02-19 23:58:17 -0500349 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000350
351 ca = cs.parse(std::vector<std::string>{"a", "permanent"});
352 BOOST_CHECK_EQUAL(ca.size(), 1);
353 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
Davide Pesaventod2147442018-02-19 23:58:17 -0500354 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), FacePersistency::FACE_PERSISTENCY_PERMANENT);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000355
356 // nfdc does not accept "on-demand"
357 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "on-demand"}), CommandDefinition::Error);
358}
359
Junxiao Shi8eda6822017-04-12 02:53:14 +0000360BOOST_AUTO_TEST_CASE(RouteOriginType)
361{
Davide Pesaventod2147442018-02-19 23:58:17 -0500362 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::ROUTE_ORIGIN), "RouteOrigin");
363
Junxiao Shi8eda6822017-04-12 02:53:14 +0000364 CommandDefinition cs("noun", "verb");
365 cs.addArg("a", ArgValueType::ROUTE_ORIGIN, Required::YES);
366
367 CommandArguments ca;
368
369 ca = cs.parse(std::vector<std::string>{"a", "Nlsr"});
370 BOOST_CHECK_EQUAL(ca.size(), 1);
371 BOOST_CHECK(ca.at("a").type() == typeid(RouteOrigin));
Davide Pesaventod2147442018-02-19 23:58:17 -0500372 BOOST_CHECK_EQUAL(ca.get<RouteOrigin>("a"), RouteOrigin::ROUTE_ORIGIN_NLSR);
Junxiao Shi8eda6822017-04-12 02:53:14 +0000373
374 ca = cs.parse(std::vector<std::string>{"a", "27"});
375 BOOST_CHECK_EQUAL(ca.size(), 1);
376 BOOST_CHECK(ca.at("a").type() == typeid(RouteOrigin));
Davide Pesaventod2147442018-02-19 23:58:17 -0500377 BOOST_CHECK_EQUAL(ca.get<RouteOrigin>("a"), static_cast<RouteOrigin>(27));
Junxiao Shi8eda6822017-04-12 02:53:14 +0000378
379 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-RouteOrigin"}), CommandDefinition::Error);
380}
381
Junxiao Shi64567bb2016-09-04 16:00:27 +0000382BOOST_AUTO_TEST_SUITE_END() // ParseValue
383
384BOOST_AUTO_TEST_SUITE_END() // TestCommandDefinition
385BOOST_AUTO_TEST_SUITE_END() // Nfdc
386
387} // namespace tests
388} // namespace nfdc
389} // namespace tools
390} // namespace nfd