Add end-to-end test

This commit is contained in:
camchenry 2018-10-13 18:16:12 -04:00
parent 52594745f3
commit a8b5210487

56
tests/end_to_end.rs Normal file
View file

@ -0,0 +1,56 @@
extern crate zip;
use std::io::prelude::*;
use zip::write::FileOptions;
use std::io::Cursor;
// This test asserts that after creating a zip file, then reading its contents back out,
// the extracted data will *always* be exactly the same as the original data.
#[test]
fn main() -> zip::result::ZipResult<()> {
let buf: &mut Vec<u8> = &mut Vec::new();
let mut file = Cursor::new(buf);
write_to_zip_file(&mut file)?;
let file_contents: String = read_zip_file(file).unwrap();
assert!(file_contents.as_bytes() == LOREM_IPSUM);
Ok(())
}
fn write_to_zip_file(file: &mut Cursor<&mut Vec<u8>>) -> zip::result::ZipResult<()> {
let mut zip = zip::ZipWriter::new(file);
zip.add_directory("test/", FileOptions::default())?;
let options = FileOptions::default()
.compression_method(zip::CompressionMethod::Stored)
.unix_permissions(0o755);
zip.start_file("test/☃.txt", options)?;
zip.write_all(b"Hello, World!\n")?;
zip.start_file("test/lorem_ipsum.txt", FileOptions::default())?;
zip.write_all(LOREM_IPSUM)?;
zip.finish()?;
Ok(())
}
fn read_zip_file(zip_file: Cursor<&mut Vec<u8>>) -> zip::result::ZipResult<String> {
let mut archive = zip::ZipArchive::new(zip_file).unwrap();
let mut file = archive.by_name("test/lorem_ipsum.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
Ok(contents)
}
const LOREM_IPSUM : &'static [u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet
molestie. Proin blandit ornare dui, a tempor nisl accumsan in. Praesent a consequat felis. Morbi metus diam, auctor in auctor vel, feugiat id odio. Curabitur ex ex,
dictum quis auctor quis, suscipit id lorem. Aliquam vestibulum dolor nec enim vehicula, porta tristique augue tincidunt. Vivamus ut gravida est. Sed pellentesque, dolor
vitae tristique consectetur, neque lectus pulvinar dui, sed feugiat purus diam id lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
inceptos himenaeos. Maecenas feugiat velit in ex ultrices scelerisque id id neque.
";