1require_relative '../../spec_helper'
2
3describe "Process.groups" do
4  platform_is_not :windows do
5    it "gets an Array of the gids of groups in the supplemental group access list" do
6      groups = `id -G`.scan(/\d+/).map { |i| i.to_i }
7      gid = Process.gid
8
9      expected = (groups.sort - [gid]).uniq.sort
10      actual = (Process.groups - [gid]).uniq.sort
11      actual.should == expected
12    end
13  end
14end
15
16describe "Process.groups=" do
17  platform_is_not :windows do
18    as_superuser do
19      it "sets the list of gids of groups in the supplemental group access list" do
20        groups = Process.groups
21        Process.groups = []
22        Process.groups.should == []
23        Process.groups = groups
24        Process.groups.sort.should == groups.sort
25      end
26    end
27
28    as_user do
29      platform_is :aix do
30        it "sets the list of gids of groups in the supplemental group access list" do
31          # setgroups() is not part of the POSIX standard,
32          # so its behavior varies from OS to OS.  AIX allows a non-root
33          # process to set the supplementary group IDs, as long as
34          # they are presently in its supplementary group IDs.
35          # The order of the following tests matters.
36          # After this process executes "Process.groups = []"
37          # it should no longer be able to set any supplementary
38          # group IDs, even if it originally belonged to them.
39          # It should only be able to set its primary group ID.
40          groups = Process.groups
41          Process.groups = groups
42          Process.groups.sort.should == groups.sort
43          Process.groups = []
44          Process.groups.should == []
45          Process.groups = [ Process.gid ]
46          Process.groups.should == [ Process.gid ]
47          supplementary = groups - [ Process.gid ]
48          if supplementary.length > 0
49            lambda { Process.groups = supplementary }.should raise_error(Errno::EPERM)
50          end
51        end
52      end
53
54      platform_is_not :aix do
55        it "raises Errno::EPERM" do
56          lambda {
57            Process.groups = [0]
58          }.should raise_error(Errno::EPERM)
59        end
60      end
61    end
62  end
63end
64