mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Refactor parser fuzz testing
This commit is contained in:
parent
51323a852a
commit
e734190c24
@ -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]
|
||||
|
@ -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)
|
||||
}
|
||||
});
|
||||
|
12
crates/ra_syntax/src/fuzz.rs
Normal file
12
crates/ra_syntax/src/fuzz.rs
Normal 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);
|
||||
}
|
@ -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]
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user