Added new integration test. Small improvements.

This commit is contained in:
Alexey Zinchenko 2021-02-13 21:23:52 +03:00
parent e2257e1458
commit f7ba2f05b4
2 changed files with 60 additions and 14 deletions

View File

@ -1,18 +1,45 @@
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use rand::Rng;
use rand::distributions::Alphanumeric;
pub const PLAIN_FILE_CONTENT: &str = "some sensitive data"; pub const PLAIN_FILE_CONTENT: &str = "some sensitive data";
pub const TEST_FILE: &str = "target/test/test.txt";
const TEST_DIR: &str = "target/test"; const TEST_DIR: &str = "target/test";
pub fn setup() { pub fn setup() -> EnvironmentDetails {
std::fs::create_dir_all(TEST_DIR).unwrap(); 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() { pub fn cleanup(env_details: EnvironmentDetails) {
std::fs::remove_file(TEST_FILE).unwrap(); if std::path::Path::new(&env_details.test_file_path).exists() {
std::fs::remove_dir(TEST_DIR).unwrap(); 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(),
}
}
} }

View File

@ -7,24 +7,43 @@ mod common;
#[test] #[test]
#[timeout(1000)] #[timeout(1000)]
fn test_basic_shredding() { fn shredding_without_file_deletion() {
common::setup(); 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_verbosity(rshred::Verbosity::None)
.set_is_interactive(false) .set_is_interactive(false)
.set_keep_files(true) .set_keep_files(true)
.build(); .build();
rshred::Shredder::with_options(options).run(); 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 // file's size hasn't changed
assert_eq!(initial_file_length, shredded_file_length); assert_eq!(initial_file_length, shredded_file_length);
// file's content will become an invalid UTF-8 string // 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);
} }