MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / p2_random_limits

Function p2_random_limits

tests/all/cli_tests.rs:2301–2389  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2299
2300 #[test]
2301 fn p2_random_limits() -> Result<()> {
2302 println!("{P2_RANDOM_COMPONENT}");
2303
2304 // By default, p2_random.rs fits within the random limits - always
2305 // asks for 256 bytes.
2306 let output = run_wasmtime(&["run", P2_RANDOM_COMPONENT])?;
2307 assert_eq!(output, "");
2308
2309 // Lowering limit to 255 bytes should produce a trap in the first
2310 // call, which goes by way of the wasip1 adapter:
2311 let output = run_wasmtime(&["run", "-Smax-random-size=255", P2_RANDOM_COMPONENT])
2312 .err()
2313 .ok_or_else(|| format_err!("execution with max-random-size=255 should trap"))?;
2314 let output = format!("{output:?}");
2315 assert!(
2316 output.contains("lib_generated::random_get"),
2317 "expected error stack frames to contain 'wasip1::lib_generated::random_get'. Got:\n{output}"
2318 );
2319 assert!(
2320 output.contains("requested len 256 exceeds limit 255"),
2321 "expected error stack frames to contain 'requested len 256 exceeds limit 255'. Got:\n{output}"
2322 );
2323
2324 // Lowering limit to 255 bytes and setting environment variable for
2325 // the first call to only request 255 bytes should be OK, and then the
2326 // program produce a trap in the second call, which calls the wasip2
2327 // random import directly:
2328 let output = run_wasmtime(&[
2329 "run",
2330 "-Smax-random-size=255",
2331 "--env",
2332 "TEST_P1_RANDOM_LEN=255",
2333 P2_RANDOM_COMPONENT,
2334 ])
2335 .err()
2336 .ok_or_else(|| format_err!("execution with max-random-size=255 should trap"))?;
2337 let output = format!("{output:?}");
2338 assert!(
2339 output.contains("random::random::get_random_bytes"),
2340 "expected error stack frames to contain 'wasi::random::random::get_random_bytes'. Got:\n{output}"
2341 );
2342 assert!(
2343 output.contains("requested len 256 exceeds limit 255"),
2344 "expected error stack frames to contain 'requested len 256 exceeds limit 255'. Got:\n{output}"
2345 );
2346
2347 // Lowering limit to 255 bytes and setting environment variable for
2348 // the first and second calls to be at the limit should produce a trap
2349 // in the third call, which calls the wasip2 insecure random import:
2350 let output = run_wasmtime(&[
2351 "run",
2352 "-Smax-random-size=255",
2353 "--env",
2354 "TEST_P1_RANDOM_LEN=255",
2355 "--env",
2356 "TEST_P2_RANDOM_LEN=255",
2357 P2_RANDOM_COMPONENT,
2358 ])

Callers

nothing calls this directly

Calls 4

run_wasmtimeFunction · 0.85
OkFunction · 0.85
errMethod · 0.45
contextMethod · 0.45

Tested by

no test coverage detected