change#3397 needs test tweak
[p5sagit/p5-mst-13.2.git] / t / op / grent.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     unshift @INC, "../lib" if -d "../lib";
6     eval { require Config; import Config; };
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';
13     }
14
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";
21                 undef $reason;
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";
33                 undef $reason;
34                 last;
35             }
36         }
37     }
38
39     if (not defined $where) {   # Try local.
40         my $GR = "/etc/group";
41         if (-f $GR && open(GR, $GR) && defined(<GR>)) {
42             undef $reason;
43             $where = $GR;
44         }
45     }
46     if ($reason) {
47         print "1..0 # Skip: $reason\n";
48         exit 0;
49     }
50 }
51
52 # By now GR filehandle should be open and full of juicy group entries.
53
54 print "1..1\n";
55
56 # Go through at most this many groups.
57 # (note that the first entry has been read away by now)
58 my $max = 25;
59
60 my $n   = 0;
61 my $tst = 1;
62 my %perfect;
63 my %seen;
64
65 while (<GR>) {
66     chomp;
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     }
75     last if $n == $max;
76     # In principle we could whine if @s != 4 but do we know enough
77     # of group file formats everywhere?
78     if (@s == 4) {
79         $members_s =~ s/\s*,\s*/,/g;
80         $members_s =~ s/\s+$//;
81         $members_s =~ s/^\s+//;
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         }
92         # NOTE: group names *CAN* contain whitespace.
93         $members =~ s/\s+/,/g;
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.
98 # Not that group passwords are used much but better not assume anything.
99                $gid     eq $gid_s     and
100                $members eq $members_s;
101     }
102     $n++;
103 }
104
105 if (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
120 EOEX
121     print "not ";
122     $not = 1;
123 } else {
124     $not = 0;
125 }
126 print "ok ", $tst++;
127 print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
128 print "\n";
129
130 close(GR);