summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/bio/Makefile7
-rw-r--r--src/regress/lib/libcrypto/bio/bio_chain.c (renamed from src/regress/lib/libcrypto/bio/biotest.c)464
-rw-r--r--src/regress/lib/libcrypto/bio/bio_host.c154
-rw-r--r--src/regress/lib/libcrypto/bio/bio_mem.c345
4 files changed, 512 insertions, 458 deletions
diff --git a/src/regress/lib/libcrypto/bio/Makefile b/src/regress/lib/libcrypto/bio/Makefile
index 0833451bd5..ee10a60812 100644
--- a/src/regress/lib/libcrypto/bio/Makefile
+++ b/src/regress/lib/libcrypto/bio/Makefile
@@ -1,6 +1,9 @@
1# $OpenBSD: Makefile,v 1.3 2022/12/03 09:55:53 tb Exp $ 1# $OpenBSD: Makefile,v 1.4 2022/12/08 17:49:02 tb Exp $
2
3PROGS += bio_chain
4PROGS += bio_host
5PROGS += bio_mem
2 6
3PROG = biotest
4LDADD = -lcrypto 7LDADD = -lcrypto
5DPADD = ${LIBCRYPTO} 8DPADD = ${LIBCRYPTO}
6WARNINGS = Yes 9WARNINGS = Yes
diff --git a/src/regress/lib/libcrypto/bio/biotest.c b/src/regress/lib/libcrypto/bio/bio_chain.c
index a624315c61..143c4e147b 100644
--- a/src/regress/lib/libcrypto/bio/biotest.c
+++ b/src/regress/lib/libcrypto/bio/bio_chain.c
@@ -1,6 +1,5 @@
1/* $OpenBSD: biotest.c,v 1.12 2022/12/08 12:27:03 tb Exp $ */ 1/* $OpenBSD: bio_chain.c,v 1.1 2022/12/08 17:49:02 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2014, 2022 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
5 * 4 *
6 * Permission to use, copy, modify, and distribute this software for any 5 * Permission to use, copy, modify, and distribute this software for any
@@ -16,458 +15,14 @@
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 16 */
18 17
19#include <sys/types.h>
20
21#include <err.h> 18#include <err.h>
22#include <stdint.h> 19#include <stdio.h>
23#include <stdlib.h>
24#include <string.h> 20#include <string.h>
25 21
26#include <netinet/in.h>
27
28#include <openssl/bio.h> 22#include <openssl/bio.h>
29#include <openssl/buffer.h>
30#include <openssl/err.h>
31 23
32#include "bio_local.h" 24#include "bio_local.h"
33 25
34struct bio_get_host_ip_test {
35 char *input;
36 uint32_t ip;
37 int ret;
38};
39
40struct bio_get_host_ip_test bio_get_host_ip_tests[] = {
41 {"", 0, 0},
42 {".", 0, 0},
43 {"1", 0, 0},
44 {"1.2", 0, 0},
45 {"1.2.3", 0, 0},
46 {"1.2.3.", 0, 0},
47 {"1.2.3.4", 0x01020304, 1},
48 {"1.2.3.256", 0, 0},
49 {"1:2:3::4", 0, 0},
50 {"0.0.0.0", INADDR_ANY, 1},
51 {"127.0.0.1", INADDR_LOOPBACK, 1},
52 {"localhost", INADDR_LOOPBACK, 1},
53 {"255.255.255.255", INADDR_BROADCAST, 1},
54 {"0xff.0xff.0xff.0xff", 0, 0},
55};
56
57#define N_BIO_GET_IP_TESTS \
58 (sizeof(bio_get_host_ip_tests) / sizeof(*bio_get_host_ip_tests))
59
60struct bio_get_port_test {
61 char *input;
62 unsigned short port;
63 int ret;
64};
65
66struct bio_get_port_test bio_get_port_tests[] = {
67 {NULL, 0, 0},
68 {"", 0, 0},
69 {"-1", 0, 0},
70 {"0", 0, 1},
71 {"1", 1, 1},
72 {"12345", 12345, 1},
73 {"65535", 65535, 1},
74 {"65536", 0, 0},
75 {"999999999999", 0, 0},
76 {"xyzzy", 0, 0},
77 {"https", 443, 1},
78 {"imaps", 993, 1},
79 {"telnet", 23, 1},
80};
81
82#define N_BIO_GET_PORT_TESTS \
83 (sizeof(bio_get_port_tests) / sizeof(*bio_get_port_tests))
84
85static int
86do_bio_get_host_ip_tests(void)
87{
88 struct bio_get_host_ip_test *bgit;
89 union {
90 unsigned char c[4];
91 uint32_t i;
92 } ip;
93 int failed = 0;
94 size_t i;
95 int ret;
96
97 for (i = 0; i < N_BIO_GET_IP_TESTS; i++) {
98 bgit = &bio_get_host_ip_tests[i];
99 memset(&ip, 0, sizeof(ip));
100
101 ret = BIO_get_host_ip(bgit->input, ip.c);
102 if (ret != bgit->ret) {
103 fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
104 i, bgit->input, ret ? "success" : "failure",
105 bgit->ret ? "success" : "failure");
106 failed = 1;
107 continue;
108 }
109 if (ret && ntohl(ip.i) != bgit->ip) {
110 fprintf(stderr, "FAIL: test %zd (\"%s\") returned ip "
111 "%x != %x\n", i, bgit->input,
112 ntohl(ip.i), bgit->ip);
113 failed = 1;
114 }
115 }
116
117 return failed;
118}
119
120static int
121do_bio_get_port_tests(void)
122{
123 struct bio_get_port_test *bgpt;
124 unsigned short port;
125 int failed = 0;
126 size_t i;
127 int ret;
128
129 for (i = 0; i < N_BIO_GET_PORT_TESTS; i++) {
130 bgpt = &bio_get_port_tests[i];
131 port = 0;
132
133 ret = BIO_get_port(bgpt->input, &port);
134 if (ret != bgpt->ret) {
135 fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
136 i, bgpt->input, ret ? "success" : "failure",
137 bgpt->ret ? "success" : "failure");
138 failed = 1;
139 continue;
140 }
141 if (ret && port != bgpt->port) {
142 fprintf(stderr, "FAIL: test %zd (\"%s\") returned port "
143 "%u != %u\n", i, bgpt->input, port, bgpt->port);
144 failed = 1;
145 }
146 }
147
148 return failed;
149}
150
151static int
152bio_mem_test(void)
153{
154 uint8_t *data = NULL;
155 size_t data_len;
156 uint8_t *rodata;
157 long rodata_len;
158 BUF_MEM *pbuf;
159 BUF_MEM *buf = NULL;
160 BIO *bio = NULL;
161 int ret;
162 int failed = 1;
163
164 data_len = 4096;
165 if ((data = malloc(data_len)) == NULL)
166 err(1, "malloc");
167
168 memset(data, 0xdb, data_len);
169 data[0] = 0x01;
170 data[data_len - 1] = 0xff;
171
172 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
173 fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
174 goto failure;
175 }
176 if ((ret = BIO_write(bio, data, data_len)) != (int)data_len) {
177 fprintf(stderr, "FAIL: BIO_write() = %d, want %zu\n", ret,
178 data_len);
179 goto failure;
180 }
181 if ((rodata_len = BIO_get_mem_data(bio, &rodata)) != (long)data_len) {
182 fprintf(stderr, "FAIL: BIO_get_mem_data() = %ld, want %zu\n",
183 rodata_len, data_len);
184 goto failure;
185 }
186 if (rodata[0] != 0x01) {
187 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", rodata[0], 0x01);
188 goto failure;
189 }
190 if (rodata[rodata_len - 1] != 0xff) {
191 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n",
192 rodata[rodata_len - 1], 0xff);
193 goto failure;
194 }
195
196 if (!BIO_get_mem_ptr(bio, &pbuf)) {
197 fprintf(stderr, "FAIL: BIO_get_mem_ptr() failed\n");
198 goto failure;
199 }
200 if (pbuf->length != data_len) {
201 fprintf(stderr, "FAIL: Got buffer with length %zu, want %zu\n",
202 pbuf->length, data_len);
203 goto failure;
204 }
205 if (memcmp(pbuf->data, data, data_len) != 0) {
206 fprintf(stderr, "FAIL: Got buffer with differing data\n");
207 goto failure;
208 }
209 pbuf = NULL;
210
211 if ((buf = BUF_MEM_new()) == NULL) {
212 fprintf(stderr, "FAIL: BUF_MEM_new() returned NULL\n");
213 goto failure;
214 }
215 if (!BIO_set_mem_buf(bio, buf, BIO_NOCLOSE)) {
216 fprintf(stderr, "FAIL: BUF_set_mem_buf() failed\n");
217 goto failure;
218 }
219 if ((ret = BIO_puts(bio, "Hello\n")) != 6) {
220 fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
221 goto failure;
222 }
223 if ((ret = BIO_puts(bio, "World\n")) != 6) {
224 fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
225 goto failure;
226 }
227 if (buf->length != 12) {
228 fprintf(stderr, "FAIL: buffer has length %zu, want %d\n",
229 buf->length, 12);
230 goto failure;
231 }
232 buf->length = 11;
233 if ((ret = BIO_gets(bio, data, data_len)) != 6) {
234 fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 6);
235 goto failure;
236 }
237 if (strcmp(data, "Hello\n") != 0) {
238 fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
239 data, "Hello\\n");
240 goto failure;
241 }
242 if ((ret = BIO_gets(bio, data, data_len)) != 5) {
243 fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 5);
244 goto failure;
245 }
246 if (strcmp(data, "World") != 0) {
247 fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
248 data, "World");
249 goto failure;
250 }
251
252 if (!BIO_eof(bio)) {
253 fprintf(stderr, "FAIL: BIO is not EOF\n");
254 goto failure;
255 }
256 if ((ret = BIO_read(bio, data, data_len)) != -1) {
257 fprintf(stderr, "FAIL: BIO_read() = %d, want -1\n", ret);
258 goto failure;
259 }
260 if (!BIO_set_mem_eof_return(bio, -2)) {
261 fprintf(stderr, "FAIL: BIO_set_mem_eof_return() failed\n");
262 goto failure;
263 }
264 if ((ret = BIO_read(bio, data, data_len)) != -2) {
265 fprintf(stderr, "FAIL: BIO_read() = %d, want -2\n", ret);
266 goto failure;
267 }
268
269 failed = 0;
270
271 failure:
272 free(data);
273 BUF_MEM_free(buf);
274 BIO_free(bio);
275
276 return failed;
277}
278
279static int
280bio_mem_small_io_test(void)
281{
282 uint8_t buf[2];
283 int i, j, ret;
284 BIO *bio;
285 int failed = 1;
286
287 memset(buf, 0xdb, sizeof(buf));
288
289 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
290 fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
291 goto failure;
292 }
293
294 for (i = 0; i < 100; i++) {
295 if (!BIO_reset(bio)) {
296 fprintf(stderr, "FAIL: BIO_reset() failed\n");
297 goto failure;
298 }
299 for (j = 0; j < 25000; j++) {
300 ret = BIO_write(bio, buf, sizeof(buf));
301 if (ret != sizeof(buf)) {
302 fprintf(stderr, "FAIL: BIO_write() = %d, "
303 "want %zu\n", ret, sizeof(buf));
304 goto failure;
305 }
306 }
307 for (j = 0; j < 25000; j++) {
308 ret = BIO_read(bio, buf, sizeof(buf));
309 if (ret != sizeof(buf)) {
310 fprintf(stderr, "FAIL: BIO_read() = %d, "
311 "want %zu\n", ret, sizeof(buf));
312 goto failure;
313 }
314 ret = BIO_write(bio, buf, sizeof(buf));
315 if (ret != sizeof(buf)) {
316 fprintf(stderr, "FAIL: BIO_write() = %d, "
317 "want %zu\n", ret, sizeof(buf));
318 goto failure;
319 }
320 }
321 for (j = 0; j < 25000; j++) {
322 ret = BIO_read(bio, buf, sizeof(buf));
323 if (ret != sizeof(buf)) {
324 fprintf(stderr, "FAIL: BIO_read() = %d, "
325 "want %zu\n", ret, sizeof(buf));
326 goto failure;
327 }
328 }
329 if (!BIO_eof(bio)) {
330 fprintf(stderr, "FAIL: BIO not EOF\n");
331 goto failure;
332 }
333 }
334
335 if (buf[0] != 0xdb || buf[1] != 0xdb) {
336 fprintf(stderr, "FAIL: buf = {0x%x, 0x%x}, want {0xdb, 0xdb}\n",
337 buf[0], buf[1]);
338 goto failure;
339 }
340
341 failed = 0;
342
343 failure:
344 BIO_free(bio);
345
346 return failed;
347}
348
349static int
350bio_mem_readonly_test(void)
351{
352 uint8_t *data = NULL;
353 size_t data_len;
354 uint8_t buf[2048];
355 BIO *bio = NULL;
356 int ret;
357 int failed = 1;
358
359 data_len = 4096;
360 if ((data = malloc(data_len)) == NULL)
361 err(1, "malloc");
362
363 memset(data, 0xdb, data_len);
364 data[0] = 0x01;
365 data[data_len - 1] = 0xff;
366
367 if ((bio = BIO_new_mem_buf(data, data_len)) == NULL) {
368 fprintf(stderr, "FAIL: BIO_new_mem_buf failed\n");
369 goto failure;
370 }
371 if ((ret = BIO_read(bio, buf, 1)) != 1) {
372 fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
373 sizeof(buf));
374 goto failure;
375 }
376 if (buf[0] != 0x01) {
377 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
378 goto failure;
379 }
380 if ((ret = BIO_read(bio, buf, sizeof(buf))) != sizeof(buf)) {
381 fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
382 sizeof(buf));
383 goto failure;
384 }
385 if (buf[0] != 0xdb) {
386 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0xdb);
387 goto failure;
388 }
389 if ((ret = BIO_write(bio, buf, 1)) != -1) {
390 fprintf(stderr, "FAIL: BIO_write() = %d, want -1\n", ret);
391 goto failure;
392 }
393 if (BIO_eof(bio)) {
394 fprintf(stderr, "FAIL: BIO is EOF\n");
395 goto failure;
396 }
397 if (BIO_ctrl_pending(bio) != 2047) {
398 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 2047\n",
399 BIO_ctrl_pending(bio));
400 goto failure;
401 }
402 if ((ret = BIO_read(bio, buf, sizeof(buf))) != 2047) {
403 fprintf(stderr, "FAIL: BIO_read() = %d, want 2047\n", ret);
404 goto failure;
405 }
406 if (buf[2045] != 0xdb) {
407 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2045], 0xdb);
408 goto failure;
409 }
410 if (buf[2046] != 0xff) {
411 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2046], 0xff);
412 goto failure;
413 }
414 if (!BIO_eof(bio)) {
415 fprintf(stderr, "FAIL: BIO is not EOF\n");
416 goto failure;
417 }
418 if (BIO_ctrl_pending(bio) != 0) {
419 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 0\n",
420 BIO_ctrl_pending(bio));
421 goto failure;
422 }
423
424 if (!BIO_reset(bio)) {
425 fprintf(stderr, "FAIL: failed to reset bio\n");
426 goto failure;
427 }
428 if (BIO_eof(bio)) {
429 fprintf(stderr, "FAIL: BIO is EOF\n");
430 goto failure;
431 }
432 if (BIO_ctrl_pending(bio) != 4096) {
433 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 4096\n",
434 BIO_ctrl_pending(bio));
435 goto failure;
436 }
437 if ((ret = BIO_read(bio, buf, 2)) != 2) {
438 fprintf(stderr, "FAIL: BIO_read() = %d, want 2\n", ret);
439 goto failure;
440 }
441 if (buf[0] != 0x01) {
442 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
443 goto failure;
444 }
445 if (buf[1] != 0xdb) {
446 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[1], 0xdb);
447 goto failure;
448 }
449
450 failed = 0;
451
452 failure:
453 BIO_free(bio);
454 free(data);
455
456 return failed;
457}
458
459static int
460do_bio_mem_tests(void)
461{
462 int failed = 0;
463
464 failed |= bio_mem_test();
465 failed |= bio_mem_small_io_test();
466 failed |= bio_mem_readonly_test();
467
468 return failed;
469}
470
471#define N_CHAIN_BIOS 5 26#define N_CHAIN_BIOS 5
472 27
473static BIO * 28static BIO *
@@ -650,7 +205,7 @@ do_bio_link_chains_at(size_t i, size_t j, int use_bio_push)
650 int failed = 1; 205 int failed = 1;
651 206
652 memset(A, 0, sizeof(A)); 207 memset(A, 0, sizeof(A));
653 memset(B, 0, sizeof(A)); 208 memset(B, 0, sizeof(B));
654 209
655 /* Create two linear chains of BIOs. */ 210 /* Create two linear chains of BIOs. */
656 prev = NULL; 211 prev = NULL;
@@ -967,14 +522,11 @@ do_bio_set_next_link_test(void)
967int 522int
968main(int argc, char **argv) 523main(int argc, char **argv)
969{ 524{
970 int ret = 0; 525 int failed = 0;
971 526
972 ret |= do_bio_get_host_ip_tests(); 527 failed |= do_bio_chain_pop_test();
973 ret |= do_bio_get_port_tests(); 528 failed |= do_bio_push_link_test();
974 ret |= do_bio_mem_tests(); 529 failed |= do_bio_set_next_link_test();
975 ret |= do_bio_chain_pop_test();
976 ret |= do_bio_push_link_test();
977 ret |= do_bio_set_next_link_test();
978 530
979 return (ret); 531 return failed;
980} 532}
diff --git a/src/regress/lib/libcrypto/bio/bio_host.c b/src/regress/lib/libcrypto/bio/bio_host.c
new file mode 100644
index 0000000000..b3a4645167
--- /dev/null
+++ b/src/regress/lib/libcrypto/bio/bio_host.c
@@ -0,0 +1,154 @@
1/* $OpenBSD: bio_host.c,v 1.1 2022/12/08 17:49:02 tb Exp $ */
2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <arpa/inet.h>
24#include <netinet/in.h>
25
26#include <openssl/bio.h>
27
28struct bio_get_host_ip_test {
29 char *input;
30 uint32_t ip;
31 int ret;
32};
33
34struct bio_get_host_ip_test bio_get_host_ip_tests[] = {
35 {"", 0, 0},
36 {".", 0, 0},
37 {"1", 0, 0},
38 {"1.2", 0, 0},
39 {"1.2.3", 0, 0},
40 {"1.2.3.", 0, 0},
41 {"1.2.3.4", 0x01020304, 1},
42 {"1.2.3.256", 0, 0},
43 {"1:2:3::4", 0, 0},
44 {"0.0.0.0", INADDR_ANY, 1},
45 {"127.0.0.1", INADDR_LOOPBACK, 1},
46 {"localhost", INADDR_LOOPBACK, 1},
47 {"255.255.255.255", INADDR_BROADCAST, 1},
48 {"0xff.0xff.0xff.0xff", 0, 0},
49};
50
51#define N_BIO_GET_IP_TESTS \
52 (sizeof(bio_get_host_ip_tests) / sizeof(*bio_get_host_ip_tests))
53
54struct bio_get_port_test {
55 char *input;
56 unsigned short port;
57 int ret;
58};
59
60struct bio_get_port_test bio_get_port_tests[] = {
61 {NULL, 0, 0},
62 {"", 0, 0},
63 {"-1", 0, 0},
64 {"0", 0, 1},
65 {"1", 1, 1},
66 {"12345", 12345, 1},
67 {"65535", 65535, 1},
68 {"65536", 0, 0},
69 {"999999999999", 0, 0},
70 {"xyzzy", 0, 0},
71 {"https", 443, 1},
72 {"imaps", 993, 1},
73 {"telnet", 23, 1},
74};
75
76#define N_BIO_GET_PORT_TESTS \
77 (sizeof(bio_get_port_tests) / sizeof(*bio_get_port_tests))
78
79static int
80do_bio_get_host_ip_tests(void)
81{
82 struct bio_get_host_ip_test *bgit;
83 union {
84 unsigned char c[4];
85 uint32_t i;
86 } ip;
87 int failed = 0;
88 size_t i;
89 int ret;
90
91 for (i = 0; i < N_BIO_GET_IP_TESTS; i++) {
92 bgit = &bio_get_host_ip_tests[i];
93 memset(&ip, 0, sizeof(ip));
94
95 ret = BIO_get_host_ip(bgit->input, ip.c);
96 if (ret != bgit->ret) {
97 fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
98 i, bgit->input, ret ? "success" : "failure",
99 bgit->ret ? "success" : "failure");
100 failed = 1;
101 continue;
102 }
103 if (ret && ntohl(ip.i) != bgit->ip) {
104 fprintf(stderr, "FAIL: test %zd (\"%s\") returned ip "
105 "%x != %x\n", i, bgit->input,
106 ntohl(ip.i), bgit->ip);
107 failed = 1;
108 }
109 }
110
111 return failed;
112}
113
114static int
115do_bio_get_port_tests(void)
116{
117 struct bio_get_port_test *bgpt;
118 unsigned short port;
119 int failed = 0;
120 size_t i;
121 int ret;
122
123 for (i = 0; i < N_BIO_GET_PORT_TESTS; i++) {
124 bgpt = &bio_get_port_tests[i];
125 port = 0;
126
127 ret = BIO_get_port(bgpt->input, &port);
128 if (ret != bgpt->ret) {
129 fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
130 i, bgpt->input, ret ? "success" : "failure",
131 bgpt->ret ? "success" : "failure");
132 failed = 1;
133 continue;
134 }
135 if (ret && port != bgpt->port) {
136 fprintf(stderr, "FAIL: test %zd (\"%s\") returned port "
137 "%u != %u\n", i, bgpt->input, port, bgpt->port);
138 failed = 1;
139 }
140 }
141
142 return failed;
143}
144
145int
146main(int argc, char **argv)
147{
148 int failed = 0;
149
150 failed |= do_bio_get_host_ip_tests();
151 failed |= do_bio_get_port_tests();
152
153 return failed;
154}
diff --git a/src/regress/lib/libcrypto/bio/bio_mem.c b/src/regress/lib/libcrypto/bio/bio_mem.c
new file mode 100644
index 0000000000..0da7ee979f
--- /dev/null
+++ b/src/regress/lib/libcrypto/bio/bio_mem.c
@@ -0,0 +1,345 @@
1/* $OpenBSD: bio_mem.c,v 1.1 2022/12/08 17:49:02 tb Exp $ */
2/*
3 * Copyright (c) 2022 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <err.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <openssl/bio.h>
25#include <openssl/buffer.h>
26
27static int
28bio_mem_test(void)
29{
30 uint8_t *data = NULL;
31 size_t data_len;
32 uint8_t *rodata;
33 long rodata_len;
34 BUF_MEM *pbuf;
35 BUF_MEM *buf = NULL;
36 BIO *bio = NULL;
37 int ret;
38 int failed = 1;
39
40 data_len = 4096;
41 if ((data = malloc(data_len)) == NULL)
42 err(1, "malloc");
43
44 memset(data, 0xdb, data_len);
45 data[0] = 0x01;
46 data[data_len - 1] = 0xff;
47
48 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
49 fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
50 goto failure;
51 }
52 if ((ret = BIO_write(bio, data, data_len)) != (int)data_len) {
53 fprintf(stderr, "FAIL: BIO_write() = %d, want %zu\n", ret,
54 data_len);
55 goto failure;
56 }
57 if ((rodata_len = BIO_get_mem_data(bio, &rodata)) != (long)data_len) {
58 fprintf(stderr, "FAIL: BIO_get_mem_data() = %ld, want %zu\n",
59 rodata_len, data_len);
60 goto failure;
61 }
62 if (rodata[0] != 0x01) {
63 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", rodata[0], 0x01);
64 goto failure;
65 }
66 if (rodata[rodata_len - 1] != 0xff) {
67 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n",
68 rodata[rodata_len - 1], 0xff);
69 goto failure;
70 }
71
72 if (!BIO_get_mem_ptr(bio, &pbuf)) {
73 fprintf(stderr, "FAIL: BIO_get_mem_ptr() failed\n");
74 goto failure;
75 }
76 if (pbuf->length != data_len) {
77 fprintf(stderr, "FAIL: Got buffer with length %zu, want %zu\n",
78 pbuf->length, data_len);
79 goto failure;
80 }
81 if (memcmp(pbuf->data, data, data_len) != 0) {
82 fprintf(stderr, "FAIL: Got buffer with differing data\n");
83 goto failure;
84 }
85 pbuf = NULL;
86
87 if ((buf = BUF_MEM_new()) == NULL) {
88 fprintf(stderr, "FAIL: BUF_MEM_new() returned NULL\n");
89 goto failure;
90 }
91 if (!BIO_set_mem_buf(bio, buf, BIO_NOCLOSE)) {
92 fprintf(stderr, "FAIL: BUF_set_mem_buf() failed\n");
93 goto failure;
94 }
95 if ((ret = BIO_puts(bio, "Hello\n")) != 6) {
96 fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
97 goto failure;
98 }
99 if ((ret = BIO_puts(bio, "World\n")) != 6) {
100 fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
101 goto failure;
102 }
103 if (buf->length != 12) {
104 fprintf(stderr, "FAIL: buffer has length %zu, want %d\n",
105 buf->length, 12);
106 goto failure;
107 }
108 buf->length = 11;
109 if ((ret = BIO_gets(bio, data, data_len)) != 6) {
110 fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 6);
111 goto failure;
112 }
113 if (strcmp(data, "Hello\n") != 0) {
114 fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
115 data, "Hello\\n");
116 goto failure;
117 }
118 if ((ret = BIO_gets(bio, data, data_len)) != 5) {
119 fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 5);
120 goto failure;
121 }
122 if (strcmp(data, "World") != 0) {
123 fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
124 data, "World");
125 goto failure;
126 }
127
128 if (!BIO_eof(bio)) {
129 fprintf(stderr, "FAIL: BIO is not EOF\n");
130 goto failure;
131 }
132 if ((ret = BIO_read(bio, data, data_len)) != -1) {
133 fprintf(stderr, "FAIL: BIO_read() = %d, want -1\n", ret);
134 goto failure;
135 }
136 if (!BIO_set_mem_eof_return(bio, -2)) {
137 fprintf(stderr, "FAIL: BIO_set_mem_eof_return() failed\n");
138 goto failure;
139 }
140 if ((ret = BIO_read(bio, data, data_len)) != -2) {
141 fprintf(stderr, "FAIL: BIO_read() = %d, want -2\n", ret);
142 goto failure;
143 }
144
145 failed = 0;
146
147 failure:
148 free(data);
149 BUF_MEM_free(buf);
150 BIO_free(bio);
151
152 return failed;
153}
154
155static int
156bio_mem_small_io_test(void)
157{
158 uint8_t buf[2];
159 int i, j, ret;
160 BIO *bio;
161 int failed = 1;
162
163 memset(buf, 0xdb, sizeof(buf));
164
165 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
166 fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
167 goto failure;
168 }
169
170 for (i = 0; i < 100; i++) {
171 if (!BIO_reset(bio)) {
172 fprintf(stderr, "FAIL: BIO_reset() failed\n");
173 goto failure;
174 }
175 for (j = 0; j < 25000; j++) {
176 ret = BIO_write(bio, buf, sizeof(buf));
177 if (ret != sizeof(buf)) {
178 fprintf(stderr, "FAIL: BIO_write() = %d, "
179 "want %zu\n", ret, sizeof(buf));
180 goto failure;
181 }
182 }
183 for (j = 0; j < 25000; j++) {
184 ret = BIO_read(bio, buf, sizeof(buf));
185 if (ret != sizeof(buf)) {
186 fprintf(stderr, "FAIL: BIO_read() = %d, "
187 "want %zu\n", ret, sizeof(buf));
188 goto failure;
189 }
190 ret = BIO_write(bio, buf, sizeof(buf));
191 if (ret != sizeof(buf)) {
192 fprintf(stderr, "FAIL: BIO_write() = %d, "
193 "want %zu\n", ret, sizeof(buf));
194 goto failure;
195 }
196 }
197 for (j = 0; j < 25000; j++) {
198 ret = BIO_read(bio, buf, sizeof(buf));
199 if (ret != sizeof(buf)) {
200 fprintf(stderr, "FAIL: BIO_read() = %d, "
201 "want %zu\n", ret, sizeof(buf));
202 goto failure;
203 }
204 }
205 if (!BIO_eof(bio)) {
206 fprintf(stderr, "FAIL: BIO not EOF\n");
207 goto failure;
208 }
209 }
210
211 if (buf[0] != 0xdb || buf[1] != 0xdb) {
212 fprintf(stderr, "FAIL: buf = {0x%x, 0x%x}, want {0xdb, 0xdb}\n",
213 buf[0], buf[1]);
214 goto failure;
215 }
216
217 failed = 0;
218
219 failure:
220 BIO_free(bio);
221
222 return failed;
223}
224
225static int
226bio_mem_readonly_test(void)
227{
228 uint8_t *data = NULL;
229 size_t data_len;
230 uint8_t buf[2048];
231 BIO *bio = NULL;
232 int ret;
233 int failed = 1;
234
235 data_len = 4096;
236 if ((data = malloc(data_len)) == NULL)
237 err(1, "malloc");
238
239 memset(data, 0xdb, data_len);
240 data[0] = 0x01;
241 data[data_len - 1] = 0xff;
242
243 if ((bio = BIO_new_mem_buf(data, data_len)) == NULL) {
244 fprintf(stderr, "FAIL: BIO_new_mem_buf failed\n");
245 goto failure;
246 }
247 if ((ret = BIO_read(bio, buf, 1)) != 1) {
248 fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
249 sizeof(buf));
250 goto failure;
251 }
252 if (buf[0] != 0x01) {
253 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
254 goto failure;
255 }
256 if ((ret = BIO_read(bio, buf, sizeof(buf))) != sizeof(buf)) {
257 fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
258 sizeof(buf));
259 goto failure;
260 }
261 if (buf[0] != 0xdb) {
262 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0xdb);
263 goto failure;
264 }
265 if ((ret = BIO_write(bio, buf, 1)) != -1) {
266 fprintf(stderr, "FAIL: BIO_write() = %d, want -1\n", ret);
267 goto failure;
268 }
269 if (BIO_eof(bio)) {
270 fprintf(stderr, "FAIL: BIO is EOF\n");
271 goto failure;
272 }
273 if (BIO_ctrl_pending(bio) != 2047) {
274 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 2047\n",
275 BIO_ctrl_pending(bio));
276 goto failure;
277 }
278 if ((ret = BIO_read(bio, buf, sizeof(buf))) != 2047) {
279 fprintf(stderr, "FAIL: BIO_read() = %d, want 2047\n", ret);
280 goto failure;
281 }
282 if (buf[2045] != 0xdb) {
283 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2045], 0xdb);
284 goto failure;
285 }
286 if (buf[2046] != 0xff) {
287 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2046], 0xff);
288 goto failure;
289 }
290 if (!BIO_eof(bio)) {
291 fprintf(stderr, "FAIL: BIO is not EOF\n");
292 goto failure;
293 }
294 if (BIO_ctrl_pending(bio) != 0) {
295 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 0\n",
296 BIO_ctrl_pending(bio));
297 goto failure;
298 }
299
300 if (!BIO_reset(bio)) {
301 fprintf(stderr, "FAIL: failed to reset bio\n");
302 goto failure;
303 }
304 if (BIO_eof(bio)) {
305 fprintf(stderr, "FAIL: BIO is EOF\n");
306 goto failure;
307 }
308 if (BIO_ctrl_pending(bio) != 4096) {
309 fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 4096\n",
310 BIO_ctrl_pending(bio));
311 goto failure;
312 }
313 if ((ret = BIO_read(bio, buf, 2)) != 2) {
314 fprintf(stderr, "FAIL: BIO_read() = %d, want 2\n", ret);
315 goto failure;
316 }
317 if (buf[0] != 0x01) {
318 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
319 goto failure;
320 }
321 if (buf[1] != 0xdb) {
322 fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[1], 0xdb);
323 goto failure;
324 }
325
326 failed = 0;
327
328 failure:
329 BIO_free(bio);
330 free(data);
331
332 return failed;
333}
334
335int
336main(int argc, char **argv)
337{
338 int failed = 0;
339
340 failed |= bio_mem_test();
341 failed |= bio_mem_small_io_test();
342 failed |= bio_mem_readonly_test();
343
344 return failed;
345}