FIX/Parser.pm

filter: Statementstmt 99.3% 144 / 145 Branchbran 92.5% 74 / 80 Conditioncond 85.0% 17 / 20 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 95.2% 258 / 271 CC 51 SCAR 25.4
CC
51
Cov
96%
parse
21.2
_construct_tag_accessor
16.0
_parse_tag_pair
8.0
CRAP 12.7
« FIX/BaseComposite.pm ↑ Summary FIX/Field.pm »
1 package Protocol::FIX::Parser;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Protocol::FIX qw/humanize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Protocol::FIX::TagsAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Protocol::FIX::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 our $VERSION = '0.08'; ## VERSION
12  
13 =head1 NAME
14  
15 Protocol::FIX::Parser - FIX messages parser
16  
17 =cut
18  
19 290 ▶sub _parse_tag_pair {
Subroutine _parse_tag_pair: called CC=8 SCAR=20.8
20 290 my ($protocol, $pair, $check_value) = @_;
21 290 ▶ return unless $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair 1 289 290
22  
23 289 ▶ if ($pair =~ /^(\d{1,})($Protocol::FIX::TAG_SEPARATOR)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /^(\d{1,})($Protocol::FIX::TAG_SEPARATOR)(.*)$/) { } 286 3 289
24 286 my ($tag_id, $value) = ($1, $3);
25  
26 286 my $field = $protocol->{fields_lookup}->{by_number}->{$tag_id};
27 286 ▶ return (undef, "Protocol error: unknown tag '$tag_id' in tag pair '" . Protocol::FIX::humanize($pair) . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $field 1 285 286
28 unless $field;
29  
30 285 ▶ return unless defined $3;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $3 0 285 285
31  
32 285 ▶ return ([$field, $value]) unless $check_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $check_value 6 279 285
33  
34 return (undef,
35 279 ▶ "Protocol error: value for field '" . $field->{name} . "' does not pass validation in tag pair '" . Protocol::FIX::humanize($pair) . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $field->check_raw($value) 2 277 279
36 unless $field->check_raw($value);
37  
38 277 return ([$field, $value]);
39  
40 } else {
41 3 ▶ if ($pair =~ /[\D$Protocol::FIX::TAG_SEPARATOR]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /[\D$Protocol::FIX::TAG_SEPARATOR]+/) 2 1 3
42 2 return (undef, "Protocol error: sequence '" . Protocol::FIX::humanize($pair) . "' does not match tag pair");
43 }
44 1 return;
45 }
46 }
47  
48 =head1 METHODS (for protocol developers)
49  
50 =head3 parse
51  
52 parse($protocol, $buff_ref)
53  
54 Tries to parse FIX message in the buffer refernce. Not for end-user usage;
55 please refer to L<Protocol/"parse_message">.
56  
57 =cut
58  
59 124 ▶sub parse {
Subroutine parse: called CC=21 SCAR=30.5
Pod documented
60 124 my ($protocol, $buff_ref) = @_;
61 124 my $consumed_length = 0;
62  
63 # this are fatal messages, i.e. point to developer error, hence we die
64 124 ▶ die("buffer is undefined, cannot parse") unless defined $buff_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buff_ref 0 124 124
65 124 ▶ die("buffer is not scalar reference") unless ref($buff_ref) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $buff_ref eq "SCALAR" 0 124 124
66  
67 124 my $begin_string = $protocol->{begin_string};
68 124 my $buff_length = length($$buff_ref);
69  
70 # no enough data
71 124 ▶ return (undef, undef) if $buff_length < length($begin_string);
Branch: 1 branch
BranchTrueFalseTotal
if $buff_length < length $begin_string 10 114 124
72  
73 114 ▶ if (substr($$buff_ref, 0, length($begin_string)) ne $begin_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$buff_ref, 0, length $begin_string) ne $begin_string) 1 113 114
74 1 return (undef, "Mismatch protocol introduction, expected: $begin_string");
75 }
76  
77 # no enough data
78 113 ▶ return if $buff_length == length($begin_string);
Branch: 1 branch
BranchTrueFalseTotal
if $buff_length == length $begin_string 2 111 113
79  
80 111 my @header_pairs = split $Protocol::FIX::SEPARATOR, $$buff_ref, 3;
81  
82 # minimal required fields: BeginString, MessageLength, MessageType, CheckSum
83 111 ▶ return (undef, "Protocol error: separator expecter right after $begin_string")
Branch: 1 branch
BranchTrueFalseTotal
if @header_pairs < 2 1 110 111
84 if (@header_pairs < 2);
85  
86 110 $consumed_length += length($begin_string) + 1;
87  
88 # extract, but do not check-for-correctness body length
89 110 my $body_length = do {
90 110 my $pair_is_complete = @header_pairs > 2;
91 110 my ($tag_pair, $err) = _parse_tag_pair($protocol, $header_pairs[1], $pair_is_complete);
92 110 ▶ return (undef, $err) unless $tag_pair;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag_pair 6 104 110
93  
94 104 my ($field, $value) = @$tag_pair;
95 104 ▶ if ($field->{name} ne 'BodyLength') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"name"} ne "BodyLength") 1 103 104
96 1 return (undef, "Protocol error: expected field 'BodyLength', but got '" . $field->{name} . "', in sequence '" . $header_pairs[1] . "'");
97 }
98 103 ▶ return unless $pair_is_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair_is_complete 5 98 103
99  
100 98 $consumed_length += length($header_pairs[1]) + 1;
101 98 $value;
102 };
103  
104 98 ▶ return (undef, undef) if $buff_length <= $consumed_length + $body_length;
Branch: 1 branch
BranchTrueFalseTotal
if $buff_length <= $consumed_length + $body_length 69 29 98
105 # -1 is used to include separator
106 29 my $trailer = substr($$buff_ref, $consumed_length + $body_length);
107  
108 # -1 is included as it is terminator / end of message boundary
109 29 my @trailer_pairs = split $Protocol::FIX::SEPARATOR, $trailer, -1;
110  
111 # 2 tags expected: checksum - empty(or may be new message)
112 # the trailing SOH is included into BODY
113 29 ▶ return (undef, undef) if (@trailer_pairs < 2) || !$trailer_pairs[0];
Branch: 1 branch
BranchTrueFalseTotal
if @trailer_pairs < 2 or not $trailer_pairs[0] 7 22 29
Condition66%2/3
@trailer_pairs < 2 or not $trailer_pairs[0]
l!l&&r!l&&!r
7022
MC/DC50%1/2
@trailer_pairs < 2 or not $trailer_pairs[0]
@trailer_pairs < 2 not $trailer_pairs[0]
Truth table66%2/3
A or not(B)
A= @trailer_pairs < 2B= not $trailer_pairs[0]
ABresult
1X1
011
000
114 # from here we assume $body_length is valid
115  
116 22 my $checksum_pair = $trailer_pairs[0];
117 22 my $body;
118 22 my $checksum = do {
119 22 my ($tag_pair, $err) = _parse_tag_pair($protocol, $checksum_pair, 1);
120 22 ▶ return (undef, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 22 22
121  
122 22 my ($field, $value) = @$tag_pair;
123 22 ▶ if ($field->{name} ne 'CheckSum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"name"} ne "CheckSum") 0 22 22
124 0 return (undef, "Protocol error: expected field 'CheckSum', but got '" . $field->{name} . "', in sequence '" . $checksum_pair . "'");
125 }
126  
127 22 my $is_number = ($value =~ /^\d{3}$/);
128  
129 22 ▶ if (!$is_number || ($is_number && ($value > 255))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_number or $is_number and $value > 255) 3 19 22
Condition83%5/6
not $is_number or $is_number and $value > 255
l!l&&r!l&&!r
2119
$is_number and $value > 255
!ll&&!rl&&r
0191
MC/DC66%2/3
not $is_number or $is_number and $value > 255
not $is_number $is_number $value > 255
Truth table75%3/4
not(A) or B and C
A= not $is_numberB= $is_numberC= $value > 255
ABCresult
1XX1
0111
00X0
0100
130 return (undef,
131 "Protocol error: value for field '"
132 . $field->{name}
133 3 . "' does not pass validation in tag pair '"
134 . Protocol::FIX::humanize($checksum_pair)
135 . "'");
136 }
137  
138 19 my $header_body = substr($$buff_ref, 0, $consumed_length + $body_length);
139 19 my $sum = 0;
140 19 $sum += ord $_ for split //, $header_body;
141 19 $sum %= 256;
142  
143 19 ▶ if ($sum != $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum != $value) 1 18 19
144 return (undef,
145 1 "Protocol error: Checksum mismatch; got $sum, expected $value for message '" . Protocol::FIX::humanize($header_body) . "'");
146 }
147 18 $body = substr($$buff_ref, $consumed_length, $body_length);
148 18 $value;
149 };
150 18 my ($message, $error) = _parse_body($protocol, \$body);
151 18 ▶ return (undef, $error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 7 11 18
152  
153 # checksum surrounded by separators
154 11 my $trailer_length = length($checksum_pair) + 1;
155 11 my $total_length = $consumed_length + $body_length + $trailer_length;
156  
157 11 $$buff_ref = substr $$buff_ref, $total_length;
158 11 return ($message, undef);
159 }
160  
161 18 ▶sub _parse_body {
Subroutine _parse_body: called CC=5 SCAR=16.1
162 18 my ($protocol, $body_ref) = @_;
163 18 my @pairs = split $Protocol::FIX::SEPARATOR, $$body_ref;
164  
165 my @tag_pairs = map {
166 18 my ($tag_pair, $err) = _parse_tag_pair($protocol, $_, 1);
167 158 ▶ return (undef, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 157 158
168 157 $tag_pair;
169 } @pairs;
170  
171 17 ▶ my $msg_pair_idx = first { $tag_pairs[$_]->[0]->{name} eq 'MsgType' } (0 .. @tag_pairs - 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
172 17 ▶ return (undef, "Protocol error: 'MsgType' was not found in body")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg_pair_idx 1 16 17
173 unless defined $msg_pair_idx;
174  
175 16 my $msg_type_pair = splice @tag_pairs, $msg_pair_idx, 1;
176  
177 16 my (undef, $msg_type) = @$msg_type_pair;
178 16 my $message = $protocol->{messages_lookup}->{by_number}->{$msg_type};
179 # we don't die, as it might be possible to use custom message types
180 # http://fixwiki.org/fixwiki/MsgType
181 16 ▶ return (undef, "Protocol error: MessageType '$msg_type' is not available")
Branch: 1 branch
BranchTrueFalseTotal
unless $message 0 16 16
182 unless $message;
183  
184 16 my ($ta, $err) = _construct_tag_accessor($protocol, $message, \@tag_pairs, 1);
185 16 ▶ return (undef, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 5 11 16
186 11 return (Protocol::FIX::MessageInstance->new($message, $ta));
187 }
188  
189 17 ▶sub _construct_tag_accessor_component {
Subroutine _construct_tag_accessor_component: called CC=2 SCAR=6.9
190 17 my ($protocol, $composite, $tag_pairs) = @_;
191 17 my $field = $tag_pairs->[0]->[0];
192  
193 17 my $sub_composite_name = $composite->{field_to_component}->{$field->{name}};
194 17 my $composite_desc = $composite->{composite_by_name}->{$sub_composite_name};
195 # TODO: check for required in description?
196 17 my ($sub_composite) = @$composite_desc;
197 17 my ($ta, $error) = _construct_tag_accessor($protocol, $sub_composite, $tag_pairs, 0);
198 17 ▶ return (undef, $error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 16 17
199 16 return ([$sub_composite => $ta]);
200 }
201  
202 121 ▶sub _construct_tag_accessor_field {
Subroutine _construct_tag_accessor_field: called CC=2 SCAR=6.9
203 121 my (undef, undef, $tag_pairs) = @_;
204  
205 121 my ($field, $value) = @{shift(@$tag_pairs)};
206 my $humanized_value =
207 $field->has_mapping
208 121 ▶ ? $field->{values}->{by_id}->{$value}
Branch: 1 branch
BranchTrueFalseTotal
$field->has_mapping ? : 22 99 121
209 : $value;
210 121 return ([$field => $humanized_value]);
211 }
212  
213 12 ▶sub _construct_tag_accessor_group {
Subroutine _construct_tag_accessor_group: called CC=3 SCAR=11.0
214 12 my ($protocol, $composite, $tag_pairs) = @_;
215 12 my ($field, $value) = @{shift(@$tag_pairs)};
216  
217 12 my $composite_desc = $composite->{composite_by_name}->{$field->{name}};
218 # todo: check that group have required field?
219 12 my ($sub_composite, undef) = @$composite_desc;
220  
221 12 my @tag_accessors;
222 12 for my $idx (1 .. $value) {
223 20 my ($ta) = _construct_tag_accessor($protocol, $sub_composite, $tag_pairs, 0);
224 return (undef,
225 "Protocol error: cannot construct item #${idx} for "
226 . $composite->{type} . " '"
227 . $composite->{name} . "' ("
228 . $sub_composite->{type} . " '"
229 20 ▶ . $sub_composite->{name} . "')")
Branch: 1 branch
BranchTrueFalseTotal
unless $ta 1 19 20
230 unless $ta;
231 19 push @tag_accessors, $ta;
232 }
233 11 return ([$sub_composite => \@tag_accessors]);
234 }
235  
236 53 ▶sub _construct_tag_accessor {
Subroutine _construct_tag_accessor: called CC=16 SCAR=27.7
237 53 my ($protocol, $composite, $tag_pairs, $fail_on_missing) = @_;
238  
239 53 my @direct_pairs;
240 my %parsed_subcomposites;
241 53 ▶ while (@$tag_pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tag_pairs) 172 29 201
242  
243 # non-destructive look ahead
244 172 my $field = $tag_pairs->[0]->[0];
245  
246 172 my $owner = $composite->{field_to_component}->{$field->{name}};
247 # do not look ahead too much, i.e. for group it is enough to construct just one item
248 172 ▶ last if $parsed_subcomposites{$field->{name}} || ($owner && $parsed_subcomposites{$owner});
Branch: 1 branch
BranchTrueFalseTotal
if $parsed_subcomposites{$field->{"name"}} or $owner and $parsed_subcomposites{$owner} 9 163 172
Condition100%6/6
$parsed_subcomposites{$field->{"name"}} or $owner and $parsed_subcomposites{$owner}
l!l&&r!l&&!r
81163
$owner and $parsed_subcomposites{$owner}
!ll&&!rl&&r
131501
MC/DC100%3/3
$parsed_subcomposites{$field->{"name"}} or $owner and $parsed_subcomposites{$owner}
$parsed_subcomposites{$field->{"name"}} $owner $parsed_subcomposites{$owner}
Truth table100%4/4
A or B and C
A= $parsed_subcomposites{$field->{"name"}}B= $ownerC= $parsed_subcomposites{$owner}
ABCresult
1XX1
0111
00X0
0100
249  
250 # The logic is following:
251 # 1. try to construct sub-components (if there are fields pointing to them)
252 # 2. otherwise try to construct field group
253 # 3. or simple (single) field
254 my $constructor =
255 ($owner && ($owner ne $composite->{name}))
256 ? \&_construct_tag_accessor_component
257 163 ▶ : ($composite->{composite_by_name}->{$field->{name}}) ? $field->{type} eq 'NUMINGROUP'
Branch: 3 branches
BranchTrueFalseTotal
$owner && $owner ne $composite->{'name'} ? : 17 146 163
$composite->{'composite_by_name'}{$field->{'name'}} ? : 133 13 146
$field->{'type'} eq 'NUMINGROUP' ? : 12 121 133
Condition100%3/3
$owner and $owner ne $composite->{"name"}
!ll&&!rl&&r
1313317
MC/DC100%2/2
$owner and $owner ne $composite->{"name"}
$owner $owner ne $composite->{"name"}
Truth table100%3/3
A and B
A= $ownerB= $owner ne $composite->{"name"}
ABresult
0X0
100
111
258 ? \&_construct_tag_accessor_group
259 : \&_construct_tag_accessor_field
260 : undef;
261  
262 163 ▶ if ($constructor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constructor) { } 150 13 163
263 150 my ($ta_descr, $error) = $constructor->($protocol, $composite, $tag_pairs);
264 150 ▶ return (undef, $error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 148 150
265 148 my ($sub_composite, $tags_accessor) = @$ta_descr;
266 148 push @direct_pairs, $sub_composite => $tags_accessor;
267 148 $parsed_subcomposites{$sub_composite->{name}} = 1;
268 } else {
269 # the error can occur only for top-level message
270 return (undef,
271 13 ▶ "Protocol error: field '" . $field->{name} . "' was not expected in " . $composite->{type} . " '" . $composite->{name} . "'")
Branch: 1 branch
BranchTrueFalseTotal
if $fail_on_missing 3 10 13
272 if $fail_on_missing;
273 10 last;
274 }
275 }
276 48 ▶ for my $mandatory_composite (@{$composite->{mandatory_composites} // []}) {
Condition50%1/2
$composite->{"mandatory_composites"} // []
l!l
480
MC/DC0%0/1
$composite->{"mandatory_composites"} // []
$composite->{"mandatory_composites"}
Truth table50%1/2
A
A= $composite->{"mandatory_composites"}
Aresult
11
00
277 83 ▶ if (!exists $parsed_subcomposites{$mandatory_composite}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parsed_subcomposites{$mandatory_composite}) 1 82 83
278 1 return (undef, "Protocol error: '$mandatory_composite' is mandatory for " . $composite->{type} . " '" . $composite->{name} . "'");
279 }
280 }
281 47 ▶ return (@direct_pairs ? Protocol::FIX::TagsAccessor->new(\@direct_pairs) : ());
Branch: 1 branch
BranchTrueFalseTotal
@direct_pairs ? : 46 1 47
282 }
283  
284 18 1;
« FIX/BaseComposite.pm ↑ Summary FIX/Field.pm »