lib/Net/BitTorrent/Torrent/Generator.pm

filter: Statementstmt 87.2% 96 / 110 Branchbran 67.6% 23 / 34 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 95.0% 19 / 20 Podpod 100% 8 / 8 totaltotal 77.3% 150 / 194 CC 27 SCAR 19.6
CC
27
Cov
78%
add_file
19.1
_generate_pieces_v1
8.0
_wrap_torrent
6.1
CRAP 7.1
« lib/Net/BitTorrent/Protocol/PeerHandler.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP03.pm »
1 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 class Net::BitTorrent::Torrent::Generator v2.1.1 : isa(Net::BitTorrent::Emitter) {
6 1 ▶ use Net::BitTorrent::Protocol::BEP03::Bencode qw[bencode];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use Digest::SHA qw[sha1 sha256];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 field $base_path : param;
10 1 field $piece_length : param = 262144; # 256KiB
11 1 field @files;
12 1 field @trackers;
13 1 field @nodes;
14 1 field $private = 0;
15 1 field $align_files = 0;
16 0 ▶ method set_align_files ($val) { $align_files = $val }
Subroutine set_align_files: not called CC=1 SCAR=6.9
Pod documented
17 1 ▶ method set_private ($val) { $private = $val }
Subroutine set_private: called CC=1 SCAR=0.0
Pod documented
18 1 ▶ method add_tracker ($url) { push @trackers, $url }
Subroutine add_tracker: called CC=1 SCAR=0.0
Pod documented
19 1 ▶ method add_node ( $h, $p ) { push @nodes, [ $h, $p ] }
Subroutine add_node: called CC=1 SCAR=0.0
Pod documented
20  
21 5 ▶ method add_file ($rel_path) {
Subroutine add_file: called CC=9 SCAR=29.5
Pod documented
22 5 my $bp = path($base_path)->absolute;
23 5 ▶ $bp = $bp->realpath if $bp->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $bp->exists 5 0 5
24 5 my $abs = $bp->child($rel_path)->absolute;
25  
26 # Canonicalize and check containment
27 5 ▶ my $abs_real = $abs->realpath if $abs->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->exists 5 0 5
28 5 ▶ $abs_real //= $abs;
Condition33%1/3
$abs_real //= $abs
l!l&&r!l&&!r
500
MC/DC0%0/2
$abs_real //= $abs
$abs_real $abs
Truth table33%1/3
A //= B
A= $abs_realB= $abs
ABresult
1X1
011
000
29 5 ▶ if ( $abs_real !~ /^\Q$bp\E/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abs_real =~ /^\Q$bp\E/u) 0 5 5
30 0 $self->_emit_log( 'fatal', "Path traversal attempt blocked: $rel_path resolves outside base_path" );
31 0 return;
32 }
33 5 ▶ if ( !$abs_real->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abs_real->is_file) 0 5 5
34 0 $self->_emit_log( 'fatal', "File does not exist or is not a regular file: $abs_real" );
35 0 return;
36 }
37 5 ▶ if ( $align_files && @files && $files[-1]{size} % $piece_length != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_files and @files and $files[-1]{"size"} % $piece_length != 0) 0 5 5
Condition33%2/6
$align_files and @files and $files[-1]{"size"} % $piece_length != 0
!ll&&!rl&&r
500
$align_files and @files
!ll&&!rl&&r
500
MC/DC0%0/3
$align_files and @files and $files[-1]{"size"} % $piece_length != 0
$align_files @files $files[-1]{"size"} % $piece_length != 0
Truth table25%1/4
A and B and C
A= $align_filesB= @filesC= $files[-1]{"size"} % $piece_length != 0
ABCresult
0XX0
10X0
1100
1111
38 0 my $pad = $piece_length - ( $files[-1]{size} % $piece_length );
39 0 push @files, { rel => ".pad/$pad", size => $pad, padding => 1 };
40 }
41 5 push @files, { rel => $rel_path, abs => $abs_real, size => $abs_real->stat->size };
42 }
43  
44 2 ▶ method generate_v1 () {
Subroutine generate_v1: called CC=1 SCAR=0.0
Pod documented
45 2 my $info = $self->_base_info();
46 2 $info->{pieces} = $self->_generate_pieces_v1();
47 2 return $self->_wrap_torrent($info);
48 }
49  
50 1 ▶ method generate_v2 () {
Subroutine generate_v2: called CC=1 SCAR=0.0
Pod documented
51 1 my ( $file_tree, $piece_layers ) = $self->_generate_v2_data();
52 1 my $info = {
53 name => path($base_path)->basename,
54 'piece length' => $piece_length,
55 'file tree' => $file_tree,
56 'meta version' => 2,
57 private => $private,
58 };
59 1 return $self->_wrap_torrent( $info, $piece_layers );
60 }
61  
62 1 ▶ method generate_hybrid () {
Subroutine generate_hybrid: called CC=1 SCAR=0.0
Pod documented
63 1 my ( $file_tree, $piece_layers ) = $self->_generate_v2_data();
64 1 my $info = $self->_base_info();
65 1 $info->{'file tree'} = $file_tree;
66 1 $info->{'meta version'} = 2;
67 1 $info->{pieces} = $self->_generate_pieces_v1();
68 1 return $self->_wrap_torrent( $info, $piece_layers );
69 }
70  
71 3 ▶ method _base_info () {
Subroutine _base_info: called CC=3 SCAR=13.5
72 3 my $info = { name => path($base_path)->basename, 'piece length' => $piece_length, private => $private, };
73 3 ▶ if ( @files == 1 && !$files[0]{padding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files == 1 and not $files[0]{'padding'}) { } 3 0 3
Condition33%1/3
@files == 1 and not $files[0]{"padding"}
!ll&&!rl&&r
003
MC/DC0%0/2
@files == 1 and not $files[0]{"padding"}
@files == 1 not $files[0]{"padding"}
Truth table33%1/3
A and not(B)
A= @files == 1B= not $files[0]{"padding"}
ABresult
0X0
100
111
74 3 $info->{length} = $files[0]{size};
75 }
76 else {
77 0 $info->{files} = [ map { { length => $_->{size}, path => [ split m{/}, $_->{rel} ] } } @files ];
78 }
79 3 return $info;
80 }
81  
82 4 ▶ method _wrap_torrent ( $info, $piece_layers = undef ) {
Subroutine _wrap_torrent: called CC=6 SCAR=18.0
83 4 my $torrent = { info => $info, 'created by' => 'Net::BitTorrent 2.0.0', 'creation date' => time(), };
84 4 ▶ $torrent->{'piece layers'} = $piece_layers if $piece_layers;
Branch: 1 branch
BranchTrueFalseTotal
if $piece_layers 2 2 4
85 4 ▶ $torrent->{announce} = $trackers[0] if @trackers;
Branch: 1 branch
BranchTrueFalseTotal
if @trackers 1 3 4
86 0 ▶ $torrent->{'announce-list'} = [ map { [$_] } @trackers ] if @trackers > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @trackers > 1 0 4 4
87 4 ▶ $torrent->{nodes} = \@nodes if @nodes;
Branch: 1 branch
BranchTrueFalseTotal
if @nodes 1 3 4
88 4 return bencode($torrent);
89 }
90  
91 3 ▶ method _generate_pieces_v1 () {
Subroutine _generate_pieces_v1: called CC=7 SCAR=20.7
92 3 my $pieces = '';
93 3 my $buffer = '';
94 3 for my $file (@files) {
95 3 ▶ if ( $file->{padding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"padding"}) 0 3 3
96 0 $buffer .= "\0" x $file->{size};
97 0 ▶ while ( length($buffer) >= $piece_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer >= $piece_length) 0 0 0
98 0 $pieces .= sha1( substr( $buffer, 0, $piece_length, '' ) );
99 }
100 0 next;
101 }
102 3 my $fh = $file->{abs}->openr_raw;
103 3 ▶ while ( read( $fh, my $chunk, $piece_length - length($buffer) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, $piece_length - length($buffer)) 5 3 8
104 5 $buffer .= $chunk;
105 5 ▶ if ( length($buffer) == $piece_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer == $piece_length) 4 1 5
106 4 $pieces .= sha1($buffer);
107 4 $buffer = '';
108 }
109 }
110 }
111 3 ▶ $pieces .= sha1($buffer) if length($buffer) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer > 0 1 2 3
112 3 return $pieces;
113 }
114  
115 2 ▶ method _generate_v2_data () {
Subroutine _generate_v2_data: called CC=6 SCAR=18.0
116 1 ▶ use Digest::Merkle::SHA256;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 2 my $file_tree = {};
118 2 my %piece_layers;
119 2 for my $file ( grep { !$_->{padding} } @files ) {
120 2 my $merkle = Digest::Merkle::SHA256->new( file_size => $file->{size} );
121 2 my $fh = $file->{abs}->openr_raw;
122 2 my $block_idx = 0;
123 2 ▶ while ( read( $fh, my $block, 16384 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $block, 16384) 4 2 6
124 4 $merkle->set_block( $block_idx++, sha256($block) );
125 }
126 2 my @path = split m{/}, $file->{rel};
127 2 my $curr = $file_tree;
128 2 my $name = pop @path;
129 2 ▶ $curr = ( $curr->{$_} //= {} ) for @path;
Condition0%0/2
$curr->{$_} //= {}
l!l
00
MC/DC0%0/1
$curr->{$_} //= {}
$curr->{$_}
Truth table0%0/2
A
A= $curr->{$_}
Aresult
11
00
130 2 $curr->{$name} = { '' => { length => $file->{size}, 'pieces root' => $merkle->root } };
131 2 ▶ if ( $file->{size} > $piece_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"size"} > $piece_length) 2 0 2
132 2 $piece_layers{ $merkle->root } = $merkle->get_piece_layer($piece_length);
133 }
134 }
135 2 return ( $file_tree, \%piece_layers );
136 }
137 1 } 1;
« lib/Net/BitTorrent/Protocol/PeerHandler.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP03.pm »