]> git.proxmox.com Git - rustc.git/blob - src/vendor/flate2/tests/gunzip.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / vendor / flate2 / tests / gunzip.rs
1 extern crate flate2;
2
3 use std::fs::File;
4 use std::io::prelude::*;
5 use std::io::{self, BufReader};
6 use std::path::Path;
7 use flate2::read::GzDecoder;
8 use flate2::read::MultiGzDecoder;
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").unwrap().read_to_end(&mut expected).unwrap();
16 assert!(content == expected);
17 }
18 //
19 // test partial extraction of a multistream gzipped file
20 #[test]
21 fn test_extract_success_partial_multi() {
22 let content = extract_file(Path::new("tests/multi.gz")).unwrap();
23 let mut expected = String::new();
24 BufReader::new(File::open("tests/multi.txt").unwrap()).read_line(&mut expected).unwrap();
25 assert_eq!(content, expected.as_bytes());
26 }
27
28 // test extraction fails on a corrupt file
29 #[test]
30 fn test_extract_failure() {
31 let result = extract_file(Path::new("tests/corrupt-file.gz"));
32 assert_eq!(result.err().unwrap().kind(), io::ErrorKind::InvalidInput);
33 }
34
35 //test complete extraction of a multistream gzipped file
36 #[test]
37 fn test_extract_success_multi() {
38 let content = extract_file_multi(Path::new("tests/multi.gz")).unwrap();
39 let mut expected = Vec::new();
40 File::open("tests/multi.txt").unwrap().read_to_end(&mut expected).unwrap();
41 assert_eq!(content, expected);
42 }
43
44 // Tries to extract path into memory (assuming a .gz file).
45 fn extract_file(path_compressed: &Path) -> io::Result<Vec<u8>>{
46 let mut v = Vec::new();
47 let f = try!(File::open(path_compressed));
48 try!(try!(GzDecoder::new(f)).read_to_end(&mut v));
49 Ok(v)
50 }
51
52 // Tries to extract path into memory (decompressing all members in case
53 // of a multi member .gz file).
54 fn extract_file_multi(path_compressed: &Path) -> io::Result<Vec<u8>>{
55 let mut v = Vec::new();
56 let f = try!(File::open(path_compressed));
57 try!(try!(MultiGzDecoder::new(f)).read_to_end(&mut v));
58 Ok(v)
59 }