]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/membio_test.c
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/bio.h>
13 static int test_dgram(void)
15 BIO
*bio
= BIO_new(BIO_s_dgram_mem()), *rbio
= NULL
;
17 const char msg1
[] = "12345656";
18 const char msg2
[] = "abcdefghijklmno";
19 const char msg3
[] = "ABCDEF";
20 const char msg4
[] = "FEDCBA";
26 rbio
= BIO_new_mem_buf(msg1
, sizeof(msg1
));
30 /* Seeting the EOF return value on a non datagram mem BIO should be fine */
31 if (!TEST_int_gt(BIO_set_mem_eof_return(rbio
, 0), 0))
34 /* Setting the EOF return value on a datagram mem BIO should fail */
35 if (!TEST_int_le(BIO_set_mem_eof_return(bio
, 0), 0))
39 if (!TEST_int_eq(BIO_write(bio
, msg1
, sizeof(msg1
)), sizeof(msg1
)))
41 if (!TEST_int_eq(BIO_write(bio
, msg2
, sizeof(msg2
)), sizeof(msg2
)))
43 if (!TEST_int_eq(BIO_write(bio
, msg3
, sizeof(msg3
)), sizeof(msg3
)))
45 if (!TEST_int_eq(BIO_write(bio
, msg4
, sizeof(msg4
)), sizeof(msg4
)))
48 /* Reading all 4 dgrams out again should all be the correct size */
49 if (!TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg1
))
50 || !TEST_mem_eq(buf
, sizeof(msg1
), msg1
, sizeof(msg1
))
51 || !TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg2
))
52 || !TEST_mem_eq(buf
, sizeof(msg2
), msg2
, sizeof(msg2
))
53 || !TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg3
))
54 || !TEST_mem_eq(buf
, sizeof(msg3
), msg3
, sizeof(msg3
))
55 || !TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg4
))
56 || !TEST_mem_eq(buf
, sizeof(msg4
), msg4
, sizeof(msg4
)))
59 /* Interleaving writes and reads should be fine */
60 if (!TEST_int_eq(BIO_write(bio
, msg1
, sizeof(msg1
)), sizeof(msg1
)))
62 if (!TEST_int_eq(BIO_write(bio
, msg2
, sizeof(msg2
)), sizeof(msg2
)))
64 if (!TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg1
))
65 || !TEST_mem_eq(buf
, sizeof(msg1
), msg1
, sizeof(msg1
)))
67 if (!TEST_int_eq(BIO_write(bio
, msg3
, sizeof(msg3
)), sizeof(msg3
)))
69 if (!TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg2
))
70 || !TEST_mem_eq(buf
, sizeof(msg2
), msg2
, sizeof(msg2
))
71 || !TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg3
))
72 || !TEST_mem_eq(buf
, sizeof(msg3
), msg3
, sizeof(msg3
)))
76 * Requesting less than the available data in a dgram should not impact the
79 if (!TEST_int_eq(BIO_write(bio
, msg1
, sizeof(msg1
)), sizeof(msg1
)))
81 if (!TEST_int_eq(BIO_write(bio
, msg2
, sizeof(msg2
)), sizeof(msg2
)))
83 if (!TEST_int_eq(BIO_read(bio
, buf
, /* Short buffer */ 2), 2)
84 || !TEST_mem_eq(buf
, 2, msg1
, 2))
86 if (!TEST_int_eq(BIO_read(bio
, buf
, sizeof(buf
)), sizeof(msg2
))
87 || !TEST_mem_eq(buf
, sizeof(msg2
), msg2
, sizeof(msg2
)))
91 * Writing a zero length datagram will return zero, but no datagrams will
92 * be written. Attempting to read when there are no datagrams to read should
93 * return a negative result, but not eof. Retry flags will be set.
95 if (!TEST_int_eq(BIO_write(bio
, NULL
, 0), 0)
96 || !TEST_int_lt(BIO_read(bio
, buf
, sizeof(buf
)), 0)
97 || !TEST_false(BIO_eof(bio
))
98 || !TEST_true(BIO_should_retry(bio
)))
109 int setup_tests(void)
111 if (!test_skip_common_options()) {
112 TEST_error("Error parsing test options\n");
116 ADD_TEST(test_dgram
);