summaryrefslogtreecommitdiffstats
path: root/vendor/digest-0.8.1/src/dev.rs
blob: e9950ee799bdbb62927e6d87563ab9376a6da78c (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use super::{Input, VariableOutput, ExtendableOutput, Reset, XofReader};
use core::fmt::Debug;

#[macro_export]
macro_rules! new_test {
    ($name:ident, $test_name:expr, $hasher:ty, $test_func:ident) => {
        #[test]
        fn $name() {
            use digest::blobby::Blob2Iterator;
            let data = include_bytes!(concat!("data/", $test_name, ".blb"));

            for (i, row) in Blob2Iterator::new(data).unwrap().enumerate() {
                let input = row[0];
                let output = row[1];
                if let Some(desc) = $test_func::<$hasher>(input, output) {
                    panic!("\n\
                        Failed test №{}: {}\n\
                        input:\t{:?}\n\
                        output:\t{:?}\n",
                        i, desc, input, output,
                    );
                }
            }
        }
    }
}

// module to separate Digest from other traits
mod foo {
    use super::super::Digest;
    use core::fmt::Debug;

    pub fn digest_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
        where D: Digest + Debug + Clone
    {
        let mut hasher = D::new();
        // Test that it works when accepting the message all at once
        hasher.input(input);
        let mut hasher2 = hasher.clone();
        if hasher.result().as_slice() != output {
            return Some("whole message");
        }

        // Test if reset works correctly
        hasher2.reset();
        hasher2.input(input);
        if hasher2.result().as_slice() != output {
            return Some("whole message after reset");
        }

        // Test that it works when accepting the message in pieces
        let mut hasher = D::new();
        let len = input.len();
        let mut left = len;
        while left > 0 {
            let take = (left + 1) / 2;
            hasher.input(&input[len - left..take + len - left]);
            left = left - take;
        }
        if hasher.result().as_slice() != output {
            return Some("message in pieces");
        }

        // Test processing byte-by-byte
        let mut hasher = D::new();
        for chunk in input.chunks(1) {
            hasher.input(chunk)
        }
        if hasher.result().as_slice() != output {
            return Some("message byte-by-byte");
        }
        None
    }


    pub fn one_million_a<D>(expected: &[u8])
        where D: Digest + Debug + Clone
    {
        let mut sh = D::new();
        for _ in 0..50_000 {
            sh.input(&[b'a'; 10]);
        }
        sh.input(&[b'a'; 500_000][..]);
        let out = sh.result();
        assert_eq!(out[..], expected[..]);
    }
}

pub use self::foo::{digest_test, one_million_a};

pub fn xof_test<D>(input: &[u8], output: &[u8])
    -> Option<&'static str>
    where D: Input + ExtendableOutput + Default + Debug + Reset  + Clone
{
    let mut hasher = D::default();
    let mut buf = [0u8; 1024];
    // Test that it works when accepting the message all at once
    hasher.input(input);

    let mut hasher2 = hasher.clone();
    {
        let out = &mut buf[..output.len()];
        hasher.xof_result().read(out);

        if out != output { return Some("whole message"); }
    }

    // Test if hasher resets correctly
    hasher2.reset();
    hasher2.input(input);

    {
        let out = &mut buf[..output.len()];
        hasher2.xof_result().read(out);

        if out != output { return Some("whole message after reset"); }
    }

    // Test if hasher accepts message in pieces correctly
    let mut hasher = D::default();
    let len = input.len();
    let mut left = len;
    while left > 0 {
        let take = (left + 1) / 2;
        hasher.input(&input[len - left..take + len - left]);
        left = left - take;
    }

    {
        let out = &mut buf[..output.len()];
        hasher.xof_result().read(out);
        if out != output { return Some("message in pieces"); }
    }

    // Test reading from reader byte by byte
    let mut hasher = D::default();
    hasher.input(input);

    let mut reader = hasher.xof_result();
    let out = &mut buf[..output.len()];
    for chunk in out.chunks_mut(1) {
        reader.read(chunk);
    }

    if out != output { return Some("message in pieces"); }
    None
}

pub fn variable_test<D>(input: &[u8], output: &[u8])
    -> Option<&'static str>
    where D: Input + VariableOutput  + Reset + Debug + Clone
{
    let mut hasher = D::new(output.len()).unwrap();
    let mut buf = [0u8; 128];
    let buf = &mut buf[..output.len()];
    // Test that it works when accepting the message all at once
    hasher.input(input);
    let mut hasher2 = hasher.clone();
    hasher.variable_result(|res| buf.copy_from_slice(res));
    if buf != output { return Some("whole message"); }

    // Test if reset works correctly
    hasher2.reset();
    hasher2.input(input);
    hasher2.variable_result(|res| buf.copy_from_slice(res));
    if buf != output { return Some("whole message after reset"); }

    // Test that it works when accepting the message in pieces
    let mut hasher = D::new(output.len()).unwrap();
    let len = input.len();
    let mut left = len;
    while left > 0 {
        let take = (left + 1) / 2;
        hasher.input(&input[len - left..take + len - left]);
        left = left - take;
    }
    hasher.variable_result(|res| buf.copy_from_slice(res));
    if buf != output { return Some("message in pieces"); }

    // Test processing byte-by-byte
    let mut hasher = D::new(output.len()).unwrap();
    for chunk in input.chunks(1) {
        hasher.input(chunk)
    }
    hasher.variable_result(|res| buf.copy_from_slice(res));
    if buf != output { return Some("message byte-by-byte"); }
    None
}


#[macro_export]
macro_rules! bench {
    ($name:ident, $engine:path, $bs:expr) => {
        #[bench]
        fn $name(b: &mut Bencher) {
            let mut d = <$engine>::default();
            let data = [0; $bs];

            b.iter(|| {
                d.input(&data[..]);
            });

            b.bytes = $bs;
        }
    };

    ($engine:path) => {
        extern crate test;

        use test::Bencher;
        use digest::Digest;

        bench!(bench1_10,    $engine, 10);
        bench!(bench2_100,   $engine, 100);
        bench!(bench3_1000,  $engine, 1000);
        bench!(bench4_10000, $engine, 10000);
    }
}