Skip to content

Commit d20d31c

Browse files
committed
test(printenv): Add test for non-UTF8 content in variable
1 parent 5d951d8 commit d20d31c

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

tests/by-util/test_printenv.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,30 @@ fn test_null_separator() {
9090
.stdout_is("FOO\x00VALUE\x00");
9191
}
9292
}
93+
94+
#[test]
95+
#[cfg(unix)]
96+
#[cfg(not(any(target_os = "freebsd", target_os = "android", target_os = "openbsd")))]
97+
fn test_non_utf8_value() {
98+
use std::ffi::OsStr;
99+
use std::os::unix::ffi::OsStrExt;
100+
// Environment variable values can contain non-UTF-8 bytes on Unix.
101+
// printenv should output them correctly, matching GNU behavior.
102+
// Reproduces: LD_PRELOAD=$'/tmp/lib.so\xff' printenv LD_PRELOAD
103+
let value_with_invalid_utf8 = OsStr::from_bytes(b"/tmp/lib.so\xff");
104+
105+
let result = new_ucmd!()
106+
.env("LD_PRELOAD", value_with_invalid_utf8)
107+
.arg("LD_PRELOAD")
108+
.run();
109+
110+
// Use byte-based assertions to avoid UTF-8 conversion issues
111+
// when the test framework tries to format error messages
112+
assert!(
113+
result.succeeded(),
114+
"Command failed with exit code: {:?}, stderr: {:?}",
115+
result.code(),
116+
String::from_utf8_lossy(result.stderr())
117+
);
118+
result.stdout_is_bytes(b"/tmp/lib.so\xff\n");
119+
}

0 commit comments

Comments
 (0)