This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Leopard has more standard /etc/passwd files than previous
[perl5.git] / t / op / pwent.t
CommitLineData
c5987ebb
JH
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
b62e3068 6 eval {my @n = getpwuid 0; setpwent()};
df284ca6
JD
7 if ($@ && $@ =~ /(The \w+ function is unimplemented)/) {
8 print "1..0 # Skip: $1\n";
9 exit 0;
10 }
c5987ebb 11 eval { require Config; import Config; };
45c0de28
GS
12 my $reason;
13 if ($Config{'i_pwd'} ne 'define') {
14 $reason = '$Config{i_pwd} undefined';
15 }
16 elsif (not -f "/etc/passwd" ) { # Play safe.
17 $reason = 'no /etc/passwd file';
b91c0863
JH
18 }
19
20 if (not defined $where) { # Try NIS.
21 foreach my $ypcat (qw(/usr/bin/ypcat /bin/ypcat /etc/ypcat)) {
22 if (-x $ypcat &&
23 open(PW, "$ypcat passwd 2>/dev/null |") &&
24 defined(<PW>)) {
25 $where = "NIS passwd";
45c0de28 26 undef $reason;
b91c0863
JH
27 last;
28 }
29 }
30 }
c5987ebb 31
b91c0863
JH
32 if (not defined $where) { # Try NetInfo.
33 foreach my $nidump (qw(/usr/bin/nidump)) {
34 if (-x $nidump &&
35 open(PW, "$nidump passwd . 2>/dev/null |") &&
36 defined(<PW>)) {
37 $where = "NetInfo passwd";
45c0de28 38 undef $reason;
b91c0863
JH
39 last;
40 }
41 }
42 }
55ec6b63 43
b91c0863
JH
44 if (not defined $where) { # Try local.
45 my $PW = "/etc/passwd";
46 if (-f $PW && open(PW, $PW) && defined(<PW>)) {
47 $where = $PW;
45c0de28 48 undef $reason;
55ec6b63 49 }
b91c0863
JH
50 }
51
a9a3fcb4
PP
52 if (not defined $where) { # Try NIS+
53 foreach my $niscat (qw(/bin/niscat)) {
54 if (-x $niscat &&
55 open(PW, "$niscat passwd.org_dir 2>/dev/null |") &&
56 defined(<PW>)) {
57 $where = "NIS+ $niscat passwd.org_dir";
58 undef $reason;
59 last;
60 }
61 }
62 }
63
45c0de28
GS
64 if ($reason) { # Give up.
65 print "1..0 # Skip: $reason\n";
c5987ebb
JH
66 exit 0;
67 }
68}
69
765e9edb 70# By now the PW filehandle should be open and full of juicy password entries.
b91c0863 71
765e9edb 72print "1..2\n";
c5987ebb
JH
73
74# Go through at most this many users.
b91c0863
JH
75# (note that the first entry has been read away by now)
76my $max = 25;
c5987ebb
JH
77
78my $n = 0;
c5987ebb 79my $tst = 1;
b91c0863 80my %perfect;
55ec6b63 81my %seen;
c5987ebb 82
f0debaab
JH
83print "# where $where\n";
84
bd055eb9 85setpwent();
f0debaab 86
c5987ebb 87while (<PW>) {
c5987ebb 88 chomp;
a941e390
MD
89 # LIMIT -1 so that users with empty shells don't fall off
90 my @s = split /:/, $_, -1;
32b4ad3c 91 my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s);
8faed529
RGS
92 (my $v) = $Config{osvers} =~ /^(\d+)/;
93 if ($^O eq 'darwin' && $v < 9) {
32b4ad3c
PS
94 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s[0,1,2,3,7,8,9];
95 } else {
96 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
97 }
b91c0863 98 next if /^\+/; # ignore NIS includes
55ec6b63
JH
99 if (@s) {
100 push @{ $seen{$name_s} }, $.;
101 } else {
102 warn "# Your $where line $. is empty.\n";
103 next;
104 }
09ac174e
GB
105 if ($n == $max) {
106 local $/;
107 my $junk = <PW>;
108 last;
109 }
55ec6b63
JH
110 # In principle we could whine if @s != 7 but do we know enough
111 # of passwd file formats everywhere?
32b4ad3c 112 if (@s == 7 || ($^O eq 'darwin' && @s == 10)) {
c5987ebb
JH
113 @n = getpwuid($uid_s);
114 # 'nobody' et al.
115 next unless @n;
116 my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
117 # Protect against one-to-many and many-to-one mappings.
118 if ($name_s ne $name) {
119 @n = getpwnam($name_s);
120 ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
121 next if $name_s ne $name;
122 }
b91c0863
JH
123 $perfect{$name_s}++
124 if $name eq $name_s and
125 $uid eq $uid_s and
126# Do not compare passwords: think shadow passwords.
127 $gid eq $gid_s and
128 $gcos eq $gcos_s and
129 $home eq $home_s and
130 $shell eq $shell_s;
c5987ebb
JH
131 }
132 $n++;
133}
f0debaab 134
bd055eb9 135endpwent();
c5987ebb 136
f0debaab
JH
137print "# max = $max, n = $n, perfect = ", scalar keys %perfect, "\n";
138
93d44f78 139if (keys %perfect == 0 && $n) {
b91c0863
JH
140 $max++;
141 print <<EOEX;
142#
143# The failure of op/pwent test is not necessarily serious.
144# It may fail due to local password administration conventions.
145# If you are for example using both NIS and local passwords,
146# test failure is possible. Any distributed password scheme
147# can cause such failures.
148#
149# What the pwent test is doing is that it compares the $max first
150# entries of $where
151# with the results of getpwuid() and getpwnam() call. If it finds no
152# matches at all, it suspects something is wrong.
153#
154EOEX
155 print "not ";
156 $not = 1;
157} else {
158 $not = 0;
55ec6b63 159}
b91c0863
JH
160print "ok ", $tst++;
161print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not;
162print "\n";
c5987ebb 163
91e74348 164# Test both the scalar and list contexts.
765e9edb
JH
165
166my @pw1;
167
765e9edb
JH
168setpwent();
169for (1..$max) {
170 my $pw = scalar getpwent();
171 last unless defined $pw;
172 push @pw1, $pw;
173}
bd055eb9 174endpwent();
765e9edb
JH
175
176my @pw2;
177
765e9edb
JH
178setpwent();
179for (1..$max) {
180 my ($pw) = (getpwent());
181 last unless defined $pw;
182 push @pw2, $pw;
183}
bd055eb9 184endpwent();
765e9edb
JH
185
186print "not " unless "@pw1" eq "@pw2";
187print "ok ", $tst++, "\n";
188
c5987ebb 189close(PW);