Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target
corpus
artifacts
33 changes: 33 additions & 0 deletions fuzz/Cargo.toml
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
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/fuzz_bit_string_with_no_unused_bits.rs
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);
});
Copy link
Owner

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?

7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/read_tag_and_get_value.rs
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

});
42 changes: 42 additions & 0 deletions src/io/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Owner

Choose a reason for hiding this comment

The 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.

    use bit_string_with_no_unused_bits_tests::TestCase;
    const TEST_CASES: &[TestCase] = &[
             // the comment for the test case.
            TestCase { 
                input: &[0x01, 0x01, 0xff],
                result: Err(error::Unspecified),
            },
            ...
        ];
        TEST_CASES.for_each(|TestCase { input, result, .. }| {
           let mut reader = ...;
           assert_eq!(bit_string_with_no_unused_bits(&mut reader), result);
        });
}

// 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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this part. Maybe just incomplete?

}
}