Skip to content

Commit d82fa57

Browse files
committed
test_unistd: Add test for getgroups/setgroups()
1 parent 0fba0ae commit d82fa57

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/test_unistd.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ mod linux_android {
104104
}
105105
}
106106

107+
#[test]
108+
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
109+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
110+
fn test_setgroups() {
111+
// Skip this test when not run as root as `setgroups()` requires root.
112+
if !Uid::current().is_root() {
113+
return
114+
}
115+
116+
// Save the existing groups
117+
let old_groups = getgroups().unwrap();
118+
119+
// Set some new made up groups
120+
let groups = [Gid::from_raw(123), Gid::from_raw(456)];
121+
setgroups(&groups).unwrap();
122+
123+
let new_groups = getgroups().unwrap();
124+
assert_eq!(new_groups, groups);
125+
126+
// Revert back to the old groups
127+
setgroups(&old_groups).unwrap();
128+
}
129+
107130
macro_rules! execve_test_factory(
108131
($test_name:ident, $syscall:ident, $exe: expr) => (
109132
#[test]

0 commit comments

Comments
 (0)