Skip to content

Commit b5e9594

Browse files
authored
Merge pull request #2134 from jbesraa/add_fromstr_to_netaddress
implement fromstr trait to netaddress
2 parents 82d92dd + 4f45cdc commit b5e9594

13 files changed

+848
-254
lines changed

fuzz/src/base32.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

fuzz/src/bin/base32_target.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
15+
#[cfg(not(fuzzing))]
16+
compile_error!("Fuzz targets need cfg=fuzzing");
17+
18+
extern crate lightning_fuzz;
19+
use lightning_fuzz::base32::*;
20+
21+
#[cfg(feature = "afl")]
22+
#[macro_use] extern crate afl;
23+
#[cfg(feature = "afl")]
24+
fn main() {
25+
fuzz!(|data| {
26+
base32_run(data.as_ptr(), data.len());
27+
});
28+
}
29+
30+
#[cfg(feature = "honggfuzz")]
31+
#[macro_use] extern crate honggfuzz;
32+
#[cfg(feature = "honggfuzz")]
33+
fn main() {
34+
loop {
35+
fuzz!(|data| {
36+
base32_run(data.as_ptr(), data.len());
37+
});
38+
}
39+
}
40+
41+
#[cfg(feature = "libfuzzer_fuzz")]
42+
#[macro_use] extern crate libfuzzer_sys;
43+
#[cfg(feature = "libfuzzer_fuzz")]
44+
fuzz_target!(|data: &[u8]| {
45+
base32_run(data.as_ptr(), data.len());
46+
});
47+
48+
#[cfg(feature = "stdin_fuzz")]
49+
fn main() {
50+
use std::io::Read;
51+
52+
let mut data = Vec::with_capacity(8192);
53+
std::io::stdin().read_to_end(&mut data).unwrap();
54+
base32_run(data.as_ptr(), data.len());
55+
}
56+
57+
#[test]
58+
fn run_test_cases() {
59+
use std::fs;
60+
use std::io::Read;
61+
use lightning_fuzz::utils::test_logger::StringBuffer;
62+
63+
use std::sync::{atomic, Arc};
64+
{
65+
let data: Vec<u8> = vec![0];
66+
base32_run(data.as_ptr(), data.len());
67+
}
68+
let mut threads = Vec::new();
69+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70+
if let Ok(tests) = fs::read_dir("test_cases/base32") {
71+
for test in tests {
72+
let mut data: Vec<u8> = Vec::new();
73+
let path = test.unwrap().path();
74+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
75+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
76+
77+
let thread_count_ref = Arc::clone(&threads_running);
78+
let main_thread_ref = std::thread::current();
79+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
80+
std::thread::spawn(move || {
81+
let string_logger = StringBuffer::new();
82+
83+
let panic_logger = string_logger.clone();
84+
let res = if ::std::panic::catch_unwind(move || {
85+
base32_test(&data, panic_logger);
86+
}).is_err() {
87+
Some(string_logger.into_string())
88+
} else { None };
89+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
90+
main_thread_ref.unpark();
91+
res
92+
})
93+
));
94+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
95+
std::thread::park();
96+
}
97+
}
98+
}
99+
let mut failed_outputs = Vec::new();
100+
for (test, thread) in threads.drain(..) {
101+
if let Some(output) = thread.join().unwrap() {
102+
println!("\nOutput of {}:\n{}\n", test, output);
103+
failed_outputs.push(test);
104+
}
105+
}
106+
if !failed_outputs.is_empty() {
107+
println!("Test cases which failed: ");
108+
for case in failed_outputs {
109+
println!("{}", case);
110+
}
111+
panic!();
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
15+
#[cfg(not(fuzzing))]
16+
compile_error!("Fuzz targets need cfg=fuzzing");
17+
18+
extern crate lightning_fuzz;
19+
use lightning_fuzz::fromstr_to_netaddress::*;
20+
21+
#[cfg(feature = "afl")]
22+
#[macro_use] extern crate afl;
23+
#[cfg(feature = "afl")]
24+
fn main() {
25+
fuzz!(|data| {
26+
fromstr_to_netaddress_run(data.as_ptr(), data.len());
27+
});
28+
}
29+
30+
#[cfg(feature = "honggfuzz")]
31+
#[macro_use] extern crate honggfuzz;
32+
#[cfg(feature = "honggfuzz")]
33+
fn main() {
34+
loop {
35+
fuzz!(|data| {
36+
fromstr_to_netaddress_run(data.as_ptr(), data.len());
37+
});
38+
}
39+
}
40+
41+
#[cfg(feature = "libfuzzer_fuzz")]
42+
#[macro_use] extern crate libfuzzer_sys;
43+
#[cfg(feature = "libfuzzer_fuzz")]
44+
fuzz_target!(|data: &[u8]| {
45+
fromstr_to_netaddress_run(data.as_ptr(), data.len());
46+
});
47+
48+
#[cfg(feature = "stdin_fuzz")]
49+
fn main() {
50+
use std::io::Read;
51+
52+
let mut data = Vec::with_capacity(8192);
53+
std::io::stdin().read_to_end(&mut data).unwrap();
54+
fromstr_to_netaddress_run(data.as_ptr(), data.len());
55+
}
56+
57+
#[test]
58+
fn run_test_cases() {
59+
use std::fs;
60+
use std::io::Read;
61+
use lightning_fuzz::utils::test_logger::StringBuffer;
62+
63+
use std::sync::{atomic, Arc};
64+
{
65+
let data: Vec<u8> = vec![0];
66+
fromstr_to_netaddress_run(data.as_ptr(), data.len());
67+
}
68+
let mut threads = Vec::new();
69+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70+
if let Ok(tests) = fs::read_dir("test_cases/fromstr_to_netaddress") {
71+
for test in tests {
72+
let mut data: Vec<u8> = Vec::new();
73+
let path = test.unwrap().path();
74+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
75+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
76+
77+
let thread_count_ref = Arc::clone(&threads_running);
78+
let main_thread_ref = std::thread::current();
79+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
80+
std::thread::spawn(move || {
81+
let string_logger = StringBuffer::new();
82+
83+
let panic_logger = string_logger.clone();
84+
let res = if ::std::panic::catch_unwind(move || {
85+
fromstr_to_netaddress_test(&data, panic_logger);
86+
}).is_err() {
87+
Some(string_logger.into_string())
88+
} else { None };
89+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
90+
main_thread_ref.unpark();
91+
res
92+
})
93+
));
94+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
95+
std::thread::park();
96+
}
97+
}
98+
}
99+
let mut failed_outputs = Vec::new();
100+
for (test, thread) in threads.drain(..) {
101+
if let Some(output) = thread.join().unwrap() {
102+
println!("\nOutput of {}:\n{}\n", test, output);
103+
failed_outputs.push(test);
104+
}
105+
}
106+
if !failed_outputs.is_empty() {
107+
println!("Test cases which failed: ");
108+
for case in failed_outputs {
109+
println!("{}", case);
110+
}
111+
panic!();
112+
}
113+
}

fuzz/src/bin/gen_target.sh

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ GEN_TEST router
2121
GEN_TEST zbase32
2222
GEN_TEST indexedmap
2323
GEN_TEST onion_hop_data
24+
GEN_TEST base32
25+
GEN_TEST fromstr_to_netaddress
2426

2527
GEN_TEST msg_accept_channel msg_targets::
2628
GEN_TEST msg_announcement_signatures msg_targets::

fuzz/src/fromstr_to_netaddress.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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::ln::msgs::NetAddress;
11+
use core::str::FromStr;
12+
13+
use crate::utils::test_logger;
14+
15+
#[inline]
16+
pub fn do_test(data: &[u8]) {
17+
if let Ok(s) = std::str::from_utf8(data) {
18+
let _ = NetAddress::from_str(s);
19+
}
20+
21+
}
22+
23+
pub fn fromstr_to_netaddress_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
24+
do_test(data);
25+
}
26+
27+
#[no_mangle]
28+
pub extern "C" fn fromstr_to_netaddress_run(data: *const u8, datalen: usize) {
29+
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
30+
}
31+

fuzz/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ pub mod refund_deser;
2929
pub mod router;
3030
pub mod zbase32;
3131
pub mod onion_hop_data;
32+
pub mod base32;
33+
pub mod fromstr_to_netaddress;
3234

3335
pub mod msg_targets;

fuzz/src/zbase32.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
use lightning::util::zbase32;
10+
use lightning::util::base32;
1111

1212
use crate::utils::test_logger;
1313

1414
#[inline]
1515
pub fn do_test(data: &[u8]) {
16-
let res = zbase32::encode(data);
17-
assert_eq!(&zbase32::decode(&res).unwrap()[..], data);
16+
let res = base32::Alphabet::ZBase32.encode(data);
17+
assert_eq!(&base32::Alphabet::ZBase32.decode(&res).unwrap()[..], data);
1818

1919
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());
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());
2223
}
2324
}
2425
}

fuzz/targets.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ void router_run(const unsigned char* data, size_t data_len);
1414
void zbase32_run(const unsigned char* data, size_t data_len);
1515
void indexedmap_run(const unsigned char* data, size_t data_len);
1616
void onion_hop_data_run(const unsigned char* data, size_t data_len);
17+
void base32_run(const unsigned char* data, size_t data_len);
18+
void fromstr_to_netaddress_run(const unsigned char* data, size_t data_len);
1719
void msg_accept_channel_run(const unsigned char* data, size_t data_len);
1820
void msg_announcement_signatures_run(const unsigned char* data, size_t data_len);
1921
void msg_channel_reestablish_run(const unsigned char* data, size_t data_len);

0 commit comments

Comments
 (0)