Skip to content

Commit

Permalink
Refactor key type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Apr 8, 2024
1 parent bc859d8 commit 420bfac
Showing 1 changed file with 31 additions and 53 deletions.
84 changes: 31 additions & 53 deletions tests/key_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,41 @@ use rustls_pki_types::PrivateKeyDer;

#[test]
fn test_private_key_from_der() {
fn is_pkcs8(key: &PrivateKeyDer<'_>) -> bool {
matches!(key, PrivateKeyDer::Pkcs8(_))
}
fn is_pkcs1(key: &PrivateKeyDer<'_>) -> bool {
matches!(key, PrivateKeyDer::Pkcs1(_))
}
fn is_sec1(key: &PrivateKeyDer<'_>) -> bool {
matches!(key, PrivateKeyDer::Sec1(_))
const NIST_P256_KEY_SEC1: &[u8] = include_bytes!("../tests/keys/nistp256key.der");
const NIST_P384_KEY_SEC1: &[u8] = include_bytes!("../tests/keys/nistp384key.der");
const NIST_P521_KEY_SEC1: &[u8] = include_bytes!("../tests/keys/nistp521key.der");
for bytes in [NIST_P256_KEY_SEC1, NIST_P384_KEY_SEC1, NIST_P521_KEY_SEC1] {
assert!(matches!(
PrivateKeyDer::try_from(bytes).unwrap(),
PrivateKeyDer::Sec1(_)
));
}

let test_cases: &[(&[u8], fn(&PrivateKeyDer<'_>) -> bool); 11] = &[
(&include_bytes!("../tests/keys/eddsakey.der")[..], is_pkcs8),
(
&include_bytes!("../tests/keys/nistp256key.der")[..],
is_sec1,
),
(
&include_bytes!("../tests/keys/nistp256key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("../tests/keys/nistp384key.der")[..],
is_sec1,
),
(
&include_bytes!("../tests/keys/nistp384key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("../tests/keys/nistp521key.der")[..],
is_sec1,
),
(
&include_bytes!("../tests/keys/nistp521key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("../tests/keys/rsa2048key.pkcs1.der")[..],
is_pkcs1,
),
(
&include_bytes!("../tests/keys/rsa2048key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("../tests/keys/rsa4096key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("../tests/keys/edd25519_v2.der")[..],
is_pkcs8,
),
const RSA_2048_KEY_PKCS1: &[u8] = include_bytes!("../tests/keys/rsa2048key.pkcs1.der");
assert!(matches!(
PrivateKeyDer::try_from(RSA_2048_KEY_PKCS1).unwrap(),
PrivateKeyDer::Pkcs1(_)
));

const NIST_P256_KEY_PKCS8: &[u8] = include_bytes!("../tests/keys/nistp256key.pkcs8.der");
const NIST_P384_KEY_PKCS8: &[u8] = include_bytes!("../tests/keys/nistp384key.pkcs8.der");
const NIST_P521_KEY_PKCS8: &[u8] = include_bytes!("../tests/keys/nistp521key.pkcs8.der");
const RSA_2048_KEY_PKCS8: &[u8] = include_bytes!("../tests/keys/rsa2048key.pkcs8.der");
const RSA_4096_KEY: &[u8] = include_bytes!("../tests/keys/rsa4096key.pkcs8.der");
const ED25519_KEY: &[u8] = include_bytes!("../tests/keys/edd25519_v2.der");
const PKCS8_KEYS: &[&[u8]] = &[
NIST_P256_KEY_PKCS8,
NIST_P384_KEY_PKCS8,
NIST_P521_KEY_PKCS8,
RSA_2048_KEY_PKCS8,
RSA_4096_KEY,
ED25519_KEY,
];

for (key_bytes, expected_check_fn) in test_cases.iter() {
assert!(expected_check_fn(
&PrivateKeyDer::try_from(*key_bytes).unwrap()
for &bytes in PKCS8_KEYS {
assert!(matches!(
PrivateKeyDer::try_from(bytes).unwrap(),
PrivateKeyDer::Pkcs8(_)
));
}
}

0 comments on commit 420bfac

Please sign in to comment.