[ID 20000802.002] [PATCH] memory pseudo-leak in sv_dump
[p5sagit/p5-mst-13.2.git] / t / op / grent.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 = getgrgid 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_grp'} ne 'define') {
14 $reason = '$Config{i_grp} not defined';
15 }
16 elsif (not -f "/etc/group" ) { # Play safe.
17 $reason = 'no /etc/group file';
b91c0863 18 }
c5987ebb 19
b91c0863 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(GR, "$ypcat group 2>/dev/null |") &&
24 defined(<GR>)) {
25 $where = "NIS group";
45c0de28 26 undef $reason;
b91c0863 27 last;
28 }
29 }
30 }
31
32 if (not defined $where) { # Try NetInfo.
33 foreach my $nidump (qw(/usr/bin/nidump)) {
34 if (-x $nidump &&
35 open(GR, "$nidump group . 2>/dev/null |") &&
36 defined(<GR>)) {
37 $where = "NetInfo group";
45c0de28 38 undef $reason;
b91c0863 39 last;
40 }
41 }
42 }
55ec6b63 43
b91c0863 44 if (not defined $where) { # Try local.
45 my $GR = "/etc/group";
46 if (-f $GR && open(GR, $GR) && defined(<GR>)) {
45c0de28 47 undef $reason;
b91c0863 48 $where = $GR;
55ec6b63 49 }
b91c0863 50 }
45c0de28 51 if ($reason) {
52 print "1..0 # Skip: $reason\n";
c5987ebb 53 exit 0;
54 }
55}
56
765e9edb 57# By now the GR filehandle should be open and full of juicy group entries.
b91c0863 58
765e9edb 59print "1..2\n";
c5987ebb 60
61# Go through at most this many groups.
b91c0863 62# (note that the first entry has been read away by now)
55ec6b63 63my $max = 25;
c5987ebb 64
55ec6b63 65my $n = 0;
c5987ebb 66my $tst = 1;
b91c0863 67my %perfect;
55ec6b63 68my %seen;
c5987ebb 69
bd055eb9 70setgrent();
c5987ebb 71while (<GR>) {
c5987ebb 72 chomp;
55ec6b63 73 my @s = split /:/;
74 my ($name_s,$passwd_s,$gid_s,$members_s) = @s;
75 if (@s) {
76 push @{ $seen{$name_s} }, $.;
77 } else {
78 warn "# Your $where line $. is empty.\n";
79 next;
80 }
09ac174e 81 if ($n == $max) {
82 local $/;
83 my $junk = <GR>;
84 last;
85 }
55ec6b63 86 # In principle we could whine if @s != 4 but do we know enough
87 # of group file formats everywhere?
c5987ebb 88 if (@s == 4) {
5e5f18aa 89 $members_s =~ s/\s*,\s*/,/g;
b56ec344 90 $members_s =~ s/\s+$//;
91 $members_s =~ s/^\s+//;
c5987ebb 92 @n = getgrgid($gid_s);
93 # 'nogroup' et al.
94 next unless @n;
95 my ($name,$passwd,$gid,$members) = @n;
96 # Protect against one-to-many and many-to-one mappings.
97 if ($name_s ne $name) {
98 @n = getgrnam($name_s);
99 ($name,$passwd,$gid,$members) = @n;
100 next if $name_s ne $name;
101 }
b91c0863 102 # NOTE: group names *CAN* contain whitespace.
5e5f18aa 103 $members =~ s/\s+/,/g;
b91c0863 104 # what about different orders of members?
105 $perfect{$name_s}++
106 if $name eq $name_s and
107# Do not compare passwords: think shadow passwords.
55ec6b63 108# Not that group passwords are used much but better not assume anything.
b91c0863 109 $gid eq $gid_s and
110 $members eq $members_s;
c5987ebb 111 }
112 $n++;
113}
114
bd055eb9 115endgrent();
116
b91c0863 117if (keys %perfect == 0) {
118 $max++;
119 print <<EOEX;
120#
121# The failure of op/grent test is not necessarily serious.
122# It may fail due to local group administration conventions.
123# If you are for example using both NIS and local groups,
124# test failure is possible. Any distributed group scheme
125# can cause such failures.
126#
127# What the grent test is doing is that it compares the $max first
128# entries of $where
129# with the results of getgrgid() and getgrnam() call. If it finds no
130# matches at all, it suspects something is wrong.
131#
132EOEX
133 print "not ";
134 $not = 1;
135} else {
136 $not = 0;
55ec6b63 137}
b91c0863 138print "ok ", $tst++;
139print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
140print "\n";
c5987ebb 141
765e9edb 142# Test both the scalar and array contexts.
143
144my @gr1;
145
765e9edb 146setgrent();
147for (1..$max) {
148 my $gr = scalar getgrent();
149 last unless defined $gr;
150 push @gr1, $gr;
151}
bd055eb9 152endgrent();
765e9edb 153
154my @gr2;
155
765e9edb 156setgrent();
157for (1..$max) {
158 my ($gr) = (getgrent());
159 last unless defined $gr;
160 push @gr2, $gr;
161}
bd055eb9 162endgrent();
765e9edb 163
164print "not " unless "@gr1" eq "@gr2";
165print "ok ", $tst++, "\n";
166
c5987ebb 167close(GR);