]> git.proxmox.com Git - rustc.git/blob - vendor/flate2/tests/gunzip.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / vendor / flate2 / tests / gunzip.rs
1 use flate2::read::GzDecoder;
2 use flate2::read::MultiGzDecoder;
3 use std::fs::File;
4 use std::io::prelude::*;
5 use std::io::{self, BufReader};
6 use std::path::Path;
7
8 // test extraction of a gzipped file
9 #[test]
10 fn test_extract_success() {
11 let content = extract_file(Path::new("tests/good-file.gz")).unwrap();
12 let mut expected = Vec::new();
13 File::open("tests/good-file.txt")
14 .unwrap()
15 .read_to_end(&mut expected)
16 .unwrap();
17 assert_eq!(content, expected);
18 }
19 //
20 // test partial extraction of a multistream gzipped file
21 #[test]
22 fn test_extract_success_partial_multi() {
23 let content = extract_file(Path::new("tests/multi.gz")).unwrap();
24 let mut expected = String::new();
25 BufReader::new(File::open("tests/multi.txt").unwrap())
26 .read_line(&mut expected)
27 .unwrap();
28 assert_eq!(content, expected.as_bytes());
29 }
30
31 // test extraction fails on a corrupt file
32 #[test]
33 fn test_extract_failure() {
34 let result = extract_file(Path::new("tests/corrupt-gz-file.bin"));
35 assert_eq!(result.err().unwrap().kind(), io::ErrorKind::InvalidInput);
36 }
37
38 //test complete extraction of a multistream gzipped file
39 #[test]
40 fn test_extract_success_multi() {
41 let content = extract_file_multi(Path::new("tests/multi.gz")).unwrap();
42 let mut expected = Vec::new();
43 File::open("tests/multi.txt")
44 .unwrap()
45 .read_to_end(&mut expected)
46 .unwrap();
47 assert_eq!(content, expected);
48 }
49
50 // Tries to extract path into memory (assuming a .gz file).
51 fn extract_file(path_compressed: &Path) -> io::Result<Vec<u8>> {
52 let mut v = Vec::new();
53 let f = File::open(path_compressed)?;
54 GzDecoder::new(f).read_to_end(&mut v)?;
55 Ok(v)
56 }
57
58 // Tries to extract path into memory (decompressing all members in case
59 // of a multi member .gz file).
60 fn extract_file_multi(path_compressed: &Path) -> io::Result<Vec<u8>> {
61 let mut v = Vec::new();
62 let f = File::open(path_compressed)?;
63 MultiGzDecoder::new(f).read_to_end(&mut v)?;
64 Ok(v)
65 }
66
67 #[test]
68 fn empty_error_once() {
69 let data: &[u8] = &[];
70 let cbjson = GzDecoder::new(data);
71 let reader = BufReader::new(cbjson);
72 let mut stream = reader.lines();
73 assert!(stream.next().unwrap().is_err());
74 assert!(stream.next().is_none());
75 }