Debian Codegraph @

src/stream/tests.rs

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 use super::{copy_encode, decode_all, encode_all};
2 use super::{Decoder, Encoder};
3
4 use std::io;
5 use std::iter;
6
7 #[test]
8 fn test_end_of_frame() {
9 use std::io::{Read, Write};
10
11 let mut enc = Encoder::new(Vec::new(), 1).unwrap();
12 enc.write_all(b"foo").unwrap();
13 let mut compressed = enc.finish().unwrap();
14
15 // Add footer/whatever to underlying storage.
16 compressed.push(0);
17
18 // Drain zstd stream until end-of-frame.
19 let mut dec = Decoder::new(&compressed[..]).unwrap().single_frame();
20 let mut buf = Vec::new();
21 dec.read_to_end(&mut buf).unwrap();
22 assert_eq!(&buf, b"foo", "Error decoding a single frame.");
23 }
24
25 #[test]
26 fn test_concatenated_frames() {
27 let mut buffer = Vec::new();
28 copy_encode(&b"foo"[..], &mut buffer, 1).unwrap();
29 copy_encode(&b"bar"[..], &mut buffer, 2).unwrap();
30 copy_encode(&b"baz"[..], &mut buffer, 3).unwrap();
31
32 assert_eq!(
33 &decode_all(&buffer[..]).unwrap(),
34 b"foobarbaz",
35 "Error decoding concatenated frames."
36 );
37 }
38
39 #[test]
40 fn test_flush() {
41 use std::io::Write;
42
43 let buf = Vec::new();
44 let mut z = Encoder::new(buf, 19).unwrap();
45
46 z.write_all(b"hello").unwrap();
47
48 z.flush().unwrap(); // Might corrupt stream
49 let buf = z.finish().unwrap();
50
51 let s = decode_all(&buf[..]).unwrap();
52 assert_eq!(s, b"hello", "Error decoding after flush.");
53 }
54
•55 /*#[test]
56 fn test_try_finish() {
57 use std::io::Write;
58 let mut z = setup_try_finish();
59
60 z.get_mut().set_ops(iter::repeat(PartialOp::Unlimited));
61
62 // flush() should continue to work even though write() doesn't.
63 z.flush().unwrap();
64
65 let buf = match z.try_finish() {
66 Ok(buf) => buf.into_inner(),
67 Err((_z, e)) => panic!("try_finish failed with {:?}", e),
68 };
69
70 // Make sure the multiple try_finish calls didn't screw up the internal
71 // buffer and continued to produce valid compressed data.
72 assert_eq!(&decode_all(&buf[..]).unwrap(), b"hello", "Error decoding");
73 }
74
75 #[test]
76 #[should_panic]
77 fn test_write_after_try_finish() {
78 use std::io::Write;
79 let mut z = setup_try_finish();
80 z.write_all(b"hello world").unwrap();
81 }
82
83 fn setup_try_finish() -> Encoder<'static, PartialWrite<Vec<u8>>> {
84 use std::io::Write;
85
86 let buf =
87 PartialWrite::new(Vec::new(), iter::repeat(PartialOp::Unlimited));
88 let mut z = Encoder::new(buf, 19).unwrap();
89
90 z.write_all(b"hello").unwrap();
91
92 z.get_mut()
93 .set_ops(iter::repeat(PartialOp::Err(io::ErrorKind::WouldBlock)));
94
95 let (z, err) = z.try_finish().unwrap_err();
96 assert_eq!(
97 err.kind(),
98 io::ErrorKind::WouldBlock,
99 "expected WouldBlock error"
100 );
101
102 z
103 }
104
105 #[test]
106 fn test_failing_write() {
107 use std::io::Write;
108
109 let buf = PartialWrite::new(
110 Vec::new(),
111 iter::repeat(PartialOp::Err(io::ErrorKind::WouldBlock)),
112 );
113 let mut z = Encoder::new(buf, 1).unwrap();
114
115 // Fill in enough data to make sure the buffer gets written out.
116 let input = vec![b'b'; 128 * 1024];
117 // This should work even though the inner writer rejects writes.
118 assert_eq!(
119 z.write(&input).unwrap(),
120 128 * 1024,
121 "did not write all input buffer"
122 );
123
124 // The next write would fail (the buffer still has some data in it).
125 assert_eq!(
126 z.write(b"abc").unwrap_err().kind(),
127 io::ErrorKind::WouldBlock,
128 "expected WouldBlock error"
129 );
130
131 z.get_mut().set_ops(iter::repeat(PartialOp::Unlimited));
132
133 // This shouldn't have led to any corruption.
134 let buf = z.finish().unwrap().into_inner();
135 assert_eq!(
136 &decode_all(&buf[..]).unwrap(),
137 &input,
138 "WouldBlock errors should not corrupt stream"
139 );
•140 }*/
141
142 #[test]
143 fn test_invalid_frame() {
144 use std::io::Read;
145
146 // I really hope this data is invalid...
147 let data = &[1u8, 2u8, 3u8, 4u8, 5u8];
148 let mut dec = Decoder::new(&data[..]).unwrap();
149 assert_eq!(
150 dec.read_to_end(&mut Vec::new()).err().map(|e| e.kind()),
151 Some(io::ErrorKind::Other),
152 "did not encounter expected 'invalid frame' error"
153 );
154 }
155
156 #[test]
157 fn test_incomplete_frame() {
158 use std::io::{Read, Write};
159
160 let mut enc = Encoder::new(Vec::new(), 1).unwrap();
161 enc.write_all(b"This is a regular string").unwrap();
162 let mut compressed = enc.finish().unwrap();
163
164 let half_size = compressed.len() - 2;
165 compressed.truncate(half_size);
166
167 let mut dec = Decoder::new(&compressed[..]).unwrap();
168 assert_eq!(
169 dec.read_to_end(&mut Vec::new()).err().map(|e| e.kind()),
170 Some(io::ErrorKind::UnexpectedEof),
171 "did not encounter expected EOF error"
172 );
173 }
174
•175 //#[test]
176 #[cfg(has_example_txt_zst)]
177 fn test_cli_compatibility() {
178 let input = include_bytes!("../../assets/example.txt.zst");
179
180 let output = decode_all(&input[..]).unwrap();
181
182 let expected = include_bytes!("../../assets/example.txt");
183
184 assert_eq!(
185 &output[..],
186 &expected[..],
187 "error decoding cli-compressed data"
188 );
189 }
190
191 #[cfg(feature = "legacy")]
•192 //#[test]
193 fn test_legacy() {
194 use std::fs;
195 use std::io::Read;
196
197 // Read the content from that file
198 let expected = include_bytes!("../../assets/example.txt");
199
200 for version in &[5, 6, 7, 8] {
201 let filename = format!("assets/example.txt.v{}.zst", version);
202 let file = fs::File::open(filename).unwrap();
203 let mut decoder = Decoder::new(file).unwrap();
204
205 let mut buffer = Vec::new();
206 decoder.read_to_end(&mut buffer).unwrap();
207
208 assert_eq!(
209 &expected[..],
210 &buffer[..],
211 "error decompressing legacy version {}",
212 version
213 );
214 }
215 }
216
217 // Check that compressing+decompressing some data gives back the original
218 fn test_full_cycle(input: &[u8], level: i32) {
219 crate::test_cycle_unwrap(
220 input,
221 |data| encode_all(data, level),
222 |data| decode_all(data),
223 );
224 }
225
226 #[test]
227 fn test_empty() {
228 // Test compressing empty data
229 for level in 1..19 {
230 test_full_cycle(b"", level);
231 }
232 }
233
234 #[test]
235 fn test_ll_source() {
236 // Where could I find some long text?...
•237 // Debian: originally it's the prebuilt bindings, but we excluded it.
238 let data = include_bytes!("raw.rs");
239 // Test a few compression levels.
240 // TODO: check them all?
241 for level in 1..5 {
242 // Test compressing actual data
243 test_full_cycle(data, level);
244 }
245 }
246
247 #[test]
248 fn reader_to_writer() {
249 use std::io::{Read, Write};
250
251 let clear = include_bytes!("../../assets/example.txt");
252 // Compress using reader
253 let mut encoder = super::read::Encoder::new(&clear[..], 1).unwrap();
254
255 let mut compressed_buffer = Vec::new();
256 encoder.read_to_end(&mut compressed_buffer).unwrap();
257
258 // eprintln!("Compressed Buffer: {:?}", compressed_buffer);
259
260 // Decompress using writer
261 let mut decompressed_buffer = Vec::new();
262 let mut decoder =
263 super::write::Decoder::new(&mut decompressed_buffer).unwrap();
264 decoder.write_all(&compressed_buffer[..]).unwrap();
265 decoder.flush().unwrap();
266 // eprintln!("{:?}", decompressed_buffer);
267
268 assert_eq!(clear, &decompressed_buffer[..]);
269 }
270
271 #[test]
272 fn test_finish_empty_encoder() {
273 use std::io::Write;
274 let mut enc = Encoder::new(Vec::new(), 0).unwrap();
275 enc.do_finish().unwrap();
276 enc.write_all(b"this should not work").unwrap_err();
277 enc.finish().unwrap();
278 }