Skip to content

Commit

Permalink
Fix bug in inspect implementation and add a test (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa authored Aug 29, 2024
1 parent c4d5f7c commit 9d88e38
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
25 changes: 25 additions & 0 deletions cli/tests/integration/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use {
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::{Request, StatusCode},
};

viceroy_test!(upstream_sync, |is_component| {
// Set up the test harness:
let test = Test::using_fixture("inspect.wasm").adapt_component(is_component);

// And send a request to exercise the hostcall:
let resp = test
.against(Request::post("/").body("Hello, Viceroy!").unwrap())
.await?;

assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.into_body().read_into_string().await?,
"inspect result: waf_response=200, tags=[], decision_ms=0ms, verdict=Allow"
);

Ok(())
});
1 change: 1 addition & 0 deletions cli/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod env_vars;
mod geolocation_lookup;
mod grpc;
mod http_semantics;
mod inspect;
mod kv_store;
mod logging;
mod memory;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/wiggle_abi/req_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ impl FastlyHttpReq for Session {

match u32::try_from(ngwaf_resp_len) {
Ok(ngwaf_resp_len) if ngwaf_resp_len <= buf_len => {
memory.copy_from_slice(ngwaf_resp.as_bytes(), buf.as_array(buf_len))?;
memory.copy_from_slice(ngwaf_resp.as_bytes(), buf.as_array(ngwaf_resp_len))?;

Ok(ngwaf_resp_len)
}
Expand Down
39 changes: 31 additions & 8 deletions test-fixtures/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test-fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ publish = false
[dependencies]
anyhow = "1.0.86"
base64 = "0.21.2"
fastly = "0.10.1"
fastly-shared = "0.10.1"
fastly-sys = "0.10.1"
fastly = "0.10.4"
fastly-shared = "0.10.4"
fastly-sys = "0.10.4"
hex-literal = "0.4.1"
bytes = "1.0.0"
http = "1.1.0"
Expand Down
37 changes: 37 additions & 0 deletions test-fixtures/src/bin/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use fastly::experimental::{inspect, InspectConfig, InspectError, InspectVerdict};
use fastly::handle::{BodyHandle, RequestHandle};
use fastly::http::{HeaderName, HeaderValue, Method, StatusCode};
use fastly::{Error, Request, Response};

#[fastly::main]
fn main(mut req: Request) -> Result<Response, Error> {
let (req, body) = req.into_handles();
let body = body.unwrap_or_else(BodyHandle::new);

let inspectconf = InspectConfig::new(&req, &body)
.corp("junichi-lab")
.workspace("lab");

let resp = match inspect(inspectconf) {
Ok(x) => {
let body = format!(
"inspect result: waf_response={}, tags={:?}, decision_ms={}ms, verdict={:?}",
x.waf_response(),
x.tags(),
x.decision_ms().as_millis(),
x.verdict()
);

Response::from_status(StatusCode::OK)
.with_body_text_plain(&body)
}
Err(e) => {
let body = format!("Error: {e:?}");

Response::from_status(StatusCode::BAD_REQUEST)
.with_body_text_plain(&body)
},
};

Ok(resp)
}

0 comments on commit 9d88e38

Please sign in to comment.