1 use super::*;
2 
3 const EMPTY_HASH: &str = "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419\
4                           d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
5 const ABC_HASH: &str = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1\
6                         7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
7 const ONE_BLOCK_HASH: &str = "865939e120e6805438478841afb739ae4250cf372653078a065cdcfffca4caf7\
8                               98e6d462b65d658fc165782640eded70963449ae1500fb0f24981d7727e22c41";
9 const THOUSAND_HASH: &str = "1ee4e51ecab5210a518f26150e882627ec839967f19d763e1508b12cfefed148\
10                              58f6a1c9d1f969bc224dc9440f5a6955277e755b9c513f9ba4421c5e50c8d787";
11 
12 #[test]
test_update_state()13 fn test_update_state() {
14     let io = &[
15         (&b""[..], EMPTY_HASH),
16         (&b"abc"[..], ABC_HASH),
17         (&[0; BLOCKBYTES], ONE_BLOCK_HASH),
18         (&[0; 1000], THOUSAND_HASH),
19     ];
20     // Test each input all at once.
21     for &(input, output) in io {
22         let hash = blake2b(input);
23         assert_eq!(&hash.to_hex(), output, "hash mismatch");
24     }
25     // Now in two chunks. This is especially important for the ONE_BLOCK case, because it would be
26     // a mistake for update() to call compress, even though the buffer is full.
27     for &(input, output) in io {
28         let mut state = State::new();
29         let split = input.len() / 2;
30         state.update(&input[..split]);
31         assert_eq!(split as Count, state.count());
32         state.update(&input[split..]);
33         assert_eq!(input.len() as Count, state.count());
34         let hash = state.finalize();
35         assert_eq!(&hash.to_hex(), output, "hash mismatch");
36     }
37     // Now one byte at a time.
38     for &(input, output) in io {
39         let mut state = State::new();
40         let mut count = 0;
41         for &b in input {
42             state.update(&[b]);
43             count += 1;
44             assert_eq!(count, state.count());
45         }
46         let hash = state.finalize();
47         assert_eq!(&hash.to_hex(), output, "hash mismatch");
48     }
49 }
50 
51 #[test]
test_multiple_finalizes()52 fn test_multiple_finalizes() {
53     let mut state = State::new();
54     assert_eq!(&state.finalize().to_hex(), EMPTY_HASH, "hash mismatch");
55     assert_eq!(&state.finalize().to_hex(), EMPTY_HASH, "hash mismatch");
56     assert_eq!(&state.finalize().to_hex(), EMPTY_HASH, "hash mismatch");
57     state.update(b"abc");
58     assert_eq!(&state.finalize().to_hex(), ABC_HASH, "hash mismatch");
59     assert_eq!(&state.finalize().to_hex(), ABC_HASH, "hash mismatch");
60     assert_eq!(&state.finalize().to_hex(), ABC_HASH, "hash mismatch");
61 }
62 
63 #[cfg(feature = "std")]
64 #[test]
test_write()65 fn test_write() {
66     use std::io::prelude::*;
67 
68     let mut state = State::new();
69     state.write_all(&[0; 1000]).unwrap();
70     let hash = state.finalize();
71     assert_eq!(&hash.to_hex(), THOUSAND_HASH, "hash mismatch");
72 }
73 
74 // You can check this case against the equivalent Python:
75 //
76 // import hashlib
77 // hashlib.blake2b(
78 //     b'foo',
79 //     digest_size=18,
80 //     key=b"bar",
81 //     salt=b"bazbazbazbazbazb",
82 //     person=b"bing bing bing b",
83 //     fanout=2,
84 //     depth=3,
85 //     leaf_size=0x04050607,
86 //     node_offset=0x08090a0b0c0d0e0f,
87 //     node_depth=16,
88 //     inner_size=17,
89 //     last_node=True,
90 // ).hexdigest()
91 #[test]
test_all_parameters()92 fn test_all_parameters() {
93     let mut params = Params::new();
94     params
95         .hash_length(18)
96         // Make sure a shorter key properly overwrites a longer one.
97         .key(b"not the real key")
98         .key(b"bar")
99         .salt(b"bazbazbazbazbazb")
100         .personal(b"bing bing bing b")
101         .fanout(2)
102         .max_depth(3)
103         .max_leaf_length(0x04050607)
104         .node_offset(0x08090a0b0c0d0e0f)
105         .node_depth(16)
106         .inner_hash_length(17)
107         .last_node(true);
108 
109     // Check the State API.
110     assert_eq!(
111         "ec0f59cb65f92e7fcca1280ba859a6925ded",
112         &params.to_state().update(b"foo").finalize().to_hex()
113     );
114 
115     // Check the all-at-once API.
116     assert_eq!(
117         "ec0f59cb65f92e7fcca1280ba859a6925ded",
118         &params.hash(b"foo").to_hex()
119     );
120 }
121 
122 #[test]
test_all_parameters_blake2bp()123 fn test_all_parameters_blake2bp() {
124     let mut params = blake2bp::Params::new();
125     params
126         .hash_length(18)
127         // Make sure a shorter key properly overwrites a longer one.
128         .key(b"not the real key")
129         .key(b"bar");
130 
131     // Check the State API.
132     assert_eq!(
133         "8c54e888a8a01c63da6585c058fe54ea81df",
134         &params.to_state().update(b"foo").finalize().to_hex()
135     );
136 
137     // Check the all-at-once API.
138     assert_eq!(
139         "8c54e888a8a01c63da6585c058fe54ea81df",
140         &params.hash(b"foo").to_hex()
141     );
142 }
143 
144 #[test]
145 #[should_panic]
test_short_hash_length_panics()146 fn test_short_hash_length_panics() {
147     Params::new().hash_length(0);
148 }
149 
150 #[test]
151 #[should_panic]
test_long_hash_length_panics()152 fn test_long_hash_length_panics() {
153     Params::new().hash_length(OUTBYTES + 1);
154 }
155 
156 #[test]
157 #[should_panic]
test_long_key_panics()158 fn test_long_key_panics() {
159     Params::new().key(&[0; KEYBYTES + 1]);
160 }
161 
162 #[test]
163 #[should_panic]
test_long_salt_panics()164 fn test_long_salt_panics() {
165     Params::new().salt(&[0; SALTBYTES + 1]);
166 }
167 
168 #[test]
169 #[should_panic]
test_long_personal_panics()170 fn test_long_personal_panics() {
171     Params::new().personal(&[0; PERSONALBYTES + 1]);
172 }
173 
174 #[test]
test_zero_max_depth_supported()175 fn test_zero_max_depth_supported() {
176     Params::new().max_depth(0);
177 }
178 
179 #[test]
180 #[should_panic]
test_long_inner_hash_length_panics()181 fn test_long_inner_hash_length_panics() {
182     Params::new().inner_hash_length(OUTBYTES + 1);
183 }
184 
185 #[test]
186 #[should_panic]
test_blake2bp_short_hash_length_panics()187 fn test_blake2bp_short_hash_length_panics() {
188     blake2bp::Params::new().hash_length(0);
189 }
190 
191 #[test]
192 #[should_panic]
test_blake2bp_long_hash_length_panics()193 fn test_blake2bp_long_hash_length_panics() {
194     blake2bp::Params::new().hash_length(OUTBYTES + 1);
195 }
196 
197 #[test]
198 #[should_panic]
test_blake2bp_long_key_panics()199 fn test_blake2bp_long_key_panics() {
200     blake2bp::Params::new().key(&[0; KEYBYTES + 1]);
201 }
202