blib/lib/Unblock/HTTP3/Capsule.pm

filter: Statementstmt 91.3% 63 / 69 Branchbran 67.6% 23 / 34 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 75.8% 110 / 145 CC 24 SCAR 17.8
CC
24
Cov
77%
_normalize_varint
13.5
_decode_varint
7.3
_encode_varint
4.3
CRAP 5.9
« blib/lib/Unblock/HTTP3/_Native.pm ↑ Summary blib/lib/Unblock/HTTP3/Body/Stream.pm »
1 package Unblock::HTTP3::Capsule;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Unblock::HTTP3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Unblock::HTTP3::_Bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.13';
12  
13 4 my $MAX_VARINT = '4611686018427387903';
14  
15 59 ▶sub _normalize_varint {
Subroutine _normalize_varint: called CC=8 SCAR=26.0
16 59 my ($value, $name) = @_;
17  
18 59 ▶ $name ||= 'capsule integer';
Condition50%1/2
$name ||= "capsule integer"
l!l
590
MC/DC0%0/1
$name ||= "capsule integer"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
19  
20 59 ▶ croak "$name must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/ 0 59 59
Condition33%2/6
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/
l!l&&r!l&&!r
0059
not defined $value or ref $value
l!l&&r!l&&!r
0059
MC/DC0%0/3
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/
not defined $value ref $value not "$value" =~ /\A[0-9]+\z/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not "$value" =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
21 if !defined($value)
22 || ref($value)
23 || "$value" !~ /\A[0-9]+\z/;
24  
25 59 my $text = "$value";
26 59 $text =~ s/\A0+(?=[0-9])//;
27  
28 59 ▶ croak "$name exceeds the HTTP/3 varint maximum"
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $text > CORE::length $MAX_VARINT or CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT 0 59 59
Condition33%2/6
CORE::length $text > CORE::length $MAX_VARINT or CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
l!l&&r!l&&!r
0059
CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
!ll&&!rl&&r
5900
MC/DC0%0/3
CORE::length $text > CORE::length $MAX_VARINT or CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
CORE::length $text > CORE::length $MAX_VARINT CORE::length $text == CORE::length $MAX_VARINT $text gt $MAX_VARINT
Truth table25%1/4
A or B and C
A= CORE::length $text > CORE::length $MAX_VARINTB= CORE::length $text == CORE::length $MAX_VARINTC= $text gt $MAX_VARINT
ABCresult
1XX1
0111
00X0
0100
29 if length($text) > length($MAX_VARINT)
30 || (
31 length($text) == length($MAX_VARINT)
32 && $text gt $MAX_VARINT
33 );
34  
35 59 return $text;
36 }
37  
38 32 ▶sub _encode_varint {
Subroutine _encode_varint: called CC=4 SCAR=14.6
39 32 my ($value) = @_;
40  
41 32 $value = _normalize_varint($value, 'capsule integer');
42 32 my $number = 0 + $value;
43  
44 32 ▶ return pack('C', $number)
Branch: 1 branch
BranchTrueFalseTotal
if $number <= 63 27 5 32
45 if $number <= 63;
46 5 ▶ return pack('n', $number | 0x4000)
Branch: 1 branch
BranchTrueFalseTotal
if $number <= 16383 4 1 5
47 if $number <= 16_383;
48 1 ▶ return pack('N', $number | 0x80000000)
Branch: 1 branch
BranchTrueFalseTotal
if $number <= 1073741823 1 0 1
49 if $number <= 1_073_741_823;
50  
51 0 my $high = (($number >> 32) & 0x3fffffff) | 0xc0000000;
52 0 my $low = $number & 0xffffffff;
53  
54 0 return pack('NN', $high, $low);
55 }
56  
57 44 ▶sub _decode_varint {
Subroutine _decode_varint: called CC=7 SCAR=19.9
58 44 my ($bytes, $offset) = @_;
59 44 ▶ $offset ||= 0;
Condition50%1/2
$offset ||= 0
l!l
044
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
60  
61 44 ▶ return if $offset >= length($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= CORE::length $bytes 12 32 44
62  
63 32 my $first = ord(substr($bytes, $offset, 1));
64 32 my $length = (1, 2, 4, 8)[ $first >> 6 ];
65  
66 32 ▶ return if length($bytes) - $offset < $length;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length($bytes) - $offset < $length 2 30 32
67  
68 30 my $value;
69  
70 30 ▶ if ($length == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length == 1) { } 26 4 30
elsif ($length == 2) { } 3 1 4
elsif ($length == 4) { } 1 0 1
71 26 $value = $first & 0x3f;
72 } elsif ($length == 2) {
73 3 $value = unpack('n', substr($bytes, $offset, 2)) & 0x3fff;
74 } elsif ($length == 4) {
75 1 $value = unpack('N', substr($bytes, $offset, 4)) & 0x3fffffff;
76 } else {
77 0 my ($high, $low) = unpack('NN', substr($bytes, $offset, 8));
78 0 $high &= 0x3fffffff;
79 0 $value = ($high << 32) | $low;
80 }
81  
82 30 return ("$value", $length);
83 }
84  
85 23 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
86 23 my ($class, %args) = @_;
87  
88 23 my $type = delete $args{type};
89 23 ▶ my $value = exists($args{value}) ? delete($args{value}) : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'value'} ? : 23 0 23
90  
91 23 ▶ croak 'Capsule type is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 23 23
92 unless defined $type;
93 23 ▶ croak 'unknown Capsule option: ' . join(', ', sort keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 23 23
94 if %args;
95  
96 23 $type = _normalize_varint($type, 'Capsule type');
97 23 $value = Unblock::HTTP3::_Bytes::byte_string('Capsule value', $value);
98  
99 23 return bless {
100 type => $type,
101 value => $value,
102 }, $class;
103 }
104  
105 9 ▶sub type {
Subroutine type: called CC=2 SCAR=7.1
Pod documented
106 9 my ($self, @args) = @_;
107 9 ▶ croak 'type() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 9 9
108 9 return $self->{type};
109 }
110  
111 9 ▶sub value {
Subroutine value: called CC=2 SCAR=7.1
Pod documented
112 9 my ($self, @args) = @_;
113 9 ▶ croak 'value() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 9 9
114 9 return $self->{value};
115 }
116  
117 1 ▶sub length {
Subroutine length: called CC=2 SCAR=7.1
Pod documented
118 1 my ($self, @args) = @_;
119 1 ▶ croak 'length() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 1 1
120 1 return CORE::length($self->{value});
121 }
122  
123 16 ▶sub encode {
Subroutine encode: called CC=2 SCAR=7.1
Pod documented
124 16 my ($self, @args) = @_;
125 16 ▶ croak 'encode() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 16 16
126  
127 return _encode_varint($self->{type})
128 . _encode_varint(CORE::length($self->{value}))
129 16 . $self->{value};
130 }
131  
132 4 1;
133  
134 __END__
« blib/lib/Unblock/HTTP3/_Native.pm ↑ Summary blib/lib/Unblock/HTTP3/Body/Stream.pm »