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

Commit

Permalink
Merge #98: Add workspace
Browse files Browse the repository at this point in the history
ef6e2f6 Make repository a workspace (Tobin C. Harding)
d12af1a Run the formatter (Tobin C. Harding)

Pull request description:

  As we are doing in other repos; add a workspace and move current code into `jsonrpc` crate. Add `fuzz` to the workspace too.

  Draft because this isn't quite right. Was being able to run `cargo test` in the `fuzz` crate the main motivation for creating a workspace? Here that doesn't work because there is no code in the fuzz test to run if we are not fuzzing (because of `FUZZ_TCP_SOCKET`).

  Throwing this up just for input please.

ACKs for top commit:
  apoelstra:
    ACK ef6e2f6

Tree-SHA512: f2ccd577b112023c29bf5b1873a3a4c6f87624efda028ad189c503a4f7110b9944d2e06ae14684453e5cd1e40dc6a16cda6d6ad976fd6449fa52dab54f3f457e
  • Loading branch information
apoelstra committed May 19, 2023
2 parents 18e94b2 + ef6e2f6 commit 51e87f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 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
35 changes: 19 additions & 16 deletions fuzz/fuzz_targets/simple_http.rs
Original file line number Diff line number Diff line change
@@ -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")]
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 51e87f7

Please sign in to comment.