From bc447524c2269a14f2b5b65107b11a4ed06cb0dd Mon Sep 17 00:00:00 2001 From: Gabriela Moldovan Date: Wed, 28 Jun 2023 19:40:20 +0100 Subject: [PATCH] keymgr: Add tests for ArtiNativeKeyStore::key_path. --- Cargo.lock | 1 + crates/tor-keymgr/Cargo.toml | 3 + crates/tor-keymgr/src/keystore/arti.rs | 89 ++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6f1e2e669..f1a3370b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4485,6 +4485,7 @@ dependencies = [ "dyn-clone", "fs-mistrust", "ssh-key", + "tempfile", "thiserror", "tor-error", "tor-hscrypto", diff --git a/crates/tor-keymgr/Cargo.toml b/crates/tor-keymgr/Cargo.toml index 4b9b82ce0..9676b96ca 100644 --- a/crates/tor-keymgr/Cargo.toml +++ b/crates/tor-keymgr/Cargo.toml @@ -32,6 +32,9 @@ tor-hscrypto = { path = "../tor-hscrypto", version = "0.2.0" } tor-llcrypto = { path = "../tor-llcrypto", version = "0.5.0" } zeroize = "1" +[dev-dependencies] +tempfile = "3" + [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] diff --git a/crates/tor-keymgr/src/keystore/arti.rs b/crates/tor-keymgr/src/keystore/arti.rs index cff4665cc..a8be1b093 100644 --- a/crates/tor-keymgr/src/keystore/arti.rs +++ b/crates/tor-keymgr/src/keystore/arti.rs @@ -127,3 +127,92 @@ impl KeyStore for ArtiNativeKeyStore { Ok(true) } } + +#[cfg(test)] +mod tests { + // @@ begin test lint list maintained by maint/add_warning @@ + #![allow(clippy::bool_assert_comparison)] + #![allow(clippy::clone_on_copy)] + #![allow(clippy::dbg_macro)] + #![allow(clippy::print_stderr)] + #![allow(clippy::print_stdout)] + #![allow(clippy::single_char_pattern)] + #![allow(clippy::unwrap_used)] + #![allow(clippy::unchecked_duration_subtraction)] + //! + use super::*; + use crate::{ArtiPath, CTorPath}; + use tempfile::{tempdir, TempDir}; + + struct TestSpecifier; + + impl KeySpecifier for TestSpecifier { + fn arti_path(&self) -> Result { + ArtiPath::new("test-specifier".into()) + } + + fn ctor_path(&self) -> Option { + None + } + } + + fn init_keystore() -> (ArtiNativeKeyStore, TempDir) { + #[cfg(unix)] + use std::os::unix::fs::PermissionsExt; + + let keystore_dir = tempdir().unwrap(); + + #[cfg(unix)] + fs::set_permissions(&keystore_dir, fs::Permissions::from_mode(0o700)).unwrap(); + + let key_store = + ArtiNativeKeyStore::from_path_and_mistrust(&keystore_dir, &Mistrust::default()) + .unwrap(); + + (key_store, keystore_dir) + } + + #[test] + #[cfg(unix)] + fn init_failure_perms() { + use std::os::unix::fs::PermissionsExt; + + let keystore_dir = tempdir().unwrap(); + + // Too permissive + let mode = 0o777; + + fs::set_permissions(&keystore_dir, fs::Permissions::from_mode(mode)).unwrap(); + let err = ArtiNativeKeyStore::from_path_and_mistrust(&keystore_dir, &Mistrust::default()) + .expect_err(&format!("expected failure (perms = {mode:o})")); + + assert_eq!( + err.to_string(), + format!( + "Invalid path or permissions on {} while attempting to Init", + keystore_dir.path().display() + ), + "expected keystore init failure (perms = {:o})", + mode + ); + } + + #[test] + fn key_path_repr() { + let (key_store, _) = init_keystore(); + + assert_eq!( + key_store + .key_path(&TestSpecifier, KeyType::Ed25519Keypair) + .unwrap(), + PathBuf::from("test-specifier.ed25519_private") + ); + + assert_eq!( + key_store + .key_path(&TestSpecifier, KeyType::X25519StaticSecret) + .unwrap(), + PathBuf::from("test-specifier.x25519_private") + ); + } +}