Merge pull request #1008 from lightning-signer/2021-07-sync-no-std
[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::zbase32;
11
12 use utils::test_logger;
13
14 #[inline]
15 pub fn do_test(data: &[u8]) {
16         let res = zbase32::encode(data);
17         assert_eq!(&zbase32::decode(&res).unwrap()[..], data);
18
19         if let Ok(s) = std::str::from_utf8(data) {
20                 if let Ok(decoded) = zbase32::decode(s) {
21                         assert_eq!(&zbase32::encode(&decoded), &s.to_ascii_lowercase());
22                 }
23         }
24 }
25
26 pub fn zbase32_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
27         do_test(data);
28 }
29
30 #[no_mangle]
31 pub extern "C" fn zbase32_run(data: *const u8, datalen: usize) {
32         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
33 }