Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/mattmccutchen…
Browse files Browse the repository at this point in the history
…-amazon/overflow-validation
  • Loading branch information
john-h-kastner-aws committed Oct 13, 2023
2 parents fe99696 + 1890515 commit 33eee68
Show file tree
Hide file tree
Showing 3 changed files with 681 additions and 803 deletions.
2 changes: 1 addition & 1 deletion cedar-dafny/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dafny": {
"version": "4.2.0",
"version": "4.3.0",
"commands": [
"dafny"
]
Expand Down
40 changes: 33 additions & 7 deletions cedar-policy-generators/src/abac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,40 @@ pub struct ConstantPool {
string_constants: Vec<SmolStr>,
}

#[derive(Debug, Clone, Copy)]
struct BiasedI64(i64);

impl<'a> Arbitrary<'a> for BiasedI64 {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(gen!(u,
1 => std::i64::MAX,
1 => std::i64::MIN,
1 => -1,
1 => 0,
6 => <i64 as Arbitrary>::arbitrary(u)?
)
.into())
}
}

impl From<i64> for BiasedI64 {
fn from(value: i64) -> Self {
Self(value)
}
}

impl From<BiasedI64> for i64 {
fn from(value: BiasedI64) -> Self {
value.0
}
}

impl<'a> Arbitrary<'a> for ConstantPool {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let sc: Vec<String> = u.arbitrary()?;
Ok(Self {
int_constants: u.arbitrary()?,
int_constants: <Vec<BiasedI64> as Arbitrary>::arbitrary(u)
.map(|bis| bis.into_iter().map(|bi| bi.into()).collect::<Vec<i64>>())?,
string_constants: sc.iter().map(|s| s.into()).collect(),
})
}
Expand Down Expand Up @@ -219,8 +248,7 @@ impl ConstantPool {

// Generate a valid IPv4 net representation
fn arbitrary_ipv4_str(&self, u: &mut Unstructured<'_>) -> Result<String> {
let bytes: [u8; 4] = u.bytes(4)?.try_into().unwrap();
let ip = Ipv4Addr::from(bytes);
let ip: Ipv4Addr = u.arbitrary()?;
// Produce a CIDR notation out of 50% probability
Ok(if u.ratio(1, 2)? {
ip.to_string()
Expand All @@ -233,8 +261,7 @@ impl ConstantPool {

// Generate a valid IPv6 net representation
fn arbitrary_ipv6_str(&self, u: &mut Unstructured<'_>) -> Result<String> {
let bytes: [u8; 16] = u.bytes(16)?.try_into().unwrap();
let ip = Ipv6Addr::from(bytes);
let ip: Ipv6Addr = u.arbitrary()?;
// Produce a CIDR notation out of a 50% probability
Ok(if u.ratio(1, 2)? {
ip.to_string()
Expand All @@ -257,8 +284,7 @@ impl ConstantPool {

/// Generate a valid decimal number representation and mutate it
pub fn arbitrary_decimal_str(&self, u: &mut Unstructured<'_>) -> Result<SmolStr> {
let bytes = u.bytes(8)?;
let i = i64::from_be_bytes(bytes.try_into().unwrap());
let i = self.arbitrary_int_constant(u)?;
mutate_str(
u,
// Replicate from Core
Expand Down
Loading

0 comments on commit 33eee68

Please sign in to comment.