summaryrefslogtreecommitdiff
path: root/src/regress/lib/libssl/buffer/buffertest.c
blob: 3dfad7c44fdcde9442a1c1d1ad03c42cae595a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* $OpenBSD: buffertest.c,v 1.6 2022/07/22 19:34:55 jsing Exp $ */
/*
 * Copyright (c) 2019, 2022 Joel Sing <jsing@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tls_internal.h"

uint8_t testdata[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};

struct read_state {
	uint8_t *buf;
	size_t len;
	size_t offset;
};

static ssize_t
read_cb(void *buf, size_t buflen, void *cb_arg)
{
	struct read_state *rs = cb_arg;
	ssize_t n;

	if (rs->offset > rs->len)
		return TLS_IO_EOF;

	if ((size_t)(n = buflen) > (rs->len - rs->offset))
		n = rs->len - rs->offset;

	if (n == 0)
		return TLS_IO_WANT_POLLIN;

	memcpy(buf, &rs->buf[rs->offset], n);
	rs->offset += n;

	return n;
}

struct extend_test {
	size_t extend_len;
	size_t read_len;
	ssize_t want_ret;
};

const struct extend_test extend_tests[] = {
	{
		.extend_len = 4,
		.read_len = 0,
		.want_ret = TLS_IO_WANT_POLLIN,
	},
	{
		.extend_len = 4,
		.read_len = 8,
		.want_ret = 4,
	},
	{
		.extend_len = 12,
		.read_len = 8,
		.want_ret = TLS_IO_WANT_POLLIN,
	},
	{
		.extend_len = 12,
		.read_len = 10,
		.want_ret = TLS_IO_WANT_POLLIN,
	},
	{
		.extend_len = 12,
		.read_len = 12,
		.want_ret = 12,
	},
	{
		.extend_len = 16,
		.read_len = 16,
		.want_ret = 16,
	},
	{
		.extend_len = 20,
		.read_len = 1,
		.want_ret = TLS_IO_EOF,
	},
};

#define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0]))

static int
tls_buffer_extend_test(void)
{
	const struct extend_test *et;
	struct tls_buffer *buf;
	struct read_state rs;
	uint8_t *data = NULL;
	size_t i, data_len;
	ssize_t ret;
	CBS cbs;
	int failed = 1;

	rs.buf = testdata;
	rs.offset = 0;

	if ((buf = tls_buffer_new(0)) == NULL)
		errx(1, "tls_buffer_new");

	for (i = 0; i < N_EXTEND_TESTS; i++) {
		et = &extend_tests[i];
		rs.len = et->read_len;

		ret = tls_buffer_extend(buf, et->extend_len, read_cb, &rs);
		if (ret != extend_tests[i].want_ret) {
			fprintf(stderr, "FAIL: Test %zd - extend returned %zd, "
			    "want %zd\n", i, ret, et->want_ret);
			goto failed;
		}

		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);
			goto failed;
		}
	}

	if (!tls_buffer_finish(buf, &data, &data_len)) {
		fprintf(stderr, "FAIL: failed to finish\n");
		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));
		goto failed;
	}
	if (memcmp(data, testdata, data_len) != 0) {
		fprintf(stderr, "FAIL: data mismatch\n");
		goto failed;
	}

	failed = 0;

 failed:
	tls_buffer_free(buf);
	free(data);

	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;
}