Commit | Line | Data |
42e55ab1 |
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'; |
20822f61 |
8 | @INC = '../lib'; |
fbb0b3b3 |
9 | require './test.pl'; |
42e55ab1 |
10 | } |
11 | |
eb57363f |
12 | use Config; |
d0c91b6a |
13 | plan(tests => 28 + 27*14); |
42e55ab1 |
14 | |
fbb0b3b3 |
15 | ok( -d 'op' ); |
16 | ok( -f 'TEST' ); |
17 | ok( !-f 'op' ); |
18 | ok( !-d 'TEST' ); |
19 | ok( -r 'TEST' ); |
42e55ab1 |
20 | |
22c35a8c |
21 | # make sure TEST is r-x |
ff2be7ed |
22 | eval { chmod 0555, 'TEST' or die "chmod 0555, 'TEST' failed: $!" }; |
23 | chomp ($bad_chmod = $@); |
846f25a3 |
24 | |
25 | $oldeuid = $>; # root can read and write anything |
26 | eval '$> = 1'; # so switch uid (may not be implemented) |
27 | |
28 | print "# oldeuid = $oldeuid, euid = $>\n"; |
29 | |
fbb0b3b3 |
30 | SKIP: { |
31 | if (!$Config{d_seteuid}) { |
32 | skip('no seteuid'); |
33 | } |
34 | elsif ($Config{config_args} =~/Dmksymlinks/) { |
35 | skip('we cannot chmod symlinks'); |
36 | } |
37 | elsif ($bad_chmod) { |
ff2be7ed |
38 | skip( $bad_chmod ); |
fbb0b3b3 |
39 | } |
40 | else { |
41 | ok( !-w 'TEST' ); |
42 | } |
22c35a8c |
43 | } |
42e55ab1 |
44 | |
846f25a3 |
45 | # Scripts are not -x everywhere so cannot test that. |
42e55ab1 |
46 | |
fd1e013e |
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...) |
fbb0b3b3 |
51 | ok( -r 'op' ); |
42e55ab1 |
52 | |
846f25a3 |
53 | # this would fail for the euid 1 |
54 | # (unless we have unpacked the source code as uid 1...) |
fbb0b3b3 |
55 | SKIP: { |
56 | if ($Config{d_seteuid}) { |
57 | ok( -w 'op' ); |
58 | } else { |
59 | skip('no seteuid'); |
60 | } |
3eeba6fb |
61 | } |
42e55ab1 |
62 | |
fbb0b3b3 |
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 |
42e55ab1 |
68 | |
fbb0b3b3 |
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 ); |
7294df96 |
81 | |
e7d3eb55 |
82 | # now with an empty file |
1ab9acc5 |
83 | my $tempfile = tempfile(); |
84 | open my $fh, ">", $tempfile; |
e7d3eb55 |
85 | close $fh; |
1ab9acc5 |
86 | ok( -f $tempfile ); |
87 | is( -s $tempfile, 0 ); |
88 | is( -f -s $tempfile, 0 ); |
89 | is( -s -f $tempfile, 0 ); |
90 | unlink $tempfile; |
e7d3eb55 |
91 | |
7294df96 |
92 | # test that _ is a bareword after filetest operators |
93 | |
94 | -f 'TEST'; |
95 | ok( -f _ ); |
96 | sub _ { "this is not a file name" } |
97 | ok( -f _ ); |
d89f1457 |
98 | |
99 | my $over; |
100 | { |
101 | package OverFtest; |
102 | |
4d57d24f |
103 | use overload |
c1d4704a |
104 | fallback => 1, |
4d57d24f |
105 | -X => sub { |
c1d4704a |
106 | $over = [qq($_[0]), $_[1]]; |
4d57d24f |
107 | "-$_[1]"; |
108 | }; |
d89f1457 |
109 | } |
f6aa8023 |
110 | { |
111 | package OverString; |
112 | |
113 | # No fallback. -X should fall back to string overload even without |
114 | # it. |
115 | use overload q/""/ => sub { $over = 1; "TEST" }; |
116 | } |
117 | { |
118 | package OverBoth; |
119 | |
120 | use overload |
121 | q/""/ => sub { "TEST" }, |
122 | -X => sub { "-$_[1]" }; |
123 | } |
124 | { |
125 | package OverNeither; |
126 | |
4d57d24f |
127 | # Need fallback. Previous versions of perl required 'fallback' to do |
f6aa8023 |
128 | # -X operations on an object with no "" overload. |
129 | use overload |
130 | '+' => sub { 1 }, |
131 | fallback => 1; |
132 | } |
133 | |
134 | my $ft = bless [], "OverFtest"; |
c1d4704a |
135 | my $ftstr = qq($ft); |
f6aa8023 |
136 | my $str = bless [], "OverString"; |
137 | my $both = bless [], "OverBoth"; |
138 | my $neither = bless [], "OverNeither"; |
c1d4704a |
139 | my $nstr = qq($neither); |
d89f1457 |
140 | |
d0c91b6a |
141 | open my $gv, "<", "TEST"; |
142 | bless $gv, "OverString"; |
143 | open my $io, "<", "TEST"; |
144 | $io = *{$io}{IO}; |
145 | bless $io, "OverString"; |
146 | |
822146ea |
147 | my $fcntl_not_available; |
148 | eval { require Fcntl } or $fcntl_not_available = 1; |
fa7f7498 |
149 | |
d89f1457 |
150 | for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") { |
d3ebc3eb |
151 | $over = []; |
f6aa8023 |
152 | ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" ) |
153 | or diag( $@ ); |
154 | is( $over->[0], $ftstr, "correct object for overloaded -$op" ); |
d89f1457 |
155 | is( $over->[1], $op, "correct op for overloaded -$op" ); |
156 | is( $rv, "-$op", "correct return value for overloaded -$op"); |
f6aa8023 |
157 | |
fa7f7498 |
158 | my ($exp, $is) = (1, "is"); |
159 | if ( |
822146ea |
160 | !$fcntl_not_available and ( |
fa7f7498 |
161 | $op eq "u" and not eval { Fcntl::S_ISUID() } or |
162 | $op eq "g" and not eval { Fcntl::S_ISGID() } or |
163 | $op eq "k" and not eval { Fcntl::S_ISVTX() } |
822146ea |
164 | ) |
fa7f7498 |
165 | ) { |
166 | ($exp, $is) = (0, "not"); |
167 | } |
168 | |
f6aa8023 |
169 | $over = 0; |
170 | $rv = eval "-$op \$str"; |
171 | ok( !$@, "-$op succeeds with string overloading" ) |
172 | or diag( $@ ); |
173 | is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" ); |
fa7f7498 |
174 | is( $over, $exp, "string overload $is called for -$op" ); |
f6aa8023 |
175 | |
fa7f7498 |
176 | ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not"); |
d0c91b6a |
177 | |
178 | $over = 0; |
179 | eval "-$op \$gv"; |
180 | is( $over, $exp, "string overload $is called for -$op on GLOB" ); |
181 | |
52f7f5ab |
182 | # IO refs always get string overload called. This might be a bug. |
183 | $op eq "t" || $op eq "T" || $op eq "B" |
184 | and ($exp, $is) = (1, "is"); |
185 | |
d0c91b6a |
186 | $over = 0; |
187 | eval "-$op \$io"; |
188 | is( $over, $exp, "string overload $is called for -$op on IO"); |
189 | |
f6aa8023 |
190 | $rv = eval "-$op \$both"; |
191 | is( $rv, "-$op", "correct -$op on string/-X overload" ); |
192 | |
193 | $rv = eval "-$op \$neither"; |
194 | ok( !$@, "-$op succeeds with random overloading" ) |
195 | or diag( $@ ); |
196 | is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" ); |
f6aa8023 |
197 | |
4d57d24f |
198 | is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" ); |
199 | is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" ); |
200 | } |