xref: /openbsd-src/regress/lib/libssl/buffer/buffertest.c (revision 1fdfe1a09fe0c62f78e359b3946ea5e69e0affd1)
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 <string.h>
19 
20 #include "tls13_internal.h"
21 
22 uint8_t testdata[] = {
23 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
24 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
25 };
26 
27 struct read_state {
28 	uint8_t *buf;
29 	size_t len;
30 	size_t offset;
31 };
32 
33 static ssize_t
34 read_cb(void *buf, size_t buflen, void *cb_arg)
35 {
36 	struct read_state *rs = cb_arg;
37 	ssize_t n;
38 
39 	if (rs->offset > rs->len)
40 		return TLS13_IO_EOF;
41 
42 	if ((size_t)(n = buflen) > (rs->len - rs->offset))
43 		n = rs->len - rs->offset;
44 
45 	if (n == 0)
46 		return TLS13_IO_WANT_POLLIN;
47 
48 	memcpy(buf, &rs->buf[rs->offset], n);
49 	rs->offset += n;
50 
51 	return n;
52 }
53 
54 struct extend_test {
55 	size_t extend_len;
56 	size_t read_len;
57 	ssize_t want_ret;
58 };
59 
60 struct extend_test extend_tests[] = {
61 	{
62 		.extend_len = 4,
63 		.read_len = 0,
64 		.want_ret = TLS13_IO_WANT_POLLIN,
65 	},
66 	{
67 		.extend_len = 4,
68 		.read_len = 8,
69 		.want_ret = 4,
70 	},
71 	{
72 		.extend_len = 12,
73 		.read_len = 8,
74 		.want_ret = TLS13_IO_WANT_POLLIN,
75 	},
76 	{
77 		.extend_len = 12,
78 		.read_len = 10,
79 		.want_ret = TLS13_IO_WANT_POLLIN,
80 	},
81 	{
82 		.extend_len = 12,
83 		.read_len = 12,
84 		.want_ret = 12,
85 	},
86 	{
87 		.extend_len = 16,
88 		.read_len = 16,
89 		.want_ret = 16,
90 	},
91 	{
92 		.extend_len = 20,
93 		.read_len = 1,
94 		.want_ret = TLS13_IO_EOF,
95 	},
96 };
97 
98 #define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0]))
99 
100 int
101 main(int argc, char **argv)
102 {
103 	struct tls13_buffer *buf;
104 	struct extend_test *et;
105 	struct read_state rs;
106 	uint8_t *data;
107 	size_t i, data_len;
108 	ssize_t ret;
109 	CBS cbs;
110 
111 	rs.buf = testdata;
112 
113 	if ((buf = tls13_buffer_new(0)) == NULL)
114 		errx(1, "tls13_buffer_new");
115 
116 	for (i = 0; i < N_EXTEND_TESTS; i++) {
117 		et = &extend_tests[i];
118 		rs.len = et->read_len;
119 
120 		ret = tls13_buffer_extend(buf, et->extend_len, read_cb, &rs);
121 		if (ret != extend_tests[i].want_ret) {
122 			fprintf(stderr, "FAIL: Test %zi - extend returned %zi, "
123 			    "want %zi\n", i, ret, et->want_ret);
124 			return 1;
125 		}
126 
127 		tls13_buffer_cbs(buf, &cbs);
128 
129 		if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) {
130 			fprintf(stderr, "FAIL: Test %zi - extend buffer "
131 			    "mismatch", i);
132 			return 1;
133 		}
134 	}
135 
136 	if (!tls13_buffer_finish(buf, &data, &data_len)) {
137 		fprintf(stderr, "FAIL: failed to finish\n");
138 		return 1;
139 	}
140 
141 	tls13_buffer_free(buf);
142 
143 	if (data_len != sizeof(testdata)) {
144 		fprintf(stderr, "FAIL: got data length %zu, want %zu\n",
145 		    data_len, sizeof(testdata));
146 		return 1;
147 	}
148 	if (memcmp(data, testdata, data_len) != 0) {
149 		fprintf(stderr, "FAIL: data mismatch\n");
150 		return 1;
151 	}
152 	free(data);
153 
154 	return 0;
155 }
156