blib/lib/Linux/Event/TLS.pm

filter: Statementstmt 99.1% 120 / 121 Branchbran 80.8% 55 / 68 Conditioncond 50.0% 30 / 60 MC/DCmcdc 18.7% 6 / 32 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 2 totaltotal 76.3% 236 / 309 CC 58 SCAR 18.9
CC
58
Cov
82%
_alpn_wire
12.3
client
9.6
_timeout
9.1
CRAP 6.6
« blib/lib/Linux/Event/Signal.pm ↑ Summary blib/lib/Linux/Event/Framer/Varint.pm »
1 package Linux::Event::TLS;
2 9 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '0.105';
7  
8 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use POSIX qw(isfinite);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 require XSLoader;
13 9 XSLoader::load(__PACKAGE__, $VERSION);
14  
15 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
16  
17 23 ▶sub import ($class, @arg) {
Subroutine import: called CC=6 SCAR=18.2
18 23 my $target = caller;
19 23 my $is_stream = $target->isa('Linux::Event::Stream');
20 23 ▶ return if $target eq 'main' && !$is_stream && !@arg;
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "main" and not $is_stream and not @arg 9 14 23
Condition66%4/6
$target eq "main" and not $is_stream and not @arg
!ll&&!rl&&r
1409
$target eq "main" and not $is_stream
!ll&&!rl&&r
1409
MC/DC33%1/3
$target eq "main" and not $is_stream and not @arg
$target eq "main" not $is_stream not @arg
Truth table50%2/4
A and not(B) and not(C)
A= $target eq "main"B= not $is_streamC= not @arg
ABCresult
0XX0
10X0
1100
1111
21 14 ▶ croak "$target must inherit from Linux::Event::Stream before declaring TLS"
Branch: 1 branch
BranchTrueFalseTotal
unless $is_stream 1 13 14
22 if !$is_stream;
23 13 ▶ croak 'TLS declaration options must be key/value pairs' if @arg % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @arg % 2 0 13 13
24 13 Linux::Event::Stream->_declare_tls(
25 $target,
26 $class->_build_declaration($target, @arg),
27 );
28 7 return;
29 }
30  
31 30 ▶sub _alpn_wire ($protocols) {
Subroutine _alpn_wire: called CC=11 SCAR=25.1
32 30 ▶ return '' if !defined $protocols;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protocols 22 8 30
33 8 ▶ croak 'alpn must be an array reference' if ref($protocols) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $protocols ne "ARRAY" 0 8 8
34 8 my $wire = '';
35 8 for my $protocol (@$protocols) {
36 263 ▶ my $bytes = defined($protocol) && !ref($protocol)
Branch: 1 branch
BranchTrueFalseTotal
defined $protocol && !ref($protocol) ? : 263 0 263
Condition33%1/3
defined $protocol and not ref $protocol
!ll&&!rl&&r
00263
MC/DC0%0/2
defined $protocol and not ref $protocol
defined $protocol not ref $protocol
Truth table33%1/3
A and not(B)
A= defined $protocolB= not ref $protocol
ABresult
0X0
100
111
37 ? "$protocol" : undef;
38 263 ▶ my $is_bytes = defined($bytes) && utf8::downgrade($bytes, 1);
Condition66%2/3
defined $bytes && utf8::downgrade($bytes, 1)
!ll&&!rl&&r
01262
MC/DC50%1/2
defined $bytes && utf8::downgrade($bytes, 1)
defined $bytes utf8::downgrade($bytes, 1)
Truth table66%2/3
A && B
A= defined $bytesB= utf8::downgrade($bytes, 1)
ABresult
0X0
100
111
39 263 ▶ croak 'each ALPN protocol must be a byte string of 1..255 bytes'
Branch: 1 branch
BranchTrueFalseTotal
if not $is_bytes or length $bytes < 1 or length $bytes > 255 1 262 263
Condition66%4/6
not $is_bytes or length $bytes < 1 or length $bytes > 255
l!l&&r!l&&!r
10262
not $is_bytes or length $bytes < 1
l!l&&r!l&&!r
10262
MC/DC33%1/3
not $is_bytes or length $bytes < 1 or length $bytes > 255
not $is_bytes length $bytes < 1 length $bytes > 255
Truth table50%2/4
not(A) or B or C
A= not $is_bytesB= length $bytes < 1C= length $bytes > 255
ABCresult
1XX1
01X1
0011
0000
40 if !$is_bytes || length($bytes) < 1 || length($bytes) > 255;
41 262 $wire .= pack('C', length($bytes)) . $bytes;
42 262 ▶ croak 'the encoded ALPN protocol list must not exceed 65535 bytes'
Branch: 1 branch
BranchTrueFalseTotal
if length $wire > 65535 1 261 262
43 if length($wire) > 65_535;
44 }
45 6 return $wire;
46 }
47  
48 55 ▶sub _timeout ($method, $name, $value, $default) {
Subroutine _timeout: called CC=8 SCAR=22.1
49 55 ▶ $value = $default if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 50 5 55
50 55 ▶ my $where = $method =~ /\s/ ? $method : "$method()";
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /\s/u ? : 17 38 55
51 55 ▶ croak "$where: $name must be a non-negative number of seconds"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0 0 55 55
Condition33%2/6
ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0
l!l&&r!l&&!r
0055
ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u
l!l&&r!l&&!r
0055
MC/DC0%0/3
ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0
ref $value not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u $value < 0
Truth table25%1/4
A or not(B) or C
A= ref $valueB= not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/uC= $value < 0
ABCresult
1XX1
01X1
0011
0000
52 if ref($value)
53 || $value !~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/
54 || $value < 0;
55 55 $value = 0 + $value;
56 55 ▶ croak "$where: $name must be a finite number of seconds"
Branch: 1 branch
BranchTrueFalseTotal
unless isfinite($value) 0 55 55
57 if !isfinite($value);
58 55 ▶ croak "$where: $name exceeds the supported timer range"
Branch: 1 branch
BranchTrueFalseTotal
if $value > 2147483647 1 54 55
59 if $value > 2_147_483_647;
60 54 return $value;
61 }
62  
63 111 ▶sub _optional_string ($target, $name, $value) {
Subroutine _optional_string: called CC=5 SCAR=16.7
64 111 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 61 50 111
65 50 ▶ croak "$target TLS $name must be a non-empty string without NUL bytes"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or $value eq "" or $value =~ /\0/u 2 48 50
Condition50%3/6
ref $value or $value eq "" or $value =~ /\0/u
l!l&&r!l&&!r
0248
ref $value or $value eq ""
l!l&&r!l&&!r
0050
MC/DC33%1/3
ref $value or $value eq "" or $value =~ /\0/u
ref $value $value eq "" $value =~ /\0/u
Truth table50%2/4
A or B or C
A= ref $valueB= $value eq ""C= $value =~ /\0/u
ABCresult
1XX1
01X1
0011
0000
66 if ref($value) || $value eq '' || $value =~ /\0/;
67 48 return "$value";
68 }
69  
70 26 ▶sub _server_name ($target, $value) {
Subroutine _server_name: called CC=6 SCAR=18.0
71 26 my $server_name = _optional_string($target, 'server_name', $value);
72 25 ▶ return undef if !defined $server_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server_name 10 15 25
73 15 ▶ $server_name = substr($server_name, 1, length($server_name) - 2)
Branch: 1 branch
BranchTrueFalseTotal
if length $server_name >= 2 and substr($server_name, 0, 1) eq "[" and substr($server_name, -1, 1) eq "]" 1 14 15
Condition66%4/6
length $server_name >= 2 and substr($server_name, 0, 1) eq "[" and substr($server_name, -1, 1) eq "]"
!ll&&!rl&&r
1401
length $server_name >= 2 and substr($server_name, 0, 1) eq "["
!ll&&!rl&&r
0141
MC/DC33%1/3
length $server_name >= 2 and substr($server_name, 0, 1) eq "[" and substr($server_name, -1, 1) eq "]"
length $server_name >= 2 substr($server_name, 0, 1) eq "[" substr($server_name, -1, 1) eq "]"
Truth table50%2/4
A and B and C
A= length $server_name >= 2B= substr($server_name, 0, 1) eq "["C= substr($server_name, -1, 1) eq "]"
ABCresult
0XX0
10X0
1100
1111
74 if length($server_name) >= 2
75 && substr($server_name, 0, 1) eq '['
76 && substr($server_name, -1, 1) eq ']';
77 15 ▶ croak "$target TLS server_name must not be empty after removing brackets"
Branch: 1 branch
BranchTrueFalseTotal
if $server_name eq "" 1 14 15
78 if $server_name eq '';
79 14 return $server_name;
80 }
81  
82 13 ▶sub _build_declaration ($class, $target, @arg) {
Subroutine _build_declaration: called CC=7 SCAR=19.8
83 13 my %opt = @arg;
84 13 my @known = qw(
85 cert_file key_file server_name verify ca_file ca_path alpn
86 handshake_timeout shutdown_timeout
87 );
88 13 my %known = map { $_ => 1 } @known;
89 13 my @unknown = grep { !$known{$_} } keys %opt;
90 13 ▶ croak "$target TLS declaration has unknown options: "
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 12 13
91 . join(', ', sort @unknown) if @unknown;
92  
93 my $cert_file = _optional_string(
94 $target, 'cert_file', delete $opt{cert_file},
95 12 );
96 my $key_file = _optional_string(
97 $target, 'key_file', delete $opt{key_file},
98 12 );
99 12 ▶ croak "$target TLS declaration requires cert_file and key_file together"
Branch: 1 branch
BranchTrueFalseTotal
if defined $cert_file != defined $key_file 1 11 12
100 if defined($cert_file) != defined($key_file);
101 11 my $server_name = _server_name($target, delete $opt{server_name});
102 my $ca_file = _optional_string(
103 $target, 'ca_file', delete $opt{ca_file},
104 10 );
105 my $ca_path = _optional_string(
106 $target, 'ca_path', delete $opt{ca_path},
107 10 );
108 10 ▶ my $verify = exists($opt{verify}) ? delete($opt{verify}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'verify'} ? : 0 10 10
109 10 ▶ croak "$target TLS verify must be 0 or 1"
Branch: 1 branch
BranchTrueFalseTotal
if ref $verify or not $verify =~ /\A[01]\z/u 0 10 10
Condition33%1/3
ref $verify or not $verify =~ /\A[01]\z/u
l!l&&r!l&&!r
0010
MC/DC0%0/2
ref $verify or not $verify =~ /\A[01]\z/u
ref $verify not $verify =~ /\A[01]\z/u
Truth table33%1/3
A or not(B)
A= ref $verifyB= not $verify =~ /\A[01]\z/u
ABresult
1X1
011
000
110 if ref($verify) || $verify !~ /\A[01]\z/;
111  
112 return {
113 target => $target,
114 cert_file => $cert_file,
115 key_file => $key_file,
116 server_name => $server_name,
117 verify => $verify ? 1 : 0,
118 ca_file => $ca_file,
119 ca_path => $ca_path,
120 alpn_wire => _alpn_wire(delete $opt{alpn}),
121 handshake_timeout => _timeout(
122 "$target TLS declaration", 'handshake_timeout',
123 delete($opt{handshake_timeout}), 10,
124 ),
125 shutdown_timeout => _timeout(
126 "$target TLS declaration", 'shutdown_timeout',
127 10 ▶ delete($opt{shutdown_timeout}), 5,
Branch: 1 branch
BranchTrueFalseTotal
$verify ? : 10 0 10
128 ),
129 };
130 }
131  
132 5 ▶sub _validate_server_declaration ($class, $definition) {
Subroutine _validate_server_declaration: called CC=2 SCAR=6.9
133 5 my $target = $definition->{target};
134 croak "$target is used for accepted TLS Streams but does not declare "
135 . 'cert_file and key_file'
136 5 ▶ if !defined($definition->{cert_file});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $definition->{"cert_file"} 1 4 5
137 4 return;
138 }
139  
140 2 ▶sub _client_from_declaration ($class, $definition, $connect_host = undef) {
Subroutine _client_from_declaration: called CC=6 SCAR=21.4
141 2 my $target = $definition->{target};
142 2 ▶ my $server_name = $definition->{server_name} // $connect_host;
Condition33%1/3
$definition->{"server_name"} // $connect_host
l!l&&r!l&&!r
020
MC/DC0%0/2
$definition->{"server_name"} // $connect_host
$definition->{"server_name"} $connect_host
Truth table33%1/3
A // B
A= $definition->{"server_name"}B= $connect_host
ABresult
1X1
011
000
143 2 ▶ croak "$target TLS client requires server_name when connect() has no host"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $server_name or ref $server_name or $server_name eq "" 0 2 2
Condition33%2/6
not defined $server_name or ref $server_name or $server_name eq ""
l!l&&r!l&&!r
002
not defined $server_name or ref $server_name
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $server_name or ref $server_name or $server_name eq ""
not defined $server_name ref $server_name $server_name eq ""
Truth table25%1/4
not(A) or B or C
A= not defined $server_nameB= ref $server_nameC= $server_name eq ""
ABCresult
1XX1
01X1
0011
0000
144 if !defined($server_name) || ref($server_name) || $server_name eq '';
145 2 $server_name = _server_name($target, $server_name);
146 return $class->_new_client(
147 $server_name,
148 $definition->{verify},
149 $definition->{ca_file},
150 $definition->{ca_path},
151 $definition->{alpn_wire},
152 $definition->{handshake_timeout},
153 $definition->{shutdown_timeout},
154 2 );
155 }
156  
157 5 ▶sub _server_from_declaration ($class, $definition) {
Subroutine _server_from_declaration: called CC=1 SCAR=0.0
158 5 $class->_validate_server_declaration($definition);
159 return $class->_new_server(
160 $definition->{cert_file},
161 $definition->{key_file},
162 $definition->{alpn_wire},
163 $definition->{handshake_timeout},
164 $definition->{shutdown_timeout},
165 4 );
166 }
167  
168 14 ▶sub client ($class, %opt) {
Subroutine client: called CC=9 SCAR=22.7
Pod undocumented
169 14 ▶ croak 'client(): must be called as a class method' if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 14 14
170 my $server_name = delete $opt{server_name}
171 14 ▶ // croak 'client(): missing server_name';
Condition50%1/2
delete $opt{"server_name"} // croak("client(): missing server_name")
l!l
140
MC/DC0%0/1
delete $opt{"server_name"} // croak("client(): missing server_name")
delete $opt{"server_name"}
Truth table50%1/2
A
A= delete $opt{"server_name"}
Aresult
11
00
172 14 ▶ my $verify = exists $opt{verify} ? delete($opt{verify}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'verify'} ? : 4 10 14
173 14 ▶ croak 'client(): verify must be 0 or 1'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verify or ref $verify or not $verify =~ /\A[01]\z/u 1 13 14
Condition50%3/6
not defined $verify or ref $verify or not $verify =~ /\A[01]\z/u
l!l&&r!l&&!r
0113
not defined $verify or ref $verify
l!l&&r!l&&!r
0014
MC/DC33%1/3
not defined $verify or ref $verify or not $verify =~ /\A[01]\z/u
not defined $verify ref $verify not $verify =~ /\A[01]\z/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $verifyB= ref $verifyC= not $verify =~ /\A[01]\z/u
ABCresult
1XX1
01X1
0011
0000
174 if !defined($verify) || ref($verify) || $verify !~ /\A[01]\z/;
175 13 $server_name = _server_name('client()', $server_name);
176 my $ca_file = _optional_string(
177 'client()', 'ca_file', delete $opt{ca_file},
178 12 );
179 my $ca_path = _optional_string(
180 'client()', 'ca_path', delete $opt{ca_path},
181 12 );
182 12 my $alpn = _alpn_wire(delete $opt{alpn});
183 my $handshake_timeout = _timeout(
184 11 'client', 'handshake_timeout', delete($opt{handshake_timeout}), 10,
185 );
186 my $shutdown_timeout = _timeout(
187 11 'client', 'shutdown_timeout', delete($opt{shutdown_timeout}), 5,
188 );
189 11 ▶ croak 'client(): unknown options: ' . join(', ', sort keys %opt) if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 11 11
190 11 ▶ return $class->_new_client(
Branch: 1 branch
BranchTrueFalseTotal
$verify ? : 8 3 11
191 $server_name, $verify ? 1 : 0, $ca_file, $ca_path, $alpn,
192 $handshake_timeout, $shutdown_timeout,
193 );
194 }
195  
196 9 ▶sub server ($class, %opt) {
Subroutine server: called CC=5 SCAR=16.6
Pod undocumented
197 9 ▶ croak 'server(): must be called as a class method' if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 9 9
198 my $cert_file = delete $opt{cert_file}
199 9 ▶ // croak 'server(): missing cert_file';
Condition50%1/2
delete $opt{"cert_file"} // croak("server(): missing cert_file")
l!l
90
MC/DC0%0/1
delete $opt{"cert_file"} // croak("server(): missing cert_file")
delete $opt{"cert_file"}
Truth table50%1/2
A
A= delete $opt{"cert_file"}
Aresult
11
00
200 my $key_file = delete $opt{key_file}
201 9 ▶ // croak 'server(): missing key_file';
Condition50%1/2
delete $opt{"key_file"} // croak("server(): missing key_file")
l!l
90
MC/DC0%0/1
delete $opt{"key_file"} // croak("server(): missing key_file")
delete $opt{"key_file"}
Truth table50%1/2
A
A= delete $opt{"key_file"}
Aresult
11
00
202 9 $cert_file = _optional_string('server()', 'cert_file', $cert_file);
203 8 $key_file = _optional_string('server()', 'key_file', $key_file);
204 8 my $alpn = _alpn_wire(delete $opt{alpn});
205 my $handshake_timeout = _timeout(
206 8 'server', 'handshake_timeout', delete($opt{handshake_timeout}), 10,
207 );
208 my $shutdown_timeout = _timeout(
209 8 'server', 'shutdown_timeout', delete($opt{shutdown_timeout}), 5,
210 );
211 8 ▶ croak 'server(): unknown options: ' . join(', ', sort keys %opt) if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 8 8
212 8 return $class->_new_server(
213 $cert_file, $key_file, $alpn,
214 $handshake_timeout, $shutdown_timeout,
215 );
216 }
217  
218 21 ▶sub _stream_transport_bind ($self, $fd) {
Subroutine _stream_transport_bind: called CC=1 SCAR=0.0
219 21 return $self->_bind_fd($fd);
220 }
221  
222 24 ▶sub _install_deadline ($self, $stream, $operation) {
Subroutine _install_deadline: called CC=3 SCAR=11.0
223 24 my $fd = $self->_arm_deadline($operation);
224 24 ▶ return if !defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd 1 23 24
225 23 ▶ return if $stream->_has_transport_deadline_watcher;
Branch: 1 branch
BranchTrueFalseTotal
if $stream->_has_transport_deadline_watcher 2 21 23
226 21 my $watcher = $stream->loop->watch(
227 fd => $fd,
228 _internal => 1,
229 data => {
230 provider => $self,
231 stream => $stream,
232 },
233 read => \&_deadline_ready,
234 );
235 21 $stream->_set_transport_deadline_watcher($watcher);
236 21 return;
237 }
238  
239 3 ▶sub _deadline_ready ($watcher) {
Subroutine _deadline_ready: called CC=1 SCAR=0.0
240 3 my $state = $watcher->data;
241 3 my $stream = $state->{stream};
242 3 my $operation = $state->{provider}->_deadline_operation;
243 my $message = $state->{provider}->_consume_deadline(
244 3 $operation,
245 );
246 3 $stream->_transport_deadline_expired(
247 $operation, $message,
248 );
249 3 return;
250 }
251  
252 21 ▶sub _stream_transport_start ($self, $stream) {
Subroutine _stream_transport_start: called CC=1 SCAR=0.0
253 21 $self->_install_deadline($stream, 'handshake');
254 }
255  
256 16 ▶sub _stream_transport_ready ($self, $stream) {
Subroutine _stream_transport_ready: called CC=1 SCAR=0.0
257 16 $stream->_clear_transport_deadline;
258 }
259  
260 3 ▶sub _stream_transport_begin_shutdown ($self, $stream) {
Subroutine _stream_transport_begin_shutdown: called CC=1 SCAR=0.0
261 3 $self->_install_deadline($stream, 'shutdown');
262 }
263  
264 18 ▶sub _stream_transport_cancel_deadline ($self) {
Subroutine _stream_transport_cancel_deadline: called CC=1 SCAR=0.0
265 18 $self->_cancel_deadline;
266 }
267  
268 21 ▶sub _stream_transport_close_deadline ($self) {
Subroutine _stream_transport_close_deadline: called CC=1 SCAR=0.0
269 21 $self->_close_deadline;
270 }
271  
272 9 1;
273  
274 __END__
« blib/lib/Linux/Event/Signal.pm ↑ Summary blib/lib/Linux/Event/Framer/Varint.pm »