summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/regress/usr.bin/openssl/Makefile4
-rw-r--r--src/regress/usr.bin/openssl/options/Makefile17
-rw-r--r--src/regress/usr.bin/openssl/options/optionstest.c219
3 files changed, 239 insertions, 1 deletions
diff --git a/src/regress/usr.bin/openssl/Makefile b/src/regress/usr.bin/openssl/Makefile
index 800e2fd061..994e1952d2 100644
--- a/src/regress/usr.bin/openssl/Makefile
+++ b/src/regress/usr.bin/openssl/Makefile
@@ -1,4 +1,6 @@
1# $OpenBSD: Makefile,v 1.1 2014/08/26 17:50:07 jsing Exp $ 1# $OpenBSD: Makefile,v 1.2 2014/12/28 14:01:33 jsing Exp $
2
3SUBDIR= options
2 4
3CLEANFILES+= testdsa.key testdsa.pem rsakey.pem rsacert.pem dsa512.pem 5CLEANFILES+= testdsa.key testdsa.pem rsakey.pem rsacert.pem dsa512.pem
4 6
diff --git a/src/regress/usr.bin/openssl/options/Makefile b/src/regress/usr.bin/openssl/options/Makefile
new file mode 100644
index 0000000000..ba3857ad39
--- /dev/null
+++ b/src/regress/usr.bin/openssl/options/Makefile
@@ -0,0 +1,17 @@
1# $OpenBSD: Makefile,v 1.1 2014/12/28 14:01:33 jsing Exp $
2
3PROG= optionstest
4SRCS= optionstest.c
5
6LDADD= -lcrypto -lssl
7DPADD= ${LIBCRYPTO} ${LIBSSL}
8
9OSSLSRC= ${.CURDIR}/../../../../usr.bin/openssl/
10CFLAGS+= -I${OSSLSRC}
11
12.PATH: ${OSSLSRC}
13SRCS+= apps.c
14
15CFLAGS+= -Werror
16
17.include <bsd.regress.mk>
diff --git a/src/regress/usr.bin/openssl/options/optionstest.c b/src/regress/usr.bin/openssl/options/optionstest.c
new file mode 100644
index 0000000000..8df208dc84
--- /dev/null
+++ b/src/regress/usr.bin/openssl/options/optionstest.c
@@ -0,0 +1,219 @@
1/* $OpenBSD: optionstest.c,v 1.1 2014/12/28 14:01:33 jsing Exp $ */
2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <openssl/bio.h>
23#include <openssl/conf.h>
24
25#include "apps.h"
26
27/* Needed to keep apps.c happy... */
28BIO *bio_err;
29CONF *config;
30
31static int argfunc(struct option *opt, char *arg);
32
33static struct {
34 char *arg;
35 int flag;
36} test_config;
37
38static struct option test_options[] = {
39 {
40 .name = "arg",
41 .type = OPTION_ARG,
42 .opt.arg = &test_config.arg,
43 },
44 {
45 .name = "argfunc",
46 .type = OPTION_ARG_FUNC,
47 .func = argfunc,
48 },
49 {
50 .name = "flag",
51 .type = OPTION_FLAG,
52 .opt.flag = &test_config.flag,
53 },
54 { NULL },
55};
56
57char *args1[] = { "opts" };
58char *args2[] = { "opts", "-arg", "arg", "-flag" };
59char *args3[] = { "opts", "-arg", "arg", "-flag", "unnamed" };
60char *args4[] = { "opts", "-arg", "arg", "unnamed", "-flag" };
61char *args5[] = { "opts", "unnamed1", "-arg", "arg", "-flag", "unnamed2" };
62char *args6[] = { "opts", "-argfunc", "arg", "-flag" };
63
64struct options_test {
65 int argc;
66 char **argv;
67 enum {
68 OPTIONS_TEST_NONE,
69 OPTIONS_TEST_UNNAMED,
70 } type;
71 char *unnamed;
72 int used;
73 int want;
74 char *wantarg;
75 int wantflag;
76};
77
78struct options_test options_tests[] = {
79 {
80 /* No arguments (only program name). */
81 .argc = 1,
82 .argv = args1,
83 .type = OPTIONS_TEST_NONE,
84 .want = 0,
85 .wantarg = NULL,
86 .wantflag = 0,
87 },
88 {
89 /* Named arguments (unnamed not permitted). */
90 .argc = 4,
91 .argv = args2,
92 .type = OPTIONS_TEST_NONE,
93 .want = 0,
94 .wantarg = "arg",
95 .wantflag = 1,
96 },
97 {
98 /* Named arguments (unnamed permitted). */
99 .argc = 4,
100 .argv = args2,
101 .type = OPTIONS_TEST_UNNAMED,
102 .unnamed = NULL,
103 .want = 0,
104 .wantarg = "arg",
105 .wantflag = 1,
106 },
107 {
108 /* Named and single unnamed (unnamed not permitted). */
109 .argc = 5,
110 .argv = args3,
111 .type = OPTIONS_TEST_NONE,
112 .want = 1,
113 },
114 {
115 /* Named and single unnamed (unnamed permitted). */
116 .argc = 5,
117 .argv = args3,
118 .type = OPTIONS_TEST_UNNAMED,
119 .unnamed = "unnamed",
120 .want = 0,
121 .wantarg = "arg",
122 .wantflag = 1,
123 },
124 {
125 /* Named and single unnamed (different sequence). */
126 .argc = 5,
127 .argv = args4,
128 .type = OPTIONS_TEST_UNNAMED,
129 .unnamed = "unnamed",
130 .want = 0,
131 .wantarg = "arg",
132 .wantflag = 1,
133 },
134 {
135 /* Multiple unnamed arguments (retain last). */
136 .argc = 6,
137 .argv = args5,
138 .type = OPTIONS_TEST_UNNAMED,
139 .unnamed = "unnamed2",
140 .want = 0,
141 .wantarg = "arg",
142 .wantflag = 1,
143 },
144 {
145 /* Function. */
146 .argc = 4,
147 .argv = args6,
148 .type = OPTIONS_TEST_NONE,
149 .want = 0,
150 .wantarg = "arg",
151 .wantflag = 1,
152 },
153};
154
155#define N_OPTIONS_TESTS \
156 (sizeof(options_tests) / sizeof(*options_tests))
157
158int
159argfunc(struct option *opt, char *arg)
160{
161 test_config.arg = arg;
162 return (0);
163}
164
165static int
166do_options_test(int test_no, struct options_test *ot)
167{
168 char *unnamed = NULL;
169 char **arg = NULL;
170 int ret;
171
172 if (ot->type == OPTIONS_TEST_UNNAMED)
173 arg = &unnamed;
174
175 memset(&test_config, 0, sizeof(test_config));
176 ret = options_parse(ot->argc, ot->argv, test_options, arg);
177 if (ret != ot->want) {
178 fprintf(stderr, "FAIL: test %i options_parse() returned %i, "
179 "want %i\n", test_no, ret, ot->want);
180 return (1);
181 }
182 if (ret != 0)
183 return (0);
184
185 if ((test_config.arg != NULL || ot->wantarg != NULL) &&
186 (test_config.arg == NULL || ot->wantarg == NULL ||
187 strcmp(test_config.arg, ot->wantarg) != 0)) {
188 fprintf(stderr, "FAIL: test %i got arg '%s', want '%s'\n",
189 test_no, test_config.arg, ot->wantarg);
190 return (1);
191 }
192 if (test_config.flag != ot->wantflag) {
193 fprintf(stderr, "FAIL: test %i got flag %i, want %i\n",
194 test_no, test_config.flag, ot->wantflag);
195 return (1);
196 }
197 if (ot->type == OPTIONS_TEST_UNNAMED &&
198 (unnamed != NULL || ot->unnamed != NULL) &&
199 (unnamed == NULL || ot->unnamed == NULL ||
200 strcmp(unnamed, ot->unnamed) != 0)) {
201 fprintf(stderr, "FAIL: test %i got unnamed '%s', want '%s'\n",
202 test_no, unnamed, ot->unnamed);
203 return (1);
204 }
205
206 return (0);
207}
208
209int
210main(int argc, char **argv)
211{
212 int failed = 0;
213 size_t i;
214
215 for (i = 0; i < N_OPTIONS_TESTS; i++)
216 failed += do_options_test(i, &options_tests[i]);
217
218 return (failed);
219}