aboutsummaryrefslogtreecommitdiff
path: root/tests/limit_tests.rs
blob: 43f734e0e735304d7ceba38eff92bdf927c9beeb (plain) (blame)
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
use ele::{parse, parser::MAX_FILTER_BYTES};
use std::process::Command;

#[test]
fn excessive_filters_report_errors_without_crashing() {
    let filters = [
        format!("{}x{}", "(".repeat(4096), ")".repeat(4096)),
        format!("{}x{}", "(".repeat(100), ")".repeat(100)),
        format!("{}x", "NOT ".repeat(100)),
        format!("{}x{}", "f(".repeat(100), ")".repeat(100)),
        "x ".repeat(300),
        "x.".repeat(300) + "x",
        "x AND ".repeat(300) + "x",
        // Three-byte UTF-8 characters keep the argument below Windows' UTF-16
        // command-line limit while exceeding the filter byte limit.
        "界".repeat(MAX_FILTER_BYTES / "界".len() + 1),
    ];
    for filter in filters {
        let output = Command::new(env!("CARGO_BIN_EXE_ele"))
            .args(["--print", &filter])
            .output()
            .unwrap();
        assert_eq!(
            output.status.code(),
            Some(1),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(output.stdout.is_empty());
        assert!(String::from_utf8_lossy(&output.stderr).contains("exceeds"));
    }
}

#[test]
fn filter_byte_limit_counts_utf8_bytes() {
    for character in ["x", "界"] {
        let within_limit = character.repeat(MAX_FILTER_BYTES / character.len());
        assert!(parse(&within_limit).is_ok());
        assert_eq!(
            parse(&(within_limit + character)).unwrap_err(),
            format!("filter exceeds {MAX_FILTER_BYTES} bytes")
        );
    }
}

#[test]
fn parser_limits_work_on_a_normal_thread_stack() {
    std::thread::Builder::new()
        .stack_size(2 * 1024 * 1024)
        .spawn(|| {
            assert!(parse(&format!("{}x{}", "(".repeat(100), ")".repeat(100))).is_err());
            assert!(parse(&("x ".repeat(300))).is_err());
            assert!(parse(&format!("{}x", "NOT ".repeat(100))).is_err());
        })
        .unwrap()
        .join()
        .unwrap();
}

#[test]
fn ordinary_nested_filters_still_parse() {
    assert!(
        parse(&format!("{}x{}", "(".repeat(16), ")".repeat(16)))
            .unwrap()
            .is_some()
    );
    assert!(parse(&("x AND ".repeat(20) + "x")).unwrap().is_some());
    assert!(parse(&("x.".repeat(20) + "x = 1")).unwrap().is_some());
    assert!(
        parse(&format!("\"{}\"", "x".repeat(65534)))
            .unwrap()
            .is_some()
    );
}