From dbee036ce654ca3259c7033b6e7e9a9a1050c137 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 18 Jul 2022 11:43:30 +0900 Subject: [PATCH] support generating random `CursorMove` with `arbitrary` --- fuzz/Cargo.toml | 2 +- fuzz/fuzz_targets/edit.rs | 28 +++++++++++++++++++++------- src/cursor.rs | 3 +++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index d726232..6f2c0ca 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -10,7 +10,7 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" -arbitrary = "1" +arbitrary = { version = "1", features = ["derive"] } tui-textarea = { path = "..", features = ["search", "arbitrary"] } tui-textarea-bench = { path = "../bench" } diff --git a/fuzz/fuzz_targets/edit.rs b/fuzz/fuzz_targets/edit.rs index 92a5fe3..2300b09 100644 --- a/fuzz/fuzz_targets/edit.rs +++ b/fuzz/fuzz_targets/edit.rs @@ -2,21 +2,35 @@ use arbitrary::{Arbitrary, Result, Unstructured}; use libfuzzer_sys::fuzz_target; -use std::iter; use std::str; -use tui_textarea::{Input, TextArea}; +use tui_textarea::{CursorMove, Input, TextArea}; use tui_textarea_bench::{dummy_terminal, TerminalExt}; +#[derive(Arbitrary)] +enum RandomInput { + Input(Input), + Cursor(CursorMove), +} + +impl RandomInput { + fn apply(self, t: &mut TextArea<'_>) { + match self { + Self::Input(input) => { + t.input(input); + } + Self::Cursor(m) => t.move_cursor(m), + } + } +} + fn fuzz(data: &[u8]) -> Result<()> { let mut term = dummy_terminal(); let mut data = Unstructured::new(data); - let inputs: Vec<_> = iter::repeat_with(|| Input::arbitrary(&mut data)) - .take(100) - .collect::>()?; let text = <&str>::arbitrary(&mut data)?; let mut textarea = TextArea::from(text.lines()); - for input in inputs { - textarea.input(input); + for _ in 0..100 { + let input = RandomInput::arbitrary(&mut data)?; + input.apply(&mut textarea); term.draw_textarea(&mut textarea); } Ok(()) diff --git a/src/cursor.rs b/src/cursor.rs index a25430e..260a949 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,8 +1,11 @@ use crate::word::{find_word_start_backward, find_word_start_forward}; +#[cfg(feature = "arbitrary")] +use arbitrary::Arbitrary; use std::cmp; /// Specify how to move the cursor. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] pub enum CursorMove { /// Move cursor forward by one character. When the cursor is at the end of line, it moves to the head of next line. /// ```