summaryrefslogtreecommitdiff
path: root/test/membio_test.c
blob: 9d551dcd8c67f56a0edb482bfaaa6b55b1082ddb (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
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/bio.h>
#include "testutil.h"

#ifndef OPENSSL_NO_DGRAM
static int test_dgram(void)
{
    BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL;
    int testresult = 0;
    const char msg1[] = "12345656";
    const char msg2[] = "abcdefghijklmno";
    const char msg3[] = "ABCDEF";
    const char msg4[] = "FEDCBA";
    char buf[80];

    if (!TEST_ptr(bio))
        goto err;

    rbio = BIO_new_mem_buf(msg1, sizeof(msg1));
    if (!TEST_ptr(rbio))
        goto err;

    /* Seeting the EOF return value on a non datagram mem BIO should be fine */
    if (!TEST_int_gt(BIO_set_mem_eof_return(rbio, 0), 0))
        goto err;

    /* Setting the EOF return value on a datagram mem BIO should fail */
    if (!TEST_int_le(BIO_set_mem_eof_return(bio, 0), 0))
        goto err;

    /* Write 4 dgrams */
    if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4)))
        goto err;

    /* Reading all 4 dgrams out again should all be the correct size */
    if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
            || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))
            || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
            || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
            || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
            || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3))
            || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg4))
            || !TEST_mem_eq(buf, sizeof(msg4), msg4, sizeof(msg4)))
        goto err;

    /* Interleaving writes and reads should be fine */
    if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
        goto err;
    if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
            || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
        goto err;
    if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
            || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
            || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
            || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3)))
        goto err;

    /*
     * Requesting less than the available data in a dgram should not impact the
     * next packet.
     */
    if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
        goto err;
    if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
        goto err;
    if (!TEST_int_eq(BIO_read(bio, buf, /* Short buffer */ 2), 2)
            || !TEST_mem_eq(buf, 2, msg1, 2))
        goto err;
    if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
            || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2)))
        goto err;

    /*
     * Writing a zero length datagram will return zero, but no datagrams will
     * be written. Attempting to read when there are no datagrams to read should
     * return a negative result, but not eof. Retry flags will be set.
     */
    if (!TEST_int_eq(BIO_write(bio, NULL, 0), 0)
            || !TEST_int_lt(BIO_read(bio, buf, sizeof(buf)), 0)
            || !TEST_false(BIO_eof(bio))
            || !TEST_true(BIO_should_retry(bio)))
        goto err;

    if (!TEST_int_eq(BIO_dgram_set_mtu(bio, 123456), 1)
            || !TEST_int_eq(BIO_dgram_get_mtu(bio), 123456))
        goto err;

    testresult = 1;
 err:
    BIO_free(rbio);
    BIO_free(bio);
    return testresult;
}
#endif

int setup_tests(void)
{
    if (!test_skip_common_options()) {
        TEST_error("Error parsing test options\n");
        return 0;
    }

#ifndef OPENSSL_NO_DGRAM
    ADD_TEST(test_dgram);
#endif

    return 1;
}