From 8608d6d351eac56f8bac4bd6066189c37a043d6d Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Wed, 14 Feb 2024 10:28:57 +0000 Subject: [PATCH] Add mock singing backend for tests --- lib/src/signing.rs | 54 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/lib/src/signing.rs b/lib/src/signing.rs index ded20a7b8a..d46e4f84c4 100644 --- a/lib/src/signing.rs +++ b/lib/src/signing.rs @@ -111,6 +111,33 @@ pub trait SigningBackend: Debug + Send + Sync { fn verify(&self, data: &[u8], signature: &[u8]) -> SignResult; } +#[derive(Debug)] +#[allow(missing_docs)] +pub struct MockSigningBackend {} + +impl SigningBackend for MockSigningBackend { + fn name(&self) -> &str { + "mock" + } + fn can_read(&self, signature: &[u8]) -> bool { + signature.starts_with(b"-----BEGIN MOCK SIGNATURE-----") + } + fn sign(&self, _: &[u8], _: Option<&str>) -> SignResult> { + Ok(r#"-----BEGIN MOCK SIGNATURE----- +mock signature +-----END MOCK SIGNATURE----- +"# + .into()) + } + fn verify(&self, _: &[u8], signature: &[u8]) -> SignResult { + if signature.starts_with(b"-----BEGIN MOCK SIGNATURE-----") { + Ok(Verification::new(SigStatus::Good, None, None)) + } else { + Ok(Verification::new(SigStatus::Bad, None, None)) + } + } +} + /// An error type for the signing/verifying operations #[derive(Debug, Error)] pub enum SignError { @@ -182,16 +209,23 @@ impl Signer { // Box::new(X509Backend::from_settings(settings)?) as Box, ]; - let main_backend = settings - .signing_backend() - .map(|backend| { - backends - .iter() - .position(|b| b.name() == backend) - .map(|i| backends.remove(i)) - .ok_or(SignInitError::UnknownBackend(backend)) - }) - .transpose()?; + let signing_backend = settings.signing_backend(); + + // This mock signing backend is only really intended to be used for testing + let main_backend = if Some("mock".into()) == signing_backend { + let mock_backend = Box::new(MockSigningBackend {}) as Box; + Some(mock_backend) + } else { + signing_backend + .map(|backend| { + backends + .iter() + .position(|b| b.name() == backend) + .map(|i| backends.remove(i)) + .ok_or(SignInitError::UnknownBackend(backend)) + }) + .transpose()? + }; let show_signatures = settings .config()