From e2257e14589168a30f27af37db0fac48a3741868 Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Sat, 13 Feb 2021 01:11:31 +0300 Subject: [PATCH] Initial setup for integration tests. --- Cargo.lock | 101 +++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/{shred/mod.rs => lib.rs} | 6 ++- src/main.rs | 4 +- tests/common.rs | 18 +++++++ tests/integration_test.rs | 30 +++++++++++ 6 files changed, 157 insertions(+), 5 deletions(-) rename src/{shred/mod.rs => lib.rs} (97%) create mode 100644 tests/common.rs create mode 100644 tests/integration_test.rs diff --git a/Cargo.lock b/Cargo.lock index eba5bc6..b0d2ee1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,12 +73,80 @@ version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +[[package]] +name = "ntest" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "984caf6c8aa869418ef88062fc685d07d50c04308e63f7eaff6a395b1f5aff33" +dependencies = [ + "ntest_proc_macro_helper", + "ntest_test_cases", + "ntest_timeout", +] + +[[package]] +name = "ntest_proc_macro_helper" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115562228962147ca51748d19446a4261535a7b6a7b5ff02681e527dcefc22f7" + +[[package]] +name = "ntest_test_cases" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03e3201148714c580c5cf1ef68b1ed4039203068193197834a43503164b8237" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ntest_timeout" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a870300c30d4224cb16022a4660fd8084d6cb4d29618563c3016b50cc757d1e7" +dependencies = [ + "ntest_proc_macro_helper", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "ppv-lite86" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.8.3" @@ -124,6 +192,7 @@ name = "rshred" version = "0.1.0" dependencies = [ "clap", + "ntest", "rand", "walkdir", ] @@ -137,12 +206,29 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "serde" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -152,12 +238,27 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "unicode-width" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 52eb502..5f5fc24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,9 @@ clap = "2.33.3" rand = "0.8.3" walkdir = "2.3.1" +[dev-dependencies] +ntest = "0.7.3" + [profile.release] opt-level = 'z' # optimize for size lto = true diff --git a/src/shred/mod.rs b/src/lib.rs similarity index 97% rename from src/shred/mod.rs rename to src/lib.rs index 20fa858..aad012e 100644 --- a/src/shred/mod.rs +++ b/src/lib.rs @@ -68,7 +68,9 @@ impl Shredder { match File::create(absolute_path) { Ok(file) => { - println!("File's size: {}", file_length); + if options.verbosity > Verbosity::Low { + println!("File's size: {}", file_length); + } let mut buffer = BufWriter::new(&file); for _ in 0..options.rewrite_iterations { @@ -215,7 +217,7 @@ impl PartialEq for Verbosity { #[cfg(test)] mod tests { - use crate::shred::Verbosity; + use crate::Verbosity; #[test] fn verbosity_check() { diff --git a/src/main.rs b/src/main.rs index 0736097..fe69057 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,9 @@ extern crate clap; use clap::{App, Arg, crate_authors, crate_name, crate_version}; -use crate::shred::{Shredder, Verbosity, ShredOptions}; use std::process::exit; use std::str::FromStr; - -mod shred; +use rshred::{Verbosity, Shredder, ShredOptions}; fn main() { let params = App::new(crate_name!()) diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..195bed6 --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,18 @@ +use std::fs::File; +use std::io::Write; + +pub const PLAIN_FILE_CONTENT: &str = "some sensitive data"; + +pub const TEST_FILE: &str = "target/test/test.txt"; +const TEST_DIR: &str = "target/test"; + +pub fn setup() { + std::fs::create_dir_all(TEST_DIR).unwrap(); + let mut file = File::create("target/test/test.txt").unwrap(); + file.write(PLAIN_FILE_CONTENT.as_bytes()).unwrap(); +} + +pub fn cleanup() { + std::fs::remove_file(TEST_FILE).unwrap(); + std::fs::remove_dir(TEST_DIR).unwrap(); +} \ No newline at end of file diff --git a/tests/integration_test.rs b/tests/integration_test.rs new file mode 100644 index 0000000..c983279 --- /dev/null +++ b/tests/integration_test.rs @@ -0,0 +1,30 @@ +extern crate rshred; + +use ntest::timeout; +use ntest::assert_false; + +mod common; + +#[test] +#[timeout(1000)] +fn test_basic_shredding() { + common::setup(); + + let initial_file_length = std::fs::metadata(common::TEST_FILE).unwrap().len(); + + let options = rshred::ShredOptions::new(common::TEST_FILE.to_string()) + .set_verbosity(rshred::Verbosity::None) + .set_is_interactive(false) + .set_keep_files(true) + .build(); + rshred::Shredder::with_options(options).run(); + + let shredded_file_length = std::fs::metadata(common::TEST_FILE).unwrap().len(); + // file's size hasn't changed + assert_eq!(initial_file_length, shredded_file_length); + + // file's content will become an invalid UTF-8 string + assert_false!(std::fs::read_to_string(common::TEST_FILE).is_ok()); + + common::cleanup(); +} \ No newline at end of file