blib/lib/Linux/Event/_SocketConfig.pm

filter: Statementstmt 89.5% 86 / 96 Branchbran 73.2% 41 / 56 Conditioncond 41.5% 22 / 53 MC/DCmcdc 18.5% 5 / 27 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 9 totaltotal 66.4% 176 / 265 CC 52 SCAR 28.1
CC
52
Cov
73%
bind_device
42.0
normalize
38.8
get_option
11.3
CRAP 16.7
« bench/run-connect-microbench.pl ↑ Summary blib/lib/Linux/Event/_ByteStream/Descriptor.pm »
1 package Linux::Event::_SocketConfig;
2 127 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 127 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 127 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 127 our $VERSION = '0.119';
7  
8 127 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 127 ▶use POSIX qw(ceil isfinite);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 127 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 127 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 AF_INET AF_INET6
13 IPPROTO_TCP SOL_SOCKET
14 SO_KEEPALIVE SO_RCVBUF SO_SNDBUF
15 TCP_KEEPCNT TCP_KEEPIDLE TCP_KEEPINTVL TCP_NODELAY
16 127 );
17 127 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 127 ▶use Linux::Event::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 127 ▶use constant _SO_BINDTODEVICE => Socket::SO_BINDTODEVICE();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 127 ▶use constant _TCP_USER_TIMEOUT => Socket::TCP_USER_TIMEOUT();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 127 my %SPEC = (
25 tcp_nodelay => {
26 level => IPPROTO_TCP, option => TCP_NODELAY, type => 'boolean', tcp => 1,
27 },
28 keepalive => {
29 level => SOL_SOCKET, option => SO_KEEPALIVE, type => 'boolean', tcp => 1,
30 },
31 keepalive_idle => {
32 level => IPPROTO_TCP, option => TCP_KEEPIDLE, type => 'positive', tcp => 1,
33 },
34 keepalive_interval => {
35 level => IPPROTO_TCP, option => TCP_KEEPINTVL, type => 'positive', tcp => 1,
36 },
37 keepalive_count => {
38 level => IPPROTO_TCP, option => TCP_KEEPCNT, type => 'positive', tcp => 1,
39 },
40 tcp_user_timeout => {
41 level => IPPROTO_TCP, option => _TCP_USER_TIMEOUT, type => 'timeout', tcp => 1,
42 },
43 send_buffer => {
44 level => SOL_SOCKET, option => SO_SNDBUF, type => 'positive', tcp => 0,
45 },
46 receive_buffer => {
47 level => SOL_SOCKET, option => SO_RCVBUF, type => 'positive', tcp => 0,
48 },
49 );
50  
51 309737 ▶sub names () { sort keys %SPEC }
Subroutine names: called CC=1 SCAR=0.0
Pod undocumented
52 0 ▶sub is_option ($name) { exists $SPEC{$name} }
Subroutine is_option: not called CC=1 SCAR=6.9
Pod undocumented
53  
54 30 ▶sub _target ($method, $name) {
Subroutine _target: called CC=2 SCAR=6.9
55 30 ▶ return "$method(): $name" if $method =~ /\A(?:new|connect)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /\A(?:new|connect)\z/u 2 28 30
56 28 return "$method $name";
57 }
58  
59 30 ▶sub normalize ($method, $name, $value) {
Subroutine normalize: called CC=20 SCAR=36.6
Pod undocumented
60 30 ▶ croak "$method(): unknown socket option: $name" if !exists $SPEC{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SPEC{$name} 0 30 30
61 30 my $type = $SPEC{$name}{type};
62 30 my $target = _target($method, $name);
63 30 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 11 19 30
64 11 ▶ croak "$target must be zero or one"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not $value =~ /\A[01]\z/u 1 10 11
Condition50%3/6
not defined $value or ref $value or not $value =~ /\A[01]\z/u
l!l&&r!l&&!r
0110
not defined $value or ref $value
l!l&&r!l&&!r
0011
MC/DC33%1/3
not defined $value or ref $value or not $value =~ /\A[01]\z/u
not defined $value ref $value not $value =~ /\A[01]\z/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not $value =~ /\A[01]\z/u
ABCresult
1XX1
01X1
0011
0000
65 if !defined($value) || ref($value) || $value !~ /\A[01]\z/;
66 10 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 8 2 10
67 }
68 19 ▶ if ($type eq 'positive') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "positive") 17 2 19
69 17 ▶ croak "$target must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not $value =~ /\A\d+\z/u or $value == 0 0 17 17
Condition33%3/9
not defined $value or ref $value or not $value =~ /\A\d+\z/u or $value == 0
l!l&&r!l&&!r
0017
not defined $value or ref $value or not $value =~ /\A\d+\z/u
l!l&&r!l&&!r
0017
not defined $value or ref $value
l!l&&r!l&&!r
0017
MC/DC0%0/4
not defined $value or ref $value or not $value =~ /\A\d+\z/u or $value == 0
not defined $value ref $value not $value =~ /\A\d+\z/u $value == 0
Truth table20%1/5
not(A) or B or not(C) or D
A= not defined $valueB= ref $valueC= not $value =~ /\A\d+\z/uD= $value == 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
70 if !defined($value) || ref($value) || $value !~ /\A\d+\z/
71 || $value == 0;
72 17 ▶ croak "$target must be at most 2147483647"
Branch: 1 branch
BranchTrueFalseTotal
if $value > 2147483647 1 16 17
73 if $value > 2_147_483_647;
74 16 return 0 + $value;
75 }
76 2 ▶ my $seconds = !defined($value) || ref($value) || !looks_like_number($value)
Branch: 1 branch
BranchTrueFalseTotal
!defined($value) || ref $value || !&looks_like_number($value) ? : 0 2 2
Condition33%2/6
not defined $value or ref $value or not &looks_like_number($value)
l!l&&r!l&&!r
002
not defined $value or ref $value
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $value or ref $value or not &looks_like_number($value)
not defined $value ref $value not &looks_like_number($value)
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not &looks_like_number($value)
ABCresult
1XX1
01X1
0011
0000
77 ? undef : 0 + $value;
78 2 ▶ croak "$target must be a non-negative number of seconds"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $seconds && isfinite($seconds) or $seconds < 0 0 2 2
Condition33%2/6
not defined $seconds && isfinite($seconds) or $seconds < 0
l!l&&r!l&&!r
002
defined $seconds && isfinite($seconds)
!ll&&!rl&&r
002
MC/DC0%0/3
not defined $seconds && isfinite($seconds) or $seconds < 0
defined $seconds isfinite($seconds) $seconds < 0
Truth table25%1/4
not(A && B) or C
A= defined $secondsB= isfinite($seconds)C= $seconds < 0
ABCresult
0XX1
10X1
1111
1100
79 if !defined($seconds) || !isfinite($seconds) || $seconds < 0;
80 2 ▶ croak "$target exceeds the Linux TCP_USER_TIMEOUT range"
Branch: 1 branch
BranchTrueFalseTotal
if $seconds > "4294967.295" 0 2 2
81 if $seconds > 4_294_967.295;
82 2 return $seconds;
83 }
84  
85 103160 ▶sub normalize_policy ($method, $policy) {
Subroutine normalize_policy: called CC=3 SCAR=11.1
Pod undocumented
86 103160 for my $name (keys %$policy) {
87 1 ▶ next if !defined $policy->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $policy->{$name} 0 1 1
88 1 $policy->{$name} = normalize($method, $name, $policy->{$name});
89 }
90 103160 return $policy;
91 }
92  
93 103160 ▶sub extract ($method, $option) {
Subroutine extract: called CC=3 SCAR=11.0
Pod undocumented
94 103160 my %socket;
95 103160 for my $name (names()) {
96 825280 ▶ $socket{$name} = delete $option->{$name} if exists $option->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option->{$name} 1 825279 825280
97 }
98 103160 return normalize_policy($method, \%socket);
99 }
100  
101 17 ▶sub _is_tcp ($family) { $family == AF_INET || $family == AF_INET6 }
Subroutine _is_tcp: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 or $family == 10 16 1 17
102  
103 1 ▶sub _error ($operation, $name, $errno, $message = undef) {
Subroutine _error: called CC=4 SCAR=14.8
104 1 local $! = $errno;
105 1 ▶ return Linux::Event::Error->new(
Condition50%2/4
$errno || undef
l!l
01
$message // "$!"
l!l
10
MC/DC0%0/1
$errno || undef
$errno
MC/DC0%0/1
$message // "$!"
$message
Truth table50%1/2
A
A= $errno
Aresult
11
00
Truth table50%1/2
A
A= $message
Aresult
11
00
106 type => 'socket_configuration',
107 operation => $operation,
108 option => $name,
109 errno => $errno || undef,
110 message => $message // "$!",
111 );
112 }
113  
114 16 ▶sub _packed_value ($spec, $value) {
Subroutine _packed_value: called CC=3 SCAR=11.0
115 16 ▶ if ($spec->{type} eq 'timeout') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"type"} eq "timeout") 1 15 16
116 1 ▶ my $milliseconds = $value == 0 ? 0 : ceil($value * 1000);
Branch: 1 branch
BranchTrueFalseTotal
$value == 0 ? : 0 1 1
117 1 return pack('I', $milliseconds);
118 }
119 15 return pack('i', $value);
120 }
121  
122 17 ▶sub set_option ($fh, $family, $name, $value) {
Subroutine set_option: called CC=5 SCAR=16.8
Pod undocumented
123 17 ▶ my $spec = $SPEC{$name} // croak "unknown socket option: $name";
Condition50%1/2
$SPEC{$name} // croak("unknown socket option: $name")
l!l
170
MC/DC0%0/1
$SPEC{$name} // croak("unknown socket option: $name")
$SPEC{$name}
Truth table50%1/2
A
A= $SPEC{$name}
Aresult
11
00
124 17 $value = normalize('socket option', $name, $value);
125 die _error('setsockopt', $name, 0,
126 "$name is valid only for TCP sockets")
127 17 ▶ if $spec->{tcp} && !_is_tcp($family);
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"tcp"} and not _is_tcp($family) 1 16 17
Condition100%3/3
$spec->{"tcp"} and not _is_tcp($family)
!ll&&!rl&&r
881
MC/DC100%2/2
$spec->{"tcp"} and not _is_tcp($family)
$spec->{"tcp"} not _is_tcp($family)
Truth table100%3/3
A and not(B)
A= $spec->{"tcp"}B= not _is_tcp($family)
ABresult
0X0
100
111
128 16 ▶ if (!setsockopt(
Branch: 1 branch
BranchTrueFalseTotal
unless (setsockopt $fh, $spec->{"level"}, $spec->{"option"}, _packed_value($spec, $value)) 0 16 16
129 $fh, $spec->{level}, $spec->{option}, _packed_value($spec, $value),
130 )) {
131 0 my $errno = 0 + $!;
132 0 die _error('setsockopt', $name, $errno);
133 }
134 16 return;
135 }
136  
137 14 ▶sub get_option ($fh, $family, $name) {
Subroutine get_option: called CC=10 SCAR=24.2
Pod undocumented
138 14 ▶ my $spec = $SPEC{$name} // croak "unknown socket option: $name";
Condition50%1/2
$SPEC{$name} // croak("unknown socket option: $name")
l!l
140
MC/DC0%0/1
$SPEC{$name} // croak("unknown socket option: $name")
$SPEC{$name}
Truth table50%1/2
A
A= $SPEC{$name}
Aresult
11
00
139 die _error('getsockopt', $name, 0,
140 "$name is valid only for TCP sockets")
141 14 ▶ if $spec->{tcp} && !_is_tcp($family);
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"tcp"} and not _is_tcp($family) 0 14 14
Condition66%2/3
$spec->{"tcp"} and not _is_tcp($family)
!ll&&!rl&&r
680
MC/DC0%0/2
$spec->{"tcp"} and not _is_tcp($family)
$spec->{"tcp"} not _is_tcp($family)
Truth table66%2/3
A and not(B)
A= $spec->{"tcp"}B= not _is_tcp($family)
ABresult
0X0
100
111
142 14 my $packed = getsockopt($fh, $spec->{level}, $spec->{option});
143 14 ▶ if (!defined $packed) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packed) 0 14 14
144 0 my $errno = 0 + $!;
145 0 die _error('getsockopt', $name, $errno);
146 }
147 14 ▶ die _error('getsockopt', $name, 0,
Branch: 1 branch
BranchTrueFalseTotal
if length $packed < 4 0 14 14
148 "kernel returned an invalid value for $name") if length($packed) < 4;
149 14 ▶ my $value = $spec->{type} eq 'timeout'
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'type'} eq 'timeout' ? : 1 13 14
150 ? unpack('I', $packed) : unpack('i', $packed);
151 14 ▶ return $value / 1000 if $spec->{type} eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"type"} eq "timeout" 1 13 14
152 13 ▶ return $value ? 1 : 0 if $spec->{type} eq 'boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $spec->{"type"} eq "boolean" 4 9 13
$value ? : 3 1 4
153 9 return $value;
154 }
155  
156 103162 ▶sub apply_policy ($fh, $family, $policy) {
Subroutine apply_policy: called CC=4 SCAR=13.9
Pod undocumented
157 103162 for my $name (names()) {
158 825295 ▶ next if !exists($policy->{$name}) || !defined($policy->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $policy->{$name} and defined $policy->{$name} 825283 12 825295
Condition100%3/3
exists $policy->{$name} and defined $policy->{$name}
!ll&&!rl&&r
15082513312
MC/DC100%2/2
exists $policy->{$name} and defined $policy->{$name}
exists $policy->{$name} defined $policy->{$name}
Truth table100%3/3
A and B
A= exists $policy->{$name}B= defined $policy->{$name}
ABresult
0X0
100
111
159 12 set_option($fh, $family, $name, $policy->{$name});
160 }
161 103161 return;
162 }
163  
164 0 ▶sub bind_device ($fh, $device) {
Subroutine bind_device: not called CC=6 SCAR=37.4
Pod undocumented
165 0 ▶ croak 'bind_device must be a non-empty interface name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $device or ref $device or $device eq "" or $device =~ /\0/u 0 0 0
Condition0%0/9
not defined $device or ref $device or $device eq "" or $device =~ /\0/u
l!l&&r!l&&!r
000
not defined $device or ref $device or $device eq ""
l!l&&r!l&&!r
000
not defined $device or ref $device
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $device or ref $device or $device eq "" or $device =~ /\0/u
not defined $device ref $device $device eq "" $device =~ /\0/u
Truth table0%0/5
not(A) or B or C or D
A= not defined $deviceB= ref $deviceC= $device eq ""D= $device =~ /\0/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
166 if !defined($device) || ref($device) || $device eq ''
167 || $device =~ /\0/;
168 0 ▶ if (!setsockopt($fh, SOL_SOCKET, _SO_BINDTODEVICE, "$device\0")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (setsockopt $fh, 1, 25, "$device\000") 0 0 0
169 0 my $errno = 0 + $!;
170 0 die _error('setsockopt', 'bind_device', $errno);
171 }
172 0 return;
173 }
174  
175 127 1;
« bench/run-connect-microbench.pl ↑ Summary blib/lib/Linux/Event/_ByteStream/Descriptor.pm »