lib/Net/EGTS/SubRecord.pm

filter: Statementstmt 100% 117 / 117 Branchbran 73.3% 22 / 30 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 26 / 26 Podpod 71.4% 5 / 7 totaltotal 91.5% 174 / 190 CC 18 SCAR 8.6
CC
18
Cov
93%
encode
4.1
decode_all
4.0
as_debug
4.0
CRAP 2.4
« lib/Net/EGTS/Packet/SignedAppdata.pm ↑ Summary lib/Net/EGTS/SubRecord/Auth/DispatcherIdentity.pm »
1 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Net::EGTS::SubRecord;
4 14 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use Net::EGTS::Codes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Net::EGTS::Util qw(usize);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Net::EGTS::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Net::EGTS::SubRecord - SubRecord common part
18  
19 =cut
20  
21 # Packet types and classes
22 14 our %TYPES = (
23 EGTS_SR_RECORD_RESPONSE, 'Net::EGTS::SubRecord::Auth::RecordResponse',
24 EGTS_SR_DISPATCHER_IDENTITY, 'Net::EGTS::SubRecord::Auth::DispatcherIdentity',
25 EGTS_SR_RESULT_CODE, 'Net::EGTS::SubRecord::Auth::ResultCode',
26 EGTS_SR_POS_DATA, 'Net::EGTS::SubRecord::Teledata::PosData',
27 );
28  
29 # Subrecord Туре
30 14 has SRT => is => 'rw', isa => 'BYTE';
31 # Subrecord Length
32 has SRL =>
33 is => 'rw',
34 isa => 'USHORT',
35 lazy => 1,
36 7 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 7 my ($self) = @_;
38 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 return length($self->SRD);
40 },
41 14 ;
42 # Subrecord Data
43 has SRD =>
44 is => 'rw',
45 isa => 'Maybe[BINARY]',
46 22 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
47 22 my ($self, $value, $old) = @_;
48 22 ▶ die 'Subrecord Data too long' if length($value) > 65495;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > 65495 0 22 22
49 }
50 14 ;
51  
52 # SubRecord binary
53 14 has bin => is => 'rw', isa => 'Str', default => '';
54  
55 22 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
56 22 my $orig = shift;
57 22 my $class = shift;
58  
59 # simple scalar decoding support
60 22 ▶ my $bin = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 1 21 22
61 22 my %opts = @_;
62  
63 22 ▶ return $class->$orig( bin => $bin, %opts ) if $bin;
Branch: 1 branch
BranchTrueFalseTotal
if $bin 1 21 22
64 21 return $class->$orig( %opts );
65 14 };
66 22 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
67 22 my $self = shift;
68 22 my $args = shift;
69 22 ▶ $self->decode( \$self->bin ) if length $self->bin;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->bin 5 17 22
70 }
71  
72 # Get chunk from binary and store it
73 45 ▶sub take {
Subroutine take: called CC=3 SCAR=11.1
Pod undocumented
74 45 my ($self, $bin, $mask, $length) = @_;
75 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 45 ▶ $length //= usize($mask);
Condition66%2/3
$length //= &usize($mask)
l!l&&r!l&&!r
15300
MC/DC0%0/2
$length //= &usize($mask)
$length &usize($mask)
Truth table66%2/3
A //= B
A= $lengthB= &usize($mask)
ABresult
1X1
011
000
78 45 ▶ confess "Can`t get chunk of length $length" if $length > length $$bin;
Branch: 1 branch
BranchTrueFalseTotal
if $length > length $$bin 0 45 45
79  
80 45 my $chunk = substr $$bin, 0 => $length, '';
81 45 $self->bin( $self->bin . $chunk );
82  
83 45 return unpack $mask => $chunk;
84 }
85  
86 # Helper to get portion of data
87 52 ▶sub nip {
Subroutine nip: called CC=3 SCAR=11.1
Pod undocumented
88 52 my ($self, $bin, $mask, $length) = @_;
89 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 52 ▶ $length //= usize($mask);
Condition66%2/3
$length //= &usize($mask)
l!l&&r!l&&!r
6460
MC/DC0%0/2
$length //= &usize($mask)
$length &usize($mask)
Truth table66%2/3
A //= B
A= $lengthB= &usize($mask)
ABresult
1X1
011
000
92 52 ▶ confess "Can`t get chunk of length $length" if $length > length $$bin;
Branch: 1 branch
BranchTrueFalseTotal
if $length > length $$bin 0 52 52
93  
94 52 my $chunk = substr $$bin, 0 => $length, '';
95 52 return unpack $mask => $chunk;
96 }
97  
98 =head2 encode
99  
100 Build subrecord as binary
101  
102 =cut
103  
104 7 ▶sub encode {
Subroutine encode: called CC=4 SCAR=14.1
Pod documented
105 7 my ($self) = @_;
106 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 7 ▶ croak 'Subrecord Туре required' unless defined $self->SRT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->SRT 0 7 7
109 7 ▶ croak 'Subrecord Data required' unless defined $self->SRD;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->SRD 0 7 7
110 7 ▶ croak 'Subrecord Length roo big' unless $self->SRL <= 65495;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SRL <= 65495 0 7 7
111  
112 7 my $bin = pack 'C S a*' => $self->SRT, $self->SRL, $self->SRD;
113 7 $self->bin( $bin );
114 7 return $bin;
115 }
116  
117 =head2 decode \$bin
118  
119 Decode binary I<$bin> into subrecord object.
120 The binary stream will be truncated!
121  
122 =cut
123  
124 15 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
125 15 my ($self, $bin) = @_;
126 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 15 $self->SRT( $self->take($bin => 'C') );
129 15 $self->SRL( $self->take($bin => 'S') );
130 15 $self->SRD( $self->take($bin => 'a*' => $self->SRL) );
131  
132 15 return $self;
133 }
134  
135 =head2 decode_all \$bin
136  
137 Parse all subrecords from record Record Data
138  
139 =cut
140  
141 4 ▶sub decode_all {
Subroutine decode_all: called CC=4 SCAR=13.9
Pod documented
142 4 my ($class, $bin) = @_;
143 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 4 my @result;
146 4 ▶ while( my $length = length $bin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = length $bin) 5 4 9
147 5 my $type = unpack 'C' => substr $bin, 0 => usize('C');
148  
149 5 my $subclass = $TYPES{ $type };
150 5 load $subclass;
151  
152 5 my $self = $subclass->new->decode( \$bin );
153 5 ▶ die 'Something wrong in subrecords decode' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 5 5
154  
155 5 push @result, $self;
156 }
157  
158 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
159 }
160  
161 =head2 as_debug
162  
163 Return human readable string
164  
165 =cut
166  
167 18 ▶sub as_debug {
Subroutine as_debug: called CC=4 SCAR=13.9
Pod documented
168 18 my ($self) = @_;
169 14 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 18 my @bytes = ((unpack('B*', $self->bin)) =~ m{.{8}}g);
172  
173 18 my @str;
174 18 push @str => sprintf('SRT: %s', splice @bytes, 0 => usize('C'));
175 18 push @str => sprintf('SRL: %s %s', splice @bytes, 0 => usize('S'));
176  
177 18 ▶ if( @bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes) 18 0 18
178 18 ▶ if( my @qualify = inner() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@qualify) = inner()) { } 15 3 18
179 15 push @str => sprintf('SRD =>');
180 15 push @str, @qualify;
181 15 push @str => sprintf('<======');
182 } else {
183 3 my $it = natatime 4, @bytes;
184 3 my @chunks;
185 3 ▶ while (my @vals = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@vals) = &$it()) 3 3 6
186 3 push @chunks, join(' ', @vals);
187 }
188 3 push @str => sprintf('SRD: %s', join("\n ", @chunks));
189 }
190 }
191  
192 18 return join "\n", @str;
193 }
194  
195 14 __PACKAGE__->meta->make_immutable();
« lib/Net/EGTS/Packet/SignedAppdata.pm ↑ Summary lib/Net/EGTS/SubRecord/Auth/DispatcherIdentity.pm »