1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-08-28 11:36:17 +03:00

support generating random CursorMove with arbitrary

Этот коммит содержится в:
rhysd
2022-07-18 11:43:30 +09:00
родитель 952158f93a
Коммит dbee036ce6
3 изменённых файлов: 25 добавлений и 8 удалений

Просмотреть файл

@@ -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" }

Просмотреть файл

@@ -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::<Result<_>>()?;
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(())