Skip to content

Commit

Permalink
test(s2n-codec): add autogenerated Bolero harnesses (#2306)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvick32 committed Sep 3, 2024
1 parent 1ff3a9c commit d8be30e
Show file tree
Hide file tree
Showing 4 changed files with 564 additions and 0 deletions.
38 changes: 38 additions & 0 deletions common/s2n-codec/src/decoder/checked_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@ pub struct CheckedRange {
original_ptr: *const u8,
}

#[cfg(any(test, feature = "generator"))]
use bolero_generator::*;

#[cfg(test)]
impl bolero::TypeGenerator for CheckedRange {
fn generate<D: bolero::Driver>(driver: &mut D) -> Option<Self> {
let start = gen::<usize>().generate(driver)?;
let end = (start..).generate(driver)?;
Some(CheckedRange::new(start, end, core::ptr::null()))
}
}

impl CheckedRange {
#[inline]
pub(crate) fn new(start: usize, end: usize, original_ptr: *const u8) -> Self {
debug_assert!(
end >= start,
"end: {end} must be greater than or equal to start: {start}",
);
#[cfg(not(all(debug_assertions, feature = "checked_range_unsafe")))]
let _ = original_ptr;

Expand Down Expand Up @@ -76,3 +92,25 @@ impl fmt::Debug for CheckedRange {
write!(f, "{}..{}", self.start, self.end)
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
#[cfg_attr(kani, kani::proof)]
fn checked_range_len() {
bolero::check!()
.with_type()
.for_each(|callee: &CheckedRange| Some(callee.len()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn checked_range_is_empty() {
bolero::check!()
.with_type()
.for_each(|callee: &CheckedRange| Some(callee.is_empty()));
}
}
57 changes: 57 additions & 0 deletions common/s2n-codec/src/encoder/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::encoder::Encoder;

/// Estimates the `encoding_size` of an `EncoderValue`
#[cfg_attr(test, derive(Clone, Debug, bolero::TypeGenerator))]
pub struct EncoderLenEstimator {
capacity: usize,
len: usize,
Expand Down Expand Up @@ -58,3 +59,59 @@ impl Encoder for EncoderLenEstimator {
self.len
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
#[cfg_attr(kani, kani::proof)]
fn encoder_len_estimator_new() {
bolero::check!()
.with_type()
.cloned()
.for_each(|capacity: usize| Some(EncoderLenEstimator::new(capacity)));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn encoder_len_estimator_overflowed() {
bolero::check!()
.with_type()
.for_each(|callee: &EncoderLenEstimator| Some(callee.overflowed()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn encoder_len_estimator_write_repeated() {
bolero::check!()
.with_type()
.cloned()
.filter(|(callee, count, _): &(EncoderLenEstimator, usize, u8)| {
count <= &(usize::MAX - callee.len)
})
.for_each(
|(mut callee, count, value): (EncoderLenEstimator, usize, u8)| {
callee.write_repeated(count, value);
Some(())
},
);
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn encoder_len_estimator_capacity() {
bolero::check!()
.with_type()
.for_each(|callee: &EncoderLenEstimator| Some(callee.capacity()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn encoder_len_estimator_len() {
bolero::check!()
.with_type()
.for_each(|callee: &EncoderLenEstimator| Some(callee.len()));
}
}
159 changes: 159 additions & 0 deletions common/s2n-codec/src/encoder/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,162 @@ impl EncoderValue for &bytes::Bytes {
self.len()
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
#[cfg_attr(kani, kani::proof)]
fn u8_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u8| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i8_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i8| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u16_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u16| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i16_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i16| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u24_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u24| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i24_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i24| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u32_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u32| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i32_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i32| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u48_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u48| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i48_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i48| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u64_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u64| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i64_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i64| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn u128_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: u128| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn i128_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: i128| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn f32_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: f32| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn f64_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: f64| Some(callee.encoding_size()));
}

#[test]
#[cfg_attr(kani, kani::proof)]
fn empty_encoding_size() {
bolero::check!()
.with_type()
.cloned()
.for_each(|callee: ()| Some(callee.encoding_size()));
}
}
Loading

0 comments on commit d8be30e

Please sign in to comment.