Stream/Codec/Netstring.pm

filter: Statementstmt 37.1% 13 / 35 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 26.8% 18 / 67 CC 10 SCAR 36.3
CC
10
Cov
24%
_decode
72.0
new
6.0
_encode
6.0
CRAP 37.8
↑ Summary Stream.pm »
1 package Linux::Event::Stream::Codec::Netstring;
2 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.002';
7  
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ▶sub new ($class, %opt) {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
11 0 my $max_frame = delete $opt{max_frame};
12 0 ▶ croak 'unknown options: ' . join(', ', sort keys %opt) if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 0 0
13  
14 0 return bless {
15 max_frame => $max_frame,
16 decode => \&_decode,
17 encode => \&_encode,
18 }, $class;
19 }
20  
21 ▶sub _decode ($self, $inref, $outref) {
Subroutine _decode: not called CC=8 SCAR=42.8
22 0 while (1) {
23 # Find ':' separating length from payload.
24 0 my $colon = index($$inref, ':');
25 0 ▶ last if $colon < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $colon < 0 0 0 0
26  
27 0 my $len_s = substr($$inref, 0, $colon);
28 0 ▶ return (0, 'invalid netstring length') if $len_s eq '' || $len_s =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $len_s eq "" or $len_s =~ /\D/u 0 0 0
Condition0%0/3
$len_s eq "" or $len_s =~ /\D/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$len_s eq "" or $len_s =~ /\D/u
$len_s eq "" $len_s =~ /\D/u
Truth table0%0/3
A or B
A= $len_s eq ""B= $len_s =~ /\D/u
ABresult
1X1
011
000
29  
30 0 my $len = 0 + $len_s;
31 0 my $max = $self->{max_frame};
32 0 ▶ if (defined($max) && $len > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and $len > $max) 0 0 0
Condition0%0/3
defined $max and $len > $max
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max and $len > $max
defined $max $len > $max
Truth table0%0/3
A and B
A= defined $maxB= $len > $max
ABresult
0X0
100
111
33 0 return (0, "frame too large (>$max bytes)");
34 }
35  
36 0 my $need = $colon + 1 + $len + 1; # <len>:<payload>,
37 0 ▶ last if length($$inref) < $need;
Branch: 1 branch
BranchTrueFalseTotal
if length $$inref < $need 0 0 0
38  
39 0 ▶ return (0, 'invalid netstring terminator') if substr($$inref, $need - 1, 1) ne ',';
Branch: 1 branch
BranchTrueFalseTotal
if substr($$inref, $need - 1, 1) ne "," 0 0 0
40  
41 0 my $payload = substr($$inref, $colon + 1, $len);
42 0 substr($$inref, 0, $need, '');
43  
44 0 push @$outref, $payload;
45 }
46  
47 0 return 1;
48 }
49  
50 ▶sub _encode ($self, $msg) {
Subroutine _encode: not called CC=2 SCAR=17.9
51 0 ▶ $msg = '' if !defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
52 0 $msg = "$msg";
53 0 return length($msg) . ':' . $msg . ',';
54 }
55  
56 7 1;
57  
58 __END__
↑ Summary Stream.pm »