IMP/Pattern.pm

filter: Statementstmt 81.8% 113 / 138 Branchbran 53.9% 55 / 102 Conditioncond 46.6% 14 / 30 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 92.3% 12 / 13 Podpod 80.0% 4 / 5 totaltotal 66.0% 202 / 306 CC 63 SCAR 38.3
CC
63
Cov
67%
data
66.8
validate_cfg
41.4
INTERFACE
30.0
CRAP 46.1
« IMP/Example/IRCShout.pm ↑ Summary IMP/Example/LogServerCertificate.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Net::IMP::Pattern;
6 5 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use fields (
8 5 'rx', # Regexp from args rx|string
9 'rxlen', # max size rx can match
10 'rxdir', # only check this direction
11 'action', # deny|reject|replace
12 'actdata', # data for action
13 'buf', # locally buffered data to match rx, <rxlen and per dir
14 'buftype', # type of data in buffer
15 'offset', # buf[dir][0] is at offset in input stream dir
16 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Net::IMP; # import IMP_ constants
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub INTERFACE {
23 0 ▶ my Net::IMP::Pattern $factory = shift;
Subroutine INTERFACE: not called CC=5 SCAR=34.0
Pod undocumented
24 0 my $action = $factory->{factory_args}{action};
25 0 my @rv = IMP_PASS;
26 0 ▶ push @rv,
Branch: 4 branches
BranchTrueFalseTotal
$action eq 'deny' ? : 0 0 0
$action eq 'reject' ? : 0 0 0
$action eq 'replace' ? : 0 0 0
!$action ? : 0 0 0
27 $action eq 'deny' ? IMP_DENY :
28 $action eq 'reject' ? (IMP_REPLACE, IMP_TOSENDER) :
29 $action eq 'replace' ? IMP_REPLACE :
30 ! $action ? IMP_DENY :
31 croak("invalid action $action");
32 0 return [ undef, \@rv ];
33 };
34  
35 sub validate_cfg {
36 16 ▶ my ($class,%args) = @_;
Subroutine validate_cfg: called CC=16 SCAR=37.2
Pod documented
37  
38 16 my @err;
39 16 my $rx = delete $args{rx};
40 16 my $string = delete $args{string};
41 16 my $rxdir = delete $args{rxdir};
42  
43 16 ▶ if ($rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx) 16 0 16
44 16 my $rxlen = delete $args{rxlen};
45 16 ▶ push @err, "rxlen must be given and >0" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $rxlen and $rxlen =~ /^\d+$/ and $rxlen > 0 0 16 16
Condition33%2/6
$rxlen and $rxlen =~ /^\d+$/ and $rxlen > 0
!ll&&!rl&&r
0016
$rxlen and $rxlen =~ /^\d+$/
!ll&&!rl&&r
0016
MC/DC0%0/3
$rxlen and $rxlen =~ /^\d+$/ and $rxlen > 0
$rxlen $rxlen =~ /^\d+$/ $rxlen > 0
Truth table25%1/4
A and B and C
A= $rxlenB= $rxlen =~ /^\d+$/C= $rxlen > 0
ABCresult
0XX0
10X0
1100
1111
46 $rxlen and $rxlen =~m{^\d+$} and $rxlen>0;
47 16 ▶ if ( ref($rx) ne 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rx ne 'Regexp') { } 0 16 16
elsif ('' =~ /$rx/) { } 0 16 16
48 0 ▶ push @err, "rx must be regex" if ref($rx) ne 'Regexp'
Branch: 1 branch
BranchTrueFalseTotal
if ref $rx ne "Regexp" 0 0 0
49 } elsif ( '' =~ $rx ) {
50 0 push @err,"rx should not match empty string"
51 }
52 }
53  
54 16 ▶ if ( defined $string ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $string) { } 0 16 16
elsif (not $rx) { } 0 16 16
55 0 ▶ push @err, "only rx or string should be given, not both" if $rx;
Branch: 1 branch
BranchTrueFalseTotal
if $rx 0 0 0
56 } elsif ( ! $rx ) {
57 0 push @err, "rx+rxlen or string need to be given for pattern";
58 }
59  
60 16 ▶ push @err, "rxdir must be 0|1" if defined $rxdir and not $rxdir ~~ [0,1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $rxdir and not $rxdir ~~ [0, 1] 0 16 16
Condition33%1/3
defined $rxdir and not $rxdir ~~ [0, 1]
!ll&&!rl&&r
1600
MC/DC0%0/2
defined $rxdir and not $rxdir ~~ [0, 1]
defined $rxdir not $rxdir ~~ [0, 1]
Truth table33%1/3
A and not(B)
A= defined $rxdirB= not $rxdir ~~ [0, 1]
ABresult
0X0
100
111
61  
62 16 my $act = delete $args{action};
63 16 ▶ push @err, "action can only be deny|reject|replace" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $act and $act ~~ ["deny", "reject", "replace"] 0 16 16
Condition33%1/3
$act and $act ~~ ["deny", "reject", "replace"]
!ll&&!rl&&r
0016
MC/DC0%0/2
$act and $act ~~ ["deny", "reject", "replace"]
$act $act ~~ ["deny", "reject", "replace"]
Truth table33%1/3
A and B
A= $actB= $act ~~ ["deny", "reject", "replace"]
ABresult
0X0
100
111
64 $act and $act ~~ [qw(deny reject replace)];
65 16 ▶ push @err, "action $act needs actdata" if ! defined(delete $args{actdata});
Branch: 1 branch
BranchTrueFalseTotal
unless defined delete $args{"actdata"} 0 16 16
66  
67 16 push @err, $class->SUPER::validate_cfg(%args);
68 16 return @err;
69 }
70  
71  
72 # create new analyzer object
73 sub new_analyzer {
74 19 ▶ my ($factory,%args) = @_;
Subroutine new_analyzer: called CC=4 SCAR=16.5
Pod documented
75 19 my $fargs = $factory->{factory_args};
76  
77 19 my $rxlen;
78 19 my $rx = $fargs->{rx};
79 19 ▶ if ($rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx) { } 19 0 19
80 19 $rxlen = $fargs->{rxlen};
81 } else {
82 0 $rx = $fargs->{string};
83 0 $rxlen = length($rx);
84 0 $rx = qr/\Q$rx/;
85 }
86  
87 my Net::IMP::Pattern $self = $factory->SUPER::new_analyzer(
88 %args, # cb, meta
89 rx => $rx,
90 rxlen => $rxlen,
91 rxdir => $fargs->{rxdir},
92 action => $fargs->{action},
93 actdata => $fargs->{actdata},
94 19 buf => ['',''], # per direction
95 buftype => [0,0], # per direction
96 offset => [0,0], # per direction
97 );
98  
99 19 ▶ if ( defined $self->{rxdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"rxdir"}) 0 19 19
100 # if rx is specified only for one direction immediatly issue PASS until
101 # end for the other direction
102 $self->run_callback([
103 IMP_PASS,
104 0 ▶ $self->{rxdir} ? 0:1,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rxdir'} ? : 0 0 0
105 IMP_MAXOFFSET,
106 ]);
107 }
108  
109 19 return $self;
110 }
111  
112 sub data {
113 82 ▶ my Net::IMP::Pattern $self = shift;
Subroutine data: called CC=39 SCAR=42.0
Pod documented
114 82 my ($dir,$data,$offset,$type) = @_;
115  
116 82 ▶ $offset and die "cannot deal with gaps in data";
Branch: 1 branch
BranchTrueFalseTotal
$offset and die "cannot deal with gaps in data" 0 82 82
117  
118 # if this is the wrong dir return, we already issued PASS
119 82 ▶ return if defined $self->{rxdir} and $dir != $self->{rxdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"rxdir"} and $dir != $self->{"rxdir"} 0 82 82
Condition33%1/3
defined $self->{"rxdir"} and $dir != $self->{"rxdir"}
!ll&&!rl&&r
8200
MC/DC0%0/2
defined $self->{"rxdir"} and $dir != $self->{"rxdir"}
defined $self->{"rxdir"} $dir != $self->{"rxdir"}
Truth table33%1/3
A and B
A= defined $self->{"rxdir"}B= $dir != $self->{"rxdir"}
ABresult
0X0
100
111
120  
121 # accumulate results
122 82 my @rv;
123  
124 my $buf;
125 82 ▶ if ( $type > 0 or $type != $self->{buftype}[$dir] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0 or $type != $self->{'buftype'}[$dir]) { } 24 58 82
Condition100%3/3
$type > 0 or $type != $self->{"buftype"}[$dir]
l!l&&r!l&&!r
32158
MC/DC100%2/2
$type > 0 or $type != $self->{"buftype"}[$dir]
$type > 0 $type != $self->{"buftype"}[$dir]
Truth table100%3/3
A or B
A= $type > 0B= $type != $self->{"buftype"}[$dir]
ABresult
1X1
011
000
126 # packet data or other streaming type
127 24 $buf = $data;
128 24 ▶ if ( $self->{buf}[$dir] ne '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'buf'}[$dir] ne '') { } 1 23 24
elsif (not $self->{'buftype'}[$dir] || $type > 0) { } 20 3 23
129 # pass previous buffer and reset it
130 1 debug("reset buffer because type=$type, buftype=$self->{buftype}[$dir]");
131 1 $self->{offset}[$dir] += length($self->{buf}[$dir]);
132 1 $self->{buf}[$dir] = '';
133 1 push @rv, [ IMP_PASS,$dir,$self->{offset}[$dir] ];
134 ▶ } elsif ( ! $self->{buftype}[$dir] and not $type > 0 ) {
Condition100%3/3
$self->{'buftype'}[$dir] || $type > 0
l!l&&r!l&&!r
2120
MC/DC100%2/2
$self->{'buftype'}[$dir] || $type > 0
$self->{'buftype'}[$dir] $type > 0
Truth table100%3/3
A || B
A= $self->{'buftype'}[$dir]B= $type > 0
ABresult
1X1
011
000
135 # initial streaming buf
136 20 $self->{buf}[$dir] = $buf;
137 }
138 24 $self->{buftype}[$dir] = $type;
139 } else {
140 # streaming data, match can span multiple chunks
141 58 $buf = ( $self->{buf}[$dir] .= $data );
142 }
143  
144 $DEBUG && debug("got %d bytes $type on %d, bufsz=%d, rxlen=%d",
145 82 ▶ length($data),$dir,length($buf),$self->{rxlen});
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Pattern::DEBUG and debug("got %d bytes $type on %d, bufsz=%d, rxlen=%d", length $data, $dir, length $buf, $self->{"rxlen"}) 0 82 82
146  
147 # for packet types we accumulate datain newdata and set changed if newdata
148 # are different from old
149 82 my $changed = 0;
150 82 my $newdata = '';
151  
152 82 while (1) {
153 109 ▶ if ( my ($good,$match) = $buf =~m{\A(.*?)($self->{rx})}s ) {
Branch: 4 branches
BranchTrueFalseTotal
if (() = $buf =~ /\A(.*?)($self->{'rx'})/s) { } 34 75 109
elsif ($type > 0) { } 3 72 75
elsif ((my $d = length($buf) - $self->{'rxlen'} + 1) > 0) { } 24 48 72
elsif ($data eq '') { } 23 25 48
154 # rx matched:
155 # - strip up to end of rx from buf
156 # - issue IMP_PASS for all data in front of rx
157 # - handle rx according to action
158 # - continue with buf after rx (e.g. redo loop)
159  
160 34 ▶ if ( length($match)> $self->{rxlen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $match > $self->{'rxlen'}) { } 0 34 34
161 # user specified a rx, which could match more than rxlen, e.g.
162 # something like qr{\d+}. make sure we only match rxlen bytes
163 0 ▶ if ( substr($match,0,$self->{rxlen}) =~m{\A($self->{rx})} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($match, 0, $self->{'rxlen'}) =~ /\A($self->{'rx'})/) { } 0 0 0
164 0 $match = $1;
165 } else {
166 # no match possible in rxlen bytes, reset match
167 # and add one char from original match to $good
168 # so that we don't try to match here again
169 0 $good .= substr($match,0,1);
170 0 $match = '';
171 }
172 } else {
173 # we checked in new_analyzer already that rx does not match
174 # empty string, so we should be save here that rxlen>=match>0
175 }
176  
177 34 ▶ if ( $good ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($good ne "") 23 11 34
178 23 ▶ $DEBUG && debug("pass %d bytes in front of match",
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Pattern::DEBUG and debug("pass %d bytes in front of match", length $good) 0 23 23
179 length($good));
180 # pass everything before the match and advance offset
181 23 $self->{offset}[$dir]+=length($good);
182 23 ▶ if ( $type>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) { } 3 20 23
183 # keep good
184 3 $newdata .= substr($buf,0,length($good),'');
185 } else {
186 # pass good
187 20 push @rv, [ IMP_PASS, $dir, $self->{offset}[$dir] ];
188 20 substr($buf,0,length($good),'');
189 }
190 }
191 # remove match
192 34 substr($buf,0,length($match),'');
193 34 $self->{offset}[$dir] += length($match);
194  
195 34 ▶ if ( $match eq '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($match eq '') { } 0 34 34
elsif ($self->{'action'} eq 'deny') { } 0 34 34
elsif ($self->{'action'} eq 'reject') { } 0 34 34
elsif ($self->{'action'} eq 'replace') { } 34 0 34
196 # match got reset if >rxlen -> no action
197  
198 # handle the matched pattern according to action
199 } elsif ( $self->{action} eq 'deny' ) {
200 # deny everything after
201 0 ▶ push @rv,[ IMP_DENY,$dir,$self->{actdata}//'' ];
Condition0%0/2
$self->{"actdata"} // ""
l!l
00
MC/DC0%0/1
$self->{"actdata"} // ""
$self->{"actdata"}
Truth table0%0/2
A
A= $self->{"actdata"}
Aresult
11
00
202 0 last; # deny is final
203  
204 } elsif ( $self->{action} eq 'reject' ) {
205 # forward nothing, send smthg back to sender
206 0 ▶ if ( $type > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) { } 0 0 0
207 # no need to add nothing to $newdata :)
208 0 $changed = 1;
209 } else {
210 push @rv,[
211 IMP_REPLACE,
212 $dir,
213 0 $self->{offset}[$dir],
214 '',
215 ];
216 }
217 push @rv,[ IMP_TOSENDER,$dir,$self->{actdata} ]
218 0 ▶ if $self->{actdata} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"actdata"} ne "" 0 0 0
219  
220 } elsif ( $self->{action} eq 'replace' ) {
221 # forward something else
222 34 ▶ if ( $type > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) { } 3 31 34
223 3 ▶ $newdata .= $self->{actdata}//'';
Condition50%1/2
$self->{"actdata"} // ""
l!l
30
MC/DC0%0/1
$self->{"actdata"} // ""
$self->{"actdata"}
Truth table50%1/2
A
A= $self->{"actdata"}
Aresult
11
00
224 3 $changed = 1;
225 } else {
226 push @rv,[
227 IMP_REPLACE,
228 $dir,
229 $self->{offset}[$dir],
230 31 ▶ $self->{actdata}//''
Condition50%1/2
$self->{"actdata"} // ""
l!l
310
MC/DC0%0/1
$self->{"actdata"} // ""
$self->{"actdata"}
Truth table50%1/2
A
A= $self->{"actdata"}
Aresult
11
00
231 ];
232 }
233  
234 } else {
235 # should not happen, because action was already checked
236 0 die "invalid action $self->{action}";
237 }
238  
239 34 ▶ last if $buf eq ''; # need more data
Branch: 1 branch
BranchTrueFalseTotal
if $buf eq "" 7 27 34
240  
241 } elsif ( $type > 0 ) {
242 # no matches across packets are allowed
243 3 $self->{offset}[$dir] += length($buf);
244 3 ▶ $newdata .= $buf if $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed 3 0 3
245 3 last;
246  
247 } elsif ( (my $d = length($buf) - $self->{rxlen} + 1) > 0 ) {
248 # rx did not match, but >=rxlen bytes in buf:
249 # we can IMP_PASS some, but rxlen-1 data needs to be kept in buffer
250 # so that we retry rx when new data come in
251 24 ▶ $DEBUG && debug("can pass %d of %d bytes",$d,length($buf));
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Pattern::DEBUG and debug("can pass %d of %d bytes", $d, length $buf) 0 24 24
252 24 push @rv, [ IMP_PASS, $dir, $self->{offset}[$dir] += $d ];
253 24 substr($buf,0,$d,'');
254  
255 24 last; # need more data
256  
257 } elsif ( $data eq '' ) {
258 # rx did not match, but eof:
259 # no more data will come which can match rx so we can pass the rest
260 23 ▶ $DEBUG && debug("pass rest of data on eof");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Pattern::DEBUG and debug("pass rest of data on eof") 0 23 23
261 23 push @rv,[ IMP_PASS,$dir,IMP_MAXOFFSET ];
262 23 $buf = '';
263  
264 23 last; # there will be no more matches because of no data
265  
266 } else {
267 # rx did not match, but no eof:
268 25 last; # need more data
269 }
270 }
271  
272 82 ▶ if ( $type > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) 3 79 82
273 3 ▶ if ( grep { IMP_DENY == $_->[0] } @rv ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {4352 == $_->[0];} @rv) { } 0 3 3
elsif ($changed) { } 3 0 3
274 # leave deny alone
275 } elsif ( $changed ) {
276 # replace whole packet
277 3 push @rv, [ IMP_REPLACE,$dir,$self->{offset}[$dir],$newdata ];
278 } else {
279 # pass whole packet
280 0 push @rv, [ IMP_PASS,$dir,$self->{offset}[$dir] ];
281 }
282 }
283  
284 82 ▶ if ( @rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rv) { } 69 13 82
285 69 ▶ $self->{buf}[$dir] = $buf unless $type > 0; # $buf got changed, put back
Branch: 1 branch
BranchTrueFalseTotal
unless $type > 0 66 3 69
286 69 debug("bufsize=".length($self->{buf}[$dir]));
287 69 $self->run_callback(@rv);
288 } else {
289 13 ▶ $DEBUG && debug("need more data");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Pattern::DEBUG and debug("need more data") 0 13 13
290 }
291 }
292  
293 sub str2cfg {
294 3 ▶ my ($class,$str) = @_;
Subroutine str2cfg: called CC=3 SCAR=11.6
Pod documented
295 3 my %cfg = $class->SUPER::str2cfg($str);
296 3 ▶ if ($cfg{rx}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"rx"}) 3 0 3
297 3 ▶ $cfg{rx} = eval { qr/$cfg{rx}/ }
Condition33%1/3
eval { do { qr/$cfg{'rx'}/ } } || croak("'$cfg{'rx'}' is no valid regex")
l!l&&r!l&&!r
300
MC/DC0%0/2
eval { do { qr/$cfg{'rx'}/ } } || croak("'$cfg{'rx'}' is no valid regex")
eval { do { qr/$cfg{'rx'}/ } } croak("'$cfg{'rx'}' is no valid regex")
Truth table33%1/3
A || B
A= eval { do { qr/$cfg{'rx'}/ } }B= croak("'$cfg{'rx'}' is no valid regex")
ABresult
1X1
011
000
298 || croak("'$cfg{rx}' is no valid regex");
299 }
300 3 return %cfg;
301 }
302  
303  
304 5 1;
305  
306 __END__
« IMP/Example/IRCShout.pm ↑ Summary IMP/Example/LogServerCertificate.pm »