OpenPGP/KeyRing.pm

filter: Statementstmt 83.6% 97 / 116 Branchbran 60.8% 28 / 46 Conditioncond 46.4% 13 / 28 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 80.0% 16 / 20 Podpod 36.3% 4 / 11 totaltotal 67.8% 160 / 236 CC 35 SCAR 25.4
CC
35
Cov
73%
find_keyblock
29.3
init
7.3
save
6.0
CRAP 12.7
« OpenPGP/PacketFactory.pm ↑ Summary OpenPGP/KeyBlock.pm »
1 package Crypt::OpenPGP::KeyRing;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 our $VERSION = '1.21'; # VERSION
6  
7 12 ▶use Crypt::OpenPGP::Constants qw( PGP_PKT_USER_ID
Subroutine BEGIN: called CC=1 SCAR=0.0
8 PGP_PKT_PUBLIC_KEY
9 PGP_PKT_SECRET_KEY
10 PGP_PKT_PUBLIC_SUBKEY
11 12 PGP_PKT_SECRET_SUBKEY );
12 12 ▶use Crypt::OpenPGP::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Crypt::OpenPGP::KeyBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Crypt::OpenPGP::PacketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use parent qw( Crypt::OpenPGP::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 16 my $class = shift;
19 16 my $ring = bless { }, $class;
20 16 $ring->init(@_);
21 }
22  
23 16 ▶sub init {
Subroutine init: called CC=7 SCAR=19.8
Pod undocumented
24 16 my $ring = shift;
25 16 my %param = @_;
26 16 ▶ $ring->{_data} = $param{Data} || '';
Condition50%1/2
$param{'Data'} || ''
l!l
016
MC/DC0%0/1
$param{'Data'} || ''
$param{'Data'}
Truth table50%1/2
A
A= $param{'Data'}
Aresult
11
00
27 16 ▶ if (!$ring->{_data} && (my $file = $param{Filename})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ring->{"_data"} and my $file = $param{"Filename"}) 16 0 16
Condition33%1/3
not $ring->{"_data"} and my $file = $param{"Filename"}
!ll&&!rl&&r
0016
MC/DC0%0/2
not $ring->{"_data"} and my $file = $param{"Filename"}
not $ring->{"_data"} my $file = $param{"Filename"}
Truth table33%1/3
not(A) and B
A= not $ring->{"_data"}B= my $file = $param{"Filename"}
ABresult
0X0
100
111
28 16 ▶ open ( my $fh, "<", $file ) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return ref($ring)->error("Can't open keyring ${file}: $!") 15 1 16
29 return (ref $ring)->error("Can't open keyring $file: $!");
30 15 binmode $fh;
31 15 { local $/; $ring->{_data} = <$fh> }
32 15 close $fh;
33 }
34 15 ▶ if ($ring->{_data} =~ /^-----BEGIN/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ring->{"_data"} =~ /^-----BEGIN/) 2 13 15
35 2 require Crypt::OpenPGP::Armour;
36 2 ▶ my $rec = Crypt::OpenPGP::Armour->unarmour($ring->{_data}) or
Branch: 1 branch
BranchTrueFalseTotal
my $rec = "Crypt::OpenPGP::Armour"->unarmour($ring->{"_data"}) or return ref($ring)->error('Unarmour failed: ' . "Crypt::OpenPGP::Armour"->errstr) 2 0 2
37 return (ref $ring)->error("Unarmour failed: " .
38 Crypt::OpenPGP::Armour->errstr);
39 2 $ring->{_data} = $rec->{Data};
40 }
41 15 $ring;
42 }
43  
44 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my $ring = shift;
46 0 my @blocks = $ring->blocks;
47 0 my $res = '';
48 0 for my $block (@blocks) {
49 0 $res .= $block->save;
50 }
51 0 $res;
52 }
53  
54 1 ▶sub read {
Subroutine read: called CC=2 SCAR=7.2
Pod undocumented
55 1 my $ring = shift;
56 1 ▶ return $ring->error("No data to read") unless $ring->{_data};
Branch: 1 branch
BranchTrueFalseTotal
unless $ring->{"_data"} 0 1 1
57 1 my $buf = Crypt::OpenPGP::Buffer->new;
58 1 $buf->append($ring->{_data});
59 1 $ring->restore($buf);
60 0 1;
61 }
62  
63 1 ▶sub restore {
Subroutine restore: called CC=5 SCAR=17.1
Pod undocumented
64 1 my $ring = shift;
65 1 my($buf) = @_;
66 1 $ring->{blocks} = [];
67 1 my($kb);
68 1 ▶ while (my $packet = Crypt::OpenPGP::PacketFactory->parse($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $packet = "Crypt::OpenPGP::PacketFactory"->parse($buf)) 1 0 1
69 1 ▶ if (ref($packet) eq "Crypt::OpenPGP::Certificate" &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey) 1 0 1
Condition33%1/3
ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey
!ll&&!rl&&r
001
MC/DC0%0/2
ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey
ref $packet eq "Crypt::OpenPGP::Certificate" not $packet->is_subkey
Truth table33%1/3
A and not(B)
A= ref $packet eq "Crypt::OpenPGP::Certificate"B= not $packet->is_subkey
ABresult
0X0
100
111
70 !$packet->is_subkey) {
71 1 $kb = Crypt::OpenPGP::KeyBlock->new;
72 1 $ring->add($kb);
73 }
74 1 ▶ $kb->add($packet) if $kb;
Branch: 1 branch
BranchTrueFalseTotal
if $kb 1 0 1
75 }
76 }
77  
78 1 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
79 1 my $ring = shift;
80 1 my($entry) = @_;
81 1 push @{ $ring->{blocks} }, $entry;
82 }
83  
84 5 ▶sub find_keyblock_by_keyid {
Subroutine find_keyblock_by_keyid: called CC=4 SCAR=14.1
Pod documented
85 5 my $ring = shift;
86 5 my($key_id) = @_;
87 5 my $ref = $ring->{by_keyid}{$key_id};
88 5 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 5 0 5
89 5 my $len = length($key_id);
90 my @kbs = $ring->find_keyblock(
91 7 ▶ sub { substr($_[0]->key_id, -$len, $len) eq $key_id },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 5 [ PGP_PKT_PUBLIC_KEY, PGP_PKT_SECRET_KEY,
93 PGP_PKT_PUBLIC_SUBKEY, PGP_PKT_SECRET_SUBKEY ], 1 );
94 1 ▶ return unless @kbs;
Branch: 1 branch
BranchTrueFalseTotal
unless @kbs 0 1 1
95 1 $ref = $ring->{by_keyid}{ $key_id } = \@kbs;
96 }
97 1 ▶ return wantarray ? @$ref : $ref->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
98 }
99  
100 0 ▶sub find_keyblock_by_uid {
Subroutine find_keyblock_by_uid: not called CC=1 SCAR=6.9
Pod documented
101 0 my $ring = shift;
102 0 my($uid) = @_;
103 0 ▶ $ring->find_keyblock(sub { $_[0]->id =~ /$uid/i },
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 [ PGP_PKT_USER_ID ], 1 );
105 }
106  
107 0 ▶sub find_keyblock_by_index {
Subroutine find_keyblock_by_index: not called CC=1 SCAR=6.9
Pod documented
108 0 my $ring = shift;
109 0 my($index) = @_;
110 ## XXX should not have to read entire keyring
111 0 $ring->read;
112 0 ($ring->blocks)[$index];
113 }
114  
115 5 ▶sub find_keyblock {
Subroutine find_keyblock: called CC=19 SCAR=33.8
Pod undocumented
116 5 my $ring = shift;
117 5 my($test, $pkttypes, $multiple) = @_;
118 5 ▶ $pkttypes ||= [];
Condition50%1/2
$pkttypes ||= []
l!l
50
MC/DC0%0/1
$pkttypes ||= []
$pkttypes
Truth table50%1/2
A
A= $pkttypes
Aresult
11
00
119 5 ▶ return $ring->error("No data to read") unless $ring->{_data};
Branch: 1 branch
BranchTrueFalseTotal
unless $ring->{"_data"} 0 5 5
120 5 my $buf = Crypt::OpenPGP::Buffer->new_with_init($ring->{_data});
121 5 my($last_kb_start_offset, $last_kb_start_cert, @kbs);
122 5 ▶ while (my $pkt = Crypt::OpenPGP::PacketFactory->parse($buf,
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkt = "Crypt::OpenPGP::PacketFactory"->parse($buf, [&PGP_PKT_SECRET_KEY(), &PGP_PKT_PUBLIC_KEY(), @$pkttypes], $pkttypes)) 7 1 8
123 [ PGP_PKT_SECRET_KEY, PGP_PKT_PUBLIC_KEY,
124 @$pkttypes ], $pkttypes)) {
125 7 ▶ if (($pkt->{__unparsed} && ($pkt->{type} == PGP_PKT_SECRET_KEY ||
Branch: 1 branch
BranchTrueFalseTotal
if ($pkt->{"__unparsed"} and $pkt->{"type"} == &PGP_PKT_SECRET_KEY() || $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY() or ref $pkt eq "Crypt::OpenPGP::Certificate" and not $pkt->is_subkey) 5 2 7
Condition41%5/12
$pkt->{"__unparsed"} and $pkt->{"type"} == &PGP_PKT_SECRET_KEY() || $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY() or ref $pkt eq "Crypt::OpenPGP::Certificate" and not $pkt->is_subkey
l!l&&r!l&&!r
052
$pkt->{"__unparsed"} and $pkt->{"type"} == &PGP_PKT_SECRET_KEY() || $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY()
!ll&&!rl&&r
700
$pkt->{"type"} == &PGP_PKT_SECRET_KEY() or $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY()
l!l&&r!l&&!r
000
ref $pkt eq "Crypt::OpenPGP::Certificate" and not $pkt->is_subkey
!ll&&!rl&&r
025
MC/DC20%1/5
$pkt->{"__unparsed"} and $pkt->{"type"} == &PGP_PKT_SECRET_KEY() || $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY() or ref $pkt eq "Crypt::OpenPGP::Certificate" and not $pkt->is_subkey
$pkt->{"__unparsed"} $pkt->{"type"} == &PGP_PKT_SECRET_KEY() $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY() ref $pkt eq "Crypt::OpenPGP::Certificate" not $pkt->is_subkey
Truth table25%2/8
A and B or C or D and not(E)
A= $pkt->{"__unparsed"}B= $pkt->{"type"} == &PGP_PKT_SECRET_KEY()C= $pkt->{"type"} == &PGP_PKT_PUBLIC_KEY()D= ref $pkt eq "Crypt::OpenPGP::Certificate"E= not $pkt->is_subkey
ABCDEresult
11XXX1
101XX1
0XX111
100111
0XX0X0
0XX100
1000X0
100100
126 $pkt->{type} == PGP_PKT_PUBLIC_KEY)) ||
127 (ref($pkt) eq 'Crypt::OpenPGP::Certificate' && !$pkt->is_subkey)) {
128 5 $last_kb_start_offset = $buf->offset;
129 5 $last_kb_start_cert = $pkt;
130 }
131 7 ▶ next unless !$pkt->{__unparsed} && $test->($pkt);
Branch: 1 branch
BranchTrueFalseTotal
unless not $pkt->{"__unparsed"} and &$test($pkt) 2 1 3
Condition66%2/3
not $pkt->{"__unparsed"} and &$test($pkt)
!ll&&!rl&&r
061
MC/DC50%1/2
not $pkt->{"__unparsed"} and &$test($pkt)
not $pkt->{"__unparsed"} &$test($pkt)
Truth table66%2/3
not(A) and B
A= not $pkt->{"__unparsed"}B= &$test($pkt)
ABresult
0X0
100
111
132 1 my $kb = Crypt::OpenPGP::KeyBlock->new;
133  
134 ## Rewind buffer; if start-cert is parsed, rewind to offset
135 ## after start-cert--otherwise rewind before start-cert
136 1 ▶ if ($last_kb_start_cert->{__unparsed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_kb_start_cert->{'__unparsed'}) { } 0 1 1
137 $buf->set_offset($last_kb_start_offset -
138 0 $last_kb_start_cert->{__pkt_len});
139 0 my $cert = Crypt::OpenPGP::PacketFactory->parse($buf);
140 0 $kb->add($cert);
141 } else {
142 1 $buf->set_offset($last_kb_start_offset);
143 1 $kb->add($last_kb_start_cert);
144 }
145 {
146 1 my $off = $buf->offset;
147 5 my $packet = Crypt::OpenPGP::PacketFactory->parse($buf);
148 5 ▶ last unless $packet;
Branch: 1 branch
BranchTrueFalseTotal
unless $packet 1 4 5
149 4 ▶ $buf->set_offset($off),
Branch: 1 branch
BranchTrueFalseTotal
if ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey 0 4 4
Condition66%2/3
ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey
!ll&&!rl&&r
310
MC/DC0%0/2
ref $packet eq "Crypt::OpenPGP::Certificate" and not $packet->is_subkey
ref $packet eq "Crypt::OpenPGP::Certificate" not $packet->is_subkey
Truth table66%2/3
A and not(B)
A= ref $packet eq "Crypt::OpenPGP::Certificate"B= not $packet->is_subkey
ABresult
0X0
100
111
150 last if ref($packet) eq "Crypt::OpenPGP::Certificate" &&
151 !$packet->is_subkey;
152 4 ▶ $kb->add($packet) if $kb;
Branch: 1 branch
BranchTrueFalseTotal
if $kb 4 0 4
153 4 redo;
154 }
155 1 ▶ unless ($multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple) { } 1 0 1
156 0 ▶ return wantarray ? ($kb, $pkt) : $kb;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
157 } else {
158 1 ▶ return $kb unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
159 1 push @kbs, $kb;
160 }
161 }
162 1 @kbs;
163 }
164  
165 1 ▶sub blocks { $_[0]->{blocks} ? @{ $_[0]->{blocks} } : () }
Subroutine blocks: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'blocks'} ? : 1 0 1
166  
167 12 1;
168 __END__
« OpenPGP/PacketFactory.pm ↑ Summary OpenPGP/KeyBlock.pm »