Skip to content

Commit 45cfffc

Browse files
committed
tests: add Vec fuzz harness
Adds a basic fuzzing test harness using `cargo-fuzz` to run randomized tests against the `Vec` type.
1 parent 681e7ab commit 45cfffc

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage

fuzz/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "heapless-fuzz"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
libfuzzer-sys = "0.4"
12+
13+
[dependencies.heapless]
14+
path = ".."
15+
16+
[[bin]]
17+
name = "fuzz_vec"
18+
path = "fuzz_targets/fuzz_vec.rs"
19+
test = false
20+
doc = false
21+
bench = false

fuzz/fuzz_targets/fuzz_vec.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
5+
use heapless::Vec;
6+
7+
fn test_vec<const N: usize>(data: &[u8]) {
8+
if let Ok(vec) = Vec::<u8, N>::from_slice(data) {
9+
assert_eq!(vec.as_slice(), data);
10+
}
11+
}
12+
13+
fuzz_target!(|data: &[u8]| {
14+
match data.len() {
15+
0 => (),
16+
len if len <= 16 => test_vec::<16>(data),
17+
len if len <= 32 => test_vec::<32>(data),
18+
len if len <= 64 => test_vec::<64>(data),
19+
len if len <= 128 => test_vec::<128>(data),
20+
len if len <= 256 => test_vec::<256>(data),
21+
len if len <= 512 => test_vec::<512>(data),
22+
len if len <= 1024 => test_vec::<1024>(data),
23+
len if len <= 2048 => test_vec::<2048>(data),
24+
len if len <= 4096 => test_vec::<4096>(data),
25+
_ => (),
26+
}
27+
});

0 commit comments

Comments
 (0)