-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathschema-roundtrip.rs
96 lines (88 loc) · 3.15 KB
/
schema-roundtrip.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![no_main]
use cedar_drt_inner::schemas::equivalence_check;
use cedar_drt_inner::*;
use cedar_policy_core::{ast, extensions::Extensions};
use cedar_policy_generators::{
schema::{downgrade_frag_to_raw, Schema},
settings::ABACSettings,
};
use cedar_policy_validator::json_schema;
use libfuzzer_sys::arbitrary::{self, Arbitrary, Unstructured};
use serde::Serialize;
use similar_asserts::SimpleDiff;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Serialize)]
struct Input {
pub schema: json_schema::Fragment<ast::InternalName>,
}
/// settings for this fuzz target
const SETTINGS: ABACSettings = ABACSettings {
match_types: false,
enable_extensions: true,
max_depth: 3,
max_width: 7,
enable_additional_attributes: false,
enable_like: true,
// ABAC fuzzing restricts the use of action because it is used to generate
// the corpus tests which will be run on Cedar and CedarCLI.
// These packages only expose the restricted action behavior.
enable_action_groups_and_attrs: false,
enable_arbitrary_func_call: true,
enable_unknowns: false,
enable_action_in_constraints: true,
enable_unspecified_apply_spec: true,
// It's Ok to enable this flag because this target is PBT.
enable_datetime_extension: true,
};
impl<'a> Arbitrary<'a> for Input {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let arb_schema = Schema::arbitrary(SETTINGS.clone(), u)?;
let namespace = arb_schema.schema;
let name = arb_schema.namespace;
let schema = json_schema::Fragment(BTreeMap::from([(name, namespace)]));
Ok(Self { schema })
}
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Schema::arbitrary_size_hint(depth)
}
}
fuzz_target!(|i: Input| {
let src = i
.schema
.to_cedarschema()
.expect("Failed to convert schema into a human readable schema");
let (parsed, _) =
json_schema::Fragment::from_cedarschema_str(&src, Extensions::all_available())
.expect("Failed to parse converted human readable schema");
let downgraded = downgrade_frag_to_raw(i.schema);
if let Err(msg) = equivalence_check(&downgraded, &parsed) {
println!("Schema: {src}");
println!(
"{}",
SimpleDiff::from_str(
&format!("{:#?}", downgraded),
&format!("{:#?}", parsed),
"Initial Schema",
"Cedar Round tripped"
)
);
panic!("{msg}");
}
});