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

Commit f44a535

Browse files
committed
simple_http: mock out TcpStream when fuzzing
1 parent 2a0903e commit f44a535

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/simple_http.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#[cfg(feature = "proxy")]
77
use socks::Socks5Stream;
88
use std::io::{BufRead, BufReader, Read, Write};
9+
#[cfg(not(fuzzing))]
910
use std::net::TcpStream;
1011
use std::net::{SocketAddr, ToSocketAddrs};
1112
use std::sync::{Arc, Mutex};
@@ -19,6 +20,43 @@ use serde_json;
1920
use crate::client::Transport;
2021
use crate::{Request, Response};
2122

23+
#[cfg(fuzzing)]
24+
/// Global mutex used by the fuzzing harness to inject data into the read
25+
/// end of the TCP stream.
26+
pub static FUZZ_TCP_SOCK: Mutex<Option<io::Cursor<Vec<u8>>>> = Mutex::new(None);
27+
28+
#[cfg(fuzzing)]
29+
#[derive(Clone, Debug)]
30+
struct TcpStream;
31+
32+
#[cfg(fuzzing)]
33+
mod impls {
34+
use super::*;
35+
impl Read for TcpStream {
36+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
37+
match *FUZZ_TCP_SOCK.lock().unwrap() {
38+
Some(ref mut cursor) => io::Read::read(cursor, buf),
39+
None => Ok(0),
40+
}
41+
}
42+
}
43+
impl Write for TcpStream {
44+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
45+
io::sink().write(buf)
46+
}
47+
fn flush(&mut self) -> io::Result<()> {
48+
Ok(())
49+
}
50+
}
51+
52+
impl TcpStream {
53+
pub fn connect_timeout(_: &SocketAddr, _: Duration) -> io::Result<Self> { Ok(TcpStream) }
54+
pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> { Ok(()) }
55+
pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> { Ok(()) }
56+
}
57+
}
58+
59+
2260
/// The default TCP port to use for connections.
2361
/// Set to 8332, the default RPC port for bitcoind.
2462
pub const DEFAULT_PORT: u16 = 8332;

0 commit comments

Comments
 (0)