lib/Net/BitTorrent/Protocol/BEP53.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 60.0% 12 / 20 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 84.2% 91 / 108 CC 21 SCAR 22.2
CC
21
Cov
86%
parse
15.2
to_string
6.4
_decode_base32
4.0
CRAP 9.2
« lib/Net/BitTorrent/Protocol/BEP11.pm ↑ Summary lib/Net/BitTorrent/Protocol/MSE.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #
6 26 class Net::BitTorrent::Protocol::BEP53 v2.1.1 : isa(Net::BitTorrent::Emitter) {
7 26 ▶ use URI::Escape qw[uri_unescape uri_escape];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #
9 26 field $infohash_v1 : reader : param = undef;
10 26 field $infohash_v2 : reader : param = undef;
11 26 field $trackers : reader : param = [];
12 26 field $name : reader : param = undef;
13 26 field $nodes : reader : param = []; # DHT bootstrap nodes (x.pe)
14  
15 #
16 15 ▶ sub parse ( $class, $uri ) {
Subroutine parse: called CC=13 SCAR=27.2
Pod documented
17 15 ▶ die 'Not a magnet URI' unless $uri =~ /^magnet:\?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ /^magnet:\?/u 0 15 15
18 15 my %params;
19 15 my $query = substr( $uri, 8 );
20 15 for my $pair ( split( /[&;]/, $query ) ) {
21 21 my ( $key, $val ) = split( /=/, $pair, 2 );
22 21 ▶ next unless defined $key && defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $val 0 21 21
Condition33%1/3
defined $key and defined $val
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $key and defined $val
defined $key defined $val
Truth table33%1/3
A and B
A= defined $keyB= defined $val
ABresult
0X0
100
111
23 21 $val = uri_unescape($val);
24 21 push @{ $params{$key} }, $val;
25 }
26 15 my ( $v1, $v2 );
27 15 ▶ for my $xt ( @{ $params{xt} // [] } ) {
Condition50%1/2
$params{"xt"} // []
l!l
150
MC/DC0%0/1
$params{"xt"} // []
$params{"xt"}
Truth table50%1/2
A
A= $params{"xt"}
Aresult
11
00
28 17 ▶ if ( $xt =~ /^urn:btih:([a-fA-F0-9]{40})$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($xt =~ /^urn:btih:([a-fA-F0-9]{40})$/u) { } 14 3 17
elsif ($xt =~ /^urn:btih:([a-zA-Z2-7]{32})$/u) { } 0 3 3
elsif ($xt =~ /^urn:btmh:1220([a-fA-F0-9]{64})$/u) { } 3 0 3
29 14 $v1 = pack( 'H*', $1 );
30 }
31 elsif ( $xt =~ /^urn:btih:([a-zA-Z2-7]{32})$/ ) {
32  
33 # Base32 encoded (v1)
34 0 $v1 = _decode_base32($1);
35 }
36 elsif ( $xt =~ /^urn:btmh:1220([a-fA-F0-9]{64})$/ ) {
37  
38 # BEP 53 v2 (multihash SHA-256)
39 3 $v2 = pack( 'H*', $1 );
40 }
41 }
42 return $class->new(
43 infohash_v1 => $v1,
44 infohash_v2 => $v2,
45 trackers => ( $params{tr} // [] ),
46 name => ( $params{dn}[0] // undef ),
47 15 ▶ nodes => ( $params{'x.pe'} // [] ),
Condition83%5/6
$params{"tr"} // []
l!l
015
$params{"dn"}[0] // undef
l!l
213
$params{"x.pe"} // []
l!l
114
MC/DC0%0/1
$params{"tr"} // []
$params{"tr"}
MC/DC100%1/1
$params{"dn"}[0] // undef
$params{"dn"}[0]
MC/DC100%1/1
$params{"x.pe"} // []
$params{"x.pe"}
Truth table50%1/2
A
A= $params{"tr"}
Aresult
11
00
Truth table100%2/2
A
A= $params{"dn"}[0]
Aresult
11
00
Truth table100%2/2
A
A= $params{"x.pe"}
Aresult
11
00
48 );
49 }
50  
51 3 ▶ sub _decode_base32 ($str) {
Subroutine _decode_base32: called CC=4 SCAR=13.9
52 3 $str = uc($str);
53 3 my $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
54 3 my %map;
55 3 @map{ split //, $alphabet } = 0 .. 31;
56 3 my $buffer = 0;
57 3 my $bits = 0;
58 3 my $res = '';
59 3 for my $char ( split //, $str ) {
60 56 ▶ next unless exists $map{$char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map{$char} 0 56 56
61 56 $buffer = ( $buffer << 5 ) | $map{$char};
62 56 $bits += 5;
63 56 ▶ if ( $bits >= 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 8) 35 21 56
64 35 $bits -= 8;
65 35 $res .= chr( ( $buffer >> $bits ) & 0xFF );
66 35 $buffer &= ( 1 << $bits ) - 1;
67 }
68 }
69 3 return $res;
70 }
71  
72 2 ▶ method to_string () {
Subroutine to_string: called CC=6 SCAR=18.6
Pod documented
73 2 my @pairs;
74 2 ▶ push @pairs, 'xt=urn:btih:' . unpack( 'H*', $infohash_v1 ) if $infohash_v1;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v1 0 2 2
75 2 ▶ push @pairs, 'xt=urn:btmh:1220' . unpack( 'H*', $infohash_v2 ) if $infohash_v2;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v2 2 0 2
76 2 ▶ push @pairs, 'dn=' . uri_escape($name) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 2 0 2
77 2 push @pairs, 'tr=' . uri_escape($_) for @$trackers;
78 2 push @pairs, 'x.pe=' . uri_escape($_) for @$nodes;
79 2 'magnet:?' . join( '&', @pairs );
80 }
81 };
82 #
83 26 1;
« lib/Net/BitTorrent/Protocol/BEP11.pm ↑ Summary lib/Net/BitTorrent/Protocol/MSE.pm »