Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions der/src/asn1/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,35 @@ impl TryFrom<AnyRef<'_>> for bool {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<bool> {
any.try_into()
any.decode_as()
}
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::bool_assert_comparison)]
mod tests {
use crate::{Decode, Encode};
use crate::{Decode, Encode, asn1::AnyRef};

#[test]
fn try_from_any() {
let true_value = AnyRef::from_der(&[0x01, 0x01, 0xFF]).unwrap();
let false_value = AnyRef::from_der(&[0x01, 0x01, 0x00]).unwrap();
assert_eq!(bool::try_from(true_value).unwrap(), true);
assert_eq!(bool::try_from(false_value).unwrap(), false);
}

#[test]
fn try_from_any_rejects_invalid_input() {
for bytes in [
&[0x02, 0x01, 0x00][..], // Wrong tag: INTEGER.
&[0x01, 0x00], // Empty BOOLEAN.
&[0x01, 0x02, 0x00, 0x00], // Overlong BOOLEAN.
&[0x01, 0x01, 0x01], // Noncanonical TRUE.
] {
let any = AnyRef::from_der(bytes).unwrap();
assert!(bool::try_from(any).is_err());
}
}

#[test]
fn decode() {
Expand Down
Loading