From 984724a465a16159c4410944560ddc4c9bc9ed47 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 28 Apr 2024 15:21:42 +0100 Subject: [PATCH] test(fs): add tests --- src/tests/fs.rs | 172 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 3 deletions(-) diff --git a/src/tests/fs.rs b/src/tests/fs.rs index 699f914..b8c5970 100644 --- a/src/tests/fs.rs +++ b/src/tests/fs.rs @@ -7,7 +7,7 @@ type TestResult = Result<(), fs::Error>; mod path_of { use super::*; #[test] - fn path_of_validate_fails_path_traversal() -> TestResult { + fn validate_fails_on_path_traversal() -> TestResult { let fs = fs::temp()?; let_assert!(Err(fs::Error::PathTraversal { base, path: _path }) = fs.path_of("..".into())); @@ -20,6 +20,7 @@ mod path_of { mod file { use super::*; #[test] + /// Write to a file, read it, verify it exists, is a file and has the expected contents fn write_read_file_exists() -> TestResult { let fs = fs::temp()?; let pathbuf = fs.base().join("foo"); @@ -44,7 +45,7 @@ mod file { mod dir_create { use super::*; #[test] - fn dir_create_should_create_a_dir() -> TestResult { + fn should_create_a_dir() -> TestResult { let fs = fs::temp()?; let pathbuf = fs.base().join("subdir"); @@ -58,9 +59,26 @@ mod dir_create { Ok(()) } + #[test] + fn should_fail_on_path_traversal() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("..").join("foo"); + let_assert!( + Err(fs::Error::PathTraversal { + base: _base, + path: _path + }) = fs.dir_create(&path) + ); + + Ok(()) + } +} + +mod dir_create_all { + use super::*; #[test] - fn dir_create_all_should_create_a_dir() -> TestResult { + fn should_create_a_dir() -> TestResult { let fs = fs::temp()?; let pathbuf = fs.base().join("subdir").join("child"); @@ -72,6 +90,154 @@ mod dir_create { let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf)); assert!(is_dir, "path is a directory"); + Ok(()) + } + #[test] + fn should_fail_on_path_traversal() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("..").join("foo"); + let_assert!( + Err(fs::Error::PathTraversal { + base: _base, + path: _path + }) = fs.dir_create_all(&path) + ); + + Ok(()) + } +} + +mod path_exists { + use super::*; + #[test] + fn should_be_true_when_it_exists() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(_) = fs.file_write(&path, "bar")); + let_assert!(Ok(exists) = fs.path_exists(&path)); + assert!(exists); + + Ok(()) + } + #[test] + fn should_be_false_when_it_does_not_exist() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(exists) = fs.path_exists(&path)); + assert!(!exists); + + Ok(()) + } + #[test] + fn should_fail_on_path_traversal() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("..").join("foo"); + let_assert!( + Err(fs::Error::PathTraversal { + base: _base, + path: _path + }) = fs.path_exists(&path) + ); + + Ok(()) + } +} + +mod path_is_dir { + use super::*; + #[test] + fn should_be_true_when_is_a_dir() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(_) = fs.dir_create(&path)); + let_assert!(Ok(is_dir) = fs.path_is_dir(&path)); + assert!(is_dir); + + Ok(()) + } + #[test] + fn should_be_false_when_is_a_file() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(_) = fs.file_write(&path, "bar")); + let_assert!(Ok(is_dir) = fs.path_is_dir(&path)); + assert!(!is_dir); + + Ok(()) + } + #[test] + #[ignore] + fn should_be_false_when_is_a_link() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + // TODO create a link + // let_assert!(Ok(_) = fs.file_write(&path, "bar")); + let_assert!(Ok(is_dir) = fs.path_is_dir(&path)); + assert!(!is_dir); + + Ok(()) + } + #[test] + fn should_fail_on_path_traversal() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("..").join("foo"); + let_assert!( + Err(fs::Error::PathTraversal { + base: _base, + path: _path + }) = fs.path_is_dir(&path) + ); + + Ok(()) + } +} + +mod path_is_file { + use super::*; + #[test] + fn should_be_true_when_is_a_file() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(_) = fs.file_write(&path, "bar")); + + let_assert!(Ok(is_file) = fs.path_is_file(&path)); + assert!(is_file); + + Ok(()) + } + #[test] + fn should_be_false_when_is_a_dir() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + let_assert!(Ok(_) = fs.dir_create(&path)); + let_assert!(Ok(is_file) = fs.path_is_file(&path)); + assert!(!is_file); + + Ok(()) + } + #[test] + #[ignore] + fn should_be_false_when_is_a_link() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("foo"); + // TODO create a link + // let_assert!(Ok(_) = fs.file_write(&path, "bar")); + let_assert!(Ok(is_file) = fs.path_is_file(&path)); + assert!(!is_file); + + Ok(()) + } + #[test] + fn should_fail_on_path_traversal() -> TestResult { + let fs = fs::temp()?; + let path = fs.base().join("..").join("foo"); + let_assert!( + Err(fs::Error::PathTraversal { + base: _base, + path: _path + }) = fs.path_is_file(&path) + ); + Ok(()) } }