From 969971c44e40c69aeda8214e92cf6bdd98ef94f1 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 8 Sep 2026 08:20:58 +0000 Subject: [PATCH] test: Cover and secure uuid generation Replaced an uncovered `.unwrap()` call in `uuid_v4` with `.unwrap_or_default()` to prevent a potential runtime panic if `duration_since` fails (e.g., system time before UNIX epoch). Added a unit test to verify that `uuid_v4` generates properly formatted strings. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/uuid.rs | 2 +- stdlib/tests/stdlib_tests.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/stdlib/src/uuid.rs b/stdlib/src/uuid.rs index 2a3ed70d..9a2af5c3 100644 --- a/stdlib/src/uuid.rs +++ b/stdlib/src/uuid.rs @@ -17,7 +17,7 @@ impl StdlibRegistry { use std::time::SystemTime; let nano = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() + .unwrap_or_default() .as_nanos(); Ok(RuntimeValue::Str(format!( "123e4567-e89b-12d3-a456-{}", diff --git a/stdlib/tests/stdlib_tests.rs b/stdlib/tests/stdlib_tests.rs index f7df969f..3547b99b 100644 --- a/stdlib/tests/stdlib_tests.rs +++ b/stdlib/tests/stdlib_tests.rs @@ -1151,3 +1151,36 @@ fn test_ai_generate_text() { let val = res.unwrap(); assert!(val.as_string().unwrap().contains("Prompt: What is 2+2?")); } + +#[test] +fn test_uuid_module() { + let registry = StdlibRegistry::new(); + let uuid_mod = registry.get_module("std.uuid").unwrap(); + + let config_unprivileged = RuntimeConfig::default(); + let mut ctx_unprivileged = RuntimeContext::new(config_unprivileged); + + let uuid_v4_fn = uuid_mod.exports.get("uuid_v4").unwrap(); + + // 1. Call uuid_v4 + let res1 = uuid_v4_fn.call(&mut ctx_unprivileged, vec![]); + assert!(res1.is_ok()); + let val1 = res1.unwrap(); + let str1 = val1.as_string().unwrap(); + + // Verify format + assert!(str1.starts_with("123e4567-e89b-12d3-a456-")); + assert!(str1.len() > 24); + + // 2. Call again to verify it produces slightly different values (since timestamp changes) + // Wait briefly if needed, but nanos should be distinct even in quick succession usually + let res2 = uuid_v4_fn.call(&mut ctx_unprivileged, vec![]); + assert!(res2.is_ok()); + let val2 = res2.unwrap(); + let str2 = val2.as_string().unwrap(); + + // They shouldn't be identical unless the system clock doesn't have nano precision + // Note: On Windows precision might be ~100ns, so they could sometimes match if very fast. + // At minimum, we just check they are well-formed strings. + assert!(str2.starts_with("123e4567-e89b-12d3-a456-")); +}