Refactor parser fuzz testing

This commit is contained in:
pcpthm 2019-03-22 02:05:12 +09:00
parent 51323a852a
commit e734190c24
5 changed files with 23 additions and 16 deletions

View File

@ -4,14 +4,14 @@ name = "ra_syntax-fuzz"
version = "0.0.1"
authors = ["rust-analyzer developers"]
publish = false
edition = "2018"
[package.metadata]
cargo-fuzz = true
[dependencies.ra_syntax]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
[dependencies]
ra_syntax = { path = ".." }
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
# Prevent this from interfering with workspaces
[workspace]

View File

@ -1,9 +1,9 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ra_syntax;
use libfuzzer_sys::fuzz_target;
use ra_syntax::fuzz::check_parser;
fuzz_target!(|data: &[u8]| {
if let Ok(text) = std::str::from_utf8(data) {
ra_syntax::check_fuzz_invariants(text)
check_parser(text)
}
});

View File

@ -0,0 +1,12 @@
use crate::{SourceFile, validation, AstNode};
fn check_file_invariants(file: &SourceFile) {
let root = file.syntax();
validation::validate_block_structure(root);
let _ = file.errors();
}
pub fn check_parser(text: &str) {
let file = SourceFile::parse(text);
check_file_invariants(&file);
}

View File

@ -29,6 +29,8 @@ mod ptr;
pub mod algo;
pub mod ast;
#[doc(hidden)]
pub mod fuzz;
pub use rowan::{SmolStr, TextRange, TextUnit};
pub use ra_parser::SyntaxKind;
@ -83,13 +85,6 @@ impl SourceFile {
}
}
pub fn check_fuzz_invariants(text: &str) {
let file = SourceFile::parse(text);
let root = file.syntax();
validation::validate_block_structure(root);
let _ = file.errors();
}
/// This test does not assert anything and instead just shows off the crate's
/// API.
#[test]

View File

@ -8,7 +8,7 @@ use std::{
};
use test_utils::{project_dir, dir_tests, read_text, collect_tests};
use ra_syntax::{SourceFile, AstNode, check_fuzz_invariants};
use ra_syntax::{SourceFile, AstNode, fuzz};
#[test]
fn lexer_tests() {
@ -47,7 +47,7 @@ fn parser_tests() {
#[test]
fn parser_fuzz_tests() {
for (_, text) in collect_tests(&test_data_dir(), &["parser/fuzz-failures"]) {
check_fuzz_invariants(&text)
fuzz::check_parser(&text)
}
}