summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2022-07-21 03:59:04 +0000
committertb <>2022-07-21 03:59:04 +0000
commit8f58a18c1565c03748923cc25ee3d0f60c1f375f (patch)
treedfcb687d678d0c0ec1cf4577d5601efdb4461cad /src
parent3ee05e46168e73e4e329e7fd818d2a3ff08ec6dd (diff)
downloadopenbsd-8f58a18c1565c03748923cc25ee3d0f60c1f375f.tar.gz
openbsd-8f58a18c1565c03748923cc25ee3d0f60c1f375f.tar.bz2
openbsd-8f58a18c1565c03748923cc25ee3d0f60c1f375f.zip
Make test table based, extend it a little
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libssl/unit/ssl_set_alpn_protos.c186
1 files changed, 117 insertions, 69 deletions
diff --git a/src/regress/lib/libssl/unit/ssl_set_alpn_protos.c b/src/regress/lib/libssl/unit/ssl_set_alpn_protos.c
index e32cf83f30..87dd4d9e5a 100644
--- a/src/regress/lib/libssl/unit/ssl_set_alpn_protos.c
+++ b/src/regress/lib/libssl/unit/ssl_set_alpn_protos.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_set_alpn_protos.c,v 1.1 2022/07/20 14:50:03 tb Exp $ */ 1/* $OpenBSD: ssl_set_alpn_protos.c,v 1.2 2022/07/21 03:59:04 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -20,34 +20,124 @@
20 20
21#include <openssl/ssl.h> 21#include <openssl/ssl.h>
22 22
23static const uint8_t valid[] = { 23struct alpn_test {
24 6, 's', 'p', 'd', 'y', '/', '1', 24 const char *description;
25 8, 'h', 't', 't', 'p', '/', '1', '.', '1', 25 const uint8_t protocols[24];
26 size_t protocols_len;
27 int ret;
26}; 28};
27 29
28static const uint8_t invalid_len1[] = { 30static const struct alpn_test alpn_tests[] = {
29 0, 31 {
32 .description = "valid protocol list",
33 .protocols = {
34 6, 's', 'p', 'd', 'y', '/', '1',
35 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
36 },
37 .protocols_len = 16,
38 .ret = 0,
39 },
40 {
41 .description = "zero length protocol",
42 .protocols = {
43 0,
44 },
45 .protocols_len = 1,
46 .ret = 1,
47 },
48 {
49 .description = "zero length protocol at start",
50 .protocols = {
51 0,
52 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
53 6, 's', 'p', 'd', 'y', '/', '1',
54 },
55 .protocols_len = 17,
56 .ret = 1,
57 },
58 {
59 .description = "zero length protocol embedded",
60 .protocols = {
61 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
62 0,
63 6, 's', 'p', 'd', 'y', '/', '1',
64 },
65 .protocols_len = 17,
66 .ret = 1,
67 },
68 {
69 .description = "zero length protocol at end",
70 .protocols = {
71 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
72 6, 's', 'p', 'd', 'y', '/', '1',
73 0,
74 },
75 .protocols_len = 17,
76 .ret = 1,
77 },
78 {
79 .description = "protocol length too short",
80 .protocols = {
81 6, 'h', 't', 't', 'p', '/', '1', '.', '1',
82 },
83 .protocols_len = 9,
84 .ret = 1,
85 },
86 {
87 .description = "protocol length too long",
88 .protocols = {
89 8, 's', 'p', 'd', 'y', '/', '1',
90 },
91 .protocols_len = 7,
92 .ret = 1,
93 },
30}; 94};
31 95
32static const uint8_t invalid_contains_len0_proto[] = { 96static const size_t N_ALPN_TESTS = sizeof(alpn_tests) / sizeof(alpn_tests[0]);
33 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
34 0,
35 6, 's', 'p', 'd', 'y', '/', '1',
36};
37 97
38static const uint8_t invalid_proto_len_too_short[] = { 98static int
39 6, 'h', 't', 't', 'p', '/', '1', '.', '1', 99test_ssl_set_alpn_protos(const struct alpn_test *tc)
40}; 100{
101 SSL_CTX *ctx;
102 SSL *ssl;
103 int ret;
104 int failed = 0;
41 105
42static const uint8_t invalid_proto_len_too_long[] = { 106 if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL)
43 8, 's', 'p', 'd', 'y', '/', '1', 107 errx(1, "SSL_CTX_new");
44}; 108
109 ret = SSL_CTX_set_alpn_protos(ctx, tc->protocols, tc->protocols_len);
110 if (ret != tc->ret) {
111 warnx("%s: setting on SSL_CTX: want %d, got %d",
112 tc->description, tc->ret, ret);
113 failed = 1;
114 }
115
116 if ((ssl = SSL_new(ctx)) == NULL)
117 errx(1, "SSL_new");
118
119 ret = SSL_set_alpn_protos(ssl, tc->protocols, tc->protocols_len);
120 if (ret != tc->ret) {
121 warnx("%s: setting on SSL: want %d, got %d",
122 tc->description, tc->ret, ret);
123 failed = 1;
124 }
125
126 SSL_CTX_free(ctx);
127 SSL_free(ssl);
128
129 return failed;
130}
45 131
46static int 132static int
47test_ssl_set_alpn_protos(void) 133test_ssl_set_alpn_protos_edge_cases(void)
48{ 134{
49 SSL_CTX *ctx; 135 SSL_CTX *ctx;
50 SSL *ssl = NULL; 136 SSL *ssl;
137 const uint8_t valid[] = {
138 6, 's', 'p', 'd', 'y', '/', '3',
139 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
140 };
51 int failed = 0; 141 int failed = 0;
52 142
53 if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL) 143 if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL)
@@ -57,7 +147,6 @@ test_ssl_set_alpn_protos(void)
57 warnx("setting valid protocols on SSL_CTX failed"); 147 warnx("setting valid protocols on SSL_CTX failed");
58 failed = 1; 148 failed = 1;
59 } 149 }
60
61 if (SSL_CTX_set_alpn_protos(ctx, NULL, 0) != 0) { 150 if (SSL_CTX_set_alpn_protos(ctx, NULL, 0) != 0) {
62 warnx("setting 'NULL, 0' on SSL_CTX failed"); 151 warnx("setting 'NULL, 0' on SSL_CTX failed");
63 failed = 1; 152 failed = 1;
@@ -67,31 +156,7 @@ test_ssl_set_alpn_protos(void)
67 failed = 1; 156 failed = 1;
68 } 157 }
69 if (SSL_CTX_set_alpn_protos(ctx, NULL, 43) != 0) { 158 if (SSL_CTX_set_alpn_protos(ctx, NULL, 43) != 0) {
70 warnx("setting 'valid, 43' on SSL_CTX failed"); 159 warnx("setting 'NULL, 43' on SSL_CTX failed");
71 failed = 1;
72 }
73
74 if (SSL_CTX_set_alpn_protos(ctx, invalid_len1, sizeof(invalid_len1))
75 != 1) {
76 warnx("setting invalid_len1 on SSL_CTX succeeded");
77 failed = 1;
78 }
79 if (SSL_CTX_set_alpn_protos(ctx, invalid_contains_len0_proto,
80 sizeof(invalid_contains_len0_proto)) != 1) {
81 warnx("setting invalid_contains_len0_proto on SSL_CTX "
82 "succeeded");
83 failed = 1;
84 }
85 if (SSL_CTX_set_alpn_protos(ctx, invalid_proto_len_too_short,
86 sizeof(invalid_proto_len_too_short)) != 1) {
87 warnx("setting invalid_proto_len_too_short on SSL_CTX "
88 "succeeded");
89 failed = 1;
90 }
91 if (SSL_CTX_set_alpn_protos(ctx, invalid_proto_len_too_long,
92 sizeof(invalid_proto_len_too_long)) != 1) {
93 warnx("setting invalid_proto_len_too_long on SSL_CTX "
94 "succeeded");
95 failed = 1; 160 failed = 1;
96 } 161 }
97 162
@@ -111,28 +176,7 @@ test_ssl_set_alpn_protos(void)
111 failed = 1; 176 failed = 1;
112 } 177 }
113 if (SSL_set_alpn_protos(ssl, NULL, 43) != 0) { 178 if (SSL_set_alpn_protos(ssl, NULL, 43) != 0) {
114 warnx("setting 'valid, 43' on SSL failed"); 179 warnx("setting 'NULL, 43' on SSL failed");
115 failed = 1;
116 }
117
118 if (SSL_set_alpn_protos(ssl, invalid_len1, sizeof(invalid_len1))
119 != 1) {
120 warnx("setting invalid_len1 on SSL succeeded");
121 failed = 1;
122 }
123 if (SSL_set_alpn_protos(ssl, invalid_contains_len0_proto,
124 sizeof(invalid_contains_len0_proto)) != 1) {
125 warnx("setting invalid_contains_len0_proto on SSL succeeded");
126 failed = 1;
127 }
128 if (SSL_set_alpn_protos(ssl, invalid_proto_len_too_short,
129 sizeof(invalid_proto_len_too_short)) != 1) {
130 warnx("setting invalid_proto_len_too_short on SSL succeeded");
131 failed = 1;
132 }
133 if (SSL_set_alpn_protos(ssl, invalid_proto_len_too_long,
134 sizeof(invalid_proto_len_too_long)) != 1) {
135 warnx("setting invalid_proto_len_too_long on SSL succeeded");
136 failed = 1; 180 failed = 1;
137 } 181 }
138 182
@@ -145,9 +189,13 @@ test_ssl_set_alpn_protos(void)
145int 189int
146main(void) 190main(void)
147{ 191{
148 int failed; 192 size_t i;
193 int failed = 0;
194
195 for (i = 0; i < N_ALPN_TESTS; i++)
196 failed |= test_ssl_set_alpn_protos(&alpn_tests[i]);
149 197
150 failed = test_ssl_set_alpn_protos(); 198 failed |= test_ssl_set_alpn_protos_edge_cases();
151 199
152 if (!failed) 200 if (!failed)
153 printf("PASS %s\n", __FILE__); 201 printf("PASS %s\n", __FILE__);