]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/bio_memleak_test.c
Make sure we use the libctx when creating an EVP_PKEY_CTX in libssl
[thirdparty/openssl.git] / test / bio_memleak_test.c
CommitLineData
c6048af2
CM
1/*
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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#include <stdio.h>
10#include <string.h>
11#include <openssl/buffer.h>
12#include <openssl/bio.h>
13
14#include "testutil.h"
15
16static int test_bio_memleak(void)
17{
18 int ok = 0;
19 BIO *bio;
20 BUF_MEM bufmem;
c9dc22bc 21 static const char str[] = "BIO test\n";
c6048af2
CM
22 char buf[100];
23
24 bio = BIO_new(BIO_s_mem());
c9dc22bc 25 if (!TEST_ptr(bio))
c6048af2 26 goto finish;
c9dc22bc 27 bufmem.length = sizeof(str);
c6048af2
CM
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);
c9dc22bc
BE
32 if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str)))
33 goto finish;
34 if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str)))
35 goto finish;
36 ok = 1;
37
38finish:
39 BIO_free(bio);
40 return ok;
41}
c6048af2 42
c9dc22bc
BE
43static int test_bio_get_mem(void)
44{
45 int ok = 0;
46 BIO *bio = NULL;
47 BUF_MEM *bufmem = NULL;
c6048af2 48
c9dc22bc
BE
49 bio = BIO_new(BIO_s_mem());
50 if (!TEST_ptr(bio))
51 goto finish;
52 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
53 goto finish;
54 BIO_get_mem_ptr(bio, &bufmem);
55 if (!TEST_ptr(bufmem))
56 goto finish;
57 if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0))
58 goto finish;
59 BIO_free(bio);
60 bio = NULL;
61 if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12))
62 goto finish;
63 ok = 1;
c6048af2
CM
64
65finish:
66 BIO_free(bio);
c9dc22bc 67 BUF_MEM_free(bufmem);
c6048af2
CM
68 return ok;
69}
70
d34bce03
TM
71static int test_bio_new_mem_buf(void)
72{
73 int ok = 0;
74 BIO *bio;
75 BUF_MEM *bufmem;
76 char data[16];
77
78 bio = BIO_new_mem_buf("Hello World\n", 12);
79 if (!TEST_ptr(bio))
80 goto finish;
81 if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
82 goto finish;
83 if (!TEST_mem_eq(data, 5, "Hello", 5))
84 goto finish;
85 if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
86 goto finish;
87 if (!TEST_int_lt(BIO_write(bio, "test", 4), 0))
88 goto finish;
89 if (!TEST_int_eq(BIO_read(bio, data, 16), 7))
90 goto finish;
91 if (!TEST_mem_eq(data, 7, " World\n", 7))
92 goto finish;
93 if (!TEST_int_gt(BIO_reset(bio), 0))
94 goto finish;
95 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
96 goto finish;
97 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
98 goto finish;
99 ok = 1;
100
101finish:
102 BIO_free(bio);
103 return ok;
104}
105
106static int test_bio_rdonly_mem_buf(void)
107{
108 int ok = 0;
109 BIO *bio, *bio2 = NULL;
110 BUF_MEM *bufmem;
111 char data[16];
112
113 bio = BIO_new_mem_buf("Hello World\n", 12);
114 if (!TEST_ptr(bio))
115 goto finish;
116 if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
117 goto finish;
118 if (!TEST_mem_eq(data, 5, "Hello", 5))
119 goto finish;
120 if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
121 goto finish;
122 (void)BIO_set_close(bio, BIO_NOCLOSE);
123
124 bio2 = BIO_new(BIO_s_mem());
125 if (!TEST_ptr(bio2))
126 goto finish;
127 BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE);
128 BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY);
129
130 if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
131 goto finish;
132 if (!TEST_mem_eq(data, 7, " World\n", 7))
133 goto finish;
134 if (!TEST_int_gt(BIO_reset(bio2), 0))
135 goto finish;
136 if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
137 goto finish;
138 if (!TEST_mem_eq(data, 7, " World\n", 7))
139 goto finish;
140 ok = 1;
141
142finish:
143 BIO_free(bio);
144 BIO_free(bio2);
145 return ok;
146}
147
06add280
TM
148static int test_bio_rdwr_rdonly(void)
149{
150 int ok = 0;
151 BIO *bio = NULL;
152 char data[16];
153
154 bio = BIO_new(BIO_s_mem());
155 if (!TEST_ptr(bio))
156 goto finish;
157 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
158 goto finish;
159
160 BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
161 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
162 goto finish;
163 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
164 goto finish;
165 if (!TEST_int_gt(BIO_reset(bio), 0))
166 goto finish;
167
168 BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
169 if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
170 goto finish;
171 if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
172 goto finish;
173
174 if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
175 goto finish;
176
177 ok = 1;
178
179finish:
180 BIO_free(bio);
181 return ok;
182}
183
8b7b3292
TM
184static int test_bio_nonclear_rst(void)
185{
186 int ok = 0;
187 BIO *bio = NULL;
188 char data[16];
189
190 bio = BIO_new(BIO_s_mem());
191 if (!TEST_ptr(bio))
192 goto finish;
193 if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
194 goto finish;
195
196 BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST);
197
198 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
199 goto finish;
200 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
201 goto finish;
202 if (!TEST_int_gt(BIO_reset(bio), 0))
203 goto finish;
204
205 if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
206 goto finish;
207 if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
208 goto finish;
209
210 BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST);
211 if (!TEST_int_gt(BIO_reset(bio), 0))
212 goto finish;
213
214 if (!TEST_int_lt(BIO_read(bio, data, 16), 1))
215 goto finish;
216
217 ok = 1;
218
219finish:
220 BIO_free(bio);
221 return ok;
222}
06add280 223
c6048af2
CM
224int setup_tests(void)
225{
226 ADD_TEST(test_bio_memleak);
c9dc22bc 227 ADD_TEST(test_bio_get_mem);
d34bce03
TM
228 ADD_TEST(test_bio_new_mem_buf);
229 ADD_TEST(test_bio_rdonly_mem_buf);
06add280 230 ADD_TEST(test_bio_rdwr_rdonly);
8b7b3292 231 ADD_TEST(test_bio_nonclear_rst);
c6048af2
CM
232 return 1;
233}