Skip to content

Commit 688667e

Browse files
committed
test_unistd: Add test for getgrouplist/initgroups()
1 parent 4a2d65e commit 688667e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

test/test_unistd.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,37 @@ fn test_setgroups() {
145145
setgroups(&old_groups).unwrap();
146146
}
147147

148+
#[test]
149+
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
150+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
151+
fn test_initgroups() {
152+
// Skip this test when not run as root as `initgroups()` and `setgroups()`
153+
// require root.
154+
if !Uid::current().is_root() {
155+
return
156+
}
157+
158+
// Save the existing groups
159+
let old_groups = getgroups().unwrap();
160+
161+
// It doesn't matter if the root user is not called "root" or if a user
162+
// called "root" doesn't exist. We are just checking that the extra,
163+
// made-up group, `123`, is set.
164+
// FIXME: This only tests half of initgroups' functionality.
165+
let user = CString::new("root").unwrap();
166+
let group = Gid::from_raw(123);
167+
let group_list = getgrouplist(&user, group).unwrap();
168+
assert!(group_list.contains(&group));
169+
170+
initgroups(&user, group).unwrap();
171+
172+
let new_groups = getgroups().unwrap();
173+
assert_eq!(new_groups, group_list);
174+
175+
// Revert back to the old groups
176+
setgroups(&old_groups).unwrap();
177+
}
178+
148179
macro_rules! execve_test_factory(
149180
($test_name:ident, $syscall:ident, $exe: expr) => (
150181
#[test]

0 commit comments

Comments
 (0)