diff options
author | jsing <> | 2022-02-17 18:51:58 +0000 |
---|---|---|
committer | jsing <> | 2022-02-17 18:51:58 +0000 |
commit | fefaffddb0bad25ca346024d37634f0dbf79a996 (patch) | |
tree | 9315b216cfd34c682f404a75247b2e0bd0472bef /src | |
parent | 6f9eb565f453fa9044079a79dec3faa21c640811 (diff) | |
download | openbsd-fefaffddb0bad25ca346024d37634f0dbf79a996.tar.gz openbsd-fefaffddb0bad25ca346024d37634f0dbf79a996.tar.bz2 openbsd-fefaffddb0bad25ca346024d37634f0dbf79a996.zip |
Add tests for memory BIO.
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bio/biotest.c | 256 |
1 files changed, 254 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/bio/biotest.c b/src/regress/lib/libcrypto/bio/biotest.c index 867305a904..442c64e93b 100644 --- a/src/regress/lib/libcrypto/bio/biotest.c +++ b/src/regress/lib/libcrypto/bio/biotest.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* $OpenBSD: biotest.c,v 1.6 2017/04/30 17:46:27 beck Exp $ */ | 1 | /* $OpenBSD: biotest.c,v 1.7 2022/02/17 18:51:58 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014, 2022 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
5 | * Permission to use, copy, modify, and distribute this software for any | 5 | * Permission to use, copy, modify, and distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above | 6 | * purpose with or without fee is hereby granted, provided that the above |
@@ -17,6 +17,7 @@ | |||
17 | 17 | ||
18 | #include <sys/types.h> | 18 | #include <sys/types.h> |
19 | 19 | ||
20 | #include <err.h> | ||
20 | #include <stdint.h> | 21 | #include <stdint.h> |
21 | #include <stdlib.h> | 22 | #include <stdlib.h> |
22 | #include <string.h> | 23 | #include <string.h> |
@@ -24,6 +25,7 @@ | |||
24 | #include <netinet/in.h> | 25 | #include <netinet/in.h> |
25 | 26 | ||
26 | #include <openssl/bio.h> | 27 | #include <openssl/bio.h> |
28 | #include <openssl/buffer.h> | ||
27 | #include <openssl/err.h> | 29 | #include <openssl/err.h> |
28 | 30 | ||
29 | struct bio_get_host_ip_test { | 31 | struct bio_get_host_ip_test { |
@@ -143,6 +145,255 @@ do_bio_get_port_tests(void) | |||
143 | return failed; | 145 | return failed; |
144 | } | 146 | } |
145 | 147 | ||
148 | static int | ||
149 | bio_mem_test(void) | ||
150 | { | ||
151 | uint8_t *data = NULL; | ||
152 | size_t data_len; | ||
153 | uint8_t *rodata; | ||
154 | long rodata_len; | ||
155 | BUF_MEM *pbuf; | ||
156 | BUF_MEM *buf = NULL; | ||
157 | BIO *bio = NULL; | ||
158 | int ret; | ||
159 | int failed = 1; | ||
160 | |||
161 | data_len = 4096; | ||
162 | if ((data = malloc(data_len)) == NULL) | ||
163 | err(1, "malloc"); | ||
164 | |||
165 | memset(data, 0xdb, data_len); | ||
166 | data[0] = 0x01; | ||
167 | data[data_len - 1] = 0xff; | ||
168 | |||
169 | if ((bio = BIO_new(BIO_s_mem())) == NULL) { | ||
170 | fprintf(stderr, "FAIL: BIO_new() returned NULL\n"); | ||
171 | goto failure; | ||
172 | } | ||
173 | if ((ret = BIO_write(bio, data, data_len)) != (int)data_len) { | ||
174 | fprintf(stderr, "FAIL: BIO_write() = %d, want %zu\n", ret, | ||
175 | data_len); | ||
176 | goto failure; | ||
177 | } | ||
178 | if ((rodata_len = BIO_get_mem_data(bio, &rodata)) != (long)data_len) { | ||
179 | fprintf(stderr, "FAIL: BIO_get_mem_data() = %ld, want %zu\n", | ||
180 | rodata_len, data_len); | ||
181 | goto failure; | ||
182 | } | ||
183 | if (rodata[0] != 0x01) { | ||
184 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", rodata[0], 0x01); | ||
185 | goto failure; | ||
186 | } | ||
187 | if (rodata[rodata_len - 1] != 0xff) { | ||
188 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", | ||
189 | rodata[rodata_len - 1], 0xff); | ||
190 | goto failure; | ||
191 | } | ||
192 | |||
193 | if (!BIO_get_mem_ptr(bio, &pbuf)) { | ||
194 | fprintf(stderr, "FAIL: BIO_get_mem_ptr() failed\n"); | ||
195 | goto failure; | ||
196 | } | ||
197 | if (pbuf->length != data_len) { | ||
198 | fprintf(stderr, "FAIL: Got buffer with length %zu, want %zu\n", | ||
199 | pbuf->length, data_len); | ||
200 | goto failure; | ||
201 | } | ||
202 | if (memcmp(pbuf->data, data, data_len) != 0) { | ||
203 | fprintf(stderr, "FAIL: Got buffer with differing data\n"); | ||
204 | goto failure; | ||
205 | } | ||
206 | pbuf = NULL; | ||
207 | |||
208 | if ((buf = BUF_MEM_new()) == NULL) { | ||
209 | fprintf(stderr, "FAIL: BUF_MEM_new() returned NULL\n"); | ||
210 | goto failure; | ||
211 | } | ||
212 | if (!BIO_set_mem_buf(bio, buf, BIO_NOCLOSE)) { | ||
213 | fprintf(stderr, "FAIL: BUF_set_mem_buf() failed\n"); | ||
214 | goto failure; | ||
215 | } | ||
216 | if ((ret = BIO_puts(bio, "Hello\n")) != 6) { | ||
217 | fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6); | ||
218 | goto failure; | ||
219 | } | ||
220 | if ((ret = BIO_puts(bio, "World\n")) != 6) { | ||
221 | fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6); | ||
222 | goto failure; | ||
223 | } | ||
224 | if (buf->length != 12) { | ||
225 | fprintf(stderr, "FAIL: buffer has length %zu, want %d\n", | ||
226 | buf->length, 12); | ||
227 | goto failure; | ||
228 | } | ||
229 | buf->length = 11; | ||
230 | if ((ret = BIO_gets(bio, data, data_len)) != 6) { | ||
231 | fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 6); | ||
232 | goto failure; | ||
233 | } | ||
234 | if (strcmp(data, "Hello\n") != 0) { | ||
235 | fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n", | ||
236 | data, "Hello\\n"); | ||
237 | goto failure; | ||
238 | } | ||
239 | if ((ret = BIO_gets(bio, data, data_len)) != 5) { | ||
240 | fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 5); | ||
241 | goto failure; | ||
242 | } | ||
243 | if (strcmp(data, "World") != 0) { | ||
244 | fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n", | ||
245 | data, "World"); | ||
246 | goto failure; | ||
247 | } | ||
248 | |||
249 | if (!BIO_eof(bio)) { | ||
250 | fprintf(stderr, "FAIL: BIO is not EOF\n"); | ||
251 | goto failure; | ||
252 | } | ||
253 | if ((ret = BIO_read(bio, data, data_len)) != -1) { | ||
254 | fprintf(stderr, "FAIL: BIO_read() = %d, want -1\n", ret); | ||
255 | goto failure; | ||
256 | } | ||
257 | if (!BIO_set_mem_eof_return(bio, -2)) { | ||
258 | fprintf(stderr, "FAIL: BIO_set_mem_eof_return() failed\n"); | ||
259 | goto failure; | ||
260 | } | ||
261 | if ((ret = BIO_read(bio, data, data_len)) != -2) { | ||
262 | fprintf(stderr, "FAIL: BIO_read() = %d, want -2\n", ret); | ||
263 | goto failure; | ||
264 | } | ||
265 | |||
266 | failed = 0; | ||
267 | |||
268 | failure: | ||
269 | free(data); | ||
270 | BUF_MEM_free(buf); | ||
271 | BIO_free(bio); | ||
272 | |||
273 | return failed; | ||
274 | } | ||
275 | |||
276 | static int | ||
277 | bio_mem_readonly_test(void) | ||
278 | { | ||
279 | uint8_t *data = NULL; | ||
280 | size_t data_len; | ||
281 | uint8_t buf[2048]; | ||
282 | BIO *bio = NULL; | ||
283 | int ret; | ||
284 | int failed = 1; | ||
285 | |||
286 | data_len = 4096; | ||
287 | if ((data = malloc(data_len)) == NULL) | ||
288 | err(1, "malloc"); | ||
289 | |||
290 | memset(data, 0xdb, data_len); | ||
291 | data[0] = 0x01; | ||
292 | data[data_len - 1] = 0xff; | ||
293 | |||
294 | if ((bio = BIO_new_mem_buf(data, data_len)) == NULL) { | ||
295 | fprintf(stderr, "FAIL: BIO_new_mem_buf failed\n"); | ||
296 | goto failure; | ||
297 | } | ||
298 | if ((ret = BIO_read(bio, buf, 1)) != 1) { | ||
299 | fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret, | ||
300 | sizeof(buf)); | ||
301 | goto failure; | ||
302 | } | ||
303 | if (buf[0] != 0x01) { | ||
304 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01); | ||
305 | goto failure; | ||
306 | } | ||
307 | if ((ret = BIO_read(bio, buf, sizeof(buf))) != sizeof(buf)) { | ||
308 | fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret, | ||
309 | sizeof(buf)); | ||
310 | goto failure; | ||
311 | } | ||
312 | if (buf[0] != 0xdb) { | ||
313 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0xdb); | ||
314 | goto failure; | ||
315 | } | ||
316 | if ((ret = BIO_write(bio, buf, 1)) != -1) { | ||
317 | fprintf(stderr, "FAIL: BIO_write() = %d, want -1\n", ret); | ||
318 | goto failure; | ||
319 | } | ||
320 | if (BIO_eof(bio)) { | ||
321 | fprintf(stderr, "FAIL: BIO is EOF\n"); | ||
322 | goto failure; | ||
323 | } | ||
324 | if (BIO_ctrl_pending(bio) != 2047) { | ||
325 | fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 2047\n", | ||
326 | BIO_ctrl_pending(bio)); | ||
327 | goto failure; | ||
328 | } | ||
329 | if ((ret = BIO_read(bio, buf, sizeof(buf))) != 2047) { | ||
330 | fprintf(stderr, "FAIL: BIO_read() = %d, want 2047\n", ret); | ||
331 | goto failure; | ||
332 | } | ||
333 | if (buf[2045] != 0xdb) { | ||
334 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2045], 0xdb); | ||
335 | goto failure; | ||
336 | } | ||
337 | if (buf[2046] != 0xff) { | ||
338 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2046], 0xff); | ||
339 | goto failure; | ||
340 | } | ||
341 | if (!BIO_eof(bio)) { | ||
342 | fprintf(stderr, "FAIL: BIO is not EOF\n"); | ||
343 | goto failure; | ||
344 | } | ||
345 | if (BIO_ctrl_pending(bio) != 0) { | ||
346 | fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 0\n", | ||
347 | BIO_ctrl_pending(bio)); | ||
348 | goto failure; | ||
349 | } | ||
350 | |||
351 | if (!BIO_reset(bio)) { | ||
352 | fprintf(stderr, "FAIL: failed to reset bio\n"); | ||
353 | goto failure; | ||
354 | } | ||
355 | if (BIO_eof(bio)) { | ||
356 | fprintf(stderr, "FAIL: BIO is EOF\n"); | ||
357 | goto failure; | ||
358 | } | ||
359 | if (BIO_ctrl_pending(bio) != 4096) { | ||
360 | fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 4096\n", | ||
361 | BIO_ctrl_pending(bio)); | ||
362 | goto failure; | ||
363 | } | ||
364 | if ((ret = BIO_read(bio, buf, 2)) != 2) { | ||
365 | fprintf(stderr, "FAIL: BIO_read() = %d, want 2\n", ret); | ||
366 | goto failure; | ||
367 | } | ||
368 | if (buf[0] != 0x01) { | ||
369 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01); | ||
370 | goto failure; | ||
371 | } | ||
372 | if (buf[1] != 0xdb) { | ||
373 | fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[1], 0xdb); | ||
374 | goto failure; | ||
375 | } | ||
376 | |||
377 | failed = 0; | ||
378 | |||
379 | failure: | ||
380 | BIO_free(bio); | ||
381 | free(data); | ||
382 | |||
383 | return failed; | ||
384 | } | ||
385 | |||
386 | static int | ||
387 | do_bio_mem_tests(void) | ||
388 | { | ||
389 | int failed = 0; | ||
390 | |||
391 | failed |= bio_mem_test(); | ||
392 | failed |= bio_mem_readonly_test(); | ||
393 | |||
394 | return failed; | ||
395 | } | ||
396 | |||
146 | int | 397 | int |
147 | main(int argc, char **argv) | 398 | main(int argc, char **argv) |
148 | { | 399 | { |
@@ -150,6 +401,7 @@ main(int argc, char **argv) | |||
150 | 401 | ||
151 | ret |= do_bio_get_host_ip_tests(); | 402 | ret |= do_bio_get_host_ip_tests(); |
152 | ret |= do_bio_get_port_tests(); | 403 | ret |= do_bio_get_port_tests(); |
404 | ret |= do_bio_mem_tests(); | ||
153 | 405 | ||
154 | return (ret); | 406 | return (ret); |
155 | } | 407 | } |