use browser_oxide::Page;
fn html(body: &str) -> String {
format!("<!DOCTYPE html><html><head></head><body>{body}</body></html>")
}
const MINIMAL_WASM_HEX: &str = "0061736D01000000";
#[tokio::test]
async fn webassembly_object_exists() {
let mut page = Page::from_html(&html(""), None::<browser_oxide::stealth::StealthProfile>)
.await
.unwrap();
let result = page
.evaluate(
r#"
const o = {};
o.WebAssembly = typeof WebAssembly;
o.Module = typeof WebAssembly.Module;
o.Instance = typeof WebAssembly.Instance;
o.compile = typeof WebAssembly.compile;
o.instantiate = typeof WebAssembly.instantiate;
o.compileStreaming = typeof WebAssembly.compileStreaming;
o.instantiateStreaming = typeof WebAssembly.instantiateStreaming;
JSON.stringify(o);
"#,
)
.unwrap();
assert!(
result.contains("\"WebAssembly\":\"object\""),
"got {result}"
);
assert!(result.contains("\"Module\":\"function\""), "got {result}");
assert!(result.contains("\"Instance\":\"function\""), "got {result}");
assert!(result.contains("\"compile\":\"function\""), "got {result}");
assert!(
result.contains("\"instantiate\":\"function\""),
"got {result}"
);
assert!(
result.contains("\"compileStreaming\":\"function\""),
"got {result}"
);
assert!(
result.contains("\"instantiateStreaming\":\"function\""),
"got {result}"
);
}
#[tokio::test]
async fn webassembly_compile_minimal_module() {
let mut page = Page::from_html(&html(""), None::<browser_oxide::stealth::StealthProfile>)
.await
.unwrap();
let result = page
.evaluate(&format!(
r#"
(async function() {{
try {{
const hex = "{MINIMAL_WASM_HEX}";
const bytes = new Uint8Array(hex.match(/.{{1,2}}/g).map(b => parseInt(b, 16)));
const mod = await WebAssembly.compile(bytes);
const inst = await WebAssembly.instantiate(mod);
return JSON.stringify({{ ok: true, type: typeof inst, hasExports: typeof inst.exports }});
}} catch (e) {{
return JSON.stringify({{ ok: false, err: e.message }});
}}
}})()
"#
))
.unwrap();
assert!(
result.contains("\"ok\":true") || result.contains("Promise"),
"got {result}"
);
}
#[tokio::test]
async fn webassembly_instantiate_streaming_fallback() {
let mut page = Page::from_html(&html(""), None::<browser_oxide::stealth::StealthProfile>)
.await
.unwrap();
let result = page
.evaluate(&format!(
r#"
(async function() {{
try {{
const hex = "{MINIMAL_WASM_HEX}";
const bytes = new Uint8Array(hex.match(/.{{1,2}}/g).map(b => parseInt(b, 16)));
const resp = new Response(bytes, {{
headers: {{ 'Content-Type': 'application/wasm' }},
}});
const inst = await WebAssembly.instantiateStreaming(resp);
return JSON.stringify({{ ok: true, hasInstance: typeof inst.instance }});
}} catch (e) {{
return JSON.stringify({{ ok: false, err: e.message }});
}}
}})()
"#
))
.unwrap();
assert!(
result.contains("\"ok\":true") || result.contains("Promise"),
"got {result}"
);
}