Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
test: add tests for missing required files in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Nov 7, 2023
1 parent 37b21d9 commit 15628db
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/flatfiledirmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -85,6 +89,39 @@ async fn test_client(req: Request<Vec<u8>>) {
)
}

#[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<Vec<u8>> {
let mut builder = Builder::new()
.method(header.method.clone())
Expand Down

0 comments on commit 15628db

Please sign in to comment.