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