Test hex('x...').
[p5sagit/p5-mst-13.2.git] / t / op / pwent.t
CommitLineData
c5987ebb 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
b91c0863 5 unshift @INC, "../lib" if -d "../lib";
df284ca6 6 eval {my @n = getpwuid 0};
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 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 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 27 last;
28 }
29 }
30 }
c5987ebb 31
b91c0863 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 39 last;
40 }
41 }
42 }
55ec6b63 43
b91c0863 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 50 }
51
45c0de28 52 if ($reason) { # Give up.
53 print "1..0 # Skip: $reason\n";
c5987ebb 54 exit 0;
55 }
56}
57
b91c0863 58# By now PW filehandle should be open and full of juicy password entries.
59
c5987ebb 60print "1..1\n";
61
62# Go through at most this many users.
b91c0863 63# (note that the first entry has been read away by now)
64my $max = 25;
c5987ebb 65
66my $n = 0;
c5987ebb 67my $tst = 1;
b91c0863 68my %perfect;
55ec6b63 69my %seen;
c5987ebb 70
c5987ebb 71while (<PW>) {
c5987ebb 72 chomp;
55ec6b63 73 my @s = split /:/;
74 my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
b91c0863 75 next if /^\+/; # ignore NIS includes
55ec6b63 76 if (@s) {
77 push @{ $seen{$name_s} }, $.;
78 } else {
79 warn "# Your $where line $. is empty.\n";
80 next;
81 }
b91c0863 82 last if $n == $max;
55ec6b63 83 # In principle we could whine if @s != 7 but do we know enough
84 # of passwd file formats everywhere?
c5987ebb 85 if (@s == 7) {
c5987ebb 86 @n = getpwuid($uid_s);
87 # 'nobody' et al.
88 next unless @n;
89 my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
90 # Protect against one-to-many and many-to-one mappings.
91 if ($name_s ne $name) {
92 @n = getpwnam($name_s);
93 ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
94 next if $name_s ne $name;
95 }
b91c0863 96 $perfect{$name_s}++
97 if $name eq $name_s and
98 $uid eq $uid_s and
99# Do not compare passwords: think shadow passwords.
100 $gid eq $gid_s and
101 $gcos eq $gcos_s and
102 $home eq $home_s and
103 $shell eq $shell_s;
c5987ebb 104 }
105 $n++;
106}
107
b91c0863 108if (keys %perfect == 0) {
109 $max++;
110 print <<EOEX;
111#
112# The failure of op/pwent test is not necessarily serious.
113# It may fail due to local password administration conventions.
114# If you are for example using both NIS and local passwords,
115# test failure is possible. Any distributed password scheme
116# can cause such failures.
117#
118# What the pwent test is doing is that it compares the $max first
119# entries of $where
120# with the results of getpwuid() and getpwnam() call. If it finds no
121# matches at all, it suspects something is wrong.
122#
123EOEX
124 print "not ";
125 $not = 1;
126} else {
127 $not = 0;
55ec6b63 128}
b91c0863 129print "ok ", $tst++;
130print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not;
131print "\n";
c5987ebb 132
133close(PW);