Skip to content

Commit 8a8bb57

Browse files
author
Roy Lin
committed
test(runtime): decode base64 BOX_EXEC_* values in spec assertions
The exec values are now base64-encoded (BOX_EXEC_B64), so the spec tests decode them (via a b64d helper) before comparing to the raw expected value — verifying the encode round-trips correctly. Also rustfmt the guest decode block.
1 parent b9806f6 commit 8a8bb57

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/guest/init/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ impl ExecConfig {
252252
// libkrun's env serialization. Decode them back; fall back to the raw value
253253
// on any decode error or when the marker is absent (older runtime).
254254
use base64::Engine;
255-
let b64 = std::env::var("BOX_EXEC_B64").map(|v| v == "1").unwrap_or(false);
255+
let b64 = std::env::var("BOX_EXEC_B64")
256+
.map(|v| v == "1")
257+
.unwrap_or(false);
256258
let decode = |s: String| -> String {
257259
if !b64 {
258260
return s;
@@ -274,7 +276,11 @@ impl ExecConfig {
274276
.and_then(|s| s.parse::<usize>().ok())
275277
{
276278
Some(argc) => (0..argc)
277-
.filter_map(|i| std::env::var(format!("BOX_EXEC_ARG_{}", i)).ok().map(&decode))
279+
.filter_map(|i| {
280+
std::env::var(format!("BOX_EXEC_ARG_{}", i))
281+
.ok()
282+
.map(&decode)
283+
})
278284
.collect(),
279285
None => vec![],
280286
};

src/runtime/src/vm/spec.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,17 @@ mod tests {
756756
use tempfile::tempdir;
757757
use tempfile::TempDir;
758758

759+
/// Decode a base64 (URL-safe, no pad) `BOX_EXEC_*` value the way guest-init
760+
/// does, so assertions can compare against the original raw value.
761+
fn b64d(s: &str) -> String {
762+
use base64::Engine;
763+
base64::engine::general_purpose::URL_SAFE_NO_PAD
764+
.decode(s.as_bytes())
765+
.ok()
766+
.and_then(|bytes| String::from_utf8(bytes).ok())
767+
.unwrap_or_else(|| s.to_string())
768+
}
769+
759770
fn test_oci_config(workdir: Option<&str>, user: Option<&str>) -> OciImageConfig {
760771
OciImageConfig {
761772
entrypoint: Some(vec!["/bin/app".to_string()]),
@@ -1074,12 +1085,12 @@ mod tests {
10741085
.entrypoint
10751086
.env
10761087
.iter()
1077-
.any(|(key, value)| key == "BOX_EXEC_USER" && value == "1000:1000"));
1088+
.any(|(key, value)| key == "BOX_EXEC_USER" && b64d(value) == "1000:1000"));
10781089
assert!(spec
10791090
.entrypoint
10801091
.env
10811092
.iter()
1082-
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && value == "/override"));
1093+
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && b64d(value) == "/override"));
10831094
}
10841095

10851096
#[test]
@@ -1126,12 +1137,12 @@ mod tests {
11261137
.entrypoint
11271138
.env
11281139
.iter()
1129-
.any(|(key, value)| key == "BOX_EXEC_USER" && value == "2000:2000"));
1140+
.any(|(key, value)| key == "BOX_EXEC_USER" && b64d(value) == "2000:2000"));
11301141
assert!(spec
11311142
.entrypoint
11321143
.env
11331144
.iter()
1134-
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && value == "/oci"));
1145+
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && b64d(value) == "/oci"));
11351146
}
11361147

11371148
#[test]
@@ -1147,7 +1158,7 @@ mod tests {
11471158
.entrypoint
11481159
.env
11491160
.iter()
1150-
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && value == GUEST_WORKDIR));
1161+
.any(|(key, value)| key == "BOX_EXEC_WORKDIR" && b64d(value) == GUEST_WORKDIR));
11511162
}
11521163

11531164
#[test]
@@ -1191,17 +1202,17 @@ mod tests {
11911202
.entrypoint
11921203
.env
11931204
.iter()
1194-
.any(|(key, value)| key == "BOX_EXEC_ENV_FOO" && value == "cli"));
1205+
.any(|(key, value)| key == "BOX_EXEC_ENV_FOO" && b64d(value) == "cli"));
11951206
assert!(spec
11961207
.entrypoint
11971208
.env
11981209
.iter()
1199-
.any(|(key, value)| key == "BOX_EXEC_ENV_BAR" && value == "image"));
1210+
.any(|(key, value)| key == "BOX_EXEC_ENV_BAR" && b64d(value) == "image"));
12001211
assert!(spec
12011212
.entrypoint
12021213
.env
12031214
.iter()
1204-
.any(|(key, value)| key == "BOX_EXEC_ENV_BAZ" && value == "cli"));
1215+
.any(|(key, value)| key == "BOX_EXEC_ENV_BAZ" && b64d(value) == "cli"));
12051216
assert!(!spec
12061217
.entrypoint
12071218
.env

0 commit comments

Comments
 (0)