aboutsummaryrefslogtreecommitdiff
path: root/test/bio_comp_test.c
diff options
context:
space:
mode:
authorTodd Short <tshort@akamai.com>2021-08-09 16:56:29 -0400
committerTodd Short <todd.short@me.com>2022-10-18 09:30:18 -0400
commit12e96a23604a7aa1cd8f83486b02f1bcab6d468f (patch)
tree0b6be9589eaab31798122128c1237d40bff9bfe2 /test/bio_comp_test.c
parent846975f367f75f3503b44c12e49d980dca181647 (diff)
downloadopenssl-12e96a23604a7aa1cd8f83486b02f1bcab6d468f.zip
openssl-12e96a23604a7aa1cd8f83486b02f1bcab6d468f.tar.gz
openssl-12e96a23604a7aa1cd8f83486b02f1bcab6d468f.tar.bz2
Add brotli compression support (RFC7924)
Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> (Merged from https://github.com/openssl/openssl/pull/18186)
Diffstat (limited to 'test/bio_comp_test.c')
-rw-r--r--test/bio_comp_test.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/test/bio_comp_test.c b/test/bio_comp_test.c
new file mode 100644
index 0000000..b148d02
--- /dev/null
+++ b/test/bio_comp_test.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+#include <stdio.h>
+#include <string.h>
+#include <openssl/evp.h>
+#include <openssl/bio.h>
+#include <openssl/rand.h>
+#include <openssl/comp.h>
+
+#include "testutil.h"
+#include "testutil/output.h"
+#include "testutil/tu_local.h"
+
+#define COMPRESS 1
+#define EXPAND 0
+
+#define BUFFER_SIZE 32 * 1024
+#define NUM_SIZES 4
+static int sizes[NUM_SIZES] = { 64, 512, 2048, 16 * 1024 };
+
+/* using global buffers */
+uint8_t *original = NULL;
+uint8_t *result = NULL;
+
+/*
+ * For compression:
+ * the write operation compresses
+ * the read operation decompresses
+ */
+
+static int do_bio_comp_test(const BIO_METHOD *meth, size_t size)
+{
+ BIO *bcomp = NULL;
+ BIO *bmem = NULL;
+ BIO *bexp = NULL;
+ int osize;
+ int rsize;
+ int ret = 0;
+
+ /* Compress */
+ if (!TEST_ptr(bcomp = BIO_new(meth)))
+ goto err;
+ if (!TEST_ptr(bmem = BIO_new(BIO_s_mem())))
+ goto err;
+ BIO_push(bcomp, bmem);
+ osize = BIO_write(bcomp, original, size);
+ if (!TEST_int_eq(osize, size)
+ || !TEST_true(BIO_flush(bcomp)))
+ goto err;
+ BIO_free(bcomp);
+ bcomp = NULL;
+
+ /* decompress */
+ if (!TEST_ptr(bexp = BIO_new(meth)))
+ goto err;
+ BIO_push(bexp, bmem);
+ rsize = BIO_read(bexp, result, size);
+
+ if (!TEST_int_eq(size, osize)
+ || !TEST_int_eq(size, rsize)
+ || !TEST_mem_eq(original, osize, result, rsize))
+ goto err;
+
+ ret = 1;
+ err:
+ BIO_free(bexp);
+ BIO_free(bcomp);
+ BIO_free(bmem);
+ return ret;
+}
+
+static int do_bio_comp(const BIO_METHOD *meth, int n)
+{
+ int i;
+ int success = 0;
+ int size = sizes[n % 4];
+ int type = n / 4;
+
+ if (!TEST_ptr(original = OPENSSL_malloc(BUFFER_SIZE))
+ || !TEST_ptr(result = OPENSSL_malloc(BUFFER_SIZE)))
+ goto err;
+
+ switch (type) {
+ case 0:
+ test_printf_stdout("# zeros of size %d\n", size);
+ memset(original, 0, BUFFER_SIZE);
+ break;
+ case 1:
+ test_printf_stdout("# ones of size %d\n", size);
+ memset(original, 0, BUFFER_SIZE);
+ break;
+ case 2:
+ test_printf_stdout("# sequential of size %d\n", size);
+ for (i = 0; i < BUFFER_SIZE; i++)
+ original[i] = i & 0xFF;
+ break;
+ case 3:
+ test_printf_stdout("# random of size %d\n", size);
+ if (!TEST_int_gt(RAND_bytes(original, BUFFER_SIZE), 0))
+ goto err;
+ break;
+ default:
+ goto err;
+ }
+
+ if (!TEST_true(do_bio_comp_test(meth, size)))
+ goto err;
+ success = 1;
+ err:
+ OPENSSL_free(original);
+ OPENSSL_free(result);
+ return success;
+}
+
+#ifndef OPENSSL_NO_BROTLI
+static int test_brotli(int n)
+{
+ return do_bio_comp(BIO_f_brotli(), n);
+}
+#endif
+#ifdef ZLIB
+static int test_zlib(int n)
+{
+ return do_bio_comp(BIO_f_zlib(), n);
+}
+#endif
+
+int setup_tests(void)
+{
+#ifdef ZLIB
+ ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4);
+#endif
+#ifndef OPENSSL_NO_BROTLI
+ ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4);
+#endif
+ return 1;
+}