regen headers
[p5sagit/p5-mst-13.2.git] / t / op / groups.t
CommitLineData
fe14fcc3 1#!./perl
2
61ae2fbf 3$ENV{PATH} ="/bin:/usr/bin:/usr/xpg4/bin:/usr/ucb" .
4 exists $ENV{PATH} ? ":$ENV{PATH}" : "";
5$ENV{LC_ALL} = "C"; # so that external utilities speak English
b9416812 6
d0f88fcc 7# We have to find a command that prints all (effective
8# and real) group names (not ids). The known commands are:
9# groups
10# id -Gn
11# id -a
12# Beware 1: some systems do just 'id -G' even when 'id -Gn' is used.
98cfb1fc 13# Beware 2: id -Gn or id -a format might be id(name) or name(id).
14# Beware 3: the groups= might be anywhere in the id output.
f62c0cf2 15# Beware 4: groups can have spaces ('id -a' being the only defense against this)
702a0e5a 16# Beware 5: id -a might not contain the groups= part.
98cfb1fc 17#
18# That is, we might meet the following:
19#
f62c0cf2 20# foo bar zot # accept
21# foo 22 42 bar zot # accept
22# 1 22 42 2 3 # reject
23# groups=(42),foo(1),bar(2),zot me(3) # parse
24# groups=22,42,1(foo),2(bar),3(zot me) # parse
98cfb1fc 25#
26# and the groups= might be after, before, or between uid=... and gid=...
d0f88fcc 27
28GROUPS: {
f62c0cf2 29 # prefer 'id' over 'groups' (is this ever wrong anywhere?)
30 # and 'id -a' over 'id -Gn' (the former is good about spaces in group names)
31 if (($groups = `id -a 2>/dev/null`) ne '') {
32 # $groups is of the form:
33 # uid=39957(gsar) gid=22(users) groups=33536,39181,22(users),0(root),1067(dev)
702a0e5a 34 last GROUPS if $groups =~ /groups=/;
f62c0cf2 35 }
36 if (($groups = `id -Gn 2>/dev/null`) ne '') {
37 # $groups could be of the form:
38 # users 33536 39181 root dev
39 last GROUPS if $groups !~ /^(\d|\s)+$/;
d0f88fcc 40 }
f62c0cf2 41 if (($groups = `groups 2>/dev/null`) ne '') {
42 # may not reflect all groups in some places, so do a sanity check
43 if (-d '/afs') {
44 print <<EOM;
45# These test results *may* be bogus, as you appear to have AFS,
46# and I can't find a working 'id' in your PATH (which I have set
47# to '$ENV{PATH}').
48#
49# If these tests fail, report the particular incantation you use
50# on this platform to find *all* the groups that an arbitrary
51# luser may belong to, using the 'perlbug' program.
52EOM
53 }
98cfb1fc 54 last GROUPS;
d0f88fcc 55 }
56 # Okay, not today.
fe14fcc3 57 print "1..0\n";
58 exit 0;
59}
60
98cfb1fc 61# Remember that group names can contain whitespace, '-', et cetera.
62# That is: do not \w, do not \S.
f62c0cf2 63if ($groups =~ /groups=(.+)( [ug]id=|$)/) {
98cfb1fc 64 my $gr = $1;
f62c0cf2 65 my @g0 = split /,/, $gr;
66 my @g1;
67 # prefer names over numbers
68 for (@g0) {
69 # 42(zot me)
70 if (/^(\d+)(?:\(([^)]+)\))?$/) {
71 push @g1, ($2 || $1);
72 }
73 # zot me(42)
74 elsif (/^([^(]*)\((\d+)\)$/) {
75 push @g1, ($1 || $2);
76 }
77 else {
78 print "# ignoring group entry [$_]\n";
79 }
80 }
81 print "# groups=$gr\n";
98cfb1fc 82 print "# g0 = @g0\n";
83 print "# g1 = @g1\n";
f62c0cf2 84 $groups = "@g1";
98cfb1fc 85}
86
988174c1 87print "1..2\n";
88
89$pwgid = $( + 0;
90($pwgnam) = getgrgid($pwgid);
91@basegroup{$pwgid,$pwgnam} = (1,1);
92
93$seen{$pwgid}++;
fe14fcc3 94
95for (split(' ', $()) {
96 next if $seen{$_}++;
6e21c824 97 ($group) = getgrgid($_);
98 if (defined $group) {
99 push(@gr, $group);
100 }
101 else {
102 push(@gr, $_);
103 }
fe14fcc3 104}
988174c1 105
b9416812 106if ($^O eq "uwin") { # Or anybody else who can have spaces in group names.
72720e3c 107 $gr1 = join(' ', grep(!$did{$_}++, sort split(' ', join(' ', @gr))));
108} else {
109 $gr1 = join(' ', sort @gr);
110}
988174c1 111
b9416812 112$gr2 = join(' ', grep(!$basegroup{$_}++, sort split(' ',$groups)));
988174c1 113
114if ($gr1 eq $gr2) {
115 print "ok 1\n";
116}
117else {
118 print "#gr1 is <$gr1>\n";
119 print "#gr2 is <$gr2>\n";
120 print "not ok 1\n";
121}
122
123# multiple 0's indicate GROUPSTYPE is currently long but should be short
124
125if ($pwgid == 0 || $seen{0} < 2) {
126 print "ok 2\n";
127}
128else {
129 print "not ok 2 (groupstype should be type short, not long)\n";
130}