Skip to content

Commit

Permalink
Typos and test infra for hostcalls::get_property
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Oct 21, 2024
1 parent 45ece5f commit bf4f2f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
34 changes: 23 additions & 11 deletions src/data/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -131,9 +131,9 @@ impl Attribute {
}
}

pub fn know_attribute_for(path: &Path) -> Option<Attribute> {
static WELL_KNOWN_ATTTRIBUTES: OnceLock<HashMap<Path, ValueType>> = OnceLock::new();
WELL_KNOWN_ATTTRIBUTES
pub fn known_attribute_for(path: &Path) -> Option<Attribute> {
static WELL_KNOWN_ATTRIBUTES: OnceLock<HashMap<Path, ValueType>> = OnceLock::new();
WELL_KNOWN_ATTRIBUTES
.get_or_init(new_well_known_attribute_map)
.get(path)
.map(|t| Attribute {
Expand Down Expand Up @@ -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(),
);
Expand Down Expand Up @@ -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 => {}
Expand Down
2 changes: 1 addition & 1 deletion src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 15 additions & 3 deletions src/data/property.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -25,9 +24,16 @@ fn remote_address() -> Result<Option<Vec<u8>>, Status> {
}
}

#[cfg(test)]
fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, 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<Option<Vec<u8>>, Status> {
debug!("get_property: {:?}", path);
proxy_wasm::hostcalls::get_property(path.tokens())
}

pub fn get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
Expand Down Expand Up @@ -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<Option<Vec<u8>>> = Cell::new(None);
);

#[test]
fn path_tokenizes_with_escaping_basic() {
let path: Path = r"one\.two..three\\\\.four\\\.\five.".into();
Expand Down

0 comments on commit bf4f2f2

Please sign in to comment.