xref: /src/crypto/openssl/test/testutil/options.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include "../testutil.h"
11 #include "internal/nelem.h"
12 #include "tu_local.h"
13 #include "output.h"
14 
15 static int used[100] = { 0 };
16 
test_skip_common_options(void)17 int test_skip_common_options(void)
18 {
19     OPTION_CHOICE_DEFAULT o;
20 
21     while ((o = (OPTION_CHOICE_DEFAULT)opt_next()) != OPT_EOF) {
22         switch (o) {
23         case OPT_TEST_CASES:
24             break;
25         default:
26         case OPT_ERR:
27             return 0;
28         }
29     }
30     return 1;
31 }
32 
test_get_argument_count(void)33 size_t test_get_argument_count(void)
34 {
35     return opt_num_rest();
36 }
37 
test_get_argument(size_t n)38 char *test_get_argument(size_t n)
39 {
40     char **argv = opt_rest();
41 
42     OPENSSL_assert(n < sizeof(used));
43     if ((int)n >= opt_num_rest() || argv == NULL)
44         return NULL;
45     used[n] = 1;
46     return argv[n];
47 }
48 
opt_check_usage(void)49 void opt_check_usage(void)
50 {
51     int i;
52     char **argv = opt_rest();
53     int n, arg_count = opt_num_rest();
54 
55     if (arg_count > (int)OSSL_NELEM(used))
56         n = (int)OSSL_NELEM(used);
57     else
58         n = arg_count;
59     for (i = 0; i < n; i++) {
60         if (used[i] == 0)
61             test_printf_stderr("Warning ignored command-line argument %d: %s\n",
62                 i, argv[i]);
63     }
64     if (i < arg_count)
65         test_printf_stderr("Warning arguments %d and later unchecked\n", i);
66 }
67 
opt_printf_stderr(const char * fmt,...)68 int opt_printf_stderr(const char *fmt, ...)
69 {
70     va_list ap;
71     int ret;
72 
73     va_start(ap, fmt);
74     ret = test_vprintf_stderr(fmt, ap);
75     va_end(ap);
76     return ret;
77 }
78