]> git.proxmox.com Git - libgit2.git/blob - tests/core/zstream.c
Reorganize zstream API and fix wrap problems
[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));
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__buffer(void)
62 {
63 git_buf out = GIT_BUF_INIT;
64 cl_git_pass(git_zstream_deflatebuf(&out, data, strlen(data) + 1));
65 assert_zlib_equal(data, strlen(data) + 1, out.ptr, out.size);
66 git_buf_free(&out);
67 }
68
69 #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"
70
71 void test_core_zstream__big_data(void)
72 {
73 git_buf in = GIT_BUF_INIT;
74 git_buf out = GIT_BUF_INIT;
75 size_t scan;
76
77 /* make a big string that's easy to compress */
78 while (in.size < 1024 * 1024)
79 cl_git_pass(git_buf_put(&in, BIG_STRING_PART, strlen(BIG_STRING_PART)));
80
81 cl_git_pass(git_zstream_deflatebuf(&out, in.ptr, in.size));
82 assert_zlib_equal(in.ptr, in.size, out.ptr, out.size);
83
84 git_buf_free(&out);
85
86 /* make a big string that's hard to compress */
87
88 srand(0xabad1dea);
89 for (scan = 0; scan < in.size; ++scan)
90 in.ptr[scan] = (char)rand();
91
92 cl_git_pass(git_zstream_deflatebuf(&out, in.ptr, in.size));
93 assert_zlib_equal(in.ptr, in.size, out.ptr, out.size);
94
95 git_buf_free(&out);
96
97 git_buf_free(&in);
98 }