Skip to content

Commit 18a597c

Browse files
Add libc direct tests for (get/set/unset)env functions
1 parent 15299b7 commit 18a597c

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//@ignore-target: windows # No libc
2+
//@compile-flags: -Zmiri-disable-isolation
3+
4+
use std::ffi::CStr;
5+
6+
fn test_getenv() {
7+
let s = unsafe { libc::getenv(b"MIRI_ENV_VAR_TEST\0".as_ptr().cast()) };
8+
assert!(!s.is_null());
9+
let value = unsafe { CStr::from_ptr(s).to_str().unwrap() };
10+
assert_eq!(value, "0");
11+
12+
// Get a non-existing environment variable
13+
let s = unsafe { libc::getenv(b"MIRI_TEST_NONEXISTENT_VAR\0".as_ptr().cast()) };
14+
assert!(s.is_null());
15+
16+
// Empty string should not crash
17+
let s = unsafe { libc::getenv(b"\0".as_ptr().cast()) };
18+
assert!(s.is_null());
19+
}
20+
21+
fn test_setenv() {
22+
// Set a new environment variable
23+
let result = unsafe {
24+
libc::setenv(b"MIRI_TEST_VAR\0".as_ptr().cast(), b"test_value\0".as_ptr().cast(), 1)
25+
};
26+
assert_eq!(result, 0);
27+
28+
// Verify it was set
29+
let s = unsafe { libc::getenv(b"MIRI_TEST_VAR\0".as_ptr().cast()) };
30+
assert!(!s.is_null());
31+
let value = unsafe { CStr::from_ptr(s).to_str().unwrap() };
32+
assert_eq!(value, "test_value");
33+
34+
// Test overwriting an existing variable
35+
let result = unsafe {
36+
libc::setenv(b"MIRI_TEST_VAR\0".as_ptr().cast(), b"new_value\0".as_ptr().cast(), 1)
37+
};
38+
assert_eq!(result, 0);
39+
40+
// Verify it was updated
41+
let s = unsafe { libc::getenv(b"MIRI_TEST_VAR\0".as_ptr().cast()) };
42+
assert!(!s.is_null());
43+
let value = unsafe { CStr::from_ptr(s).to_str().unwrap() };
44+
assert_eq!(value, "new_value");
45+
46+
// Test invalid parameters
47+
let result = unsafe { libc::setenv(std::ptr::null(), b"value\0".as_ptr().cast(), 1) };
48+
assert_eq!(result, -1);
49+
50+
let result = unsafe { libc::setenv(b"\0".as_ptr().cast(), b"value\0".as_ptr().cast(), 1) };
51+
assert_eq!(result, -1);
52+
53+
let result =
54+
unsafe { libc::setenv(b"INVALID=NAME\0".as_ptr().cast(), b"value\0".as_ptr().cast(), 1) };
55+
assert_eq!(result, -1);
56+
}
57+
58+
fn test_unsetenv() {
59+
// Set a variable
60+
let result = unsafe {
61+
libc::setenv(b"MIRI_TEST_UNSET_VAR\0".as_ptr().cast(), b"to_be_unset\0".as_ptr().cast(), 1)
62+
};
63+
assert_eq!(result, 0);
64+
65+
// Verify it exists
66+
let s = unsafe { libc::getenv(b"MIRI_TEST_UNSET_VAR\0".as_ptr().cast()) };
67+
assert!(!s.is_null());
68+
69+
// Unset it
70+
let result = unsafe { libc::unsetenv(b"MIRI_TEST_UNSET_VAR\0".as_ptr().cast()) };
71+
assert_eq!(result, 0);
72+
73+
// Verify it was unset
74+
let s = unsafe { libc::getenv(b"MIRI_TEST_UNSET_VAR\0".as_ptr().cast()) };
75+
assert!(s.is_null());
76+
77+
// Test unsetting a non-existing variable (should succeed)
78+
let result = unsafe { libc::unsetenv(b"MIRI_TEST_NONEXISTENT_VAR\0".as_ptr().cast()) };
79+
assert_eq!(result, 0);
80+
81+
// Test invalid parameters
82+
let result = unsafe { libc::unsetenv(std::ptr::null()) };
83+
assert_eq!(result, -1);
84+
85+
let result = unsafe { libc::unsetenv(b"\0".as_ptr().cast()) };
86+
assert_eq!(result, -1);
87+
88+
let result = unsafe { libc::unsetenv(b"INVALID=NAME\0".as_ptr().cast()) };
89+
assert_eq!(result, -1);
90+
}
91+
92+
fn main() {
93+
test_getenv();
94+
test_setenv();
95+
test_unsetenv();
96+
}

0 commit comments

Comments
 (0)