This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Need @INC = '../lib' mantra
[perl5.git] / t / op / groups.t
CommitLineData
fe14fcc3
LW
1#!./perl
2
61ae2fbf
JH
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
MS
9 chdir 't';
10 @INC = '../lib';
11
9380b46b
JH
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
GS
22 exit 0;
23}
24
8e3eacad
MS
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
JH
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
JH
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
JH
42#
43# That is, we might meet the following:
44#
f62c0cf2
GS
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
JH
50#
51# and the groups= might be after, before, or between uid=... and gid=...
d0f88fcc
JH
52
53GROUPS: {
f62c0cf2
GS
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)
702a0e5a 59 last GROUPS if $groups =~ /groups=/;
f62c0cf2
GS
60 }
61 if (($groups = `id -Gn 2>/dev/null`) ne '') {
62 # $groups could be of the form:
63 # users 33536 39181 root dev
64 last GROUPS if $groups !~ /^(\d|\s)+$/;
d0f88fcc 65 }
f62c0cf2
GS
66 if (($groups = `groups 2>/dev/null`) ne '') {
67 # may not reflect all groups in some places, so do a sanity check
68 if (-d '/afs') {
69 print <<EOM;
70# These test results *may* be bogus, as you appear to have AFS,
71# and I can't find a working 'id' in your PATH (which I have set
72# to '$ENV{PATH}').
73#
74# If these tests fail, report the particular incantation you use
75# on this platform to find *all* the groups that an arbitrary
9380b46b 76# user may belong to, using the 'perlbug' program.
f62c0cf2
GS
77EOM
78 }
98cfb1fc 79 last GROUPS;
d0f88fcc
JH
80 }
81 # Okay, not today.
13d7cbc1 82 quit();
fe14fcc3
LW
83}
84
dd570ea6
JH
85chomp($groups);
86
87print "# groups = $groups\n";
88
98cfb1fc
JH
89# Remember that group names can contain whitespace, '-', et cetera.
90# That is: do not \w, do not \S.
f62c0cf2 91if ($groups =~ /groups=(.+)( [ug]id=|$)/) {
98cfb1fc 92 my $gr = $1;
f62c0cf2
GS
93 my @g0 = split /,/, $gr;
94 my @g1;
95 # prefer names over numbers
96 for (@g0) {
97 # 42(zot me)
9461e3d0 98 if (/^(\d+)(?:\(([^)]+)\))?/) {
f62c0cf2
GS
99 push @g1, ($2 || $1);
100 }
101 # zot me(42)
9461e3d0 102 elsif (/^([^(]*)\((\d+)\)/) {
f62c0cf2
GS
103 push @g1, ($1 || $2);
104 }
105 else {
106 print "# ignoring group entry [$_]\n";
107 }
108 }
109 print "# groups=$gr\n";
98cfb1fc
JH
110 print "# g0 = @g0\n";
111 print "# g1 = @g1\n";
f62c0cf2 112 $groups = "@g1";
98cfb1fc
JH
113}
114
988174c1
LW
115print "1..2\n";
116
117$pwgid = $( + 0;
118($pwgnam) = getgrgid($pwgid);
9380b46b 119if ($Config{myuname} =~ /^cygwin_nt/i) { # basegroup on CYGWIN_NT has id = 0.
f8da21f9
GH
120 @basegroup{$pwgid,$pwgnam} = (0,0);
121} else {
122 @basegroup{$pwgid,$pwgnam} = (1,1);
123}
988174c1 124$seen{$pwgid}++;
fe14fcc3 125
dd570ea6
JH
126print "# pwgid = $pwgid, pwgnam = $pwgnam\n";
127
fe14fcc3
LW
128for (split(' ', $()) {
129 next if $seen{$_}++;
6e21c824
LW
130 ($group) = getgrgid($_);
131 if (defined $group) {
132 push(@gr, $group);
133 }
134 else {
135 push(@gr, $_);
136 }
fe14fcc3 137}
988174c1 138
dd570ea6
JH
139print "# gr = @gr\n";
140
27b4d0f8
GS
141if ($^O =~ /^(?:uwin|solaris)$/) {
142 # Or anybody else who can have spaces in group names.
72720e3c
GS
143 $gr1 = join(' ', grep(!$did{$_}++, sort split(' ', join(' ', @gr))));
144} else {
145 $gr1 = join(' ', sort @gr);
146}
988174c1 147
b9416812 148$gr2 = join(' ', grep(!$basegroup{$_}++, sort split(' ',$groups)));
988174c1 149
dd570ea6 150if ($gr1 eq $gr2 || ($gr1 eq '' && $gr2 eq $pwgid)) {
988174c1
LW
151 print "ok 1\n";
152}
153else {
154 print "#gr1 is <$gr1>\n";
155 print "#gr2 is <$gr2>\n";
156 print "not ok 1\n";
157}
158
159# multiple 0's indicate GROUPSTYPE is currently long but should be short
160
161if ($pwgid == 0 || $seen{0} < 2) {
162 print "ok 2\n";
163}
164else {
165 print "not ok 2 (groupstype should be type short, not long)\n";
166}