diff options
Diffstat (limited to 'src/key/tests.rs')
-rw-r--r-- | src/key/tests.rs | 132 |
1 files changed, 131 insertions, 1 deletions
diff --git a/src/key/tests.rs b/src/key/tests.rs index ada3673..ccbf20f 100644 --- a/src/key/tests.rs +++ b/src/key/tests.rs @@ -15,7 +15,7 @@ //////////////////////////////////////////////////////////////////////////////// use super::*; -use crate::{cbor::value::Value, iana, util::expect_err, CborSerializable}; +use crate::{cbor::value::Value, iana, util::expect_err, CborOrdering, CborSerializable}; use alloc::{borrow::ToOwned, string::ToString, vec}; #[test] @@ -742,3 +742,133 @@ fn test_key_builder_core_param_panic() { .param(1, Value::Null) .build(); } + +#[test] +fn test_key_canonicalize() { + struct TestCase { + key_data: &'static str, // hex + rfc7049_key: CoseKey, + rfc8949_key: CoseKey, + rfc7049_data: Option<&'static str>, // hex, `None` indicates same as `key_data` + rfc8949_data: Option<&'static str>, // hex, `None` indicates same as `key_data` + } + let tests = [ + TestCase { + key_data: concat!( + "a2", // 2-map + "01", "01", // 1 (kty) => OKP + "03", "26", // 3 (alg) => -7 + ), + rfc7049_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + ..Default::default() + }, + rfc8949_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + ..Default::default() + }, + rfc7049_data: None, + rfc8949_data: None, + }, + TestCase { + key_data: concat!( + "a2", // 2-map + "03", "26", // 3 (alg) => -7 + "01", "01", // 1 (kty) => OKP + ), + rfc7049_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + ..Default::default() + }, + rfc8949_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + ..Default::default() + }, + rfc7049_data: Some(concat!( + "a2", // 2-map + "01", "01", // 1 (kty) => OKP + "03", "26", // 3 (alg) => -7 + )), + rfc8949_data: Some(concat!( + "a2", // 2-map + "01", "01", // 1 (kty) => OKP + "03", "26", // 3 (alg) => -7 + )), + }, + TestCase { + key_data: concat!( + "a4", // 4-map + "03", "26", // 3 (alg) => -7 + "1904d2", "01", // 1234 => 1 + "01", "01", // 1 (kty) => OKP + "6161", "01", // "a" => 1 + ), + // "a" encodes shorter than 1234, so appears first + rfc7049_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + params: vec![ + (Label::Text("a".to_string()), Value::Integer(1.into())), + (Label::Int(1234), Value::Integer(1.into())), + ], + ..Default::default() + }, + // 1234 encodes with leading byte 0x19, so appears before a tstr + rfc8949_key: CoseKey { + kty: KeyType::Assigned(iana::KeyType::OKP), + alg: Some(Algorithm::Assigned(iana::Algorithm::ES256)), + params: vec![ + (Label::Int(1234), Value::Integer(1.into())), + (Label::Text("a".to_string()), Value::Integer(1.into())), + ], + ..Default::default() + }, + rfc7049_data: Some(concat!( + "a4", // 4-map + "01", "01", // 1 (kty) => OKP + "03", "26", // 3 (alg) => -7 + "6161", "01", // "a" => 1 + "1904d2", "01", // 1234 => 1 + )), + rfc8949_data: Some(concat!( + "a4", // 4-map + "01", "01", // 1 (kty) => OKP + "03", "26", // 3 (alg) => -7 + "1904d2", "01", // 1234 => 1 + "6161", "01", // "a" => 1 + )), + }, + ]; + for testcase in tests { + let key_data = hex::decode(testcase.key_data).unwrap(); + let mut key = CoseKey::from_slice(&key_data) + .unwrap_or_else(|e| panic!("Failed to deserialize {}: {e:?}", testcase.key_data)); + + // Canonicalize according to RFC 7049. + key.canonicalize(CborOrdering::LengthFirstLexicographic); + assert_eq!( + key, testcase.rfc7049_key, + "Mismatch for {}", + testcase.key_data + ); + let got = testcase.rfc7049_key.to_vec().unwrap(); + let want = testcase.rfc7049_data.unwrap_or(testcase.key_data); + assert_eq!(hex::encode(got), want, "Mismatch for {}", testcase.key_data); + + // Canonicalize according to RFC 8949. + key.canonicalize(CborOrdering::Lexicographic); + assert_eq!( + key, testcase.rfc8949_key, + "Mismatch for {}", + testcase.key_data + ); + + let got = testcase.rfc8949_key.to_vec().unwrap(); + let want = testcase.rfc8949_data.unwrap_or(testcase.key_data); + assert_eq!(hex::encode(got), want, "Mismatch for {}", testcase.key_data); + } +} |