From bf4f2f2754ab4460da86f5a7a59d627d9420c42b Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Mon, 21 Oct 2024 17:23:56 -0400 Subject: [PATCH] Typos and test infra for hostcalls::get_property Signed-off-by: Alex Snaps --- src/data/cel.rs | 34 +++++++++++++++++++++++----------- src/data/mod.rs | 2 +- src/data/property.rs | 18 +++++++++++++++--- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/data/cel.rs b/src/data/cel.rs index 0813f49b..6523053d 100644 --- a/src/data/cel.rs +++ b/src/data/cel.rs @@ -23,7 +23,7 @@ impl Expression { let attributes = props .into_iter() .map(|tokens| { - know_attribute_for(&Path::new(tokens)) + known_attribute_for(&Path::new(tokens)) // resolve to known root, and then inspect proper location // path = ["auth", "identity", "anonymous", ...] // UnknownAttribute { known_root: Path, Path } @@ -131,9 +131,9 @@ impl Attribute { } } -pub fn know_attribute_for(path: &Path) -> Option { - static WELL_KNOWN_ATTTRIBUTES: OnceLock> = OnceLock::new(); - WELL_KNOWN_ATTTRIBUTES +pub fn known_attribute_for(path: &Path) -> Option { + static WELL_KNOWN_ATTRIBUTES: OnceLock> = OnceLock::new(); + WELL_KNOWN_ATTRIBUTES .get_or_init(new_well_known_attribute_map) .get(path) .map(|t| Attribute { @@ -343,16 +343,16 @@ pub mod data { #[cfg(test)] mod tests { use crate::data::cel::data::{AttributeMap, Token}; - use crate::data::know_attribute_for; + use crate::data::known_attribute_for; #[test] fn it_works() { let map = AttributeMap::new( [ - know_attribute_for(&"request.method".into()).unwrap(), - know_attribute_for(&"request.referer".into()).unwrap(), - know_attribute_for(&"source.address".into()).unwrap(), - know_attribute_for(&"destination.port".into()).unwrap(), + known_attribute_for(&"request.method".into()).unwrap(), + known_attribute_for(&"request.referer".into()).unwrap(), + known_attribute_for(&"source.address".into()).unwrap(), + known_attribute_for(&"destination.port".into()).unwrap(), ] .into(), ); @@ -408,13 +408,25 @@ pub mod data { #[cfg(test)] mod tests { - use crate::data::know_attribute_for; + use crate::data::known_attribute_for; use cel_interpreter::objects::ValueType; + #[test] + fn attribute_resolve() { + super::super::property::test::TEST_PROPERTY_VALUE.set(Some(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())); + let value = known_attribute_for(&"request.method".into()).unwrap().get(); + assert_eq!(value, "GET".into()); + } + #[test] fn finds_known_attributes() { let path = "request.method".into(); - let attr = know_attribute_for(&path).expect("Must be a hit!"); + let attr = known_attribute_for(&path).expect("Must be a hit!"); assert_eq!(attr.path, path); match attr.cel_type { ValueType::String => {} diff --git a/src/data/mod.rs b/src/data/mod.rs index 3fd26430..db471171 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -8,7 +8,7 @@ pub use attribute::store_metadata; pub use attribute::AttributeValue; #[allow(unused_imports)] -pub use cel::know_attribute_for; +pub use cel::known_attribute_for; #[allow(unused_imports)] pub use cel::Attribute; diff --git a/src/data/property.rs b/src/data/property.rs index f8692629..1199676c 100644 --- a/src/data/property.rs +++ b/src/data/property.rs @@ -1,6 +1,5 @@ use log::debug; use log::warn; -use proxy_wasm::hostcalls; use proxy_wasm::types::Status; use std::fmt::{Debug, Display, Formatter}; @@ -25,9 +24,16 @@ fn remote_address() -> Result>, Status> { } } +#[cfg(test)] fn host_get_property(path: &Path) -> Result>, Status> { debug!("get_property: {:?}", path); - hostcalls::get_property(path.tokens()) + Ok(test::TEST_PROPERTY_VALUE.take()) +} + +#[cfg(not(test))] +fn host_get_property(path: &Path) -> Result>, Status> { + debug!("get_property: {:?}", path); + proxy_wasm::hostcalls::get_property(path.tokens()) } pub fn get_property(path: &Path) -> Result>, Status> { @@ -100,8 +106,14 @@ impl Path { } #[cfg(test)] -mod test { +pub mod test { use crate::data::property::Path; + use std::cell::Cell; + + thread_local!( + pub static TEST_PROPERTY_VALUE: Cell>> = Cell::new(None); + ); + #[test] fn path_tokenizes_with_escaping_basic() { let path: Path = r"one\.two..three\\\\.four\\\.\five.".into();