]> git.proxmox.com Git - libgit2.git/blob - tests/core/zstream.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / zstream.c
1 #include "clar_libgit2.h"
2 #include "zstream.h"
3
4 static const char *data = "This is a test test test of This is a test";
5
6 #define INFLATE_EXTRA 2
7
8 static void assert_zlib_equal_(
9 const void *expected, size_t e_len,
10 const void *compressed, size_t c_len,
11 const char *msg, const char *file, const char *func, int line)
12 {
13 z_stream stream;
14 char *expanded = git__calloc(1, e_len + INFLATE_EXTRA);
15 cl_assert(expanded);
16
17 memset(&stream, 0, sizeof(stream));
18 stream.next_out = (Bytef *)expanded;
19 stream.avail_out = (uInt)(e_len + INFLATE_EXTRA);
20 stream.next_in = (Bytef *)compressed;
21 stream.avail_in = (uInt)c_len;
22
23 cl_assert(inflateInit(&stream) == Z_OK);
24 cl_assert(inflate(&stream, Z_FINISH));
25 inflateEnd(&stream);
26
27 clar__assert_equal(
28 file, func, line, msg, 1,
29 "%d", (int)stream.total_out, (int)e_len);
30 clar__assert_equal(
31 file, func, line, "Buffer len was not exact match", 1,
32 "%d", (int)stream.avail_out, (int)INFLATE_EXTRA);
33
34 clar__assert(
35 memcmp(expanded, expected, e_len) == 0,
36 file, func, line, "uncompressed data did not match", NULL, 1);
37
38 git__free(expanded);
39 }
40
41 #define assert_zlib_equal(E,EL,C,CL) \
42 assert_zlib_equal_(E, EL, C, CL, #EL " != " #CL, __FILE__, __func__, (int)__LINE__)
43
44 void test_core_zstream__basic(void)
45 {
46 git_zstream z = GIT_ZSTREAM_INIT;
47 char out[128];
48 size_t outlen = sizeof(out);
49
50 cl_git_pass(git_zstream_init(&z, GIT_ZSTREAM_DEFLATE));
51 cl_git_pass(git_zstream_set_input(&z, data, strlen(data) + 1));
52 cl_git_pass(git_zstream_get_output(out, &outlen, &z));
53 cl_assert(git_zstream_done(&z));
54 cl_assert(outlen > 0);
55 git_zstream_free(&z);
56
57 assert_zlib_equal(data, strlen(data) + 1, out, outlen);
58 }
59
60 void test_core_zstream__fails_on_trailing_garbage(void)
61 {
62 git_str deflated = GIT_STR_INIT, inflated = GIT_STR_INIT;
63 char i = 0;
64
65 /* compress a simple string */
66 git_zstream_deflatebuf(&deflated, "foobar!!", 8);
67
68 /* append some garbage */
69 for (i = 0; i < 10; i++) {
70 git_str_putc(&deflated, i);
71 }
72
73 cl_git_fail(git_zstream_inflatebuf(&inflated, deflated.ptr, deflated.size));
74
75 git_str_dispose(&deflated);
76 git_str_dispose(&inflated);
77 }
78
79 void test_core_zstream__buffer(void)
80 {
81 git_str out = GIT_STR_INIT;
82 cl_git_pass(git_zstream_deflatebuf(&out, data, strlen(data) + 1));
83 assert_zlib_equal(data, strlen(data) + 1, out.ptr, out.size);
84 git_str_dispose(&out);
85 }
86
87 #define BIG_STRING_PART "Big Data IS Big - Long Data IS Long - We need a buffer larger than 1024 x 1024 to make sure we trigger chunked compression - Big Big Data IS Bigger than Big - Long Long Data IS Longer than Long"
88
89 static void compress_and_decompress_input_various_ways(git_str *input)
90 {
91 git_str out1 = GIT_STR_INIT, out2 = GIT_STR_INIT;
92 git_str inflated = GIT_STR_INIT;
93 size_t i, fixed_size = max(input->size / 2, 256);
94 char *fixed = git__malloc(fixed_size);
95 cl_assert(fixed);
96
97 /* compress with deflatebuf */
98
99 cl_git_pass(git_zstream_deflatebuf(&out1, input->ptr, input->size));
100 assert_zlib_equal(input->ptr, input->size, out1.ptr, out1.size);
101
102 /* compress with various fixed size buffer (accumulating the output) */
103
104 for (i = 0; i < 3; ++i) {
105 git_zstream zs = GIT_ZSTREAM_INIT;
106 size_t use_fixed_size;
107
108 switch (i) {
109 case 0: use_fixed_size = 256; break;
110 case 1: use_fixed_size = fixed_size / 2; break;
111 case 2: use_fixed_size = fixed_size; break;
112 }
113 cl_assert(use_fixed_size <= fixed_size);
114
115 cl_git_pass(git_zstream_init(&zs, GIT_ZSTREAM_DEFLATE));
116 cl_git_pass(git_zstream_set_input(&zs, input->ptr, input->size));
117
118 while (!git_zstream_done(&zs)) {
119 size_t written = use_fixed_size;
120 cl_git_pass(git_zstream_get_output(fixed, &written, &zs));
121 cl_git_pass(git_str_put(&out2, fixed, written));
122 }
123
124 git_zstream_free(&zs);
125 assert_zlib_equal(input->ptr, input->size, out2.ptr, out2.size);
126
127 /* did both approaches give the same data? */
128 cl_assert_equal_sz(out1.size, out2.size);
129 cl_assert(!memcmp(out1.ptr, out2.ptr, out1.size));
130
131 git_str_dispose(&out2);
132 }
133
134 cl_git_pass(git_zstream_inflatebuf(&inflated, out1.ptr, out1.size));
135 cl_assert_equal_i(input->size, inflated.size);
136 cl_assert(memcmp(input->ptr, inflated.ptr, inflated.size) == 0);
137
138 git_str_dispose(&out1);
139 git_str_dispose(&inflated);
140 git__free(fixed);
141 }
142
143 void test_core_zstream__big_data(void)
144 {
145 git_str in = GIT_STR_INIT;
146 size_t scan, target;
147
148 for (target = 1024; target <= 1024 * 1024 * 4; target *= 8) {
149
150 /* make a big string that's easy to compress */
151 git_str_clear(&in);
152 while (in.size < target)
153 cl_git_pass(
154 git_str_put(&in, BIG_STRING_PART, strlen(BIG_STRING_PART)));
155
156 compress_and_decompress_input_various_ways(&in);
157
158 /* make a big string that's hard to compress */
159 srand(0xabad1dea);
160 for (scan = 0; scan < in.size; ++scan)
161 in.ptr[scan] = (char)rand();
162
163 compress_and_decompress_input_various_ways(&in);
164 }
165
166 git_str_dispose(&in);
167 }