aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_targets/write_helpers_are_correct.rs
blob: 41e27bd47da10e128e1d66f20b8758e08a2af251 (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
88
89
90
91
92
93
94
95
96
#![no_main]
use libfuzzer_sys::fuzz_target;
use yaxpeax_arch::display::DisplaySink;

use std::convert::TryInto;

fuzz_target!(|data: &[u8]| {
    let mut buf = String::new();
    match data.len() {
        1 => {
            let i = data[0];

            buf.clear();
            buf.write_u8(i).expect("write succeeds");
            assert_eq!(buf, format!("{:x}", i));

            buf.clear();
            buf.write_prefixed_u8(i).expect("write succeeds");
            assert_eq!(buf, format!("0x{:x}", i));

            let expected = if (i as i8) < 0 {
                format!("-0x{:x}", (i as i8).unsigned_abs())
            } else {
                format!("0x{:x}", i)
            };

            buf.clear();
            buf.write_prefixed_i8(i as i8).expect("write succeeds");
            assert_eq!(buf, expected);
        },
        2 => {
            let i: u16 = u16::from_le_bytes(data.try_into().expect("checked the size is right"));

            buf.clear();
            buf.write_u16(i).expect("write succeeds");
            assert_eq!(buf, format!("{:x}", i));

            buf.clear();
            buf.write_prefixed_u16(i).expect("write succeeds");
            assert_eq!(buf, format!("0x{:x}", i));

            let expected = if (i as i16) < 0 {
                format!("-0x{:x}", (i as i16).unsigned_abs())
            } else {
                format!("0x{:x}", i)
            };

            buf.clear();
            buf.write_prefixed_i16(i as i16).expect("write succeeds");
            assert_eq!(buf, expected);
        }
        4 => {
            let i: u32 = u32::from_le_bytes(data.try_into().expect("checked the size is right"));

            buf.clear();
            buf.write_u32(i).expect("write succeeds");
            assert_eq!(buf, format!("{:x}", i));

            buf.clear();
            buf.write_prefixed_u32(i).expect("write succeeds");
            assert_eq!(buf, format!("0x{:x}", i));

            let expected = if (i as i32) < 0 {
                format!("-0x{:x}", (i as i32).unsigned_abs())
            } else {
                format!("0x{:x}", i)
            };

            buf.clear();
            buf.write_prefixed_i32(i as i32).expect("write succeeds");
            assert_eq!(buf, expected);
        },
        8 => {
            let i: u64 = u64::from_le_bytes(data.try_into().expect("checked the size is right"));

            buf.clear();
            buf.write_u64(i).expect("write succeeds");
            assert_eq!(buf, format!("{:x}", i));

            buf.clear();
            buf.write_prefixed_u64(i).expect("write succeeds");
            assert_eq!(buf, format!("0x{:x}", i));

            let expected = if (i as i64) < 0 {
                format!("-0x{:x}", (i as i64).unsigned_abs())
            } else {
                format!("0x{:x}", i)
            };

            buf.clear();
            buf.write_prefixed_i64(i as i64).expect("write succeeds");
            assert_eq!(buf, expected);
        },
        _ => {}
    }
});