Integrate from mainperl.
[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";
c5987ebb 6 eval { require Config; import Config; };
45c0de28 7 my $reason;
8 if ($Config{'i_grp'} ne 'define') {
9 $reason = '$Config{i_grp} not defined';
10 }
11 elsif (not -f "/etc/group" ) { # Play safe.
12 $reason = 'no /etc/group file';
b91c0863 13 }
c5987ebb 14
b91c0863 15 if (not defined $where) { # Try NIS.
16 foreach my $ypcat (qw(/usr/bin/ypcat /bin/ypcat /etc/ypcat)) {
17 if (-x $ypcat &&
18 open(GR, "$ypcat group 2>/dev/null |") &&
19 defined(<GR>)) {
20 $where = "NIS group";
45c0de28 21 undef $reason;
b91c0863 22 last;
23 }
24 }
25 }
26
27 if (not defined $where) { # Try NetInfo.
28 foreach my $nidump (qw(/usr/bin/nidump)) {
29 if (-x $nidump &&
30 open(GR, "$nidump group . 2>/dev/null |") &&
31 defined(<GR>)) {
32 $where = "NetInfo group";
45c0de28 33 undef $reason;
b91c0863 34 last;
35 }
36 }
37 }
55ec6b63 38
b91c0863 39 if (not defined $where) { # Try local.
40 my $GR = "/etc/group";
41 if (-f $GR && open(GR, $GR) && defined(<GR>)) {
45c0de28 42 undef $reason;
b91c0863 43 $where = $GR;
55ec6b63 44 }
b91c0863 45 }
45c0de28 46 if ($reason) {
47 print "1..0 # Skip: $reason\n";
c5987ebb 48 exit 0;
49 }
50}
51
b91c0863 52# By now GR filehandle should be open and full of juicy group entries.
53
c5987ebb 54print "1..1\n";
55
56# Go through at most this many groups.
b91c0863 57# (note that the first entry has been read away by now)
55ec6b63 58my $max = 25;
c5987ebb 59
55ec6b63 60my $n = 0;
c5987ebb 61my $tst = 1;
b91c0863 62my %perfect;
55ec6b63 63my %seen;
c5987ebb 64
c5987ebb 65while (<GR>) {
c5987ebb 66 chomp;
55ec6b63 67 my @s = split /:/;
68 my ($name_s,$passwd_s,$gid_s,$members_s) = @s;
69 if (@s) {
70 push @{ $seen{$name_s} }, $.;
71 } else {
72 warn "# Your $where line $. is empty.\n";
73 next;
74 }
b91c0863 75 last if $n == $max;
55ec6b63 76 # In principle we could whine if @s != 4 but do we know enough
77 # of group file formats everywhere?
c5987ebb 78 if (@s == 4) {
5e5f18aa 79 $members_s =~ s/\s*,\s*/,/g;
b56ec344 80 $members_s =~ s/\s+$//;
81 $members_s =~ s/^\s+//;
c5987ebb 82 @n = getgrgid($gid_s);
83 # 'nogroup' et al.
84 next unless @n;
85 my ($name,$passwd,$gid,$members) = @n;
86 # Protect against one-to-many and many-to-one mappings.
87 if ($name_s ne $name) {
88 @n = getgrnam($name_s);
89 ($name,$passwd,$gid,$members) = @n;
90 next if $name_s ne $name;
91 }
b91c0863 92 # NOTE: group names *CAN* contain whitespace.
5e5f18aa 93 $members =~ s/\s+/,/g;
b91c0863 94 # what about different orders of members?
95 $perfect{$name_s}++
96 if $name eq $name_s and
97# Do not compare passwords: think shadow passwords.
55ec6b63 98# Not that group passwords are used much but better not assume anything.
b91c0863 99 $gid eq $gid_s and
100 $members eq $members_s;
c5987ebb 101 }
102 $n++;
103}
104
b91c0863 105if (keys %perfect == 0) {
106 $max++;
107 print <<EOEX;
108#
109# The failure of op/grent test is not necessarily serious.
110# It may fail due to local group administration conventions.
111# If you are for example using both NIS and local groups,
112# test failure is possible. Any distributed group scheme
113# can cause such failures.
114#
115# What the grent test is doing is that it compares the $max first
116# entries of $where
117# with the results of getgrgid() and getgrnam() call. If it finds no
118# matches at all, it suspects something is wrong.
119#
120EOEX
121 print "not ";
122 $not = 1;
123} else {
124 $not = 0;
55ec6b63 125}
b91c0863 126print "ok ", $tst++;
127print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
128print "\n";
c5987ebb 129
130close(GR);