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

feat: arbitrary feature, base fuzzing #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target
Cargo.lock

# afl fuzz output
out
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ inline = ["text"]
unicode = ["text", "unicode-segmentation", "bstr?/unicode", "bstr?/std"]
bytes = ["bstr", "text"]

arbitrary = ["dep:arbitrary"]

[dev-dependencies]
insta = "1.10.0"
console = "0.15.0"
Expand All @@ -35,6 +37,7 @@ serde_json = "1.0.68"
unicode-segmentation = { version = "1.7.1", optional = true }
bstr = { version = "1.5.0", optional = true, default-features = false }
serde = { version = "1.0.130", optional = true, features = ["derive"] }
arbitrary = { version = "1.1.6", optional = true, features = ["derive"] }

[[example]]
name = "patience"
Expand Down Expand Up @@ -70,3 +73,6 @@ required-features = ["text", "serde"]

[profile.release]
debug = true

[workspace]
members = ["fuzz"]
9 changes: 9 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "similar-fuzz"
version = "0.1.0"
edition = "2021"

[dependencies]
afl = "0.15.10"
arbitrary = "1.3.2"
similar = { path = "../", features = ["arbitrary", "bytes", "unicode"] }
1 change: 1 addition & 0 deletions fuzz/in/char_diff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
  
62 changes: 62 additions & 0 deletions fuzz/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#![allow(clippy::all)]

use afl::fuzz;
use arbitrary::Arbitrary;
use similar::TextDiffConfig;

#[derive(Arbitrary, Debug)]
enum FuzzVariant {
Chars(Vec<u8>, Vec<u8>),
Graphemes(Vec<u8>, Vec<u8>),
Lines(Vec<u8>, Vec<u8>),
UnicodeWords(Vec<u8>, Vec<u8>),
Words(Vec<u8>, Vec<u8>),
Slices(Vec<String>, Vec<String>),
SlicesBytes(Vec<Vec<u8>>, Vec<Vec<u8>>),
}

#[derive(Arbitrary, Debug)]
struct FuzzOptions {
config: TextDiffConfig,
variant: FuzzVariant,
}

fn main() {
fuzz!(|data: FuzzOptions| {
match data.variant {
FuzzVariant::Chars(old, new) => {
let _ = data.config.diff_chars(&old, &new);
}
FuzzVariant::Graphemes(old, new) => {
let _ = data.config.diff_graphemes(&old, &new);
}
FuzzVariant::Lines(old, new) => {
let _ = data.config.diff_lines(&old, &new);
}
FuzzVariant::Slices(old, new) => {
let _ = data.config.diff_slices::<str>(
old.iter().map(|x| &**x).collect::<Vec<_>>().as_slice(),
new.iter().map(|x| &**x).collect::<Vec<_>>().as_slice(),
);
}
FuzzVariant::SlicesBytes(old, new) => {
let _ = data.config.diff_slices::<[u8]>(
old.iter()
.map(|x| x.as_slice())
.collect::<Vec<_>>()
.as_slice(),
new.iter()
.map(|x| x.as_slice())
.collect::<Vec<_>>()
.as_slice(),
);
}
FuzzVariant::UnicodeWords(old, new) => {
let _ = data.config.diff_unicode_words(&old, &new);
}
FuzzVariant::Words(old, new) => {
let _ = data.config.diff_words(&old, &new);
}
};
});
}
20 changes: 20 additions & 0 deletions src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ enum Deadline {
Relative(Duration),
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Deadline {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
// always do absolute for we can catch overflow errors before they happen
Duration::arbitrary(u)
.map(|duration| {
let now = Instant::now();
if bool::arbitrary(u).is_ok() {
now.checked_add(duration)
} else {
now.checked_sub(duration)
}
})
.map(|option| option.ok_or(arbitrary::Error::IncorrectFormat))
.and_then(std::convert::identity)
.map(Deadline::Absolute)
}
}

impl Deadline {
fn into_instant(self) -> Instant {
match self {
Expand All @@ -38,6 +57,7 @@ impl Deadline {
///
/// Requires the `text` feature.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TextDiffConfig {
algorithm: Algorithm,
newline_terminated: Option<bool>,
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::iter::ChangesIter;
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Algorithm {
/// Picks the myers algorithm from [`crate::algorithms::myers`]
Myers,
Expand Down