aboutsummaryrefslogtreecommitdiffstats
path: root/fuzz/fuzz_parse.rs
blob: 0efdf4284f118aac2b3da46ecfe56dca942990bb (plain)
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
87
//! Fuzz targets for message parsers.
//!
//! Run with: cargo +nightly fuzz run fuzz_parse
//!
//! Requires: cargo install cargo-fuzz
//!
//! These targets verify that no input can cause a panic,
//! buffer overflow, or undefined behavior in the parsers.

// Note: this file is a reference for cargo-fuzz targets.
// To use, create a fuzz/Cargo.toml and fuzz_targets/
// directory per cargo-fuzz conventions. The actual fuzz
// harnesses are:

#[cfg(test)]
mod tests {
    /// Fuzz MsgHeader::parse with random bytes.
    #[test]
    fn fuzz_header_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 128];
            tesseras_dht::sys::random_bytes(&mut buf);
            // Should never panic
            let _ = tesseras_dht::wire::MsgHeader::parse(&buf);
        }
    }

    /// Fuzz msg::parse_store with random bytes.
    #[test]
    fn fuzz_store_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 256];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::msg::parse_store(&buf);
        }
    }

    /// Fuzz msg::parse_find_node with random bytes.
    #[test]
    fn fuzz_find_node_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 128];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::msg::parse_find_node(&buf);
        }
    }

    /// Fuzz msg::parse_find_value with random bytes.
    #[test]
    fn fuzz_find_value_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 256];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::msg::parse_find_value(&buf);
        }
    }

    /// Fuzz rdp::parse_rdp_wire with random bytes.
    #[test]
    fn fuzz_rdp_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 128];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::rdp::parse_rdp_wire(&buf);
        }
    }

    /// Fuzz dgram::parse_fragment with random bytes.
    #[test]
    fn fuzz_fragment_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 64];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::dgram::parse_fragment(&buf);
        }
    }

    /// Fuzz msg::parse_find_value_reply with random bytes.
    #[test]
    fn fuzz_find_value_reply_parse() {
        for _ in 0..10_000 {
            let mut buf = [0u8; 256];
            tesseras_dht::sys::random_bytes(&mut buf);
            let _ = tesseras_dht::msg::parse_find_value_reply(&buf);
        }
    }
}