lib/Net/BitTorrent/Torrent.pm

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