| 1 |
/* test_inflate_sync.cc - Test inflateSync using full flush deflate and corrupted data */ |
| 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 |
TEST(inflate, sync) { |
| 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 |
|
| 27 |
/* build compressed stream with full flush */ |
| 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 |
c_stream.avail_in = 3; |
| 34 |
c_stream.avail_out = (uint32_t)compr_len; |
| 35 |
|
| 36 |
err = PREFIX(deflate)(&c_stream, Z_FULL_FLUSH); |
| 37 |
EXPECT_EQ(err, Z_OK); |
| 38 |
|
| 39 |
/* force an error in first compressed block */ |
| 40 |
compr[3]++; |
| 41 |
c_stream.avail_in = hello_len-3; |
| 42 |
|
| 43 |
err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 44 |
EXPECT_EQ(err, Z_STREAM_END); |
| 45 |
|
| 46 |
err = PREFIX(deflateEnd)(&c_stream); |
| 47 |
EXPECT_EQ(err, Z_OK); |
| 48 |
|
| 49 |
compr_len = (z_size_t)c_stream.total_out; |
| 50 |
|
| 51 |
memset(&d_stream, 0, sizeof(d_stream)); |
| 52 |
/* just read the zlib header */ |
| 53 |
d_stream.next_in = compr; |
| 54 |
d_stream.avail_in = 2; |
| 55 |
|
| 56 |
err = PREFIX(inflateInit)(&d_stream); |
| 57 |
EXPECT_EQ(err, Z_OK); |
| 58 |
|
| 59 |
d_stream.next_out = uncompr; |
| 60 |
d_stream.avail_out = (uint32_t)uncompr_len; |
| 61 |
|
| 62 |
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
| 63 |
EXPECT_EQ(err, Z_OK); |
| 64 |
|
| 65 |
/* read all compressed data, but skip damaged part */ |
| 66 |
d_stream.avail_in = (uint32_t)compr_len-2; |
| 67 |
err = PREFIX(inflateSync)(&d_stream); |
| 68 |
EXPECT_EQ(err, Z_OK); |
| 69 |
|
| 70 |
err = PREFIX(inflate)(&d_stream, Z_FINISH); |
| 71 |
EXPECT_EQ(err, Z_STREAM_END); |
| 72 |
|
| 73 |
err = PREFIX(inflateEnd)(&d_stream); |
| 74 |
EXPECT_EQ(err, Z_OK); |
| 75 |
} |