Skip to content

Commit

Permalink
rust: Add unit test for certification validation helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuo-cpp committed Oct 25, 2023
1 parent 49dd508 commit 2d1055f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/rust/cryptography-x509-validation/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ use cryptography_x509::certificate::Certificate;

use crate::ops::CryptoOps;

#[allow(dead_code)]
pub(crate) fn cert_is_self_issued(cert: &Certificate<'_>) -> bool {
cert.issuer() == cert.subject()
}

#[allow(dead_code)]
pub(crate) fn cert_is_self_signed<B: CryptoOps>(cert: &Certificate<'_>, ops: &B) -> bool {
match ops.public_key(cert) {
Ok(pk) => cert_is_self_issued(cert) && ops.verify_signed_by(cert, pk).is_ok(),
Err(_) => false,
}
}

#[cfg(test)]
mod tests {
use crate::ops::tests::{cert, v1_cert_pem, NullOps};

use super::{cert_is_self_issued, cert_is_self_signed};

#[test]
fn test_certificate_validation_helpers() {
let cert_pem = v1_cert_pem();
let cert = cert(&cert_pem);
let ops = NullOps {};

assert!(!cert_is_self_issued(&cert));
assert!(!cert_is_self_signed(&cert, &ops));
}
}

0 comments on commit 2d1055f

Please sign in to comment.