Small optimisations, by Brandon Black
[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
b598356e 6$ENV{LANGUAGE} = 'C'; # GNU locale extension
b9416812 7
9380b46b 8BEGIN {
e0889c13 9 chdir 't';
10 @INC = '../lib';
11
9380b46b 12 require Config;
13 if ($@) {
14 print "1..0 # Skip: no Config\n";
15 } else {
16 Config->import;
17 }
18}
19
13d7cbc1 20sub quit {
45c0de28 21 print "1..0 # Skip: no `id` or `groups`\n";
13d7cbc1 22 exit 0;
23}
24
8e3eacad 25unless (eval { getgrgid(0); 1 }) {
26 print "1..0 # Skip: getgrgid() not implemented\n";
27 exit 0;
28}
29
2986a63f 30quit() if (($^O eq 'MSWin32' || $^O eq 'NetWare') or $^O =~ /lynxos/i);
13d7cbc1 31
d0f88fcc 32# We have to find a command that prints all (effective
33# and real) group names (not ids). The known commands are:
34# groups
35# id -Gn
36# id -a
37# Beware 1: some systems do just 'id -G' even when 'id -Gn' is used.
98cfb1fc 38# Beware 2: id -Gn or id -a format might be id(name) or name(id).
39# Beware 3: the groups= might be anywhere in the id output.
f62c0cf2 40# Beware 4: groups can have spaces ('id -a' being the only defense against this)
702a0e5a 41# Beware 5: id -a might not contain the groups= part.
98cfb1fc 42#
43# That is, we might meet the following:
44#
f62c0cf2 45# foo bar zot # accept
46# foo 22 42 bar zot # accept
47# 1 22 42 2 3 # reject
48# groups=(42),foo(1),bar(2),zot me(3) # parse
49# groups=22,42,1(foo),2(bar),3(zot me) # parse
98cfb1fc 50#
51# and the groups= might be after, before, or between uid=... and gid=...
d0f88fcc 52
53GROUPS: {
f62c0cf2 54 # prefer 'id' over 'groups' (is this ever wrong anywhere?)
55 # and 'id -a' over 'id -Gn' (the former is good about spaces in group names)
56 if (($groups = `id -a 2>/dev/null`) ne '') {
57 # $groups is of the form:
58 # uid=39957(gsar) gid=22(users) groups=33536,39181,22(users),0(root),1067(dev)
7b75a55b 59 # FreeBSD since 6.2 has a fake id -a:
60 # uid=1001(tobez) gid=20(staff) groups=20(staff), 0(wheel), 68(dialer)
702a0e5a 61 last GROUPS if $groups =~ /groups=/;
f62c0cf2 62 }
63 if (($groups = `id -Gn 2>/dev/null`) ne '') {
64 # $groups could be of the form:
65 # users 33536 39181 root dev
66 last GROUPS if $groups !~ /^(\d|\s)+$/;
d0f88fcc 67 }
f62c0cf2 68 if (($groups = `groups 2>/dev/null`) ne '') {
69 # may not reflect all groups in some places, so do a sanity check
70 if (-d '/afs') {
71 print <<EOM;
72# These test results *may* be bogus, as you appear to have AFS,
73# and I can't find a working 'id' in your PATH (which I have set
74# to '$ENV{PATH}').
75#
76# If these tests fail, report the particular incantation you use
77# on this platform to find *all* the groups that an arbitrary
9380b46b 78# user may belong to, using the 'perlbug' program.
f62c0cf2 79EOM
80 }
98cfb1fc 81 last GROUPS;
d0f88fcc 82 }
83 # Okay, not today.
13d7cbc1 84 quit();
fe14fcc3 85}
86
dd570ea6 87chomp($groups);
88
89print "# groups = $groups\n";
90
98cfb1fc 91# Remember that group names can contain whitespace, '-', et cetera.
92# That is: do not \w, do not \S.
f62c0cf2 93if ($groups =~ /groups=(.+)( [ug]id=|$)/) {
98cfb1fc 94 my $gr = $1;
7b75a55b 95 my @g0 = split /, ?/, $gr;
f62c0cf2 96 my @g1;
97 # prefer names over numbers
98 for (@g0) {
da4b9520 99 # 42(zot me)
9461e3d0 100 if (/^(\d+)(?:\(([^)]+)\))?/) {
f62c0cf2 101 push @g1, ($2 || $1);
102 }
da4b9520 103 # zot me(42)
9461e3d0 104 elsif (/^([^(]*)\((\d+)\)/) {
f62c0cf2 105 push @g1, ($1 || $2);
106 }
107 else {
108 print "# ignoring group entry [$_]\n";
109 }
110 }
111 print "# groups=$gr\n";
98cfb1fc 112 print "# g0 = @g0\n";
113 print "# g1 = @g1\n";
f62c0cf2 114 $groups = "@g1";
98cfb1fc 115}
116
988174c1 117print "1..2\n";
118
119$pwgid = $( + 0;
120($pwgnam) = getgrgid($pwgid);
988174c1 121$seen{$pwgid}++;
fe14fcc3 122
dd570ea6 123print "# pwgid = $pwgid, pwgnam = $pwgnam\n";
124
fe14fcc3 125for (split(' ', $()) {
6e21c824 126 ($group) = getgrgid($_);
04333ffa 127 next if (! defined $group or ! grep { $_ eq $group } @gr) and $seen{$_}++;
6e21c824 128 if (defined $group) {
129 push(@gr, $group);
130 }
131 else {
132 push(@gr, $_);
133 }
da4b9520 134}
988174c1 135
dd570ea6 136print "# gr = @gr\n";
137
da4b9520 138my %did;
0c52c6a9 139if ($^O =~ /^(?:uwin|cygwin|interix|solaris)$/) {
27b4d0f8 140 # Or anybody else who can have spaces in group names.
72720e3c 141 $gr1 = join(' ', grep(!$did{$_}++, sort split(' ', join(' ', @gr))));
142} else {
da4b9520 143 # Don't assume that there aren't duplicate groups
144 $gr1 = join(' ', sort grep defined $_ && !$did{$_}++, @gr);
72720e3c 145}
988174c1 146
732266dc 147if ($Config{myuname} =~ /^cygwin_nt/i) { # basegroup on CYGWIN_NT has id = 0.
148 @basegroup{$pwgid,$pwgnam} = (0,0);
149} else {
150 @basegroup{$pwgid,$pwgnam} = (1,1);
151}
b9416812 152$gr2 = join(' ', grep(!$basegroup{$_}++, sort split(' ',$groups)));
988174c1 153
732266dc 154my $ok1 = 0;
dd570ea6 155if ($gr1 eq $gr2 || ($gr1 eq '' && $gr2 eq $pwgid)) {
988174c1 156 print "ok 1\n";
732266dc 157 $ok1++;
988174c1 158}
732266dc 159elsif ($Config{myuname} =~ /^cygwin_nt/i) { # basegroup on CYGWIN_NT has id = 0.
160 # Retry in default unix mode
161 %basegroup = ( $pwgid => 1, $pwgnam => 1 );
162 $gr2 = join(' ', grep(!$basegroup{$_}++, sort split(' ',$groups)));
163 if ($gr1 eq $gr2 || ($gr1 eq '' && $gr2 eq $pwgid)) {
164 print "ok 1 # This Cygwin behaves like Unix (Win2k?)\n";
165 $ok1++;
166 }
167}
168unless ($ok1) {
988174c1 169 print "#gr1 is <$gr1>\n";
170 print "#gr2 is <$gr2>\n";
171 print "not ok 1\n";
172}
173
174# multiple 0's indicate GROUPSTYPE is currently long but should be short
175
176if ($pwgid == 0 || $seen{0} < 2) {
177 print "ok 2\n";
178}
179else {
180 print "not ok 2 (groupstype should be type short, not long)\n";
181}