Skip to content

Commit

Permalink
Simplify tests for DER errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cpu committed Oct 24, 2023
1 parent 7956538 commit 2eeb292
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/der.rs
Expand Up @@ -426,8 +426,8 @@ mod tests {

// Only 0x00 and 0xff are accepted values
assert_eq!(
Err(Error::BadDer),
optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x42]))
optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x42])).unwrap_err(),
Error::BadDer,
);

// True
Expand All @@ -443,33 +443,35 @@ mod tests {

// Unexpected type
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x01, 0x01, 0xff]))
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x01, 0x01, 0xff])).unwrap_err(),
Error::BadDer,
);

// Unexpected nonexistent type
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x42, 0xff, 0xff]))
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x42, 0xff, 0xff])).unwrap_err(),
Error::BadDer,
);

// Unexpected empty input
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[]))
bit_string_with_no_unused_bits(&mut bytes_reader(&[])).unwrap_err(),
Error::BadDer,
);

// 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]))
.unwrap_err(),
Error::BadDer,
);

// Valid input
assert_eq!(
untrusted::Input::from(&[0x12, 0x34]),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x00, 0x12, 0x34]))
.unwrap()
.as_slice_less_safe(),
&[0x12, 0x34],
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/signed_data.rs
Expand Up @@ -528,10 +528,12 @@ mod tests {
let tsd = parse_test_signed_data(file_contents);
let signature = untrusted::Input::from(&tsd.signature);
assert_eq!(
Err(expected_error),
signature.read_all(Error::BadDer, |input| {
der::bit_string_with_no_unused_bits(input)
})
signature
.read_all(Error::BadDer, |input| {
der::bit_string_with_no_unused_bits(input)
})
.unwrap_err(),
expected_error
);
}

Expand All @@ -549,10 +551,11 @@ mod tests {
let tsd = parse_test_signed_data(file_contents);
let spki = untrusted::Input::from(&tsd.spki);
assert_eq!(
Err(expected_error),
spki.read_all(Error::BadDer, |input| {
der::expect_tag_and_get_value(input, der::Tag::Sequence)
})
.unwrap_err(),
expected_error,
);
}

Expand Down

0 comments on commit 2eeb292

Please sign in to comment.