]>
git.proxmox.com Git - libgit2.git/blob - tests/core/zstream.c
1 #include "clar_libgit2.h"
5 static const char * data
= "This is a test test test of This is a test" ;
7 #define INFLATE_EXTRA 2
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
)
15 char * expanded
= git__calloc ( 1 , e_len
+ INFLATE_EXTRA
);
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
;
24 cl_assert ( inflateInit (& stream
) == Z_OK
);
25 cl_assert ( inflate (& stream
, Z_FINISH
));
30 "%d" , ( int ) stream
. total_out
, ( int ) e_len
);
32 file
, line
, "Buffer len was not exact match" , 1 ,
33 "%d" , ( int ) stream
. avail_out
, ( int ) INFLATE_EXTRA
);
36 memcmp ( expanded
, expected
, e_len
) == 0 ,
37 file
, line
, "uncompressed data did not match" , NULL
, 1 );
42 #define assert_zlib_equal(E,EL,C,CL) \
43 assert_zlib_equal_(E, EL, C, CL, #EL " != " #CL, __FILE__, (int)__LINE__)
45 void test_core_zstream__basic ( void )
47 git_zstream z
= GIT_ZSTREAM_INIT
;
49 size_t outlen
= sizeof ( out
);
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 );
58 assert_zlib_equal ( data
, strlen ( data
) + 1 , out
, outlen
);
61 void test_core_zstream__fails_on_trailing_garbage ( void )
63 git_buf deflated
= GIT_BUF_INIT
, inflated
= GIT_BUF_INIT
;
66 /* compress a simple string */
67 git_zstream_deflatebuf (& deflated
, "foobar!!" , 8 );
69 /* append some garbage */
70 for ( i
= 0 ; i
< 10 ; i
++) {
71 git_buf_putc (& deflated
, i
);
74 cl_git_fail ( git_zstream_inflatebuf (& inflated
, deflated
. ptr
, deflated
. size
));
76 git_buf_free (& deflated
);
77 git_buf_free (& inflated
);
80 void test_core_zstream__buffer ( void )
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
);
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"
90 static void compress_and_decompress_input_various_ways ( git_buf
* input
)
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
);
98 /* compress with deflatebuf */
100 cl_git_pass ( git_zstream_deflatebuf (& out1
, input
-> ptr
, input
-> size
));
101 assert_zlib_equal ( input
-> ptr
, input
-> size
, out1
. ptr
, out1
. size
);
103 /* compress with various fixed size buffer (accumulating the output) */
105 for ( i
= 0 ; i
< 3 ; ++ i
) {
106 git_zstream zs
= GIT_ZSTREAM_INIT
;
107 size_t use_fixed_size
;
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 ;
114 cl_assert ( use_fixed_size
<= fixed_size
);
116 cl_git_pass ( git_zstream_init (& zs
, GIT_ZSTREAM_DEFLATE
));
117 cl_git_pass ( git_zstream_set_input (& zs
, input
-> ptr
, input
-> size
));
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
));
125 git_zstream_free (& zs
);
126 assert_zlib_equal ( input
-> ptr
, input
-> size
, out2
. ptr
, out2
. size
);
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
));
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 );
140 git_buf_free (& inflated
);
144 void test_core_zstream__big_data ( void )
146 git_buf in
= GIT_BUF_INIT
;
149 for ( target
= 1024 ; target
<= 1024 * 1024 * 4 ; target
*= 8 ) {
151 /* make a big string that's easy to compress */
153 while ( in
. size
< target
)
155 git_buf_put (& in
, BIG_STRING_PART
, strlen ( BIG_STRING_PART
)));
157 compress_and_decompress_input_various_ways (& in
);
159 /* make a big string that's hard to compress */
161 for ( scan
= 0 ; scan
< in
. size
; ++ scan
)
162 in
. ptr
[ scan
] = ( char ) rand ();
164 compress_and_decompress_input_various_ways (& in
);