Debian Codegraph @

debian/rust-vendor/flate2/tests/capabilities.rs

raw
1 //! Validate that certain feature-gated functionality is still available.
2 #[cfg(feature = "any_zlib")]
3 use flate2::{Compress, Compression, Decompress, FlushCompress, FlushDecompress};
4
5 // Unsupported for `miniz_oxide`.
6 #[cfg(feature = "any_zlib")]
7 #[test]
8 fn compress_new_with_window_bits_is_present_and_works() {
9 let string = "hello world".as_bytes();
10
11 // Test with window_bits = 9 (minimum)
12 let mut encoded = Vec::with_capacity(1024);
13 let mut encoder = Compress::new_with_window_bits(Compression::default(), true, 9);
14 encoder
15 .compress_vec(string, &mut encoded, FlushCompress::Finish)
16 .unwrap();
17 assert_ne!(encoded.len(), 0);
18
19 let mut decoder = Decompress::new_with_window_bits(true, 9);
20 let mut decoded = [0; 1024];
21 decoder
22 .decompress(&encoded, &mut decoded, FlushDecompress::Finish)
23 .unwrap();
24 assert_eq!(&decoded[..string.len()], string);
25
26 // Test with window_bits = 15 (maximum)
27 let mut encoded = Vec::with_capacity(1024);
28 let mut encoder = Compress::new_with_window_bits(Compression::default(), false, 15);
29 encoder
30 .compress_vec(string, &mut encoded, FlushCompress::Finish)
31 .unwrap();
32 assert_ne!(encoded.len(), 0);
33
34 let mut decoder = Decompress::new_with_window_bits(false, 15);
35 let mut decoded = [0; 1024];
36 decoder
37 .decompress(&encoded, &mut decoded, FlushDecompress::Finish)
38 .unwrap();
39 assert_eq!(&decoded[..string.len()], string);
40 }
41
42 // Unsupported for `miniz_oxide`.
43 #[cfg(feature = "any_zlib")]
44 #[test]
45 fn decompress_new_gzip_window_bits_is_present_and_works() {
46 let string = "hello world".as_bytes();
47
48 // Test with different window_bits values
49 for window_bits in [9, 12, 15] {
50 let mut encoded = Vec::with_capacity(1024);
51 let mut encoder = Compress::new_gzip(Compression::default(), window_bits);
52 encoder
53 .compress_vec(string, &mut encoded, FlushCompress::Finish)
54 .unwrap();
55
56 let mut decoder = Decompress::new_gzip(window_bits);
57 let mut decoded = [0; 1024];
58 decoder
59 .decompress(&encoded, &mut decoded, FlushDecompress::Finish)
60 .unwrap();
61 assert_eq!(
62 &decoded[..string.len()],
63 string,
64 "Failed with window_bits={}",
65 window_bits
66 );
67 }
68 }
69
70 // Unsupported for `miniz_oxide`.
71 #[cfg(feature = "any_zlib")]
72 #[test]
73 #[should_panic(expected = "window_bits must be within 9 ..= 15")]
74 fn compress_new_with_window_bits_invalid_low() {
75 let _ = Compress::new_with_window_bits(Compression::default(), true, 8);
76 }
77
78 // Unsupported for `miniz_oxide`.
79 #[cfg(feature = "any_zlib")]
80 #[test]
81 #[should_panic(expected = "window_bits must be within 9 ..= 15")]
82 fn compress_new_with_window_bits_invalid_high() {
83 let _ = Compress::new_with_window_bits(Compression::default(), true, 16);
84 }
85
86 // Unsupported for `miniz_oxide`.
87 #[cfg(feature = "any_zlib")]
88 #[test]
89 #[should_panic(expected = "window_bits must be within 9 ..= 15")]
90 fn compress_new_gzip_invalid_low() {
91 let _ = Compress::new_gzip(Compression::default(), 8);
92 }
93
94 // Unsupported for `miniz_oxide`.
95 #[cfg(feature = "any_zlib")]
96 #[test]
97 #[should_panic(expected = "window_bits must be within 9 ..= 15")]
98 fn compress_new_gzip_invalid_high() {
99 let _ = Compress::new_gzip(Compression::default(), 16);
100 }
101
102 // Unsupported for `miniz_oxide`.
103 #[cfg(feature = "any_zlib")]
104 #[test]
105 fn set_dictionary_with_zlib_header() {
106 let string = "hello, hello!".as_bytes();
107 let dictionary = "hello".as_bytes();
108
109 let mut encoded = Vec::with_capacity(1024);
110
111 let mut encoder = Compress::new(Compression::default(), true);
112
113 let dictionary_adler = encoder.set_dictionary(dictionary).unwrap();
114
115 encoder
116 .compress_vec(string, &mut encoded, FlushCompress::Finish)
117 .unwrap();
118
119 assert_eq!(encoder.total_in(), string.len() as u64);
120 assert_eq!(encoder.total_out(), encoded.len() as u64);
121
122 let mut decoder = Decompress::new(true);
123 let mut decoded = [0; 1024];
124 let decompress_error = decoder
125 .decompress(&encoded, &mut decoded, FlushDecompress::Finish)
126 .expect_err("decompression should fail due to requiring a dictionary");
127
128 let required_adler = decompress_error.needs_dictionary()
129 .expect("the first call to decompress should indicate a dictionary is required along with the required Adler-32 checksum");
130
131 assert_eq!(required_adler, dictionary_adler,
132 "the Adler-32 checksum should match the value when the dictionary was set on the compressor");
133
134 let actual_adler = decoder.set_dictionary(dictionary).unwrap();
135
136 assert_eq!(required_adler, actual_adler);
137
138 // Decompress the rest of the input to the remainder of the output buffer
139 let total_in = decoder.total_in();
140 let total_out = decoder.total_out();
141
142 let decompress_result = decoder.decompress(
143 &encoded[total_in as usize..],
144 &mut decoded[total_out as usize..],
145 FlushDecompress::Finish,
146 );
147 assert!(decompress_result.is_ok());
148
149 assert_eq!(&decoded[..decoder.total_out() as usize], string);
150 }
151
152 // Unsupported for `miniz_oxide`.
153 #[cfg(feature = "any_zlib")]
154 #[test]
155 fn set_dictionary_raw() {
156 let string = "hello, hello!".as_bytes();
157 let dictionary = "hello".as_bytes();
158
159 let mut encoded = Vec::with_capacity(1024);
160
161 let mut encoder = Compress::new(Compression::default(), false);
162
163 encoder.set_dictionary(dictionary).unwrap();
164
165 encoder
166 .compress_vec(string, &mut encoded, FlushCompress::Finish)
167 .unwrap();
168
169 assert_eq!(encoder.total_in(), string.len() as u64);
170 assert_eq!(encoder.total_out(), encoded.len() as u64);
171
172 let mut decoder = Decompress::new(false);
173
174 decoder.set_dictionary(dictionary).unwrap();
175
176 let mut decoded = [0; 1024];
177 let decompress_result = decoder.decompress(&encoded, &mut decoded, FlushDecompress::Finish);
178
179 assert!(decompress_result.is_ok());
180
181 assert_eq!(&decoded[..decoder.total_out() as usize], string);
182 }
183
184 // Unsupported for `miniz_oxide`.
185 #[cfg(feature = "any_zlib")]
186 #[test]
187 fn compression_levels_are_effective() {
188 let input = b"hello hello hello hello hello hello hello hello";
189
190 // Compress with no compression
191 let mut encoded_none = Vec::new();
192 Compress::new(Compression::none(), true)
193 .compress_vec(input, &mut encoded_none, FlushCompress::Finish)
194 .unwrap();
195
196 // Compress with best compression
197 let mut encoded_best = Vec::new();
198 Compress::new(Compression::best(), true)
199 .compress_vec(input, &mut encoded_best, FlushCompress::Finish)
200 .unwrap();
201
202 assert!(
203 encoded_best.len() <= encoded_none.len(),
204 "best compression produced larger output than no compression: best={}, none={}",
205 encoded_best.len(),
206 encoded_none.len(),
207 );
208 }
209
210 // Unsupported for `miniz_oxide`.
211 #[cfg(feature = "any_zlib")]
212 #[test]
213 fn set_level_is_effective() {
214 let input = b"hello hello hello hello hello hello hello hello";
215 let no_compression = Compression::none();
216 let best_compression = Compression::best();
217
218 // Compress with no compression
219 let mut encoded_none = Vec::new();
220 let mut compress = Compress::new(best_compression, true);
221 compress.set_level(no_compression).unwrap();
222 compress
223 .compress_vec(input, &mut encoded_none, FlushCompress::Finish)
224 .unwrap();
225
226 // Compress with best compression
227 let mut encoded_best = Vec::new();
228 let mut compress = Compress::new(no_compression, true);
229 compress.set_level(best_compression).unwrap();
230 compress
231 .compress_vec(input, &mut encoded_best, FlushCompress::Finish)
232 .unwrap();
233
234 assert!(
235 encoded_best.len() <= encoded_none.len(),
236 "best compression produced larger output than no compression: best={}, none={}",
237 encoded_best.len(),
238 encoded_none.len(),
239 );
240 }