From 6e19a65ccf68a1f08c7bd32f9307a833a7a223b8 Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Thu, 21 Mar 2024 12:36:07 -0400 Subject: [PATCH] tests: simplify and rename test_private_key_from_bytes * Rename to `test_private_key_from_der`. * Rewrite to be table-driven. --- src/lib.rs | 94 ++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 604e7a0..ad136ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() + )); + } } }