Skip to content

Commit

Permalink
tests: simplify and rename test_private_key_from_bytes
Browse files Browse the repository at this point in the history
* Rename to `test_private_key_from_der`.
* Rewrite to be table-driven.
  • Loading branch information
cpu committed Mar 21, 2024
1 parent 02d4228 commit 6e19a65
Showing 1 changed file with 46 additions and 48 deletions.
94 changes: 46 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,54 +766,52 @@ mod non_std_tests {
use super::*;

#[test]
fn test_private_key_from_bytes() {
let key = include_bytes!("test_keys/eddsakey.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));

let key = include_bytes!("test_keys/nistp256key.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Sec1(_)));

let key = include_bytes!("test_keys/nistp256key.pkcs8.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));

let key = include_bytes!("test_keys/nistp384key.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();
assert!(matches!(key, PrivateKeyDer::Sec1(_)));

let key = include_bytes!("test_keys/nistp384key.pkcs8.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));

let key = include_bytes!("test_keys/nistp521key.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Sec1(_)));

let key = include_bytes!("test_keys/nistp521key.pkcs8.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));

let key = include_bytes!("test_keys/rsa2048key.pkcs1.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs1(_)));

let key = include_bytes!("test_keys/rsa2048key.pkcs8.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));

let key = include_bytes!("test_keys/rsa4096key.pkcs8.der");
let key = PrivateKeyDer::try_from(&key[..]).unwrap();
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(_))
}

assert!(matches!(key, PrivateKeyDer::Pkcs8(_)));
let test_cases: &[(&[u8], fn(&PrivateKeyDer<'_>) -> bool); 10] = &[
(&include_bytes!("test_keys/eddsakey.der")[..], is_pkcs8),
(&include_bytes!("test_keys/nistp256key.der")[..], is_sec1),
(
&include_bytes!("test_keys/nistp256key.pkcs8.der")[..],
is_pkcs8,
),
(&include_bytes!("test_keys/nistp384key.der")[..], is_sec1),
(
&include_bytes!("test_keys/nistp384key.pkcs8.der")[..],
is_pkcs8,
),
(&include_bytes!("test_keys/nistp521key.der")[..], is_sec1),
(
&include_bytes!("test_keys/nistp521key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("test_keys/rsa2048key.pkcs1.der")[..],
is_pkcs1,
),
(
&include_bytes!("test_keys/rsa2048key.pkcs8.der")[..],
is_pkcs8,
),
(
&include_bytes!("test_keys/rsa4096key.pkcs8.der")[..],
is_pkcs8,
),
];

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

0 comments on commit 6e19a65

Please sign in to comment.