summaryrefslogtreecommitdiff
path: root/src/regress/lib/libssl
diff options
context:
space:
mode:
authorjsing <>2022-07-22 19:34:55 +0000
committerjsing <>2022-07-22 19:34:55 +0000
commit3890176708c6f990b2e0bc4a469efc1eab093c1e (patch)
tree4e5ff87620a1b8d1cb504812f92a37391a71a20e /src/regress/lib/libssl
parent6612d759045c7571f6be2ff1366a704ba82eb216 (diff)
downloadopenbsd-3890176708c6f990b2e0bc4a469efc1eab093c1e.tar.gz
openbsd-3890176708c6f990b2e0bc4a469efc1eab093c1e.tar.bz2
openbsd-3890176708c6f990b2e0bc4a469efc1eab093c1e.zip
Extend TLS buffer regress to cover read/write usage.
Diffstat (limited to 'src/regress/lib/libssl')
-rw-r--r--src/regress/lib/libssl/buffer/buffertest.c232
1 files changed, 219 insertions, 13 deletions
diff --git a/src/regress/lib/libssl/buffer/buffertest.c b/src/regress/lib/libssl/buffer/buffertest.c
index 45742dec4e..3dfad7c44f 100644
--- a/src/regress/lib/libssl/buffer/buffertest.c
+++ b/src/regress/lib/libssl/buffer/buffertest.c
@@ -1,5 +1,6 @@
1/* $OpenBSD: buffertest.c,v 1.6 2022/07/22 19:34:55 jsing Exp $ */
1/* 2/*
2 * Copyright (c) 2019 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2019, 2022 Joel Sing <jsing@openbsd.org>
3 * 4 *
4 * Permission to use, copy, modify, and distribute this software for any 5 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above 6 * purpose with or without fee is hereby granted, provided that the above
@@ -59,7 +60,7 @@ struct extend_test {
59 ssize_t want_ret; 60 ssize_t want_ret;
60}; 61};
61 62
62struct extend_test extend_tests[] = { 63const struct extend_test extend_tests[] = {
63 { 64 {
64 .extend_len = 4, 65 .extend_len = 4,
65 .read_len = 0, 66 .read_len = 0,
@@ -99,16 +100,17 @@ struct extend_test extend_tests[] = {
99 100
100#define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0])) 101#define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0]))
101 102
102int 103static int
103main(int argc, char **argv) 104tls_buffer_extend_test(void)
104{ 105{
106 const struct extend_test *et;
105 struct tls_buffer *buf; 107 struct tls_buffer *buf;
106 struct extend_test *et;
107 struct read_state rs; 108 struct read_state rs;
108 uint8_t *data; 109 uint8_t *data = NULL;
109 size_t i, data_len; 110 size_t i, data_len;
110 ssize_t ret; 111 ssize_t ret;
111 CBS cbs; 112 CBS cbs;
113 int failed = 1;
112 114
113 rs.buf = testdata; 115 rs.buf = testdata;
114 rs.offset = 0; 116 rs.offset = 0;
@@ -124,35 +126,239 @@ main(int argc, char **argv)
124 if (ret != extend_tests[i].want_ret) { 126 if (ret != extend_tests[i].want_ret) {
125 fprintf(stderr, "FAIL: Test %zd - extend returned %zd, " 127 fprintf(stderr, "FAIL: Test %zd - extend returned %zd, "
126 "want %zd\n", i, ret, et->want_ret); 128 "want %zd\n", i, ret, et->want_ret);
127 return 1; 129 goto failed;
128 } 130 }
129 131
130 tls_buffer_cbs(buf, &cbs); 132 if (!tls_buffer_data(buf, &cbs)) {
133 fprintf(stderr, "FAIL: Test %zd - failed to get data\n",
134 i);
135 goto failed;
136 }
131 137
132 if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) { 138 if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) {
133 fprintf(stderr, "FAIL: Test %zd - extend buffer " 139 fprintf(stderr, "FAIL: Test %zd - extend buffer "
134 "mismatch", i); 140 "mismatch", i);
135 return 1; 141 goto failed;
136 } 142 }
137 } 143 }
138 144
139 if (!tls_buffer_finish(buf, &data, &data_len)) { 145 if (!tls_buffer_finish(buf, &data, &data_len)) {
140 fprintf(stderr, "FAIL: failed to finish\n"); 146 fprintf(stderr, "FAIL: failed to finish\n");
141 return 1; 147 goto failed;
142 } 148 }
143 149
144 tls_buffer_free(buf); 150 tls_buffer_free(buf);
151 buf = NULL;
145 152
146 if (data_len != sizeof(testdata)) { 153 if (data_len != sizeof(testdata)) {
147 fprintf(stderr, "FAIL: got data length %zu, want %zu\n", 154 fprintf(stderr, "FAIL: got data length %zu, want %zu\n",
148 data_len, sizeof(testdata)); 155 data_len, sizeof(testdata));
149 return 1; 156 goto failed;
150 } 157 }
151 if (memcmp(data, testdata, data_len) != 0) { 158 if (memcmp(data, testdata, data_len) != 0) {
152 fprintf(stderr, "FAIL: data mismatch\n"); 159 fprintf(stderr, "FAIL: data mismatch\n");
153 return 1; 160 goto failed;
154 } 161 }
162
163 failed = 0;
164
165 failed:
166 tls_buffer_free(buf);
155 free(data); 167 free(data);
156 168
157 return 0; 169 return failed;
170}
171
172struct read_write_test {
173 uint8_t pattern;
174 size_t read;
175 size_t write;
176 size_t append;
177 ssize_t want;
178};
179
180const struct read_write_test read_write_tests[] = {
181 {
182 .read = 2048,
183 .want = TLS_IO_WANT_POLLIN,
184 },
185 {
186 .pattern = 0xdb,
187 .write = 2048,
188 .want = 2048,
189 },
190 {
191 .pattern = 0xbd,
192 .append = 2048,
193 .want = 1,
194 },
195 {
196 .pattern = 0xdb,
197 .read = 2048,
198 .want = 2048,
199 },
200 {
201 .pattern = 0xfe,
202 .append = 1024,
203 .want = 1,
204 },
205 {
206 .pattern = 0xbd,
207 .read = 1000,
208 .want = 1000,
209 },
210 {
211 .pattern = 0xbd,
212 .read = 1048,
213 .want = 1048,
214 },
215 {
216 .pattern = 0xdb,
217 .write = 2048,
218 .want = 2048,
219 },
220 {
221 .pattern = 0xbd,
222 .append = 1024,
223 .want = 1,
224 },
225 {
226 .pattern = 0xee,
227 .append = 4096,
228 .want = 1,
229 },
230 {
231 .pattern = 0xfe,
232 .append = 1,
233 .want = 0,
234 },
235 {
236 .pattern = 0xfe,
237 .write = 1,
238 .want = TLS_IO_FAILURE,
239 },
240 {
241 .pattern = 0xfe,
242 .read = 1024,
243 .want = 1024,
244 },
245 {
246 .pattern = 0xdb,
247 .read = 2048,
248 .want = 2048,
249 },
250 {
251 .pattern = 0xbd,
252 .read = 1024,
253 .want = 1024,
254 },
255 {
256 .pattern = 0xee,
257 .read = 1024,
258 .want = 1024,
259 },
260 {
261 .pattern = 0xee,
262 .read = 4096,
263 .want = 3072,
264 },
265 {
266 .read = 2048,
267 .want = TLS_IO_WANT_POLLIN,
268 },
269};
270
271#define N_READ_WRITE_TESTS (sizeof(read_write_tests) / sizeof(read_write_tests[0]))
272
273static int
274tls_buffer_read_write_test(void)
275{
276 const struct read_write_test *rwt;
277 struct tls_buffer *buf = NULL;
278 uint8_t *rbuf = NULL, *wbuf = NULL;
279 ssize_t n;
280 size_t i;
281 int ret;
282 int failed = 1;
283
284 if ((buf = tls_buffer_new(0)) == NULL)
285 errx(1, "tls_buffer_new");
286
287 tls_buffer_set_capacity_limit(buf, 8192);
288
289 for (i = 0; i < N_READ_WRITE_TESTS; i++) {
290 rwt = &read_write_tests[i];
291
292 if (rwt->append > 0) {
293 free(wbuf);
294 if ((wbuf = malloc(rwt->append)) == NULL)
295 errx(1, "malloc");
296 memset(wbuf, rwt->pattern, rwt->append);
297 if ((ret = tls_buffer_append(buf, wbuf, rwt->append)) !=
298 rwt->want) {
299 fprintf(stderr, "FAIL: test %zu - "
300 "tls_buffer_append() = %d, want %zu\n",
301 i, ret, rwt->want);
302 goto failed;
303 }
304 }
305
306 if (rwt->write > 0) {
307 free(wbuf);
308 if ((wbuf = malloc(rwt->write)) == NULL)
309 errx(1, "malloc");
310 memset(wbuf, rwt->pattern, rwt->write);
311 if ((n = tls_buffer_write(buf, wbuf, rwt->write)) !=
312 rwt->want) {
313 fprintf(stderr, "FAIL: test %zu - "
314 "tls_buffer_write() = %zi, want %zu\n",
315 i, n, rwt->want);
316 goto failed;
317 }
318 }
319
320 if (rwt->read > 0) {
321 free(rbuf);
322 if ((rbuf = calloc(1, rwt->read)) == NULL)
323 errx(1, "malloc");
324 if ((n = tls_buffer_read(buf, rbuf, rwt->read)) !=
325 rwt->want) {
326 fprintf(stderr, "FAIL: test %zu - "
327 "tls_buffer_read() = %zi, want %zu\n",
328 i, n, rwt->want);
329 goto failed;
330 }
331 if (rwt->want > 0) {
332 free(wbuf);
333 if ((wbuf = malloc(rwt->want)) == NULL)
334 errx(1, "malloc");
335 memset(wbuf, rwt->pattern, rwt->want);
336 if (memcmp(rbuf, wbuf, rwt->want) != 0) {
337 fprintf(stderr, "FAIL: test %zu - "
338 "read byte mismatch\n", i);
339 goto failed;
340 }
341 }
342 }
343 }
344
345 failed = 0;
346
347 failed:
348 tls_buffer_free(buf);
349 free(rbuf);
350 free(wbuf);
351
352 return failed;
353}
354
355int
356main(int argc, char **argv)
357{
358 int failed = 0;
359
360 failed |= tls_buffer_extend_test();
361 failed |= tls_buffer_read_write_test();
362
363 return failed;
158} 364}