This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Be even more relaxed (re-allow having only one entry).
[perl5.git] / t / op / grent.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     eval {my @n = getgrgid 0};
7     if ($@ && $@ =~ /(The \w+ function is unimplemented)/) {
8         print "1..0 # Skip: $1\n";
9         exit 0;
10     }
11     eval { require Config; import Config; };
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';
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(GR, "$ypcat group 2>/dev/null |") &&
24                 defined(<GR>)) {
25                 $where = "NIS group";
26                 undef $reason;
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";
38                 undef $reason;
39                 last;
40             }
41         }
42     }
43
44     if (not defined $where) {   # Try local.
45         my $GR = "/etc/group";
46         if (-f $GR && open(GR, $GR) && defined(<GR>)) {
47             undef $reason;
48             $where = $GR;
49         }
50     }
51     if ($reason) {
52         print "1..0 # Skip: $reason\n";
53         exit 0;
54     }
55 }
56
57 # By now the GR filehandle should be open and full of juicy group entries.
58
59 print "1..2\n";
60
61 # Go through at most this many groups.
62 # (note that the first entry has been read away by now)
63 my $max = 25;
64
65 my $n   = 0;
66 my $tst = 1;
67 my %perfect;
68 my %seen;
69
70 print "# where $where\n";
71
72 setgrent();
73
74 while (<GR>) {
75     chomp;
76     # LIMIT -1 so that groups with no users don't fall off
77     my @s = split /:/, $_, -1;
78     my ($name_s,$passwd_s,$gid_s,$members_s) = @s;
79     if (@s) {
80         push @{ $seen{$name_s} }, $.;
81     } else {
82         warn "# Your $where line $. is empty.\n";
83         next;
84     }
85     if ($n == $max) {
86         local $/;
87         my $junk = <GR>;
88         last;
89     }
90     # In principle we could whine if @s != 4 but do we know enough
91     # of group file formats everywhere?
92     if (@s == 4) {
93         $members_s =~ s/\s*,\s*/,/g;
94         $members_s =~ s/\s+$//;
95         $members_s =~ s/^\s+//;
96         @n = getgrgid($gid_s);
97         # 'nogroup' et al.
98         next unless @n;
99         my ($name,$passwd,$gid,$members) = @n;
100         # Protect against one-to-many and many-to-one mappings.
101         if ($name_s ne $name) {
102             @n = getgrnam($name_s);
103             ($name,$passwd,$gid,$members) = @n;
104             next if $name_s ne $name;
105         }
106         # NOTE: group names *CAN* contain whitespace.
107         $members =~ s/\s+/,/g;
108         # what about different orders of members?
109         $perfect{$name_s}++
110             if $name    eq $name_s    and
111 # Do not compare passwords: think shadow passwords.
112 # Not that group passwords are used much but better not assume anything.
113                $gid     eq $gid_s     and
114                $members eq $members_s;
115     }
116     $n++;
117 }
118
119 endgrent();
120
121 print "# max = $max, n = $n, perfect = ", scalar keys %perfect, "\n";
122
123 if (keys %perfect == 0 && $n) {
124     $max++;
125     print <<EOEX;
126 #
127 # The failure of op/grent test is not necessarily serious.
128 # It may fail due to local group administration conventions.
129 # If you are for example using both NIS and local groups,
130 # test failure is possible.  Any distributed group scheme
131 # can cause such failures.
132 #
133 # What the grent test is doing is that it compares the $max first
134 # entries of $where
135 # with the results of getgrgid() and getgrnam() call.  If it finds no
136 # matches at all, it suspects something is wrong.
137
138 EOEX
139     print "not ";
140     $not = 1;
141 } else {
142     $not = 0;
143 }
144 print "ok ", $tst++;
145 print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
146 print "\n";
147
148 # Test both the scalar and list contexts.
149
150 my @gr1;
151
152 setgrent();
153 for (1..$max) {
154     my $gr = scalar getgrent();
155     last unless defined $gr;
156     push @gr1, $gr;
157 }
158 endgrent();
159
160 my @gr2;
161
162 setgrent();
163 for (1..$max) {
164     my ($gr) = (getgrent());
165     last unless defined $gr;
166     push @gr2, $gr;
167 }
168 endgrent();
169
170 print "not " unless "@gr1" eq "@gr2";
171 print "ok ", $tst++, "\n";
172
173 close(GR);