This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added porting tests for CUSTOMIZED files
[perl5.git] / t / op / filetest.t
1 #!./perl
2
3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';
9     require './test.pl';
10 }
11
12 use Config;
13 plan(tests => 34 + 27*14);
14
15 ok( -d 'op' );
16 ok( -f 'TEST' );
17 ok( !-f 'op' );
18 ok( !-d 'TEST' );
19 ok( -r 'TEST' );
20
21 # Make a read only file
22 my $ro_file = tempfile();
23
24 {
25     open my $fh, '>', $ro_file or die "open $fh: $!";
26     close $fh or die "close $fh: $!";
27 }
28
29 chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";
30
31 $oldeuid = $>;          # root can read and write anything
32 eval '$> = 1';          # so switch uid (may not be implemented)
33
34 print "# oldeuid = $oldeuid, euid = $>\n";
35
36 SKIP: {
37     if (!$Config{d_seteuid}) {
38         skip('no seteuid');
39     } 
40     else {
41         ok( !-w $ro_file );
42     }
43 }
44
45 # Scripts are not -x everywhere so cannot test that.
46
47 eval '$> = $oldeuid';   # switch uid back (may not be implemented)
48
49 # this would fail for the euid 1
50 # (unless we have unpacked the source code as uid 1...)
51 ok( -r 'op' );
52
53 # this would fail for the euid 1
54 # (unless we have unpacked the source code as uid 1...)
55 SKIP: {
56     if ($Config{d_seteuid}) {
57         ok( -w 'op' );
58     } else {
59         skip('no seteuid');
60     }
61 }
62
63 ok( -x 'op' ); # Hohum.  Are directories -x everywhere?
64
65 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
66
67 # Test stackability of filetest operators
68
69 ok( defined( -f -d 'TEST' ) && ! -f -d _ );
70 ok( !defined( -e 'zoo' ) );
71 ok( !defined( -e -d 'zoo' ) );
72 ok( !defined( -f -e 'zoo' ) );
73 ok( -f -e 'TEST' );
74 ok( -e -f 'TEST' );
75 ok( defined(-d -e 'TEST') );
76 ok( defined(-e -d 'TEST') );
77 ok( ! -f -d 'op' );
78 ok( -x -d -x 'op' );
79 ok( (-s -f 'TEST' > 1), "-s returns real size" );
80 ok( -f -s 'TEST' == 1 );
81
82 # now with an empty file
83 my $tempfile = tempfile();
84 open my $fh, ">", $tempfile;
85 close $fh;
86 ok( -f $tempfile );
87 is( -s $tempfile, 0 );
88 is( -f -s $tempfile, 0 );
89 is( -s -f $tempfile, 0 );
90 unlink_all $tempfile;
91
92 # stacked -l
93 eval { -l -e "TEST" };
94 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
95   'stacked -l non-lstat error with warnings off';
96 {
97  local $^W = 1;
98  eval { -l -e "TEST" };
99  like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
100   'stacked -l non-lstat error with warnings on';
101 }
102 # Make sure -l is using the previous stat buffer, and not using the previ-
103 # ous op’s return value as a file name.
104 SKIP: {
105  use Perl::OSType 'os_type';
106  if (os_type ne 'Unix') { skip "Not Unix", 2 }
107  if (-l "TEST") { skip "TEST is a symlink", 2 }
108  chomp(my $ln = `which ln`);
109  if ( ! -e $ln ) { skip "No ln"   , 2 }
110  lstat "TEST";
111  `ln -s TEST 1`;
112  ok ! -l -e _, 'stacked -l uses previous stat, not previous retval';
113  unlink 1;
114
115  # Since we already have our skip block set up, we might as well put this
116  # test here, too:
117  # -l always treats a non-bareword argument as a file name
118  system qw "ln -s TEST", \*foo;
119  local $^W = 1;
120  ok -l \*foo, '-l \*foo is a file name';
121  unlink \*foo;
122 }
123
124 # test that _ is a bareword after filetest operators
125
126 -f 'TEST';
127 ok( -f _ );
128 sub _ { "this is not a file name" }
129 ok( -f _ );
130
131 my $over;
132 {
133     package OverFtest;
134
135     use overload 
136         fallback => 1,
137         -X => sub { 
138             $over = [qq($_[0]), $_[1]];
139             "-$_[1]"; 
140         };
141 }
142 {
143     package OverString;
144
145     # No fallback. -X should fall back to string overload even without
146     # it.
147     use overload q/""/ => sub { $over = 1; "TEST" };
148 }
149 {
150     package OverBoth;
151
152     use overload
153         q/""/   => sub { "TEST" },
154         -X      => sub { "-$_[1]" };
155 }
156 {
157     package OverNeither;
158
159     # Need fallback. Previous versions of perl required 'fallback' to do
160     # -X operations on an object with no "" overload.
161     use overload 
162         '+' => sub { 1 },
163         fallback => 1;
164 }
165
166 my $ft = bless [], "OverFtest";
167 my $ftstr = qq($ft);
168 my $str = bless [], "OverString";
169 my $both = bless [], "OverBoth";
170 my $neither = bless [], "OverNeither";
171 my $nstr = qq($neither);
172
173 open my $gv, "<", "TEST";
174 bless $gv, "OverString";
175 open my $io, "<", "TEST";
176 $io = *{$io}{IO};
177 bless $io, "OverString";
178
179 my $fcntl_not_available;
180 eval { require Fcntl } or $fcntl_not_available = 1;
181
182 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
183     $over = [];
184     ok( my $rv = eval "-$op \$ft",  "overloaded -$op succeeds" )
185         or diag( $@ );
186     is( $over->[0], $ftstr,         "correct object for overloaded -$op" );
187     is( $over->[1], $op,            "correct op for overloaded -$op" );
188     is( $rv,        "-$op",         "correct return value for overloaded -$op");
189
190     my ($exp, $is) = (1, "is");
191     if (
192         !$fcntl_not_available and (
193         $op eq "u" and not eval { Fcntl::S_ISUID() } or
194         $op eq "g" and not eval { Fcntl::S_ISGID() } or
195         $op eq "k" and not eval { Fcntl::S_ISVTX() }
196         )
197     ) {
198         ($exp, $is) = (0, "not");
199     }
200
201     $over = 0;
202     $rv = eval "-$op \$str";
203     ok( !$@,                        "-$op succeeds with string overloading" )
204         or diag( $@ );
205     is( $rv, eval "-$op 'TEST'",    "correct -$op on string overload" );
206     is( $over,      $exp,           "string overload $is called for -$op" );
207
208     ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
209
210     $over = 0;
211     eval "-$op \$gv";
212     is( $over,      $exp,   "string overload $is called for -$op on GLOB" );
213
214     # IO refs always get string overload called. This might be a bug.
215     $op eq "t" || $op eq "T" || $op eq "B"
216         and ($exp, $is) = (1, "is");
217
218     $over = 0;
219     eval "-$op \$io";
220     is( $over,      $exp,   "string overload $is called for -$op on IO");
221
222     $rv = eval "-$op \$both";
223     is( $rv,        "-$op",         "correct -$op on string/-X overload" );
224
225     $rv = eval "-$op \$neither";
226     ok( !$@,                        "-$op succeeds with random overloading" )
227         or diag( $@ );
228     is( $rv, eval "-$op \$nstr",    "correct -$op with random overloading" );
229
230     is( eval "-r -$op \$ft", "-r",      "stacked overloaded -$op" );
231     is( eval "-$op -r \$ft", "-$op",    "overloaded stacked -$op" );
232 }
233
234 # -l stack corruption: this bug occurred from 5.8 to 5.14
235 {
236  push my @foo, "bar", -l baz;
237  is $foo[0], "bar", '-l bareword does not corrupt the stack';
238 }
239
240 # File test ops should not call get-magic on the topmost SV on the stack if
241 # it belongs to another op.
242 {
243   my $w;
244   sub oon::TIESCALAR{bless[],'oon'}
245   sub oon::FETCH{$w++}
246   tie my $t, 'oon';
247   push my @a, $t, -t;
248   is $w, 1, 'file test does not call FETCH on stack item not its own';
249 }