/
lib.rs
86 lines (75 loc) · 2.72 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! # pest grammars
//!
//! Contains a series of default grammars.
#![doc(html_root_url = "https://docs.rs/pest_grammars")]
extern crate pest;
#[macro_use]
extern crate pest_derive;
pub use pest::Parser;
pub mod json {
/// JSON parser.
#[derive(Parser)]
#[grammar = "grammars/json.pest"]
pub struct JsonParser;
}
pub mod toml {
/// TOML parser.
#[derive(Parser)]
#[grammar = "grammars/toml.pest"]
pub struct TomlParser;
}
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use pest::Parser;
use crate::{json, toml};
#[test]
fn toml_handles_deep_nesting() {
let sample1 = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/test/tomlfuzzsample1.toml"
));
let sample2 = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/test/tomlfuzzsample2.toml"
));
const ERROR: &str = "call limit reached";
for limit in [1, 100usize, 250, 500, 1000, 1500].iter() {
dbg!(limit);
pest::set_call_limit(Some((*limit).try_into().unwrap()));
let s1 = toml::TomlParser::parse(toml::Rule::toml, sample1);
assert!(s1.is_err());
assert_eq!(s1.unwrap_err().variant.message(), ERROR);
let s2 = toml::TomlParser::parse(toml::Rule::toml, sample2);
assert!(s2.is_err());
assert_eq!(s2.unwrap_err().variant.message(), ERROR);
}
// pest::set_call_limit(Some(2_000usize.try_into().unwrap()));
// let s1 = toml::TomlParser::parse(toml::Rule::toml, sample1);
// assert!(s1.is_err());
// assert_eq!(s1.unwrap_err().variant.message(), ERROR);
// let s2 = toml::TomlParser::parse(toml::Rule::toml, sample2);
// assert!(s2.is_err());
// assert_eq!(s2.unwrap_err().variant.message(), ERROR);
}
#[test]
fn json_handles_deep_nesting() {
let sample1 = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/test/jsonfuzzsample1.json"
));
const ERROR: &str = "call limit reached";
pest::set_call_limit(Some(45_000usize.try_into().unwrap()));
let s1 = json::JsonParser::parse(json::Rule::json, sample1);
assert!(s1.is_err());
assert_eq!(s1.unwrap_err().variant.message(), ERROR);
}
}