Merge pull request #3107 from mhrheaume/mhr/closure_reason_abandoned
[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 =
20                                 base32::Alphabet::RFC4648 { padding: true }.encode(&first_decoding);
21                         assert_eq!(encoding_response, s.to_ascii_uppercase());
22                         let second_decoding =
23                                 base32::Alphabet::RFC4648 { padding: true }.decode(&encoding_response).unwrap();
24                         assert_eq!(first_decoding, second_decoding);
25                 }
26         }
27
28         if let Ok(s) = std::str::from_utf8(data) {
29                 let first_decoding = base32::Alphabet::RFC4648 { padding: false }.decode(s);
30                 if let Ok(first_decoding) = first_decoding {
31                         let encoding_response =
32                                 base32::Alphabet::RFC4648 { padding: false }.encode(&first_decoding);
33                         assert_eq!(encoding_response, s.to_ascii_uppercase());
34                         let second_decoding =
35                                 base32::Alphabet::RFC4648 { padding: false }.decode(&encoding_response).unwrap();
36                         assert_eq!(first_decoding, second_decoding);
37                 }
38         }
39
40         let encode_response = base32::Alphabet::RFC4648 { padding: false }.encode(&data);
41         let decode_response =
42                 base32::Alphabet::RFC4648 { padding: false }.decode(&encode_response).unwrap();
43         assert_eq!(data, decode_response);
44
45         let encode_response = base32::Alphabet::RFC4648 { padding: true }.encode(&data);
46         let decode_response =
47                 base32::Alphabet::RFC4648 { padding: true }.decode(&encode_response).unwrap();
48         assert_eq!(data, decode_response);
49 }
50
51 pub fn base32_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
52         do_test(data);
53 }
54
55 #[no_mangle]
56 pub extern "C" fn base32_run(data: *const u8, datalen: usize) {
57         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
58 }