mirror of
https://github.com/Prominence/rshred.git
synced 2026-01-09 18:26:41 +03:00
Added new integration test. Small improvements.
This commit is contained in:
parent
e2257e1458
commit
f7ba2f05b4
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user