This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Extend tr/\0-\377/blah/c support
[perl5.git] / t / op / grent.t
CommitLineData
c5987ebb
JH
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
df284ca6
JD
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
GS
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
JH
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
JH
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
JH
39 last;
40 }
41 }
42 }
55ec6b63 43
b91c0863
JH
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
GS
51 if ($reason) {
52 print "1..0 # Skip: $reason\n";
c5987ebb
JH
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
JH
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;
a941e390
MD
73 # LIMIT -1 so that groups with no users don't fall off
74 my @s = split /:/, $_, -1;
55ec6b63
JH
75 my ($name_s,$passwd_s,$gid_s,$members_s) = @s;
76 if (@s) {
77 push @{ $seen{$name_s} }, $.;
78 } else {
79 warn "# Your $where line $. is empty.\n";
80 next;
81 }
09ac174e
GB
82 if ($n == $max) {
83 local $/;
84 my $junk = <GR>;
85 last;
86 }
55ec6b63
JH
87 # In principle we could whine if @s != 4 but do we know enough
88 # of group file formats everywhere?
c5987ebb 89 if (@s == 4) {
5e5f18aa 90 $members_s =~ s/\s*,\s*/,/g;
b56ec344
JH
91 $members_s =~ s/\s+$//;
92 $members_s =~ s/^\s+//;
c5987ebb
JH
93 @n = getgrgid($gid_s);
94 # 'nogroup' et al.
95 next unless @n;
96 my ($name,$passwd,$gid,$members) = @n;
97 # Protect against one-to-many and many-to-one mappings.
98 if ($name_s ne $name) {
99 @n = getgrnam($name_s);
100 ($name,$passwd,$gid,$members) = @n;
101 next if $name_s ne $name;
102 }
b91c0863 103 # NOTE: group names *CAN* contain whitespace.
5e5f18aa 104 $members =~ s/\s+/,/g;
b91c0863
JH
105 # what about different orders of members?
106 $perfect{$name_s}++
107 if $name eq $name_s and
108# Do not compare passwords: think shadow passwords.
55ec6b63 109# Not that group passwords are used much but better not assume anything.
b91c0863
JH
110 $gid eq $gid_s and
111 $members eq $members_s;
c5987ebb
JH
112 }
113 $n++;
114}
115
bd055eb9
JH
116endgrent();
117
b91c0863
JH
118if (keys %perfect == 0) {
119 $max++;
120 print <<EOEX;
121#
122# The failure of op/grent test is not necessarily serious.
123# It may fail due to local group administration conventions.
124# If you are for example using both NIS and local groups,
125# test failure is possible. Any distributed group scheme
126# can cause such failures.
127#
128# What the grent test is doing is that it compares the $max first
129# entries of $where
130# with the results of getgrgid() and getgrnam() call. If it finds no
131# matches at all, it suspects something is wrong.
132#
133EOEX
134 print "not ";
135 $not = 1;
136} else {
137 $not = 0;
55ec6b63 138}
b91c0863
JH
139print "ok ", $tst++;
140print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
141print "\n";
c5987ebb 142
91e74348 143# Test both the scalar and list contexts.
765e9edb
JH
144
145my @gr1;
146
765e9edb
JH
147setgrent();
148for (1..$max) {
149 my $gr = scalar getgrent();
150 last unless defined $gr;
151 push @gr1, $gr;
152}
bd055eb9 153endgrent();
765e9edb
JH
154
155my @gr2;
156
765e9edb
JH
157setgrent();
158for (1..$max) {
159 my ($gr) = (getgrent());
160 last unless defined $gr;
161 push @gr2, $gr;
162}
bd055eb9 163endgrent();
765e9edb
JH
164
165print "not " unless "@gr1" eq "@gr2";
166print "ok ", $tst++, "\n";
167
c5987ebb 168close(GR);