blob: 777a5540aa748539e768e918980e2ec2e8e66e06 [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/*
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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
Junxiao Shi64567bb2016-09-04 16:00:27 +000035BOOST_AUTO_TEST_SUITE(Nfdc)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_AUTO_TEST_SUITE(TestCommandDefinition)
Junxiao Shi64567bb2016-09-04 16:00:27 +000037
38BOOST_AUTO_TEST_SUITE(Arguments)
39
40BOOST_AUTO_TEST_CASE(NoArg)
41{
42 CommandDefinition cs("noun", "verb");
43
44 CommandArguments ca;
45
46 ca = cs.parse(std::vector<std::string>{});
47 BOOST_CHECK_EQUAL(ca.size(), 0);
48
49 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x"}), CommandDefinition::Error);
50 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x", "y"}), CommandDefinition::Error);
51}
52
53BOOST_AUTO_TEST_CASE(NamedArgs)
54{
55 CommandDefinition cs("noun", "verb");
56 cs.addArg("a", ArgValueType::UNSIGNED, Required::NO, Positional::NO, "int")
57 .addArg("b", ArgValueType::NAME, Required::NO, Positional::NO, "name");
58
59 CommandArguments ca;
60
61 ca = cs.parse(std::vector<std::string>{});
62 BOOST_CHECK_EQUAL(ca.size(), 0);
63
64 ca = cs.parse(std::vector<std::string>{"a", "1"});
65 BOOST_CHECK_EQUAL(ca.size(), 1);
66 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
67
68 ca = cs.parse(std::vector<std::string>{"a", "1", "b", "/n"});
69 BOOST_CHECK_EQUAL(ca.size(), 2);
70 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
71 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
72
73 ca = cs.parse(std::vector<std::string>{"b", "/n", "a", "1"});
74 BOOST_CHECK_EQUAL(ca.size(), 2);
75 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
76 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
77
78 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"1"}), CommandDefinition::Error);
79 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"c", "1"}), CommandDefinition::Error);
80}
81
82BOOST_AUTO_TEST_CASE(PositionalArgs)
83{
84 CommandDefinition cs("face", "create");
85 cs.addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
86 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES);
87
88 CommandArguments ca;
89
90 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com", "persistent"});
91 BOOST_CHECK_EQUAL(ca.size(), 2);
92 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
93 FaceUri("udp4://router.example.com"));
94 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
95 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
96
97 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com"});
98 BOOST_CHECK_EQUAL(ca.size(), 1);
99 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
100 FaceUri("udp4://router.example.com"));
101
102 ca = cs.parse(std::vector<std::string>{"remote", "udp4://router.example.com"});
103 BOOST_CHECK_EQUAL(ca.size(), 1);
104 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
105 FaceUri("udp4://router.example.com"));
106
107 ca = cs.parse(std::vector<std::string>{
108 "udp4://router.example.com", "persistency", "persistent"});
109 BOOST_CHECK_EQUAL(ca.size(), 2);
110 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
111 FaceUri("udp4://router.example.com"));
112 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
113 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
114
115 ca = cs.parse(std::vector<std::string>{
116 "remote", "udp4://router.example.com", "persistency", "persistent"});
117 BOOST_CHECK_EQUAL(ca.size(), 2);
118 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
119 FaceUri("udp4://router.example.com"));
120 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
121 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
122
123 ca = cs.parse(std::vector<std::string>{
124 "persistency", "persistent", "remote", "udp4://router.example.com"});
125 BOOST_CHECK_EQUAL(ca.size(), 2);
126 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
127 FaceUri("udp4://router.example.com"));
128 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
129 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
130
131 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
132 "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
133 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
134 "persistency", "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
135 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
136 "remote", "udp4://router.example.com", "persistent"}), CommandDefinition::Error);
137}
138
139BOOST_AUTO_TEST_SUITE_END() // Arguments
140
141BOOST_AUTO_TEST_SUITE(ParseValue)
142
143BOOST_AUTO_TEST_CASE(NoneType)
144{
Davide Pesaventod2147442018-02-19 23:58:17 -0500145 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::NONE), "none");
146
Junxiao Shi64567bb2016-09-04 16:00:27 +0000147 CommandDefinition cs("noun", "verb");
148 cs.addArg("a", ArgValueType::NONE, Required::YES, Positional::NO);
149
150 CommandArguments ca;
151
152 ca = cs.parse(std::vector<std::string>{"a"});
153 BOOST_CHECK_EQUAL(ca.size(), 1);
154 BOOST_CHECK(ca.at("a").type() == typeid(bool));
155 BOOST_CHECK_EQUAL(ca.get<bool>("a"), true);
156
157 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "value"}), CommandDefinition::Error);
158}
159
160BOOST_AUTO_TEST_CASE(AnyType)
161{
Davide Pesaventod2147442018-02-19 23:58:17 -0500162 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::ANY), "any");
163
Junxiao Shi64567bb2016-09-04 16:00:27 +0000164 CommandDefinition cs("noun", "verb");
165 cs.addArg("a", ArgValueType::ANY, Required::NO, Positional::YES);
166
167 CommandArguments ca;
168
169 ca = cs.parse(std::vector<std::string>{});
170 BOOST_CHECK_EQUAL(ca.size(), 0);
171
172 ca = cs.parse(std::vector<std::string>{"a"});
173 BOOST_CHECK_EQUAL(ca.size(), 1);
174 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
175 std::vector<std::string> values = ca.get<std::vector<std::string>>("a");
176 BOOST_CHECK_EQUAL(values.size(), 1);
177 BOOST_CHECK_EQUAL(values.at(0), "a");
178
179 ca = cs.parse(std::vector<std::string>{"b", "c"});
180 BOOST_CHECK_EQUAL(ca.size(), 1);
181 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
182 values = ca.get<std::vector<std::string>>("a");
183 BOOST_CHECK_EQUAL(values.size(), 2);
184 BOOST_CHECK_EQUAL(values.at(0), "b");
185 BOOST_CHECK_EQUAL(values.at(1), "c");
186}
187
Davide Pesaventod2147442018-02-19 23:58:17 -0500188BOOST_AUTO_TEST_CASE(BooleanType)
189{
190 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::BOOLEAN), "boolean");
191
192 CommandDefinition cs("noun", "verb");
193 cs.addArg("a", ArgValueType::BOOLEAN, Required::YES);
194
195 CommandArguments ca;
196
197 ca = cs.parse({"a", "on"});
198 BOOST_CHECK_EQUAL(ca.size(), 1);
199 BOOST_CHECK(ca.at("a").type() == typeid(bool));
200 BOOST_CHECK_EQUAL(ca.get<bool>("a"), true);
201
202 ca = cs.parse({"a", "off"});
203 BOOST_CHECK_EQUAL(ca.size(), 1);
204 BOOST_CHECK(ca.at("a").type() == typeid(bool));
205 BOOST_CHECK_EQUAL(ca.get<bool>("a"), false);
206
207 BOOST_CHECK_EQUAL(cs.parse({"a", "true"}).get<bool>("a"), true);
208 BOOST_CHECK_EQUAL(cs.parse({"a", "enabled"}).get<bool>("a"), true);
209 BOOST_CHECK_EQUAL(cs.parse({"a", "yes"}).get<bool>("a"), true);
210 BOOST_CHECK_EQUAL(cs.parse({"a", "1"}).get<bool>("a"), true);
211 BOOST_CHECK_EQUAL(cs.parse({"a", "false"}).get<bool>("a"), false);
212 BOOST_CHECK_EQUAL(cs.parse({"a", "disabled"}).get<bool>("a"), false);
213 BOOST_CHECK_EQUAL(cs.parse({"a", "no"}).get<bool>("a"), false);
214 BOOST_CHECK_EQUAL(cs.parse({"a", "0"}).get<bool>("a"), false);
215
216 BOOST_CHECK_THROW(cs.parse({"a", "42"}), CommandDefinition::Error);
217 BOOST_CHECK_THROW(cs.parse({"a", "not-bool"}), CommandDefinition::Error);
218}
219
Junxiao Shi64567bb2016-09-04 16:00:27 +0000220BOOST_AUTO_TEST_CASE(UnsignedType)
221{
Davide Pesaventod2147442018-02-19 23:58:17 -0500222 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::UNSIGNED), "non-negative integer");
223
Junxiao Shi64567bb2016-09-04 16:00:27 +0000224 CommandDefinition cs("noun", "verb");
225 cs.addArg("a", ArgValueType::UNSIGNED, Required::YES);
226
227 CommandArguments ca;
228
229 ca = cs.parse(std::vector<std::string>{"a", "0"});
230 BOOST_CHECK_EQUAL(ca.size(), 1);
231 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
232 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 0);
233
234 ca = cs.parse(std::vector<std::string>{"a", "12923"});
235 BOOST_CHECK_EQUAL(ca.size(), 1);
236 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
237 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 12923);
238
239 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "-25705"}), CommandDefinition::Error);
240 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-uint"}), CommandDefinition::Error);
241}
242
243BOOST_AUTO_TEST_CASE(StringType)
244{
Davide Pesaventod2147442018-02-19 23:58:17 -0500245 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::STRING), "string");
246
Junxiao Shi64567bb2016-09-04 16:00:27 +0000247 CommandDefinition cs("noun", "verb");
248 cs.addArg("a", ArgValueType::STRING, Required::YES);
249
250 CommandArguments ca;
251
252 ca = cs.parse(std::vector<std::string>{"a", "hello"});
253 BOOST_CHECK_EQUAL(ca.size(), 1);
254 BOOST_CHECK(ca.at("a").type() == typeid(std::string));
255 BOOST_CHECK_EQUAL(ca.get<std::string>("a"), "hello");
256}
257
258BOOST_AUTO_TEST_CASE(ReportFormatType)
259{
Davide Pesaventod2147442018-02-19 23:58:17 -0500260 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::REPORT_FORMAT), "ReportFormat");
261
Junxiao Shi64567bb2016-09-04 16:00:27 +0000262 CommandDefinition cs("noun", "verb");
263 cs.addArg("a", ArgValueType::REPORT_FORMAT, Required::YES);
264
265 CommandArguments ca;
266
267 ca = cs.parse(std::vector<std::string>{"a", "xml"});
268 BOOST_CHECK_EQUAL(ca.size(), 1);
269 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
270 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::XML);
271
272 ca = cs.parse(std::vector<std::string>{"a", "text"});
273 BOOST_CHECK_EQUAL(ca.size(), 1);
274 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
275 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::TEXT);
276
277 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-fmt"}), CommandDefinition::Error);
278}
279
280BOOST_AUTO_TEST_CASE(NameType)
281{
Davide Pesaventod2147442018-02-19 23:58:17 -0500282 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::NAME), "Name");
283
Junxiao Shi64567bb2016-09-04 16:00:27 +0000284 CommandDefinition cs("noun", "verb");
285 cs.addArg("a", ArgValueType::NAME, Required::YES);
286
287 CommandArguments ca;
288
289 ca = cs.parse(std::vector<std::string>{"a", "/n"});
290 BOOST_CHECK_EQUAL(ca.size(), 1);
291 BOOST_CHECK(ca.at("a").type() == typeid(Name));
292 BOOST_CHECK_EQUAL(ca.get<Name>("a"), "/n");
293}
294
295BOOST_AUTO_TEST_CASE(FaceUriType)
296{
Davide Pesaventod2147442018-02-19 23:58:17 -0500297 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_URI), "FaceUri");
298
Junxiao Shi64567bb2016-09-04 16:00:27 +0000299 CommandDefinition cs("noun", "verb");
300 cs.addArg("a", ArgValueType::FACE_URI, Required::YES);
301
302 CommandArguments ca;
303
304 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
305 BOOST_CHECK_EQUAL(ca.size(), 1);
306 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
307 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
308
309 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "208"}), CommandDefinition::Error);
310 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
311}
312
313BOOST_AUTO_TEST_CASE(FaceIdOrUriType)
314{
Davide Pesaventod2147442018-02-19 23:58:17 -0500315 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_ID_OR_URI), "FaceId or FaceUri");
316
Junxiao Shi64567bb2016-09-04 16:00:27 +0000317 CommandDefinition cs("noun", "verb");
318 cs.addArg("a", ArgValueType::FACE_ID_OR_URI, Required::YES);
319
320 CommandArguments ca;
321
322 ca = cs.parse(std::vector<std::string>{"a", "208"});
323 BOOST_CHECK_EQUAL(ca.size(), 1);
324 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
325 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 208);
326
327 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
328 BOOST_CHECK_EQUAL(ca.size(), 1);
329 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
330 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
331
332 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
333}
334
335BOOST_AUTO_TEST_CASE(FacePersistencyType)
336{
Davide Pesaventod2147442018-02-19 23:58:17 -0500337 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::FACE_PERSISTENCY), "FacePersistency");
338
Junxiao Shi64567bb2016-09-04 16:00:27 +0000339 CommandDefinition cs("noun", "verb");
340 cs.addArg("a", ArgValueType::FACE_PERSISTENCY, Required::YES);
341
342 CommandArguments ca;
343
344 ca = cs.parse(std::vector<std::string>{"a", "persistent"});
345 BOOST_CHECK_EQUAL(ca.size(), 1);
346 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
Davide Pesaventod2147442018-02-19 23:58:17 -0500347 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000348
349 ca = cs.parse(std::vector<std::string>{"a", "permanent"});
350 BOOST_CHECK_EQUAL(ca.size(), 1);
351 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
Davide Pesaventod2147442018-02-19 23:58:17 -0500352 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), FacePersistency::FACE_PERSISTENCY_PERMANENT);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000353
354 // nfdc does not accept "on-demand"
355 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "on-demand"}), CommandDefinition::Error);
356}
357
Junxiao Shi8eda6822017-04-12 02:53:14 +0000358BOOST_AUTO_TEST_CASE(RouteOriginType)
359{
Davide Pesaventod2147442018-02-19 23:58:17 -0500360 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ArgValueType::ROUTE_ORIGIN), "RouteOrigin");
361
Junxiao Shi8eda6822017-04-12 02:53:14 +0000362 CommandDefinition cs("noun", "verb");
363 cs.addArg("a", ArgValueType::ROUTE_ORIGIN, Required::YES);
364
365 CommandArguments ca;
366
367 ca = cs.parse(std::vector<std::string>{"a", "Nlsr"});
368 BOOST_CHECK_EQUAL(ca.size(), 1);
369 BOOST_CHECK(ca.at("a").type() == typeid(RouteOrigin));
Davide Pesaventod2147442018-02-19 23:58:17 -0500370 BOOST_CHECK_EQUAL(ca.get<RouteOrigin>("a"), RouteOrigin::ROUTE_ORIGIN_NLSR);
Junxiao Shi8eda6822017-04-12 02:53:14 +0000371
372 ca = cs.parse(std::vector<std::string>{"a", "27"});
373 BOOST_CHECK_EQUAL(ca.size(), 1);
374 BOOST_CHECK(ca.at("a").type() == typeid(RouteOrigin));
Davide Pesaventod2147442018-02-19 23:58:17 -0500375 BOOST_CHECK_EQUAL(ca.get<RouteOrigin>("a"), static_cast<RouteOrigin>(27));
Junxiao Shi8eda6822017-04-12 02:53:14 +0000376
377 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-RouteOrigin"}), CommandDefinition::Error);
378}
379
Junxiao Shi64567bb2016-09-04 16:00:27 +0000380BOOST_AUTO_TEST_SUITE_END() // ParseValue
381
382BOOST_AUTO_TEST_SUITE_END() // TestCommandDefinition
383BOOST_AUTO_TEST_SUITE_END() // Nfdc
384
385} // namespace tests
386} // namespace nfdc
387} // namespace tools
388} // namespace nfd