| 1 | package Csistck::Test::FileBase; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use base 'Csistck::Test'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use Csistck::Oper qw/debug/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use Csistck::Util qw/backup_file hash_file hash_string/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 17 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 17 | ▶ | use File::stat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 17 | ▶ | use Sys::Hostname::Long qw//; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub desc { sprintf("File check on %s", shift->{target}); } | ||||||||||||||||||||||||||
|
Subroutine
desc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 47 | ▶ | sub dest { shift->{target}; } | ||||||||||||||||||||||||||
|
Subroutine
dest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | sub src { shift->{src}; } | ||||||||||||||||||||||||||
|
Subroutine
src: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 42 | ▶ | sub mode { shift->{mode}; } | ||||||||||||||||||||||||||
|
Subroutine
mode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | sub uid { shift->{uid}; } | ||||||||||||||||||||||||||
|
Subroutine
uid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | sub gid { shift->{gid}; } | ||||||||||||||||||||||||||
|
Subroutine
gid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub check { | ||||||||||||||||||||||||||||
| 26 | 11 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 11 | my $ret = 1; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | ▶ | die("Destination path not found") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if (! -e $self->dest); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # If we defined a source file | ||||||||||||||||||||||||||||
| 33 | 11 | ▶ | if (defined($self->src) and $self->can('file_check')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->src and $self->can("file_check")
MC/DC0%0/2
defined $self->src and $self->can("file_check")
defined $self->src
$self->can("file_check")
Truth table33%1/3
A and B
A= defined $self->srcB= $self->can("file_check")
| |||||||||||||||||||||||||||||
| 34 | 0 | $ret &= $self->file_check; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 11 | $ret &= $self->mode_process(\&mode_check); | |||||||||||||||||||||||||||
| 37 | 11 | $ret &= $self->uid_process(\&uid_check); | |||||||||||||||||||||||||||
| 38 | 11 | $ret &= $self->gid_process(\&gid_check); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | ▶ | return (($ret == 1) ? $self->pass('File matches') : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | $self->fail("File doesn't match")); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub repair { | ||||||||||||||||||||||||||||
| 45 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
repair: called CC=8 SCAR=32.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 3 | my $ret = 1; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # If we defined a source file | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if (defined($self->src) and $self->can('file_repair')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->src and $self->can("file_repair")
MC/DC0%0/2
defined $self->src and $self->can("file_repair")
defined $self->src
$self->can("file_repair")
Truth table33%1/3
A and B
A= defined $self->srcB= $self->can("file_repair")
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (-e $self->dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | die("Destination ${\$self->dest} is not a file") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | if (-d $self->dest); | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | die("Destination ${\$self->dest} exists is is not writable") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $self->dest and not -w $self->dest
MC/DC0%0/2
-f $self->dest and not -w $self->dest
-f $self->dest
not -w $self->dest
Truth table0%0/3
A and not(B)
A= -f $self->destB= not -w $self->dest
| |||||||||||||||||||||||||||||
| 54 | if (-f $self->dest and ! -w $self->dest); | ||||||||||||||||||||||||||||
| 55 | 0 | backup_file($self->dest); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | $ret &= $self->file_repair; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 3 | $ret &= $self->mode_process(\&mode_repair); | |||||||||||||||||||||||||||
| 61 | 3 | $ret &= $self->uid_process(\&uid_repair); | |||||||||||||||||||||||||||
| 62 | 3 | $ret &= $self->gid_process(\&gid_repair); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | return (($ret == 1) ? $self->pass('File repaired') : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | $self->fail('File not repaired')); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Diff for files | ||||||||||||||||||||||||||||
| 69 | sub diff { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
diff: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | die("Destination file does not exist: dest=<${\$self->dest}>") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | unless (-f -e -r $self->dest); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # If we defined a source file | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (defined($self->src) and $self->can('file_diff')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->src and $self->can("file_diff")
MC/DC0%0/2
defined $self->src and $self->can("file_diff")
defined $self->src
$self->can("file_diff")
Truth table0%0/3
A and B
A= defined $self->srcB= $self->can("file_diff")
| |||||||||||||||||||||||||||||
| 77 | 0 | $self->file_diff(); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # TODO mode, uid, gid diff functions | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Wrapper functions to perform sanity tests on arguments | ||||||||||||||||||||||||||||
| 84 | # Return pass if arguments are missing, die if invalid | ||||||||||||||||||||||||||||
| 85 | sub mode_process { | ||||||||||||||||||||||||||||
| 86 | 14 | ▶ | my ($self, $func) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mode_process: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 14 | ▶ | return 1 unless($self->mode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 14 | my $mode = $self->mode; | |||||||||||||||||||||||||||
| 90 | 14 | ▶ | die("Invalid file mode") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | if ($mode !~ m/^[0-7]{3,4}$/); | ||||||||||||||||||||||||||||
| 92 | 14 | $mode =~ s/^([0-7]{3})$/0$1/; | |||||||||||||||||||||||||||
| 93 | 14 | $self->{mode} = $mode; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 14 | &{$func}($self->dest, $self->mode); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub uid_process { | ||||||||||||||||||||||||||||
| 99 | 14 | ▶ | my ($self, $func) = @_; | ||||||||||||||||||||||||||
|
Subroutine
uid_process: called CC=3 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 14 | ▶ | return 1 unless ($self->uid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | die("Invalid user id") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if ($self->uid !~ m/^[0-9]+$/); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | &{$func}($self->dest, $self->uid); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub gid_process { | ||||||||||||||||||||||||||||
| 109 | 14 | ▶ | my ($self, $func) = @_; | ||||||||||||||||||||||||||
|
Subroutine
gid_process: called CC=3 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 14 | ▶ | return 1 unless ($self->gid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | die("Invalid group id") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | if ($self->gid !~ m/^[0-9]+$/); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | &{$func}($self->dest, $self->gid); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Mode operations | ||||||||||||||||||||||||||||
| 119 | sub mode_check { | ||||||||||||||||||||||||||||
| 120 | 11 | ▶ | my ($file, $mode) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mode_check: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 11 | my $fh = stat($file); | |||||||||||||||||||||||||||
| 122 | 11 | ▶ | if ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 11 | my $curmode = sprintf "%04o", $fh->mode & 07777; | |||||||||||||||||||||||||||
| 124 | 11 | debug("File mode: file=<$file> mode=<$curmode>"); | |||||||||||||||||||||||||||
| 125 | 11 | ▶ | return 1 if ($curmode eq $mode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub mode_repair { | ||||||||||||||||||||||||||||
| 130 | 3 | ▶ | my ($file, $mode) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mode_repair: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 3 | debug("Chmod file: file=<$file> mode=<$mode>"); | |||||||||||||||||||||||||||
| 132 | 3 | chmod(oct($mode), $file); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # UID operations | ||||||||||||||||||||||||||||
| 136 | sub uid_check { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my ($file, $uid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
uid_check: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | my $fh = stat($file); | |||||||||||||||||||||||||||
| 139 | 0 | my $curuid = undef; | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | my $curuid = $fh->uid; | |||||||||||||||||||||||||||
| 142 | 0 | debug("File owner: file=<$file> uid=<$uid>"); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | return ($curuid == $uid); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub uid_repair { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my ($file, $uid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
uid_repair: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 0 | debug("Chown file: file=<$file> uid=<$uid>"); | |||||||||||||||||||||||||||
| 150 | 0 | chown($uid, -1, $file); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # GID operations | ||||||||||||||||||||||||||||
| 154 | sub gid_check { | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my ($file, $gid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
gid_check: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 0 | my $fh = stat($file); | |||||||||||||||||||||||||||
| 157 | 0 | my $curgid = undef; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | $curgid = $fh->gid; | |||||||||||||||||||||||||||
| 160 | 0 | debug("File group: file=<$file> gid=<$gid>"); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 0 | return ($curgid == $gid); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub gid_repair { | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my ($file, $gid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
gid_repair: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | 0 | debug("Chown file: file=<$file> gid=<$gid>"); | |||||||||||||||||||||||||||
| 168 | 0 | chown(-1, $gid, $file); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Compare hashes between two files | ||||||||||||||||||||||||||||
| 172 | sub file_compare { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my @files = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_compare: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | return 0 unless (scalar @files == 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # Get hashes and return compare | ||||||||||||||||||||||||||||
| 177 | 0 | my ($hasha, $hashb) = map hash_file($_), @files; | |||||||||||||||||||||||||||
| 178 | 0 | debug(sprintf "File compare result: <hash=%s> <hash=%s>", $hasha, $hashb); | |||||||||||||||||||||||||||
| 179 | 0 | return ($hasha eq $hashb); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 17 | 1; | |||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||