-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff7ed59
commit ff734c7
Showing
12 changed files
with
183 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,11 @@ | ||
#![no_main] | ||
use arbitrary::Arbitrary; | ||
use chrono::{DateTime, Datelike}; | ||
use libfuzzer_sys::{fuzz_target, Corpus}; | ||
|
||
use std::fmt::Debug; | ||
|
||
use opening_hours::localization::{Coordinates, Localize}; | ||
use opening_hours::{Context, OpeningHours}; | ||
|
||
#[derive(Arbitrary, Clone, Debug)] | ||
pub enum CompareWith { | ||
Stringified, | ||
Normalized, | ||
} | ||
|
||
#[derive(Arbitrary, Clone, Debug)] | ||
pub enum Operation { | ||
DoubleNormalize, | ||
Compare(CompareWith), | ||
} | ||
|
||
#[derive(Arbitrary, Clone)] | ||
pub struct Data { | ||
date_secs: i64, | ||
oh: String, | ||
coords: Option<[i16; 2]>, | ||
operation: Operation, | ||
} | ||
|
||
impl Data { | ||
fn coords_float(&self) -> Option<[f64; 2]> { | ||
self.coords.map(|coords| { | ||
[ | ||
90.0 * coords[0] as f64 / (i16::MAX as f64 + 1.0), | ||
180.0 * coords[1] as f64 / (i16::MAX as f64 + 1.0), | ||
] | ||
}) | ||
} | ||
} | ||
|
||
impl Debug for Data { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut debug = f.debug_struct("Data"); | ||
|
||
if let Some(date) = DateTime::from_timestamp(self.date_secs, 0) { | ||
debug.field("date", &date.naive_utc()); | ||
} | ||
|
||
debug.field("operation", &self.operation); | ||
debug.field("oh", &self.oh); | ||
|
||
if let Some(coords) = &self.coords_float() { | ||
debug.field("coords", coords); | ||
} | ||
|
||
debug.finish() | ||
} | ||
} | ||
use opening_hours::fuzzing::{run_fuzz_oh, Data}; | ||
|
||
fuzz_target!(|data: Data| -> Corpus { | ||
if data.oh.contains('=') { | ||
// The fuzzer spends way too much time building comments. | ||
return Corpus::Reject; | ||
} | ||
|
||
let Some(date) = DateTime::from_timestamp(data.date_secs, 0) else { | ||
return Corpus::Reject; | ||
}; | ||
|
||
let date = date.naive_utc(); | ||
|
||
if date.year() < 1900 || date.year() > 9999 { | ||
return Corpus::Reject; | ||
if run_fuzz_oh(data) { | ||
Corpus::Keep | ||
} else { | ||
Corpus::Reject | ||
} | ||
|
||
let Ok(oh_1) = data.oh.parse::<OpeningHours>() else { | ||
return Corpus::Reject; | ||
}; | ||
|
||
match &data.operation { | ||
Operation::DoubleNormalize => { | ||
let normalized = oh_1.normalize(); | ||
assert_eq!(normalized, normalized.clone().normalize()); | ||
} | ||
Operation::Compare(compare_with) => { | ||
let oh_2 = match compare_with { | ||
CompareWith::Normalized => oh_1.normalize(), | ||
CompareWith::Stringified => oh_1.to_string().parse().unwrap_or_else(|err| { | ||
eprintln!("[ERR] Initial Expression: {}", data.oh); | ||
eprintln!("[ERR] Invalid stringified Expression: {oh_1}"); | ||
panic!("{err}") | ||
}), | ||
}; | ||
|
||
if let Some([lat, lon]) = data.coords_float() { | ||
let ctx = Context::from_coords(Coordinates::new(lat, lon).unwrap()); | ||
let date = ctx.locale.datetime(date); | ||
let oh_1 = oh_1.with_context(ctx.clone()); | ||
let oh_2 = oh_2.with_context(ctx.clone()); | ||
|
||
assert_eq!(oh_1.state(date), oh_2.state(date)); | ||
assert_eq!(oh_1.next_change(date), oh_2.next_change(date)); | ||
} else { | ||
assert_eq!(oh_1.state(date), oh_2.state(date)); | ||
assert_eq!(oh_1.next_change(date), oh_2.next_change(date)); | ||
} | ||
} | ||
} | ||
|
||
Corpus::Keep | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use arbitrary::Arbitrary; | ||
use chrono::{DateTime, Datelike}; | ||
|
||
use std::fmt::Debug; | ||
|
||
use crate::localization::{Coordinates, Localize}; | ||
use crate::{Context, OpeningHours}; | ||
|
||
#[derive(Arbitrary, Clone, Debug)] | ||
pub enum CompareWith { | ||
Stringified, | ||
Normalized, | ||
} | ||
|
||
#[derive(Arbitrary, Clone, Debug)] | ||
pub enum Operation { | ||
DoubleNormalize, | ||
Compare(CompareWith), | ||
} | ||
|
||
#[derive(Arbitrary, Clone)] | ||
pub struct Data { | ||
date_secs: i64, | ||
oh: String, | ||
coords: Option<[i16; 2]>, | ||
operation: Operation, | ||
} | ||
|
||
impl Data { | ||
fn coords_float(&self) -> Option<[f64; 2]> { | ||
self.coords.map(|coords| { | ||
[ | ||
90.0 * coords[0] as f64 / (i16::MAX as f64 + 1.0), | ||
180.0 * coords[1] as f64 / (i16::MAX as f64 + 1.0), | ||
] | ||
}) | ||
} | ||
} | ||
|
||
impl Debug for Data { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut debug = f.debug_struct("Data"); | ||
|
||
if let Some(date) = DateTime::from_timestamp(self.date_secs, 0) { | ||
debug.field("date", &date.naive_utc()); | ||
} | ||
|
||
debug.field("operation", &self.operation); | ||
debug.field("oh", &self.oh); | ||
|
||
if let Some(coords) = &self.coords_float() { | ||
debug.field("coords", coords); | ||
} | ||
|
||
debug.finish() | ||
} | ||
} | ||
|
||
pub fn run_fuzz_oh(data: Data) -> bool { | ||
if data.oh.contains('=') { | ||
// The fuzzer spends way too much time building comments. | ||
return false; | ||
} | ||
|
||
let Some(date) = DateTime::from_timestamp(data.date_secs, 0) else { | ||
return false; | ||
}; | ||
|
||
let date = date.naive_utc(); | ||
|
||
if date.year() < 1900 || date.year() > 9999 { | ||
return false; | ||
} | ||
|
||
let Ok(oh_1) = data.oh.parse::<OpeningHours>() else { | ||
return false; | ||
}; | ||
|
||
match &data.operation { | ||
Operation::DoubleNormalize => { | ||
let normalized = oh_1.normalize(); | ||
assert_eq!(normalized, normalized.clone().normalize()); | ||
} | ||
Operation::Compare(compare_with) => { | ||
let oh_2 = match compare_with { | ||
CompareWith::Normalized => oh_1.normalize(), | ||
CompareWith::Stringified => oh_1.to_string().parse().unwrap_or_else(|err| { | ||
eprintln!("[ERR] Initial Expression: {}", data.oh); | ||
eprintln!("[ERR] Invalid stringified Expression: {oh_1}"); | ||
panic!("{err}") | ||
}), | ||
}; | ||
|
||
if let Some([lat, lon]) = data.coords_float() { | ||
let ctx = Context::from_coords(Coordinates::new(lat, lon).unwrap()); | ||
let date = ctx.locale.datetime(date); | ||
let oh_1 = oh_1.with_context(ctx.clone()); | ||
let oh_2 = oh_2.with_context(ctx.clone()); | ||
|
||
assert_eq!(oh_1.state(date), oh_2.state(date)); | ||
assert_eq!(oh_1.next_change(date), oh_2.next_change(date)); | ||
} else { | ||
assert_eq!(oh_1.state(date), oh_2.state(date)); | ||
assert_eq!(oh_1.next_change(date), oh_2.next_change(date)); | ||
} | ||
} | ||
} | ||
|
||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::path::Path; | ||
|
||
use arbitrary::{Arbitrary, Unstructured}; | ||
|
||
use crate::fuzzing::{run_fuzz_oh, Data}; | ||
|
||
#[test] | ||
fn run_fuzz_corpus() { | ||
let path = Path::new(env!("CARGO_MANIFEST_DIR")) | ||
.join("fuzz") | ||
.join("corpus") | ||
.join("fuzz_oh"); | ||
|
||
let dir = std::fs::read_dir(path).expect("could not open corpus directory"); | ||
|
||
for entry in dir { | ||
let entry = entry.expect("failed to iter corpus directory"); | ||
eprintln!("Running fuzz corpus file {:?}", entry.path()); | ||
let mut file = File::open(entry.path()).expect("failed to open corpus example"); | ||
let mut bytes = Vec::new(); | ||
file.read_to_end(&mut bytes).expect("failed to read file"); | ||
let data = Data::arbitrary(&mut Unstructured::new(&bytes)).expect("could not parse corpus"); | ||
eprintln!("Input: {data:?}"); | ||
let output = run_fuzz_oh(data); | ||
eprintln!("Output: {output:?}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters