Add fuzzing harness via cargo-fuzz with three distinct targets

pull/44/head
Sergey "Shnatsel" Davidoff 7 years ago
parent e4a52cde05
commit 465ba41116
  1. 30
      fuzz/Cargo.toml
  2. 12
      fuzz/fuzz_targets/parse_frame.rs
  3. 37
      fuzz/fuzz_targets/read_message_client.rs
  4. 37
      fuzz/fuzz_targets/read_message_server.rs

@ -0,0 +1,30 @@
[package]
name = "tungstenite-fuzz"
version = "0.0.1"
authors = ["Sergey \"Shnatsel\" Davidoff <shnatsel@gmail.com>"]
publish = false
[package.metadata]
cargo-fuzz = true
[dependencies.tungstenite]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "parse_frame"
path = "fuzz_targets/parse_frame.rs"
[[bin]]
name = "read_message_server"
path = "fuzz_targets/read_message_server.rs"
[[bin]]
name = "read_message_client"
path = "fuzz_targets/read_message_client.rs"

@ -0,0 +1,12 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate tungstenite;
use std::io::Cursor;
fuzz_target!(|data: &[u8]| {
let vector: Vec<u8> = data.into();
let mut cursor = Cursor::new(vector);
tungstenite::protocol::frame::Frame::parse(&mut cursor);
});

@ -0,0 +1,37 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate tungstenite;
use std::io;
use std::io::Cursor;
use tungstenite::WebSocket;
use tungstenite::protocol::Role;
//use std::result::Result;
// FIXME: copypasted from tungstenite's protocol/mod.rs
struct WriteMoc<Stream>(Stream);
impl<Stream> io::Write for WriteMoc<Stream> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<Stream: io::Read> io::Read for WriteMoc<Stream> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
// end of copypasta
fuzz_target!(|data: &[u8]| {
//let vector: Vec<u8> = data.into();
let cursor = Cursor::new(data);
let mut socket = WebSocket::from_raw_socket(WriteMoc(cursor), Role::Client, None);
socket.read_message();
});

@ -0,0 +1,37 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate tungstenite;
use std::io;
use std::io::Cursor;
use tungstenite::WebSocket;
use tungstenite::protocol::Role;
//use std::result::Result;
// FIXME: copypasted from tungstenite's protocol/mod.rs
struct WriteMoc<Stream>(Stream);
impl<Stream> io::Write for WriteMoc<Stream> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<Stream: io::Read> io::Read for WriteMoc<Stream> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
// end of copypasta
fuzz_target!(|data: &[u8]| {
//let vector: Vec<u8> = data.into();
let cursor = Cursor::new(data);
let mut socket = WebSocket::from_raw_socket(WriteMoc(cursor), Role::Server, None);
socket.read_message();
});
Loading…
Cancel
Save