]>
Commit | Line | Data |
---|---|---|
d9b04d78 | 1 | #include "clar_libgit2.h" |
d9b04d78 RB |
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, | |
22a2d3d5 | 11 | const char *msg, const char *file, const char *func, int line) |
d9b04d78 RB |
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( | |
22a2d3d5 | 28 | file, func, line, msg, 1, |
d9b04d78 RB |
29 | "%d", (int)stream.total_out, (int)e_len); |
30 | clar__assert_equal( | |
22a2d3d5 | 31 | file, func, line, "Buffer len was not exact match", 1, |
d9b04d78 RB |
32 | "%d", (int)stream.avail_out, (int)INFLATE_EXTRA); |
33 | ||
34 | clar__assert( | |
35 | memcmp(expanded, expected, e_len) == 0, | |
22a2d3d5 | 36 | file, func, line, "uncompressed data did not match", NULL, 1); |
d9b04d78 RB |
37 | |
38 | git__free(expanded); | |
39 | } | |
40 | ||
41 | #define assert_zlib_equal(E,EL,C,CL) \ | |
22a2d3d5 | 42 | assert_zlib_equal_(E, EL, C, CL, #EL " != " #CL, __FILE__, __func__, (int)__LINE__) |
d9b04d78 RB |
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 | ||
b88f1713 | 50 | cl_git_pass(git_zstream_init(&z, GIT_ZSTREAM_DEFLATE)); |
d9b04d78 RB |
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 | ||
b8dc2fdb ET |
60 | void test_core_zstream__fails_on_trailing_garbage(void) |
61 | { | |
e579e0f7 | 62 | git_str deflated = GIT_STR_INIT, inflated = GIT_STR_INIT; |
ac3d33df | 63 | char i = 0; |
b8dc2fdb ET |
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++) { | |
e579e0f7 | 70 | git_str_putc(&deflated, i); |
b8dc2fdb ET |
71 | } |
72 | ||
73 | cl_git_fail(git_zstream_inflatebuf(&inflated, deflated.ptr, deflated.size)); | |
74 | ||
e579e0f7 MB |
75 | git_str_dispose(&deflated); |
76 | git_str_dispose(&inflated); | |
b8dc2fdb ET |
77 | } |
78 | ||
d9b04d78 RB |
79 | void test_core_zstream__buffer(void) |
80 | { | |
e579e0f7 | 81 | git_str out = GIT_STR_INIT; |
d9b04d78 RB |
82 | cl_git_pass(git_zstream_deflatebuf(&out, data, strlen(data) + 1)); |
83 | assert_zlib_equal(data, strlen(data) + 1, out.ptr, out.size); | |
e579e0f7 | 84 | git_str_dispose(&out); |
d9b04d78 RB |
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 | ||
e579e0f7 | 89 | static void compress_and_decompress_input_various_ways(git_str *input) |
d9b04d78 | 90 | { |
e579e0f7 MB |
91 | git_str out1 = GIT_STR_INIT, out2 = GIT_STR_INIT; |
92 | git_str inflated = GIT_STR_INIT; | |
8606f33b RB |
93 | size_t i, fixed_size = max(input->size / 2, 256); |
94 | char *fixed = git__malloc(fixed_size); | |
95 | cl_assert(fixed); | |
d9b04d78 | 96 | |
8606f33b | 97 | /* compress with deflatebuf */ |
d9b04d78 | 98 | |
8606f33b RB |
99 | cl_git_pass(git_zstream_deflatebuf(&out1, input->ptr, input->size)); |
100 | assert_zlib_equal(input->ptr, input->size, out1.ptr, out1.size); | |
d9b04d78 | 101 | |
8606f33b | 102 | /* compress with various fixed size buffer (accumulating the output) */ |
d9b04d78 | 103 | |
8606f33b RB |
104 | for (i = 0; i < 3; ++i) { |
105 | git_zstream zs = GIT_ZSTREAM_INIT; | |
106 | size_t use_fixed_size; | |
d9b04d78 | 107 | |
8606f33b RB |
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); | |
d9b04d78 | 114 | |
b88f1713 | 115 | cl_git_pass(git_zstream_init(&zs, GIT_ZSTREAM_DEFLATE)); |
8606f33b | 116 | cl_git_pass(git_zstream_set_input(&zs, input->ptr, input->size)); |
d9b04d78 | 117 | |
8606f33b RB |
118 | while (!git_zstream_done(&zs)) { |
119 | size_t written = use_fixed_size; | |
120 | cl_git_pass(git_zstream_get_output(fixed, &written, &zs)); | |
e579e0f7 | 121 | cl_git_pass(git_str_put(&out2, fixed, written)); |
8606f33b RB |
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 | ||
e579e0f7 | 131 | git_str_dispose(&out2); |
8606f33b RB |
132 | } |
133 | ||
b88f1713 ET |
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 | ||
e579e0f7 MB |
138 | git_str_dispose(&out1); |
139 | git_str_dispose(&inflated); | |
8606f33b RB |
140 | git__free(fixed); |
141 | } | |
142 | ||
143 | void test_core_zstream__big_data(void) | |
144 | { | |
e579e0f7 | 145 | git_str in = GIT_STR_INIT; |
8606f33b RB |
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 */ | |
e579e0f7 | 151 | git_str_clear(&in); |
8606f33b RB |
152 | while (in.size < target) |
153 | cl_git_pass( | |
e579e0f7 | 154 | git_str_put(&in, BIG_STRING_PART, strlen(BIG_STRING_PART))); |
8606f33b | 155 | |
b88f1713 | 156 | compress_and_decompress_input_various_ways(&in); |
8606f33b RB |
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 | ||
b88f1713 | 163 | compress_and_decompress_input_various_ways(&in); |
8606f33b | 164 | } |
d9b04d78 | 165 | |
e579e0f7 | 166 | git_str_dispose(&in); |
d9b04d78 | 167 | } |