chop/chomp modify readonly values
[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; };
7
b91c0863 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 }
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";
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 }
55ec6b63 36
b91c0863 37 if (not defined $where) { # Try local.
38 my $GR = "/etc/group";
39 if (-f $GR && open(GR, $GR) && defined(<GR>)) {
40 $where = $GR;
55ec6b63 41 }
b91c0863 42 }
43
44 if (not defined $where) { # Give up.
c5987ebb 45 print "1..0\n";
46 exit 0;
47 }
48}
49
b91c0863 50# By now GR filehandle should be open and full of juicy group entries.
51
c5987ebb 52print "1..1\n";
53
54# Go through at most this many groups.
b91c0863 55# (note that the first entry has been read away by now)
55ec6b63 56my $max = 25;
c5987ebb 57
55ec6b63 58my $n = 0;
c5987ebb 59my $tst = 1;
b91c0863 60my %perfect;
55ec6b63 61my %seen;
c5987ebb 62
c5987ebb 63while (<GR>) {
c5987ebb 64 chomp;
55ec6b63 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 }
b91c0863 73 last if $n == $max;
55ec6b63 74 # In principle we could whine if @s != 4 but do we know enough
75 # of group file formats everywhere?
c5987ebb 76 if (@s == 4) {
5e5f18aa 77 $members_s =~ s/\s*,\s*/,/g;
b56ec344 78 $members_s =~ s/\s+$//;
79 $members_s =~ s/^\s+//;
c5987ebb 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 }
b91c0863 90 # NOTE: group names *CAN* contain whitespace.
5e5f18aa 91 $members =~ s/\s+/,/g;
b91c0863 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.
55ec6b63 96# Not that group passwords are used much but better not assume anything.
b91c0863 97 $gid eq $gid_s and
98 $members eq $members_s;
c5987ebb 99 }
100 $n++;
101}
102
b91c0863 103if (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#
118EOEX
119 print "not ";
120 $not = 1;
121} else {
122 $not = 0;
55ec6b63 123}
b91c0863 124print "ok ", $tst++;
125print "\t# (not necessarily serious: run t/op/grent.t by itself)" if $not;
126print "\n";
c5987ebb 127
128close(GR);