@@ -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+
148179macro_rules! execve_test_factory(
149180 ( $test_name: ident, $syscall: ident, $exe: expr) => (
150181 #[ test]
0 commit comments