From ac04feb5ea7f140f58900d76a4dcddb1622e878f Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 16 Jul 2022 21:40:05 +0900 Subject: [PATCH] add `arbitrary` crate as optional dependency to avoid converting `Input` in fuzz test for making arbitrary inputs --- Cargo.toml | 1 + fuzz/Cargo.lock | 40 ++++++++++++++++++++++-- fuzz/Cargo.toml | 6 ++-- fuzz/fuzz_targets/edit.rs | 66 +++------------------------------------ src/input.rs | 4 +++ 5 files changed, 48 insertions(+), 69 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 57a2a94..78f620e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ crossterm = { version = "0.23", optional = true } regex = { version = "1", optional = true } termion = { version = "1.5", optional = true } tui = { version = "0.18", default-features = false } +arbitrary = { version = "1", features = ["derive"], optional = true } [[example]] name = "minimal" diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index f77c3c2..94acf1f 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + [[package]] name = "arbitrary" version = "1.1.2" @@ -113,6 +122,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "mio" version = "0.8.3" @@ -181,6 +196,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + [[package]] name = "scopeguard" version = "1.1.0" @@ -219,9 +251,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" [[package]] name = "syn" @@ -249,9 +281,11 @@ dependencies = [ [[package]] name = "tui-textarea" -version = "0.0.0" +version = "0.1.4" dependencies = [ + "arbitrary", "crossterm", + "regex", "tui", ] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index f9d338e..441de4b 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -10,10 +10,8 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" -arbitrary = { version = "1", features = ["derive"] } - -[dependencies.tui-textarea] -path = ".." +arbitrary = "1" +tui-textarea = { path = "..", features = ["search", "arbitrary"] } # Prevent this from interfering with workspaces [workspace] diff --git a/fuzz/fuzz_targets/edit.rs b/fuzz/fuzz_targets/edit.rs index 3a38249..76347fd 100644 --- a/fuzz/fuzz_targets/edit.rs +++ b/fuzz/fuzz_targets/edit.rs @@ -4,77 +4,19 @@ use arbitrary::{Arbitrary, Result, Unstructured}; use libfuzzer_sys::fuzz_target; use std::iter; use std::str; -use tui_textarea::{Input, Key, TextArea}; - -macro_rules! arbitrary_key_enum { - ($($p:ident$(($x:ident))?,)+) => { - - #[derive(Arbitrary)] - enum ArbitraryKey { - $( - $p$(($x))?, - )+ - } - - impl From for Key { - fn from(k: ArbitraryKey) -> Key { - match k { - $( - ArbitraryKey::$p$(($x))? => Key::$p$(($x))?, - )+ - } - } - } - } -} - -arbitrary_key_enum!( - Char(char), - F(u8), - Backspace, - Enter, - Left, - Right, - Up, - Down, - Tab, - Delete, - Home, - End, - PageUp, - PageDown, - Esc, - Null, -); - -#[derive(Arbitrary)] -struct ArbitraryInput { - key: ArbitraryKey, - ctrl: bool, - alt: bool, -} - -impl From for Input { - fn from(i: ArbitraryInput) -> Input { - Input { - key: i.key.into(), - ctrl: i.ctrl, - alt: i.alt, - } - } -} +use tui_textarea::{Input, TextArea}; fn fuzz(data: &[u8]) -> Result<()> { let mut u = Unstructured::new(data); - let inputs: Vec<_> = iter::repeat_with(|| ArbitraryInput::arbitrary(&mut u)) - .take(10) + let inputs: Vec<_> = iter::repeat_with(|| Input::arbitrary(&mut u)) + .take(100) .collect::>()?; let text = <&str>::arbitrary(&mut u)?; let mut textarea = TextArea::from(text.lines()); for input in inputs { textarea.input(input); + let _ = textarea.widget(); } - let _ = textarea.widget(); Ok(()) } diff --git a/src/input.rs b/src/input.rs index 3fc3875..60b9c7b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "arbitrary")] +use arbitrary::Arbitrary; #[cfg(feature = "crossterm")] use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; #[cfg(feature = "termion")] @@ -6,6 +8,7 @@ use termion::event::{Event as TerimonEvent, Key as TermionKey}; /// Backend-agnostic key input kind. #[non_exhaustive] #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] pub enum Key { /// Normal letter key input. Char(char), @@ -62,6 +65,7 @@ pub enum Key { /// }); /// ``` #[derive(Debug, Clone)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] pub struct Input { /// Typed key. pub key: Key,