Jmx4Perl/Agent/Jolokia/Verifier/ChecksumVerifier.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 59 CC 4 SCAR 23.0
CC
4
Cov
0%
verify
20.0
new
2.0
extension
2.0
CRAP 10.0
1 #!/usr/bin/perl
2 package JMX::Jmx4Perl::Agent::Jolokia::Verifier::ChecksumVerifier;
3  
4 =head1 NAME
5  
6 JMX::Jmx4Perl::Agent::Jolokia::Verifier::ChecksumVerifier - Verifies a
7 checksum for a downloaded artifact.
8  
9 =head1 DESCRIPTION
10  
11 This verifier provides the base for simple checksum checking. It needs to be
12 subclassed to provide the proper extension (e.g. ".sha1") and creating of a
13 digester.
14  
15 =cut
16  
17  
18 use strict;
19  
20 sub new {
21 my $class = shift;
22 my $self = {};
23 bless $self,(ref($class) || $class);
24 }
25  
26 sub extension {
27 die "abstract";
28 }
29  
30 sub name {
31 die "abstract";
32 }
33  
34 sub create_digester {
35 die "abstract";
36 }
37  
38 sub verify {
39 my $self = shift;
40 my %args = @_;
41 my $logger = $args{logger};
42 my $sig = $args{signature};
43 chomp $sig;
44 $sig =~ s/^([^\s]+).*$/$1/;
45 my $digester = $self->create_digester;
46 my $file = $args{path};
47 if ($file) {
48 open (my $fh, "<", $file) || ($logger->error("Cannot open $file for ",$self->name," check: $!") && die "\n");
49 $digester->addfile($fh);
50 close $fh;
51 } else {
52 my $data = $args{data};
53 $digester->add($data);
54 }
55 my $sig_calc = $digester->hexdigest;
56 if (lc($sig) eq lc($sig_calc)) {
57 $logger->info("Passed ",$self->name," check (" . $sig_calc . ")",($file ? " for file $file" : ""));
58 } else {
59 $logger->error("Failed ",$self->name," check. Got: " . $sig_calc . ", Expected: " . $sig);
60 die "\n";
61 }
62 }
63  
64 =head1 LICENSE
65  
66 This file is part of jmx4perl.
67 Jmx4perl is free software: you can redistribute it and/or modify
68 it under the terms of the GNU General Public License as published by
69 The Free Software Foundation, either version 2 of the License, or
70 (at your option) any later version.
71
72 jmx4perl is distributed in the hope that it will be useful,
73 but WITHOUT ANY WARRANTY; without even the implied warranty of
74 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 GNU General Public License for more details.
76
77 You should have received a copy of the GNU General Public License
78 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
79  
80 A commercial license is available as well. Please contact roland@cpan.org for
81 further details.
82  
83 =head1 AUTHOR
84  
85 roland@cpan.org
86  
87 =cut
88  
89 1;