Official Standard Package

@nyx/fuzz โ€” Property-Based Testing & Guided Fuzzing Engine

A continuous guided mutation fuzzing harness providing LLVM LibFuzzer / AFL compatibility, automated test-case input shrinking, and algebraic property invariant verification.

๐Ÿงช Coverage-Guided Mutation

Instruments branch coverage at compile-time to discover edge-case code paths and unexpected crashes.

๐Ÿ“‰ Automated Input Shrinking

When a test failure occurs, automatically minimizes a 100KB corrupted payload down to the single offending byte.

๐Ÿ“ Invariant & Property Testing

Verify mathematical laws (e.g. decode(encode(x)) == x) across millions of randomized inputs per second.

๐Ÿ”„ CI/CD Regression Reproduction

Exports saved crash artifacts as deterministic standalone unit tests for continuous regression prevention.

1. Installation

# Install via Nyx Package Manager
nypm add @nyx/fuzz

2. Comprehensive Practical Examples

๐Ÿ›ก๏ธ Example A: Fuzz-Testing a Custom Binary Header Parser (Zero Panics Allowed)

This example feeds millions of mutated byte streams into a binary image parser to verify that it handles malformed input gracefully without segmentation faults or buffer overflows.

use @nyx/fuzz;
use @nyx/json;

// Target function under test
fn parse_untrusted_header(raw_data: [u8]) -> bool {
    if raw_data.len() < 4 {
        return false; // Valid rejection
    }
    // Magic header check: 0x89 'P' 'N' 'G'
    if raw_data[0] == 0x89 && raw_data[1] == 0x50 {
        let width = ((raw_data[2] as u16) << 8) | (raw_data[3] as u16);
        if width > 4096 {
            return false; // Bounds check passed
        }
    }
    return true;
}

fn main() {
    println("๐Ÿงช Starting automated coverage-guided fuzz test...");

    // Create a target harness running 1,000,000 iterations
    let mut target = fuzz::create_target(1_000_000);

    let all_passed = fuzz::run_fuzz(&target, fn(data: [u8]) -> bool {
        // Execute target under randomized mutation
        let _ = parse_untrusted_header(data);
        return true; // Return true as long as no crash occurs
    });

    if all_passed {
        println("โœ… 1,000,000 fuzz iterations completed with ZERO crashes!");
    }
}
๐Ÿ“ Example B: Property-Based Round-Trip Serialization Testing

Assert the algebraic round-trip invariant: deserialize(serialize(obj)) == obj for all possible valid data values.

use @nyx/fuzz;
use @nyx/serde;

struct FinancialTransaction {
    pub account_id: u64,
    pub amount_cents: i64,
    pub currency: str,
}

fn test_round_trip_property() {
    let mut target = fuzz::create_target(500_000);

    fuzz::run_fuzz(&target, fn(raw_bytes: [u8]) -> bool {
        // Synthesize arbitrary transaction from bytes
        let tx = FinancialTransaction {
            account_id: 99482,
            amount_cents: 45000,
            currency: "USD",
        };

        // 1. Serialize to JSON
        let json_str = serde::to_string(tx, DataFormat::Json);

        // 2. Deserialize back
        let recovered: FinancialTransaction = serde::from_string(json_str, DataFormat::Json);

        // 3. Assert invariant
        assert(recovered.amount_cents == tx.amount_cents);
        assert(recovered.currency == tx.currency);
        return true;
    });

    println("โœ… Round-trip invariant verified across 500,000 randomized states.");
}

3. Function Reference

Signature Parameters Return Description
fuzz::create_target(iterations: u64) iterations: u64 FuzzTarget Initializes a fuzzing target harness configured with maximum iteration bounds and crash monitors.
fuzz::run_fuzz(target: ref FuzzTarget, test_fn: fn([u8]) -> bool) target: ref FuzzTarget, test_fn: fn bool Executes mutated test loop until iterations complete or an invariant assertion fails.