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 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Net::EGTS::SubRecord;
4 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use Net::EGTS::Codes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Net::EGTS::Util qw(usize);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶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 15 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 15 has SRT => is => 'rw', isa => 'BYTE';
31 # Subrecord Length
32 has SRL =>
33 is => 'rw',
34 isa => 'USHORT',
35 lazy => 1,
36 builder => sub {
37 10 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
38 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 10 return length($self->SRD);
40 },
41 15 ;
42 # Subrecord Data
43 has SRD =>
44 is => 'rw',
45 isa => 'Maybe[BINARY]',
46 trigger => sub {
47 28 ▶ my ($self, $value, $old) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
48 28 ▶ die 'Subrecord Data too long' if length($value) > 65495;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > 65495 0 28 28
49 }
50 15 ;
51  
52 # SubRecord binary
53 15 has bin => is => 'rw', isa => 'Str', default => '';
54  
55 around BUILDARGS => sub {
56 28 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
57 28 my $class = shift;
58  
59 # simple scalar decoding support
60 28 ▶ my $bin = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 1 27 28
61 28 my %opts = @_;
62  
63 28 ▶ return $class->$orig( bin => $bin, %opts ) if $bin;
Branch: 1 branch
BranchTrueFalseTotal
if $bin 1 27 28
64 27 return $class->$orig( %opts );
65 15 };
66 sub BUILD {
67 28 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
68 28 my $args = shift;
69 28 ▶ $self->decode( \$self->bin ) if length $self->bin;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->bin 5 23 28
70 }
71  
72 # Get chunk from binary and store it
73 sub take {
74 54 ▶ my ($self, $bin, $mask, $length) = @_;
Subroutine take: called CC=3 SCAR=11.1
Pod undocumented
75 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 54 ▶ $length //= usize($mask);
Condition66%2/3
$length //= &usize($mask)
l!l&&r!l&&!r
18360
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 54 ▶ confess "Can`t get chunk of length $length" if $length > length $$bin;
Branch: 1 branch
BranchTrueFalseTotal
if $length > length $$bin 0 54 54
79  
80 54 my $chunk = substr $$bin, 0 => $length, '';
81 54 $self->bin( $self->bin . $chunk );
82  
83 54 return unpack $mask => $chunk;
84 }
85  
86 # Helper to get portion of data
87 sub nip {
88 58 ▶ my ($self, $bin, $mask, $length) = @_;
Subroutine nip: called CC=3 SCAR=11.1
Pod undocumented
89 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 58 ▶ $length //= usize($mask);
Condition66%2/3
$length //= &usize($mask)
l!l&&r!l&&!r
6520
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 58 ▶ confess "Can`t get chunk of length $length" if $length > length $$bin;
Branch: 1 branch
BranchTrueFalseTotal
if $length > length $$bin 0 58 58
93  
94 58 my $chunk = substr $$bin, 0 => $length, '';
95 58 return unpack $mask => $chunk;
96 }
97  
98 =head2 encode
99  
100 Build subrecord as binary
101  
102 =cut
103  
104 sub encode {
105 10 ▶ my ($self) = @_;
Subroutine encode: called CC=4 SCAR=14.1
Pod documented
106 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 10 ▶ croak 'Subrecord Туре required' unless defined $self->SRT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->SRT 0 10 10
109 10 ▶ croak 'Subrecord Data required' unless defined $self->SRD;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->SRD 0 10 10
110 10 ▶ croak 'Subrecord Length roo big' unless $self->SRL <= 65495;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SRL <= 65495 0 10 10
111  
112 10 my $bin = pack 'C S a*' => $self->SRT, $self->SRL, $self->SRD;
113 10 $self->bin( $bin );
114 10 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 sub decode {
125 18 ▶ my ($self, $bin) = @_;
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
126 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 18 $self->SRT( $self->take($bin => 'C') );
129 18 $self->SRL( $self->take($bin => 'S') );
130 18 $self->SRD( $self->take($bin => 'a*' => $self->SRL) );
131  
132 18 return $self;
133 }
134  
135 =head2 decode_all \$bin
136  
137 Parse all subrecords from record Record Data
138  
139 =cut
140  
141 sub decode_all {
142 7 ▶ my ($class, $bin) = @_;
Subroutine decode_all: called CC=4 SCAR=13.9
Pod documented
143 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 7 my @result;
146 7 ▶ while( my $length = length $bin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = length $bin) 8 7 15
147 8 my $type = unpack 'C' => substr $bin, 0 => usize('C');
148  
149 8 my $subclass = $TYPES{ $type };
150 8 load $subclass;
151  
152 8 my $self = $subclass->new->decode( \$bin );
153 8 ▶ die 'Something wrong in subrecords decode' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 8 8
154  
155 8 push @result, $self;
156 }
157  
158 7 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 6 7
159 }
160  
161 =head2 as_debug
162  
163 Return human readable string
164  
165 =cut
166  
167 sub as_debug {
168 18 ▶ my ($self) = @_;
Subroutine as_debug: called CC=4 SCAR=13.9
Pod documented
169 15 ▶ 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 15 __PACKAGE__->meta->make_immutable();
« lib/Net/EGTS/Packet/SignedAppdata.pm ↑ Summary lib/Net/EGTS/SubRecord/Auth/DispatcherIdentity.pm »