Debian Codegraph @

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

raw
1 use flate2::read::GzDecoder;
2 use flate2::read::MultiGzDecoder;
3 use flate2::write::GzEncoder;
4 use flate2::Compression;
5 use std::fs::File;
6 use std::io::prelude::*;
7 use std::io::{self, BufReader};
8 use std::path::Path;
9
10 // test extraction of a gzipped file
11 #[test]
12 fn test_extract_success() {
13 let content = extract_file(Path::new("tests/good-file.gz")).unwrap();
14 let mut expected = Vec::new();
15 File::open("tests/good-file.txt")
16 .unwrap()
17 .read_to_end(&mut expected)
18 .unwrap();
19 assert_eq!(content, expected);
20 }
21 //
22 // test partial extraction of a multistream gzipped file
23 #[test]
24 fn test_extract_success_partial_multi() {
25 let content = extract_file(Path::new("tests/multi.gz")).unwrap();
26 let mut expected = String::new();
27 BufReader::new(File::open("tests/multi.txt").unwrap())
28 .read_line(&mut expected)
29 .unwrap();
30 assert_eq!(content, expected.as_bytes());
31 }
32
33 // test extraction fails on a corrupt file
34 #[test]
35 fn test_extract_failure() {
36 let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
37 encoder.write_all(b"corrupt me").unwrap();
38 let mut gzip = encoder.finish().unwrap();
39 // The gzip footer stores CRC32 followed by ISIZE; corrupt a CRC byte.
40 let crc_offset = gzip.len() - 8;
41 gzip[crc_offset] ^= 0xff;
42
43 let mut output = Vec::new();
44 let result = GzDecoder::new(&gzip[..]).read_to_end(&mut output);
45 assert_eq!(result.err().unwrap().kind(), io::ErrorKind::InvalidInput);
46 }
47
48 //test complete extraction of a multistream gzipped file
49 #[test]
50 fn test_extract_success_multi() {
51 let content = extract_file_multi(Path::new("tests/multi.gz")).unwrap();
52 let mut expected = Vec::new();
53 File::open("tests/multi.txt")
54 .unwrap()
55 .read_to_end(&mut expected)
56 .unwrap();
57 assert_eq!(content, expected);
58 }
59
60 // Tries to extract path into memory (assuming a .gz file).
61 fn extract_file(path_compressed: &Path) -> io::Result<Vec<u8>> {
62 let mut v = Vec::new();
63 let f = File::open(path_compressed)?;
64 GzDecoder::new(f).read_to_end(&mut v)?;
65 Ok(v)
66 }
67
68 // Tries to extract path into memory (decompressing all members in case
69 // of a multi member .gz file).
70 fn extract_file_multi(path_compressed: &Path) -> io::Result<Vec<u8>> {
71 let mut v = Vec::new();
72 let f = File::open(path_compressed)?;
73 MultiGzDecoder::new(f).read_to_end(&mut v)?;
74 Ok(v)
75 }
76
77 #[test]
78 fn empty_error_once() {
79 let data: &[u8] = &[];
80 let cbjson = GzDecoder::new(data);
81 let reader = BufReader::new(cbjson);
82 let mut stream = reader.lines();
83 assert!(stream.next().unwrap().is_err());
84 assert!(stream.next().is_none());
85 }