]>
Commit | Line | Data |
---|---|---|
3bfc58ad | 1 | /* |
da1c088f | 2 | * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. |
3bfc58ad MC |
3 | * |
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 | |
8 | */ | |
9 | ||
10 | #include <openssl/bio.h> | |
11 | #include "testutil.h" | |
12 | ||
3a857b95 | 13 | #ifndef OPENSSL_NO_DGRAM |
3bfc58ad MC |
14 | static int test_dgram(void) |
15 | { | |
16 | BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL; | |
17 | int testresult = 0; | |
18 | const char msg1[] = "12345656"; | |
19 | const char msg2[] = "abcdefghijklmno"; | |
20 | const char msg3[] = "ABCDEF"; | |
21 | const char msg4[] = "FEDCBA"; | |
22 | char buf[80]; | |
23 | ||
24 | if (!TEST_ptr(bio)) | |
25 | goto err; | |
26 | ||
27 | rbio = BIO_new_mem_buf(msg1, sizeof(msg1)); | |
28 | if (!TEST_ptr(rbio)) | |
29 | goto err; | |
30 | ||
eb4129e1 | 31 | /* Setting the EOF return value on a non datagram mem BIO should be fine */ |
3bfc58ad MC |
32 | if (!TEST_int_gt(BIO_set_mem_eof_return(rbio, 0), 0)) |
33 | goto err; | |
34 | ||
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)) | |
37 | goto err; | |
38 | ||
39 | /* Write 4 dgrams */ | |
40 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
41 | goto err; | |
42 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
43 | goto err; | |
44 | if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) | |
45 | goto err; | |
46 | if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4))) | |
47 | goto err; | |
48 | ||
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))) | |
58 | goto err; | |
59 | ||
60 | /* Interleaving writes and reads should be fine */ | |
61 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
62 | goto err; | |
63 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
64 | goto err; | |
65 | if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1)) | |
66 | || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))) | |
67 | goto err; | |
68 | if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) | |
69 | goto err; | |
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))) | |
74 | goto err; | |
75 | ||
76 | /* | |
77 | * Requesting less than the available data in a dgram should not impact the | |
78 | * next packet. | |
79 | */ | |
80 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
81 | goto err; | |
82 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
83 | goto err; | |
84 | if (!TEST_int_eq(BIO_read(bio, buf, /* Short buffer */ 2), 2) | |
85 | || !TEST_mem_eq(buf, 2, msg1, 2)) | |
86 | goto err; | |
87 | if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2)) | |
88 | || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))) | |
89 | goto err; | |
90 | ||
91 | /* | |
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. | |
95 | */ | |
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))) | |
100 | goto err; | |
101 | ||
3a857b95 TM |
102 | if (!TEST_int_eq(BIO_dgram_set_mtu(bio, 123456), 1) |
103 | || !TEST_int_eq(BIO_dgram_get_mtu(bio), 123456)) | |
104 | goto err; | |
105 | ||
3bfc58ad MC |
106 | testresult = 1; |
107 | err: | |
108 | BIO_free(rbio); | |
109 | BIO_free(bio); | |
110 | return testresult; | |
111 | } | |
3a857b95 | 112 | #endif |
3bfc58ad MC |
113 | |
114 | int setup_tests(void) | |
115 | { | |
116 | if (!test_skip_common_options()) { | |
117 | TEST_error("Error parsing test options\n"); | |
118 | return 0; | |
119 | } | |
120 | ||
3a857b95 | 121 | #ifndef OPENSSL_NO_DGRAM |
3bfc58ad | 122 | ADD_TEST(test_dgram); |
3a857b95 | 123 | #endif |
3bfc58ad MC |
124 | |
125 | return 1; | |
126 | } |