configtest.c revision 1.1
1/* $OpenBSD: configtest.c,v 1.1 2017/12/09 16:43:09 jsing Exp $ */
2/*
3 * Copyright (c) 2017 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 <err.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22#include <tls.h>
23
24struct parse_protocols_test {
25	const char *protostr;
26	int want_return;
27	uint32_t want_protocols;
28};
29
30struct parse_protocols_test parse_protocols_tests[] = {
31	{
32		.protostr = NULL,
33		.want_return = 0,
34		.want_protocols = TLS_PROTOCOLS_DEFAULT,
35	},
36	{
37		.protostr = "default",
38		.want_return = 0,
39		.want_protocols = TLS_PROTOCOLS_DEFAULT,
40	},
41	{
42		.protostr = "secure",
43		.want_return = 0,
44		.want_protocols = TLS_PROTOCOLS_DEFAULT,
45	},
46	{
47		.protostr = "all",
48		.want_return = 0,
49		.want_protocols = TLS_PROTOCOLS_ALL,
50	},
51	{
52		.protostr = "tlsv1",
53		.want_return = 0,
54		.want_protocols = TLS_PROTOCOL_TLSv1,
55	},
56	{
57		.protostr = "tlsv1.2",
58		.want_return = 0,
59		.want_protocols = TLS_PROTOCOL_TLSv1_2,
60	},
61	{
62		.protostr = "",
63		.want_return = -1,
64		.want_protocols = 0,
65	},
66	{
67		.protostr = "tlsv1.0:tlsv1.1:tlsv1.2",
68		.want_return = 0,
69		.want_protocols = TLS_PROTOCOL_TLSv1_0 | TLS_PROTOCOL_TLSv1_1 |
70		    TLS_PROTOCOL_TLSv1_2,
71	},
72	{
73		.protostr = "tlsv1.0,tlsv1.1,tlsv1.2",
74		.want_return = 0,
75		.want_protocols = TLS_PROTOCOL_TLSv1_0 | TLS_PROTOCOL_TLSv1_1 |
76		    TLS_PROTOCOL_TLSv1_2,
77	},
78	{
79		.protostr = "tlsv1.1,tlsv1.2,tlsv1.0",
80		.want_return = 0,
81		.want_protocols = TLS_PROTOCOL_TLSv1_0 | TLS_PROTOCOL_TLSv1_1 |
82		    TLS_PROTOCOL_TLSv1_2,
83	},
84	{
85		.protostr = "tlsv1.1,tlsv1.2,tlsv1.1",
86		.want_return = 0,
87		.want_protocols = TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2,
88	},
89	{
90		.protostr = "tlsv1.1,tlsv1.2,!tlsv1.1",
91		.want_return = 0,
92		.want_protocols = TLS_PROTOCOL_TLSv1_2,
93	},
94	{
95		.protostr = "unknown",
96		.want_return = -1,
97		.want_protocols = 0,
98	},
99	{
100		.protostr = "all,!unknown",
101		.want_return = -1,
102		.want_protocols = 0,
103	},
104	{
105		.protostr = "sslv3,tlsv1.0,tlsv1.1,tlsv1.2",
106		.want_return = -1,
107		.want_protocols = 0,
108	},
109	{
110		.protostr = "all,!tlsv1.0",
111		.want_return = 0,
112		.want_protocols = TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2,
113	},
114	{
115		.protostr = "!tlsv1.0",
116		.want_return = 0,
117		.want_protocols = TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2,
118	},
119	{
120		.protostr = "!tlsv1.0,!tlsv1.1",
121		.want_return = 0,
122		.want_protocols = TLS_PROTOCOL_TLSv1_2,
123	},
124	{
125		.protostr = "!tlsv1.0,!tlsv1.1,tlsv1.2",
126		.want_return = 0,
127		.want_protocols = TLS_PROTOCOL_TLSv1_2,
128	},
129};
130
131#define N_PARSE_PROTOCOLS_TESTS \
132    (sizeof(parse_protocols_tests) / sizeof(*parse_protocols_tests))
133
134static int
135do_parse_protocols_test(int test_no, struct parse_protocols_test *ppt)
136{
137	uint32_t protocols = 0;
138	int failed = 1;
139	int rv;
140
141	rv = tls_config_parse_protocols(&protocols, ppt->protostr);
142	if (rv != ppt->want_return) {
143		fprintf(stderr, "FAIL: test %i - tls_config_parse_protocols() "
144		    "returned %i, want %i\n", test_no, rv, ppt->want_return);
145		goto done;
146	}
147	if (protocols != ppt->want_protocols) {
148		fprintf(stderr, "FAIL: test %i - got protocols 0x%x, "
149		    "want 0x%x\n", test_no, protocols, ppt->want_protocols);
150		goto done;
151	}
152
153	failed = 0;
154
155 done:
156	return (failed);
157}
158
159int
160main(int argc, char **argv)
161{
162	int failed = 0;
163	size_t i;
164
165	tls_init();
166
167	for (i = 0; i < N_PARSE_PROTOCOLS_TESTS; i++)
168		failed += do_parse_protocols_test(i, &parse_protocols_tests[i]);
169
170	return (failed);
171}
172