From f7ba2f05b46bedf37782ef0650b6af878536d0da Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Sat, 13 Feb 2021 21:23:52 +0300 Subject: [PATCH] Added new integration test. Small improvements. --- tests/common.rs | 41 ++++++++++++++++++++++++++++++++------- tests/integration_test.rs | 33 ++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index 195bed6..280c279 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,18 +1,45 @@ use std::fs::File; use std::io::Write; +use rand::Rng; +use rand::distributions::Alphanumeric; 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() { +pub fn setup() -> EnvironmentDetails { 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(); + + let filename: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(7) + .map(char::from) + .collect(); + let tmp_file_path = format!("{}/{}", TEST_DIR, filename); + let mut tmp_file = File::create(&tmp_file_path).unwrap(); + + tmp_file.write(PLAIN_FILE_CONTENT.as_bytes()).unwrap(); + tmp_file.sync_all().unwrap(); + + println!("{}", filename); + + EnvironmentDetails::new(&tmp_file_path) } -pub fn cleanup() { - std::fs::remove_file(TEST_FILE).unwrap(); - std::fs::remove_dir(TEST_DIR).unwrap(); +pub fn cleanup(env_details: EnvironmentDetails) { + if std::path::Path::new(&env_details.test_file_path).exists() { + std::fs::remove_file(&env_details.test_file_path).unwrap(); + } +} + +pub struct EnvironmentDetails { + pub test_file_path: String, +} + +impl EnvironmentDetails { + pub fn new(filepath: &str) -> EnvironmentDetails { + EnvironmentDetails { + test_file_path: filepath.to_string(), + } + } } \ No newline at end of file diff --git a/tests/integration_test.rs b/tests/integration_test.rs index c983279..2dd4f86 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -7,24 +7,43 @@ mod common; #[test] #[timeout(1000)] -fn test_basic_shredding() { - common::setup(); +fn shredding_without_file_deletion() { + let env_details = common::setup(); - let initial_file_length = std::fs::metadata(common::TEST_FILE).unwrap().len(); + let initial_file_length = std::fs::metadata(&env_details.test_file_path).unwrap().len(); - let options = rshred::ShredOptions::new(common::TEST_FILE.to_string()) + let options = rshred::ShredOptions::new(env_details.test_file_path.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(); + let shredded_file_length = std::fs::metadata(&env_details.test_file_path).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()); + assert_false!(std::fs::read_to_string(&env_details.test_file_path).is_ok()); - common::cleanup(); + common::cleanup(env_details); +} + +#[test] +#[timeout(1000)] +fn shredding_with_file_deletion() { + let env_details = common::setup(); + + let options = rshred::ShredOptions::new(env_details.test_file_path.to_string()) + .set_verbosity(rshred::Verbosity::None) + .set_is_interactive(false) + .set_keep_files(false) + .build(); + rshred::Shredder::with_options(options).run(); + + let shredded_file_exists = std::path::Path::new(&env_details.test_file_path).exists(); + + assert_false!(shredded_file_exists); + + common::cleanup(env_details); } \ No newline at end of file