LINUX: dcache updates for mkdir and sillyrename
[openafs.git] / tests / cmd / command-t.c
1 /*
2  * Copyright (c) 2010 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 /*
26  * Test the command line parsing library
27  */
28
29 #include <afsconfig.h>
30 #include <afs/param.h>
31
32 #include <roken.h>
33
34 #include <afs/cmd.h>
35
36 #include <tests/tap/basic.h>
37
38 enum cmdOptions {
39    copt_flag = 0,
40    copt_first,
41    copt_second,
42    copt_sugar,
43    copt_fourth,
44    copt_fifth,
45    copt_perhaps,
46    copt_sanity
47 };
48
49 static int
50 testproc(struct cmd_syndesc *as, void *rock)
51 {
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,
57        "flag is set");
58
59     return 0;
60 }
61
62 static void
63 checkList(struct cmd_item *list, ...)
64 {
65     va_list ap;
66     char *el;
67
68     va_start(ap, list);
69     el = va_arg(ap, char *);
70     while (el != NULL && list != NULL) {
71        is_string(el, list->data, "list element matches");
72        list = list->next;
73        el = va_arg(ap, char *);
74     }
75
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");
82     }
83 }
84
85 int
86 main(int argc, char **argv)
87 {
88     char *tv[100];
89     struct cmd_syndesc *opts;
90     struct cmd_syndesc *retopts;
91     struct cmd_item *list;
92     int code;
93     int tc;
94     int retval;
95     char *path;
96     char *retstring = NULL;
97
98     plan(109);
99
100     initialize_CMD_error_table();
101
102     opts = cmd_CreateSyntax(NULL, testproc, NULL, 0, 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");
106
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);
120     cmd_FreeArgv(tv);
121
122     /* unknown switch */
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");
129     cmd_FreeArgv(tv);
130
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");
138     cmd_FreeArgv(tv);
139
140     /* missing option */
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");
147     cmd_FreeArgv(tv);
148
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");
155     cmd_FreeArgv(tv);
156
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);
165     cmd_FreeArgv(tv);
166
167     /* Abbreviations */
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);
175
176     cmd_FreeArgv(tv);
177
178     /* Ambiguous */
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");
185     cmd_FreeArgv(tv);
186
187     /* Check that paramaters with abbreviation disabled don't make things
188      * ambiguous */
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);
198     cmd_FreeArgv(tv);
199
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");
208     cmd_FreeArgv(tv);
209
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");
218
219     cmd_FreeArgv(tv);
220
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");
232
233     cmd_FreeOptions(&retopts);
234     cmd_FreeArgv(tv);
235     /* Try adding a couple of parameters at specific positions */
236     cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL,
237                        "fifth option");
238     cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL,
239                        "fourth option" );
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);
249     cmd_FreeArgv(tv);
250
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);
255
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");
261
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");
265     free(retstring);
266     retstring = NULL;
267     ok(cmd_OptionPresent(retopts, copt_second),
268        " ... and is marked as present");
269
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");
275
276     ok(!cmd_OptionPresent(retopts, copt_sugar),
277        "Missing option is marked as such");
278
279     cmd_FreeOptions(&retopts);
280     cmd_FreeArgv(tv);
281
282     /* Add an alias */
283     code = cmd_AddParmAlias(opts, copt_second, "-twa");
284     is_int(0, code, "cmd_AddParmAlias succeeds");
285
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");
292     free(retstring);
293     retstring = NULL;
294
295     cmd_FreeOptions(&retopts);
296     cmd_FreeArgv(tv);
297
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,
301                 "what am I");
302     cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check");
303
304     /* Try using as an option */
305
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);
314     cmd_FreeArgv(tv);
315
316     /* And now, as a flag */
317
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);
327     cmd_FreeArgv(tv);
328
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");
335
336     /* Check splitting with '=' */
337
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);
349     cmd_FreeArgv(tv);
350
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);
360     cmd_FreeArgv(tv);
361
362     /* Now, try adding a configuration file into the mix */
363     if (getenv("SOURCE") == NULL)
364         path = strdup("test1.conf");
365     else {
366         if (asprintf(&path, "%s/cmd/test1.conf", getenv("SOURCE")) < 0)
367             path = NULL;
368     }
369     if (path != NULL) {
370         cmd_SetCommandName("test");
371         code = cmd_OpenConfigFile(path);
372         is_int(0, code, "cmd_OpenConfigFile succeeds");
373     } else {
374         skip("no memory to build config file path");
375     }
376
377     code = cmd_ParseLine("-first 1", tv, &tc, 100);
378     is_int(0, code, "cmd_ParseLine succeeds");
379     code = cmd_Parse(tc, tv, &retopts);
380     is_int(0, code, "cmd_Parse succeeds when we have a config file");
381     code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
382     is_int(0, code, "cmd_OptionsAsInt succeeds");
383     is_int(10, retval, " ... and we have the correct value for perhaps");
384     code = cmd_OptionAsString(retopts, copt_sanity, &retstring);
385     is_int(0, code, "cmd_OptionAsString succeeds");
386     is_string("testing", retstring,
387               " ... and we have the correct value for sanity");
388
389     /* Check breaking up a list of options */
390     code = cmd_OptionAsList(retopts, copt_second, &list);
391     is_int(0, code, "cmd_OptionAsList succeeds");
392     checkList(list, "one", "two", "three", "four", NULL);
393
394     return 0;
395 }
396