1 /* test_deflate_header.cc - Test deflateSetHeader() with small buffers */
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,header)19 TEST(deflate, header) {
20     PREFIX3(stream) c_stream;
21     PREFIX(gz_header) *head;
22     uint8_t compr[128];
23     z_size_t compr_len = sizeof(compr);
24     int err;
25 
26     head = (PREFIX(gz_header) *)calloc(1, sizeof(PREFIX(gz_header)));
27     ASSERT_TRUE(head != NULL);
28 
29     memset(&c_stream, 0, sizeof(c_stream));
30 
31     /* gzip */
32     err = PREFIX(deflateInit2)(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
33     EXPECT_EQ(err, Z_OK);
34 
35     head->text = 1;
36     head->comment = (uint8_t *)"comment";
37     head->name = (uint8_t *)"name";
38     head->hcrc = 1;
39     head->extra = (uint8_t *)"extra";
40     head->extra_len = (uint32_t)strlen((const char *)head->extra);
41 
42     err = PREFIX(deflateSetHeader)(&c_stream, head);
43     EXPECT_EQ(err, Z_OK);
44 
45     PREFIX(deflateBound)(&c_stream, (unsigned long)compr_len);
46 
47     c_stream.next_in  = (unsigned char *)hello;
48     c_stream.next_out = compr;
49 
50     while (c_stream.total_in != hello_len && c_stream.total_out < compr_len) {
51         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
52         err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
53         EXPECT_EQ(err, Z_OK);
54     }
55 
56     /* Finish the stream, still forcing small buffers: */
57     for (;;) {
58         c_stream.avail_out = 1;
59         err = PREFIX(deflate)(&c_stream, Z_FINISH);
60         if (err == Z_STREAM_END) break;
61         EXPECT_EQ(err, Z_OK);
62     }
63 
64     err = PREFIX(deflateEnd)(&c_stream);
65     EXPECT_EQ(err, Z_OK);
66 
67     free(head);
68 }
69