[fuzz] Fix slice copy in `peer_crypt_target`
[rust-lightning] / fuzz / src / base32.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 use lightning::util::base32;
11
12 use crate::utils::test_logger;
13
14 #[inline]
15 pub fn do_test(data: &[u8]) {
16         if let Ok(s) = std::str::from_utf8(data) {
17                 let first_decoding = base32::Alphabet::RFC4648 { padding: true }.decode(s);
18                 if let Ok(first_decoding) = first_decoding {
19                         let encoding_response = base32::Alphabet::RFC4648 { padding: true }.encode(&first_decoding);
20                         assert_eq!(encoding_response, s.to_ascii_uppercase());
21                         let second_decoding = base32::Alphabet::RFC4648 { padding: true }.decode(&encoding_response).unwrap();
22                         assert_eq!(first_decoding, second_decoding);
23                 }
24         }
25
26         if let Ok(s) = std::str::from_utf8(data) {
27                 let first_decoding = base32::Alphabet::RFC4648 { padding: false }.decode(s);
28                 if let Ok(first_decoding) = first_decoding {
29                         let encoding_response = base32::Alphabet::RFC4648 { padding: false }.encode(&first_decoding);
30                         assert_eq!(encoding_response, s.to_ascii_uppercase());
31                         let second_decoding = base32::Alphabet::RFC4648 { padding: false }.decode(&encoding_response).unwrap();
32                         assert_eq!(first_decoding, second_decoding);
33                 }
34         }
35         
36         let encode_response = base32::Alphabet::RFC4648 { padding: false }.encode(&data);
37         let decode_response = base32::Alphabet::RFC4648 { padding: false }.decode(&encode_response).unwrap();
38         assert_eq!(data, decode_response);
39
40         let encode_response = base32::Alphabet::RFC4648 { padding: true }.encode(&data);
41         let decode_response = base32::Alphabet::RFC4648 { padding: true }.decode(&encode_response).unwrap();
42         assert_eq!(data, decode_response);
43 }
44
45 pub fn base32_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
46         do_test(data);
47 }
48
49 #[no_mangle]
50 pub extern "C" fn base32_run(data: *const u8, datalen: usize) {
51         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
52 }