xref: /openbsd-src/regress/lib/libssl/buffer/buffertest.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
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 	rs.offset = 0;
113 
114 	if ((buf = tls13_buffer_new(0)) == NULL)
115 		errx(1, "tls13_buffer_new");
116 
117 	for (i = 0; i < N_EXTEND_TESTS; i++) {
118 		et = &extend_tests[i];
119 		rs.len = et->read_len;
120 
121 		ret = tls13_buffer_extend(buf, et->extend_len, read_cb, &rs);
122 		if (ret != extend_tests[i].want_ret) {
123 			fprintf(stderr, "FAIL: Test %zi - extend returned %zi, "
124 			    "want %zi\n", i, ret, et->want_ret);
125 			return 1;
126 		}
127 
128 		tls13_buffer_cbs(buf, &cbs);
129 
130 		if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) {
131 			fprintf(stderr, "FAIL: Test %zi - extend buffer "
132 			    "mismatch", i);
133 			return 1;
134 		}
135 	}
136 
137 	if (!tls13_buffer_finish(buf, &data, &data_len)) {
138 		fprintf(stderr, "FAIL: failed to finish\n");
139 		return 1;
140 	}
141 
142 	tls13_buffer_free(buf);
143 
144 	if (data_len != sizeof(testdata)) {
145 		fprintf(stderr, "FAIL: got data length %zu, want %zu\n",
146 		    data_len, sizeof(testdata));
147 		return 1;
148 	}
149 	if (memcmp(data, testdata, data_len) != 0) {
150 		fprintf(stderr, "FAIL: data mismatch\n");
151 		return 1;
152 	}
153 	free(data);
154 
155 	return 0;
156 }
157