Skip to content

Commit

Permalink
feat: implement FakeCertificaterRetriever for CertificateRetriever trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed Oct 22, 2024
1 parent 7a41319 commit fb8d950
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ use thiserror::Error;

use crate::{entities::Certificate, StdError};

#[cfg(test)]
use mockall::automock;

/// [CertificateRetriever] related errors.
#[derive(Debug, Error)]
#[error("Error when retrieving certificate")]
pub struct CertificateRetrieverError(#[source] pub StdError);

/// CertificateRetriever is in charge of retrieving a [Certificate] given its hash
#[cfg_attr(test, automock)]
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait CertificateRetriever: Sync + Send {
Expand Down
77 changes: 77 additions & 0 deletions mithril-common/src/certificate_chain/fake_certificate_retriever.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! A module used for a fake implementation of a certificate chain retriever
//!

use anyhow::anyhow;
use async_trait::async_trait;
use std::collections::HashMap;
use tokio::sync::RwLock;

use crate::entities::Certificate;

use super::{CertificateRetriever, CertificateRetrieverError};

/// A fake [CertificateRetriever] that returns a [Certificate] given its hash
pub struct FakeCertificaterRetriever {
certificates_map: RwLock<HashMap<String, Certificate>>,
}

impl FakeCertificaterRetriever {
/// Create a new [FakeCertificaterRetriever]
pub fn from_certificates(certificates: &[Certificate]) -> Self {
let certificates_map = certificates
.iter()
.map(|certificate| (certificate.hash.clone(), certificate.clone()))
.collect::<HashMap<_, _>>();
let certificates_map = RwLock::new(certificates_map);

Self { certificates_map }
}
}

#[async_trait]
impl CertificateRetriever for FakeCertificaterRetriever {
async fn get_certificate_details(
&self,
certificate_hash: &str,
) -> Result<Certificate, CertificateRetrieverError> {
let certificates_map = self.certificates_map.read().await;
certificates_map
.get(certificate_hash)
.cloned()
.ok_or_else(|| CertificateRetrieverError(anyhow!("Certificate not found")))
}
}

#[cfg(test)]
mod tests {
use crate::test_utils::fake_data;

use super::*;

#[tokio::test]
async fn fake_certificate_retriever_retrieves_existing_certificate() {
let certificate = fake_data::certificate("certificate-hash-123".to_string());
let certificate_hash = certificate.hash.clone();
let certificate_retriever =
FakeCertificaterRetriever::from_certificates(&[certificate.clone()]);

let retrieved_certificate = certificate_retriever
.get_certificate_details(&certificate_hash)
.await
.expect("Should retrieve certificate");

assert_eq!(retrieved_certificate, certificate);
}

#[tokio::test]
async fn test_fake_certificate_fails_retrieving_unknow_certificate() {
let certificate = fake_data::certificate("certificate-hash-123".to_string());
let certificate_retriever = FakeCertificaterRetriever::from_certificates(&[certificate]);

let retrieved_certificate = certificate_retriever
.get_certificate_details("certificate-hash-not-found")
.await;

retrieved_certificate.expect_err("get_certificate_details shoudl fail");
}
}
7 changes: 7 additions & 0 deletions mithril-common/src/certificate_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
mod certificate_genesis;
mod certificate_retriever;
mod certificate_verifier;
cfg_test_tools! {
mod fake_certificate_retriever;
}

pub use certificate_genesis::CertificateGenesisProducer;
pub use certificate_retriever::{CertificateRetriever, CertificateRetrieverError};
pub use certificate_verifier::{
CertificateVerifier, CertificateVerifierError, MithrilCertificateVerifier,
};

cfg_test_tools! {
pub use fake_certificate_retriever::FakeCertificaterRetriever;
}

0 comments on commit fb8d950

Please sign in to comment.