• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* test_small_buffers.cc - Test deflate() and inflate() 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,small_buffers)19 TEST(deflate, small_buffers) {
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     int err;
24 
25     memset(&c_stream, 0, sizeof(c_stream));
26     memset(&d_stream, 0, sizeof(d_stream));
27 
28     err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION);
29     EXPECT_EQ(err, Z_OK);
30 
31     c_stream.next_in  = (z_const unsigned char *)hello;
32     c_stream.next_out = compr;
33 
34     while (c_stream.total_in != hello_len && c_stream.total_out < compr_len) {
35         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
36         err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
37         EXPECT_EQ(err, Z_OK);
38     }
39     /* Finish the stream, still forcing small buffers */
40     for (;;) {
41         c_stream.avail_out = 1;
42         err = PREFIX(deflate)(&c_stream, Z_FINISH);
43         if (err == Z_STREAM_END) break;
44         EXPECT_EQ(err, Z_OK);
45     }
46 
47     err = PREFIX(deflateEnd)(&c_stream);
48     EXPECT_EQ(err, Z_OK);
49 
50     strcpy((char*)uncompr, "garbage");
51 
52     d_stream.next_in  = compr;
53     d_stream.next_out = uncompr;
54 
55     err = PREFIX(inflateInit)(&d_stream);
56     EXPECT_EQ(err, Z_OK);
57 
58     while (d_stream.total_out < uncompr_len && d_stream.total_in < compr_len) {
59         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
60         err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
61         if (err == Z_STREAM_END) break;
62         EXPECT_EQ(err, Z_OK);
63     }
64 
65     err = PREFIX(inflateEnd)(&d_stream);
66     EXPECT_EQ(err, Z_OK);
67 
68     EXPECT_STREQ((char*)uncompr, hello);
69 }
70