From a8b521048780ef3e05d692c3ccbc5dc7f8d8b5ee Mon Sep 17 00:00:00 2001 From: camchenry Date: Sat, 13 Oct 2018 18:16:12 -0400 Subject: [PATCH 1/3] Add end-to-end test --- tests/end_to_end.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/end_to_end.rs diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs new file mode 100644 index 00000000..3102401f --- /dev/null +++ b/tests/end_to_end.rs @@ -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 = &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>) -> 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>) -> zip::result::ZipResult { + 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. +"; From 19250df9e93ea120d56b6e65a79a6ba30ab0ee72 Mon Sep 17 00:00:00 2001 From: camchenry Date: Sat, 13 Oct 2018 18:40:06 -0400 Subject: [PATCH 2/3] Change end-to-end test to use &[u8] instead of Vec --- tests/end_to_end.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index 3102401f..86f45748 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -7,20 +7,19 @@ 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 = &mut Vec::new(); +fn main() { + // TODO: The buffer length is tied to the LOREM_IPSUM length currently + let buf: &mut [u8] = &mut [0u8; 760]; let mut file = Cursor::new(buf); - write_to_zip_file(&mut file)?; + write_to_zip_file(&mut file).expect("file written"); 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>) -> zip::result::ZipResult<()> { +fn write_to_zip_file(file: &mut Cursor<&mut [u8]>) -> zip::result::ZipResult<()> { let mut zip = zip::ZipWriter::new(file); zip.add_directory("test/", FileOptions::default())?; @@ -38,7 +37,7 @@ fn write_to_zip_file(file: &mut Cursor<&mut Vec>) -> zip::result::ZipResult< Ok(()) } -fn read_zip_file(zip_file: Cursor<&mut Vec>) -> zip::result::ZipResult { +fn read_zip_file(zip_file: Cursor<&mut [u8]>) -> zip::result::ZipResult { let mut archive = zip::ZipArchive::new(zip_file).unwrap(); let mut file = archive.by_name("test/lorem_ipsum.txt")?; From 59075de36ac9bd1dcdcf9a282d26e10e0f2a45b5 Mon Sep 17 00:00:00 2001 From: camchenry Date: Mon, 15 Oct 2018 12:06:54 -0400 Subject: [PATCH 3/3] Revert to Vec owned by cursor --- tests/end_to_end.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index 86f45748..e6350467 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -8,18 +8,16 @@ use std::io::Cursor; // the extracted data will *always* be exactly the same as the original data. #[test] fn main() { - // TODO: The buffer length is tied to the LOREM_IPSUM length currently - let buf: &mut [u8] = &mut [0u8; 760]; - let mut file = Cursor::new(buf); + let file = &mut Cursor::new(Vec::new()); - write_to_zip_file(&mut file).expect("file written"); + write_to_zip_file(file).expect("file written"); let file_contents: String = read_zip_file(file).unwrap(); assert!(file_contents.as_bytes() == LOREM_IPSUM); } -fn write_to_zip_file(file: &mut Cursor<&mut [u8]>) -> zip::result::ZipResult<()> { +fn write_to_zip_file(file: &mut Cursor>) -> zip::result::ZipResult<()> { let mut zip = zip::ZipWriter::new(file); zip.add_directory("test/", FileOptions::default())?; @@ -37,7 +35,7 @@ fn write_to_zip_file(file: &mut Cursor<&mut [u8]>) -> zip::result::ZipResult<()> Ok(()) } -fn read_zip_file(zip_file: Cursor<&mut [u8]>) -> zip::result::ZipResult { +fn read_zip_file(zip_file: &mut Cursor>) -> zip::result::ZipResult { let mut archive = zip::ZipArchive::new(zip_file).unwrap(); let mut file = archive.by_name("test/lorem_ipsum.txt")?;