• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* test_deflate_prime.cc - Test deflatePrime() wrapping gzip around deflate stream */
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 
TEST(deflate,prime)19 TEST(deflate, prime) {
20     PREFIX3(stream) c_stream, d_stream;
21     uint8_t compr[128], uncompr[128];
22     z_size_t compr_len = sizeof(compr), uncompr_len = sizeof(uncompr);
23     uint32_t crc = 0;
24     int err;
25 
26     memset(&c_stream, 0, sizeof(c_stream));
27     memset(&d_stream, 0, sizeof(d_stream));
28 
29     /* Raw deflate windowBits is -15 */
30     err = PREFIX(deflateInit2)(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
31     EXPECT_EQ(err, Z_OK);
32 
33     /* Gzip magic number */
34     err = PREFIX(deflatePrime)(&c_stream, 16, 0x8b1f);
35     EXPECT_EQ(err, Z_OK);
36     /* Gzip compression method (deflate) */
37     err = PREFIX(deflatePrime)(&c_stream, 8, 0x08);
38     EXPECT_EQ(err, Z_OK);
39     /* Gzip flags (one byte, using two odd bit calls) */
40     err = PREFIX(deflatePrime)(&c_stream, 3, 0x0);
41     EXPECT_EQ(err, Z_OK);
42     err = PREFIX(deflatePrime)(&c_stream, 5, 0x0);
43     EXPECT_EQ(err, Z_OK);
44     /* Gzip modified time */
45     err = PREFIX(deflatePrime)(&c_stream, 32, 0x0);
46     EXPECT_EQ(err, Z_OK);
47     /* Gzip extra flags */
48     err = PREFIX(deflatePrime)(&c_stream, 8, 0x0);
49     EXPECT_EQ(err, Z_OK);
50     /* Gzip operating system */
51     err = PREFIX(deflatePrime)(&c_stream, 8, 255);
52     EXPECT_EQ(err, Z_OK);
53 
54     c_stream.next_in = (z_const unsigned char *)hello;
55     c_stream.avail_in = (uint32_t)hello_len;
56     c_stream.next_out = compr;
57     c_stream.avail_out = (uint32_t)compr_len;
58 
59     err = PREFIX(deflate)(&c_stream, Z_FINISH);
60     EXPECT_EQ(err, Z_STREAM_END);
61 
62     /* Gzip uncompressed data crc32 */
63     crc = PREFIX(crc32)(0, (const uint8_t *)hello, (uint32_t)hello_len);
64     err = PREFIX(deflatePrime)(&c_stream, 32, crc);
65     EXPECT_EQ(err, Z_OK);
66     /* Gzip uncompressed data length */
67     err = PREFIX(deflatePrime)(&c_stream, 32, (uint32_t)hello_len);
68     EXPECT_EQ(err, Z_OK);
69 
70     err = PREFIX(deflateEnd)(&c_stream);
71     EXPECT_EQ(err, Z_OK);
72 
73     d_stream.next_in  = compr;
74     d_stream.avail_in = (uint32_t)c_stream.total_out;
75     d_stream.next_out = uncompr;
76     d_stream.avail_out = (uint32_t)uncompr_len;
77     d_stream.total_in = 0;
78     d_stream.total_out = 0;
79 
80     /* Inflate with gzip header */
81     err = PREFIX(inflateInit2)(&d_stream, MAX_WBITS + 32);
82     EXPECT_EQ(err, Z_OK);
83 
84     err = PREFIX(inflate)(&d_stream, Z_FINISH);
85     EXPECT_EQ(err, Z_BUF_ERROR);
86 
87     err = PREFIX(inflateEnd)(&d_stream);
88     EXPECT_EQ(err, Z_OK);
89 
90     EXPECT_STREQ((char *)uncompr, hello);
91 }
92