Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target
corpus
artifacts
41 changes: 41 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

[package]
name = "tendril-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
publish = false

[package.metadata]
cargo-fuzz = true

[dependencies.tendril]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"

[dependencies]
rand = "0.4"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "string_bytes_eq_tendril"
path = "fuzz_targets/string_bytes_eq_tendril.rs"

[[bin]]
name = "pop_front"
path = "fuzz_targets/pop_front.rs"

[[bin]]
name = "pop_back"
path = "fuzz_targets/pop_back.rs"

[[bin]]
name = "subtendril"
path = "fuzz_targets/subtendril.rs"

[[bin]]
name = "try_push_char"
path = "fuzz_targets/try_push_char.rs"
48 changes: 48 additions & 0 deletions fuzz/fuzz_targets/pop_back.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple fuzz tester for the library.
#![no_main]
#![deny(warnings)]
#[macro_use]
extern crate libfuzzer_sys;

extern crate tendril;
extern crate rand;

use rand::Rng;
use tendril::StrTendril;
use std::convert::TryInto;
use rand::distributions::{IndependentSample, Range};


fuzz_target!(|data: &[u8]| {
// prelude
let capacity= data.len();
let mut buf_string = String::with_capacity(capacity as usize);
let mut buf_tendril = StrTendril::with_capacity(capacity.try_into().unwrap());
if let Ok(str) = std::str::from_utf8(&data) {
buf_string.push_str(&str);
buf_tendril.push_slice(&str);

// test pop_front
let mut rng = rand::thread_rng();
let new_len = random_boundary(&mut rng, &buf_string);
let n = buf_string.len() - new_len;
buf_string.truncate(new_len);
buf_tendril.pop_back(n as u32);
assert_eq!(&*buf_string, &*buf_tendril);
}
});

fn random_boundary<R: Rng>(rng: &mut R, text: &str) -> usize {
loop {
let i = Range::new(0, text.len() + 1).ind_sample(rng);
if text.is_char_boundary(i) {
return i;
}
}
}
46 changes: 46 additions & 0 deletions fuzz/fuzz_targets/pop_front.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple fuzz tester for the library.
#![no_main]
#![deny(warnings)]
#[macro_use] extern crate libfuzzer_sys;

extern crate tendril;
extern crate rand;

use rand::Rng;
use tendril::StrTendril;
use std::convert::TryInto;
use rand::distributions::{IndependentSample, Range};


fuzz_target!(|data: &[u8]| {
// prelude
let capacity= data.len();
let mut buf_string = String::with_capacity(capacity as usize);
let mut buf_tendril = StrTendril::with_capacity(capacity.try_into().unwrap());
if let Ok(str) = std::str::from_utf8(&data) {
buf_string.push_str(&str);
buf_tendril.push_slice(&str);

// test pop_front
let mut rng = rand::thread_rng();
let n = random_boundary(&mut rng, &buf_string);
buf_tendril.pop_front(n as u32);
buf_string = buf_string[n..].to_owned();
assert_eq!(&*buf_string, &*buf_tendril);
}
});

fn random_boundary<R: Rng>(rng: &mut R, text: &str) -> usize {
loop {
let i = Range::new(0, text.len()+1).ind_sample(rng);
if text.is_char_boundary(i) {
return i;
}
}
}
26 changes: 26 additions & 0 deletions fuzz/fuzz_targets/string_bytes_eq_tendril.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple fuzz tester for the library.
#![no_main]
#![deny(warnings)]
#[macro_use]
extern crate libfuzzer_sys;

extern crate tendril;
use tendril::StrTendril;
use std::convert::TryInto;

fuzz_target!(|data: &[u8]| {
let capacity = data.len();
let mut buf_string = String::with_capacity(capacity as usize);
let mut buf_tendril = StrTendril::with_capacity(capacity.try_into().unwrap());
if let Ok(str) = std::str::from_utf8(&data) {
buf_string.push_str(&str);
buf_tendril.push_slice(&str);
assert_eq!(&*buf_string, &*buf_tendril);
}
});
53 changes: 53 additions & 0 deletions fuzz/fuzz_targets/subtendril.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple fuzz tester for the library.
#![no_main]
#![deny(warnings)]
#[macro_use]
extern crate libfuzzer_sys;

extern crate tendril;
extern crate rand;

use rand::Rng;
use tendril::StrTendril;
use std::convert::TryInto;
use rand::distributions::{IndependentSample, Range};


fuzz_target!(|data: &[u8]| {
// prelude
let capacity= data.len();
let mut buf_string = String::with_capacity(capacity as usize);
let mut buf_tendril = StrTendril::with_capacity(capacity.try_into().unwrap());
if let Ok(str) = std::str::from_utf8(&data) {
buf_string.push_str(&str);
buf_tendril.push_slice(&str);

// test subtendril
let mut rng = rand::thread_rng();
let (start, end) = random_slice(&mut rng, &buf_string);
buf_string = buf_string[start..end].to_owned();
buf_tendril = buf_tendril.subtendril(start as u32, (end - start) as u32);
assert_eq!(&*buf_string, &*buf_tendril);

}
});

fn random_slice<R: Rng>(rng: &mut R, text: &str) -> (usize, usize) {
loop {
let start = Range::new(0, text.len() + 1).ind_sample(rng);
let end = Range::new(start, text.len() + 1).ind_sample(rng);
if !text.is_char_boundary(start) {
continue;
}
if end < text.len() && !text.is_char_boundary(end) {
continue;
}
return (start, end);
}
}
37 changes: 37 additions & 0 deletions fuzz/fuzz_targets/try_push_char.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple fuzz tester for the library.
#![no_main]
#![deny(warnings)]
#[macro_use]
extern crate libfuzzer_sys;

extern crate tendril;
extern crate rand;

use rand::Rng;
use tendril::StrTendril;
use std::convert::TryInto;


fuzz_target!(|data: &[u8]| {
// prelude
let capacity= data.len();
let mut buf_string = String::with_capacity(capacity as usize);
let mut buf_tendril = StrTendril::with_capacity(capacity.try_into().unwrap());
if let Ok(str) = std::str::from_utf8(&data) {
buf_string.push_str(&str);
buf_tendril.push_slice(&str);

// test try_push_char
let mut rng = rand::thread_rng();
let c = rng.gen();
buf_string.push(c);
assert!(buf_tendril.try_push_char(c).is_ok());
assert_eq!(&*buf_string, &*buf_tendril);
}
});