Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: policy expr type errors due to X.0 floating points in JSON being treated as ints #451

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
1 change: 0 additions & 1 deletion hipcheck/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ pub fn start_plugins(
)?;

let current_arch = get_current_arch();
println!("CURRENT ARCH: {}", current_arch);

// retrieve, verify and extract all required plugins
let required_plugin_names = retrieve_plugins(&policy_file.plugins.0, plugin_cache)?;
Expand Down
15 changes: 10 additions & 5 deletions hipcheck/src/policy_exprs/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ fn partially_evaluate(fn_name: &'static str, arg: Expr) -> Result<Expr> {
Ok(lambda)
}

pub fn upcast(i: i64) -> Primitive {
Float(F64::new(i as f64).unwrap())
}

/// Define binary operations on primitives.
fn binary_primitive_op<F>(name: &'static str, env: &Env, args: &[Expr], op: F) -> Result<Expr>
where
Expand All @@ -164,8 +168,9 @@ where
};

let primitive = match (&arg_1, &arg_2) {
(Int(_), Int(_)) | (Float(_), Float(_)) | (Bool(_), Bool(_)) => op(arg_1, arg_2)?,
_ => return Err(Error::BadType(name)),
(Int(i), Float(_)) => op(upcast(*i), arg_2)?,
(Float(_), Int(i)) => op(arg_1, upcast(*i))?,
_ => op(arg_1, arg_2)?,
};

Ok(Primitive(primitive))
Expand Down Expand Up @@ -454,9 +459,9 @@ fn add(env: &Env, args: &[Expr]) -> Result<Expr> {
let op = |arg_1, arg_2| match (arg_1, arg_2) {
(Int(arg_1), Int(arg_2)) => Ok(Int(arg_1 + arg_2)),
(Float(arg_1), Float(arg_2)) => Ok(Float(arg_1 + arg_2)),
(DateTime(arg_1), Span(arg_2)) => Ok(DateTime(
arg_1
.checked_add(arg_2)
// Span or DateTime can come first
(DateTime(dt), Span(s)) | (Span(s), DateTime(dt)) => Ok(DateTime(
dt.checked_add(s)
.map_err(|err| Error::Datetime(err.to_string()))?,
)),
(Span(arg_1), Span(arg_2)) => Ok(Span(
Expand Down
28 changes: 27 additions & 1 deletion hipcheck/src/policy_exprs/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use nom::{
use ordered_float::NotNan;
use std::{fmt::Display, ops::Deref};

#[cfg(test)]
use jiff::civil::Date;

/// A `deke` expression to evaluate.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Expr {
Expand Down Expand Up @@ -318,13 +321,25 @@ mod tests {
let input = "2024-09-17T09:30:00-05";
let result = parse(input).unwrap();

let ts: Timestamp = "2024-09-17T09:30:00-05".parse().unwrap();
let ts: Timestamp = input.parse().unwrap();
let dt = Zoned::new(ts, TimeZone::UTC);
let expected = datetime(dt).into_expr();

assert_eq!(result, expected);
}

#[test]
fn parse_simple_datetime() {
let input = "2024-09-17";
let result = parse(input).unwrap();

let ts: Date = input.parse().unwrap();
let dt = ts.to_zoned(TimeZone::UTC).unwrap();
let expected = datetime(dt).into_expr();

assert_eq!(result, expected);
}

#[test]
fn parse_span() {
let input = "P2W4DT1H30M";
Expand All @@ -336,6 +351,17 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn parse_simple_span() {
let input = "P2w";
let result = parse(input).unwrap();

let raw_span: Span = "P14d".parse().unwrap();
let expected = span(raw_span).into_expr();

assert_eq!(result, expected);
}

#[test]
fn parse_function() {
let input = "(add 2 3)";
Expand Down
33 changes: 33 additions & 0 deletions hipcheck/src/policy_exprs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,37 @@ mod tests {
])
);
}

#[test]
fn eval_upcasted_int() {
let program_and_expected = vec![
("(lte 3 3.0)", Expr::Primitive(Primitive::Bool(true))),
(
"(add 3 5.5)",
Expr::Primitive(Primitive::Float(F64::new(8.5).unwrap())),
),
];
let context = Value::Null;
for (program, expected) in program_and_expected.into_iter() {
let result = Executor::std().parse_and_eval(program, &context).unwrap();
assert_eq!(result, expected);
}
}

#[test]
fn eval_datetime_span_add() {
let date = "2024-09-26";
let span = "P1w";
let eval_fmt = "(add {} {})";
let context = Value::Null;
let expected = parse("2024-10-03").unwrap();
let result1 = Executor::std()
.parse_and_eval(format!("(add {} {})", date, span).as_str(), &context)
.unwrap();
assert_eq!(expected, result1);
let result2 = Executor::std()
.parse_and_eval(format!("(add {} {})", span, date).as_str(), &context)
.unwrap();
assert_eq!(expected, result2);
}
}