summaryrefslogtreecommitdiff
path: root/test/bio_comp_test.c
blob: 75ae46adb6fba30680548c652010fd2e30c1adc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * 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 */
static unsigned char *original = NULL;
static unsigned char *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(meth))
        goto err;
    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, 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_info("zeros of size %d\n", size);
        memset(original, 0, BUFFER_SIZE);
        break;
    case 1:
        TEST_info("ones of size %d\n", size);
        memset(original, 1, BUFFER_SIZE);
        break;
    case 2:
        TEST_info("sequential of size %d\n", size);
        for (i = 0; i < BUFFER_SIZE; i++)
            original[i] = i & 0xFF;
        break;
    case 3:
        TEST_info("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_ZSTD
static int test_zstd(int n)
{
    return do_bio_comp(BIO_f_zstd(), n);
}
#endif
#ifndef OPENSSL_NO_BROTLI
static int test_brotli(int n)
{
    return do_bio_comp(BIO_f_brotli(), n);
}
#endif
#ifndef OPENSSL_NO_ZLIB
static int test_zlib(int n)
{
    return do_bio_comp(BIO_f_zlib(), n);
}
#endif

int setup_tests(void)
{
#ifndef OPENSSL_NO_ZLIB
    ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4);
#endif
#ifndef OPENSSL_NO_BROTLI
    ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4);
#endif
#ifndef OPENSSL_NO_ZSTD
    ADD_ALL_TESTS(test_zstd, NUM_SIZES * 4);
#endif
    return 1;
}