blob: bbefe815d3a5680558955e6afdb8c6f1c2c26ba2 [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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 "nfdc/command-definition.hpp"
27#include "nfdc/status-report.hpp"
28#include <ndn-cxx/encoding/nfd-constants.hpp>
29#include <ndn-cxx/util/face-uri.hpp>
30
31#include "tests/test-common.hpp"
32
33namespace nfd {
34namespace tools {
35namespace nfdc {
36namespace tests {
37
38using namespace nfd::tests;
39
40using ndn::util::FaceUri;
41using ndn::nfd::FacePersistency;
42
43BOOST_AUTO_TEST_SUITE(Nfdc)
44BOOST_FIXTURE_TEST_SUITE(TestCommandDefinition, BaseFixture)
45
46BOOST_AUTO_TEST_SUITE(Arguments)
47
48BOOST_AUTO_TEST_CASE(NoArg)
49{
50 CommandDefinition cs("noun", "verb");
51
52 CommandArguments ca;
53
54 ca = cs.parse(std::vector<std::string>{});
55 BOOST_CHECK_EQUAL(ca.size(), 0);
56
57 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x"}), CommandDefinition::Error);
58 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x", "y"}), CommandDefinition::Error);
59}
60
61BOOST_AUTO_TEST_CASE(NamedArgs)
62{
63 CommandDefinition cs("noun", "verb");
64 cs.addArg("a", ArgValueType::UNSIGNED, Required::NO, Positional::NO, "int")
65 .addArg("b", ArgValueType::NAME, Required::NO, Positional::NO, "name");
66
67 CommandArguments ca;
68
69 ca = cs.parse(std::vector<std::string>{});
70 BOOST_CHECK_EQUAL(ca.size(), 0);
71
72 ca = cs.parse(std::vector<std::string>{"a", "1"});
73 BOOST_CHECK_EQUAL(ca.size(), 1);
74 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
75
76 ca = cs.parse(std::vector<std::string>{"a", "1", "b", "/n"});
77 BOOST_CHECK_EQUAL(ca.size(), 2);
78 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
79 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
80
81 ca = cs.parse(std::vector<std::string>{"b", "/n", "a", "1"});
82 BOOST_CHECK_EQUAL(ca.size(), 2);
83 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1);
84 BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n");
85
86 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"1"}), CommandDefinition::Error);
87 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"c", "1"}), CommandDefinition::Error);
88}
89
90BOOST_AUTO_TEST_CASE(PositionalArgs)
91{
92 CommandDefinition cs("face", "create");
93 cs.addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
94 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES);
95
96 CommandArguments ca;
97
98 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com", "persistent"});
99 BOOST_CHECK_EQUAL(ca.size(), 2);
100 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
101 FaceUri("udp4://router.example.com"));
102 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
103 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
104
105 ca = cs.parse(std::vector<std::string>{"udp4://router.example.com"});
106 BOOST_CHECK_EQUAL(ca.size(), 1);
107 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
108 FaceUri("udp4://router.example.com"));
109
110 ca = cs.parse(std::vector<std::string>{"remote", "udp4://router.example.com"});
111 BOOST_CHECK_EQUAL(ca.size(), 1);
112 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
113 FaceUri("udp4://router.example.com"));
114
115 ca = cs.parse(std::vector<std::string>{
116 "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 "remote", "udp4://router.example.com", "persistency", "persistent"});
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 ca = cs.parse(std::vector<std::string>{
132 "persistency", "persistent", "remote", "udp4://router.example.com"});
133 BOOST_CHECK_EQUAL(ca.size(), 2);
134 BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"),
135 FaceUri("udp4://router.example.com"));
136 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"),
137 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
138
139 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
140 "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
141 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
142 "persistency", "persistent", "udp4://router.example.com"}), CommandDefinition::Error);
143 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{
144 "remote", "udp4://router.example.com", "persistent"}), CommandDefinition::Error);
145}
146
147BOOST_AUTO_TEST_SUITE_END() // Arguments
148
149BOOST_AUTO_TEST_SUITE(ParseValue)
150
151BOOST_AUTO_TEST_CASE(NoneType)
152{
153 CommandDefinition cs("noun", "verb");
154 cs.addArg("a", ArgValueType::NONE, Required::YES, Positional::NO);
155
156 CommandArguments ca;
157
158 ca = cs.parse(std::vector<std::string>{"a"});
159 BOOST_CHECK_EQUAL(ca.size(), 1);
160 BOOST_CHECK(ca.at("a").type() == typeid(bool));
161 BOOST_CHECK_EQUAL(ca.get<bool>("a"), true);
162
163 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "value"}), CommandDefinition::Error);
164}
165
166BOOST_AUTO_TEST_CASE(AnyType)
167{
168 CommandDefinition cs("noun", "verb");
169 cs.addArg("a", ArgValueType::ANY, Required::NO, Positional::YES);
170
171 CommandArguments ca;
172
173 ca = cs.parse(std::vector<std::string>{});
174 BOOST_CHECK_EQUAL(ca.size(), 0);
175
176 ca = cs.parse(std::vector<std::string>{"a"});
177 BOOST_CHECK_EQUAL(ca.size(), 1);
178 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
179 std::vector<std::string> values = ca.get<std::vector<std::string>>("a");
180 BOOST_CHECK_EQUAL(values.size(), 1);
181 BOOST_CHECK_EQUAL(values.at(0), "a");
182
183 ca = cs.parse(std::vector<std::string>{"b", "c"});
184 BOOST_CHECK_EQUAL(ca.size(), 1);
185 BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>));
186 values = ca.get<std::vector<std::string>>("a");
187 BOOST_CHECK_EQUAL(values.size(), 2);
188 BOOST_CHECK_EQUAL(values.at(0), "b");
189 BOOST_CHECK_EQUAL(values.at(1), "c");
190}
191
192BOOST_AUTO_TEST_CASE(UnsignedType)
193{
194 CommandDefinition cs("noun", "verb");
195 cs.addArg("a", ArgValueType::UNSIGNED, Required::YES);
196
197 CommandArguments ca;
198
199 ca = cs.parse(std::vector<std::string>{"a", "0"});
200 BOOST_CHECK_EQUAL(ca.size(), 1);
201 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
202 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 0);
203
204 ca = cs.parse(std::vector<std::string>{"a", "12923"});
205 BOOST_CHECK_EQUAL(ca.size(), 1);
206 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
207 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 12923);
208
209 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "-25705"}), CommandDefinition::Error);
210 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-uint"}), CommandDefinition::Error);
211}
212
213BOOST_AUTO_TEST_CASE(StringType)
214{
215 CommandDefinition cs("noun", "verb");
216 cs.addArg("a", ArgValueType::STRING, Required::YES);
217
218 CommandArguments ca;
219
220 ca = cs.parse(std::vector<std::string>{"a", "hello"});
221 BOOST_CHECK_EQUAL(ca.size(), 1);
222 BOOST_CHECK(ca.at("a").type() == typeid(std::string));
223 BOOST_CHECK_EQUAL(ca.get<std::string>("a"), "hello");
224}
225
226BOOST_AUTO_TEST_CASE(ReportFormatType)
227{
228 CommandDefinition cs("noun", "verb");
229 cs.addArg("a", ArgValueType::REPORT_FORMAT, Required::YES);
230
231 CommandArguments ca;
232
233 ca = cs.parse(std::vector<std::string>{"a", "xml"});
234 BOOST_CHECK_EQUAL(ca.size(), 1);
235 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
236 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::XML);
237
238 ca = cs.parse(std::vector<std::string>{"a", "text"});
239 BOOST_CHECK_EQUAL(ca.size(), 1);
240 BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat));
241 BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::TEXT);
242
243 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-fmt"}), CommandDefinition::Error);
244}
245
246BOOST_AUTO_TEST_CASE(NameType)
247{
248 CommandDefinition cs("noun", "verb");
249 cs.addArg("a", ArgValueType::NAME, Required::YES);
250
251 CommandArguments ca;
252
253 ca = cs.parse(std::vector<std::string>{"a", "/n"});
254 BOOST_CHECK_EQUAL(ca.size(), 1);
255 BOOST_CHECK(ca.at("a").type() == typeid(Name));
256 BOOST_CHECK_EQUAL(ca.get<Name>("a"), "/n");
257}
258
259BOOST_AUTO_TEST_CASE(FaceUriType)
260{
261 CommandDefinition cs("noun", "verb");
262 cs.addArg("a", ArgValueType::FACE_URI, Required::YES);
263
264 CommandArguments ca;
265
266 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
267 BOOST_CHECK_EQUAL(ca.size(), 1);
268 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
269 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
270
271 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "208"}), CommandDefinition::Error);
272 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
273}
274
275BOOST_AUTO_TEST_CASE(FaceIdOrUriType)
276{
277 CommandDefinition cs("noun", "verb");
278 cs.addArg("a", ArgValueType::FACE_ID_OR_URI, Required::YES);
279
280 CommandArguments ca;
281
282 ca = cs.parse(std::vector<std::string>{"a", "208"});
283 BOOST_CHECK_EQUAL(ca.size(), 1);
284 BOOST_CHECK(ca.at("a").type() == typeid(uint64_t));
285 BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 208);
286
287 ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"});
288 BOOST_CHECK_EQUAL(ca.size(), 1);
289 BOOST_CHECK(ca.at("a").type() == typeid(FaceUri));
290 BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363"));
291
292 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error);
293}
294
295BOOST_AUTO_TEST_CASE(FacePersistencyType)
296{
297 CommandDefinition cs("noun", "verb");
298 cs.addArg("a", ArgValueType::FACE_PERSISTENCY, Required::YES);
299
300 CommandArguments ca;
301
302 ca = cs.parse(std::vector<std::string>{"a", "persistent"});
303 BOOST_CHECK_EQUAL(ca.size(), 1);
304 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
305 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"),
306 FacePersistency::FACE_PERSISTENCY_PERSISTENT);
307
308 ca = cs.parse(std::vector<std::string>{"a", "permanent"});
309 BOOST_CHECK_EQUAL(ca.size(), 1);
310 BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency));
311 BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"),
312 FacePersistency::FACE_PERSISTENCY_PERMANENT);
313
314 // nfdc does not accept "on-demand"
315 BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "on-demand"}), CommandDefinition::Error);
316}
317
318BOOST_AUTO_TEST_SUITE_END() // ParseValue
319
320BOOST_AUTO_TEST_SUITE_END() // TestCommandDefinition
321BOOST_AUTO_TEST_SUITE_END() // Nfdc
322
323} // namespace tests
324} // namespace nfdc
325} // namespace tools
326} // namespace nfd