Reliability Labs · 16 min · free lesson

Adversarial Parser Tests

Start with a grammar, then make a test table for the ways input can violate it.

Core idea

No panic is necessary for an expected invalid input, but success must still mean the whole record was accepted.

Diagram for Adversarial Parser Tests: a Rust code example flows to the lesson rule and the key observation.
Reject malformed data without panic or silent truncation

Understand it

A parser that never panics can still be wrong if it silently ignores a suffix, rounds an overflowing value, or accepts a missing field. Write down the accepted grammar before writing adversarial cases. This lesson's grammar is exactly two trimmed unsigned decimal byte values separated by one colon. Both values must fit u8. A third field, an empty field, a sign, or nondecimal text is invalid.

Organize tests by failure class instead of adding random strings without a hypothesis. Test a missing delimiter, repeated delimiters, a negative value, a leading plus, overflow, whitespace-only fields, Unicode digits, and the two valid numeric boundaries. Rust's numeric parser may accept syntax you did not intend, so enforce the grammar's ASCII-digit rule before parsing. Return None for this small API; a richer user-facing parser should expose a precise error type.

Avoid filter_map on fallible fields: it can turn a three-field record with one bad field into two apparently good values. Do not index a collected vector until its length has been checked. Check complete consumption so a valid prefix cannot conceal trailing junk. These ideas transfer directly to command input, imported project metadata, and network messages.

Local compiler profile: standard library only. The lab uses deterministic hostile cases. Desktop cargo-fuzz is a separate toolchain that can explore byte inputs and save crashes; it does not belong to the browser or iOS run profile merely because the parser itself is portable. A useful fuzz target asserts domain properties after successful parsing and never treats rejection of arbitrary bytes as a bug.

Read the code

fn decimal_byte(text: &str) -> Option<u8> {
    let text = text.trim();
    if text.is_empty()
        || !text.bytes().all(|b| b.is_ascii_digit())
    {
        return None;
    }
    text.parse().ok()
}
fn main() {
    assert_eq!(decimal_byte("255"), Some(255));
    assert_eq!(decimal_byte("256"), None);
    assert_eq!(decimal_byte("+1"), None);
}

Reject malformed data without panic or silent truncation

Try it

Parse exactly two trimmed ASCII-decimal u8 fields separated by one colon. Reject signs, empty fields, extra fields, non-ASCII digits, and overflow. Return None on invalid input. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.

What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.

Check your understanding

Why test a leading plus sign separately from a negative number?

Reveal the answer
  1. The declared grammar may forbid syntax that a numeric parser accepts
  2. Plus signs always overflow u8
  3. It makes the parser multithreaded

The application's grammar can be narrower than parse::<u8>(). Test the declared syntax as well as the numeric range.

Go deeper

Common misconception: Anything accepted by parse::<u8>() belongs to my format.

Primitive parsing is a building block; the full record grammar remains your responsibility.

  1. Partition: List valid boundaries and independent invalid-input classes.
  2. Validate: Check field count and ASCII decimal syntax before conversion.
  3. Reject: Return a failure rather than panicking or accepting a valid prefix.

Transfer challenge: Write a desktop fuzz target that feeds arbitrary bytes through UTF-8 validation and then checks the successful parse contract.

Connections

  • Test Properties, Not Just Examples: An independent oracle and bounded generated cases
  • Keep a Unicode Regression: A minimal failure that survives a refactor