Skip to content

Commit 3d112b3

Browse files
committed
test_unistd: Add test for getgrouplist/initgroups()
1 parent 87c2d8d commit 3d112b3

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
@@ -127,6 +127,37 @@ fn test_setgroups() {
127127
setgroups(&old_groups).unwrap();
128128
}
129129

130+
#[test]
131+
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
132+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
133+
fn test_initgroups() {
134+
// Skip this test when not run as root as `initgroups()` and `setgroups()`
135+
// require root.
136+
if !Uid::current().is_root() {
137+
return
138+
}
139+
140+
// Save the existing groups
141+
let old_groups = getgroups().unwrap();
142+
143+
// It doesn't matter if the root user is not called "root" or if a user
144+
// called "root" doesn't exist. We are just checking that the extra,
145+
// made-up group, `123`, is set.
146+
// FIXME: This only tests half of initgroups' functionality.
147+
let user = CString::new("root").unwrap();
148+
let group = Gid::from_raw(123);
149+
let group_list = getgrouplist(&user, group).unwrap();
150+
assert!(group_list.contains(&group));
151+
152+
initgroups(&user, group).unwrap();
153+
154+
let new_groups = getgroups().unwrap();
155+
assert_eq!(new_groups, group_list);
156+
157+
// Revert back to the old groups
158+
setgroups(&old_groups).unwrap();
159+
}
160+
130161
macro_rules! execve_test_factory(
131162
($test_name:ident, $syscall:ident, $exe: expr) => (
132163
#[test]

0 commit comments

Comments
 (0)