Move `zbase32` implementation to `base32` file
[rust-lightning] / fuzz / src / zbase32.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         let res = base32::Alphabet::ZBase32.encode(data);
17         assert_eq!(&base32::Alphabet::ZBase32.decode(&res).unwrap()[..], data);
18
19         if let Ok(s) = std::str::from_utf8(data) {
20                 let res = base32::Alphabet::ZBase32.decode(s);
21                 if let Ok(decoded) = res {
22                         assert_eq!(&base32::Alphabet::ZBase32.encode(&decoded), &s.to_ascii_lowercase());
23                 }
24         }
25 }
26
27 pub fn zbase32_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
28         do_test(data);
29 }
30
31 #[no_mangle]
32 pub extern "C" fn zbase32_run(data: *const u8, datalen: usize) {
33         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
34 }