Added better sanity checks in Diffie-Hellman key exchange.
[gnutls.git] / gl / tests / test-argp.c
blob6ba55e94d2991cf1f1ad24dedd9e781186ba6ff7
1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2012 Free Software Foundation, Inc.
3 This file is part of the GNUlib Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "argp.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #if HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28 #include "progname.h"
30 struct test_args
32 int test;
33 int verbose;
34 char *file;
35 int read;
36 char *hidden;
37 int opt;
38 char *optional;
39 int optional_set;
40 int group_2_1_option;
41 int group_1_1_option;
44 static struct argp_option group1_option[] = {
45 { NULL, 0, NULL, 0, "Option Group 1", 0 },
46 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
47 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
48 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
49 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 },
50 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
51 { NULL, 0, NULL, 0, NULL, 0 }
54 static error_t
55 group1_parser (int key, char *arg, struct argp_state *state)
57 struct test_args *args = state->input;
59 switch (key)
61 case 'v':
62 args->verbose++;
63 break;
65 case 'r':
66 args->read = 1;
67 /* fall through */
68 case 'f':
69 args->file = arg;
70 break;
72 case 'H':
73 args->hidden = arg;
74 break;
76 default:
77 return ARGP_ERR_UNKNOWN;
79 return 0;
82 struct argp group1_argp = {
83 group1_option,
84 group1_parser
87 struct argp_child group1_child = {
88 &group1_argp,
90 "",
95 static struct argp_option group1_1_option[] = {
96 { NULL, 0, NULL, 0, "Option Group 1.1", 0 },
97 { "cantiga", 'C', NULL, 0, "create a cantiga" },
98 { "sonet", 'S', NULL, 0, "create a sonet" },
99 { NULL, 0, NULL, 0, NULL, 0 }
102 static error_t
103 group1_1_parser (int key, char *arg, struct argp_state *state)
105 struct test_args *args = state->input;
106 switch (key)
108 case 'C':
109 case 'S':
110 args->group_1_1_option = key;
111 break;
112 default:
113 return ARGP_ERR_UNKNOWN;
115 return 0;
118 struct argp group1_1_argp = {
119 group1_1_option,
120 group1_1_parser
123 struct argp_child group1_1_child = {
124 &group1_1_argp,
131 static struct argp_option group2_option[] = {
132 { NULL, 0, NULL, 0, "Option Group 2", 0 },
133 { "option", 'O', NULL, 0, "An option", 1 },
134 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
135 "Option with an optional argument. ARG is one of the following:", 2 },
136 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 },
137 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 },
138 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 },
139 { NULL, 0, NULL, 0, NULL, 0 }
142 static error_t
143 group2_parser (int key, char *arg, struct argp_state *state)
145 struct test_args *args = state->input;
147 switch (key)
149 case 'O':
150 args->opt = 1;
151 break;
153 case 'o':
154 args->optional_set = 1;
155 args->optional = arg;
156 break;
158 default:
159 return ARGP_ERR_UNKNOWN;
161 return 0;
164 struct argp group2_argp = {
165 group2_option,
166 group2_parser
169 struct argp_child group2_child = {
170 &group2_argp,
177 static struct argp_option group2_1_option[] = {
178 { NULL, 0, NULL, 0, "Option Group 2.1", 0 },
179 { "poem", 'p', NULL, 0, "create a poem" },
180 { "limerick", 'l', NULL, 0, "create a limerick" },
181 { NULL, 0, NULL, 0, NULL, 0 }
184 static error_t
185 group2_1_parser (int key, char *arg, struct argp_state *state)
187 struct test_args *args = state->input;
188 switch (key)
190 case 'p':
191 case 'e':
192 args->group_2_1_option = key;
193 break;
194 default:
195 return ARGP_ERR_UNKNOWN;
197 return 0;
200 struct argp group2_1_argp = {
201 group2_1_option,
202 group2_1_parser
205 struct argp_child group2_1_child = {
206 &group2_1_argp,
213 static struct argp_option main_options[] = {
214 { NULL, 0, NULL, 0, "Main options", 0 },
215 { "test", 't', NULL, 0, NULL, 1 },
216 { NULL, 0, NULL, 0, NULL, 0 }
219 static error_t
220 parse_opt (int key, char *arg, struct argp_state *state)
222 struct test_args *args = state->input;
223 int i;
225 switch (key)
227 case ARGP_KEY_INIT:
228 for (i = 0; state->root_argp->children[i].argp; i++)
229 state->child_inputs[i] = args;
230 break;
232 case 't':
233 args->test = 1;
234 break;
236 default:
237 return ARGP_ERR_UNKNOWN;
239 return 0;
242 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
243 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
244 static char doc[] = "documentation string";
246 struct argp test_argp = {
247 main_options,
248 parse_opt,
249 "ARGS...",
250 doc,
251 NULL,
252 NULL,
253 NULL
256 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
257 #define ARGV0 "test-argp"
258 #define init_args(a) memset (&(a), 0, sizeof (a));
260 #define INIT_TEST_COMMON(n) \
261 int argc = NARGS (argv); \
262 struct test_args test_args; \
263 init_args (test_args); \
264 test_number = n;
266 #define INIT_TEST1(n, arg1) \
267 char *argv[] = { ARGV0, arg1, NULL }; \
268 INIT_TEST_COMMON (n)
270 #define INIT_TEST2(n, arg1, arg2) \
271 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
272 INIT_TEST_COMMON (n)
274 #define INIT_TEST3(n, arg1, arg2, arg3) \
275 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
276 INIT_TEST_COMMON (n)
278 int test_number;
279 unsigned failure_count = 0;
281 void
282 fail (const char *msg)
284 fprintf (stderr, "Test %d: %s\n", test_number, msg);
285 failure_count++;
288 void
289 test1 (struct argp *argp)
291 INIT_TEST1 (1, "--test");
292 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
293 fail ("argp_parse failed");
294 else if (test_args.test != 1)
295 fail ("option not processed");
298 void
299 test2 (struct argp *argp)
301 INIT_TEST1 (2, "-t");
302 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
303 fail ("argp_parse failed");
304 else if (test_args.test != 1)
305 fail ("option not processed");
308 void
309 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
311 if (argp_parse (argp, argc, argv, 0, NULL, args))
312 fail ("argp_parse failed");
313 else if (!args->file)
314 fail ("option not processed");
315 else if (strcmp (args->file, "FILE"))
316 fail ("option processed incorrectly");
319 void
320 test3 (struct argp *argp)
322 INIT_TEST1 (3, "--file=FILE");
323 test_file (argp, argc, argv, &test_args);
326 void
327 test4 (struct argp *argp)
329 INIT_TEST2 (4, "--file", "FILE");
330 test_file (argp, argc, argv, &test_args);
333 void
334 test5 (struct argp *argp)
336 INIT_TEST1 (5, "--input=FILE");
337 test_file (argp, argc, argv, &test_args);
340 void
341 test6 (struct argp *argp)
343 INIT_TEST2 (6, "--input", "FILE");
344 test_file (argp, argc, argv, &test_args);
347 void
348 test_optional (struct argp *argp, int argc, char **argv,
349 struct test_args *args, const char *val, const char *a)
351 int index;
352 if (argp_parse (argp, argc, argv, 0, &index, args))
353 fail ("argp_parse failed");
354 else if (!args->optional_set)
355 fail ("option not processed");
357 if (!val)
359 if (args->optional)
360 fail ("option processed incorrectly");
362 else if (strcmp (args->optional, val))
363 fail ("option processed incorrectly");
365 if (a)
367 if (index == argc)
368 fail ("expected command line argument not found");
369 else if (strcmp (argv[index], a))
370 fail ("expected command line argument does not match");
374 void
375 test7 (struct argp *argp)
377 INIT_TEST1 (7, "-oARG");
378 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
381 void
382 test8 (struct argp *argp)
384 INIT_TEST2 (8, "-o", "ARG");
385 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
388 void
389 test9 (struct argp *argp)
391 INIT_TEST1 (9, "--optional=ARG");
392 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
395 void
396 test10 (struct argp *argp)
398 INIT_TEST2 (10, "--optional", "ARG");
399 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
402 void
403 test11 (struct argp *argp)
405 INIT_TEST1 (11, "--optiona=ARG");
406 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
409 void
410 test12 (struct argp *argp)
412 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
413 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
416 void
417 test13 (struct argp *argp)
419 INIT_TEST1 (1, "--cantiga");
420 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
421 fail ("argp_parse failed");
422 else if (test_args.group_1_1_option != 'C')
423 fail ("option not processed");
426 void
427 test14 (struct argp *argp)
429 INIT_TEST1 (1, "--limerick");
430 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
431 fail ("argp_parse failed");
432 else if (test_args.group_2_1_option != 'l')
433 fail ("option not processed");
436 void
437 test15 (struct argp *argp)
439 INIT_TEST2 (1, "-r", "FILE");
440 test_file (argp, argc, argv, &test_args);
441 if (!test_args.read)
442 fail ("short alias not recognized properly");
446 typedef void (*test_fp) (struct argp *argp);
448 test_fp test_fun[] = {
449 test1, test2, test3, test4,
450 test5, test6, test7, test8,
451 test9, test10, test11, test12,
452 test13, test14, test15,
453 NULL
457 main (int argc, char **argv)
459 struct argp_child argp_children[3], group1_children[2], group2_children[2];
460 test_fp *fun;
462 set_program_name (argv[0]);
464 group1_children[0] = group1_1_child;
465 group1_children[1].argp = NULL;
466 group1_argp.children = group1_children;
468 group2_children[0] = group2_1_child;
469 group2_children[1].argp = NULL;
470 group2_argp.children = group2_children;
472 argp_children[0] = group1_child;
473 argp_children[1] = group2_child;
474 argp_children[2].argp = NULL;
475 test_argp.children = argp_children;
477 if (argc > 0)
479 struct test_args test_args;
480 init_args (test_args);
481 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
484 for (fun = test_fun; *fun; fun++)
485 (*fun) (&test_argp);
487 if (failure_count)
488 return 1;
490 return 0;