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