lib/Net/BitTorrent/Torrent.pm

filter: Statementstmt 80.5% 727 / 903 Branchbran 60.9% 333 / 546 Conditioncond 50.5% 153 / 303 MC/DCmcdc 27.4% 48 / 175 Subroutinesub 78.8% 82 / 104 Podpod 57.8% 33 / 57 totaltotal 65.9% 1376 / 2088 SCAR 86.5
CC
425
Cov
69%
get_superseed_piece
156.0
_init_from_metadata
66.6
name
42.0
CRAP 5684.5
« lib/Net/BitTorrent.pm ↑ Summary lib/Net/BitTorrent/DHT.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class', 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 class Net::BitTorrent::Torrent v2.1.0 : isa(Net::BitTorrent::Emitter) {
6 26 ▶ use Net::BitTorrent::Protocol::BEP03::Bencode qw[bencode bdecode];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶ use Net::BitTorrent::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶ use Net::BitTorrent::Tracker;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶ use Acme::Bitfield;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶ use Net::BitTorrent::Torrent::PiecePicker;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶ use Net::BitTorrent::Tracker::WebSeed;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶ use Digest::SHA qw[sha1 sha256];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶ use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 26 ▶ use Net::BitTorrent::Types qw[:state :pick];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 26 ▶ use Algorithm::RateLimiter::TokenBucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Security limits
20 26 ▶ use constant MAX_METADATA_SIZE => 10 * 1024 * 1024; # 10 MB which would be... massive
Subroutine BEGIN: called CC=1 SCAR=0.0
21 26 ▶ use constant MAX_FILE_TREE_DEPTH => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 26 ▶ use constant MAX_BLOCK_CACHE => 32; # Max cached incomplete pieces per torrent (~8MB worst case)
Subroutine BEGIN: called CC=1 SCAR=0.0
23 26 ▶ use constant MAX_PEERS => 10_000; # Max discovered peers per torrent
Subroutine BEGIN: called CC=1 SCAR=0.0
24 26 ▶ use constant MAX_ATTEMPTED => 5000; # Max attempted connection entries
Subroutine BEGIN: called CC=1 SCAR=0.0
25 26 ▶ use constant ENDGAME_STALL_TIMEOUT => 60; # Seconds without piece verification before endgame fallback
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #
28 26 field $path : param = undef;
29 26 field $base_path : param;
30 26 field $client : param : reader;
31 26 field $metadata : reader = undef;
32 26 field $infohash : param = undef;
33 26 field $infohash_v1 : writer : param = undef;
34 26 field $infohash_v2 : param = undef;
35 26 field $initial_trackers : param = [];
36 26 field $initial_peers : param = [];
37 26 field $storage : reader;
38 26 field $bitfield : reader;
39 26 field $picker : reader;
40 26 field $tracker_manager;
41 26 field $features : reader;
42 26 field $peer_id : param = undef;
43 26 field %peers; # 'ip:port' => { ip => ..., port => ... }
44 26 field %peer_objects;
45 14 ▶ method peer_objects () { [ values %peer_objects ] }
Subroutine peer_objects: called CC=1 SCAR=0.0
Pod undocumented
46 2 ▶ method peer_objects_hash () { \%peer_objects }
Subroutine peer_objects_hash: called CC=1 SCAR=0.0
Pod undocumented
47 26 field %peer_bitfields; # Peer object => Bitfield object
48 294 ▶ method peer_bitfields () { \%peer_bitfields }
Subroutine peer_bitfields: called CC=1 SCAR=0.0
Pod undocumented
49 26 field %blocks_pending; # piece_index => { offset => Peer }
50 1 ▶ method blocks_pending () { \%blocks_pending }
Subroutine blocks_pending: called CC=1 SCAR=0.0
Pod undocumented
51 26 field %blocks_received; # piece_index => { offset => 1 }
52 34 ▶ method blocks_received () { \%blocks_received }
Subroutine blocks_received: called CC=1 SCAR=0.0
Pod undocumented
53 26 field %block_sources; # piece_index => { offset => Peer }
54 26 field $is_private : reader;
55 26 field $dht_nodes;
56 26 field $last_piece_verified_at = 0; # Timestamp of last successful piece verification (for endgame stall detection)
57 26 field %test_data; # For simulation
58 26 field %block_cache; # piece_index => { offset => data }
59 26 field $bytes_downloaded = 0;
60 26 field $bytes_uploaded = 0;
61 26 field $bytes_left = 0;
62 26 field @piece_priorities;
63 26 field $picking_strategy = PICK_RAREST_FIRST;
64 26 field $is_partial_seed : reader : writer(set_partial_seed) = 0;
65 26 field $is_superseed : reader : writer(set_superseed) = 0;
66 26 field %superseed_offers; # Peer object => piece_index
67 26 field $debug : param = 0;
68 26 field $max_peers : param : reader : writer = 100;
69 #
70 ▶ method get_superseed_piece ($peer) {
Subroutine get_superseed_piece: not called CC=12 SCAR=50.5
Pod documented
71 0 ▶ return undef unless $is_superseed;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_superseed 0 0 0
72 0 ▶ if ( exists $superseed_offers{$peer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $superseed_offers{$peer}) 0 0 0
73 0 my $offered = $superseed_offers{$peer};
74 0 my $shared = 0;
75 0 for my $p ( values %peer_objects ) {
76 0 ▶ next if $p == $peer;
Branch: 1 branch
BranchTrueFalseTotal
if $p == $peer 0 0 0
77 0 ▶ if ( $peer_bitfields{$p} && $peer_bitfields{$p}->get($offered) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer_bitfields{$p} and $peer_bitfields{$p}->get($offered)) 0 0 0
Condition0%0/3
$peer_bitfields{$p} and $peer_bitfields{$p}->get($offered)
!ll&&!rl&&r
000
MC/DC0%0/2
$peer_bitfields{$p} and $peer_bitfields{$p}->get($offered)
$peer_bitfields{$p} $peer_bitfields{$p}->get($offered)
Truth table0%0/3
A and B
A= $peer_bitfields{$p}B= $peer_bitfields{$p}->get($offered)
ABresult
0X0
100
111
78 0 $shared = 1;
79 0 last;
80 }
81 }
82 0 ▶ return $offered unless $shared;
Branch: 1 branch
BranchTrueFalseTotal
unless $shared 0 0 0
83 0 delete $superseed_offers{$peer};
84 }
85 0 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 0 0 0
86 0 ▶ next unless $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield->get($i) 0 0 0
87 0 my $avail = $picker->get_availability($i);
88 0 ▶ if ( defined $avail && $avail == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $avail and $avail == 0) 0 0 0
Condition0%0/3
defined $avail and $avail == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $avail and $avail == 0
defined $avail $avail == 0
Truth table0%0/3
A and B
A= defined $availB= $avail == 0
ABresult
0X0
100
111
89 0 $superseed_offers{$peer} = $i;
90 0 return $i;
91 }
92 }
93 0 return undef;
94 }
95 26 field @webseeds;
96 26 field $limit_up : reader;
97 26 field $limit_down : reader;
98 26 field $choke_timer = 0;
99 26 field $optimistic_timer = 0;
100 26 field $optimistic_unchoke_peer = undef;
101 26 field $state : reader = STATE_STOPPED; # STOPPED, STARTING, RUNNING, PAUSED, METADATA
102  
103 # PEX (BEP 11) logic
104 26 field $pex_timer = 0;
105 26 field $tracker_timer = 0;
106 26 field $dht_lookup_timer = 0;
107 26 field %pex_added; # ip:port => { ip, port }
108 26 field %pex_dropped; # ip:port => { ip, port }
109  
110 # Magnet/Metadata fetching
111 26 field %metadata_pieces;
112 26 field $metadata_size : reader = 0;
113  
114 ▶ method metadata_received_count () {
Subroutine metadata_received_count: not called CC=2 SCAR=17.9
Pod documented
115 0 my $total = 0;
116 0 $total += length($_) for values %metadata_pieces;
117 0 return $total;
118 }
119  
120 # DHT Search Frontier
121 26 field %dht_frontier; # node_id_hex => { id, ip, port, visited }
122  
123 # Swarm stats from DHT (BEP 33)
124 26 field $dht_seeders : reader = 0;
125 26 field $dht_leechers : reader = 0;
126 0 ▶ method set_limit_up ($val) { $limit_up->set_limit($val) }
Subroutine set_limit_up: not called CC=1 SCAR=6.9
Pod documented
127 0 ▶ method set_limit_down ($val) { $limit_down->set_limit($val) }
Subroutine set_limit_down: not called CC=1 SCAR=6.9
Pod documented
128  
129 ▶ method can_read ($amount) {
Subroutine can_read: called CC=1 SCAR=0.0
Pod documented
130 22 my $allowed = $client->limit_down->consume($amount);
131 22 $allowed = $limit_down->consume($allowed);
132 22 return $allowed;
133 }
134  
135 ▶ method can_write ($amount) {
Subroutine can_write: called CC=1 SCAR=0.0
Pod documented
136 27 my $allowed = $client->limit_up->consume($amount);
137 27 $allowed = $limit_up->consume($allowed);
138 27 return $allowed;
139 }
140  
141 ▶ method is_finished () {
Subroutine is_finished: called CC=3 SCAR=18.4
Pod documented
142 1 ▶ return 0 unless $self->is_metadata_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_metadata_complete 1 0 1
143 0 ▶ return 0 if $state == STATE_METADATA;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 4 0 0 0
144 0 return $self->is_seed;
145 }
146  
147 ▶ method is_seed () {
Subroutine is_seed: not called CC=2 SCAR=17.9
Pod documented
148 0 ▶ return $bitfield && $bitfield->count == $bitfield->size;
Condition0%0/3
$bitfield && $bitfield->count == $bitfield->size
!ll&&!rl&&r
000
MC/DC0%0/2
$bitfield && $bitfield->count == $bitfield->size
$bitfield $bitfield->count == $bitfield->size
Truth table0%0/3
A && B
A= $bitfieldB= $bitfield->count == $bitfield->size
ABresult
0X0
100
111
149 }
150  
151 ▶ method is_metadata_complete () {
Subroutine is_metadata_complete: called CC=2 SCAR=7.6
Pod documented
152 3 ▶ return defined $metadata ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $metadata ? : 0 3 3
153 }
154  
155 ▶ method is_running () {
Subroutine is_running: not called CC=3 SCAR=24.8
Pod documented
156 0 ▶ return $state == STATE_RUNNING || $state == STATE_STARTING || $state == STATE_METADATA;
Condition0%0/6
$state == 2 || $state == 1 || $state == 4
l!l&&r!l&&!r
000
$state == 2 or $state == 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$state == 2 || $state == 1 || $state == 4
$state == 2 $state == 1 $state == 4
Truth table0%0/4
A or B || C
A= $state == 2B= $state == 1C= $state == 4
ABCresult
1XX1
01X1
0011
0000
157 }
158  
159 ▶ method name () {
Subroutine name: not called CC=6 SCAR=37.4
Pod documented
160 0 ▶ return $metadata->{info}{name} if $metadata && $metadata->{info} && $metadata->{info}{name};
Branch: 1 branch
BranchTrueFalseTotal
if $metadata and $metadata->{"info"} and $metadata->{"info"}{"name"} 0 0 0
Condition0%0/6
$metadata and $metadata->{"info"} and $metadata->{"info"}{"name"}
!ll&&!rl&&r
000
$metadata and $metadata->{"info"}
!ll&&!rl&&r
000
MC/DC0%0/3
$metadata and $metadata->{"info"} and $metadata->{"info"}{"name"}
$metadata $metadata->{"info"} $metadata->{"info"}{"name"}
Truth table0%0/4
A and B and C
A= $metadataB= $metadata->{"info"}C= $metadata->{"info"}{"name"}
ABCresult
0XX0
10X0
1100
1111
161 0 ▶ return unpack( 'H*', $self->infohash_v1 // $self->infohash_v2 // '' );
Condition0%0/5
($self->infohash_v1 // $self->infohash_v2) // ""
l!l
00
$self->infohash_v1 // $self->infohash_v2
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->infohash_v1 // $self->infohash_v2) // ""
$self->infohash_v1 $self->infohash_v2
Truth table0%0/3
A // B
A= $self->infohash_v1B= $self->infohash_v2
ABresult
1X1
011
000
162 }
163  
164 ▶ method progress () {
Subroutine progress: called CC=5 SCAR=30.0
Pod documented
165 1 ▶ return 0 unless $self->is_metadata_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_metadata_complete 1 0 1
166 0 ▶ return 0 if $state == STATE_METADATA;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 4 0 0 0
167 0 ▶ return ( ( $bitfield->count / $bitfield->size ) * 100 ) if $bitfield && $bitfield->size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield and $bitfield->size > 0 0 0 0
Condition0%0/3
$bitfield and $bitfield->size > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$bitfield and $bitfield->size > 0
$bitfield $bitfield->size > 0
Truth table0%0/3
A and B
A= $bitfieldB= $bitfield->size > 0
ABresult
0X0
100
111
168 0 return 0;
169 }
170  
171 ▶ method start () {
Subroutine start: called CC=8 SCAR=21.6
Pod documented
172 50 ▶ return if $state != STATE_STOPPED;
Branch: 1 branch
BranchTrueFalseTotal
if $state != 0 1 49 50
173 49 $state = STATE_STARTING;
174 49 $self->announce('started');
175 49 ▶ $self->start_dht_lookup() unless $is_private;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private 49 0 49
176  
177 # BEP 33: Scraping
178 49 ▶ if ( !$is_private && $client->dht ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_private and $client->dht) 46 3 49
Condition66%2/3
not $is_private and $client->dht
!ll&&!rl&&r
0346
MC/DC50%1/2
not $is_private and $client->dht
not $is_private $client->dht
Truth table66%2/3
not(A) and B
A= not $is_privateB= $client->dht
ABresult
0X0
100
111
179 46 my $weak_self = $self;
180 46 builtin::weaken($weak_self);
181 ▶ $client->dht_scrape(
Subroutine __ANON__: not called CC=2 SCAR=17.9
182 $infohash_v2 || $infohash_v1,
183 sub ( $emitter, $res ) {
184 0 ▶ $weak_self->handle_dht_scrape($res) if $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
if $weak_self 0 0 0
185 }
186 46 ▶ );
Condition66%2/3
$infohash_v2 || $infohash_v1
l!l&&r!l&&!r
35110
MC/DC0%0/2
$infohash_v2 || $infohash_v1
$infohash_v2 $infohash_v1
Truth table66%2/3
A || B
A= $infohash_v2B= $infohash_v1
ABresult
1X1
011
000
187 }
188 49 ▶ if ( !$metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $metadata) { } 5 44 49
189 5 $state = STATE_METADATA;
190 5 ▶ $self->_emit_log( 'debug', 'Torrent starting in METADATA mode' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
191 }
192 else {
193 44 $state = STATE_RUNNING;
194 44 $self->_emit('started');
195 }
196 }
197  
198 ▶ method stop () {
Subroutine stop: not called CC=4 SCAR=30.0
Pod documented
199 0 ▶ return if $state == STATE_STOPPED;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 0 0 0 0
200 0 $state = STATE_STOPPED;
201 0 ▶ $storage->explicit_flush() if $storage;
Branch: 1 branch
BranchTrueFalseTotal
if $storage 0 0 0
202 0 $self->announce('stopped');
203 0 for my $peer ( grep {defined} values %peer_objects ) {
204 0 $peer->disconnected();
205 }
206 0 %peer_objects = ();
207 0 %peers = ();
208 0 $self->_emit('stopped');
209 }
210  
211 ▶ method pause () {
Subroutine pause: not called CC=3 SCAR=24.8
Pod documented
212 0 ▶ return if $state != STATE_RUNNING && $state != STATE_METADATA;
Branch: 1 branch
BranchTrueFalseTotal
if $state != 2 and $state != 4 0 0 0
Condition0%0/3
$state != 2 and $state != 4
!ll&&!rl&&r
000
MC/DC0%0/2
$state != 2 and $state != 4
$state != 2 $state != 4
Truth table0%0/3
A and B
A= $state != 2B= $state != 4
ABresult
0X0
100
111
213 0 $state = STATE_PAUSED;
214 0 $self->_emit('paused');
215 }
216  
217 ▶ method resume () {
Subroutine resume: not called CC=2 SCAR=17.9
Pod documented
218 0 ▶ return if $state != STATE_PAUSED;
Branch: 1 branch
BranchTrueFalseTotal
if $state != 3 0 0 0
219 0 $state = STATE_RUNNING;
220 0 $self->_emit('resumed');
221 }
222 ADJUST {
223 102 ▶ $self->set_parent_emitter($client);
Subroutine __ANON__: called CC=20 SCAR=33.0
224 102 ▶ $self->_emit_log( 'debug',
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 101 102
Condition50%4/8
$path // "undef"
l!l
01
$infohash // "undef"
l!l
01
$infohash_v1 // "undef"
l!l
10
$infohash_v2 // "undef"
l!l
01
MC/DC0%0/1
$path // "undef"
$path
MC/DC0%0/1
$infohash // "undef"
$infohash
MC/DC0%0/1
$infohash_v1 // "undef"
$infohash_v1
MC/DC0%0/1
$infohash_v2 // "undef"
$infohash_v2
Truth table50%1/2
A
A= $path
Aresult
11
00
Truth table50%1/2
A
A= $infohash
Aresult
11
00
Truth table50%1/2
A
A= $infohash_v1
Aresult
11
00
Truth table50%1/2
A
A= $infohash_v2
Aresult
11
00
225 'Torrent::ADJUST path=' .
226 ( $path // 'undef' ) . ' ih=' .
227 ( $infohash // 'undef' ) . ' v1=' .
228 ( $infohash_v1 // 'undef' ) . ' v2=' .
229 ( $infohash_v2 // 'undef' ) )
230 if $debug;
231 102 ▶ builtin::weaken($client) if defined $client;
Branch: 1 branch
BranchTrueFalseTotal
if defined $client 102 0 102
232 102 ▶ $features = { %{ $client->features // {} } };
Condition50%1/2
$client->features // {}
l!l
1020
MC/DC0%0/1
$client->features // {}
$client->features
Truth table50%1/2
A
A= $client->features
Aresult
11
00
233 102 ▶ $peer_id //= $client->node_id;
Condition66%2/3
$peer_id //= $client->node_id
l!l&&r!l&&!r
84180
MC/DC0%0/2
$peer_id //= $client->node_id
$peer_id $client->node_id
Truth table66%2/3
A //= B
A= $peer_idB= $client->node_id
ABresult
1X1
011
000
234 102 $limit_up = Algorithm::RateLimiter::TokenBucket->new( limit => 0 );
235 102 $limit_down = Algorithm::RateLimiter::TokenBucket->new( limit => 0 );
236 102 ▶ if ($path) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path) { } 80 22 102
elsif ($infohash or $infohash_v1 or $infohash_v2) { } 22 0 22
237 80 my $data = path($path)->slurp_raw;
238 80 $metadata = bdecode($data);
239 78 ▶ $self->_emit_log( 'error', 'Missing info dictionary' ) unless ref $metadata eq 'HASH' && ref $metadata->{info} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $metadata eq "HASH" and ref $metadata->{"info"} eq "HASH" 2 76 78
Condition66%2/3
ref $metadata eq "HASH" and ref $metadata->{"info"} eq "HASH"
!ll&&!rl&&r
0276
MC/DC50%1/2
ref $metadata eq "HASH" and ref $metadata->{"info"} eq "HASH"
ref $metadata eq "HASH" ref $metadata->{"info"} eq "HASH"
Truth table66%2/3
A and B
A= ref $metadata eq "HASH"B= ref $metadata->{"info"} eq "HASH"
ABresult
0X0
100
111
240 78 $self->_init_from_metadata();
241 }
242 ▶ elsif ( $infohash || $infohash_v1 || $infohash_v2 ) {
Condition50%3/6
$infohash or $infohash_v1 or $infohash_v2
l!l&&r!l&&!r
2200
$infohash or $infohash_v1
l!l&&r!l&&!r
9130
MC/DC0%0/3
$infohash or $infohash_v1 or $infohash_v2
$infohash $infohash_v1 $infohash_v2
Truth table50%2/4
A or B or C
A= $infohashB= $infohash_v1C= $infohash_v2
ABCresult
1XX1
01X1
0011
0000
243 22 ▶ if ($infohash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infohash) 9 13 22
244 9 ▶ if ( length($infohash) == 20 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $infohash == 20) { } 8 1 9
elsif (length $infohash == 32) { } 0 1 1
245 8 $infohash_v1 = $infohash;
246 }
247 elsif ( length($infohash) == 32 ) {
248 0 $infohash_v2 = $infohash;
249 }
250 else {
251 1 $self->_emit_log( 'error', 'Invalid infohash length' );
252 }
253 }
254 22 my @tiers = map { [$_] } @$initial_trackers;
255 22 $tracker_manager = Net::BitTorrent::Tracker->new( tiers_raw => \@tiers, debug => $debug );
256 22 for my $p_str (@$initial_peers) {
257 2 ▶ if ( $p_str =~ /^([^:]+):(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_str =~ /^([^:]+):(\d+)$/u) 2 0 2
258 2 $self->add_peer( { ip => $1, port => $2 } );
259 }
260 }
261 }
262 else {
263 0 $self->_emit_log( 'error', 'Either path or infohash required' );
264 }
265 }
266  
267 49 ▶ method _validate_file_tree ( $tree, $depth = 0 ) {
Subroutine _validate_file_tree: called CC=14 SCAR=31.7
268 49 ▶ if ( $depth > MAX_FILE_TREE_DEPTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 128) 0 49 49
269 0 $self->_emit_log( 'error', 'File tree depth limit exceeded (max ' . MAX_FILE_TREE_DEPTH . ' levels)' );
270 0 return;
271 }
272 49 ▶ if ( ref $tree ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree ne "HASH") 0 49 49
273 0 $self->_emit_log( 'error', 'Invalid file tree' );
274 0 return;
275 }
276 49 for my $name ( keys %$tree ) {
277 57 ▶ if ( $name eq '' || $name eq '.' || $name eq '..' || $name =~ /[\\\/]/ || $name =~ /\0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "" or $name eq "." or $name eq ".." or $name =~ m[[\\/]]u or $name =~ /\0/u) 1 56 57
Condition41%5/12
$name eq "" or $name eq "." or $name eq ".." or $name =~ m[[\\/]]u or $name =~ /\0/u
l!l&&r!l&&!r
0156
$name eq "" or $name eq "." or $name eq ".." or $name =~ m[[\\/]]u
l!l&&r!l&&!r
0057
$name eq "" or $name eq "." or $name eq ".."
l!l&&r!l&&!r
0057
$name eq "" or $name eq "."
l!l&&r!l&&!r
0057
MC/DC20%1/5
$name eq "" or $name eq "." or $name eq ".." or $name =~ m[[\\/]]u or $name =~ /\0/u
$name eq "" $name eq "." $name eq ".." $name =~ m[[\\/]]u $name =~ /\0/u
Truth table33%2/6
A or B or C or D or E
A= $name eq ""B= $name eq "."C= $name eq ".."D= $name =~ m[[\\/]]uE= $name =~ /\0/u
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
278 1 $self->_emit_log( 'error', 'Invalid path element' );
279 1 return;
280 }
281 56 my $node = $tree->{$name};
282 56 ▶ if ( exists $node->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{''}) { } 54 1 55
283 54 ▶ if ( ref $node->{''} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{""} ne "HASH") 0 54 54
284 0 $self->_emit_log( 'error', 'Invalid file metadata' );
285 0 return;
286 }
287 54 ▶ if ( ( $node->{''}{length} // -1 ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($node->{""}{"length"} // -1) < 0) 1 53 54
Condition50%1/2
$node->{""}{"length"} // -1
l!l
540
MC/DC0%0/1
$node->{""}{"length"} // -1
$node->{""}{"length"}
Truth table50%1/2
A
A= $node->{""}{"length"}
Aresult
11
00
288 1 $self->_emit_log( 'error', 'Invalid file length' );
289 1 return;
290 }
291 }
292 else {
293 1 $self->_validate_file_tree( $node, $depth + 1 );
294 }
295 }
296 }
297  
298 ▶ method _init_from_metadata () {
Subroutine _init_from_metadata: called CC=47 SCAR=42.0
299 79 ▶ if ( !$metadata || ref $metadata->{info} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $metadata or ref $metadata->{"info"} ne "HASH") 2 77 79
Condition66%2/3
not $metadata or ref $metadata->{"info"} ne "HASH"
l!l&&r!l&&!r
0277
MC/DC50%1/2
not $metadata or ref $metadata->{"info"} ne "HASH"
not $metadata ref $metadata->{"info"} ne "HASH"
Truth table66%2/3
not(A) or B
A= not $metadataB= ref $metadata->{"info"} ne "HASH"
ABresult
1X1
011
000
300 2 $self->_emit_log( 'error', 'Missing info dictionary' );
301 2 return;
302 }
303 77 my $info = $metadata->{info};
304 77 ▶ if ( ( $info->{'piece length'} // 0 ) <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($info->{"piece length"} // 0) <= 0) 1 76 77
Condition50%1/2
$info->{"piece length"} // 0
l!l
770
MC/DC0%0/1
$info->{"piece length"} // 0
$info->{"piece length"}
Truth table50%1/2
A
A= $info->{"piece length"}
Aresult
11
00
305 1 $self->_emit_log( 'error', 'Invalid piece length' );
306 1 return;
307 }
308 76 ▶ if ( !defined $info->{name} || !length $info->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info->{"name"} and length $info->{"name"}) 1 75 76
Condition66%2/3
defined $info->{"name"} and length $info->{"name"}
!ll&&!rl&&r
1075
MC/DC50%1/2
defined $info->{"name"} and length $info->{"name"}
defined $info->{"name"} length $info->{"name"}
Truth table66%2/3
A and B
A= defined $info->{"name"}B= length $info->{"name"}
ABresult
0X0
100
111
309 1 $self->_emit_log( 'error', 'Missing name' );
310 1 return;
311 }
312 75 ▶ if ( $info->{name} =~ /[\\\/]/ || $info->{name} eq '..' || $info->{name} =~ /\0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"name"} =~ m[[\\/]]u or $info->{"name"} eq ".." or $info->{"name"} =~ /\0/u) 2 67 69
Condition100%6/6
$info->{"name"} =~ m[[\\/]]u or $info->{"name"} eq ".." or $info->{"name"} =~ /\0/u
l!l&&r!l&&!r
7167
$info->{"name"} =~ m[[\\/]]u or $info->{"name"} eq ".."
l!l&&r!l&&!r
6168
MC/DC100%3/3
$info->{"name"} =~ m[[\\/]]u or $info->{"name"} eq ".." or $info->{"name"} =~ /\0/u
$info->{"name"} =~ m[[\\/]]u $info->{"name"} eq ".." $info->{"name"} =~ /\0/u
Truth table100%4/4
A or B or C
A= $info->{"name"} =~ m[[\\/]]uB= $info->{"name"} eq ".."C= $info->{"name"} =~ /\0/u
ABCresult
1XX1
01X1
0011
0000
313 8 $self->_emit_log( 'error', 'Invalid name: path traversal characters detected' );
314 8 return;
315 }
316 67 require File::Spec;
317 67 ▶ if ( File::Spec->file_name_is_absolute( $info->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($info->{"name"})) 0 67 67
318 0 $self->_emit_log( 'error', 'Invalid name: absolute path' );
319 0 return;
320 }
321 67 ▶ if ( !$info->{pieces} && !$info->{'file tree'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"pieces"} or $info->{"file tree"}) 0 67 67
Condition66%2/3
$info->{"pieces"} or $info->{"file tree"}
l!l&&r!l&&!r
6520
MC/DC0%0/2
$info->{"pieces"} or $info->{"file tree"}
$info->{"pieces"} $info->{"file tree"}
Truth table66%2/3
A or B
A= $info->{"pieces"}B= $info->{"file tree"}
ABresult
1X1
011
000
322 0 $self->_emit_log( 'error', 'Torrent must have either \'pieces\' (v1) or \'file tree\' (v2)' );
323 0 return;
324 }
325  
326 # Validate file sizes and paths
327 67 ▶ if ( $info->{'file tree'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'file tree'}) { } 48 19 67
elsif ($info->{'files'}) { } 5 14 19
328 48 $self->_validate_file_tree( $info->{'file tree'} );
329 }
330 elsif ( $info->{files} ) {
331 5 ▶ if ( ref $info->{files} ne 'ARRAY' || !@{ $info->{files} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info->{"files"} ne "ARRAY" or not @{$info->{"files"};}) 1 4 5
Condition66%2/3
ref $info->{"files"} ne "ARRAY" or not @{$info->{"files"};}
l!l&&r!l&&!r
014
MC/DC50%1/2
ref $info->{"files"} ne "ARRAY" or not @{$info->{"files"};}
ref $info->{"files"} ne "ARRAY" not @{$info->{"files"};}
Truth table66%2/3
A or not(B)
A= ref $info->{"files"} ne "ARRAY"B= not @{$info->{"files"};}
ABresult
1X1
011
000
332 1 $self->_emit_log( 'error', 'Invalid files list' );
333 1 return;
334 }
335 4 for my $f ( @{ $info->{files} } ) {
336 6 ▶ if ( ( $f->{length} // -1 ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($f->{"length"} // -1) < 0) 0 6 6
Condition50%1/2
$f->{"length"} // -1
l!l
60
MC/DC0%0/1
$f->{"length"} // -1
$f->{"length"}
Truth table50%1/2
A
A= $f->{"length"}
Aresult
11
00
337 0 $self->_emit_log( 'error', 'Invalid file length' );
338 0 return;
339 }
340 6 ▶ if ( ref $f->{path} ne 'ARRAY' || !@{ $f->{path} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f->{"path"} ne "ARRAY" or not @{$f->{"path"};}) 1 5 6
Condition66%2/3
ref $f->{"path"} ne "ARRAY" or not @{$f->{"path"};}
l!l&&r!l&&!r
105
MC/DC50%1/2
ref $f->{"path"} ne "ARRAY" or not @{$f->{"path"};}
ref $f->{"path"} ne "ARRAY" not @{$f->{"path"};}
Truth table66%2/3
A or not(B)
A= ref $f->{"path"} ne "ARRAY"B= not @{$f->{"path"};}
ABresult
1X1
011
000
341 1 $self->_emit_log( 'error', 'Missing path' );
342 1 return;
343 }
344 5 for my $p ( @{ $f->{path} } ) {
345 5 ▶ if ( $p eq '' || $p eq '.' || $p eq '..' || $p =~ /[\\\/]/ || $p =~ /\0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq "" or $p eq "." or $p eq ".." or $p =~ m[[\\/]]u or $p =~ /\0/u) 3 2 5
Condition58%7/12
$p eq "" or $p eq "." or $p eq ".." or $p =~ m[[\\/]]u or $p =~ /\0/u
l!l&&r!l&&!r
212
$p eq "" or $p eq "." or $p eq ".." or $p =~ m[[\\/]]u
l!l&&r!l&&!r
023
$p eq "" or $p eq "." or $p eq ".."
l!l&&r!l&&!r
005
$p eq "" or $p eq "."
l!l&&r!l&&!r
005
MC/DC40%2/5
$p eq "" or $p eq "." or $p eq ".." or $p =~ m[[\\/]]u or $p =~ /\0/u
$p eq "" $p eq "." $p eq ".." $p =~ m[[\\/]]u $p =~ /\0/u
Truth table50%3/6
A or B or C or D or E
A= $p eq ""B= $p eq "."C= $p eq ".."D= $p =~ m[[\\/]]uE= $p =~ /\0/u
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
346 3 $self->_emit_log( 'error', 'Invalid path element' );
347 3 return;
348 }
349 }
350 }
351 }
352 else {
353 # Single-file v1 or v2 (v2 length is inside 'file tree', handled above)
354 14 ▶ if ( !exists $info->{length} && $info->{pieces} && !$info->{'file tree'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $info->{'length'} and $info->{'pieces'} and not $info->{'file tree'}) { } 4 10 14
Condition66%4/6
not exists $info->{"length"} and $info->{"pieces"} and not $info->{"file tree"}
!ll&&!rl&&r
1004
not exists $info->{"length"} and $info->{"pieces"}
!ll&&!rl&&r
1004
MC/DC33%1/3
not exists $info->{"length"} and $info->{"pieces"} and not $info->{"file tree"}
not exists $info->{"length"} $info->{"pieces"} not $info->{"file tree"}
Truth table50%2/4
not(A) and B and not(C)
A= not exists $info->{"length"}B= $info->{"pieces"}C= not $info->{"file tree"}
ABCresult
0XX0
10X0
1100
1111
355  
356 # Some minimal tests or old v1 might omit length if it's empty or inferred?
357 # Actually, BEP 03 says length is required for single-file.
358 # But some tests use minimal dictionaries.
359 # Let's be lenient for v1 minimal tests if pieces is present.
360 # (Optionally, we could default to 0)
361 }
362 else {
363 10 ▶ if ( ( $info->{length} // -1 ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($info->{"length"} // -1) < 0) 0 10 10
Condition50%1/2
$info->{"length"} // -1
l!l
100
MC/DC0%0/1
$info->{"length"} // -1
$info->{"length"}
Truth table50%1/2
A
A= $info->{"length"}
Aresult
11
00
364 0 $self->_emit_log( 'error', 'Invalid file length' );
365 0 return;
366 }
367 }
368 }
369 61 my $info_encoded = bencode($info);
370 61 ▶ $infohash_v1 = sha1($info_encoded) if $info->{pieces};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"pieces"} 59 2 61
371 61 ▶ $infohash_v2 = sha256($info_encoded) if $info->{'file tree'};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"file tree"} 47 14 61
372 61 ▶ $is_private = $info->{private} // 0;
Condition50%1/2
$info->{"private"} // 0
l!l
061
MC/DC0%0/1
$info->{"private"} // 0
$info->{"private"}
Truth table50%1/2
A
A= $info->{"private"}
Aresult
11
00
373 61 ▶ $dht_nodes = $metadata->{nodes} // [];
Condition50%1/2
$metadata->{"nodes"} // []
l!l
061
MC/DC0%0/1
$metadata->{"nodes"} // []
$metadata->{"nodes"}
Truth table50%1/2
A
A= $metadata->{"nodes"}
Aresult
11
00
374 61 my $tree = $self->file_tree;
375 $storage = Net::BitTorrent::Storage->new(
376 base_path => $base_path,
377 file_tree => $tree,
378 piece_size => $info->{'piece length'},
379 pieces_v1 => $info->{pieces}
380 61 );
381  
382 61 ▶ if ( my $layers = $metadata->{'piece layers'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $layers = $metadata->{"piece layers"}) 2 59 61
383 2 for my $root ( keys %$layers ) {
384 9 $storage->set_piece_layer( $root, $layers->{$root} );
385 }
386 }
387 61 ▶ my $num_pieces = int( ( length( $info->{pieces} // '' ) / 20 ) );
Condition100%2/2
$info->{"pieces"} // ""
l!l
592
MC/DC100%1/1
$info->{"pieces"} // ""
$info->{"pieces"}
Truth table100%2/2
A
A= $info->{"pieces"}
Aresult
11
00
388 61 ▶ if ( !$num_pieces && $info->{'file tree'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $num_pieces and $info->{"file tree"}) 2 59 61
Condition66%2/3
not $num_pieces and $info->{"file tree"}
!ll&&!rl&&r
5902
MC/DC50%1/2
not $num_pieces and $info->{"file tree"}
not $num_pieces $info->{"file tree"}
Truth table66%2/3
not(A) and B
A= not $num_piecesB= $info->{"file tree"}
ABresult
0X0
100
111
389 2 my $total_size = $self->_calculate_total_size();
390 2 $num_pieces = int( ( $total_size + $info->{'piece length'} - 1 ) / $info->{'piece length'} );
391 }
392 61 $bitfield = Acme::Bitfield->new( size => $num_pieces );
393 61 $self->_init_picker();
394 61 my @tiers;
395 61 ▶ if ( $metadata->{'announce-list'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($metadata->{'announce-list'}) { } 0 61 61
elsif ($metadata->{'announce'}) { } 5 56 61
396 0 @tiers = @{ $metadata->{'announce-list'} };
397 }
398 elsif ( $metadata->{announce} ) {
399 5 @tiers = ( [ $metadata->{announce} ] );
400 }
401 61 $tracker_manager = Net::BitTorrent::Tracker->new( tiers_raw => \@tiers, debug => $debug );
402 61 ▶ my $urls = $metadata->{'url-list'} // [];
Condition50%1/2
$metadata->{"url-list"} // []
l!l
061
MC/DC0%0/1
$metadata->{"url-list"} // []
$metadata->{"url-list"}
Truth table50%1/2
A
A= $metadata->{"url-list"}
Aresult
11
00
403 61 ▶ $urls = [$urls] unless ref $urls eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $urls eq "ARRAY" 0 61 61
404 61 push @webseeds, Net::BitTorrent::Tracker::WebSeed->new( url => $_ ) for @$urls;
405 61 my $total_size = $self->_calculate_total_size();
406 61 $bytes_left = $total_size;
407  
408 #~ warn " [DEBUG] Swarm initialized from metadata: $total_size bytes\n";
409 }
410  
411 ▶ method _init_picker () {
Subroutine _init_picker: called CC=3 SCAR=11.0
412 61 ▶ if ( !@piece_priorities && $bitfield ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @piece_priorities and $bitfield) 60 1 61
Condition66%2/3
not @piece_priorities and $bitfield
!ll&&!rl&&r
1060
MC/DC50%1/2
not @piece_priorities and $bitfield
not @piece_priorities $bitfield
Truth table66%2/3
not(A) and B
A= not @piece_prioritiesB= $bitfield
ABresult
0X0
100
111
413 60 @piece_priorities = (1) x $bitfield->size;
414 }
415 61 $picker = Net::BitTorrent::Torrent::PiecePicker->new(
416 bitfield => $bitfield,
417 piece_priorities => \@piece_priorities,
418 strategy => $picking_strategy,
419 );
420 }
421  
422 ▶ method set_piece_priority ( $index, $priority ) {
Subroutine set_piece_priority: not called CC=2 SCAR=17.9
Pod documented
423 0 ▶ $picker->set_priority( $index, $priority ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 0 0 0
424 }
425  
426 ▶ method set_picking_strategy ($strategy) {
Subroutine set_picking_strategy: not called CC=2 SCAR=17.9
Pod documented
427 0 ▶ $picker->set_strategy($strategy) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 0 0 0
428 }
429  
430 ▶ method get_allowed_fast_set ($ip) {
Subroutine get_allowed_fast_set: called CC=5 SCAR=17.0
Pod undocumented
431 7 ▶ return [] unless $bitfield && $bitfield->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield and $bitfield->size 0 7 7
Condition33%1/3
$bitfield and $bitfield->size
!ll&&!rl&&r
007
MC/DC0%0/2
$bitfield and $bitfield->size
$bitfield $bitfield->size
Truth table33%1/3
A and B
A= $bitfieldB= $bitfield->size
ABresult
0X0
100
111
432 7 my @set;
433 7 ▶ for ( my $i = 0; $i < 5 && $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5 and $i < $bitfield->size) 8 7 15
Condition66%2/3
$i < 5 and $i < $bitfield->size
!ll&&!rl&&r
078
MC/DC50%1/2
$i < 5 and $i < $bitfield->size
$i < 5 $i < $bitfield->size
Truth table66%2/3
A and B
A= $i < 5B= $i < $bitfield->size
ABresult
0X0
100
111
434 8 push @set, $i;
435 }
436 7 return \@set;
437 }
438  
439 ▶ method suggest_piece ($peer) {
Subroutine suggest_piece: not called CC=5 SCAR=34.0
Pod undocumented
440 0 ▶ return undef unless $bitfield && $bitfield->count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield and $bitfield->count > 0 0 0 0
Condition0%0/3
$bitfield and $bitfield->count > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$bitfield and $bitfield->count > 0
$bitfield $bitfield->count > 0
Truth table0%0/3
A and B
A= $bitfieldB= $bitfield->count > 0
ABresult
0X0
100
111
441 0 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 0 0 0
442 0 ▶ return $i if $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($i) 0 0 0
443 }
444 0 return undef;
445 }
446  
447 ▶ method handle_dht_scrape ($res) {
Subroutine handle_dht_scrape: called CC=5 SCAR=19.9
Pod undocumented
448 1 ▶ $dht_seeders = $res->{sn} if exists $res->{sn};
Branch: 1 branch
BranchTrueFalseTotal
if exists $res->{"sn"} 1 0 1
449 1 ▶ $dht_leechers = $res->{ln} if exists $res->{ln};
Branch: 1 branch
BranchTrueFalseTotal
if exists $res->{"ln"} 1 0 1
450 $self->_emit_log( 'info',
451 1 ▶ 'Scrape results for ' . ( $metadata ? $metadata->{info}{name} : 'unknown' ) . ": $dht_seeders seeds, $dht_leechers leechers" )
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 1 1
$metadata ? : 0 0 0
452 if $debug;
453 }
454  
455 101 ▶ method tick ( $delta = 0.1 ) {
Subroutine tick: called CC=22 SCAR=30.9
Pod documented
456 101 ▶ return if $state == STATE_STOPPED || $state == STATE_PAUSED;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 0 or $state == 3 14 87 101
Condition66%2/3
$state == 0 or $state == 3
l!l&&r!l&&!r
14087
MC/DC50%1/2
$state == 0 or $state == 3
$state == 0 $state == 3
Truth table66%2/3
A or B
A= $state == 0B= $state == 3
ABresult
1X1
011
000
457 87 $limit_up->tick($delta);
458 87 $limit_down->tick($delta);
459 87 ▶ $storage->tick($delta) if $storage;
Branch: 1 branch
BranchTrueFalseTotal
if $storage 82 5 87
460 87 ▶ $tracker_manager->tick($delta) if $tracker_manager;
Branch: 1 branch
BranchTrueFalseTotal
if $tracker_manager 87 0 87
461  
462 # Attempt to connect to discovered peers if we need more
463 87 ▶ $self->_attempt_connections() if keys %peer_objects < 50;
Branch: 1 branch
BranchTrueFalseTotal
if keys %peer_objects < 50 87 0 87
464  
465 # Snapshot peer list to avoid hash mutation during iteration
466 # (tick may trigger disconnect which modifies %peer_objects)
467 87 my @peers_snapshot = values %peer_objects;
468 87 for my $peer (@peers_snapshot) {
469 49 $peer->tick();
470 49 ▶ if ( $state == STATE_METADATA ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 4) { } 5 44 49
elsif ($state == 2) { } 44 0 44
471 5 $self->_request_metadata($peer);
472 }
473 elsif ( $state == STATE_RUNNING ) {
474  
475 # Update interest
476 44 my $is_interesting = $picker->is_interesting($peer);
477 44 ▶ if ( $is_interesting && !$peer->am_interested ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_interesting and not $peer->am_interested) { } 1 43 44
elsif (not $is_interesting and $peer->am_interested) { } 3 41 44
Condition100%3/3
$is_interesting and not $peer->am_interested
!ll&&!rl&&r
4211
MC/DC100%2/2
$is_interesting and not $peer->am_interested
$is_interesting not $peer->am_interested
Truth table100%3/3
A and not(B)
A= $is_interestingB= not $peer->am_interested
ABresult
0X0
100
111
478 1 $peer->interested();
479 }
480 ▶ elsif ( !$is_interesting && $peer->am_interested ) {
Condition100%3/3
not $is_interesting and $peer->am_interested
!ll&&!rl&&r
1393
MC/DC100%2/2
not $is_interesting and $peer->am_interested
not $is_interesting $peer->am_interested
Truth table100%3/3
not(A) and B
A= not $is_interestingB= $peer->am_interested
ABresult
0X0
100
111
481 3 $peer->not_interested();
482 }
483  
484 # Request pieces if not choked
485 44 ▶ $self->_request_pieces($peer) if !$peer->peer_choking && $peer->am_interested;
Branch: 1 branch
BranchTrueFalseTotal
if not $peer->peer_choking and $peer->am_interested 2 42 44
Condition100%3/3
not $peer->peer_choking and $peer->am_interested
!ll&&!rl&&r
22202
MC/DC100%2/2
not $peer->peer_choking and $peer->am_interested
not $peer->peer_choking $peer->am_interested
Truth table100%3/3
not(A) and B
A= not $peer->peer_chokingB= $peer->am_interested
ABresult
0X0
100
111
486 }
487 }
488 87 $choke_timer += $delta;
489 87 ▶ if ( $choke_timer >= 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($choke_timer >= 10) 2 85 87
490 2 $self->_evaluate_choking();
491 2 $self->_cleanup_connections();
492 2 $self->_emit( 'status_update',
493 { downloaded => $bytes_downloaded, uploaded => $bytes_uploaded, left => $bytes_left, peers => scalar keys %peer_objects, } );
494 2 $choke_timer = 0;
495 }
496 87 $pex_timer += $delta;
497 87 ▶ if ( $pex_timer >= 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pex_timer >= 60) 1 86 87
498 1 $self->_broadcast_pex();
499 1 $pex_timer = 0;
500 }
501 87 $tracker_timer += $delta;
502 87 ▶ if ( $tracker_timer >= 60 ) { # Every 60s check if trackers want an announce
Branch: 1 branch
BranchTrueFalseTotal
if ($tracker_timer >= 60) 1 86 87
503 1 $self->announce();
504 1 $tracker_timer = 0;
505 }
506 87 $dht_lookup_timer += $delta;
507  
508 # Accelerate DHT lookups during startup/metadata phase or if starved for peers
509 87 ▶ my $dht_interval = ( $state == STATE_METADATA || keys %peer_objects < 5 ) ? 2 : 120;
Branch: 1 branch
BranchTrueFalseTotal
$state == 4 || keys %peer_objects < 5 ? : 86 1 87
Condition100%3/3
$state == 4 or keys %peer_objects < 5
l!l&&r!l&&!r
5811
MC/DC100%2/2
$state == 4 or keys %peer_objects < 5
$state == 4 keys %peer_objects < 5
Truth table100%3/3
A or B
A= $state == 4B= keys %peer_objects < 5
ABresult
1X1
011
000
510 87 ▶ if ( $dht_lookup_timer >= $dht_interval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dht_lookup_timer >= $dht_interval) 3 84 87
511 3 $self->_update_dht_search();
512 3 $dht_lookup_timer = 0;
513 }
514 }
515 26 field %attempted_connections; # ip:port => timestamp
516  
517 ▶ method _attempt_connections () {
Subroutine _attempt_connections: called CC=8 SCAR=22.0
518 87 state $last_attempt = 0;
519 87 ▶ return if time() - $last_attempt < 5; # Don't spam connection attempts
Branch: 1 branch
BranchTrueFalseTotal
if time - $last_attempt < 5 79 8 87
520 8 $last_attempt = time();
521 8 my $peers = $self->discovered_peers;
522 8 my $count = 0;
523 8 for my $p (@$peers) {
524 1 my $key = "$p->{ip}:$p->{port}";
525 1 ▶ next if $peer_objects{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $peer_objects{$key} 0 1 1
526 1 ▶ next if $attempted_connections{$key} && ( time() - $attempted_connections{$key} < 60 );
Branch: 1 branch
BranchTrueFalseTotal
if $attempted_connections{$key} and time - $attempted_connections{$key} < 60 0 1 1
Condition33%1/3
$attempted_connections{$key} and time - $attempted_connections{$key} < 60
!ll&&!rl&&r
100
MC/DC0%0/2
$attempted_connections{$key} and time - $attempted_connections{$key} < 60
$attempted_connections{$key} time - $attempted_connections{$key} < 60
Truth table33%1/3
A and B
A= $attempted_connections{$key}B= time - $attempted_connections{$key} < 60
ABresult
0X0
100
111
527  
528 # Try to connect
529 1 $attempted_connections{$key} = time();
530 1 ▶ my $ih = $infohash_v2 || $infohash_v1;
Condition33%1/3
$infohash_v2 || $infohash_v1
l!l&&r!l&&!r
010
MC/DC0%0/2
$infohash_v2 || $infohash_v1
$infohash_v2 $infohash_v1
Truth table33%1/3
A || B
A= $infohash_v2B= $infohash_v1
ABresult
1X1
011
000
531 1 $client->connect_to_peer( $p->{ip}, $p->{port}, $ih );
532 1 $count++;
533 1 ▶ last if $count >= 5; # Limit concurrent attempts
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 5 0 1 1
534 }
535 }
536  
537 ▶ method _evaluate_choking () {
Subroutine _evaluate_choking: called CC=14 SCAR=26.9
538 2 ▶ $self->_emit_log( 'debug', 'Evaluating choking for ' . scalar( keys %peer_objects ) . ' peers' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
539 2 my @interested = grep { $_->peer_interested } values %peer_objects;
540  
541 # Even if nobody is interested in US, we should still unchoke some if we want pieces?
542 # No, unchoking is for UPLOAD. For DOWNLOAD, we need to send INTERESTED.
543 2 my @sorted;
544 2 ▶ if ( $bitfield && $bitfield->count < $bitfield->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitfield and $bitfield->count < $bitfield->size) { } 1 1 2
Condition66%2/3
$bitfield and $bitfield->count < $bitfield->size
!ll&&!rl&&r
101
MC/DC50%1/2
$bitfield and $bitfield->count < $bitfield->size
$bitfield $bitfield->count < $bitfield->size
Truth table66%2/3
A and B
A= $bitfieldB= $bitfield->count < $bitfield->size
ABresult
0X0
100
111
545 1 @sorted = sort { $b->rate_down <=> $a->rate_down } @interested;
546 }
547 else {
548 1 @sorted = sort { $b->rate_up <=> $a->rate_up } @interested;
549 }
550 2 my $max_unchoked = 4;
551 2 my %to_unchoke;
552 2 ▶ for ( my $i = 0; $i < $max_unchoked && $i < @sorted; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max_unchoked and $i < @sorted) 4 2 6
Condition100%3/3
$i < $max_unchoked and $i < @sorted
!ll&&!rl&&r
114
MC/DC100%2/2
$i < $max_unchoked and $i < @sorted
$i < $max_unchoked $i < @sorted
Truth table100%3/3
A and B
A= $i < $max_unchokedB= $i < @sorted
ABresult
0X0
100
111
553 4 $to_unchoke{ $sorted[$i] } = 1;
554 }
555 2 $optimistic_timer += 10;
556 2 ▶ if ( $optimistic_timer >= 30 || !$optimistic_unchoke_peer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optimistic_timer >= 30 or not $optimistic_unchoke_peer) 2 0 2
Condition33%1/3
$optimistic_timer >= 30 or not $optimistic_unchoke_peer
l!l&&r!l&&!r
020
MC/DC0%0/2
$optimistic_timer >= 30 or not $optimistic_unchoke_peer
$optimistic_timer >= 30 not $optimistic_unchoke_peer
Truth table33%1/3
A or not(B)
A= $optimistic_timer >= 30B= not $optimistic_unchoke_peer
ABresult
1X1
011
000
557 2 my @candidates = grep { !$to_unchoke{$_} } @interested;
558 2 ▶ if (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 1 1 2
559 1 $optimistic_unchoke_peer = $candidates[ rand @candidates ];
560 }
561 2 $optimistic_timer = 0;
562 }
563 2 ▶ $to_unchoke{$optimistic_unchoke_peer} = 1 if $optimistic_unchoke_peer;
Branch: 1 branch
BranchTrueFalseTotal
if $optimistic_unchoke_peer 1 1 2
564 2 for my $peer ( values %peer_objects ) {
565 7 ▶ if ( $to_unchoke{$peer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_unchoke{$peer}) { } 5 2 7
566 5 ▶ $peer->unchoke() if $peer->am_choking;
Branch: 1 branch
BranchTrueFalseTotal
if $peer->am_choking 5 0 5
567 }
568 else {
569 2 ▶ $peer->choke() if !$peer->am_choking;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer->am_choking 0 2 2
570 }
571 }
572 }
573 26 field %metadata_pending; # peer => piece_index
574  
575 ▶ method _request_metadata ($peer) {
Subroutine _request_metadata: called CC=12 SCAR=26.9
576 5 ▶ return unless $peer->protocol->isa('Net::BitTorrent::Protocol::BEP10');
Branch: 1 branch
BranchTrueFalseTotal
unless $peer->protocol->isa("Net::BitTorrent::Protocol::BEP10") 0 5 5
577 5 my $remote_ext = $peer->protocol->remote_extensions;
578 5 ▶ return unless exists $remote_ext->{ut_metadata};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $remote_ext->{"ut_metadata"} 1 4 5
579  
580 # We need metadata_size from the peer (from extended handshake)
581 4 my $m_size = $peer->protocol->metadata_size;
582 4 ▶ return unless $m_size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $m_size > 0 0 4 4
583 4 ▶ if ( $metadata_size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata_size == 0) 2 2 4
584 2 $metadata_size = $m_size;
585 2 ▶ $self->_emit_log( 'debug', "Metadata size identified: $metadata_size bytes" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
586 }
587  
588 # How many pieces? (BEP 09 uses 16KiB pieces)
589 4 my $num_pieces = int( ( $metadata_size + 16383 ) / 16384 );
590  
591 # Check if we already have a request pending for this peer
592 4 ▶ return if exists $metadata_pending{$peer};
Branch: 1 branch
BranchTrueFalseTotal
if exists $metadata_pending{$peer} 2 2 4
593  
594 # Find a piece we don't have and isn't pending from another peer (simple greedy)
595 # Note: In a real client, we'd track global pending metadata pieces.
596 2 ▶ for ( my $i = 0; $i < $num_pieces; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_pieces) 2 0 2
597 2 ▶ if ( !exists $metadata_pieces{$i} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $metadata_pieces{$i}) 2 0 2
598  
599 # Check if anyone else is already requesting this specific piece
600 2 my $already_requested = 0;
601 2 for my $p_pending ( values %metadata_pending ) {
602 0 ▶ if ( $p_pending == $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_pending == $i) 0 0 0
603 0 $already_requested = 1;
604 0 last;
605 }
606 }
607 2 ▶ next if $already_requested;
Branch: 1 branch
BranchTrueFalseTotal
if $already_requested 0 2 2
608 2 $metadata_pending{$peer} = $i;
609 2 $peer->protocol->send_metadata_request($i);
610 2 return;
611 }
612 }
613 }
614  
615 ▶ method _request_pieces ($peer) {
Subroutine _request_pieces: called CC=6 SCAR=18.6
616 2 ▶ return if $peer->blocks_inflight >= 20; # Throttle per-peer
Branch: 1 branch
BranchTrueFalseTotal
if $peer->blocks_inflight >= 20 0 2 2
617 2 my $pbitfield = $peer_bitfields{$peer};
618 2 ▶ unless ($pbitfield) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pbitfield) 0 2 2
619 0 return;
620 }
621 2 ▶ while ( $peer->blocks_inflight < 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer->blocks_inflight < 20) 3 0 3
622 3 my ( $index, $begin, $len ) = $picker->pick_block( $peer, \%blocks_pending );
623 3 ▶ unless ( defined $index ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 2 1 3
624 2 last;
625 }
626 1 $blocks_pending{$index}{$begin} = $peer;
627 1 $block_sources{$index}{$begin} = $peer;
628 1 $peer->request( $index, $begin, $len );
629 1 ▶ $self->_emit_log( 'debug', "Requested block at $begin of piece $index from " . $peer->ip ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
630 }
631 }
632  
633 ▶ method handle_metadata_request ( $peer, $piece ) {
Subroutine handle_metadata_request: called CC=4 SCAR=13.9
Pod undocumented
634 4 ▶ return unless $metadata;
Branch: 1 branch
BranchTrueFalseTotal
unless $metadata 0 4 4
635 4 my $info_encoded = bencode( $metadata->{info} );
636 4 my $num_pieces = int( ( length($info_encoded) + 16383 ) / 16384 );
637 4 ▶ return if $piece < 0 || $piece >= $num_pieces;
Branch: 1 branch
BranchTrueFalseTotal
if $piece < 0 or $piece >= $num_pieces 2 2 4
Condition100%3/3
$piece < 0 or $piece >= $num_pieces
l!l&&r!l&&!r
112
MC/DC100%2/2
$piece < 0 or $piece >= $num_pieces
$piece < 0 $piece >= $num_pieces
Truth table100%3/3
A or B
A= $piece < 0B= $piece >= $num_pieces
ABresult
1X1
011
000
638 2 my $piece_data = substr( $info_encoded, $piece * 16384, 16384 );
639 2 $peer->protocol->send_metadata_data( $piece, length($info_encoded), $piece_data );
640 }
641  
642 ▶ method handle_metadata_data ( $peer, $piece, $total_size, $data ) {
Subroutine handle_metadata_data: called CC=15 SCAR=33.3
Pod undocumented
643 10 ▶ delete $metadata_pending{$peer} if defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer 2 8 10
644 10 ▶ if ( $metadata_size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata_size == 0) 8 2 10
645 8 ▶ if ( $total_size > MAX_METADATA_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_size > 10485760) 2 6 8
646 2 $self->_emit_log( 'fatal', "Metadata too large ($total_size bytes, max " . MAX_METADATA_SIZE . ')' );
647 0 return;
648 }
649 6 $metadata_size = $total_size;
650 }
651 8 my $num_pieces = int( ( $metadata_size + 16383 ) / 16384 );
652 8 ▶ if ( $piece < 0 || $piece >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece < 0 or $piece >= $num_pieces) 0 8 8
Condition33%1/3
$piece < 0 or $piece >= $num_pieces
l!l&&r!l&&!r
008
MC/DC0%0/2
$piece < 0 or $piece >= $num_pieces
$piece < 0 $piece >= $num_pieces
Truth table33%1/3
A or B
A= $piece < 0B= $piece >= $num_pieces
ABresult
1X1
011
000
653 0 $self->_emit_log( 'warning', "Received out-of-range metadata piece index $piece (max " . ( $num_pieces - 1 ) . ')' );
654 0 return;
655 }
656 8 ▶ $self->_emit_log( 'debug', "Received metadata piece $piece (len " . length($data) . ') from ' . ( $peer ? $peer->ip : 'unknown' ) ) if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 1 7 8
$peer ? : 0 1 1
657 8 $metadata_pieces{$piece} = $data;
658 8 ▶ $self->_emit_log( 'debug', 'Metadata progress: ' . scalar( keys %metadata_pieces ) . "/$num_pieces pieces" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 7 8
659 8 ▶ if ( scalar keys %metadata_pieces == $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %metadata_pieces == $num_pieces) 8 0 8
660 8 my $full_info = join( '', map { $metadata_pieces{$_} } sort { $a <=> $b } keys %metadata_pieces );
661  
662 # Verify hash
663 8 my $calculated_ih = sha1($full_info);
664 8 my $verified = 0;
665 8 ▶ if ( $infohash_v1 && $calculated_ih eq $infohash_v1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($infohash_v1 and $calculated_ih eq $infohash_v1) { } 8 0 8
elsif ($infohash_v2) { } 0 0 0
Condition33%1/3
$infohash_v1 and $calculated_ih eq $infohash_v1
!ll&&!rl&&r
008
MC/DC0%0/2
$infohash_v1 and $calculated_ih eq $infohash_v1
$infohash_v1 $calculated_ih eq $infohash_v1
Truth table33%1/3
A and B
A= $infohash_v1B= $calculated_ih eq $infohash_v1
ABresult
0X0
100
111
666 8 $verified = 1;
667 }
668 elsif ($infohash_v2) {
669 0 ▶ if ( sha256($full_info) eq $infohash_v2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&sha256($full_info) eq $infohash_v2) 0 0 0
670 0 $verified = 1;
671 }
672 }
673 8 ▶ if ( !$verified ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($verified) 0 8 8
674 0 $self->_emit_log( 'error', 'Metadata verification FAILED! Hash mismatch.' );
675 0 %metadata_pieces = ();
676 0 %metadata_pending = ();
677 0 $metadata_size = 0;
678 0 return;
679 }
680  
681 # Decode and start torrent
682 8 my $info = bdecode($full_info);
683 8 $metadata = { info => $info };
684 8 $self->_on_metadata_received();
685 }
686 }
687  
688 ▶ method handle_metadata_reject ( $peer, $piece ) {
Subroutine handle_metadata_reject: not called CC=3 SCAR=24.8
Pod undocumented
689 0 ▶ delete $metadata_pending{$peer} if defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer 0 0 0
690 0 ▶ $self->_emit_log( 'debug', "Peer rejected metadata request for piece $piece" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
691 }
692  
693 ▶ method _on_metadata_received () {
Subroutine _on_metadata_received: called CC=17 SCAR=32.1
694 8 ▶ $self->_emit_log( 'debug', 'Metadata fully received and verified' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 7 8
695  
696 # Validate name for path traversal before using it
697 8 my $name = $metadata->{info}{name};
698 8 ▶ if ( !defined $name || !length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length $name) 0 8 8
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
008
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
699 0 $self->_emit_log( 'error', 'Missing name in metadata' );
700 0 return;
701 }
702 8 ▶ if ( $name =~ /[\\\/]/ || $name eq '..' || $name =~ /\0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[[\\/]]u or $name eq ".." or $name =~ /\0/u) 0 7 7
Condition66%4/6
$name =~ m[[\\/]]u or $name eq ".." or $name =~ /\0/u
l!l&&r!l&&!r
107
$name =~ m[[\\/]]u or $name eq ".."
l!l&&r!l&&!r
107
MC/DC33%1/3
$name =~ m[[\\/]]u or $name eq ".." or $name =~ /\0/u
$name =~ m[[\\/]]u $name eq ".." $name =~ /\0/u
Truth table50%2/4
A or B or C
A= $name =~ m[[\\/]]uB= $name eq ".."C= $name =~ /\0/u
ABCresult
1XX1
01X1
0011
0000
703 1 $self->_emit_log( 'error', 'Invalid name: path traversal characters detected' );
704 1 return;
705 }
706 7 require File::Spec;
707 7 ▶ if ( File::Spec->file_name_is_absolute($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($name)) 0 7 7
708 0 $self->_emit_log( 'error', 'Invalid name: absolute path' );
709 0 return;
710 }
711  
712 # Multi-file torrents should be in a directory named after the torrent
713 7 my $storage_path = $base_path;
714 7 ▶ if ( $metadata->{info}{'file tree'} || $metadata->{info}{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata->{"info"}{"file tree"} or $metadata->{"info"}{"files"}) 1 6 7
Condition66%2/3
$metadata->{"info"}{"file tree"} or $metadata->{"info"}{"files"}
l!l&&r!l&&!r
106
MC/DC50%1/2
$metadata->{"info"}{"file tree"} or $metadata->{"info"}{"files"}
$metadata->{"info"}{"file tree"} $metadata->{"info"}{"files"}
Truth table66%2/3
A or B
A= $metadata->{"info"}{"file tree"}B= $metadata->{"info"}{"files"}
ABresult
1X1
011
000
715 1 $storage_path = $base_path->child( $metadata->{info}{name} );
716 }
717  
718 # Initialize storage
719 7 ▶ $self->_emit_log( 'debug', "Initializing storage at $storage_path" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 6 7
720 $storage = Net::BitTorrent::Storage->new(
721 base_path => $storage_path,
722 piece_size => $metadata->{info}{'piece length'},
723 pieces_v1 => $metadata->{info}{pieces},
724 7 );
725  
726 # Load files into storage
727 7 ▶ if ( my $tree = $metadata->{info}{'file tree'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $tree = $metadata->{'info'}{'file tree'}) { } 1 6 7
elsif (my $files = $metadata->{'info'}{'files'}) { } 0 6 6
728 1 $storage->load_file_tree($tree);
729 }
730 elsif ( my $files = $metadata->{info}{files} ) { # v1 Multi-file
731 0 for my $f (@$files) {
732 0 my $rel_path = Path::Tiny::path( @{ $f->{path} } );
733 0 $storage->add_file( $rel_path, $f->{length} );
734 }
735 }
736 else { # v1 Single-file
737 6 $storage->add_file( $metadata->{info}{name}, $metadata->{info}{length} );
738 }
739  
740 # Initialize bitfield
741 7 my $num_pieces = 0;
742 7 ▶ $num_pieces = length( $metadata->{info}{pieces} ) / 20 if exists $metadata->{info}{pieces};
Branch: 1 branch
BranchTrueFalseTotal
if exists $metadata->{"info"}{"pieces"} 7 0 7
743 7 ▶ $num_pieces ||= $storage->piece_count;
Condition33%1/3
$num_pieces ||= $storage->piece_count
l!l&&r!l&&!r
700
MC/DC0%0/2
$num_pieces ||= $storage->piece_count
$num_pieces $storage->piece_count
Truth table33%1/3
A ||= B
A= $num_piecesB= $storage->piece_count
ABresult
1X1
011
000
744 7 $bitfield = Acme::Bitfield->new( size => $num_pieces );
745  
746 # Initialize picker
747 7 $picker = Net::BitTorrent::Torrent::PiecePicker->new( bitfield => $bitfield, );
748 7 $bytes_left = $self->_calculate_total_size();
749 7 $state = STATE_RUNNING;
750 7 $self->_emit('started');
751  
752 # Re-initialize peer bitfields now that we have the size
753 7 $self->init_peer_bitfield($_) for values %peer_objects;
754  
755 # Announce to trackers now that we have full infohash info
756 7 $self->announce();
757 }
758  
759 ▶ method receive_block ( $peer, $index, $begin, $data ) {
Subroutine receive_block: called CC=13 SCAR=29.4
Pod undocumented
760 35 ▶ return 0 unless $bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield 0 35 35
761 35 ▶ return 0 if $bitfield->get($index);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($index) 0 35 35
762  
763 # If we've already received this block, or the piece is already being verified, skip.
764 # We use blocks_received as an indicator that the piece is complete/queued.
765 35 ▶ return 0 if exists $blocks_received{$index} && $self->is_piece_complete($index);
Branch: 1 branch
BranchTrueFalseTotal
if exists $blocks_received{$index} and $self->is_piece_complete($index) 26 9 35
Condition66%2/3
exists $blocks_received{$index} and $self->is_piece_complete($index)
!ll&&!rl&&r
9026
MC/DC50%1/2
exists $blocks_received{$index} and $self->is_piece_complete($index)
exists $blocks_received{$index} $self->is_piece_complete($index)
Truth table66%2/3
A and B
A= exists $blocks_received{$index}B= $self->is_piece_complete($index)
ABresult
0X0
100
111
766  
767 # v2 Block-level verification (if we have pieces root)
768 9 my ( $root, $rel_piece ) = $storage->map_v2_piece($index);
769 9 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) 2 7 9
770 2 my $info = $metadata->{info};
771 2 my $block_index = ( $rel_piece * ( $info->{'piece length'} / 16384 ) ) + ( $begin / 16384 );
772 2 ▶ if ( !$storage->verify_block( $root, $block_index, $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storage->verify_block($root, $block_index, $data)) 0 2 2
773 0 ▶ $self->_emit_log( 'error', "v2 block verification FAILED for block $block_index of root " . unpack( 'H*', $root ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
774 0 ▶ $peer->adjust_reputation(-50) if $peer;
Branch: 1 branch
BranchTrueFalseTotal
if $peer 0 0 0
775 0 return 0;
776 }
777 }
778 9 $self->_store_block( $peer, $index, $begin, $data );
779 9 ▶ if ( $self->is_piece_complete($index) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_piece_complete($index)) 9 0 9
780 9 ▶ $self->_emit_log( 'debug', "Piece $index is COMPLETE" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 8 9
781 9 ▶ return 0 if $bitfield->get($index);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($index) 0 9 9
782 9 my $piece_data = $self->_get_full_piece($index);
783 9 ▶ if ($piece_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece_data) 9 0 9
784 9 $self->_clear_piece_data($index);
785 9 $client->queue_verification( $self, $index, $piece_data );
786 9 return 1;
787 }
788 }
789 0 return 0;
790 }
791 59 ▶ method _clear_piece_data ($index) { delete $block_cache{$index} }
Subroutine _clear_piece_data: called CC=1 SCAR=0.0
792  
793 ▶ method _verify_queued_piece ( $index, $piece_data ) {
Subroutine _verify_queued_piece: called CC=14 SCAR=27.0
794 50 ▶ my $sources = delete $block_sources{$index} // {};
Condition100%2/2
delete $block_sources{$index} // {}
l!l
545
MC/DC100%1/1
delete $block_sources{$index} // {}
delete $block_sources{$index}
Truth table100%2/2
A
A= delete $block_sources{$index}
Aresult
11
00
795 50 my $verified = 0;
796  
797 # Try v2 verification first if possible
798 50 my ( $root, $rel_index ) = $storage->map_v2_piece($index);
799 50 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 4 46 50
800 4 my $res = $storage->verify_piece_v2( $root, $rel_index, $piece_data );
801 4 ▶ if ( defined $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 0 4 4
802 0 $verified = $res;
803 }
804 else {
805 # Fallback to v1 if v2 fails because layer is missing?
806 # (Normally v2 is authoritative if pieces_root exists)
807 4 ▶ $verified = $storage->verify_piece_v1( $index, $piece_data ) // 0;
Condition50%1/2
$storage->verify_piece_v1($index, $piece_data) // 0
l!l
40
MC/DC0%0/1
$storage->verify_piece_v1($index, $piece_data) // 0
$storage->verify_piece_v1($index, $piece_data)
Truth table50%1/2
A
A= $storage->verify_piece_v1($index, $piece_data)
Aresult
11
00
808 }
809 }
810 else {
811 46 ▶ $verified = $storage->verify_piece_v1( $index, $piece_data ) // 0;
Condition100%2/2
$storage->verify_piece_v1($index, $piece_data) // 0
l!l
640
MC/DC100%1/1
$storage->verify_piece_v1($index, $piece_data) // 0
$storage->verify_piece_v1($index, $piece_data)
Truth table100%2/2
A
A= $storage->verify_piece_v1($index, $piece_data)
Aresult
11
00
812 }
813 50 ▶ if ($verified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verified) { } 9 41 50
814 9 $storage->write_piece_v1( $index, $piece_data );
815 9 $bitfield->set($index);
816 9 $bytes_downloaded += length($piece_data);
817 9 $bytes_left -= length($piece_data);
818 9 $last_piece_verified_at = time;
819 9 ▶ $self->_emit_log( 'debug', "Piece $index VERIFIED successfully via throttled queue" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 8 9
820 9 $self->_clear_piece_cache($index);
821 9 $self->_emit( 'piece_verified', $index );
822  
823 9 for my $peer ( values %$sources ) {
824 4 ▶ $peer->adjust_reputation(1) if defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer 4 0 4
825 }
826 9 return 1;
827 }
828 else {
829 41 ▶ $self->_emit_log( 'debug', "\nPiece $index FAILED verification (len " . length( $piece_data // '' ) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 41 41
Condition0%0/2
$piece_data // ""
l!l
00
MC/DC0%0/1
$piece_data // ""
$piece_data
Truth table0%0/2
A
A= $piece_data
Aresult
11
00
830 41 $self->_clear_piece_cache($index);
831 41 $self->_emit( 'piece_failed', $index );
832 41 for my $peer ( values %$sources ) {
833 1 ▶ $peer->adjust_reputation(-20) if defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer 1 0 1
834 }
835 41 return -1;
836 }
837 }
838  
839 ▶ method _cancel_duplicate_requests ( $sender, $index, $begin ) {
Subroutine _cancel_duplicate_requests: called CC=5 SCAR=17.1
840 9 ▶ return unless $picker && $picker->end_game;
Branch: 1 branch
BranchTrueFalseTotal
unless $picker and $picker->end_game 6 3 9
Condition66%2/3
$picker and $picker->end_game
!ll&&!rl&&r
063
MC/DC50%1/2
$picker and $picker->end_game
$picker $picker->end_game
Truth table66%2/3
A and B
A= $pickerB= $picker->end_game
ABresult
0X0
100
111
841 3 for my $peer ( values %peer_objects ) {
842 3 ▶ next if $peer == $sender;
Branch: 1 branch
BranchTrueFalseTotal
if $peer == $sender 3 0 3
843 0 $peer->cancel_request( $index, $begin );
844 }
845 }
846  
847 ▶ method _store_block ( $peer, $index, $begin, $data ) {
Subroutine _store_block: called CC=5 SCAR=18.7
848 9 delete $blocks_pending{$index}{$begin};
849 9 ▶ return if $blocks_received{$index}{$begin};
Branch: 1 branch
BranchTrueFalseTotal
if $blocks_received{$index}{$begin} 0 9 9
850 9 $self->_cancel_duplicate_requests( $peer, $index, $begin );
851 9 ▶ if ( keys %block_cache >= MAX_BLOCK_CACHE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %block_cache >= 32) 0 9 9
852 0 my @oldest = sort { $a <=> $b } keys %block_cache;
853 0 my $evicted = shift @oldest;
854 0 delete $block_cache{$evicted};
855 0 delete $blocks_received{$evicted};
856 0 delete $block_sources{$evicted};
857 }
858 9 ▶ $block_cache{$index} //= {};
Condition50%1/2
$block_cache{$index} //= {}
l!l
09
MC/DC0%0/1
$block_cache{$index} //= {}
$block_cache{$index}
Truth table50%1/2
A
A= $block_cache{$index}
Aresult
11
00
859 9 $block_cache{$index}{$begin} = $data;
860 9 $blocks_received{$index}{$begin} = 1;
861 9 ▶ $block_sources{$index}{$begin} = $peer if $peer;
Branch: 1 branch
BranchTrueFalseTotal
if $peer 6 3 9
862 9 delete $blocks_pending{$index}{$begin};
863 }
864  
865 ▶ method is_piece_complete ($index) {
Subroutine is_piece_complete: called CC=2 SCAR=7.0
Pod undocumented
866 35 my $piece_length = $self->piece_length($index);
867 35 my $blocks_needed = int( ( $piece_length + 16383 ) / 16384 );
868 35 ▶ return ( scalar keys %{ $blocks_received{$index} // {} } ) == $blocks_needed;
Condition50%1/2
$blocks_received{$index} // {}
l!l
350
MC/DC0%0/1
$blocks_received{$index} // {}
$blocks_received{$index}
Truth table50%1/2
A
A= $blocks_received{$index}
Aresult
11
00
869 }
870  
871 ▶ method piece_length ($index) {
Subroutine piece_length: called CC=4 SCAR=13.9
Pod undocumented
872 79 my $total_size = $self->_calculate_total_size();
873 79 ▶ my $standard_len = $metadata->{info}{'piece length'} // 16384;
Condition50%1/2
$metadata->{"info"}{"piece length"} // 16384
l!l
790
MC/DC0%0/1
$metadata->{"info"}{"piece length"} // 16384
$metadata->{"info"}{"piece length"}
Truth table50%1/2
A
A= $metadata->{"info"}{"piece length"}
Aresult
11
00
874 79 my $num_pieces = int( ( $total_size + $standard_len - 1 ) / $standard_len );
875 79 ▶ if ( $index == $num_pieces - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $num_pieces - 1) 53 26 79
876 53 my $rem = $total_size % $standard_len;
877 53 ▶ return $rem == 0 ? $standard_len : $rem;
Branch: 1 branch
BranchTrueFalseTotal
$rem == 0 ? : 50 3 53
878 }
879 26 return $standard_len;
880 }
881  
882 ▶ method _get_full_piece ($index) {
Subroutine _get_full_piece: called CC=4 SCAR=14.0
883 9 ▶ my $cache = $block_cache{$index} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $cache = $block_cache{$index} or return undef 0 9 9
884 9 my $piece_length = $self->piece_length($index);
885 9 my $full = '';
886 9 my $offset = 0;
887 9 ▶ while ( $offset < $piece_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $piece_length) 9 9 18
888 9 ▶ my $block = $cache->{$offset} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $block = $cache->{$offset} or return undef 0 9 9
889 9 $full .= $block;
890 9 $offset += length($block);
891 }
892 9 return $full;
893 }
894  
895 ▶ method _clear_piece_cache ($index) {
Subroutine _clear_piece_cache: called CC=1 SCAR=0.0
896 50 $self->_clear_piece_data($index);
897 50 delete $blocks_received{$index};
898 50 delete $blocks_pending{$index};
899 50 delete $block_sources{$index};
900 }
901  
902 ▶ method get_next_request ($peer) {
Subroutine get_next_request: called CC=11 SCAR=25.0
Pod undocumented
903 51 ▶ return undef if $state ne STATE_RUNNING;
Branch: 1 branch
BranchTrueFalseTotal
if $state ne 2 2 49 51
904 49 my $p_bf = $peer_bitfields{$peer};
905 49 ▶ if ( !$p_bf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p_bf) 16 33 49
906  
907 # warn ' [DEBUG] Peer ' . $peer->ip . " has no bitfield\n" if $debug;
908 16 return undef;
909 }
910 33 ▶ if ( !$picker->end_game ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($picker->end_game) 7 26 33
911 7 my $missing = $bitfield->size - $bitfield->count;
912 7 ▶ my $stalled = ( $last_piece_verified_at > 0 && ( time - $last_piece_verified_at ) >= ENDGAME_STALL_TIMEOUT );
Condition33%1/3
$last_piece_verified_at > 0 && time - $last_piece_verified_at >= 60
!ll&&!rl&&r
700
MC/DC0%0/2
$last_piece_verified_at > 0 && time - $last_piece_verified_at >= 60
$last_piece_verified_at > 0 time - $last_piece_verified_at >= 60
Truth table33%1/3
A && B
A= $last_piece_verified_at > 0B= time - $last_piece_verified_at >= 60
ABresult
0X0
100
111
913 7 ▶ if ( $missing <= 3 || $missing < ( $bitfield->size / 100 ) || $stalled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing <= 3 or $missing < $bitfield->size / 100 or $stalled) 0 1 1
Condition66%4/6
$missing <= 3 or $missing < $bitfield->size / 100 or $stalled
l!l&&r!l&&!r
601
$missing <= 3 or $missing < $bitfield->size / 100
l!l&&r!l&&!r
601
MC/DC33%1/3
$missing <= 3 or $missing < $bitfield->size / 100 or $stalled
$missing <= 3 $missing < $bitfield->size / 100 $stalled
Truth table50%2/4
A or B or C
A= $missing <= 3B= $missing < $bitfield->size / 100C= $stalled
ABCresult
1XX1
01X1
0011
0000
914 6 ▶ $self->_emit_log( 'debug', 'Entering END-GAME mode' . ( $stalled ? ' (stall detected)' : '' ) ) if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 6 6
$stalled ? : 0 0 0
915 6 $picker->enter_end_game();
916 }
917 }
918 33 my ( $piece_idx, $offset, $len ) = $picker->pick_block( $peer, \%blocks_pending );
919 33 ▶ if ( !defined $piece_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $piece_idx) 17 16 33
920  
921 # warn ' [DEBUG] No piece picked for ' . $peer->ip . "\n" if $debug;
922 17 return undef;
923 }
924 16 $blocks_pending{$piece_idx}{$offset} = $peer;
925 16 return { index => $piece_idx, begin => $offset, length => $len };
926 }
927  
928 ▶ method peer_disconnected ($peer) {
Subroutine peer_disconnected: called CC=10 SCAR=23.3
Pod undocumented
929 32 my $ip_port = $peer->ip . ':' . $peer->port;
930 32 ▶ $self->_emit_log( 'debug', "Peer disconnected: $ip_port" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 32 32
931 32 ▶ $client->on_peer_disconnected( $peer->ip ) if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 2 30 32
932 32 ▶ $client->_emit( 'peer_disconnected', $peer ) if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 2 30 32
933 32 ▶ delete $metadata_pending{$peer} if defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer 32 0 32
934 32 $pex_dropped{$ip_port} = { ip => $peer->ip, port => $peer->port };
935 32 delete $pex_added{$ip_port};
936 32 ▶ if ( my $bf = $peer_bitfields{$peer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bf = $peer_bitfields{$peer}) 15 17 32
937 15 ▶ $picker->update_availability( $bf, -1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 15 0 15
938 }
939 32 for my $i ( keys %blocks_pending ) {
940 8 for my $offset ( keys %{ $blocks_pending{$i} } ) {
941 2 ▶ if ( $blocks_pending{$i}{$offset} == $peer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks_pending{$i}{$offset} == $peer) 2 0 2
942 2 delete $blocks_pending{$i}{$offset};
943 }
944 }
945 }
946 32 delete $peer_bitfields{$peer};
947 32 delete $peer_objects{$ip_port};
948 }
949  
950 ▶ method set_peer_bitfield ( $peer, $data ) {
Subroutine set_peer_bitfield: called CC=7 SCAR=20.5
Pod undocumented
951 12 ▶ return unless $bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield 2 10 12
952 10 my $bf = Acme::Bitfield->new( size => $bitfield->size );
953 10 $bf->set_data($data);
954 10 ▶ if ( my $old_bf = $peer_bitfields{$peer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old_bf = $peer_bitfields{$peer}) 0 10 10
955 0 ▶ $picker->update_availability( $old_bf, -1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 0 0 0
956 }
957 10 $peer_bitfields{$peer} = $bf;
958 10 my $flags = 0;
959 10 ▶ $flags |= 0x01 if $peer->transport->filter; # Encrypted
Branch: 1 branch
BranchTrueFalseTotal
if $peer->transport->filter 0 10 10
960 10 ▶ $flags |= 0x02 if $bf->count == $bf->size; # Seeder
Branch: 1 branch
BranchTrueFalseTotal
if $bf->count == $bf->size 2 8 10
961 10 $pex_added{ $peer->ip . ':' . $peer->port } = { ip => $peer->ip, port => $peer->port, flags => $flags };
962 10 delete $pex_dropped{ $peer->ip . ':' . $peer->port };
963 10 ▶ $picker->update_availability( $bf, 1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 10 0 10
964 }
965  
966 ▶ method update_peer_have ( $peer, $index ) {
Subroutine update_peer_have: called CC=5 SCAR=16.6
Pod undocumented
967 3 ▶ return unless $bitfield; # Might not be initialized yet during metadata phase
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield 0 3 3
968 3 ▶ $self->init_peer_bitfield($peer) unless $peer_bitfields{$peer};
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfields{$peer} 2 1 3
969 3 ▶ $peer_bitfields{$peer}->set($index) if $peer_bitfields{$peer};
Branch: 1 branch
BranchTrueFalseTotal
if $peer_bitfields{$peer} 3 0 3
970 3 my $tmp_bf = Acme::Bitfield->new( size => $bitfield->size );
971 3 $tmp_bf->set($index);
972 3 ▶ $picker->update_availability( $tmp_bf, 1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 3 0 3
973 }
974  
975 ▶ method init_peer_bitfield ($peer) {
Subroutine init_peer_bitfield: called CC=7 SCAR=19.9
Pod undocumented
976 8 ▶ return if $peer_bitfields{$peer};
Branch: 1 branch
BranchTrueFalseTotal
if $peer_bitfields{$peer} 1 7 8
977 7 ▶ return unless $bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitfield 0 7 7
978 7 my $bf = Acme::Bitfield->new( size => $bitfield->size );
979 7 $peer_bitfields{$peer} = $bf;
980  
981 # Apply stored status
982 7 my $status = $peer->bitfield_status;
983 7 ▶ if ( defined $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status) 4 3 7
984 4 ▶ if ( $status eq 'all' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status eq 'all') { } 0 4 4
elsif ($status eq 'none') { } 2 2 4
985 0 $bf->fill();
986 }
987 elsif ( $status eq 'none' ) {
988  
989 # already zeros
990 }
991 else {
992 2 $bf->set_data($status);
993 }
994 4 ▶ $picker->update_availability( $bf, 1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 4 0 4
995 }
996 }
997  
998 ▶ method set_peer_have_all ($peer) {
Subroutine set_peer_have_all: called CC=4 SCAR=14.6
Pod undocumented
999 2 $self->init_peer_bitfield($peer);
1000 2 ▶ return unless $peer_bitfields{$peer};
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfields{$peer} 0 2 2
1001 2 ▶ $picker->update_availability( $peer_bitfields{$peer}, -1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 2 0 2
1002 2 $peer_bitfields{$peer}->fill();
1003 2 ▶ $picker->update_availability( $peer_bitfields{$peer}, 1 ) if $picker;
Branch: 1 branch
BranchTrueFalseTotal
if $picker 2 0 2
1004 }
1005  
1006 ▶ method set_peer_have_none ($peer) {
Subroutine set_peer_have_none: called CC=1 SCAR=0.0
Pod undocumented
1007 2 $self->init_peer_bitfield($peer);
1008 }
1009  
1010 ▶ method _broadcast_pex () {
Subroutine _broadcast_pex: called CC=13 SCAR=32.1
1011 1 ▶ return unless keys %pex_added || keys %pex_dropped;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %pex_added or keys %pex_dropped 0 1 1
Condition33%1/3
keys %pex_added or keys %pex_dropped
l!l&&r!l&&!r
100
MC/DC0%0/2
keys %pex_added or keys %pex_dropped
keys %pex_added keys %pex_dropped
Truth table33%1/3
A or B
A= keys %pex_addedB= keys %pex_dropped
ABresult
1X1
011
000
1012  
1013 # Limit to 100 peers per message per BEP 11
1014 1 my @added = values %pex_added;
1015 1 ▶ if ( @added > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@added > 100) 0 1 1
1016 0 @added = splice( @added, 0, 100 );
1017 }
1018 1 my @dropped = values %pex_dropped;
1019 1 ▶ if ( @dropped > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dropped > 100) 0 1 1
1020 0 @dropped = splice( @dropped, 0, 100 );
1021 }
1022 1 my @added4 = grep { $_->{ip} !~ /:/ } @added;
1023 1 my @added6 = grep { $_->{ip} =~ /:/ } @added;
1024 1 my @dropped4 = grep { $_->{ip} !~ /:/ } @dropped;
1025 1 my @dropped6 = grep { $_->{ip} =~ /:/ } @dropped;
1026 1 for my $peer ( values %peer_objects ) {
1027 1 ▶ if ( $peer->protocol->isa('Net::BitTorrent::Protocol::BEP11') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer->protocol->isa("Net::BitTorrent::Protocol::BEP11")) 1 0 1
1028  
1029 # Filter out the peer itself from the added list
1030 1 ▶ my @final_added4 = grep { $_->{ip} ne $peer->ip || $_->{port} != $peer->port } @added4;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"ip"} ne $peer->ip or $_->{"port"} != $peer->port 0 1 1
1031 1 ▶ my @final_added6 = grep { $_->{ip} ne $peer->ip || $_->{port} != $peer->port } @added6;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"ip"} ne $peer->ip or $_->{"port"} != $peer->port 0 0 0
1032 1 ▶ next unless @final_added4 || @final_added6 || @dropped4 || @dropped6;
Branch: 1 branch
BranchTrueFalseTotal
unless @final_added4 or @final_added6 or @dropped4 or @dropped6 0 1 1
Condition33%3/9
@final_added4 or @final_added6 or @dropped4 or @dropped6
l!l&&r!l&&!r
100
@final_added4 or @final_added6 or @dropped4
l!l&&r!l&&!r
100
@final_added4 or @final_added6
l!l&&r!l&&!r
100
MC/DC0%0/4
@final_added4 or @final_added6 or @dropped4 or @dropped6
@final_added4 @final_added6 @dropped4 @dropped6
Truth table20%1/5
A or B or C or D
A= @final_added4B= @final_added6C= @dropped4D= @dropped6
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1033 1 $peer->protocol->send_pex( \@final_added4, \@dropped4, \@final_added6, \@dropped6 );
1034 }
1035 }
1036 1 %pex_added = ();
1037 1 %pex_dropped = ();
1038 }
1039  
1040 ▶ method fetch_from_webseeds ($index) {
Subroutine fetch_from_webseeds: not called CC=5 SCAR=34.0
Pod undocumented
1041 0 my $segments = $storage->map_v1_piece($index);
1042 0 ▶ return 0 unless @$segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @$segments 0 0 0
1043 0 for my $seg (@$segments) {
1044 0 $seg->{rel_path} = $seg->{file}->path->relative($base_path)->stringify;
1045 }
1046 0 for my $ws (@webseeds) {
1047 0 try {
1048 0 my $data = $ws->fetch_piece($segments);
1049 0 ▶ if ( $storage->verify_piece_v1( $index, $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage->verify_piece_v1($index, $data)) 0 0 0
1050 0 $storage->write_piece_v1( $index, $data );
1051 0 $bitfield->set($index);
1052 0 $bytes_downloaded += length($data);
1053 0 $bytes_left -= length($data);
1054 0 $self->_emit( 'piece_verified', $index );
1055 0 return 1;
1056 }
1057 }
1058 catch ($e) { }
1059 }
1060 0 return 0;
1061 }
1062  
1063 ▶ method primary_pieces_root () {
Subroutine primary_pieces_root: not called CC=1 SCAR=6.9
Pod documented
1064 0 return $self->_find_first_root( $self->file_tree );
1065 }
1066  
1067 ▶ method _find_first_root ($tree) {
Subroutine _find_first_root: not called CC=4 SCAR=30.0
1068 0 for my $node ( values %$tree ) {
1069 0 ▶ if ( exists $node->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{''}) { } 0 0 0
1070 0 return $node->{''}{'pieces root'};
1071 }
1072 else {
1073 0 my $r = $self->_find_first_root($node);
1074 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
1075 }
1076 }
1077 0 return undef;
1078 }
1079 26 field $_cached_total_size = undef;
1080  
1081 ▶ method _calculate_total_size () {
Subroutine _calculate_total_size: called CC=6 SCAR=18.4
1082 149 ▶ return $_cached_total_size if defined $_cached_total_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_cached_total_size 82 67 149
1083 67 my $total = 0;
1084 67 my $info = $metadata->{info};
1085 67 ▶ if ( $info->{'file tree'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'file tree'}) { } 47 20 67
1086 47 $total = $self->_sum_file_tree( $info->{'file tree'} );
1087 }
1088 else {
1089 20 ▶ $total = $info->{length} // 0;
Condition100%2/2
$info->{"length"} // 0
l!l
164
MC/DC100%1/1
$info->{"length"} // 0
$info->{"length"}
Truth table100%2/2
A
A= $info->{"length"}
Aresult
11
00
1090 20 ▶ if ( $info->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"files"}) 0 20 20
1091 0 for my $f ( @{ $info->{files} } ) {
1092 0 $total += $f->{length};
1093 }
1094 }
1095 }
1096 67 $_cached_total_size = $total;
1097 67 return $total;
1098 }
1099  
1100 ▶ method _sum_file_tree ($tree) {
Subroutine _sum_file_tree: called CC=3 SCAR=11.0
1101 49 my $total = 0;
1102 49 for my $node ( values %$tree ) {
1103 57 ▶ if ( exists $node->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{''}) { } 55 2 57
1104 55 $total += $node->{''}{length};
1105 }
1106 else {
1107 2 $total += $self->_sum_file_tree($node);
1108 }
1109 }
1110 49 return $total;
1111 }
1112  
1113 57 ▶ method announce ( $event = undef, $cb = undef ) {
Subroutine announce: called CC=11 SCAR=27.6
Pod undocumented
1114 57 ▶ return unless defined $tracker_manager;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tracker_manager 0 57 57
1115 57 my @ihs;
1116 57 ▶ push @ihs, $infohash_v2 if $infohash_v2;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v2 35 22 57
1117 57 ▶ push @ihs, $infohash_v1 if $infohash_v1;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v1 57 0 57
1118 57 ▶ my $params = {
Branch: 2 branches
BranchTrueFalseTotal
$client && $client->can('listen_port') ? : 0 57 57
$client && $client->can('user_agent') ? : 56 1 57
Condition50%3/6
$client and $client->can("listen_port")
!ll&&!rl&&r
0570
$client and $client->can("user_agent")
!ll&&!rl&&r
0156
MC/DC0%0/2
$client and $client->can("listen_port")
$client $client->can("listen_port")
MC/DC50%1/2
$client and $client->can("user_agent")
$client $client->can("user_agent")
Truth table33%1/3
A and B
A= $clientB= $client->can("listen_port")
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $clientB= $client->can("user_agent")
ABresult
0X0
100
111
1119 infohash => \@ihs,
1120 peer_id => $peer_id,
1121 port => ( $client && $client->can('listen_port') ? $client->listen_port : 6881 ),
1122 uploaded => $bytes_uploaded,
1123 downloaded => $bytes_downloaded,
1124 left => $bytes_left,
1125 compact => 1,
1126 ( $client && $client->can('user_agent') ? ( ua => $client->user_agent ) : () ),
1127 };
1128 57 ▶ $params->{event} = $event if $event;
Branch: 1 branch
BranchTrueFalseTotal
if $event 49 8 57
1129 57 my $weak_self = $self;
1130 57 builtin::weaken($weak_self);
1131 ▶ my $on_peers = sub ($peers) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1132 0 ▶ return unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 0 0
1133 0 $weak_self->add_peer($_) for @$peers;
1134 0 ▶ $cb->($peers) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
1135 57 };
1136 57 $tracker_manager->announce_all( $params, $on_peers );
1137 57 return [ values %peers ];
1138 }
1139  
1140 ▶ method add_peer ($peer) {
Subroutine add_peer: called CC=8 SCAR=21.3
Pod documented
1141 10142 my $ip;
1142 10142 try {
1143 10142 $ip = $peer->ip;
1144 }
1145 catch ($e) {
1146 10106 ▶ $ip = $peer->{ip} // $peer->{address};
Condition33%1/3
$peer->{"ip"} // $peer->{"address"}
l!l&&r!l&&!r
1010600
MC/DC0%0/2
$peer->{"ip"} // $peer->{"address"}
$peer->{"ip"} $peer->{"address"}
Truth table33%1/3
A // B
A= $peer->{"ip"}B= $peer->{"address"}
ABresult
1X1
011
000
1147 }
1148 10142 my $port;
1149 10142 try {
1150 10142 $port = $peer->port;
1151 }
1152 catch ($e) {
1153 10106 $port = $peer->{port};
1154 }
1155 10142 ▶ $self->_emit_log( 'debug', "Torrent::add_peer: $ip:$port" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 10142 10142
1156 10142 ▶ return unless $ip && $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip and $port 0 10142 10142
Condition33%1/3
$ip and $port
!ll&&!rl&&r
0010142
MC/DC0%0/2
$ip and $port
$ip $port
Truth table33%1/3
A and B
A= $ipB= $port
ABresult
0X0
100
111
1157 10142 my $key = "$ip:$port";
1158  
1159 # Cap discovered peers to prevent memory exhaustion
1160 10142 ▶ return if keys %peers >= MAX_PEERS && !$peers{$key};
Branch: 1 branch
BranchTrueFalseTotal
if keys %peers >= 10000 and not $peers{$key} 1 10141 10142
Condition66%2/3
keys %peers >= 10000 and not $peers{$key}
!ll&&!rl&&r
1014101
MC/DC50%1/2
keys %peers >= 10000 and not $peers{$key}
keys %peers >= 10000 not $peers{$key}
Truth table66%2/3
A and not(B)
A= keys %peers >= 10000B= not $peers{$key}
ABresult
0X0
100
111
1161 10141 ▶ unless ( $peers{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($peers{$key}) 10106 35 10141
1162 10106 my $flags = 0;
1163 10106 try {
1164 10106 $flags = $peer->flags;
1165 }
1166 catch ($e) { }
1167 10106 $peers{$key} = { ip => $ip, port => $port, flags => $flags };
1168 10106 $pex_added{$key} = $peers{$key};
1169 10106 delete $pex_dropped{$key};
1170 10106 $self->_emit( 'peer_discovered', $peers{$key} );
1171 }
1172 }
1173  
1174 ▶ method add_dht_nodes ($nodes) {
Subroutine add_dht_nodes: called CC=11 SCAR=32.6
Pod documented
1175 14 for my $node (@$nodes) {
1176 548 my ( $id, $ip, $port );
1177 548 ▶ if ( ref $node eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 548 0 548
elsif (ref $node) { } 0 0 0
1178 548 $id = $node->{id};
1179 548 ▶ $ip = $node->{ip} || $node->{address};
Condition33%1/3
$node->{'ip'} || $node->{'address'}
l!l&&r!l&&!r
54800
MC/DC0%0/2
$node->{'ip'} || $node->{'address'}
$node->{'ip'} $node->{'address'}
Truth table33%1/3
A || B
A= $node->{'ip'}B= $node->{'address'}
ABresult
1X1
011
000
1180 548 $port = $node->{port};
1181 }
1182 elsif ( ref $node ) {
1183 0 try {
1184 0 ▶ if ( $node->can('id') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("id")) 0 0 0
1185 0 $id = $node->id;
1186 0 $ip = $node->ip;
1187 0 $port = $node->port;
1188 }
1189 }
1190 catch ($e) { }
1191 }
1192 548 ▶ next unless $id && $ip && $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $id and $ip and $port 0 548 548
Condition33%2/6
$id and $ip and $port
!ll&&!rl&&r
00548
$id and $ip
!ll&&!rl&&r
00548
MC/DC0%0/3
$id and $ip and $port
$id $ip $port
Truth table25%1/4
A and B and C
A= $idB= $ipC= $port
ABCresult
0XX0
10X0
1100
1111
1193 548 my $nid_hex = unpack( 'H*', $id );
1194 548 ▶ next if exists $dht_frontier{$nid_hex};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dht_frontier{$nid_hex} 490 58 548
1195  
1196 # Cap frontier size
1197 58 ▶ if ( keys %dht_frontier > 500 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %dht_frontier > 500) 0 58 58
1198  
1199 # Remove a random unvisited node or the furthest one?
1200 # For simplicity, just stop adding if full.
1201 # In a real client we might want to replace less-desirable nodes.
1202 0 next;
1203 }
1204 58 $dht_frontier{$nid_hex} = { id => $id, ip => $ip, port => $port, visited => 0 };
1205 }
1206 }
1207  
1208 ▶ method ban_peer ( $ip, $port ) {
Subroutine ban_peer: called CC=1 SCAR=0.0
Pod documented
1209 1 my $key = "$ip:$port";
1210 1 delete $peers{$key};
1211 1 $attempted_connections{$key} = time() + 3600; # Ban for an hour
1212 }
1213  
1214 ▶ method _cleanup_connections () {
Subroutine _cleanup_connections: called CC=4 SCAR=18.7
1215 2 my $now = time();
1216 2 for my $key ( keys %attempted_connections ) {
1217 1 ▶ if ( $now - $attempted_connections{$key} > 3600 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $attempted_connections{$key} > 3600) 0 1 1
1218 0 delete $attempted_connections{$key};
1219 }
1220 }
1221 2 ▶ if ( keys %attempted_connections > MAX_ATTEMPTED ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %attempted_connections > 5000) 0 2 2
1222 0 my $to_delete = scalar( keys %attempted_connections ) - MAX_ATTEMPTED;
1223 0 my @oldest = ( sort { $attempted_connections{$a} <=> $attempted_connections{$b} } keys %attempted_connections )[ 0 .. $to_delete - 1 ];
1224 0 delete @attempted_connections{@oldest};
1225 }
1226 }
1227  
1228 ▶ method register_peer_object ($peer_obj) {
Subroutine register_peer_object: called CC=1 SCAR=0.0
Pod documented
1229 51 my $key = $peer_obj->ip . ':' . $peer_obj->port;
1230 51 $peer_objects{$key} = $peer_obj;
1231 }
1232  
1233 ▶ method start_dht_lookup () {
Subroutine start_dht_lookup: called CC=8 SCAR=21.0
Pod documented
1234 50 ▶ return if $is_private;
Branch: 1 branch
BranchTrueFalseTotal
if $is_private 0 50 50
1235 50 my $dht = $client->dht();
1236 50 ▶ return unless $dht;
Branch: 1 branch
BranchTrueFalseTotal
unless $dht 3 47 50
1237 47 my @ihs;
1238 47 ▶ push @ihs, $infohash_v2 if $infohash_v2;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v2 35 12 47
1239 47 ▶ push @ihs, $infohash_v1 if $infohash_v1;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v1 47 0 47
1240  
1241 # Explicitly ask bootstrap nodes.
1242 # This forces a query even if the local routing table is empty.
1243 47 my @boot_nodes = ( [ 'router.bittorrent.com', 6881 ], [ 'router.utorrent.com', 6881 ], [ 'dht.transmissionbt.com', 6881 ], );
1244 47 for my $ih (@ihs) {
1245 82 ▶ $self->_emit_log( 'debug', 'Starting DHT peer search for ' . unpack( 'H*', $ih ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 82 82
1246 82 $dht->find_peers($ih); # Query local routing table
1247 82 for my $node (@boot_nodes) { # Force query to bootstrap nodes
1248  
1249 # Resolve hostname if needed (get_peers expects IP)
1250 # But dht->get_peers might handle hostnames if IO::Socket::IP does?
1251 # Let's assume the DHT module handles resolution or the socket does.
1252 # Actually, standard DHT expects IP.
1253 # Let's trust the DHT module's resolving or the fact that we passed these as boot_nodes.
1254 # Wait, get_peers sends a packet. UDP sendto needs packed address or IP.
1255 # IO::Socket::IP can handle hostnames in send() usually.
1256 246 $dht->get_peers( $ih, $node->[0], $node->[1] );
1257 }
1258 }
1259 }
1260  
1261 ▶ method _update_dht_search () {
Subroutine _update_dht_search: called CC=15 SCAR=28.0
1262 3 ▶ return if $is_private;
Branch: 1 branch
BranchTrueFalseTotal
if $is_private 0 3 3
1263 3 my $dht = $client->dht();
1264 3 ▶ return unless $dht;
Branch: 1 branch
BranchTrueFalseTotal
unless $dht 0 3 3
1265 3 my @ihs;
1266 3 ▶ push @ihs, $infohash_v2 if $infohash_v2;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v2 0 3 3
1267 3 ▶ push @ihs, $infohash_v1 if $infohash_v1;
Branch: 1 branch
BranchTrueFalseTotal
if $infohash_v1 3 0 3
1268 3 for my $ih (@ihs) {
1269  
1270 # Merge routing table nodes into our search frontier
1271 # Net::BitTorrent::DHT::routing_table->find_closest returns objects
1272 # where the data is in {data}{ip} and {data}{port}
1273 3 my @closest_in_table = $dht->routing_table->find_closest( $ih, 50 );
1274 3 for my $node (@closest_in_table) {
1275 16 my $nid_hex = unpack( 'H*', $node->{id} );
1276 16 ▶ next if exists $dht_frontier{$nid_hex};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dht_frontier{$nid_hex} 16 0 16
1277 0 $dht_frontier{$nid_hex} = { id => $node->{id}, ip => $node->{data}{ip}, port => $node->{data}{port}, visited => 0 };
1278 }
1279  
1280 # Pick the top N closest unvisited candidates
1281 # Note: ^. is bitwise XOR on strings in Modern Perl
1282 3 ▶ my @to_query = sort { ( $a->{id} ^.$ih ) cmp ( $b->{id} ^.$ih ) } grep { !$_->{visited} && $_->{ip} } values %dht_frontier;
Branch: 1 branch
BranchTrueFalseTotal
not $_->{"visited"} and $_->{"ip"} 13 4 17
1283 3 ▶ if (@to_query) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_query) { } 2 1 3
1284 2 my $best_dist = unpack( 'H*', $to_query[0]{id} ^.$ih );
1285 2 ▶ $self->_emit_log( 'debug', sprintf( 'DHT Frontier: %d nodes. Best dist: %s', scalar( keys %dht_frontier ), $best_dist ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
1286 2 my $count = 0;
1287 2 for my $c (@to_query) {
1288 12 ▶ $self->_emit_log( 'debug', 'DHT Querying: ' . unpack( 'H*', $c->{id} ) . " at $c->{ip}:$c->{port}" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
1289 12 $dht->get_peers( $ih, $c->{ip}, $c->{port} );
1290 12 $c->{visited} = 1;
1291 12 ▶ last if ++$count >= 8;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count >= 8 1 11 12
1292 }
1293 }
1294 else {
1295 1 ▶ $self->_emit_log( 'debug', 'DHT Frontier exhausted for ' . unpack( 'H*', $ih ) . '. Re-bootstrapping...' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
1296 1 $self->start_dht_lookup();
1297  
1298 # Fallback: If we are starving, try adding a public tracker if not already present
1299 #~ state $added_fallback = 0;
1300 #~ if ( !$added_fallback && keys %peer_objects < 5 ) {
1301 #~ warn " [DEBUG] Adding fallback OpenTrackr\n" if $debug;
1302 #~ $tracker_manager->add_tracker('udp://tracker.opentrackr.org:1337/announce');
1303 #~ $self->announce('started');
1304 #~ $added_fallback = 1;
1305 #~ }
1306 }
1307 }
1308 }
1309  
1310 ▶ method _sort_peers_rfc6724 ($peer_list) {
Subroutine _sort_peers_rfc6724: called CC=15 SCAR=33.8
1311 13 ▶ my $has_v6 = $client->dht && $client->dht->want_v6;
Condition66%2/3
$client->dht && $client->dht->want_v6
!ll&&!rl&&r
2011
MC/DC50%1/2
$client->dht && $client->dht->want_v6
$client->dht $client->dht->want_v6
Truth table66%2/3
A && B
A= $client->dhtB= $client->dht->want_v6
ABresult
0X0
100
111
1312 return [
1313 sort {
1314 13 ▶ my $a_v6 = ( $a->{ip} =~ /:/ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$Net::BitTorrent::Torrent::a->{'ip'} =~ /:/u ? : 0 331619 331619
1315 331619 ▶ my $b_v6 = ( $b->{ip} =~ /:/ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$Net::BitTorrent::Torrent::b->{'ip'} =~ /:/u ? : 0 331619 331619
1316 331619 ▶ my $a_ll = ( $a->{ip} =~ /^fe80:/i ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$Net::BitTorrent::Torrent::a->{'ip'} =~ /^fe80:/iu ? : 0 331619 331619
1317 331619 ▶ my $b_ll = ( $b->{ip} =~ /^fe80:/i ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$Net::BitTorrent::Torrent::b->{'ip'} =~ /^fe80:/iu ? : 0 331619 331619
1318 331619 ▶ if ($has_v6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_v6) 331619 0 331619
1319  
1320 # Prefer Link-Local
1321 331619 ▶ return -1 if $a_ll && !$b_ll;
Branch: 1 branch
BranchTrueFalseTotal
if $a_ll and not $b_ll 0 331619 331619
Condition33%1/3
$a_ll and not $b_ll
!ll&&!rl&&r
33161900
MC/DC0%0/2
$a_ll and not $b_ll
$a_ll not $b_ll
Truth table33%1/3
A and not(B)
A= $a_llB= not $b_ll
ABresult
0X0
100
111
1322 331619 ▶ return 1 if !$a_ll && $b_ll;
Branch: 1 branch
BranchTrueFalseTotal
if not $a_ll and $b_ll 0 331619 331619
Condition33%1/3
not $a_ll and $b_ll
!ll&&!rl&&r
03316190
MC/DC0%0/2
not $a_ll and $b_ll
not $a_ll $b_ll
Truth table33%1/3
not(A) and B
A= not $a_llB= $b_ll
ABresult
0X0
100
111
1323  
1324 # Prefer Global IPv6
1325 331619 ▶ return -1 if $a_v6 && !$b_v6;
Branch: 1 branch
BranchTrueFalseTotal
if $a_v6 and not $b_v6 0 331619 331619
Condition33%1/3
$a_v6 and not $b_v6
!ll&&!rl&&r
33161900
MC/DC0%0/2
$a_v6 and not $b_v6
$a_v6 not $b_v6
Truth table33%1/3
A and not(B)
A= $a_v6B= not $b_v6
ABresult
0X0
100
111
1326 331619 ▶ return 1 if !$a_v6 && $b_v6;
Branch: 1 branch
BranchTrueFalseTotal
if not $a_v6 and $b_v6 0 331619 331619
Condition33%1/3
not $a_v6 and $b_v6
!ll&&!rl&&r
03316190
MC/DC0%0/2
not $a_v6 and $b_v6
not $a_v6 $b_v6
Truth table33%1/3
not(A) and B
A= not $a_v6B= $b_v6
ABresult
0X0
100
111
1327 }
1328  
1329 # Tie-break: Randomize
1330 331619 return rand() <=> rand();
1331 } @$peer_list
1332 ];
1333 }
1334  
1335 ▶ method discovered_peers () {
Subroutine discovered_peers: called CC=1 SCAR=0.0
Pod documented
1336 13 my @list = values %peers;
1337 13 return $self->_sort_peers_rfc6724( \@list );
1338 }
1339 178 ▶ method infohash_v1 () {$infohash_v1}
Subroutine infohash_v1: called CC=1 SCAR=0.0
Pod documented
1340 134 ▶ method infohash_v2 () {$infohash_v2}
Subroutine infohash_v2: called CC=1 SCAR=0.0
Pod documented
1341 0 ▶ method peer_id () {$peer_id}
Subroutine peer_id: not called CC=1 SCAR=6.9
Pod documented
1342 0 ▶ method trackers () { return $tracker_manager->trackers() }
Subroutine trackers: not called CC=1 SCAR=6.9
Pod documented
1343  
1344 ▶ method DESTROY () {
Subroutine DESTROY: called CC=3 SCAR=11.0
1345 82 ▶ return unless $state != STATE_STOPPED;
Branch: 1 branch
BranchTrueFalseTotal
unless $state != 0 48 34 82
1346 34 for my $peer ( grep {defined} values %peer_objects ) {
1347 30 $peer->disconnected();
1348 }
1349 }
1350  
1351 ▶ method files () {
Subroutine files: not called CC=2 SCAR=17.9
Pod documented
1352 0 ▶ return [] unless $storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage 0 0 0
1353 0 return [ map { $_->path->absolute->stringify } $storage->files_ordered->@* ];
1354 }
1355  
1356 ▶ method dump_state () {
Subroutine dump_state: called CC=1 SCAR=0.0
Pod documented
1357 return {
1358 1 metadata => $metadata,
1359 bitfield => $bitfield->data,
1360 storage => $storage->dump_state(),
1361 downloaded => $bytes_downloaded,
1362 uploaded => $bytes_uploaded
1363 };
1364 }
1365  
1366 ▶ method load_state ($state) {
Subroutine load_state: called CC=14 SCAR=26.7
Pod documented
1367 3 ▶ return unless ref $state eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $state eq "HASH" 0 3 3
1368 3 ▶ if ( exists $state->{metadata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"metadata"}) 2 1 3
1369 2 ▶ if ( ref $state->{metadata} eq 'HASH' && ref $state->{metadata}{info} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state->{'metadata'} eq 'HASH' and ref $state->{'metadata'}{'info'} eq 'HASH') { } 1 1 2
Condition66%2/3
ref $state->{"metadata"} eq "HASH" and ref $state->{"metadata"}{"info"} eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $state->{"metadata"} eq "HASH" and ref $state->{"metadata"}{"info"} eq "HASH"
ref $state->{"metadata"} eq "HASH" ref $state->{"metadata"}{"info"} eq "HASH"
Truth table66%2/3
A and B
A= ref $state->{"metadata"} eq "HASH"B= ref $state->{"metadata"}{"info"} eq "HASH"
ABresult
0X0
100
111
1370 1 $metadata = $state->{metadata};
1371 1 $self->_init_from_metadata();
1372 }
1373 else {
1374 1 $self->_emit_log( 'warn', 'load_state: invalid metadata structure, skipping' );
1375 }
1376 }
1377 3 ▶ if ( exists $state->{bitfield} && defined $state->{bitfield} && $bitfield ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"bitfield"} and defined $state->{"bitfield"} and $bitfield) 2 1 3
Condition66%4/6
exists $state->{"bitfield"} and defined $state->{"bitfield"} and $bitfield
!ll&&!rl&&r
102
exists $state->{"bitfield"} and defined $state->{"bitfield"}
!ll&&!rl&&r
102
MC/DC33%1/3
exists $state->{"bitfield"} and defined $state->{"bitfield"} and $bitfield
exists $state->{"bitfield"} defined $state->{"bitfield"} $bitfield
Truth table50%2/4
A and B and C
A= exists $state->{"bitfield"}B= defined $state->{"bitfield"}C= $bitfield
ABCresult
0XX0
10X0
1100
1111
1378 2 ▶ if ( length( $state->{bitfield} ) == int( ( $bitfield->size + 7 ) / 8 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{'bitfield'} == int(($bitfield->size + 7) / 8)) { } 1 1 2
1379 1 $bitfield->set_data( $state->{bitfield} );
1380 1 ▶ my $piece_len = $metadata->{info}{'piece length'} // 16384;
Condition50%1/2
$metadata->{"info"}{"piece length"} // 16384
l!l
10
MC/DC0%0/1
$metadata->{"info"}{"piece length"} // 16384
$metadata->{"info"}{"piece length"}
Truth table50%1/2
A
A= $metadata->{"info"}{"piece length"}
Aresult
11
00
1381 1 $bytes_left = ( $bitfield->size - $bitfield->count ) * $piece_len;
1382 }
1383 else {
1384 1 $self->_emit_log( 'warn', 'load_state: bitfield size mismatch, skipping' );
1385 }
1386 }
1387 3 ▶ $storage->load_state( $state->{storage} ) if exists $state->{storage} && $storage;
Branch: 1 branch
BranchTrueFalseTotal
if exists $state->{"storage"} and $storage 1 2 3
Condition66%2/3
exists $state->{"storage"} and $storage
!ll&&!rl&&r
201
MC/DC50%1/2
exists $state->{"storage"} and $storage
exists $state->{"storage"} $storage
Truth table66%2/3
A and B
A= exists $state->{"storage"}B= $storage
ABresult
0X0
100
111
1388 3 ▶ $bytes_downloaded = $state->{downloaded} // 0;
Condition100%2/2
$state->{"downloaded"} // 0
l!l
12
MC/DC100%1/1
$state->{"downloaded"} // 0
$state->{"downloaded"}
Truth table100%2/2
A
A= $state->{"downloaded"}
Aresult
11
00
1389 3 ▶ $bytes_uploaded = $state->{uploaded} // 0;
Condition100%2/2
$state->{"uploaded"} // 0
l!l
12
MC/DC100%1/1
$state->{"uploaded"} // 0
$state->{"uploaded"}
Truth table100%2/2
A
A= $state->{"uploaded"}
Aresult
11
00
1390 }
1391  
1392 ▶ method file_tree () {
Subroutine file_tree: called CC=10 SCAR=34.9
Pod undocumented
1393 61 my $info = $metadata->{info};
1394 61 ▶ if ( $info->{'file tree'} ) { return $info->{'file tree'} }
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"file tree"}) 47 14 61
1395 14 my $tree = {};
1396 14 ▶ if ( $info->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'files'}) { } 0 14 14
1397 0 for my $f ( @{ $info->{files} } ) {
1398 0 my $curr = $tree;
1399 0 ▶ my @path = grep { $_ ne '' && $_ ne '.' && $_ ne '..' } @{ $f->{path} };
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "" and $_ ne "." and $_ ne ".." 0 0 0
Condition0%0/3
$_ ne "" and $_ ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne "" and $_ ne "."
$_ ne "" $_ ne "."
Truth table0%0/3
A and B
A= $_ ne ""B= $_ ne "."
ABresult
0X0
100
111
1400 0 my $filename = pop @path;
1401 0 for my $dir (@path) {
1402 0 ▶ $curr->{$dir} //= {};
Condition0%0/2
$curr->{$dir} //= {}
l!l
00
MC/DC0%0/1
$curr->{$dir} //= {}
$curr->{$dir}
Truth table0%0/2
A
A= $curr->{$dir}
Aresult
11
00
1403 0 $curr = $curr->{$dir};
1404 }
1405 0 ▶ next unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
1406 0 $curr->{$filename} = { '' => { length => $f->{length} } };
1407 }
1408 }
1409 else {
1410 14 my $name = $info->{name};
1411 14 $name =~ s|[\\/]+|_|g;
1412 14 ▶ $tree->{$name} = { '' => { length => $info->{length} // 0 } };
Condition100%2/2
$info->{"length"} // 0
l!l
104
MC/DC100%1/1
$info->{"length"} // 0
$info->{"length"}
Truth table100%2/2
A
A= $info->{"length"}
Aresult
11
00
1413 }
1414 14 return $tree;
1415 }
1416 };
1417 26 1;
« lib/Net/BitTorrent.pm ↑ Summary lib/Net/BitTorrent/DHT.pm »