2 * Copyright (c) 2010 Your File System Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Test the command line parsing library
29 #include <afsconfig.h>
30 #include <afs/param.h>
36 #include <tests/tap/basic.h>
50 testproc(struct cmd_syndesc *as, void *rock)
52 is_string("foo", as->parms[copt_first].items->data,
53 "first option matches");
54 is_string("bar", as->parms[copt_second].items->data,
55 "second option matches");
56 ok(as->parms[copt_flag].items != NULL,
63 checkList(struct cmd_item *list, ...)
69 el = va_arg(ap, char *);
70 while (el != NULL && list != NULL) {
71 is_string(el, list->data, "list element matches");
73 el = va_arg(ap, char *);
76 if (el == NULL && list == NULL) {
77 ok(1, "List has correct number of elements");
78 } else if (el == NULL) {
79 ok(0, "List has too many elements");
80 } else if (list == NULL) {
81 ok(0, "List has too few elements");
86 main(int argc, char **argv)
89 struct cmd_syndesc *opts;
90 struct cmd_syndesc *retopts;
91 struct cmd_item *list;
96 char *retstring = NULL;
100 initialize_CMD_error_table();
102 opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL);
103 cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag");
104 cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option");
105 cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option");
107 /* A simple command line */
108 code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100);
109 is_int(0, code, "cmd_ParseLine succeeds");
110 code = cmd_Dispatch(tc, tv);
111 is_int(0, code, "dispatching simple comamnd line succeeds");
112 code = cmd_Parse(tc, tv, &retopts);
113 is_int(0, code, "parsing simple command line succeeds");
114 is_string("foo", retopts->parms[copt_first].items->data,
115 " ... 1st option matches");
116 is_string("bar", retopts->parms[copt_second].items->data,
117 " ... 2nd option matches");
118 ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches");
119 cmd_FreeOptions(&retopts);
123 code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100);
124 is_int(0, code, "cmd_ParseLine succeeds");
125 code = cmd_Dispatch(tc, tv);
126 is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected");
127 code = cmd_Parse(tc, tv, &retopts);
128 is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
131 /* missing parameter */
132 code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100);
133 is_int(0, code, "cmd_ParseLine succeeds");
134 code = cmd_Dispatch(tc, tv);
135 is_int(CMD_TOOFEW, code, "missing parameters fail as expected");
136 code = cmd_Parse(tc, tv, &retopts);
137 is_int(CMD_TOOFEW, code, "and still fail with cmd_Parse");
141 code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100);
142 is_int(0, code, "cmd_ParseLine succeeds");
143 code = cmd_Dispatch(tc, tv);
144 is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected");
145 code = cmd_Parse(tc, tv, &retopts);
146 is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
149 code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100);
150 is_int(0, code, "cmd_ParseLine succeeds");
151 code = cmd_Dispatch(tc, tv);
152 is_int(CMD_NOTLIST, code, "too many parameters fails as expected");
153 code = cmd_Parse(tc, tv, &retopts);
154 is_int(CMD_NOTLIST, code, "and still fail with cmd_Parse");
157 /* Positional parameters */
158 code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
159 is_int(0, code, "cmd_ParseLine succeeds");
160 code = cmd_Dispatch(tc, tv);
161 is_int(0, code, "dispatching positional parameters succeeds");
162 code = cmd_Parse(tc, tv, &retopts);
163 is_int(0, code, "and works with cmd_Parse");
164 cmd_FreeOptions(&retopts);
168 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
169 is_int(0, code, "cmd_ParseLine succeeds");
170 code = cmd_Dispatch(tc, tv);
171 is_int(0, code, "dispatching abbreviations succeeds");
172 code = cmd_Parse(tc, tv, &retopts);
173 is_int(0, code, "and works with cmd_Parse");
174 cmd_FreeOptions(&retopts);
179 code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100);
180 is_int(0, code, "cmd_ParseLine succeeds");
181 code = cmd_Dispatch(tc, tv);
182 is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail");
183 code = cmd_Parse(tc, tv, &retopts);
184 is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
187 /* Check that paramaters with abbreviation disabled don't make things
189 cmd_AddParmAtOffset(opts, copt_sugar, "-sugar", CMD_SINGLE,
190 CMD_OPTIONAL | CMD_NOABBRV, "sugar with that");
191 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
192 is_int(0, code, "cmd_ParseLine succeeds");
193 code = cmd_Dispatch(tc, tv);
194 is_int(0, code, "disabling specific abbreviations succeeds");
195 code = cmd_Parse(tc, tv, &retopts);
196 is_int(0, code, "and works with cmd_Parse into the bargain");
197 cmd_FreeOptions(&retopts);
200 /* Disable positional commands */
201 cmd_DisablePositionalCommands();
202 code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
203 is_int(0, code, "cmd_ParseLine succeeds");
204 code = cmd_Dispatch(tc, tv);
205 is_int(CMD_NOTLIST, code, "positional parameters can be disabled");
206 code = cmd_Parse(tc, tv, &retopts);
207 is_int(CMD_NOTLIST, code, "and fail with cmd_Parse too");
210 /* Disable abbreviations */
211 cmd_DisableAbbreviations();
212 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
213 is_int(0, code, "cmd_ParseLine succeeds");
214 code = cmd_Dispatch(tc, tv);
215 is_int(CMD_UNKNOWNSWITCH, code, "dispatching abbreviations succeeds");
216 code = cmd_Parse(tc, tv, &retopts);
217 is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
221 /* Try the new cmd_Parse function with something different*/
222 code = cmd_ParseLine("-first one -second two -flag", tv, &tc, 100);
223 is_int(0, code, "cmd_ParseLine succeeds");
224 code = cmd_Parse(tc, tv, &retopts);
225 is_int(0, code, "Parsing with cmd_Parse works");
226 is_string("one", retopts->parms[copt_first].items->data,
227 " ... 1st option matches");
228 is_string("two", retopts->parms[copt_second].items->data,
229 " ... 2nd option matches");
230 ok(retopts->parms[copt_flag].items != NULL,
231 " ... 3rd option matches");
233 cmd_FreeOptions(&retopts);
235 /* Try adding a couple of parameters at specific positions */
236 cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL,
238 cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL,
240 code = cmd_ParseLine("-first a -fourth b -fifth c", tv, &tc, 100);
241 is_int(0, code, "cmd_ParseLine succeeds");
242 code = cmd_Parse(tc, tv, &retopts);
243 is_int(0, code, "parsing our new options succeeds");
244 is_string("b", retopts->parms[copt_fourth].items->data,
245 " Fourth option in right place");
246 is_string("c", retopts->parms[copt_fifth].items->data,
247 " Fifth option in right place");
248 cmd_FreeOptions(&retopts);
251 /* Check Accessors */
252 code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100);
253 is_int(0, code, "cmd_ParseLine succeeds");
254 code = cmd_Parse(tc, tv, &retopts);
256 code = cmd_OptionAsInt(retopts, copt_first, &retval);
257 is_int(0, code, "cmd_OptionsAsInt succeeds");
258 is_int(1, retval, " ... and returns correct value");
259 ok(cmd_OptionPresent(retopts, copt_first),
260 " ... and is marked as present");
262 code = cmd_OptionAsString(retopts, copt_second, &retstring);
263 is_int(0, code, "cmd_OptionsAsString succeeds");
264 is_string("second", retstring, " ... and returns correct value");
267 ok(cmd_OptionPresent(retopts, copt_second),
268 " ... and is marked as present");
270 code = cmd_OptionAsFlag(retopts, copt_flag, &retval);
271 is_int(0, code, "cmd_OptionsAsFlag succeeds");
272 ok(retval, " ... and flag is correct");
273 ok(cmd_OptionPresent(retopts, copt_flag),
274 " ... and is marked as present");
276 ok(!cmd_OptionPresent(retopts, copt_sugar),
277 "Missing option is marked as such");
279 cmd_FreeOptions(&retopts);
283 code = cmd_AddParmAlias(opts, copt_second, "-twa");
284 is_int(0, code, "cmd_AddParmAlias succeeds");
286 code = cmd_ParseLine("-first 1 -twa tup", tv, &tc, 100);
287 is_int(0, code, "cmd_ParseLine succeeds");
288 code = cmd_Parse(tc, tv, &retopts);
289 is_int(0, code, "cmd_Parse succeeds for alias");
290 cmd_OptionAsString(retopts, copt_second, &retstring);
291 is_string("tup", retstring, " ... and we have the correct value");
295 cmd_FreeOptions(&retopts);
298 /* Add something that can be a flag or a value, and put something after
299 * it so we can check for parse problems*/
300 cmd_AddParm(opts, "-perhaps", CMD_SINGLE_OR_FLAG, CMD_OPTIONAL,
302 cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check");
304 /* Try using as an option */
306 code = cmd_ParseLine("-first 1 -perhaps 2 -sanity 3", tv, &tc, 100);
307 is_int(0, code, "cmd_ParseLine succeeds");
308 code = cmd_Parse(tc, tv, &retopts);
309 is_int(0, code, "cmd_Parse succeeds for option-as-flag as opt");
310 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
311 is_int(0, code, "cmd_OptionAsInt succeeds");
312 is_int(2, retval, " ... and we have the correct value");
313 cmd_FreeOptions(&retopts);
316 /* And now, as a flag */
318 code = cmd_ParseLine("-first 1 -perhaps -sanity 3", tv, &tc, 100);
319 is_int(0, code, "cmd_ParseLine succeeds");
320 code = cmd_Parse(tc, tv, &retopts);
321 is_int(0, code, "cmd_Parse succeeds for option-as-flag as flag");
322 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
323 is_int(CMD_MISSING, code, " ... pulling out a value fails as expected");
324 cmd_OptionAsFlag(retopts, copt_perhaps, &retval);
325 ok(retval, " ... but parsing as a flag works");
326 cmd_FreeOptions(&retopts);
329 /* Check that we can produce help output */
330 code = cmd_ParseLine("-help", tv, &tc, 100);
331 is_int(0, code, "cmd_ParseLine succeeds");
332 code = cmd_Parse(tc, tv, &retopts);
333 is_int(CMD_HELP, code, "cmd_Parse returns help indicator with help output");
334 ok(retopts == NULL, " ... and options is empty");
336 /* Check splitting with '=' */
338 code = cmd_ParseLine("-first 1 -perhaps=6 -sanity=3", tv, &tc, 100);
339 is_int(0, code, "cmd_ParseLine succeeds");
340 code = cmd_Parse(tc, tv, &retopts);
341 is_int(0, code, "cmd_Parse succeeds for items split with '='");
342 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
343 is_int(0, code, "cmd_OptionAsInt succeeds");
344 is_int(6, retval, " ... and we have the correct value once");
345 code = cmd_OptionAsInt(retopts, copt_sanity, &retval);
346 is_int(0, code, "cmd_OptionAsInt succeeds");
347 is_int(3, retval, " ... and we have the correct value twice");
348 cmd_FreeOptions(&retopts);
351 /* Check list behaviour */
352 code = cmd_ParseLine("-first 1 -second one two three", tv, &tc, 100);
353 is_int(0, code, "cmd_ParseLine succeeds");
354 code = cmd_Parse(tc, tv, &retopts);
355 is_int(0, code, "cmd_Parse succeeds for a list");
356 code = cmd_OptionAsList(retopts, copt_second, &list);
357 is_int(0, code, "cmd_OptionAsList succeeds");
358 checkList(list, "one", "two", "three", NULL);
359 cmd_FreeOptions(&retopts);
362 /* Now, try adding a configuration file into the mix */
363 if (getenv("SOURCE") == NULL)
364 path = strdup("test1.conf");
366 asprintf(&path, "%s/cmd/test1.conf", getenv("SOURCE"));
368 cmd_SetCommandName("test");
369 code = cmd_OpenConfigFile(path);
370 is_int(0, code, "cmd_OpenConfigFile succeeds");
372 code = cmd_ParseLine("-first 1", tv, &tc, 100);
373 is_int(0, code, "cmd_ParseLine succeeds");
374 code = cmd_Parse(tc, tv, &retopts);
375 is_int(0, code, "cmd_Parse succeeds when we have a config file");
376 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
377 is_int(0, code, "cmd_OptionsAsInt succeeds");
378 is_int(10, retval, " ... and we have the correct value for perhaps");
379 code = cmd_OptionAsString(retopts, copt_sanity, &retstring);
380 is_int(0, code, "cmd_OptionAsString succeeds");
381 is_string("testing", retstring,
382 " ... and we have the correct value for sanity");
384 /* Check breaking up a list of options */
385 code = cmd_OptionAsList(retopts, copt_second, &list);
386 is_int(0, code, "cmd_OptionAsList succeeds");
387 checkList(list, "one", "two", "three", "four", NULL);