diff --git a/Cargo.toml b/Cargo.toml index 8121ea52..8858d96c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,6 @@ serde_json = { version = "1", features = [ "raw_value" ] } base64 = { version = "0.13.0", optional = true } socks = { version = "0.3.4", optional = true} + +[workspace] +members = ["fuzz"] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c5b0d03b..bbf5a84d 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -14,10 +14,6 @@ honggfuzz_fuzz = ["honggfuzz"] honggfuzz = { version = "0.5", optional = true, default-features = false } jsonrpc = { path = ".." } -# Prevent this from interfering with workspaces -[workspace] -members = ["."] - [[bin]] name = "simple_http" path = "fuzz_targets/simple_http.rs" diff --git a/fuzz/fuzz_targets/simple_http.rs b/fuzz/fuzz_targets/simple_http.rs index 53812b0c..8b54bdbf 100644 --- a/fuzz/fuzz_targets/simple_http.rs +++ b/fuzz/fuzz_targets/simple_http.rs @@ -1,26 +1,29 @@ - extern crate jsonrpc; -#[cfg(not(fuzzing))] -compile_error!("You must set RUSTFLAGS=--cfg=fuzzing to run these test, or run the actual fuzz harness."); +// Note, tests are empty "fuzzing" is not set but still show up in output of `cargo test --workspace`. -use jsonrpc::Client; -use jsonrpc::simple_http::SimpleHttpTransport; -use jsonrpc::simple_http::FUZZ_TCP_SOCK; +#[allow(unused_variables)] // `data` is not used when "fuzzing" is not set. +fn do_test(data: &[u8]) { + #[cfg(fuzzing)] + { + use std::io; -use std::io; + use jsonrpc::simple_http::SimpleHttpTransport; + use jsonrpc::simple_http::FUZZ_TCP_SOCK; + use jsonrpc::Client; -fn do_test(data: &[u8]) { - *FUZZ_TCP_SOCK.lock().unwrap() = Some(io::Cursor::new(data.to_vec())); + *FUZZ_TCP_SOCK.lock().unwrap() = Some(io::Cursor::new(data.to_vec())); - let t = SimpleHttpTransport::builder() - .url("localhost:123").expect("parse url") - .auth("", None) - .build(); + let t = SimpleHttpTransport::builder() + .url("localhost:123") + .expect("parse url") + .auth("", None) + .build(); - let client = Client::with_transport(t); - let request = client.build_request("uptime", &[]); - let _ = client.send_request(request); + let client = Client::with_transport(t); + let request = client.build_request("uptime", &[]); + let _ = client.send_request(request); + } } #[cfg(feature = "honggfuzz")] diff --git a/src/simple_http.rs b/src/simple_http.rs index f907ea79..8314f6d5 100644 --- a/src/simple_http.rs +++ b/src/simple_http.rs @@ -28,6 +28,12 @@ pub const DEFAULT_PROXY_PORT: u16 = 9050; /// Absolute maximum content length allowed before cutting off the response. const FINAL_RESP_ALLOC: u64 = 1024 * 1024 * 1024; +#[cfg(not(fuzzing))] +const DEFAULT_TIMEOUT: Duration = Duration::from_secs(15); + +#[cfg(fuzzing)] +const DEFAULT_TIMEOUT: Duration = Duration::from_millis(1); + /// Simple HTTP transport that implements the necessary subset of HTTP for /// running a bitcoind RPC client. #[derive(Clone, Debug)] @@ -52,10 +58,7 @@ impl Default for SimpleHttpTransport { DEFAULT_PORT, ), path: "/".to_owned(), - #[cfg(fuzzing)] - timeout: Duration::from_millis(1), - #[cfg(not(fuzzing))] - timeout: Duration::from_secs(15), + timeout: DEFAULT_TIMEOUT, basic_auth: None, #[cfg(feature = "proxy")] proxy_addr: net::SocketAddr::new( @@ -719,7 +722,7 @@ mod tests { let builder = Builder::new().url(u).unwrap_or_else(|_| panic!("error for: {}", u)); assert_eq!(builder.tp.addr, addr); assert_eq!(builder.tp.path, path); - assert_eq!(builder.tp.timeout, Duration::from_secs(15)); + assert_eq!(builder.tp.timeout, DEFAULT_TIMEOUT); assert_eq!(builder.tp.basic_auth, None); #[cfg(feature = "proxy")] assert_eq!(builder.tp.proxy_addr, SocketAddr::from_str("127.0.0.1:9050").unwrap()); @@ -778,7 +781,7 @@ mod tests { /// Test that the client will detect that a socket is closed and open a fresh one before sending /// the request - #[cfg(not(feature = "proxy"))] + #[cfg(all(not(feature = "proxy"), not(fuzzing)))] #[test] fn request_to_closed_socket() { let (tx, rx) = mpsc::sync_channel(1);