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