lib/CPANPLUS/Module/Checksums.pm

filter: Statementstmt 81.9% 95 / 116 Branchbran 57.3% 39 / 68 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 74.3% 151 / 203 CC 36 SCAR 28.4
CC
36
Cov
73%
_validate_checksum
40.9
_parse_checksums_file
11.1
_check_signature_for_checksum_file
10.9
CRAP 17.1
« lib/CPANPLUS/Internals/Utils.pm ↑ Summary lib/CPANPLUS/Internals/Source.pm »
1 package CPANPLUS::Module::Checksums;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use vars qw[@ISA $VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use CPANPLUS::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use CPANPLUS::Internals::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 ▶use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Params::Check qw[check];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 20 $Params::Check::VERBOSE = 1;
16  
17 20 @ISA = qw[ CPANPLUS::Module::Signature ];
18 20 $VERSION = "0.9914";
19  
20 =head1 NAME
21  
22 CPANPLUS::Module::Checksums - checking the checksum of a distribution
23  
24 =head1 SYNOPSIS
25  
26 $file = $modobj->checksums;
27 $bool = $mobobj->_validate_checksum;
28  
29 =head1 DESCRIPTION
30  
31 This is a class that provides functions for checking the checksum
32 of a distribution. Should not be loaded directly, but used via the
33 interface provided via C<CPANPLUS::Module>.
34  
35 =head1 METHODS
36  
37 =head2 $mod->checksums
38  
39 Fetches the checksums file for this module object.
40 For the options it can take, see C<CPANPLUS::Module::fetch()>.
41  
42 Returns the location of the checksums file on success and false
43 on error.
44  
45 The location of the checksums file is also stored as
46  
47 $mod->status->checksums
48  
49 =cut
50  
51 sub checksums {
52 4 ▶ my $mod = shift or return;
Subroutine checksums: called CC=3 SCAR=12.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $mod = shift() or return 0 4 4
53  
54 4 my $file = $mod->_get_checksums_file( @_ );
55  
56 4 ▶ return $mod->status->checksums( $file ) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 4 0 4
57  
58 0 return;
59 }
60  
61 ### checks if the package checksum matches the one
62 ### from the checksums file
63 sub _validate_checksum {
64 16 ▶ my $self = shift; #must be isa CPANPLUS::Module
Subroutine _validate_checksum: called CC=14 SCAR=37.1
65 16 my $conf = $self->parent->configure_object;
66 16 my %hash = @_;
67  
68 16 my $verbose;
69 16 my $tmpl = {
70 verbose => { default => $conf->get_conf('verbose'),
71 store => \$verbose },
72 };
73  
74 16 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 16 16
75  
76 ### if we can't check it, we must assume it's ok ###
77 16 ▶ return $self->status->checksum_ok(1)
Branch: 1 branch
BranchTrueFalseTotal
unless can_load("modules", {"Digest::SHA", "0.0"}) 0 16 16
78 unless can_load( modules => { 'Digest::SHA' => '0.0' } );
79 #class CPANPLUS::Module::Status is runtime-generated
80  
81 16 ▶ my $file = $self->_get_checksums_file( verbose => $verbose ) or (
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_get_checksums_file("verbose", $verbose) or error(loc("Could not fetch '%1' file", "CHECKSUMS")), return 0 16 16
82 error(loc(q[Could not fetch '%1' file], CHECKSUMS)), return );
83  
84 16 ▶ $self->_check_signature_for_checksum_file( file => $file ) or (
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_signature_for_checksum_file("file", $file) or error(loc("Could not verify '%1' file", "CHECKSUMS")), return 0 16 16
85 error(loc(q[Could not verify '%1' file], CHECKSUMS)), return );
86 #for whole CHECKSUMS file
87  
88 16 ▶ my $href = $self->_parse_checksums_file( file => $file ) or (
Branch: 1 branch
BranchTrueFalseTotal
my $href = $self->_parse_checksums_file("file", $file) or error(loc("Could not parse '%1' file", "CHECKSUMS")), return 0 16 16
89 error(loc(q[Could not parse '%1' file], CHECKSUMS)), return );
90  
91 16 my $cpan_path = $href->{ $self->package }->{'cpan_path'};
92  
93 16 ▶ if ( defined $cpan_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cpan_path) 0 16 16
94 0 my $chk_pth = join '/', 'authors/id', $cpan_path;
95 0 ▶ if ( $chk_pth ne $self->path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chk_pth ne $self->path) 0 0 0
96 0 error(loc( "Archive checksum path for '%1': " .
97 "should be '%2', but it says it is '%3'. Abandoning.",
98 $self->package, $self->path, $chk_pth));
99 0 return $self->status->checksum_ok(0);
100 }
101 }
102  
103 16 my $size = $href->{ $self->package }->{'size'};
104  
105 ### the checksums file tells us the size of the archive
106 ### but the downloaded file is of different size
107 16 ▶ if( defined $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) { } 15 1 16
108 15 ▶ if( not (-s $self->status->fetch == $size) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $self->status->fetch == $size) 0 15 15
109 0 error(loc( "Archive size does not match for '%1': " .
110 "size is '%2' but should be '%3'",
111 $self->package, -s $self->status->fetch, $size));
112 0 return $self->status->checksum_ok(0);
113 }
114 } else {
115 1 msg(loc("Archive size is not known for '%1'",$self->package),$verbose);
116 }
117  
118 16 my $sha = $href->{ $self->package }->{'sha256'};
119  
120 16 ▶ unless( defined $sha ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sha) 16 0 16
121 16 msg(loc("No 'sha256' checksum known for '%1'",$self->package),$verbose);
122  
123 16 return $self->status->checksum_ok(1);
124 }
125  
126 0 $self->status->checksum_value($sha);
127  
128  
129 0 ▶ my $fh = FileHandle->new( $self->status->fetch ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new($self->status->fetch) or return 0 0 0
130 0 binmode $fh;
131  
132 0 my $ctx = Digest::SHA->new(256);
133 0 $ctx->addfile( $fh );
134  
135 0 my $hexdigest = $ctx->hexdigest;
136 0 my $flag = $hexdigest eq $sha;
137 0 ▶ $flag
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 0 0 0
138 ? msg(loc("Checksum matches for '%1'", $self->package),$verbose)
139 : error(loc("Checksum does not match for '%1': " .
140 "SHA256 is '%2' but should be '%3'",
141 $self->package, $hexdigest, $sha),$verbose);
142  
143  
144 0 ▶ return $self->status->checksum_ok(1) if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 0 0 0
145 0 return $self->status->checksum_ok(0);
146 }
147  
148  
149 ### fetches the module objects checksum file ###
150 sub _get_checksums_file {
151 24 ▶ my $self = shift;
Subroutine _get_checksums_file: called CC=3 SCAR=11.1
152 24 my %hash = @_;
153  
154 24 my $clone = $self->clone;
155 24 $clone->package( CHECKSUMS );
156  
157 # If the user specified a fetchdir, then every CHECKSUMS file will always
158 # be stored there, not in an author-specific subdir. Thus, in this case,
159 # we need to always re-fetch the CHECKSUMS file and hence need to set the
160 # TTL to something small.
161 24 my $have_fetchdir =
162 $self->parent->configure_object->get_conf('fetchdir') ne '';
163 24 ▶ my $ttl = $have_fetchdir ? 0.001 : 3600;
Branch: 1 branch
BranchTrueFalseTotal
$have_fetchdir ? : 0 24 24
164 24 ▶ my $file = $clone->fetch( ttl => $ttl, %hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $clone->fetch("ttl", $ttl, %hash) or return 0 24 24
165  
166 24 return $file;
167 }
168  
169 sub _parse_checksums_file {
170 18 ▶ my $self = shift;
Subroutine _parse_checksums_file: called CC=11 SCAR=24.1
171 18 my %hash = @_;
172  
173 18 my $file;
174 18 my $tmpl = {
175 file => { required => 1, allow => FILE_READABLE, store => \$file },
176 };
177 18 my $args = check( $tmpl, \%hash );
178  
179 18 ▶ my $fh = OPEN_FILE->( $file ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = sub { package CPANPLUS::Internals::Constants; use strict; my($file, $mode) = (@_, ''); my $fh; open $fh, "$mode" . $file or error(loc(q[Could not open file '%1': %2], $file, $!)); return $fh if $fh; return;}->($file) or return 0 18 18
180  
181 ### loop over the header, there might be a pgp signature ###
182 18 my $signed;
183 18 ▶ while (local $_ = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = readline $fh)) 108 0 108
184 108 ▶ last if /^\$cksum = \{\s*$/; # skip till this line
Branch: 1 branch
BranchTrueFalseTotal
if /^\$cksum = \{\s*$/ 18 90 108
185 90 my $header = PGP_HEADER; # but be tolerant of whitespace
186 90 ▶ $signed = 1 if /^${header}\s*$/;# due to crossplatform linebreaks
Branch: 1 branch
BranchTrueFalseTotal
if /^$header\s*$/ 18 72 90
187 }
188  
189 ### read the filehandle, parse it rather than eval it, even though it
190 ### *should* be valid perl code
191 18 my $dist;
192 18 my $cksum = {};
193 18 ▶ while (local $_ = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = readline $fh)) 381 0 381
194  
195 381 ▶ if (/^\s*'([^']+)' => \{\s*$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^\s*'([^']+)' => \{\s*$/) { } 69 312 381
elsif (/^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/ and defined $dist) { } 173 139 312
elsif (/^\s*}[,;]?\s*$/) { } 87 52 139
elsif (/^__END__\s*$/) { } 18 34 52
196 69 $dist = $1;
197  
198 ▶ } elsif (/^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/ and defined $dist) {
Condition66%2/3
/^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/ and defined $dist
!ll&&!rl&&r
1390173
MC/DC50%1/2
/^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/ and defined $dist
/^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/ defined $dist
Truth table66%2/3
A and B
A= /^\s*'([^']+)' => '?([^'\n]+)'?,?\s*$/B= defined $dist
ABresult
0X0
100
111
199 173 $cksum->{$dist}{$1} = $2;
200  
201 } elsif (/^\s*}[,;]?\s*$/) {
202 87 undef $dist;
203  
204 } elsif (/^__END__\s*$/) {
205 18 last;
206  
207 } else {
208 34 error( loc("Malformed %1 line: %2", CHECKSUMS, $_) );
209 }
210 }
211  
212 18 return $cksum;
213 }
214  
215 sub _check_signature_for_checksum_file {
216 16 ▶ my $self = shift;
Subroutine _check_signature_for_checksum_file: called CC=9 SCAR=23.9
217  
218 16 my $conf = $self->parent->configure_object;
219 16 my %hash = @_;
220  
221 ### you don't want to check signatures,
222 ### so let's just return true;
223 16 ▶ return 1 unless $conf->get_conf('signature');
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->get_conf("signature") 15 1 16
224  
225 1 my($force,$file,$verbose);
226 1 my $tmpl = {
227 file => { required => 1, allow => FILE_READABLE, store => \$file },
228 force => { default => $conf->get_conf('force'), store => \$force },
229 verbose => { default => $conf->get_conf('verbose'), store => \$verbose },
230 };
231  
232 1 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 1 1
233  
234 1 ▶ my $fh = OPEN_FILE->($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = sub { package CPANPLUS::Internals::Constants; use strict; my($file, $mode) = (@_, ''); my $fh; open $fh, "$mode" . $file or error(loc(q[Could not open file '%1': %2], $file, $!)); return $fh if $fh; return;}->($file) or return 0 1 1
235  
236 1 my $signed;
237 1 ▶ while (local $_ = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = readline $fh)) 35 1 36
238 35 my $header = PGP_HEADER;
239 35 ▶ $signed = 1 if /^$header$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$header$/ 1 34 35
240 }
241  
242 1 ▶ if ( !$signed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signed) 0 1 1
243 0 msg(loc("No signature found in %1 file '%2'",
244 CHECKSUMS, $file), $verbose);
245  
246 0 ▶ return 1 unless $force;
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
247  
248 0 error( loc( "%1 file '%2' is not signed -- aborting",
249 CHECKSUMS, $file ) );
250 0 return;
251  
252 }
253  
254 1 ▶ if( can_load( modules => { 'Module::Signature' => '0.06' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {"Module::Signature", "0.06"})) 0 1 1
255 # local $Module::Signature::SIGNATURE = $file;
256 # ... check signatures ...
257 }
258  
259 1 return 1;
260 }
261  
262  
263  
264 # Local variables:
265 # c-indentation-style: bsd
266 # c-basic-offset: 4
267 # indent-tabs-mode: nil
268 # End:
269 # vim: expandtab shiftwidth=4:
270  
271 20 1;
« lib/CPANPLUS/Internals/Utils.pm ↑ Summary lib/CPANPLUS/Internals/Source.pm »