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