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 #ifndef OPENSSL_NO_DGRAM
14 static int test_dgram(void)
16 BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL;
18 const char msg1[] = "12345656";
19 const char msg2[] = "abcdefghijklmno";
20 const char msg3[] = "ABCDEF";
21 const char msg4[] = "FEDCBA";
27 rbio = BIO_new_mem_buf(msg1, sizeof(msg1));
31 /* Seeting the EOF return value on a non datagram mem BIO should be fine */
32 if (!TEST_int_gt(BIO_set_mem_eof_return(rbio, 0), 0))
35 /* Setting the EOF return value on a datagram mem BIO should fail */
36 if (!TEST_int_le(BIO_set_mem_eof_return(bio, 0), 0))
40 if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
42 if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
44 if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
46 if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4)))
49 /* Reading all 4 dgrams out again should all be the correct size */
50 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
51 || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))
52 || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
53 || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
54 || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
55 || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3))
56 || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg4))
57 || !TEST_mem_eq(buf, sizeof(msg4), msg4, sizeof(msg4)))
60 /* Interleaving writes and reads should be fine */
61 if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
63 if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
65 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
66 || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1)))
68 if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
70 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
71 || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
72 || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
73 || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3)))
77 * Requesting less than the available data in a dgram should not impact the
80 if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
82 if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
84 if (!TEST_int_eq(BIO_read(bio, buf, /* Short buffer */ 2), 2)
85 || !TEST_mem_eq(buf, 2, msg1, 2))
87 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
88 || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2)))
92 * Writing a zero length datagram will return zero, but no datagrams will
93 * be written. Attempting to read when there are no datagrams to read should
94 * return a negative result, but not eof. Retry flags will be set.
96 if (!TEST_int_eq(BIO_write(bio, NULL, 0), 0)
97 || !TEST_int_lt(BIO_read(bio, buf, sizeof(buf)), 0)
98 || !TEST_false(BIO_eof(bio))
99 || !TEST_true(BIO_should_retry(bio)))
102 if (!TEST_int_eq(BIO_dgram_set_mtu(bio, 123456), 1)
103 || !TEST_int_eq(BIO_dgram_get_mtu(bio), 123456))
114 int setup_tests(void)
116 if (!test_skip_common_options()) {
117 TEST_error("Error parsing test options\n");
121 #ifndef OPENSSL_NO_DGRAM
122 ADD_TEST(test_dgram);