Files
ArenaPublic/crates/arena-core/tests/vdf_prop.rs
T

141 lines
5.0 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Property tests on the VDF tokenizer and parser: no panics on arbitrary bytes, and generated documents survive
//! a write/parse round trip.
use arena_core::vdf::{self, Obj, TokenKind, Value};
use arena_core::{WinPath, epic, steam};
use proptest::prelude::*;
/// Strings the format can carry: anything printable plus the characters the writer must escape.
fn vdf_string(max: usize) -> impl Strategy<Value = String> {
proptest::collection::vec(
prop_oneof![
8 => proptest::char::range(' ', '~'),
2 => Just('"'),
2 => Just('\\'),
1 => Just('\n'),
1 => Just('\t'),
1 => Just('\r'),
1 => proptest::char::range('\u{a0}', '\u{2fff}'),
1 => proptest::char::range('\u{1f300}', '\u{1f64f}'),
],
0..max,
)
.prop_map(|cs| cs.into_iter().collect())
}
fn key() -> impl Strategy<Value = String> {
vdf_string(12).prop_filter("keys are not empty", |k| !k.is_empty())
}
#[derive(Debug, Clone)]
enum Gen {
Str(String),
Obj(Vec<(String, Gen)>),
}
fn gen_value() -> impl Strategy<Value = Gen> {
let leaf = vdf_string(24).prop_map(Gen::Str);
leaf.prop_recursive(6, 64, 6, |inner| proptest::collection::vec((key(), inner), 0..6).prop_map(Gen::Obj))
}
/// Builds an `Obj`, dropping keys that repeat (ignoring ASCII case) as the format requires.
fn build(pairs: Vec<(String, Gen)>) -> Obj {
let mut o = Obj::new();
for (k, v) in pairs {
let v = match v {
Gen::Str(s) => Value::Str(s),
Gen::Obj(p) => Value::Obj(build(p)),
};
let _ = o.insert(k, v);
}
o
}
fn gen_doc() -> impl Strategy<Value = Obj> {
proptest::collection::vec((key(), gen_value()), 0..6).prop_map(build)
}
fn lib() -> WinPath {
WinPath::parse(r"D:\SteamLibrary").unwrap()
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
#[test]
fn tokenizer_never_panics(bytes in proptest::collection::vec(any::<u8>(), 0..2048)) {
let _ = vdf::tokenize(&bytes);
let _ = vdf::parse(&bytes);
let _ = steam::library_folders(&bytes);
let _ = steam::app_manifest(&bytes, &lib());
let _ = epic::item(&bytes);
}
#[test]
fn tokenizer_never_panics_on_vdf_like_bytes(
parts in proptest::collection::vec(
prop_oneof![
Just(&b"\""[..]), Just(&b"{"[..]), Just(&b"}"[..]), Just(&b"\\"[..]), Just(&b"//"[..]),
Just(&b"\n"[..]), Just(&b" "[..]), Just(&b"\\\""[..]), Just(&b"\\n"[..]), Just(&b"\0"[..]),
Just(&b"\xff"[..]), Just(&b"\xc3"[..]), Just(&b"key"[..]), Just(&b"\xEF\xBB\xBF"[..]),
Just(&b"\"AppState\""[..]), Just(&b"\"appid\""[..]), Just(&b"\"10\""[..]),
Just(&b"\"libraryfolders\""[..]), Just(&b"\"path\""[..]), Just(&b"\"C:\\\\G\""[..]),
],
0..200,
)
) {
let bytes: Vec<u8> = parts.concat();
let _ = vdf::tokenize(&bytes);
let _ = vdf::parse(&bytes);
let _ = steam::library_folders(&bytes);
let _ = steam::app_manifest(&bytes, &lib());
}
#[test]
fn tokens_carry_valid_offsets(bytes in proptest::collection::vec(any::<u8>(), 0..512)) {
if let Ok(tokens) = vdf::tokenize(&bytes) {
let mut last = None;
for t in &tokens {
prop_assert!(t.at < bytes.len());
prop_assert!(last.is_none_or(|l| l < t.at), "offsets increase");
last = Some(t.at);
let first = bytes[t.at];
match t.kind {
TokenKind::Open => prop_assert_eq!(first, b'{'),
TokenKind::Close => prop_assert_eq!(first, b'}'),
TokenKind::Str(_) => prop_assert_eq!(first, b'"'),
}
}
}
}
#[test]
fn syntax_errors_point_inside_the_input(bytes in proptest::collection::vec(any::<u8>(), 0..512)) {
if let Err(arena_core::ParseError::Syntax { at }) = vdf::parse(&bytes) {
prop_assert!(at <= bytes.len());
}
}
#[test]
fn generated_documents_round_trip(doc in gen_doc()) {
let text = vdf::write(&doc);
let back = vdf::parse(text.as_bytes());
prop_assert_eq!(back.as_ref(), Ok(&doc), "{}", text);
// Writing again is byte-identical.
prop_assert_eq!(vdf::write(&back.unwrap()), text);
}
#[test]
fn generated_strings_round_trip_through_tokens(s in vdf_string(200)) {
let mut o = Obj::new();
o.insert("k".into(), Value::Str(s.clone())).unwrap();
let tokens = vdf::tokenize(vdf::write(&o).as_bytes()).unwrap();
let kinds: Vec<TokenKind> = tokens.into_iter().map(|t| t.kind).collect();
prop_assert_eq!(kinds, vec![TokenKind::Str("k".into()), TokenKind::Str(s)]);
}
}