Skip to content

Commit 89b7055

Browse files
committed
test_unistd: Add test for getgroups/setgroups()
1 parent 5ff0c35 commit 89b7055

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
@@ -122,6 +122,29 @@ mod linux_android {
122122
}
123123
}
124124

125+
#[test]
126+
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
127+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
128+
fn test_setgroups() {
129+
// Skip this test when not run as root as `setgroups()` requires root.
130+
if !Uid::current().is_root() {
131+
return
132+
}
133+
134+
// Save the existing groups
135+
let old_groups = getgroups().unwrap();
136+
137+
// Set some new made up groups
138+
let groups = [Gid::from_raw(123), Gid::from_raw(456)];
139+
setgroups(&groups).unwrap();
140+
141+
let new_groups = getgroups().unwrap();
142+
assert_eq!(new_groups, groups);
143+
144+
// Revert back to the old groups
145+
setgroups(&old_groups).unwrap();
146+
}
147+
125148
macro_rules! execve_test_factory(
126149
($test_name:ident, $syscall:ident, $exe: expr) => (
127150
#[test]

0 commit comments

Comments
 (0)