]>
git.proxmox.com Git - libgit2.git/blob - tests/core/zstream.c
1 #include "clar_libgit2.h"
4 static const char * data
= "This is a test test test of This is a test" ;
6 #define INFLATE_EXTRA 2
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
)
14 char * expanded
= git__calloc ( 1 , e_len
+ INFLATE_EXTRA
);
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
;
23 cl_assert ( inflateInit (& stream
) == Z_OK
);
24 cl_assert ( inflate (& stream
, Z_FINISH
));
28 file
, func
, line
, msg
, 1 ,
29 "%d" , ( int ) stream
. total_out
, ( int ) e_len
);
31 file
, func
, line
, "Buffer len was not exact match" , 1 ,
32 "%d" , ( int ) stream
. avail_out
, ( int ) INFLATE_EXTRA
);
35 memcmp ( expanded
, expected
, e_len
) == 0 ,
36 file
, func
, line
, "uncompressed data did not match" , NULL
, 1 );
41 #define assert_zlib_equal(E,EL,C,CL) \
42 assert_zlib_equal_(E, EL, C, CL, #EL " != " #CL, __FILE__, __func__, (int)__LINE__)
44 void test_core_zstream__basic ( void )
46 git_zstream z
= GIT_ZSTREAM_INIT
;
48 size_t outlen
= sizeof ( out
);
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 );
57 assert_zlib_equal ( data
, strlen ( data
) + 1 , out
, outlen
);
60 void test_core_zstream__fails_on_trailing_garbage ( void )
62 git_str deflated
= GIT_STR_INIT
, inflated
= GIT_STR_INIT
;
65 /* compress a simple string */
66 git_zstream_deflatebuf (& deflated
, "foobar!!" , 8 );
68 /* append some garbage */
69 for ( i
= 0 ; i
< 10 ; i
++) {
70 git_str_putc (& deflated
, i
);
73 cl_git_fail ( git_zstream_inflatebuf (& inflated
, deflated
. ptr
, deflated
. size
));
75 git_str_dispose (& deflated
);
76 git_str_dispose (& inflated
);
79 void test_core_zstream__buffer ( void )
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
);
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"
89 static void compress_and_decompress_input_various_ways ( git_str
* input
)
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
);
97 /* compress with deflatebuf */
99 cl_git_pass ( git_zstream_deflatebuf (& out1
, input
-> ptr
, input
-> size
));
100 assert_zlib_equal ( input
-> ptr
, input
-> size
, out1
. ptr
, out1
. size
);
102 /* compress with various fixed size buffer (accumulating the output) */
104 for ( i
= 0 ; i
< 3 ; ++ i
) {
105 git_zstream zs
= GIT_ZSTREAM_INIT
;
106 size_t use_fixed_size
;
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 ;
113 cl_assert ( use_fixed_size
<= fixed_size
);
115 cl_git_pass ( git_zstream_init (& zs
, GIT_ZSTREAM_DEFLATE
));
116 cl_git_pass ( git_zstream_set_input (& zs
, input
-> ptr
, input
-> size
));
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
));
124 git_zstream_free (& zs
);
125 assert_zlib_equal ( input
-> ptr
, input
-> size
, out2
. ptr
, out2
. size
);
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
));
131 git_str_dispose (& out2
);
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 );
138 git_str_dispose (& out1
);
139 git_str_dispose (& inflated
);
143 void test_core_zstream__big_data ( void )
145 git_str in
= GIT_STR_INIT
;
148 for ( target
= 1024 ; target
<= 1024 * 1024 * 4 ; target
*= 8 ) {
150 /* make a big string that's easy to compress */
152 while ( in
. size
< target
)
154 git_str_put (& in
, BIG_STRING_PART
, strlen ( BIG_STRING_PART
)));
156 compress_and_decompress_input_various_ways (& in
);
158 /* make a big string that's hard to compress */
160 for ( scan
= 0 ; scan
< in
. size
; ++ scan
)
161 in
. ptr
[ scan
] = ( char ) rand ();
163 compress_and_decompress_input_various_ways (& in
);
166 git_str_dispose (& in
);