-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fuzz tests with fully arbitrary fragments
The new `wrap_first_fit.rs` and `wrap_optimal_fit.rs` fuzz tests generate completely random fragments with arbitrary widths. They then check that the fragments can be wrapped without overflow. The tests currently fail in less than a second and triggers the overflows mentioned in #247 and #416.
- Loading branch information
Showing
3 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![no_main] | ||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
use textwrap::core; | ||
use textwrap::wrap_algorithms::wrap_first_fit; | ||
|
||
#[derive(Arbitrary, Debug, Eq, PartialEq)] | ||
struct Word { | ||
width: usize, | ||
whitespace_width: usize, | ||
penalty_width: usize, | ||
} | ||
|
||
#[rustfmt::skip] | ||
impl core::Fragment for Word { | ||
fn width(&self) -> usize { self.width } | ||
fn whitespace_width(&self) -> usize { self.whitespace_width } | ||
fn penalty_width(&self) -> usize { self.penalty_width } | ||
} | ||
|
||
fuzz_target!(|input: (usize, Vec<Word>)| { | ||
let width = input.0; | ||
let words = input.1; | ||
let _ = wrap_first_fit(&words, &[width]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![no_main] | ||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
use textwrap::core; | ||
use textwrap::wrap_algorithms::{wrap_optimal_fit, OptimalFit}; | ||
|
||
#[derive(Arbitrary, Debug)] | ||
struct Penalties { | ||
nline_penalty: i32, | ||
overflow_penalty: i32, | ||
short_last_line_fraction: usize, | ||
short_last_line_penalty: i32, | ||
hyphen_penalty: i32, | ||
} | ||
|
||
impl Into<OptimalFit> for Penalties { | ||
fn into(self) -> OptimalFit { | ||
OptimalFit { | ||
nline_penalty: self.nline_penalty, | ||
overflow_penalty: self.overflow_penalty, | ||
short_last_line_fraction: std::cmp::max(1, self.short_last_line_fraction), | ||
short_last_line_penalty: self.short_last_line_penalty, | ||
hyphen_penalty: self.hyphen_penalty, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Arbitrary, Debug, Eq, PartialEq)] | ||
struct Word { | ||
width: usize, | ||
whitespace_width: usize, | ||
penalty_width: usize, | ||
} | ||
|
||
#[rustfmt::skip] | ||
impl core::Fragment for Word { | ||
fn width(&self) -> usize { self.width } | ||
fn whitespace_width(&self) -> usize { self.whitespace_width } | ||
fn penalty_width(&self) -> usize { self.penalty_width } | ||
} | ||
|
||
fuzz_target!(|input: (usize, Vec<Word>, Penalties)| { | ||
let width = input.0; | ||
let words = input.1; | ||
let penalties = input.2.into(); | ||
let _ = wrap_optimal_fit(&words, &[width], &penalties); | ||
}); |