]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil/main.c
Following the license change, modify the boilerplates in test/
[thirdparty/openssl.git] / test / testutil / main.c
CommitLineData
e364c3b2 1/*
4db40c94 2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
e364c3b2 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
e364c3b2
EK
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
4db40c94 10#include "../testutil.h"
176db6dc 11#include "internal/nelem.h"
ad887416
P
12#include "output.h"
13#include "tu_local.h"
14
15#include <string.h>
16
17static size_t arg_count;
18static char **args;
19static unsigned char arg_used[1000];
20
21static void check_arg_usage(void)
22{
23 size_t i, n = arg_count < OSSL_NELEM(arg_used) ? arg_count
24 : OSSL_NELEM(arg_used);
25
26 for (i = 0; i < n; i++)
27 if (!arg_used[i+1])
28 test_printf_stderr("Warning ignored command-line argument %d: %s\n",
29 i, args[i+1]);
30 if (i < arg_count)
31 test_printf_stderr("Warning arguments %zu and later unchecked\n", i);
32}
e364c3b2
EK
33
34int main(int argc, char *argv[])
35{
ad887416
P
36 int ret = EXIT_FAILURE;
37
38 test_open_streams();
39
40 if (!global_init()) {
41 test_printf_stderr("Global init failed - aborting\n");
42 return ret;
43 }
44
45 arg_count = argc - 1;
46 args = argv;
47
48 setup_test_framework();
49
50 if (setup_tests())
51 ret = run_tests(argv[0]);
52 cleanup_tests();
53 check_arg_usage();
54
55 ret = pulldown_test_framework(ret);
56 test_close_streams();
57 return ret;
58}
59
60const char *test_get_program_name(void)
61{
62 return args[0];
63}
64
65char *test_get_argument(size_t n)
66{
67 if (n > arg_count)
68 return NULL;
69 if (n + 1 < OSSL_NELEM(arg_used))
70 arg_used[n + 1] = 1;
71 return args[n + 1];
72}
73
74size_t test_get_argument_count(void)
75{
76 return arg_count;
77}
e364c3b2 78
ad887416
P
79int test_has_option(const char *option)
80{
81 size_t i;
e364c3b2 82
ad887416
P
83 for (i = 1; i <= arg_count; i++)
84 if (strcmp(args[i], option) == 0) {
85 arg_used[i] = 1;
86 return 1;
87 }
88 return 0;
e364c3b2 89}
ad887416
P
90
91const char *test_get_option_argument(const char *option)
92{
93 size_t i, n = strlen(option);
94
95 for (i = 1; i <= arg_count; i++)
96 if (strncmp(args[i], option, n) == 0) {
97 arg_used[i] = 1;
98 if (args[i][n] == '\0' && i + 1 < arg_count) {
99 arg_used[++i] = 1;
100 return args[i];
101 }
102 return args[i] + n;
103 }
104 return NULL;
105}
106