From 25c4e8bd056e974b28f4a0ffd39d76c190a56013 Mon Sep 17 00:00:00 2001 From: jsing Date: Fri, 22 Jul 2022 19:34:55 +0000 Subject: [PATCH] Extend TLS buffer regress to cover read/write usage. --- regress/lib/libssl/buffer/buffertest.c | 232 +++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 13 deletions(-) diff --git a/regress/lib/libssl/buffer/buffertest.c b/regress/lib/libssl/buffer/buffertest.c index 45742dec4ea..3dfad7c44fd 100644 --- a/regress/lib/libssl/buffer/buffertest.c +++ b/regress/lib/libssl/buffer/buffertest.c @@ -1,5 +1,6 @@ +/* $OpenBSD: buffertest.c,v 1.6 2022/07/22 19:34:55 jsing Exp $ */ /* - * Copyright (c) 2019 Joel Sing + * Copyright (c) 2019, 2022 Joel Sing * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -59,7 +60,7 @@ struct extend_test { ssize_t want_ret; }; -struct extend_test extend_tests[] = { +const struct extend_test extend_tests[] = { { .extend_len = 4, .read_len = 0, @@ -99,16 +100,17 @@ struct extend_test extend_tests[] = { #define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0])) -int -main(int argc, char **argv) +static int +tls_buffer_extend_test(void) { + const struct extend_test *et; struct tls_buffer *buf; - struct extend_test *et; struct read_state rs; - uint8_t *data; + uint8_t *data = NULL; size_t i, data_len; ssize_t ret; CBS cbs; + int failed = 1; rs.buf = testdata; rs.offset = 0; @@ -124,35 +126,239 @@ main(int argc, char **argv) if (ret != extend_tests[i].want_ret) { fprintf(stderr, "FAIL: Test %zd - extend returned %zd, " "want %zd\n", i, ret, et->want_ret); - return 1; + goto failed; } - tls_buffer_cbs(buf, &cbs); + if (!tls_buffer_data(buf, &cbs)) { + fprintf(stderr, "FAIL: Test %zd - failed to get data\n", + i); + goto failed; + } if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) { fprintf(stderr, "FAIL: Test %zd - extend buffer " "mismatch", i); - return 1; + goto failed; } } if (!tls_buffer_finish(buf, &data, &data_len)) { fprintf(stderr, "FAIL: failed to finish\n"); - return 1; + goto failed; } tls_buffer_free(buf); + buf = NULL; if (data_len != sizeof(testdata)) { fprintf(stderr, "FAIL: got data length %zu, want %zu\n", data_len, sizeof(testdata)); - return 1; + goto failed; } if (memcmp(data, testdata, data_len) != 0) { fprintf(stderr, "FAIL: data mismatch\n"); - return 1; + goto failed; } + + failed = 0; + + failed: + tls_buffer_free(buf); free(data); - return 0; + return failed; +} + +struct read_write_test { + uint8_t pattern; + size_t read; + size_t write; + size_t append; + ssize_t want; +}; + +const struct read_write_test read_write_tests[] = { + { + .read = 2048, + .want = TLS_IO_WANT_POLLIN, + }, + { + .pattern = 0xdb, + .write = 2048, + .want = 2048, + }, + { + .pattern = 0xbd, + .append = 2048, + .want = 1, + }, + { + .pattern = 0xdb, + .read = 2048, + .want = 2048, + }, + { + .pattern = 0xfe, + .append = 1024, + .want = 1, + }, + { + .pattern = 0xbd, + .read = 1000, + .want = 1000, + }, + { + .pattern = 0xbd, + .read = 1048, + .want = 1048, + }, + { + .pattern = 0xdb, + .write = 2048, + .want = 2048, + }, + { + .pattern = 0xbd, + .append = 1024, + .want = 1, + }, + { + .pattern = 0xee, + .append = 4096, + .want = 1, + }, + { + .pattern = 0xfe, + .append = 1, + .want = 0, + }, + { + .pattern = 0xfe, + .write = 1, + .want = TLS_IO_FAILURE, + }, + { + .pattern = 0xfe, + .read = 1024, + .want = 1024, + }, + { + .pattern = 0xdb, + .read = 2048, + .want = 2048, + }, + { + .pattern = 0xbd, + .read = 1024, + .want = 1024, + }, + { + .pattern = 0xee, + .read = 1024, + .want = 1024, + }, + { + .pattern = 0xee, + .read = 4096, + .want = 3072, + }, + { + .read = 2048, + .want = TLS_IO_WANT_POLLIN, + }, +}; + +#define N_READ_WRITE_TESTS (sizeof(read_write_tests) / sizeof(read_write_tests[0])) + +static int +tls_buffer_read_write_test(void) +{ + const struct read_write_test *rwt; + struct tls_buffer *buf = NULL; + uint8_t *rbuf = NULL, *wbuf = NULL; + ssize_t n; + size_t i; + int ret; + int failed = 1; + + if ((buf = tls_buffer_new(0)) == NULL) + errx(1, "tls_buffer_new"); + + tls_buffer_set_capacity_limit(buf, 8192); + + for (i = 0; i < N_READ_WRITE_TESTS; i++) { + rwt = &read_write_tests[i]; + + if (rwt->append > 0) { + free(wbuf); + if ((wbuf = malloc(rwt->append)) == NULL) + errx(1, "malloc"); + memset(wbuf, rwt->pattern, rwt->append); + if ((ret = tls_buffer_append(buf, wbuf, rwt->append)) != + rwt->want) { + fprintf(stderr, "FAIL: test %zu - " + "tls_buffer_append() = %d, want %zu\n", + i, ret, rwt->want); + goto failed; + } + } + + if (rwt->write > 0) { + free(wbuf); + if ((wbuf = malloc(rwt->write)) == NULL) + errx(1, "malloc"); + memset(wbuf, rwt->pattern, rwt->write); + if ((n = tls_buffer_write(buf, wbuf, rwt->write)) != + rwt->want) { + fprintf(stderr, "FAIL: test %zu - " + "tls_buffer_write() = %zi, want %zu\n", + i, n, rwt->want); + goto failed; + } + } + + if (rwt->read > 0) { + free(rbuf); + if ((rbuf = calloc(1, rwt->read)) == NULL) + errx(1, "malloc"); + if ((n = tls_buffer_read(buf, rbuf, rwt->read)) != + rwt->want) { + fprintf(stderr, "FAIL: test %zu - " + "tls_buffer_read() = %zi, want %zu\n", + i, n, rwt->want); + goto failed; + } + if (rwt->want > 0) { + free(wbuf); + if ((wbuf = malloc(rwt->want)) == NULL) + errx(1, "malloc"); + memset(wbuf, rwt->pattern, rwt->want); + if (memcmp(rbuf, wbuf, rwt->want) != 0) { + fprintf(stderr, "FAIL: test %zu - " + "read byte mismatch\n", i); + goto failed; + } + } + } + } + + failed = 0; + + failed: + tls_buffer_free(buf); + free(rbuf); + free(wbuf); + + return failed; +} + +int +main(int argc, char **argv) +{ + int failed = 0; + + failed |= tls_buffer_extend_test(); + failed |= tls_buffer_read_write_test(); + + return failed; } -- 2.20.1