blib/lib/WWW/HKP.pm

filter: Statementstmt 38.9% 69 / 177 Branchbran 33.7% 29 / 86 Conditioncond 60.4% 29 / 48 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 46.4% 13 / 28 Podpod 100% 6 / 6 totaltotal 40.6% 150 / 369 CC 65 SCAR 36.8
CC
65
Cov
41%
query
156.0
_parse_mr
41.6
__ANON__
20.0
CRAP 39.8
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package WWW::HKP;
5  
6 # ABSTRACT: Interface to HTTP Keyserver Protocol (HKP)
7  
8 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use AnyEvent::HTTP qw(http_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI 1.60;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use URI::Escape 3.31;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URL::Encode qw(url_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Email::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION = '0.041'; # VERSION
19  
20 2 ▶sub new {
Subroutine new: called CC=6 SCAR=19.3
Pod documented
21 2 my ( $class, %options ) = @_;
22  
23 2 ▶ my $uri = URI->new( $options{secure} ? 'https:' : 'http:' );
Branch: 1 branch
BranchTrueFalseTotal
$options{'secure'} ? : 0 2 2
24 2 ▶ $uri->host( $options{host} || 'localhost' );
Condition50%1/2
$options{'host'} || 'localhost'
l!l
02
MC/DC0%0/1
$options{'host'} || 'localhost'
$options{'host'}
Truth table50%1/2
A
A= $options{'host'}
Aresult
11
00
25 2 ▶ $uri->port( $options{port} || ( $options{secure} ? 443 : 11371 ) );
Branch: 1 branch
BranchTrueFalseTotal
$options{'secure'} ? : 0 2 2
Condition33%1/3
$options{'port'} || ($options{'secure'} ? 443 : 11371)
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{'port'} || ($options{'secure'} ? 443 : 11371)
$options{'port'} ($options{'secure'} ? 443 : 11371)
Truth table33%1/3
A || B
A= $options{'port'}B= ($options{'secure'} ? 443 : 11371)
ABresult
1X1
011
000
26  
27 2 my $ua = $AnyEvent::HTTP::USERAGENT;
28 {
29 2 local $_ = __PACKAGE__ . '/' . $VERSION;
30 2 $ua =~ s{\)$}{ +$_)};
31 }
32  
33 2 AE::log debug => "User-Agent: $ua";
34 2 AE::log debug => "Host: $uri";
35  
36 2 my $self = {
37 ua => $ua,
38 uri => $uri,
39 };
40  
41 2 ▶ return bless $self => ( ref $class || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
42 }
43  
44 0 ▶sub discover {
Subroutine discover: not called CC=3 SCAR=24.8
Pod documented
45 0 my ( $email, %options ) = @_;
46 0 ▶ ($email) = Email::Address->parse($email)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $email eq "Email::Address" 0 0 0
47 unless ref $email eq 'Email::Address';
48 0 ▶ return unless $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 0 0
49 0 my $cv = AE::cv;
50 0 AE::log debug => "srv query: _hkp._tcp." . $email->host;
51 0 ▶ AnyEvent::DNS::srv 'hkp', 'tcp', $email->host, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
52 0 foreach my $rr (@_) {
53 0 my ( $prio, $weight, $port, $host ) = @$rr;
54 0 AE::log debug =>
55 "srv answer: $host:$port (prio $prio weight $weight)";
56 0 ▶ my $hkp = __PACKAGE__->new(
Branch: 1 branch
BranchTrueFalseTotal
$port == 443 ? : 0 0 0
57 host => $host,
58 port => $port,
59 secure => ( $port == 443 ? 1 : 0 )
60 );
61 0 my $result =
62 $hkp->query( index => $email->address, exact => 1, %options );
63 0 ▶ $cv->send($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
64 0 AE::log debug => "no result, next...";
65 }
66 0 AE::log debug => "no success";
67 0 $cv->send(undef);
68 0 };
69 0 $cv->recv;
70 }
71  
72 0 ▶sub _ua { shift->{ua} }
Subroutine _ua: not called CC=1 SCAR=6.9
73 0 ▶sub _uri { shift->{uri} }
Subroutine _uri: not called CC=1 SCAR=6.9
74  
75 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
76 0 my ( $self, %query ) = @_;
77 0 $self->{error} = undef;
78 0 $self->_uri->path('/pks/lookup');
79 0 $self->_uri->query_form(%query);
80  
81 0 my $cv = AE::cv;
82 0 AE::log debug => "GET " . $self->_uri;
83 http_request(
84 GET => $self->_uri,
85 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my ( $body, $hdr ) = @_;
87 0 ▶ if ( $hdr->{Status} ne '200' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{'Status'} ne '200') { } 0 0 0
88 $self->{error} = sprintf 'HTTP %d: %s', $hdr->{Status},
89 0 $hdr->{Reason};
90 0 AE::log error => $self->{error};
91 0 $cv->send;
92 }
93 else {
94 0 $cv->send($body);
95 }
96 }
97 0 );
98 0 $cv->recv;
99 }
100  
101 0 ▶sub _x_www_form_urlencode {
Subroutine _x_www_form_urlencode: not called CC=4 SCAR=30.0
102 0 my $it = natatime 2 => @_;
103 0 my @params;
104 0 ▶ while ( my ( $key, $val ) = $it->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 0 0 0
105 0 ▶ next unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
106 0 ▶ $val ||= '';
Condition0%0/2
$val ||= ""
l!l
00
MC/DC0%0/1
$val ||= ""
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
107 0 push @params => url_encode($key) . '=' . url_encode($val);
108 }
109 0 join '&' => @params;
110 }
111  
112 0 ▶sub _post {
Subroutine _post: not called CC=1 SCAR=6.9
113 0 my ( $self, @query ) = @_;
114 0 $self->{error} = undef;
115 0 $self->_uri->path('/pks/add');
116 0 my $body = _x_www_form_urlencode(@query);
117 0 my $cv = AE::cv;
118 0 AE::log debug => "POST " . $self->_uri;
119 http_request(
120 POST => $self->_uri,
121 body => $body,
122 headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
123 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
124 0 my ( $body, $hdr ) = @_;
125 0 ▶ if ( $hdr->{Status} ne '200' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{'Status'} ne '200') { } 0 0 0
126 $self->{error} = sprintf 'HTTP %d: %s', $hdr->{Status},
127 0 $hdr->{Reason};
128 0 AE::log error => $self->{error};
129 0 $cv->send;
130 }
131 else {
132 0 $cv->send($body);
133 }
134 }
135 0 );
136 0 $cv->recv;
137 }
138  
139 3 ▶sub _parse_mr {
Subroutine _parse_mr: called CC=32 SCAR=37.3
140 3 my ( $self, $lines, $filter_ok ) = @_;
141 3 my $keys = {};
142 3 my $key;
143 3 my ( $keyc, $keyn ) = ( 0, 0 );
144 3 foreach my $line ( split /\r?\n/ => $lines ) {
145 10 ▶ if ( $line =~ /^info:(\d+):(\d+)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^info:(\d+):(\d+)$/) { } 3 7 10
elsif ($line =~ /^pub:([0-9a-f]{8,40}):(\d*):(\d*):(\d*):(\d*):([der]*)$/i) { } 3 4 7
elsif ($line =~ /^uid:([^:]*):(\d*):(\d*):([der]*)$/i) { } 4 0 4
146 3 ▶ croak "unsupported hkp version: v$1" unless $1 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 == 1 0 3 3
147 3 $keyc = $2;
148 }
149 elsif ( $line =~
150 /^pub:([0-9a-f]{8,40}):(\d*):(\d*):(\d*):(\d*):([der]*)$/i )
151 {
152 3 $key = $1;
153 3 $keyn++;
154 3 my ( $algo, $keylen, $created, $expires, $flags, $ok ) =
155 ( $2, $3, $4, $5, $6, undef );
156 3 ▶ $ok = (
Branch: 1 branch
BranchTrueFalseTotal
$created && $created > time || $expires && $expires < time || length $flags ? : 1 2 3
Condition58%7/12
$created and $created > time or $expires and $expires < time or length $flags
l!l&&r!l&&!r
102
$created and $created > time or $expires and $expires < time
l!l&&r!l&&!r
012
$created and $created > time
!ll&&!rl&&r
030
$expires and $expires < time
!ll&&!rl&&r
021
MC/DC20%1/5
$created and $created > time or $expires and $expires < time or length $flags
$created $created > time $expires $expires < time length $flags
Truth table18%2/11
A and B or C and D or E
A= $createdB= $created > timeC= $expiresD= $expires < timeE= length $flags
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
157 (
158 ( $created and $created > time )
159 or ( $expires and $expires < time )
160 or ( length $flags )
161 ) ? 0 : 1
162 );
163 3 ▶ if ( $filter_ok and !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_ok and not $ok) 0 3 3
Condition66%2/3
$filter_ok and not $ok
!ll&&!rl&&r
120
MC/DC0%0/2
$filter_ok and not $ok
$filter_ok not $ok
Truth table66%2/3
A and not(B)
A= $filter_okB= not $ok
ABresult
0X0
100
111
164 0 $key = undef;
165 0 next;
166 }
167 3 ▶ $keys->{$key} = {
Branch: 3 branches
BranchTrueFalseTotal
$flags =~ /r/ ? : 1 2 3
$flags =~ /e/ ? : 1 2 3
$flags =~ /d/ ? : 1 2 3
Condition50%2/4
$created || undef
l!l
30
$expires || undef
l!l
30
MC/DC0%0/1
$created || undef
$created
MC/DC0%0/1
$expires || undef
$expires
Truth table50%1/2
A
A= $created
Aresult
11
00
Truth table50%1/2
A
A= $expires
Aresult
11
00
168 algo => $algo,
169 keylen => $keylen,
170 created => $created || undef,
171 expires => $expires || undef,
172 revoked => ( $flags =~ /r/ ? 1 : 0 ),
173 expired => ( $flags =~ /e/ ? 1 : 0 ),
174 deleted => ( $flags =~ /d/ ? 1 : 0 ),
175 ok => $ok,
176 uids => []
177 };
178 }
179 elsif ( $line =~ /^uid:([^:]*):(\d*):(\d*):([der]*)$/i ) {
180 4 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
181 4 my ( $uid, $created, $expires, $flags, $ok ) =
182 ( $1, $2, $3, $4, undef );
183 4 ▶ $ok = (
Branch: 1 branch
BranchTrueFalseTotal
$created && $created > time || $expires && $expires < time || length $flags ? : 3 1 4
Condition83%10/12
$created and $created > time or $expires and $expires < time or length $flags
l!l&&r!l&&!r
211
$created and $created > time or $expires and $expires < time
l!l&&r!l&&!r
022
$created and $created > time
!ll&&!rl&&r
130
$expires and $expires < time
!ll&&!rl&&r
112
MC/DC20%1/5
$created and $created > time or $expires and $expires < time or length $flags
$created $created > time $expires $expires < time length $flags
Truth table27%3/11
A and B or C and D or E
A= $createdB= $created > timeC= $expiresD= $expires < timeE= length $flags
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
184 (
185 ( $created and $created > time )
186 or ( $expires and $expires < time )
187 or ( length $flags )
188 ) ? 0 : 1
189 );
190 4 ▶ next if $filter_ok and !$ok;
Branch: 1 branch
BranchTrueFalseTotal
if $filter_ok and not $ok 2 2 4
Condition100%3/3
$filter_ok and not $ok
!ll&&!rl&&r
112
MC/DC100%2/2
$filter_ok and not $ok
$filter_ok not $ok
Truth table100%3/3
A and not(B)
A= $filter_okB= not $ok
ABresult
0X0
100
111
191 2 ▶ push @{ $keys->{$key}->{uids} } => {
Branch: 3 branches
BranchTrueFalseTotal
$flags =~ /r/ ? : 1 1 2
$flags =~ /e/ ? : 1 1 2
$flags =~ /d/ ? : 1 1 2
Condition50%2/4
$created || undef
l!l
20
$expires || undef
l!l
20
MC/DC0%0/1
$created || undef
$created
MC/DC0%0/1
$expires || undef
$expires
Truth table50%1/2
A
A= $created
Aresult
11
00
Truth table50%1/2
A
A= $expires
Aresult
11
00
192 uid => uri_unescape($uid),
193 created => $created || undef,
194 expires => $expires || undef,
195 revoked => ( $flags =~ /r/ ? 1 : 0 ),
196 expired => ( $flags =~ /e/ ? 1 : 0 ),
197 deleted => ( $flags =~ /d/ ? 1 : 0 ),
198 ok => $ok
199 };
200 }
201 else {
202 0 carp "unknown line: $line";
203 }
204 }
205 3 ▶ carp "server said there where $keyc keys, but $keyn keys parsed"
Branch: 1 branch
BranchTrueFalseTotal
unless $keyc == $keyn 0 3 3
206 unless $keyc == $keyn;
207 3 return $keys;
208 }
209  
210 0 ▶sub _filter_result {
Subroutine _filter_result: not called CC=4 SCAR=30.0
211 0 my ( $self, $result, $grep ) = @_;
212 0 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
213 0 my $filtered = {};
214 0 foreach my $keyid ( keys %$result ) {
215 my @uids =
216 0 map { $_->[0] }
217 0 grep { $grep->( $_->[1] ) }
218 0 grep { defined $_->[1] }
219 0 map { [ $_, Email::Address->parse( $_->{uid} ) ] }
220 0 @{ $result->{$keyid}->{uids} };
221 0 ▶ next unless @uids;
Branch: 1 branch
BranchTrueFalseTotal
unless @uids 0 0 0
222 0 $filtered->{$keyid} = { %{ $result->{$keyid} }, uids => \@uids };
223 }
224 0 return $filtered;
225 }
226  
227 0 ▶sub query {
Subroutine query: not called CC=12 SCAR=50.5
Pod documented
228 0 my ( $self, $type, $search, %options ) = @_;
229  
230 0 ▶ if ( $type eq 'index' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'index') { } 0 0 0
elsif ($type eq 'get') { } 0 0 0
231 my $message = $self->_get(
232 op => 'index',
233 options => 'mr',
234 search => $search,
235 exact => ( $options{exact} ? 'on' : 'off' ),
236 0 ▶ fingerprint => ( $options{fingerprint} ? 'on' : 'off' ),
Branch: 2 branches
BranchTrueFalseTotal
$options{'exact'} ? : 0 0 0
$options{'fingerprint'} ? : 0 0 0
237 );
238 0 ▶ unless ( defined $message ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $message) 0 0 0
239 0 ▶ return () if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
240 0 return;
241 }
242 0 ▶ my $result = $self->_parse_mr( $message, $options{filter_ok} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$options{'filter_ok'} ? : 0 0 0
243 0 ▶ if ( $options{exact} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"exact"}) 0 0 0
244 $result = $self->_filter_result( $result,
245 0 ▶ sub { shift->address eq $search } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 }
247 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
248 0 return keys %$result;
249 }
250 else {
251 0 return $result;
252 }
253 }
254  
255 elsif ( $type eq 'get' ) {
256 0 ▶ if ( $search !~ /^0x/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($search =~ /^0x/) 0 0 0
257 0 $search = '0x' . $search;
258 }
259 0 my $message =
260 $self->_get( op => 'get', options => 'exact', search => $search );
261 0 ▶ return unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
262 0 return $message;
263 }
264  
265 else {
266 0 confess "unknown query type '$type'";
267 }
268 }
269  
270 0 ▶sub import_keys {
Subroutine import_keys: not called CC=2 SCAR=17.9
Pod documented
271 0 my ( $self, $gnupg, @subjects ) = @_;
272 0 ▶ confess "" unless ref $gnupg eq 'AnyEvent::GnuPG';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gnupg eq "AnyEvent::GnuPG" 0 0 0
273 0 map { $gnupg->import_key($_) }
274 0 grep { defined }
275 0 map { $self->query( get => $_ ) } map { keys %$_ } grep { defined } map {
276 0 scalar $self->query(
277 index => $_,
278 exact => 1,
279 filter_ok => 1,
280 fingerprint => 1
281 )
282 } @subjects;
283 }
284  
285 0 ▶sub submit {
Subroutine submit: not called CC=4 SCAR=30.0
Pod documented
286 0 my ( $self, @keys ) = @_;
287 0 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 0 0
288 0 ▶ if ( ref $keys[0] eq 'AnyEvent::GnuPG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $keys[0] eq 'AnyEvent::GnuPG') { } 0 0 0
289 0 my $gnupg = shift @keys;
290 0 my $output;
291 0 ▶ $gnupg->export_keys(
Branch: 1 branch
BranchTrueFalseTotal
@keys ? : 0 0 0
292 armor => 1,
293 ( @keys ? ( keys => \@keys ) : () ), output => \$output
294 );
295 0 return !!$self->_post( keytext => $output );
296 }
297 else {
298 0 return !!$self->_post( map { ( keytext => $_ ) } @keys );
299 }
300 }
301  
302 0 ▶sub error { shift->{error} }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
303  
304 2 1;
305  
306 __END__
↑ Summary