bbd29974db450355843c9ebc36f3f3c1dbe927fd
[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 <tap/basic.h>
37
38 static int
39 testproc(struct cmd_syndesc *as, void *rock)
40 {
41     is_string("foo", as->parms[0].items->data,
42               "first option matches");
43     is_string("bar", as->parms[1].items->data,
44               "second option matches");
45     ok(as->parms[2].items != NULL,
46        "flag is set");
47
48     return 0;
49 }
50
51 int
52 main(int argc, char **argv)
53 {
54     char *tv[100];
55     struct cmd_syndesc *opts;
56     int code;
57     int tc;
58
59     plan(25);
60
61     initialize_CMD_error_table();
62
63     opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL);
64     cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option");
65     cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option");
66     cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag");
67
68     /* A simple command line */
69     code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100);
70     is_int(0, code, "cmd_ParseLine succeeds");
71     code = cmd_Dispatch(tc, tv);
72     is_int(0, code, "dispatching simple comamnd line succeeds");
73     cmd_FreeArgv(tv);
74
75     /* unknown switch */
76     code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100);
77     is_int(0, code, "cmd_ParseLine succeeds");
78     code = cmd_Dispatch(tc, tv);
79     is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected");
80     cmd_FreeArgv(tv);
81
82     /* missing parameter */
83     code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100);
84     is_int(0, code, "cmd_ParseLine succeeds");
85     code = cmd_Dispatch(tc, tv);
86     is_int(CMD_TOOFEW, code, "missing parameters fail as expected");
87     cmd_FreeArgv(tv);
88
89     /* missing option */
90     code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100);
91     is_int(0, code, "cmd_ParseLine succeeds");
92     code = cmd_Dispatch(tc, tv);
93     is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected");
94     cmd_FreeArgv(tv);
95
96     code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100);
97     is_int(0, code, "cmd_ParseLine succeeds");
98     code = cmd_Dispatch(tc, tv);
99     is_int(CMD_NOTLIST, code, "too many parameters fails as expected");
100     cmd_FreeArgv(tv);
101
102
103
104
105     /* Positional parameters */
106     code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
107     is_int(0, code, "cmd_ParseLine succeeds");
108     code = cmd_Dispatch(tc, tv);
109     is_int(0, code, "dispatching positional parameters succeeds");
110     cmd_FreeArgv(tv);
111
112     /* Abbreviations */
113     code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
114     is_int(0, code, "cmd_ParseLine succeeds");
115     code = cmd_Dispatch(tc, tv);
116     is_int(0, code, "dispatching abbreviations succeeds");
117     cmd_FreeArgv(tv);
118
119     /* Ambiguous */
120     code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100);
121     is_int(0, code, "cmd_ParseLine succeeds");
122     code = cmd_Dispatch(tc, tv);
123     is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail");
124     cmd_FreeArgv(tv);
125
126     return 0;
127 }