Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

6.4 KB
Raw Blame
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::p2::bindings::Command;

foreach_p2_http!(assert_test_exists);

async fn run(path: &str, server: &Server) -> Result<()> {
    let engine = test_programs_artifacts::engine(|config| {
        config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
    });
    let component = Component::from_file(&engine, path)?;
    let mut store = store(&engine, server);
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
    wasmtime_wasi_http::p2::add_only_http_to_linker_async(&mut linker)?;
    let command = Command::instantiate_async(&mut store, &component, &linker).await?;
    let result = command.wasi_cli_run().call_run(&mut store).await?;
    result.map_err(|()| wasmtime::format_err!("run returned an error"))
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_get() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_GET_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_between_bytes_timeout() -> Result<()> {
    let server = Server::http1_stalled_body(1, std::time::Duration::from_secs(5))?;
    run(
        P2_HTTP_OUTBOUND_REQUEST_BETWEEN_BYTES_TIMEOUT_COMPONENT,
        &server,
    )
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_first_byte_timeout() -> Result<()> {
    let server = Server::http1_delayed(1, std::time::Duration::from_secs(5))?;
    run(
        P2_HTTP_OUTBOUND_REQUEST_FIRST_BYTE_TIMEOUT_COMPONENT,
        &server,
    )
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_timeout() -> Result<()> {
    let server = Server::http1(3)?;
    run(P2_HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_post() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_POST_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_large_post() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_LARGE_POST_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_long_path() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_LONG_PATH_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_put() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_PUT_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_invalid_version() -> Result<()> {
    let server = Server::http2(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_INVALID_VERSION_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_invalid_header() -> Result<()> {
    let server = Server::http2(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_INVALID_HEADER_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_unknown_method() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_UNKNOWN_METHOD_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_unsupported_scheme() -> Result<()> {
    let server = Server::http1(1)?;
    run(
        P2_HTTP_OUTBOUND_REQUEST_UNSUPPORTED_SCHEME_COMPONENT,
        &server,
    )
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_invalid_port() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_INVALID_PORT_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_invalid_dnsname() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_INVALID_DNSNAME_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_response_build() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_RESPONSE_BUILD_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_content_length() -> Result<()> {
    let server = Server::http1(1)?;
    run(P2_HTTP_OUTBOUND_REQUEST_CONTENT_LENGTH_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_forward_duplicate_content_length() -> Result<()> {
    crate::shared::forward_duplicate_content_length(async |server| {
        run(
            P2_HTTP_OUTBOUND_REQUEST_FORWARD_DUPLICATE_CONTENT_LENGTH_COMPONENT,
            server,
        )
        .await
    })
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_request_missing_path_and_query() -> Result<()> {
    let server = Server::http1(1)?;
    run(
        P2_HTTP_OUTBOUND_REQUEST_MISSING_PATH_AND_QUERY_COMPONENT,
        &server,
    )
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_named_imports() -> Result<()> {
    crate::shared::run_named_imports_test(
        P2_HTTP_NAMED_IMPORTS_COMPONENT,
        wasmtime_wasi_http::p2::add_only_http_to_linker_async,
        wasmtime_wasi_http::p2::add_named_to_linker_async,
        async |store, component, linker| {
            let command = Command::instantiate_async(&mut *store, component, linker).await?;
            let result = command.wasi_cli_run().call_run(&mut *store).await?;
            result.map_err(|()| wasmtime::format_err!("run returned an error"))
        },
    )
    .await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_http_outbound_body_write_backpressure() -> Result<()> {
    let server = Server::http1(0)?;
    let e = run(P2_HTTP_OUTBOUND_BODY_WRITE_BACKPRESSURE_COMPONENT, &server)
        .await
        .err()
        .expect("guest execution should trap");
    let e_debug = format!("{e:?}");
    assert!(
        e_debug.contains("write exceeded budget"),
        "expected trap to contain 'write exceeded budget': {e:?}"
    );
    Ok(())
}