Initial setup for integration tests.

This commit is contained in:
Alexey Zinchenko 2021-02-13 01:11:31 +03:00
parent c6583871b4
commit e2257e1458
6 changed files with 157 additions and 5 deletions

101
Cargo.lock generated
View File

@ -73,12 +73,80 @@ version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" 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]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.10" version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" 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]] [[package]]
name = "rand" name = "rand"
version = "0.8.3" version = "0.8.3"
@ -124,6 +192,7 @@ name = "rshred"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"ntest",
"rand", "rand",
"walkdir", "walkdir",
] ]
@ -137,12 +206,29 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "serde"
version = "1.0.123"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 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]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -152,12 +238,27 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.8" version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"

View File

@ -13,6 +13,9 @@ clap = "2.33.3"
rand = "0.8.3" rand = "0.8.3"
walkdir = "2.3.1" walkdir = "2.3.1"
[dev-dependencies]
ntest = "0.7.3"
[profile.release] [profile.release]
opt-level = 'z' # optimize for size opt-level = 'z' # optimize for size
lto = true lto = true

View File

@ -68,7 +68,9 @@ impl Shredder {
match File::create(absolute_path) { match File::create(absolute_path) {
Ok(file) => { 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); let mut buffer = BufWriter::new(&file);
for _ in 0..options.rewrite_iterations { for _ in 0..options.rewrite_iterations {
@ -215,7 +217,7 @@ impl PartialEq for Verbosity {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::shred::Verbosity; use crate::Verbosity;
#[test] #[test]
fn verbosity_check() { fn verbosity_check() {

View File

@ -2,11 +2,9 @@ extern crate clap;
use clap::{App, Arg, crate_authors, crate_name, crate_version}; use clap::{App, Arg, crate_authors, crate_name, crate_version};
use crate::shred::{Shredder, Verbosity, ShredOptions};
use std::process::exit; use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use rshred::{Verbosity, Shredder, ShredOptions};
mod shred;
fn main() { fn main() {
let params = App::new(crate_name!()) let params = App::new(crate_name!())

18
tests/common.rs Normal file
View File

@ -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();
}

30
tests/integration_test.rs Normal file
View File

@ -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();
}