Skip to content

Commit

Permalink
Add mock signing backend for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Feb 17, 2024
1 parent 9d3d16b commit 4ac383b
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions lib/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ pub trait SigningBackend: Debug + Send + Sync {
fn verify(&self, data: &[u8], signature: &[u8]) -> SignResult<Verification>;
}

#[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<Vec<u8>> {
Ok(r#"-----BEGIN MOCK SIGNATURE-----
mock signature
-----END MOCK SIGNATURE-----
"#
.into())
}
fn verify(&self, _: &[u8], signature: &[u8]) -> SignResult<Verification> {
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 {
Expand Down Expand Up @@ -182,16 +209,23 @@ impl Signer {
// Box::new(X509Backend::from_settings(settings)?) as Box<dyn SigningBackend>,
];

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<dyn SigningBackend>;
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()
Expand Down

0 comments on commit 4ac383b

Please sign in to comment.