From 245d4d1a0fa631b52b7a1aeb2589228b1cbf03d7 Mon Sep 17 00:00:00 2001 From: Benjamin Lipp Date: Wed, 7 Aug 2024 12:50:04 +0200 Subject: [PATCH] feat: add tests for util file.rs Co-authored-by: Paul Spooren --- Cargo.lock | 1 + util/Cargo.toml | 5 +-- util/src/file.rs | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc92f88..aea1c07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2053,6 +2053,7 @@ dependencies = [ "mio 1.0.1", "rustix", "static_assertions", + "tempfile", "thiserror", "typenum", "zerocopy", diff --git a/util/Cargo.toml b/util/Cargo.toml index e80ba0f..4ad42a7 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -16,8 +16,9 @@ base64ct = { workspace = true } anyhow = { workspace = true } typenum = { workspace = true } static_assertions = { workspace = true } -rustix = {workspace = true} -zeroize = {workspace = true} +rustix = { workspace = true } +zeroize = { workspace = true } zerocopy = { workspace = true } thiserror = { workspace = true } mio = { workspace = true } +tempfile = { workspace = true } diff --git a/util/src/file.rs b/util/src/file.rs index dfba76c..3124cd7 100644 --- a/util/src/file.rs +++ b/util/src/file.rs @@ -114,3 +114,96 @@ pub trait DisplayValueB64 { fn display_b64<'o>(&self, output: &'o mut [u8]) -> Result<&'o str, Self::Error>; } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::Write; + use std::os::unix::fs::PermissionsExt; + use tempfile::tempdir; + + #[test] + fn test_fopen_w_public() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = fopen_w(path, Visibility::Public).unwrap(); + file.write_all(b"test").unwrap(); + let metadata = file.metadata().unwrap(); + let permissions = metadata.permissions(); + assert_eq!(permissions.mode(), 0o100644); + } + + #[test] + fn test_fopen_w_secret() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = fopen_w(path, Visibility::Secret).unwrap(); + file.write_all(b"test").unwrap(); + let metadata = file.metadata().unwrap(); + let permissions = metadata.permissions(); + assert_eq!(permissions.mode(), 0o100600); + } + + #[test] + fn test_fopen_r() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = File::create(path.clone()).unwrap(); + file.write_all(b"test").unwrap(); + let mut contents = String::new(); + let mut file = fopen_r(path).unwrap(); + file.read_to_string(&mut contents).unwrap(); + assert_eq!(contents, "test"); + } + + #[test] + fn test_read_slice_to_end() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = File::create(path.clone()).unwrap(); + file.write_all(b"test").unwrap(); + let mut buf = [0u8; 4]; + let mut file = fopen_r(path).unwrap(); + file.read_slice_to_end(&mut buf).unwrap(); + assert_eq!(buf, [116, 101, 115, 116]); + } + + #[test] + fn test_read_exact_to_end() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = File::create(path.clone()).unwrap(); + file.write_all(b"test").unwrap(); + let mut buf = [0u8; 4]; + let mut file = fopen_r(path).unwrap(); + file.read_exact_to_end(&mut buf).unwrap(); + assert_eq!(buf, [116, 101, 115, 116]); + } + + #[test] + fn test_read_exact_to_end_to_long() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = File::create(path.clone()).unwrap(); + file.write_all(b"test").unwrap(); + let mut buf = [0u8; 3]; + let mut file = fopen_r(path).unwrap(); + let result = file.read_exact_to_end(&mut buf); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "File too long!"); + } + + #[test] + fn test_read_slice_to_end_to_long() { + let tmp_dir = tempdir().unwrap(); + let path = tmp_dir.path().join("test"); + let mut file = File::create(path.clone()).unwrap(); + file.write_all(b"test").unwrap(); + let mut buf = [0u8; 3]; + let mut file = fopen_r(path).unwrap(); + let result = file.read_slice_to_end(&mut buf); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "File too long!"); + } +}