Skip to content

Commit

Permalink
test_unistd: Try add a test for initgroups on Apple
Browse files Browse the repository at this point in the history
  • Loading branch information
JayH5 committed Sep 30, 2017
1 parent 863addd commit 44bf23c
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,44 @@ fn test_initgroups() {
setgroups(&old_groups).unwrap();
}

/// `initgroups()` test specific to Apple platforms.
///
/// `getgroups()` and `setgroups()` do not behave as expected on Apple
/// platforms and the use of `setgroups()` is highly discouraged. The only
/// thing we have to work on is this note in the `getgroups(2)` manpage:
/// _"Calling initgroups(3) to opt-in for supplementary groups will cause
/// getgroups() to return a single entry, the GID that was passed to
/// initgroups(3)"_
/// This tests that behaviour.
#[test]
#[cfg(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() {
let stderr = std::io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "test_initgroups requires root privileges. Skipping test.").unwrap();
return;
}

#[allow(unused_variables)]
let m = ::GROUPS_MTX.lock().expect("Mutex got poisoned by another test");

// 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: Test the other half of initgroups' functionality: whether the
// groups that the user belongs to are also set.
let user = CString::new("root").unwrap();
let group = Gid::from_raw(123);

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

let new_groups = getgroups().unwrap();
assert!(new_groups.contains(&group));
}

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

0 comments on commit 44bf23c

Please sign in to comment.