diff options
author | jsing <> | 2019-01-17 06:46:10 +0000 |
---|---|---|
committer | jsing <> | 2019-01-17 06:46:10 +0000 |
commit | 154e80a0b5a0c3c4a9d3390a220e96f0f8c36aab (patch) | |
tree | f511d92b8435288aa4c04f27e021d1ccac6c1a6d /src/regress | |
parent | 82b2a8d8237a63124493a39742b98ea95d36d13a (diff) | |
download | openbsd-154e80a0b5a0c3c4a9d3390a220e96f0f8c36aab.tar.gz openbsd-154e80a0b5a0c3c4a9d3390a220e96f0f8c36aab.tar.bz2 openbsd-154e80a0b5a0c3c4a9d3390a220e96f0f8c36aab.zip |
Add regress for extensible buffer code.
Diffstat (limited to 'src/regress')
-rw-r--r-- | src/regress/lib/libssl/Makefile | 3 | ||||
-rw-r--r-- | src/regress/lib/libssl/buffer/Makefile | 10 | ||||
-rw-r--r-- | src/regress/lib/libssl/buffer/buffertest.c | 157 |
3 files changed, 169 insertions, 1 deletions
diff --git a/src/regress/lib/libssl/Makefile b/src/regress/lib/libssl/Makefile index b1caac9284..2f092cc14e 100644 --- a/src/regress/lib/libssl/Makefile +++ b/src/regress/lib/libssl/Makefile | |||
@@ -1,6 +1,7 @@ | |||
1 | # $OpenBSD: Makefile,v 1.32 2018/11/10 08:35:43 beck Exp $ | 1 | # $OpenBSD: Makefile,v 1.33 2019/01/17 06:46:10 jsing Exp $ |
2 | 2 | ||
3 | SUBDIR += asn1 | 3 | SUBDIR += asn1 |
4 | SUBDIR += buffer | ||
4 | SUBDIR += bytestring | 5 | SUBDIR += bytestring |
5 | SUBDIR += ciphers | 6 | SUBDIR += ciphers |
6 | SUBDIR += client | 7 | SUBDIR += client |
diff --git a/src/regress/lib/libssl/buffer/Makefile b/src/regress/lib/libssl/buffer/Makefile new file mode 100644 index 0000000000..64ed46fa90 --- /dev/null +++ b/src/regress/lib/libssl/buffer/Makefile | |||
@@ -0,0 +1,10 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2019/01/17 06:46:10 jsing Exp $ | ||
2 | |||
3 | PROG= buffertest | ||
4 | LDADD= ${SSL_INT} -lcrypto | ||
5 | DPADD= ${LIBSSL} ${LIBCRYPTO} | ||
6 | WARNINGS= Yes | ||
7 | CFLAGS+= -DLIBRESSL_INTERNAL -Wall -Wundef -Werror | ||
8 | CFLAGS+= -I${.CURDIR}/../../../../lib/libssl | ||
9 | |||
10 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libssl/buffer/buffertest.c b/src/regress/lib/libssl/buffer/buffertest.c new file mode 100644 index 0000000000..3157e5cab8 --- /dev/null +++ b/src/regress/lib/libssl/buffer/buffertest.c | |||
@@ -0,0 +1,157 @@ | |||
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 | uint8_t want_buf[128]; | ||
58 | size_t want_len; | ||
59 | ssize_t want_ret; | ||
60 | }; | ||
61 | |||
62 | struct extend_test extend_tests[] = { | ||
63 | { | ||
64 | .extend_len = 4, | ||
65 | .read_len = 0, | ||
66 | .want_ret = TLS13_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 = TLS13_IO_WANT_POLLIN, | ||
77 | }, | ||
78 | { | ||
79 | .extend_len = 12, | ||
80 | .read_len = 10, | ||
81 | .want_ret = TLS13_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 = TLS13_IO_EOF, | ||
97 | }, | ||
98 | }; | ||
99 | |||
100 | #define N_EXTEND_TESTS (sizeof(extend_tests) / sizeof(extend_tests[0])) | ||
101 | |||
102 | int | ||
103 | main(int argc, char **argv) | ||
104 | { | ||
105 | struct tls13_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 | |||
115 | if ((buf = tls13_buffer_new(0)) == NULL) | ||
116 | errx(1, "tls13_buffer_new"); | ||
117 | |||
118 | for (i = 0; i < N_EXTEND_TESTS; i++) { | ||
119 | et = &extend_tests[i]; | ||
120 | rs.len = et->read_len; | ||
121 | |||
122 | ret = tls13_buffer_extend(buf, et->extend_len, read_cb, &rs); | ||
123 | if (ret != extend_tests[i].want_ret) { | ||
124 | fprintf(stderr, "FAIL: Test %zi - extend returned %zi, " | ||
125 | "want %zi\n", i, ret, et->want_ret); | ||
126 | return 1; | ||
127 | } | ||
128 | |||
129 | tls13_buffer_cbs(buf, &cbs); | ||
130 | |||
131 | if (!CBS_mem_equal(&cbs, testdata, CBS_len(&cbs))) { | ||
132 | fprintf(stderr, "FAIL: Test %zi - extend buffer " | ||
133 | "mismatch", i); | ||
134 | return 1; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | if (!tls13_buffer_finish(buf, &data, &data_len)) { | ||
139 | fprintf(stderr, "FAIL: failed to finish\n"); | ||
140 | return 1; | ||
141 | } | ||
142 | |||
143 | tls13_buffer_free(buf); | ||
144 | |||
145 | if (data_len != sizeof(testdata)) { | ||
146 | fprintf(stderr, "FAIL: got data length %zu, want %zu\n", | ||
147 | data_len, sizeof(testdata)); | ||
148 | return 1; | ||
149 | } | ||
150 | if (memcmp(data, testdata, data_len) != 0) { | ||
151 | fprintf(stderr, "FAIL: data mismatch\n"); | ||
152 | return 1; | ||
153 | } | ||
154 | free(data); | ||
155 | |||
156 | return 0; | ||
157 | } | ||