Skip to content

Commit

Permalink
Assert on the path requested when accessing well known attr
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 2, 2024
1 parent 9b6766f commit eab58b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
55 changes: 43 additions & 12 deletions src/data/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,14 @@ pub mod data {
#[cfg(test)]
mod tests {
use crate::data::cel::{known_attribute_for, Expression, Predicate};
use crate::data::property;
use cel_interpreter::objects::ValueType;

#[test]
fn predicates() {
let predicate = Predicate::new("source.port == 65432").expect("This is valid CEL!");
super::super::property::test::TEST_PROPERTY_VALUE.set(Some(65432_i64.to_le_bytes().into()));
property::test::TEST_PROPERTY_VALUE
.set(Some(("source.port".into(), 65432_i64.to_le_bytes().into())));
assert!(predicate.test());
}

Expand All @@ -470,36 +472,65 @@ mod tests {

#[test]
fn expressions_to_json_resolve() {
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("true".bytes().collect()));
property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec![
"filter_state",
"wasm.kuadrant.auth.identity.anonymous",
]),
"true".bytes().collect(),
)));
let value = Expression::new("auth.identity.anonymous").unwrap().eval();
assert_eq!(value, true.into());
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("42".bytes().collect()));

property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]),
"42".bytes().collect(),
)));
let value = Expression::new("auth.identity.age").unwrap().eval();
assert_eq!(value, 42.into());
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("42.3".bytes().collect()));

property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]),
"42.3".bytes().collect(),
)));
let value = Expression::new("auth.identity.age").unwrap().eval();
assert_eq!(value, 42.3.into());
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("\"John\"".bytes().collect()));
let value = Expression::new("auth.identity.name").unwrap().eval();
assert_eq!(value, "John".into());
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("-42".bytes().collect()));

property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]),
"\"John\"".bytes().collect(),
)));
let value = Expression::new("auth.identity.age").unwrap().eval();
assert_eq!(value, "John".into());

property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.name"]),
"-42".bytes().collect(),
)));
let value = Expression::new("auth.identity.name").unwrap().eval();
assert_eq!(value, (-42).into());

// let's fall back to strings, as that's what we read and set in store_metadata
super::super::property::test::TEST_PROPERTY_VALUE
.set(Some("some random crap".bytes().collect()));
property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]),
"some random crap".bytes().collect(),
)));
let value = Expression::new("auth.identity.age").unwrap().eval();
assert_eq!(value, "some random crap".into());
}

#[test]
fn attribute_resolve() {
super::super::property::test::TEST_PROPERTY_VALUE.set(Some(80_i64.to_le_bytes().into()));
property::test::TEST_PROPERTY_VALUE.set(Some((
"destination.port".into(),
80_i64.to_le_bytes().into(),
)));
let value = known_attribute_for(&"destination.port".into())
.unwrap()
.get();
assert_eq!(value, 80.into());
super::super::property::test::TEST_PROPERTY_VALUE.set(Some("GET".bytes().collect()));
property::test::TEST_PROPERTY_VALUE
.set(Some(("request.method".into(), "GET".bytes().collect())));
let value = known_attribute_for(&"request.method".into()).unwrap().get();
assert_eq!(value, "GET".into());
}
Expand Down
10 changes: 8 additions & 2 deletions src/data/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ fn wasm_prop(tokens: &[&str]) -> Path {
#[cfg(test)]
fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
debug!("get_property: {:?}", path);
Ok(test::TEST_PROPERTY_VALUE.take())
match test::TEST_PROPERTY_VALUE.take() {
None => Err(Status::NotFound),
Some((expected_path, data)) => {
assert_eq!(&expected_path, path);
Ok(Some(data))
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -146,7 +152,7 @@ pub mod test {
use std::cell::Cell;

thread_local!(
pub static TEST_PROPERTY_VALUE: Cell<Option<Vec<u8>>> = const { Cell::new(None) };
pub static TEST_PROPERTY_VALUE: Cell<Option<(Path, Vec<u8>)>> = const { Cell::new(None) };
);

#[test]
Expand Down

0 comments on commit eab58b9

Please sign in to comment.