From 706f20f2d79d2a30c21a2bd7887389a541b2c62a Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 22 Apr 2021 15:39:57 -0400 Subject: [PATCH 1/4] Add test coverage for der.rs --- src/der.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/der.rs b/src/der.rs index 43d0847d..dd644ae7 100644 --- a/src/der.rs +++ b/src/der.rs @@ -171,3 +171,44 @@ macro_rules! oid { [(40 * $first) + $second, $( $tail ),*] ) } + +#[cfg(test)] +mod tests { + fn bytes_reader(bytes: &[u8]) -> untrusted::Reader { + let input = untrusted::Input::from(bytes); + let reader = untrusted::Reader::new(input); + return reader; + } + + #[test] + fn test_optional_boolean() { + use super::{optional_boolean, Error}; + + // Empty input results in false + assert_eq!(false, optional_boolean(&mut bytes_reader(&[])).unwrap()); + + // Optional, so another data type results in false + assert_eq!( + false, + optional_boolean(&mut bytes_reader(&[0x05, 0x00])).unwrap() + ); + + // Only 0x00 and 0xff are accepted values + assert_eq!( + Err(Error::BadDer), + optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x42])) + ); + + // True + assert_eq!( + true, + optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0xff])).unwrap() + ); + + // False + assert_eq!( + false, + optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x00])).unwrap() + ); + } +} From db24fe81531e7ee3447f4331bdb3831be4900f4a Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 22 Apr 2021 15:59:21 -0400 Subject: [PATCH 2/4] Add test coverage for der.rs --- src/der.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/der.rs b/src/der.rs index dd644ae7..3cd10093 100644 --- a/src/der.rs +++ b/src/der.rs @@ -175,9 +175,7 @@ macro_rules! oid { #[cfg(test)] mod tests { fn bytes_reader(bytes: &[u8]) -> untrusted::Reader { - let input = untrusted::Input::from(bytes); - let reader = untrusted::Reader::new(input); - return reader; + return untrusted::Reader::new(untrusted::Input::from(bytes)); } #[test] @@ -211,4 +209,7 @@ mod tests { optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x00])).unwrap() ); } + + #[test] + fn test_bit_string_with_no_unused_bits() {} } From 3c1abded710ec976024d0f3c9ffa0a3ba45891e0 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 22 Apr 2021 17:11:49 -0400 Subject: [PATCH 3/4] Add test coverage for der.rs --- src/der.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/der.rs b/src/der.rs index 3cd10093..f9d6c362 100644 --- a/src/der.rs +++ b/src/der.rs @@ -211,5 +211,38 @@ mod tests { } #[test] - fn test_bit_string_with_no_unused_bits() {} + fn test_bit_string_with_no_unused_bits() { + use super::{bit_string_with_no_unused_bits, Error}; + + // Unexpected type + assert_eq!( + Err(Error::BadDer), + bit_string_with_no_unused_bits(&mut bytes_reader(&[0x01, 0x01, 0xff])) + ); + + // Unexpected nonexistent type + assert_eq!( + Err(Error::BadDer), + bit_string_with_no_unused_bits(&mut bytes_reader(&[0x42, 0xff, 0xff])) + ); + + // Unexpected empty input + assert_eq!( + Err(Error::BadDer), + bit_string_with_no_unused_bits(&mut bytes_reader(&[])) + ); + + // Valid input with non-zero unused bits + assert_eq!( + Err(Error::BadDer), + bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x04, 0x12, 0x34])) + ); + + // Valid input + assert_eq!( + untrusted::Input::from(&[0x12, 0x34]), + bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x00, 0x12, 0x34])).unwrap() + ); + } } + From e4d2683da84b26cf094cd9f799d5ad37898c1bd1 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 22 Apr 2021 17:12:15 -0400 Subject: [PATCH 4/4] Add test coverage for der.rs --- src/der.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/der.rs b/src/der.rs index f9d6c362..f7c89327 100644 --- a/src/der.rs +++ b/src/der.rs @@ -241,8 +241,8 @@ mod tests { // Valid input assert_eq!( untrusted::Input::from(&[0x12, 0x34]), - bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x00, 0x12, 0x34])).unwrap() + bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x00, 0x12, 0x34])) + .unwrap() ); } } -