-
Notifications
You must be signed in to change notification settings - Fork 739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding tests and fuzzing to der.rs (using cargo-fuzz) #1254
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
target | ||
corpus | ||
artifacts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
[package] | ||
name = "ring-fuzz" | ||
version = "0.0.0" | ||
authors = ["Automatically generated"] | ||
publish = false | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
untrusted = "0.7.1" | ||
|
||
[dependencies.ring] | ||
path = ".." | ||
|
||
# Prevent this from interfering with workspaces | ||
[workspace] | ||
members = ["."] | ||
|
||
[[bin]] | ||
name = "fuzz_bit_string_with_no_unused_bits" | ||
path = "fuzz_targets/fuzz_bit_string_with_no_unused_bits.rs" | ||
test = false | ||
doc = false | ||
|
||
[[bin]] | ||
name = "read_tag_and_get_value" | ||
path = "fuzz_targets/read_tag_and_get_value.rs" | ||
test = false | ||
doc = false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let mut reader = untrusted::Reader::new(untrusted::Input::from(data)); | ||
let _ = ring::io::der::bit_string_with_no_unused_bits(&mut reader); | ||
}); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let mut reader = untrusted::Reader::new(untrusted::Input::from(data)); | ||
let _ = ring::io::der::read_tag_and_get_value(&mut reader); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,4 +308,46 @@ mod tests { | |
}); | ||
} | ||
} | ||
|
||
fn bytes_reader(bytes: &[u8]) -> untrusted::Reader { | ||
return untrusted::Reader::new(untrusted::Input::from(bytes)); | ||
} | ||
|
||
#[test] | ||
fn test_bit_string_with_no_unused_bits() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independently of the idea of sharing the test logic between the fuzzer and the known-answer tests, it is really important that tests in ring are written as parameterized tests with minimal boilerplate.
|
||
// Unexpected type | ||
assert_eq!( | ||
Err(error::Unspecified), | ||
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x01, 0x01, 0xff])) | ||
); | ||
|
||
// Unexpected nonexistent type | ||
assert_eq!( | ||
Err(error::Unspecified), | ||
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x42, 0xff, 0xff])) | ||
); | ||
|
||
// Unexpected empty input | ||
assert_eq!( | ||
Err(error::Unspecified), | ||
bit_string_with_no_unused_bits(&mut bytes_reader(&[])) | ||
); | ||
|
||
// Valid input with non-zero unused bits | ||
assert_eq!( | ||
Err(error::Unspecified), | ||
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() | ||
); | ||
} | ||
|
||
#[test] | ||
fn fuzz_bit_string_with_no_unused_bits() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand this part. Maybe just incomplete? |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I would expect here is that the thing inside
fuzz_target!
would be a function that can be shared between fuzz tests and unit known-answer tests.Also, I want to clarify the goal here: The purpose of this test is to verify that
bit_string_with_no_unused_bits
never panics or crashes, right?