Skip to content

Commit

Permalink
test_unistd: Add test for getgrouplist/initgroups()
Browse files Browse the repository at this point in the history
  • Loading branch information
JayH5 committed Aug 27, 2017
1 parent 87c2d8d commit 3d112b3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ fn test_setgroups() {
setgroups(&old_groups).unwrap();
}

#[test]
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
fn test_initgroups() {
// Skip this test when not run as root as `initgroups()` and `setgroups()`
// require root.
if !Uid::current().is_root() {
return
}

// Save the existing groups
let old_groups = getgroups().unwrap();

// It doesn't matter if the root user is not called "root" or if a user
// called "root" doesn't exist. We are just checking that the extra,
// made-up group, `123`, is set.
// FIXME: This only tests half of initgroups' functionality.
let user = CString::new("root").unwrap();
let group = Gid::from_raw(123);
let group_list = getgrouplist(&user, group).unwrap();
assert!(group_list.contains(&group));

initgroups(&user, group).unwrap();

let new_groups = getgroups().unwrap();
assert_eq!(new_groups, group_list);

// Revert back to the old groups
setgroups(&old_groups).unwrap();
}

macro_rules! execve_test_factory(
($test_name:ident, $syscall:ident, $exe: expr) => (
#[test]
Expand Down

0 comments on commit 3d112b3

Please sign in to comment.