2 * Copyright 2018-2021 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
11 #include <openssl/buffer.h>
12 #include <openssl/bio.h>
16 static int test_bio_memleak(void)
21 static const char str[] = "BIO test\n";
24 bio = BIO_new(BIO_s_mem());
27 bufmem.length = sizeof(str);
28 bufmem.data = (char *) str;
29 bufmem.max = bufmem.length;
30 BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
31 BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
32 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str)))
34 if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str)))
43 static int test_bio_get_mem(void)
47 BUF_MEM *bufmem = NULL;
49 bio = BIO_new(BIO_s_mem());
52 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
54 BIO_get_mem_ptr(bio, &bufmem);
55 if (!TEST_ptr(bufmem))
57 if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0))
61 if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12))
71 static int test_bio_new_mem_buf(void)
78 bio = BIO_new_mem_buf("Hello World\n", 12);
81 if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
83 if (!TEST_mem_eq(data, 5, "Hello", 5))
85 if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
87 if (!TEST_int_lt(BIO_write(bio, "test", 4), 0))
89 if (!TEST_int_eq(BIO_read(bio, data, 16), 7))
91 if (!TEST_mem_eq(data, 7, " World\n", 7))
93 if (!TEST_int_gt(BIO_reset(bio), 0))
95 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
97 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
106 static int test_bio_rdonly_mem_buf(void)
109 BIO *bio, *bio2 = NULL;
113 bio = BIO_new_mem_buf("Hello World\n", 12);
116 if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
118 if (!TEST_mem_eq(data, 5, "Hello", 5))
120 if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
122 (void)BIO_set_close(bio, BIO_NOCLOSE);
124 bio2 = BIO_new(BIO_s_mem());
127 BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE);
128 BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY);
130 if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
132 if (!TEST_mem_eq(data, 7, " World\n", 7))
134 if (!TEST_int_gt(BIO_reset(bio2), 0))
136 if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
138 if (!TEST_mem_eq(data, 7, " World\n", 7))
148 static int test_bio_rdwr_rdonly(void)
154 bio = BIO_new(BIO_s_mem());
157 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
160 BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
161 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
163 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
165 if (!TEST_int_gt(BIO_reset(bio), 0))
168 BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
169 if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
171 if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
174 if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
184 static int test_bio_nonclear_rst(void)
190 bio = BIO_new(BIO_s_mem());
193 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
196 BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST);
198 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
200 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
202 if (!TEST_int_gt(BIO_reset(bio), 0))
205 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
207 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
210 BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST);
211 if (!TEST_int_gt(BIO_reset(bio), 0))
214 if (!TEST_int_lt(BIO_read(bio, data, 16), 1))
224 static int error_callback_fired;
225 static long BIO_error_callback(BIO *bio, int cmd, const char *argp,
226 size_t len, int argi,
227 long argl, int ret, size_t *processed)
229 if ((cmd & (BIO_CB_READ | BIO_CB_RETURN)) != 0) {
230 error_callback_fired = 1;
231 ret = 0; /* fail for read operations to simulate error in input BIO */
236 /* Checks i2d_ASN1_bio_stream() is freeing all memory when input BIO ends unexpectedly. */
237 static int test_bio_i2d_ASN1_mime(void)
240 BIO *bio = NULL, *out = NULL;
242 static const char str[] = "BIO mime test\n";
245 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
248 bufmem.length = sizeof(str);
249 bufmem.data = (char *) str;
250 bufmem.max = bufmem.length;
251 BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
252 BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
253 BIO_set_callback_ex(bio, BIO_error_callback);
255 if (!TEST_ptr(out = BIO_new(BIO_s_mem())))
257 if (!TEST_ptr(p7 = PKCS7_new()))
259 if (!TEST_true(PKCS7_set_type(p7, NID_pkcs7_data)))
262 error_callback_fired = 0;
264 if (!TEST_false(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio,
265 SMIME_STREAM | SMIME_BINARY,
266 ASN1_ITEM_rptr(PKCS7))))
269 if (!TEST_int_eq(error_callback_fired, 1))
281 int setup_tests(void)
283 ADD_TEST(test_bio_memleak);
284 ADD_TEST(test_bio_get_mem);
285 ADD_TEST(test_bio_new_mem_buf);
286 ADD_TEST(test_bio_rdonly_mem_buf);
287 ADD_TEST(test_bio_rdwr_rdonly);
288 ADD_TEST(test_bio_nonclear_rst);
289 ADD_TEST(test_bio_i2d_ASN1_mime);