buffertest.c revision 1.4
1/* 2 * Copyright (c) 2019 Joel Sing <jsing@openbsd.org> 3 * 4 * 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 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17#include <err.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <string.h> 21 22#include "tls_internal.h" 23 24uint8_t testdata[] = { 25 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 26 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 27}; 28 29struct read_state { 30 uint8_t *buf; 31 size_t len; 32 size_t offset; 33}; 34 35static ssize_t 36read_cb(void *buf, size_t buflen, void *cb_arg) 37{ 38 struct read_state *rs = cb_arg; 39 ssize_t n; 40 41 if (rs->offset > rs->len) 42 return TLS_IO_EOF; 43 44 if ((size_t)(n = buflen) > (rs->len - rs->offset)) 45 n = rs->len - rs->offset; 46 47 if (n == 0) 48 return TLS_IO_WANT_POLLIN; 49 50 memcpy(buf, &rs->buf[rs->offset], n); 51 rs->offset += n; 52 53 return n; 54} 55 56struct extend_test { 57 size_t extend_len; 58 size_t read_len; 59 ssize_t want_ret; 60}; 61 62struct extend_test extend_tests[] = { 63 { 64 .extend_len = 4, 65 .read_len = 0, 66 .want_ret = TLS_IO_WANT_POLLIN, 67 }, 68 { 69 .extend_len = 4, 70 .read_len = 8, 71 .want_ret = 4, 72 }, 73 { 74 .extend_len = 12, 75 .read_len = 8, 76 .want_ret = TLS_IO_WANT_POLLIN, 77 }, 78 { 79 .extend_len = 12, 80 .read_len = 10, 81 .want_ret = TLS_IO_WANT_POLLIN, 82 }, 83 { 84 .extend_len = 12, 85 .read_len = 12, 86 .want_ret = 12, 87 }, 88 { 89 .extend_len = 16, 90 .read_len = 16, 91 .want_ret = 16, 92 }, 93 { 94 .extend_len = 20, 95 .read_len = 1, 96 .want_ret = TLS_IO_EOF, 97 }, 98}; 99 100#define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0])) 101 102int 103main(int argc, char **argv) 104{ 105 struct tls_buffer *buf; 106 struct extend_test *et; 107 struct read_state rs; 108 uint8_t *data; 109 size_t i, data_len; 110 ssize_t ret; 111 CBS cbs; 112 113 rs.buf = testdata; 114 rs.offset = 0; 115 116 if ((buf = tls_buffer_new(0)) == NULL) 117 errx(1, "tls_buffer_new"); 118 119 for (i = 0; i < N_EXTEND_TESTS; i++) { 120 et = &extend_tests[i]; 121 rs.len = et->read_len; 122 123 ret = tls_buffer_extend(buf, et->extend_len, read_cb, &rs); 124 if (ret != extend_tests[i].want_ret) { 125 fprintf(stderr, "FAIL: Test %zi - extend returned %zi, " 126 "want %zi\n", i, ret, et->want_ret); 127 return 1; 128 } 129 130 tls_buffer_cbs(buf, &cbs); 131 132 if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) { 133 fprintf(stderr, "FAIL: Test %zi - extend buffer " 134 "mismatch", i); 135 return 1; 136 } 137 } 138 139 if (!tls_buffer_finish(buf, &data, &data_len)) { 140 fprintf(stderr, "FAIL: failed to finish\n"); 141 return 1; 142 } 143 144 tls_buffer_free(buf); 145 146 if (data_len != sizeof(testdata)) { 147 fprintf(stderr, "FAIL: got data length %zu, want %zu\n", 148 data_len, sizeof(testdata)); 149 return 1; 150 } 151 if (memcmp(data, testdata, data_len) != 0) { 152 fprintf(stderr, "FAIL: data mismatch\n"); 153 return 1; 154 } 155 free(data); 156 157 return 0; 158} 159