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