| 1 |
/* test_dict.cc - Test deflate() and inflate() with preset dictionary */ |
| 2 |
|
| 3 |
#include "zbuild.h" |
| 4 |
#ifdef ZLIB_COMPAT |
| 5 |
# include "zlib.h" |
| 6 |
#else |
| 7 |
# include "zlib-ng.h" |
| 8 |
#endif |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "test_shared.h" |
| 16 |
|
| 17 |
#include <gtest/gtest.h> |
| 18 |
|
| 19 |
/* Maximum dictionary size, according to inflateGetDictionary() description. */ |
| 20 |
#define MAX_DICTIONARY_SIZE 32768 |
| 21 |
|
| 22 |
static const char dictionary[] = "hello"; |
| 23 |
|
| 24 |
TEST(dictionary, basic) { |
| 25 |
PREFIX3(stream) c_stream, d_stream; |
| 26 |
uint8_t compr[128], uncompr[128]; |
| 27 |
z_size_t compr_len = sizeof(compr), uncompr_len = sizeof(uncompr); |
| 28 |
uint32_t dict_adler = 0; |
| 29 |
uint8_t check_dict[MAX_DICTIONARY_SIZE]; |
| 30 |
uint32_t check_dict_len = 0; |
| 31 |
int err; |
| 32 |
|
| 33 |
memset(&c_stream, 0, sizeof(c_stream)); |
| 34 |
memset(&d_stream, 0, sizeof(d_stream)); |
| 35 |
|
| 36 |
err = PREFIX(deflateInit)(&c_stream, Z_BEST_COMPRESSION); |
| 37 |
EXPECT_EQ(err, Z_OK); |
| 38 |
|
| 39 |
err = PREFIX(deflateSetDictionary)(&c_stream, |
| 40 |
(const unsigned char *)dictionary, (int)sizeof(dictionary)); |
| 41 |
EXPECT_EQ(err, Z_OK); |
| 42 |
|
| 43 |
dict_adler = c_stream.adler; |
| 44 |
c_stream.next_out = compr; |
| 45 |
c_stream.avail_out = (uint32_t)compr_len; |
| 46 |
|
| 47 |
c_stream.next_in = (z_const unsigned char *)hello; |
| 48 |
c_stream.avail_in = (uint32_t)hello_len; |
| 49 |
|
| 50 |
err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 51 |
EXPECT_EQ(err, Z_STREAM_END); |
| 52 |
|
| 53 |
err = PREFIX(deflateEnd)(&c_stream); |
| 54 |
EXPECT_EQ(err, Z_OK); |
| 55 |
|
| 56 |
strcpy((char*)uncompr, "garbage garbage garbage"); |
| 57 |
|
| 58 |
d_stream.next_in = compr; |
| 59 |
d_stream.avail_in = (unsigned int)compr_len; |
| 60 |
|
| 61 |
err = PREFIX(inflateInit)(&d_stream); |
| 62 |
EXPECT_EQ(err, Z_OK); |
| 63 |
|
| 64 |
d_stream.next_out = uncompr; |
| 65 |
d_stream.avail_out = (unsigned int)uncompr_len; |
| 66 |
|
| 67 |
for (;;) { |
| 68 |
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
| 69 |
if (err == Z_STREAM_END) |
| 70 |
break; |
| 71 |
if (err == Z_NEED_DICT) { |
| 72 |
EXPECT_EQ(d_stream.adler, dict_adler); |
| 73 |
err = PREFIX(inflateSetDictionary)(&d_stream, (const unsigned char*)dictionary, |
| 74 |
(uint32_t)sizeof(dictionary)); |
| 75 |
EXPECT_EQ(d_stream.adler, dict_adler); |
| 76 |
} |
| 77 |
EXPECT_EQ(err, Z_OK); |
| 78 |
} |
| 79 |
|
| 80 |
err = PREFIX(inflateGetDictionary)(&d_stream, NULL, &check_dict_len); |
| 81 |
EXPECT_EQ(err, Z_OK); |
| 82 |
#ifndef S390_DFLTCC_INFLATE |
| 83 |
EXPECT_GE(check_dict_len, sizeof(dictionary)); |
| 84 |
#endif |
| 85 |
|
| 86 |
err = PREFIX(inflateGetDictionary)(&d_stream, check_dict, &check_dict_len); |
| 87 |
EXPECT_EQ(err, Z_OK); |
| 88 |
#ifndef S390_DFLTCC_INFLATE |
| 89 |
EXPECT_TRUE(memcmp(dictionary, check_dict, sizeof(dictionary)) == 0); |
| 90 |
#endif |
| 91 |
|
| 92 |
err = PREFIX(inflateEnd)(&d_stream); |
| 93 |
EXPECT_EQ(err, Z_OK); |
| 94 |
|
| 95 |
EXPECT_TRUE(strncmp((char*)uncompr, hello, sizeof(hello)) == 0); |
| 96 |
} |