blib/lib/Overload/FileCheck.pm

filter: Statementstmt 97.4% 232 / 238 Branchbran 88.8% 128 / 144 Conditioncond 73.1% 30 / 41 MC/DCmcdc 57.6% 15 / 26 Subroutinesub 100% 32 / 32 Podpod 92.3% 12 / 13 totaltotal 90.8% 449 / 494 CC 94 SCAR 30.4
CC
94
Cov
92%
_check_from_stat
44.5
_check
20.8
_stat_for
9.2
CRAP 20.9
↑ Summary FileCheck.xs »
1 # Copyright (c) 2018, cPanel, LLC.
2 # All rights reserved.
3 # http://cpanel.net
4 #
5 # This is free software; you can redistribute it and/or modify it under the
6 # same terms as Perl itself. See L<perlartistic>.
7  
8 package Overload::FileCheck;
9  
10 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: override/mock perl file checks ops
14  
15 47 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 47 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 BEGIN {
20  
21 47 ▶ our $VERSION = '0.004'; # VERSION: generated by DZP::OurPkgVersion
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 47 require XSLoader;
24 47 XSLoader::load(__PACKAGE__);
25 }
26  
27 use Fcntl (
28 47 '_S_IFMT', # bit mask for the file type bit field
29 #'S_IFPERMS', # bit mask for file perms.
30 'S_IFSOCK', # socket
31 'S_IFLNK', # symbolic link
32 'S_IFREG', # regular file
33 'S_IFBLK', # block device
34 'S_IFDIR', # directory
35 'S_IFCHR', # character device
36 'S_IFIFO', # FIFO
37  
38 # qw{S_IRUSR S_IWUSR S_IXUSR S_IRWXU}
39 47 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 47 my @STAT_T_IX = qw{
42 ST_DEV
43 ST_INO
44 ST_MODE
45 ST_NLINK
46 ST_UID
47 ST_GID
48 ST_RDEV
49 ST_SIZE
50 ST_ATIME
51 ST_MTIME
52 ST_CTIME
53 ST_BLKSIZE
54 ST_BLOCKS
55 };
56  
57 47 my @CHECK_STATUS = qw{CHECK_IS_FALSE CHECK_IS_TRUE FALLBACK_TO_REAL_OP};
58  
59 47 my @STAT_HELPERS = qw{ stat_as_directory stat_as_file stat_as_symlink
60 stat_as_socket stat_as_chr stat_as_block};
61  
62 47 our @EXPORT_OK = (
63 qw{
64 mock_all_from_stat
65 mock_all_file_checks mock_file_check mock_stat
66 unmock_file_check unmock_all_file_checks unmock_stat
67 },
68 @CHECK_STATUS,
69 @STAT_T_IX,
70 @STAT_HELPERS,
71 );
72  
73 47 our %EXPORT_TAGS = (
74 all => [@EXPORT_OK],
75  
76 # status code
77 check => [@CHECK_STATUS],
78  
79 # STAT array indexes
80 stat => [ @STAT_T_IX, @STAT_HELPERS ],
81 );
82  
83 # hash for every filecheck we can mock
84 # and their corresonding OP_TYPE
85 47 my %MAP_FC_OP = (
86 'R' => OP_FTRREAD,
87 'W' => OP_FTRWRITE,
88 'X' => OP_FTREXEC,
89 'r' => OP_FTEREAD,
90 'w' => OP_FTEWRITE,
91 'x' => OP_FTEEXEC,
92  
93 'e' => OP_FTIS,
94 's' => OP_FTSIZE, # OP_CAN_RETURN_INT
95 'M' => OP_FTMTIME, # OP_CAN_RETURN_INT
96 'C' => OP_FTCTIME, # OP_CAN_RETURN_INT
97 'A' => OP_FTATIME, # OP_CAN_RETURN_INT
98  
99 'O' => OP_FTROWNED,
100 'o' => OP_FTEOWNED,
101 'z' => OP_FTZERO,
102 'S' => OP_FTSOCK,
103 'c' => OP_FTCHR,
104 'b' => OP_FTBLK,
105 'f' => OP_FTFILE,
106 'd' => OP_FTDIR,
107 'p' => OP_FTPIPE,
108 'u' => OP_FTSUID,
109 'g' => OP_FTSGID,
110 'k' => OP_FTSVTX,
111  
112 'l' => OP_FTLINK,
113  
114 't' => OP_FTTTY,
115  
116 'T' => OP_FTTEXT,
117 'B' => OP_FTBINARY,
118  
119 # special cases for stat & lstat
120 'stat' => OP_STAT,
121 'lstat' => OP_LSTAT,
122  
123 );
124  
125 47 my %MAP_STAT_T_IX = (
126 st_dev => ST_DEV,
127 st_ino => ST_INO,
128 st_mode => ST_MODE,
129 st_nlink => ST_NLINK,
130 st_uid => ST_UID,
131 st_gid => ST_GID,
132 st_rdev => ST_RDEV,
133 st_size => ST_SIZE,
134 st_atime => ST_ATIME,
135 st_mtime => ST_MTIME,
136 st_ctime => ST_CTIME,
137 st_blksize => ST_BLKSIZE,
138 st_blocks => ST_BLOCKS,
139 );
140  
141 # op_type_id => check
142 47 my %REVERSE_MAP;
143  
144 47 my %OP_CAN_RETURN_INT = map { $MAP_FC_OP{$_} => 1 } qw{ s M C A };
145 47 my %OP_IS_STAT_OR_LSTAT = map { $MAP_FC_OP{$_} => 1 } qw{ stat lstat };
146 #
147 # This is listing the default ERRNO codes
148 # used by each test when the test fails and
149 # the user did not provide one ERRNO error
150 #
151 47 my %DEFAULT_ERRNO = (
152 'default' => Errno::ENOENT, # default value for any other not listed
153 'x' => Errno::ENOEXEC,
154 'X' => Errno::ENOEXEC,
155  
156 # ...
157 );
158  
159 # this is saving our custom ops
160 # optype_id => sub
161 47 my $_current_mocks = {};
162  
163 sub import {
164 46 ▶ my ( $class, @args ) = @_;
Subroutine import: called CC=7 SCAR=19.5
165  
166 # mock on import...
167 46 my $_next_check;
168 my @for_exporter;
169 46 foreach my $check (@args) {
170 57 ▶ if ( !$_next_check && $check !~ qr{^-} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_next_check or $check =~ qr/^-/) 53 4 57
Condition100%3/3
$_next_check or $check =~ qr/^-/
l!l&&r!l&&!r
2253
MC/DC100%2/2
$_next_check or $check =~ qr/^-/
$_next_check $check =~ qr/^-/
Truth table100%3/3
A or B
A= $_next_checkB= $check =~ qr/^-/
ABresult
1X1
011
000
171  
172 # this is a valid arg for exporter
173 53 push @for_exporter, $check;
174 53 next;
175 }
176 4 ▶ if ( !$_next_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_next_check) { } 2 2 4
177  
178 # we found a key like '-e' in '-e => sub {} '
179 2 $_next_check = $check;
180 }
181 else {
182 # now this is the value
183 2 my $code = $check;
184  
185 # use Overload::FileCheck -from_stat => \&my_stat;
186 2 ▶ if ( $_next_check eq q{-from_stat} || $_next_check eq q{-from-stat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_next_check eq '-from_stat' or $_next_check eq '-from-stat') { } 1 1 2
Condition66%2/3
$_next_check eq "-from_stat" or $_next_check eq "-from-stat"
l!l&&r!l&&!r
101
MC/DC50%1/2
$_next_check eq "-from_stat" or $_next_check eq "-from-stat"
$_next_check eq "-from_stat" $_next_check eq "-from-stat"
Truth table66%2/3
A or B
A= $_next_check eq "-from_stat"B= $_next_check eq "-from-stat"
ABresult
1X1
011
000
187 1 mock_all_from_stat($code);
188 }
189 else {
190 1 mock_file_check( $_next_check, $code );
191 }
192  
193 2 undef $_next_check;
194 }
195 }
196  
197 # callback the exporter logic
198 46 return __PACKAGE__->export_to_level( 1, $class, @for_exporter );
199 }
200  
201 sub mock_all_file_checks {
202 7 ▶ my ($sub) = @_;
Subroutine mock_all_file_checks: called CC=3 SCAR=11.0
Pod undocumented
203  
204 7 foreach my $check ( sort keys %MAP_FC_OP ) {
205 203 ▶ next if $check =~ qr{^l?stat$}; # we should not mock stat
Branch: 1 branch
BranchTrueFalseTotal
if $check =~ qr/^l?stat$/ 14 189 203
206 mock_file_check(
207 $check,
208 sub {
209 851 ▶ my (@args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
210 851 return $sub->( $check, @args );
211 }
212 189 );
213 }
214  
215 7 return 1;
216 }
217  
218 sub mock_file_check {
219 218 ▶ my ( $check, $sub ) = @_;
Subroutine mock_file_check: called CC=5 SCAR=16.3
Pod documented
220  
221 218 ▶ die q[Check is not defined] unless defined $check;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check 0 218 218
222 218 ▶ die q[Second arg must be a CODE ref] unless ref $sub eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 218 218
223  
224 218 $check =~ s{^-+}{}; # strip any extra dashes
225 #return -1 unless defined $MAP_FC_OP{$check}; # we should not do that
226 218 ▶ die qq[Unknown check '$check'] unless defined $MAP_FC_OP{$check};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $MAP_FC_OP{$check} 0 218 218
227  
228 218 my $optype = $MAP_FC_OP{$check};
229 218 ▶ die qq[-$check is already mocked by Overload::FileCheck] if exists $_current_mocks->{$optype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_current_mocks->{$optype} 1 217 218
230  
231 217 $_current_mocks->{$optype} = $sub;
232  
233 217 _xs_mock_op($optype);
234  
235 217 return 1;
236 }
237  
238 sub unmock_file_check {
239 31 ▶ my (@checks) = @_;
Subroutine unmock_file_check: called CC=4 SCAR=14.0
Pod documented
240  
241 31 foreach my $check (@checks) {
242 115 ▶ die q[Check is not defined] unless defined $check;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check 0 115 115
243 115 $check =~ s{^-+}{}; # strip any extra dashes
244 115 ▶ die qq[Unknown check '$check'] unless defined $MAP_FC_OP{$check};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $MAP_FC_OP{$check} 0 115 115
245  
246 115 my $optype = $MAP_FC_OP{$check};
247  
248 115 delete $_current_mocks->{$optype};
249  
250 115 _xs_unmock_op($optype);
251 }
252  
253 31 return 1;
254 }
255  
256 sub mock_all_from_stat {
257 4 ▶ my ($sub_for_stat) = @_;
Subroutine mock_all_from_stat: called CC=1 SCAR=0.0
Pod documented
258  
259 # then mock all -X checks to our custom
260 mock_all_file_checks(
261 sub {
262 672 ▶ my ( $check, $f_or_fh ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
263  
264 # the main call
265 672 my $return = _check_from_stat( $check, $f_or_fh, $sub_for_stat );
266  
267 # auto remock the OP (it could have been temporary unmocked to use -X _)
268 672 _xs_mock_op( $MAP_FC_OP{$check} );
269  
270 672 return $return;
271 }
272 4 );
273  
274 # start by mocking 'stat' and 'lstat' call
275 4 mock_stat($sub_for_stat);
276  
277 4 return 1;
278 }
279  
280 sub _check_from_stat {
281 672 ▶ my ( $check, $f_or_fh, $sub_for_stat ) = @_;
Subroutine _check_from_stat: called CC=44 SCAR=37.9
282  
283 672 my $optype = $MAP_FC_OP{$check};
284  
285 # stat would need to be called twice
286 # 1/ we first need to check if we are mocking the file
287 # or if we let it fallback to the Perl OP
288 # 2/ doing a second stat call in order to cache _
289  
290 672 my $can_use_stat;
291 672 ▶ $can_use_stat = 1 if $check =~ qr{^[sfdMXxzACORWeorw]$};
Branch: 1 branch
BranchTrueFalseTotal
if $check =~ qr/^[sfdMXxzACORWeorw]$/ 427 245 672
292  
293 672 ▶ my $stat_or_lstat = $can_use_stat ? 'stat' : 'lstat';
Branch: 1 branch
BranchTrueFalseTotal
$can_use_stat ? : 427 245 672
294  
295 672 my (@mocked_lstat_result) = $sub_for_stat->( $stat_or_lstat, $f_or_fh );
296 672 ▶ if ( scalar @mocked_lstat_result == 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @mocked_lstat_result == 1 and not ref $mocked_lstat_result[0] and $mocked_lstat_result[0] == -1) 32 640 672
Condition66%4/6
scalar @mocked_lstat_result == 1 and not ref $mocked_lstat_result[0] and $mocked_lstat_result[0] == -1
!ll&&!rl&&r
640032
scalar @mocked_lstat_result == 1 and not ref $mocked_lstat_result[0]
!ll&&!rl&&r
064032
MC/DC33%1/3
scalar @mocked_lstat_result == 1 and not ref $mocked_lstat_result[0] and $mocked_lstat_result[0] == -1
scalar @mocked_lstat_result == 1 not ref $mocked_lstat_result[0] $mocked_lstat_result[0] == -1
Truth table50%2/4
A and not(B) and C
A= scalar @mocked_lstat_result == 1B= not ref $mocked_lstat_result[0]C= $mocked_lstat_result[0] == -1
ABCresult
0XX0
10X0
1100
1111
297 && !ref $mocked_lstat_result[0]
298 && $mocked_lstat_result[0] == FALLBACK_TO_REAL_OP ) {
299 32 return FALLBACK_TO_REAL_OP;
300 }
301  
302 # avoid a second callback to the user hook (do not really happen for now)
303 local $_current_mocks->{ $MAP_FC_OP{$stat_or_lstat} } = sub {
304 639 ▶ return @mocked_lstat_result;
Subroutine __ANON__: called CC=1 SCAR=0.0
305 640 };
306  
307 # now performing a real stat call [ using the mocked stat function ]
308 640 my ( @stat, @lstat );
309  
310 640 ▶ if ($can_use_stat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_use_stat) { } 399 241 640
311 47 ▶ no warnings; # throw warnings with Perl <= 5.14
Subroutine BEGIN: called CC=1 SCAR=0.0
312 399 ▶ @stat = stat($f_or_fh) if defined $f_or_fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $f_or_fh 398 1 399
313 }
314 else {
315 47 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
316 241 ▶ @lstat = lstat($f_or_fh) if defined $f_or_fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $f_or_fh 241 0 241
317 }
318  
319 640 ▶ if ( $check eq 'r' ) {
Branch: 27 branches
BranchTrueFalseTotal
if ($check eq 'r') { } 21 619 640
elsif ($check eq 'w') { } 21 598 619
elsif ($check eq 'x') { } 22 576 598
elsif ($check eq 'o') { } 21 555 576
elsif ($check eq 'R') { } 21 534 555
elsif ($check eq 'W') { } 21 513 534
elsif ($check eq 'X') { } 21 492 513
elsif ($check eq 'O') { } 21 471 492
elsif ($check eq 'e') { } 38 433 471
elsif ($check eq 'z') { } 24 409 433
elsif ($check eq 's') { } 24 385 409
elsif ($check eq 'f') { } 29 356 385
elsif ($check eq 'd') { } 52 304 356
elsif ($check eq 'l') { } 26 278 304
elsif ($check eq 'p') { } 21 257 278
elsif ($check eq 'S') { } 26 231 257
elsif ($check eq 'b') { } 23 208 231
elsif ($check eq 'c') { } 23 185 208
elsif ($check eq 't') { } 21 164 185
elsif ($check eq 'u') { } 21 143 164
elsif ($check eq 'g') { } 21 122 143
elsif ($check eq 'k') { } 21 101 122
elsif ($check eq 'T') { } 19 82 101
elsif ($check eq 'B') { } 19 63 82
elsif ($check eq 'M') { } 21 42 63
elsif ($check eq 'A') { } 21 21 42
elsif ($check eq 'C') { } 21 0 21
320  
321 # -r File is readable by effective uid/gid.
322 # return _cando(stat_mode, effective, &PL_statcache)
323 # return _cando( S_IRUSR, 1 )
324  
325 # ugly need a better way to do this...
326 21 _xs_unmock_op($optype);
327 21 return _to_bool( scalar -r _ );
328 }
329 elsif ( $check eq 'w' ) {
330  
331 # -w File is writable by effective uid/gid.
332 21 _xs_unmock_op($optype);
333 21 return _to_bool( scalar -w _ );
334 }
335 elsif ( $check eq 'x' ) {
336  
337 # -x File is executable by effective uid/gid.
338 22 _xs_unmock_op($optype);
339 22 return _to_bool( scalar -x _ );
340 }
341 elsif ( $check eq 'o' ) {
342  
343 # -o File is owned by effective uid.
344 21 _xs_unmock_op($optype);
345 21 return _to_bool( scalar -o _ );
346 }
347 elsif ( $check eq 'R' ) {
348  
349 # -R File is readable by real uid/gid.
350 21 _xs_unmock_op($optype);
351 21 return _to_bool( scalar -R _ );
352 }
353 elsif ( $check eq 'W' ) {
354  
355 # -W File is writable by real uid/gid.
356 21 _xs_unmock_op($optype);
357 21 return _to_bool( scalar -W _ );
358 }
359 elsif ( $check eq 'X' ) {
360  
361 # -X File is executable by real uid/gid.
362  
363 21 _xs_unmock_op($optype);
364 21 return _to_bool( scalar -X _ );
365 }
366 elsif ( $check eq 'O' ) {
367  
368 # -O File is owned by real uid.
369 21 _xs_unmock_op($optype);
370 21 return _to_bool( scalar -O _ );
371 }
372 elsif ( $check eq 'e' ) {
373  
374 # -e File exists.
375 # a file can only exists if MODE is set ?
376 38 ▶ return _to_bool( scalar @stat && $stat[ST_MODE] );
Condition100%3/3
scalar @stat && $stat[2]
!ll&&!rl&&r
4133
MC/DC100%2/2
scalar @stat && $stat[2]
scalar @stat $stat[2]
Truth table100%3/3
A && B
A= scalar @statB= $stat[2]
ABresult
0X0
100
111
377 }
378 elsif ( $check eq 'z' ) {
379  
380 # -z File has zero size (is empty).
381  
382 # TODO: can probably avoid the extra called...
383 # by checking it ourself
384  
385 24 _xs_unmock_op($optype);
386 24 return _to_bool( scalar -z _ );
387 }
388 elsif ( $check eq 's' ) {
389  
390 # -s File has nonzero size (returns size in bytes).
391  
392 # fallback does not work with symlinks
393 # do the check ourself, which also save a few calls
394  
395 24 return $stat[ST_SIZE];
396 }
397 elsif ( $check eq 'f' ) {
398  
399 # -f File is a plain file.
400 29 return _check_mode_type( $stat[ST_MODE], S_IFREG );
401 }
402 elsif ( $check eq 'd' ) {
403  
404 # -d File is a directory.
405  
406 52 return _check_mode_type( $stat[ST_MODE], S_IFDIR );
407 }
408 elsif ( $check eq 'l' ) {
409  
410 # -l File is a symbolic link (false if symlinks aren't
411 # supported by the file system).
412  
413 26 return _check_mode_type( $lstat[ST_MODE], S_IFLNK );
414 }
415 elsif ( $check eq 'p' ) {
416  
417 # -p File is a named pipe (FIFO), or Filehandle is a pipe.
418 21 return _check_mode_type( $lstat[ST_MODE], S_IFIFO );
419 }
420 elsif ( $check eq 'S' ) {
421  
422 # -S File is a socket.
423 26 return _check_mode_type( $lstat[ST_MODE], S_IFSOCK );
424 }
425 elsif ( $check eq 'b' ) {
426  
427 # -b File is a block special file.
428 23 return _check_mode_type( $lstat[ST_MODE], S_IFBLK );
429 }
430 elsif ( $check eq 'c' ) {
431  
432 # -c File is a character special file.
433 23 return _check_mode_type( $lstat[ST_MODE], S_IFCHR );
434 }
435 elsif ( $check eq 't' ) {
436  
437 # -t Filehandle is opened to a tty.
438 21 _xs_unmock_op($optype);
439 21 return _to_bool( scalar -t _ );
440 }
441 elsif ( $check eq 'u' ) {
442  
443 # -u File has setuid bit set.
444 21 _xs_unmock_op($optype);
445 21 return _to_bool( scalar -u _ );
446 }
447 elsif ( $check eq 'g' ) {
448  
449 # -g File has setgid bit set.
450 21 _xs_unmock_op($optype);
451 21 return _to_bool( scalar -g _ );
452 }
453 elsif ( $check eq 'k' ) {
454  
455 # -k File has sticky bit set.
456  
457 21 _xs_unmock_op($optype);
458 21 return _to_bool( scalar -k _ );
459 }
460 elsif ( $check eq 'T' ) { # heuristic guess.. throw a die?
461  
462 # -T File is an ASCII or UTF-8 text file (heuristic guess).
463  
464 #return CHECK_IS_FALSE if -d $f_or_fh;
465  
466 19 _xs_unmock_op($optype);
467 19 return _to_bool( scalar -T *_ );
468 }
469 elsif ( $check eq 'B' ) { # heuristic guess.. throw a die?
470  
471 # -B File is a "binary" file (opposite of -T).
472  
473 19 ▶ return CHECK_IS_TRUE if -d $f_or_fh;
Branch: 1 branch
BranchTrueFalseTotal
if -d $f_or_fh 10 9 19
474  
475 # ... we cannot really know...
476 # ... this is an heuristic guess...
477  
478 9 _xs_unmock_op($optype);
479 9 return _to_bool( scalar -B *_ );
480 }
481 elsif ( $check eq 'M' ) {
482  
483 # -M Script start time minus file modification time, in days.
484  
485 21 ▶ return CHECK_IS_NULL unless scalar @stat && defined $stat[ST_MTIME];
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @stat and defined $stat[9] 3 18 21
Condition66%2/3
scalar @stat and defined $stat[9]
!ll&&!rl&&r
3018
MC/DC50%1/2
scalar @stat and defined $stat[9]
scalar @stat defined $stat[9]
Truth table66%2/3
A and B
A= scalar @statB= defined $stat[9]
ABresult
0X0
100
111
486 18 return ( ( get_basetime() - $stat[ST_MTIME] ) / 86400.0 );
487  
488 #return int( scalar -M _ );
489 }
490 elsif ( $check eq 'A' ) {
491  
492 # -A Same for access time.
493 #
494 # ((NV)PL_basetime - PL_statcache.st_atime) / 86400.0
495 21 ▶ return CHECK_IS_NULL unless scalar @stat && defined $stat[ST_ATIME];
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @stat and defined $stat[8] 3 18 21
Condition66%2/3
scalar @stat and defined $stat[8]
!ll&&!rl&&r
3018
MC/DC50%1/2
scalar @stat and defined $stat[8]
scalar @stat defined $stat[8]
Truth table66%2/3
A and B
A= scalar @statB= defined $stat[8]
ABresult
0X0
100
111
496  
497 18 return ( ( get_basetime() - $stat[ST_ATIME] ) / 86400.0 );
498 }
499 elsif ( $check eq 'C' ) {
500  
501 # -C Same for inode change time (Unix, may differ for other
502 #_xs_unmock_op($optype);
503 #return scalar -C *_;
504 21 ▶ return CHECK_IS_NULL unless scalar @stat && defined $stat[ST_CTIME];
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @stat and defined $stat[10] 3 18 21
Condition66%2/3
scalar @stat and defined $stat[10]
!ll&&!rl&&r
3018
MC/DC50%1/2
scalar @stat and defined $stat[10]
scalar @stat defined $stat[10]
Truth table66%2/3
A and B
A= scalar @statB= defined $stat[10]
ABresult
0X0
100
111
505  
506 18 return ( ( get_basetime() - $stat[ST_CTIME] ) / 86400.0 );
507 }
508 else {
509 0 die "Unknown check $check.\n";
510 }
511  
512 0 die "FileCheck -$check is not implemented by Overload::FileCheck...";
513  
514 0 return FALLBACK_TO_REAL_OP;
515 }
516  
517 sub _to_bool {
518 523 ▶ my ($s) = @_;
Subroutine _to_bool: called CC=2 SCAR=6.9
519  
520 523 ▶ return ( $s ? CHECK_IS_TRUE : CHECK_IS_FALSE );
Branch: 1 branch
BranchTrueFalseTotal
$s ? : 189 334 523
521 }
522  
523 sub _check_mode_type {
524 200 ▶ my ( $mode, $type ) = @_;
Subroutine _check_mode_type: called CC=2 SCAR=6.9
525  
526 200 ▶ return CHECK_IS_FALSE unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 20 180 200
527 180 return _to_bool( ( $mode & _S_IFMT ) == $type );
528 }
529  
530 # this is a special case used to mock OP_STAT & OP_LSTAT
531 sub mock_stat {
532 7 ▶ my ($sub) = @_;
Subroutine mock_stat: called CC=4 SCAR=14.0
Pod documented
533  
534 7 ▶ die q[First arg must be a CODE ref] unless ref $sub eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 7 7
535  
536 7 foreach my $opname (qw{stat lstat}) {
537 14 my $optype = $MAP_FC_OP{$opname};
538 14 ▶ die qq[No optype found for $opname] unless $optype;
Branch: 1 branch
BranchTrueFalseTotal
unless $optype 0 14 14
539  
540 # plug the sub
541 $_current_mocks->{$optype} = sub {
542 37 ▶ my $file_or_handle = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
543 37 return $sub->( $opname, $file_or_handle );
544 14 };
545  
546 # setup the mock for the OP
547 14 _xs_mock_op($optype);
548 }
549  
550 7 return 1;
551 }
552  
553 # just an alias to unmock stat & lstat at the same time
554 sub unmock_stat {
555 1 ▶ return unmock_file_check(qw{stat lstat});
Subroutine unmock_stat: called CC=1 SCAR=0.0
Pod documented
556 }
557  
558 sub unmock_all_file_checks {
559  
560 9 ▶ if ( !scalar %REVERSE_MAP ) {
Subroutine unmock_all_file_checks: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %REVERSE_MAP) 8 1 9
561 8 foreach my $k ( keys %MAP_FC_OP ) {
562 232 $REVERSE_MAP{ $MAP_FC_OP{$k} } = $k;
563 }
564 }
565  
566 9 my @mocks = sort map { $REVERSE_MAP{$_} } keys %$_current_mocks;
567 9 ▶ return unless scalar @mocks;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @mocks 0 9 9
568  
569 9 return unmock_file_check(@mocks);
570 }
571  
572 # should not be called directly
573 # this is called from XS to check if one OP is mocked
574 # and trigger the callback function when mocked
575 sub _check {
576 1879 ▶ my ( $optype, $file, @others ) = @_;
Subroutine _check: called CC=20 SCAR=30.4
577  
578 1879 ▶ die if scalar @others; # need to move this in a unit test
Branch: 1 branch
BranchTrueFalseTotal
if scalar @others 0 1879 1879
579  
580 # we have no custom mock at this point
581 1879 ▶ return FALLBACK_TO_REAL_OP unless defined $_current_mocks->{$optype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_current_mocks->{$optype} 0 1879 1879
582  
583 1879 my ( $out, @extra ) = $_current_mocks->{$optype}->($file);
584  
585 # FIXME return undef when not defined out
586  
587 1879 ▶ if ( defined $out && $OP_CAN_RETURN_INT{$optype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out and $OP_CAN_RETURN_INT{$optype}) 144 1735 1879
Condition100%3/3
defined $out and $OP_CAN_RETURN_INT{$optype}
!ll&&!rl&&r
121723144
MC/DC100%2/2
defined $out and $OP_CAN_RETURN_INT{$optype}
defined $out $OP_CAN_RETURN_INT{$optype}
Truth table100%3/3
A and B
A= defined $outB= $OP_CAN_RETURN_INT{$optype}
ABresult
0X0
100
111
588 144 return $out; # limitation to int for now in fact some returns NVs
589 }
590  
591 1735 ▶ if ( !$out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 528 1207 1735
592  
593 # check if the user provided a custom ERRNO error otherwise
594 # set one for him, so a test could never fail without having
595 # ERRNO set
596 528 ▶ if ( !int($!) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (int $!) 21 507 528
597 21 ▶ $! = $DEFAULT_ERRNO{ $REVERSE_MAP{$optype} || 'default' } || $DEFAULT_ERRNO{'default'};
Condition40%2/5
$DEFAULT_ERRNO{$REVERSE_MAP{$optype} or 'default'} || $DEFAULT_ERRNO{'default'}
l!l&&r!l&&!r
2100
$REVERSE_MAP{$optype} || 'default'
l!l
021
MC/DC0%0/2
$DEFAULT_ERRNO{$REVERSE_MAP{$optype} or 'default'} || $DEFAULT_ERRNO{'default'}
$DEFAULT_ERRNO{$REVERSE_MAP{$optype} or 'default'} $DEFAULT_ERRNO{'default'}
MC/DC0%0/1
$REVERSE_MAP{$optype} || 'default'
$REVERSE_MAP{$optype}
Truth table33%1/3
A || B
A= $DEFAULT_ERRNO{$REVERSE_MAP{$optype} or 'default'}B= $DEFAULT_ERRNO{'default'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $REVERSE_MAP{$optype}
Aresult
11
00
598 }
599  
600 #return undef unless defined $out;
601 528 return CHECK_IS_FALSE;
602 }
603  
604 1207 ▶ return FALLBACK_TO_REAL_OP if !ref $out && $out == FALLBACK_TO_REAL_OP;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $out and $out == -1 167 1040 1207
Condition100%3/3
not ref $out and $out == -1
!ll&&!rl&&r
671369167
MC/DC100%2/2
not ref $out and $out == -1
not ref $out $out == -1
Truth table100%3/3
not(A) and B
A= not ref $outB= $out == -1
ABresult
0X0
100
111
605  
606 # stat and lstat OP are returning a stat ARRAY in addition to the status code
607 1040 ▶ if ( $OP_IS_STAT_OR_LSTAT{$optype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OP_IS_STAT_OR_LSTAT{$optype}) 673 367 1040
608  
609 # .......... Stat_t
610 # dev_t st_dev Device ID of device containing file.
611 # ino_t st_ino File serial number.
612 # mode_t st_mode Mode of file (see below).
613 # nlink_t st_nlink Number of hard links to the file.
614 # uid_t st_uid User ID of file.
615 # gid_t st_gid Group ID of file.
616 # dev_t st_rdev Device ID (if file is character or block special).
617 # off_t st_size For regular files, the file size in bytes.
618 # time_t st_atime Time of last access.
619 # time_t st_mtime Time of last data modification.
620 # time_t st_ctime Time of last status change.
621 # blksize_t st_blksize A file system-specific preferred I/O block size for
622 # blkcnt_t st_blocks Number of blocks allocated for this object.
623 # ......
624  
625 673 ▶ my $stat = $out // $others[0]; # can be array or hash at this point
Condition33%1/3
$out // $others[0]
l!l&&r!l&&!r
67300
MC/DC0%0/2
$out // $others[0]
$out $others[0]
Truth table33%1/3
A // B
A= $outB= $others[0]
ABresult
1X1
011
000
626 673 my $stat_is_a = ref $stat;
627 673 ▶ die q[Your mocked function for stat should return a stat array or hash] unless $stat_is_a;
Branch: 1 branch
BranchTrueFalseTotal
unless $stat_is_a 2 671 673
628  
629 671 my $stat_as_arrayref;
630  
631 # can handle one ARRAY or a HASH
632 671 my $stat_t_max = STAT_T_MAX;
633 671 ▶ if ( $stat_is_a eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stat_is_a eq 'ARRAY') { } 664 7 671
elsif ($stat_is_a eq 'HASH') { } 7 0 7
634 664 $stat_as_arrayref = $stat;
635 664 my $av_size = scalar @$stat;
636 664 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($av_size and $av_size != $stat_t_max) 4 660 664
Condition100%3/3
$av_size and $av_size != $stat_t_max
!ll&&!rl&&r
755854
MC/DC100%2/2
$av_size and $av_size != $stat_t_max
$av_size $av_size != $stat_t_max
Truth table100%3/3
A and B
A= $av_sizeB= $av_size != $stat_t_max
ABresult
0X0
100
111
637 $av_size # 0 is valid when the file is missing
638 && $av_size != $stat_t_max
639 ) {
640 4 die qq[Stat array should contain exactly 0 or $stat_t_max values];
641 }
642 }
643 elsif ( $stat_is_a eq 'HASH' ) {
644 7 $stat_as_arrayref = [ (0) x $stat_t_max ]; # start with an empty array
645 7 foreach my $k ( keys %$stat ) {
646 22 my $ix = $MAP_STAT_T_IX{ lc($k) };
647 22 ▶ die qq[Unknown index for stat_t struct key $k] unless defined $ix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ix 2 20 22
648 20 $stat_as_arrayref->[$ix] = $stat->{$k};
649 }
650 }
651 else {
652 0 die q[Your mocked function for stat should return a stat array or hash];
653 }
654  
655 665 return ( CHECK_IS_TRUE, $stat_as_arrayref );
656 }
657  
658 367 return CHECK_IS_TRUE;
659 }
660  
661 # accessors for testing purpose mainly
662 sub _get_filecheck_ops_map {
663 43 ▶ return {%MAP_FC_OP}; # return a copy
Subroutine _get_filecheck_ops_map: called CC=1 SCAR=0.0
664 }
665  
666 ######################################################
667 ### stat helpers
668 ######################################################
669  
670 sub stat_as_directory {
671 2 ▶ my (%opts) = @_;
Subroutine stat_as_directory: called CC=1 SCAR=0.0
Pod documented
672  
673 2 return _stat_for( S_IFDIR, \%opts );
674 }
675  
676 sub stat_as_file {
677 12 ▶ my (%opts) = @_;
Subroutine stat_as_file: called CC=1 SCAR=0.0
Pod documented
678  
679 12 return _stat_for( S_IFREG, \%opts );
680 }
681  
682 sub stat_as_symlink {
683 2 ▶ my (%opts) = @_;
Subroutine stat_as_symlink: called CC=1 SCAR=0.0
Pod documented
684  
685 2 return _stat_for( S_IFLNK, \%opts );
686 }
687  
688 sub stat_as_socket {
689 2 ▶ my (%opts) = @_;
Subroutine stat_as_socket: called CC=1 SCAR=0.0
Pod documented
690  
691 2 return _stat_for( S_IFSOCK, \%opts );
692 }
693  
694 sub stat_as_chr {
695 1 ▶ my (%opts) = @_;
Subroutine stat_as_chr: called CC=1 SCAR=0.0
Pod documented
696  
697 1 return _stat_for( S_IFCHR, \%opts );
698 }
699  
700 sub stat_as_block {
701 1 ▶ my (%opts) = @_;
Subroutine stat_as_block: called CC=1 SCAR=0.0
Pod documented
702  
703 1 return _stat_for( S_IFBLK, \%opts );
704 }
705  
706 sub _stat_for {
707 20 ▶ my ( $type, $opts ) = @_;
Subroutine _stat_for: called CC=9 SCAR=22.2
708  
709 20 my @stat = ( (0) x 13 ); # STAT_T_MAX
710  
711 # set file type
712 20 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 20 0 20
713  
714 # _S_IFMT is used as a protection to do not flip outside the mask
715 20 $stat[ST_MODE] |= ( $type & _S_IFMT );
716 }
717  
718 # set permission using octal
719 20 ▶ if ( defined $opts->{perms} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"perms"}) 2 18 20
720  
721 # _S_IFMT is used as a protection to do not flip outside the mask
722 2 $stat[ST_MODE] |= ( $opts->{perms} & ~_S_IFMT );
723 }
724  
725 # deal with UID / GID
726 20 ▶ if ( defined $opts->{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"uid"}) 1 19 20
727 1 ▶ if ( $opts->{uid} =~ qr{^[0-9]+$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'uid'} =~ qr/^[0-9]+$/) { } 1 0 1
728 1 $stat[ST_UID] = $opts->{uid};
729 }
730 else {
731  
732 0 $stat[ST_UID] = getpwnam( $opts->{uid} );
733 }
734 }
735  
736 20 ▶ if ( defined $opts->{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"gid"}) 1 19 20
737 1 ▶ if ( $opts->{gid} =~ qr{^[0-9]+$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'gid'} =~ qr/^[0-9]+$/) { } 1 0 1
738 1 $stat[ST_GID] = $opts->{gid};
739 }
740 else {
741 0 $stat[ST_GID] = getgrnam( $opts->{gid} );
742 }
743 }
744  
745 # options that we can simply copy to a slot
746 20 my %name2ix = (
747 size => ST_SIZE,
748 atime => ST_ATIME,
749 mtime => ST_MTIME,
750 ctime => ST_CTIME,
751 blksize => ST_BLKSIZE,
752 blocks => ST_BLOCKS,
753 );
754  
755 20 foreach my $k ( keys %$opts ) {
756 15 $k = lc($k);
757 15 $k =~ s{^st_}{};
758 15 ▶ next unless defined $name2ix{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name2ix{$k} 4 11 15
759  
760 11 $stat[ $name2ix{$k} ] = $opts->{$k};
761 }
762  
763 20 return \@stat;
764 }
765  
766 47 1;
767  
768 __END__
↑ Summary FileCheck.xs »