From 15628dbc6c5c00ae6f7183d54feda0986100cbd2 Mon Sep 17 00:00:00 2001 From: Carine Dengler Date: Tue, 7 Nov 2023 16:38:09 +0100 Subject: [PATCH] test: add tests for missing required files in directory --- src/flatfiledirmgr.rs | 8 ++++---- tests/client.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/flatfiledirmgr.rs b/src/flatfiledirmgr.rs index 1363da3..144f024 100644 --- a/src/flatfiledirmgr.rs +++ b/src/flatfiledirmgr.rs @@ -33,13 +33,13 @@ const CHURN_FRACTION: usize = 6; /// Contents of the directory cache. /// CONSENSUS_FILENAME is the name of the file containing the consensus. -pub const CONSENSUS_FILENAME: &'static str = "consensus.txt"; +pub const CONSENSUS_FILENAME: &str = "consensus.txt"; /// MICRODESCRIPTORS_FILENAME is the name of the file containing the microdescriptors. -pub const MICRODESCRIPTORS_FILENAME: &'static str = "microdescriptors.txt"; +pub const MICRODESCRIPTORS_FILENAME: &str = "microdescriptors.txt"; /// CERTIFICATE_FILENAME is the name of the certificate. -pub const CERTIFICATE_FILENAME: &'static str = "certificate.txt"; +pub const CERTIFICATE_FILENAME: &str = "certificate.txt"; /// CHURN_FILENAME is the name of the churn info file. -pub const CHURN_FILENAME: &'static str = "churn.txt"; +pub const CHURN_FILENAME: &str = "churn.txt"; /// A directory manager that loads the directory information from flat files read from the cache /// directory. diff --git a/tests/client.rs b/tests/client.rs index 1b5b730..f8861b5 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,6 +1,10 @@ use http::request::{Builder, Parts}; use http::Request; use lightarti_rest::Client; +use lightarti_rest::CERTIFICATE_FILENAME; +use lightarti_rest::CHURN_FILENAME; +use lightarti_rest::CONSENSUS_FILENAME; +use lightarti_rest::MICRODESCRIPTORS_FILENAME; use url::Url; mod utils; @@ -85,6 +89,39 @@ async fn test_client(req: Request>) { ) } +#[tokio::test] +// Tests that no error is raised if directory is left intact. +// If this tests raises an error, please check if your local copy of directory_cache is up to date. +async fn test_required_files_ok() { + let cache = utils::setup_cache(); + let res = Client::new(cache.path()).await; + assert!(res.is_ok()); +} + +#[tokio::test] +// Tests that an error is raised by FlatFileDirMgr::check_directory if any of the required files +// are missing. The authority.json file is checked for in Client::tor_config. +async fn test_required_files_missing() { + for filename in [ + CONSENSUS_FILENAME, + MICRODESCRIPTORS_FILENAME, + CERTIFICATE_FILENAME, + CHURN_FILENAME, + ] + .iter() + { + let cache = utils::setup_cache(); + let _ = std::fs::remove_file(cache.path().join(filename)); + let res = Client::new(cache.path()).await; + let error = res.err().expect(""); + let root_cause = error.root_cause(); + assert_eq!( + format!("{}", root_cause), + "Corrupt cache: required files missing in cache" + ); + } +} + fn clone_request(header: &Parts, body: &[u8]) -> Request> { let mut builder = Builder::new() .method(header.method.clone())