| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use uutests::new_ucmd; |
| 7 |
|
| 8 |
#[test] |
| 9 |
fn test_get_all() { |
| 10 |
new_ucmd!() |
| 11 |
.env("HOME", "FOO") |
| 12 |
.env("KEY", "VALUE") |
| 13 |
.succeeds() |
| 14 |
.stdout_contains("HOME=FOO") |
| 15 |
.stdout_contains("KEY=VALUE"); |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_get_var() { |
| 20 |
new_ucmd!() |
| 21 |
.env("KEY", "VALUE") |
| 22 |
.env("FOO", "BAR") |
| 23 |
.arg("KEY") |
| 24 |
.succeeds() |
| 25 |
.stdout_is("VALUE\n"); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_ignore_equal_var() { |
| 30 |
// Basic printenv functionality |
| 31 |
new_ucmd!().env("a=b", "c").arg("a=b").fails().no_stdout(); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn test_silent_error_equal_var() { |
| 36 |
// printenv should ignore variables with equal signs e.g. a=b=c |
| 37 |
new_ucmd!() |
| 38 |
.env("KEY", "VALUE") |
| 39 |
.env("a=b", "c") |
| 40 |
.arg("KEY") |
| 41 |
.arg("a=b") |
| 42 |
.fails_with_code(1) |
| 43 |
.stdout_is("VALUE\n") |
| 44 |
.no_stderr(); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn test_silent_error_not_present() { |
| 49 |
// printenv should ignore unspecified variables, not panic on them |
| 50 |
new_ucmd!() |
| 51 |
.env("KEY", "VALUE") |
| 52 |
.arg("FOO") |
| 53 |
.arg("KEY") |
| 54 |
.fails_with_code(1) |
| 55 |
.stdout_is("VALUE\n") |
| 56 |
.no_stderr(); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn test_invalid_option_exit_code() { |
| 61 |
// printenv should return exit code 2 for invalid options |
| 62 |
// This matches GNU printenv behavior and the GNU tests expectation |
| 63 |
new_ucmd!() |
| 64 |
.arg("-/") |
| 65 |
.fails() |
| 66 |
.code_is(2) |
| 67 |
.stderr_contains("unexpected argument") |
| 68 |
.stderr_contains("For more information, try '--help'"); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_null_separator() { |
| 73 |
// printenv should use \x00 as separator if null option is provided |
| 74 |
for null_opt in ["-0", "--null"] { |
| 75 |
new_ucmd!() |
| 76 |
.env("HOME", "FOO") |
| 77 |
.env("KEY", "VALUE") |
| 78 |
.arg(null_opt) |
| 79 |
.succeeds() |
| 80 |
.stdout_contains("HOME=FOO\x00") |
| 81 |
.stdout_contains("KEY=VALUE\x00"); |
| 82 |
|
| 83 |
new_ucmd!() |
| 84 |
.env("HOME", "FOO") |
| 85 |
.env("KEY", "VALUE") |
| 86 |
.env("FOO", "BAR") |
| 87 |
.arg(null_opt) |
| 88 |
.arg("HOME") |
| 89 |
.arg("KEY") |
| 90 |
.succeeds() |
| 91 |
.stdout_is("FOO\x00VALUE\x00"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
#[cfg(unix)] |
| 97 |
#[cfg(not(any(target_os = "freebsd", target_os = "android", target_os = "openbsd")))] |
| 98 |
fn test_non_utf8_value() { |
| 99 |
use std::ffi::OsStr; |
| 100 |
use std::os::unix::ffi::OsStrExt; |
| 101 |
// Environment variable values can contain non-UTF-8 bytes on Unix. |
| 102 |
// printenv should output them correctly, matching GNU behavior. |
| 103 |
// Reproduces: LD_PRELOAD=$'/tmp/lib.so\xff' printenv LD_PRELOAD |
| 104 |
let value_with_invalid_utf8 = OsStr::from_bytes(b"/tmp/lib.so\xff"); |
| 105 |
|
| 106 |
let result = new_ucmd!() |
| 107 |
.env("LD_PRELOAD", value_with_invalid_utf8) |
| 108 |
.arg("LD_PRELOAD") |
| 109 |
.run(); |
| 110 |
|
| 111 |
// Use byte-based assertions to avoid UTF-8 conversion issues |
| 112 |
// when the test framework tries to format error messages |
| 113 |
assert!( |
| 114 |
result.succeeded(), |
| 115 |
"Command failed with exit code: {:?}, stderr: {:?}", |
| 116 |
result.code(), |
| 117 |
String::from_utf8_lossy(result.stderr()) |
| 118 |
); |
| 119 |
result.stdout_is_bytes(b"/tmp/lib.so\xff\n"); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
#[cfg(unix)] |
| 124 |
fn test_non_utf8_env_vars() { |
| 125 |
use std::ffi::OsString; |
| 126 |
use std::os::unix::ffi::OsStringExt; |
| 127 |
|
| 128 |
let non_utf8_value = OsString::from_vec(b"hello\x80world".to_vec()); |
| 129 |
new_ucmd!() |
| 130 |
.env("NON_UTF8_VAR", &non_utf8_value) |
| 131 |
.succeeds() |
| 132 |
.stdout_contains_bytes(b"NON_UTF8_VAR=hello\x80world"); |
| 133 |
} |