]>
Commit | Line | Data |
---|---|---|
3bfc58ad MC |
1 | /* |
2 | * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. | |
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 | ||
13 | static int test_dgram(void) | |
14 | { | |
15 | BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL; | |
16 | int testresult = 0; | |
17 | const char msg1[] = "12345656"; | |
18 | const char msg2[] = "abcdefghijklmno"; | |
19 | const char msg3[] = "ABCDEF"; | |
20 | const char msg4[] = "FEDCBA"; | |
21 | char buf[80]; | |
22 | ||
23 | if (!TEST_ptr(bio)) | |
24 | goto err; | |
25 | ||
26 | rbio = BIO_new_mem_buf(msg1, sizeof(msg1)); | |
27 | if (!TEST_ptr(rbio)) | |
28 | goto err; | |
29 | ||
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)) | |
32 | goto err; | |
33 | ||
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)) | |
36 | goto err; | |
37 | ||
38 | /* Write 4 dgrams */ | |
39 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
40 | goto err; | |
41 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
42 | goto err; | |
43 | if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) | |
44 | goto err; | |
45 | if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4))) | |
46 | goto err; | |
47 | ||
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))) | |
57 | goto err; | |
58 | ||
59 | /* Interleaving writes and reads should be fine */ | |
60 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
61 | goto err; | |
62 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
63 | goto err; | |
64 | if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1)) | |
65 | || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))) | |
66 | goto err; | |
67 | if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) | |
68 | goto err; | |
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))) | |
73 | goto err; | |
74 | ||
75 | /* | |
76 | * Requesting less than the available data in a dgram should not impact the | |
77 | * next packet. | |
78 | */ | |
79 | if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) | |
80 | goto err; | |
81 | if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) | |
82 | goto err; | |
83 | if (!TEST_int_eq(BIO_read(bio, buf, /* Short buffer */ 2), 2) | |
84 | || !TEST_mem_eq(buf, 2, msg1, 2)) | |
85 | goto err; | |
86 | if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2)) | |
87 | || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))) | |
88 | goto err; | |
89 | ||
90 | /* | |
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. | |
94 | */ | |
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))) | |
99 | goto err; | |
100 | ||
101 | testresult = 1; | |
102 | err: | |
103 | BIO_free(rbio); | |
104 | BIO_free(bio); | |
105 | return testresult; | |
106 | } | |
107 | ||
108 | ||
109 | int setup_tests(void) | |
110 | { | |
111 | if (!test_skip_common_options()) { | |
112 | TEST_error("Error parsing test options\n"); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | ADD_TEST(test_dgram); | |
117 | ||
118 | return 1; | |
119 | } |