lib/Test/FitNesse/Slim/List.pm

filter: Statementstmt 98.1% 107 / 109 Branchbran 96.5% 56 / 58 Conditioncond 84.0% 21 / 25 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 93.7% 209 / 223 CC 41 SCAR 20.9
CC
41
Cov
96%
_parse
14.0
list
9.0
serialize
7.1
CRAP 8.1
« lib/Test/FitNesse/Slim/ConverterRegistry.pm ↑ Summary lib/Test/FitNesse/Slim/Statement.pm »
1 package Test::FitNesse::Slim::List;
2  
3 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 64 ▶use Encode qw/ is_utf8 encode decode FB_CROAK /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 64 our %DEFAULT_LIMITS = (
9 nesting => 128,
10 elements => 1_000_000,
11 item_units => 16_777_216,
12 frame_bytes => 67_108_864,
13 );
14  
15 1801 ▶sub _utf16_code_units_length {
Subroutine _utf16_code_units_length: called CC=1 SCAR=0.0
16 1801 my($s) = @_;
17 1801 my $utf16be = encode("UTF-16BE", $s);
18 64 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1801 return length($utf16be) / 2;
20 }
21  
22 1743 ▶sub _take_utf16_units {
Subroutine _take_utf16_units: called CC=6 SCAR=17.9
23 1743 my ($s, $position, $units) = @_;
24 1743 my $start = $$position;
25 1743 my $count = 0;
26  
27 1743 ▶ while ($count < $units && $$position < length $s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $units and $$position < length $s) 1039062 1742 1040804
Condition100%3/3
$count < $units and $$position < length $s
!ll&&!rl&&r
174111039062
MC/DC100%2/2
$count < $units and $$position < length $s
$count < $units $$position < length $s
Truth table100%3/3
A and B
A= $count < $unitsB= $$position < length $s
ABresult
0X0
100
111
28 1039062 my $ch = substr($s, $$position, 1);
29 1039062 my $codepoint = ord($ch);
30 1039062 ▶ my $units_in_char = $codepoint > 0xFFFF ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$codepoint > 65535 ? : 29 1039033 1039062
31 1039062 ▶ last if $count + $units_in_char > $units;
Branch: 1 branch
BranchTrueFalseTotal
if $count + $units_in_char > $units 1 1039061 1039062
32 1039061 $count += $units_in_char;
33 1039061 ++$$position;
34 }
35  
36 1743 ▶ die "syntax error: item length exceeds available UTF-16 code units"
Branch: 1 branch
BranchTrueFalseTotal
unless $count == $units 2 1741 1743
37 unless $count == $units;
38 1741 return substr($s, $start, $$position - $start);
39 }
40  
41 710 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
42 710 my($this,$l,@options) = @_;
43 710 ▶ my $class = ref($this) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
3703400
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
44 710 my %options = @options;
45 710 ▶ my $limits = { %DEFAULT_LIMITS, %{ $options{limits} || {} } };
Condition100%2/2
$options{'limits'} || {}
l!l
62648
MC/DC100%1/1
$options{'limits'} || {}
$options{'limits'}
Truth table100%2/2
A
A= $options{'limits'}
Aresult
11
00
46 710 ▶ if (ref($l) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $l eq "ARRAY") 540 170 710
47 540 return bless { LIST => [ @$l ], LIMITS => $limits } => $class;
48 }
49 170 return bless { RAW => $l, LIMITS => $limits } => $class;
50 }
51  
52 2278 ▶sub _decimal {
Subroutine _decimal: called CC=6 SCAR=17.9
53 2278 my($s,$position,$maximum,$kind) = @_;
54 2278 pos($s) = $$position;
55 2278 ▶ die "syntax error: missing $kind" unless $s =~ /\G([0-9]{6,}):/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\G([0-9]{6,}):/cg 21 2257 2278
56 2257 my $digits = $1;
57 2257 $$position = pos($s);
58 2257 (my $significant = $digits) =~ s/^0+//;
59 2257 ▶ $significant = '0' unless length $significant;
Branch: 1 branch
BranchTrueFalseTotal
unless length $significant 19 2238 2257
60 2257 my $limit = "$maximum";
61 2257 ▶ die "resource limit: $kind exceeds configured maximum"
Branch: 1 branch
BranchTrueFalseTotal
if length $significant > length $limit or length $significant == length $limit and $significant gt $limit 3 2254 2257
Condition83%5/6
length $significant > length $limit or length $significant == length $limit and $significant gt $limit
l!l&&r!l&&!r
122254
length $significant == length $limit and $significant gt $limit
!ll&&!rl&&r
225402
MC/DC66%2/3
length $significant > length $limit or length $significant == length $limit and $significant gt $limit
length $significant > length $limit length $significant == length $limit $significant gt $limit
Truth table75%3/4
A or B and C
A= length $significant > length $limitB= length $significant == length $limitC= $significant gt $limit
ABCresult
1XX1
0111
00X0
0100
62 if length($significant) > length($limit)
63 || length($significant) == length($limit) && $significant gt $limit;
64 2254 return 0 + $significant;
65 }
66  
67 538 ▶sub _parse {
Subroutine _parse: called CC=14 SCAR=26.4
68 538 my($s,$limits,$state,$depth) = @_;
69 die "resource limit: nested-list depth exceeds configured maximum"
70 538 ▶ if $depth > $limits->{nesting};
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $limits->{"nesting"} 1 537 538
71 537 ▶ die "syntax error: missing open bracket" unless substr($s,0,1) eq '[';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($s, 0, 1) eq "[" 3 534 537
72 534 my $position = 1;
73 534 my $count = _decimal($s,\$position,$limits->{elements},'list-length');
74 511 $state->{elements} += $count;
75 die "resource limit: total list elements exceed configured maximum"
76 511 ▶ if $state->{elements} > $limits->{elements};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"elements"} > $limits->{"elements"} 0 511 511
77  
78 511 my @items;
79 511 for (1 .. $count) {
80 1744 my $units = _decimal($s,\$position,$limits->{item_units},'item-length');
81 1743 my $item = _take_utf16_units($s,\$position,$units);
82 1741 ▶ die "syntax error: item is not terminated by a colon"
Branch: 1 branch
BranchTrueFalseTotal
unless substr($s, $position++, 1) eq ":" 2 1739 1741
83 unless substr($s,$position++,1) eq ':';
84  
85 1739 my $nested;
86 1739 ▶ if ($item =~ /\A\[/ && $item !~ /\A\[\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\A\[/ and not $item =~ /\A\[\s*\z/) 376 1363 1739
Condition100%3/3
$item =~ /\A\[/ and not $item =~ /\A\[\s*\z/
!ll&&!rl&&r
13621376
MC/DC100%2/2
$item =~ /\A\[/ and not $item =~ /\A\[\s*\z/
$item =~ /\A\[/ not $item =~ /\A\[\s*\z/
Truth table100%3/3
A and not(B)
A= $item =~ /\A\[/B= not $item =~ /\A\[\s*\z/
ABresult
0X0
100
111
87 376 my $previous_elements = $state->{elements};
88 376 my $ok = eval { $nested = _parse($item,$limits,$state,$depth + 1); 1 };
89 376 ▶ die $@ if !$ok && $@ =~ /\Aresource limit:/;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $@ =~ /\Aresource limit:/ 2 374 376
Condition100%3/3
not $ok and $@ =~ /\Aresource limit:/
!ll&&!rl&&r
354202
MC/DC100%2/2
not $ok and $@ =~ /\Aresource limit:/
not $ok $@ =~ /\Aresource limit:/
Truth table100%3/3
not(A) and B
A= not $okB= $@ =~ /\Aresource limit:/
ABresult
0X0
100
111
90 374 ▶ $state->{elements} = $previous_elements unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 20 354 374
91 }
92 1737 ▶ push @items, defined($nested) ? $nested : $item;
Branch: 1 branch
BranchTrueFalseTotal
defined $nested ? : 354 1383 1737
93 }
94  
95 504 ▶ die "syntax error: missing close bracket" unless substr($s,$position++,1) eq ']';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($s, $position++, 1) eq "]" 4 500 504
96 500 ▶ die "syntax error: trailing serialized data" unless $position == length($s);
Branch: 1 branch
BranchTrueFalseTotal
unless $position == length $s 1 499 500
97 499 return \@items;
98 }
99  
100 709 ▶sub list {
Subroutine list: called CC=9 SCAR=22.0
Pod documented
101 709 my($self) = @_;
102 709 ▶ return @{ $self->{LIST} } if $self->{LIST};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"LIST"} 540 169 709
103 169 my $raw = $self->{RAW};
104 169 ▶ die "cannot deserialize undefined value" unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 1 168 169
105 168 ▶ die "cannot deserialize empty string" unless length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 1 167 168
106 167 ▶ my $limits = $self->{LIMITS} || \%DEFAULT_LIMITS;
Condition50%1/2
$self->{'LIMITS'} || \%Test::FitNesse::Slim::List::DEFAULT_LIMITS
l!l
1670
MC/DC0%0/1
$self->{'LIMITS'} || \%Test::FitNesse::Slim::List::DEFAULT_LIMITS
$self->{'LIMITS'}
Truth table50%1/2
A
A= $self->{'LIMITS'}
Aresult
11
00
107 167 my $measure = $raw;
108 167 ▶ my $frame_bytes = is_utf8($measure)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($measure) ? : 83 84 167
109 ? length(encode("UTF-8",$measure,FB_CROAK)) : length($measure);
110 die "resource limit: frame exceeds configured maximum"
111 167 ▶ if $frame_bytes > $limits->{frame_bytes};
Branch: 1 branch
BranchTrueFalseTotal
if $frame_bytes > $limits->{"frame_bytes"} 1 166 167
112 166 ▶ my $decoded = is_utf8($raw) ? $raw : eval { decode("UTF-8",$raw,FB_CROAK) };
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($raw) ? : 82 84 166
113 166 ▶ die "cannot deserialize non-UTF-8 encoding" unless defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 4 162 166
114 162 my $parsed = _parse($decoded,$limits,{elements => 0},1);
115 145 @{ $self->{LIST} = $parsed };
116 }
117  
118 2346 ▶sub length_string {
Subroutine length_string: called CC=1 SCAR=0.0
Pod documented
119 2346 my($self,$length) = @_;
120 2346 sprintf "%06d:", $length;
121 }
122  
123 541 ▶sub serialize {
Subroutine serialize: called CC=7 SCAR=19.6
Pod documented
124 541 my($self) = @_;
125 541 ▶ return $self->{RAW} if $self->{RAW};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"RAW"} 1 540 541
126 540 my @l = $self->list;
127  
128 540 my $result = "[" . $self->length_string(scalar @l);
129 540 for (@l) {
130 1806 my $item;
131 my $length;
132 1806 ▶ if (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 1801 5 1806
133 1801 ▶ if (ref $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 370 1431 1801
elsif (&is_utf8($_) or defined eval { do { $_ = &decode('utf8', $_, 1) } }) { } 1431 0 1431
134 370 $item = $self->new($_)->serialize;
135 370 $length = _utf16_code_units_length $item;
136 }
137 1390 ▶ elsif (is_utf8($_) || defined eval { $_ = decode("utf8", $_, 1) }) {
Condition66%2/3
&is_utf8($_) or defined eval { do { $_ = &decode("utf8", $_, 1) } }
l!l&&r!l&&!r
4113900
MC/DC0%0/2
&is_utf8($_) or defined eval { do { $_ = &decode("utf8", $_, 1) } }
&is_utf8($_) defined eval { do { $_ = &decode("utf8", $_, 1) } }
Truth table66%2/3
A or B
A= &is_utf8($_)B= defined eval { do { $_ = &decode("utf8", $_, 1) } }
ABresult
1X1
011
000
138 1431 $item = $_;
139 1431 $length = _utf16_code_units_length $item;
140 }
141 else {
142 0 $item = $_;
143 0 $length = _utf16_code_units_length $item;
144 }
145 }
146 else {
147 5 $item = "null";
148 5 $length = length $item;
149 }
150  
151 1806 $result .= $self->length_string($length) . $item . ":";
152 }
153 540 $result .= "]";
154  
155 540 $self->{RAW} = $result;
156 }
157  
158 64 1;
159  
160 =head1 NAME
161  
162 Test::FitNesse::Slim::List - Parse and serialize SLiM list values
163  
164 =head1 DESCRIPTION
165  
166 This module handles the nested list representation used inside SLiM
167 messages. Serialized string lengths count UTF-16 code units; the server is
168 responsible for outer UTF-8 byte framing. Parsing validates the complete
169 input, including nesting and configurable resource limits, and dies with a
170 descriptive error for malformed data.
171  
172 =head1 METHODS
173  
174 =head2 new
175  
176 my $list = Test::FitNesse::Slim::List->new($serialized, limits => \%limits);
177 my $list = Test::FitNesse::Slim::List->new(\@values);
178  
179 Construct a parser from serialized text or a serializer from an array
180 reference. Optional limits override C<%DEFAULT_LIMITS>. The object is lazy:
181 serialized input is parsed when C<list> is requested.
182  
183 =head2 list
184  
185 Return the list's values. Nested lists are array references and protocol
186 nulls are represented by C<undef>. Malformed input throws an exception.
187  
188 =head2 serialize
189  
190 Return the serialized SLiM list string. Unicode length fields use UTF-16
191 code units. References are recursively serialized as nested lists.
192  
193 =head2 length_string
194  
195 Format a six-digit-minimum decimal length prefix for an inner list item.
196 Expanded lengths are permitted.
197  
198 =head1 LIMITS
199  
200 C<%DEFAULT_LIMITS> defines maximum nesting (128), decoded elements
201 (1,000,000), item length (16,777,216 UTF-16 units), and frame bytes
202 (67,108,864). These are server defensive defaults, not protocol limits.
203  
204 =cut
« lib/Test/FitNesse/Slim/ConverterRegistry.pm ↑ Summary lib/Test/FitNesse/Slim/Statement.pm »