From a8f666fb5dc7dab2d1a157c2c55c9af960b04e30 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 19 Dec 2022 21:12:57 +0100 Subject: [PATCH] Fuzzer: compares SPARQL evaluation with and without the optimizer --- fuzz/fuzz_targets/sparql_eval.rs | 46 +++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/fuzz/fuzz_targets/sparql_eval.rs b/fuzz/fuzz_targets/sparql_eval.rs index 40c32d15..969bafe1 100644 --- a/fuzz/fuzz_targets/sparql_eval.rs +++ b/fuzz/fuzz_targets/sparql_eval.rs @@ -1,8 +1,9 @@ #![no_main] + use lazy_static::lazy_static; use libfuzzer_sys::fuzz_target; use oxigraph::io::DatasetFormat; -use oxigraph::sparql::Query; +use oxigraph::sparql::{Query, QueryOptions, QueryResults, QuerySolutionIter}; use oxigraph::store::Store; lazy_static! { @@ -20,7 +21,46 @@ lazy_static! { } fuzz_target!(|data: sparql_smith::Query| { - if let Ok(q) = Query::parse(&data.to_string(), None) { - STORE.query(q).unwrap(); + let query_str = data.to_string(); + if let Ok(query) = Query::parse(&query_str, None) { + let options = QueryOptions::default(); + let with_opt = STORE.query_opt(query.clone(), options.clone()).unwrap(); + let without_opt = STORE + .query_opt(query, options.without_optimizations()) + .unwrap(); + match (with_opt, without_opt) { + (QueryResults::Solutions(with_opt), QueryResults::Solutions(without_opt)) => { + assert_eq!( + query_solutions_key(with_opt, query_str.contains(" REDUCED ")), + query_solutions_key(without_opt, query_str.contains(" REDUCED ")) + ) + } + (QueryResults::Graph(_), QueryResults::Graph(_)) => unimplemented!(), + (QueryResults::Boolean(with_opt), QueryResults::Boolean(without_opt)) => { + assert_eq!(with_opt, without_opt) + } + _ => panic!("Different query result types"), + } } }); + +fn query_solutions_key(iter: QuerySolutionIter, is_reduced: bool) -> String { + // TODO: ordering + let mut b = iter + .into_iter() + .map(|t| { + let mut b = t + .unwrap() + .iter() + .map(|(var, val)| format!("{}: {}", var, val)) + .collect::>(); + b.sort_unstable(); + b.join(" ") + }) + .collect::>(); + b.sort_unstable(); + if is_reduced { + b.dedup(); + } + b.join("\n") +}