1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#!/usr/bin/perl -w
# Copyright (c) 2018, cPanel, LLC.
# All rights reserved.
# http://cpanel.net
#
# This is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself. See L<perlartistic>.
use strict;
use warnings;
use Test2::Bundle::Extended;
use Test2::Tools::Explain;
#use Test2::Plugin::NoWarnings;
use File::Temp qw/ tempdir /;
use Overload::FileCheck q{:all};
use Carp;
{
my $save_lstats = {};
my $save_stats = {};
my $save_checks = {};
my $fixtures;
eval { $fixtures = create_fixtures(); 1 } or skip_all "Fail to create fixtures files: $@";
my @SKIP = (
'T-/dev/tty1', # .
'B-/dev/tty1', # .
'T-./my-sample.socket', # .
'B-./my-sample.socket', # .
'T-/dev/random', # .
'B-/dev/random', # .
'c-/dev/random', # .
);
push @SKIP, 'k-/tmp' if $^O =~ qr{darwin}i;
my %forbidden = map { $_ => 1 } @SKIP;
foreach my $f (@$fixtures) {
# use lstat otherwise we will read the target for symlink
my @lstat = lstat($f)
or next;
my @stat = stat($f)
or next;
$save_lstats->{$f} = [ @lstat ];
$save_stats->{$f} = [ @stat ]; # we need both...
$save_checks->{$f} = {};
# let keys add some randomness
foreach my $check ( keys %{ Overload::FileCheck::_get_filecheck_ops_map() } ) {
next if $forbidden{"$check-$f"};
#note "Unmocked -$check '$f'";
local $SIG{ALRM} = sub { die "Alarm from Unmocked -$check '$f'" };
alarm(60); # just avoid to run forever, should be large enough for slow systems...
if ( $check =~ qr{stat} ) {
$save_checks->{$f}->{$check} = eval qq{ [ $check('$f') ] };
}
else {
$save_checks->{$f}->{$check} = eval qq{scalar -$check '$f'};
}
alarm(0);
}
}
ok mock_all_from_stat( \&mock_stat_from_sys ), "mock_again";
my $last_cache_for;
sub mock_stat_from_sys {
my ( $stat_or_lstat, $f ) = @_;
# we are adding a FAKE/ prefix to be sure we are not
# using the system this time but our fake FileSystem...
return FALLBACK_TO_REAL_OP() unless $f =~ s{^FAKE/}{};
my $cache = $stat_or_lstat eq 'stat' ? $save_stats : $save_lstats;
if ( defined $cache->{$f} ) {
#note "Returning Cached $stat_or_lstat for $f"; #, Carp::longmess();
$last_cache_for = $f;
return $cache->{$f};
}
return FALLBACK_TO_REAL_OP();
}
# Testing the mock stat
ok -e $fixtures->[0], "-e $fixtures->[0]";
is $last_cache_for, undef, "fallback... when not using FAKE/ prefix" or die;
ok -e "FAKE/" . $fixtures->[0], "-e FAKE/$fixtures->[0]" or die;
is $last_cache_for, $fixtures->[0], "last_cache_for set when using FAKE/ prefix";
# ok ! -X 'FAKE/./link-to-textfile', "-X ./link-to-textfile";
# ok -X 'FAKE//bin/true', "-X /bin/true";
# done_testing; exit;
#note explain $save_checks;
my ( $last_file, $last_check );
my %todo = map { $_ => 1 } qw{ C-/bin M-/bin };
my $all_clear;
foreach my $f (@$fixtures) {
$last_file = $f;
# let keys add some randomness
foreach my $check ( sort keys %{ Overload::FileCheck::_get_filecheck_ops_map() } ) {
next if $check =~ qr{stat}; # TODO also check mocked stat maybe first
next if $forbidden{"$check-$f"};
my $expect = $save_checks->{$f}->{$check}
or next;
$last_check = "-$check $f";
note "Checking Mocked: -$check '$f' ";
my $got = eval qq{scalar -$check 'FAKE/$f'};
if ( $todo{"$check-$f"} || $check =~ qr{^[BT]$} ) {
# -B and -T are using heuristic guess and need to open the file...
todo "-$check '$f' known limitation (using heuristic guess)" => sub {
is $got, $expect, "-$check '$f'";
};
next;
}
if ( !defined $expect && defined $got && ( $got eq '' || $got eq '0' ) ) {
todo "-$check '$f' returns '' instead of undef..." => sub {
is $got, $expect, "-$check '$f'";
};
next;
}
if ( !defined $got && defined $expect && $expect eq '' ) {
todo "-$check '$f' returns undef instead of ''..." => sub {
is $got, $expect, "-$check '$f'";
};
next;
}
if ( $check =~ qr{^[AC]$} && defined $expect ) {
# Script X time minus file modification time, in days.
# add a small tolerance
# A is for 'access time'
if ( !defined $got ) {
todo "got undef..." => sub {
is $got, $expect, "-$check '$f'";
};
next;
}
if ( !( ( $expect - $got ) < 0.1 ) ) {
todo "-A tolerance not enough..." => sub {
is $got, $expect, "-$check '$f'";
};
next;
}
ok( ( $expect - $got ) < 0.1, "small tolerance for -A '$f': $got vs $expect" )
or diag "-A access time; got: ", $got, " expect ", $expect;
next;
}
is $got, $expect, "-$check '$f'" or goto DEBUG;
}
#last;
}
$all_clear = 1;
DEBUG: if ( !$all_clear ) {
diag "Last check was ", $last_check, " ; lstat: ", explain $save_lstats->{$last_file};
die "The previous test failed...";
}
}
done_testing;
exit;
my $TMP;
sub create_fixtures {
$TMP = tempdir( CLEANUP => 1 );
chdir $TMP or die;
mkdir("dir1") or die;
mkdir( "dir2", 0600 ) or die;
if ( open( my $fh, '>', "dir2/file" ) ) {
print {$fh} "some content\n";
close $fh;
}
if ( open( my $fh, '>', "empty-file" ) ) {
close $fh;
}
if ( open( my $fh, '>', "text-file" ) ) {
print {$fh} "this is a text file\n" x 10;
close $fh;
}
symlink( "dir1", "link-to-dir1" ) or die;
symlink( "dir2", "link-to-dir2" ) or die;
symlink( "text-file", "link-to-textfile" ) or die;
symlink( "empty-file", "link-to-emptyfile" ) or die;
symlink( "not-there", "link-to-void" ) or die;
# create a socket ?
qx{mkfifo my-sample.socket};
# auto populate fixtures
my @fixtures = qx[ find . ]; # improve use File::Find there
die "find fails" if $?;
chomp @fixtures;
die "no fixtures..." unless scalar @fixtures;
push( @fixtures, "missing-file", "missing-dir/missing-file" );
# try to add /bin/true and /bin/false
my @extra = qw{
/bin/true
/bin/false
/dev/random
/dev/tty1
/dev/vda1
/dev/sda1
/
/home
/usr
/usr/local
/tmp
};
foreach my $f (@extra) {
push @fixtures, $f if -e $f;
}
# my @ls = qx[ls -l];
# chomp @ls;
# note explain [ @ls ];
note explain \@fixtures;
return \@fixtures;
}
|