From 689064e4c4dcfa8ffb37104cfea7e07b808ea2a5 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Tue, 28 Feb 2023 13:52:11 +0100 Subject: [PATCH] Normalize Certificate Serial Number --- packages/hurl/src/http/certificate.rs | 47 +++++++++++++++++++++++++-- packages/hurl/tests/libcurl.rs | 11 ++----- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/packages/hurl/src/http/certificate.rs b/packages/hurl/src/http/certificate.rs index 81e20818b..6e09ae8c1 100644 --- a/packages/hurl/src/http/certificate.rs +++ b/packages/hurl/src/http/certificate.rs @@ -91,10 +91,27 @@ fn parse_date(value: &str) -> Result, String> { } fn parse_serial_number(attributes: &HashMap) -> Result { - attributes + let value = attributes .get("serial number") .cloned() - .ok_or(format!("Missing serial number attribute in {attributes:?}")) + .ok_or(format!("Missing serial number attribute in {attributes:?}"))?; + let normalized_value = if value.contains(':') { + value + .split(':') + .filter(|e| !e.is_empty()) + .collect::>() + .join(":") + } else { + value + .chars() + .collect::>() + .chunks(2) + .map(|c| c.iter().collect::()) + .collect::>() + .join(":") + }; + + Ok(normalized_value) } fn parse_attributes(data: &Vec) -> HashMap { @@ -149,6 +166,29 @@ mod tests { ) } + #[test] + fn test_parse_serial_number() { + let mut attributes = HashMap::new(); + attributes.insert( + "serial number".to_string(), + "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0:".to_string(), + ); + assert_eq!( + parse_serial_number(&attributes).unwrap(), + "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string() + ); + + let mut attributes = HashMap::new(); + attributes.insert( + "serial number".to_string(), + "1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string(), + ); + assert_eq!( + parse_serial_number(&attributes).unwrap(), + "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string() + ) + } + #[test] fn test_try_from() { assert_eq!( @@ -174,7 +214,8 @@ mod tests { expire_date: chrono::DateTime::parse_from_rfc2822("Thu, 30 Oct 2025 08:29:52 GMT") .unwrap() .with_timezone(&chrono::Utc), - serial_number: "1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string() + serial_number: "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0" + .to_string() } ); assert_eq!( diff --git a/packages/hurl/tests/libcurl.rs b/packages/hurl/tests/libcurl.rs index 76a1946ae..b647797cf 100644 --- a/packages/hurl/tests/libcurl.rs +++ b/packages/hurl/tests/libcurl.rs @@ -643,14 +643,9 @@ fn test_cacert() { .with_timezone(&chrono::Utc) ); - let serial_number = certificate.serial_number; - let serial_numbers = [ - "1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string(), - "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0:".to_string(), - ]; - assert!( - serial_numbers.contains(&serial_number), - "actual serial_number is {serial_number}" + assert_eq!( + certificate.serial_number, + "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string() ); }