Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Make repository a workspace
Browse files Browse the repository at this point in the history
Make this repo a workspace so that `cargo` commands hit the `fuzz`
directory as well even though running `cargo test` in the `fuzz` crate
does nothing unless "fuzzing" is enabled. However one can run the fuzz
test with `RUSTFLAGS=--cfg=fuzzing cargo test --all`.

In order to make fuzz tests pass without "fuzzing" enabled do a few
things:

- Add a TIMEOUT const for the timeout duration
- feature guard the logic in the fuzz test, leaves an empty test when
  "fuzzing" is not enabled - comment the file to highlight this
- feature guard closed socket test
  • Loading branch information
tcharding committed May 16, 2023
1 parent d12af1a commit ef6e2f6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 0 additions & 4 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 19 additions & 18 deletions fuzz/fuzz_targets/simple_http.rs
Original file line number Diff line number Diff line change
@@ -1,28 +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::simple_http::SimpleHttpTransport;
use jsonrpc::simple_http::FUZZ_TCP_SOCK;
use jsonrpc::Client;
#[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")]
Expand Down
15 changes: 9 additions & 6 deletions src/simple_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ef6e2f6

Please sign in to comment.