2 * Copyright 2022 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/evp.h>
12 #include <openssl/bio.h>
13 #include <openssl/rand.h>
14 #include <openssl/comp.h>
17 #include "testutil/output.h"
18 #include "testutil/tu_local.h"
23 #define BUFFER_SIZE 32 * 1024
25 static int sizes[NUM_SIZES] = { 64, 512, 2048, 16 * 1024 };
27 /* using global buffers */
28 static unsigned char *original = NULL;
29 static unsigned char *result = NULL;
33 * the write operation compresses
34 * the read operation decompresses
37 static int do_bio_comp_test(const BIO_METHOD *meth, size_t size)
49 if (!TEST_ptr(bcomp = BIO_new(meth)))
51 if (!TEST_ptr(bmem = BIO_new(BIO_s_mem())))
53 BIO_push(bcomp, bmem);
54 osize = BIO_write(bcomp, original, size);
55 if (!TEST_int_eq(osize, size)
56 || !TEST_true(BIO_flush(bcomp)))
62 if (!TEST_ptr(bexp = BIO_new(meth)))
65 rsize = BIO_read(bexp, result, size);
67 if (!TEST_int_eq(size, rsize)
68 || !TEST_mem_eq(original, osize, result, rsize))
79 static int do_bio_comp(const BIO_METHOD *meth, int n)
83 int size = sizes[n % 4];
86 if (!TEST_ptr(original = OPENSSL_malloc(BUFFER_SIZE))
87 || !TEST_ptr(result = OPENSSL_malloc(BUFFER_SIZE)))
92 TEST_info("zeros of size %d\n", size);
93 memset(original, 0, BUFFER_SIZE);
96 TEST_info("ones of size %d\n", size);
97 memset(original, 1, BUFFER_SIZE);
100 TEST_info("sequential of size %d\n", size);
101 for (i = 0; i < BUFFER_SIZE; i++)
102 original[i] = i & 0xFF;
105 TEST_info("random of size %d\n", size);
106 if (!TEST_int_gt(RAND_bytes(original, BUFFER_SIZE), 0))
113 if (!TEST_true(do_bio_comp_test(meth, size)))
117 OPENSSL_free(original);
118 OPENSSL_free(result);
122 #ifndef OPENSSL_NO_ZSTD
123 static int test_zstd(int n)
125 return do_bio_comp(BIO_f_zstd(), n);
128 #ifndef OPENSSL_NO_BROTLI
129 static int test_brotli(int n)
131 return do_bio_comp(BIO_f_brotli(), n);
134 #ifndef OPENSSL_NO_ZLIB
135 static int test_zlib(int n)
137 return do_bio_comp(BIO_f_zlib(), n);
141 int setup_tests(void)
143 #ifndef OPENSSL_NO_ZLIB
144 ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4);
146 #ifndef OPENSSL_NO_BROTLI
147 ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4);
149 #ifndef OPENSSL_NO_ZSTD
150 ADD_ALL_TESTS(test_zstd, NUM_SIZES * 4);