Match.pm

filter: Statementstmt 81.5% 75 / 92 Branchbran 60.8% 28 / 46 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 71.6% 119 / 166 CC 29 SCAR 30.0
CC
29
Cov
73%
new
27.6
match
15.3
BEGIN
1.0
CRAP 20.1
« Match/PTR.pm ↑ Summary Match/PID.pm »
1 package Net::Connection::Match;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Connection::Match - Runs a stack of checks to match Net::Connection objects.
11  
12 =head1 VERSION
13  
14 Version 0.5.2
15  
16 =cut
17  
18 2 our $VERSION = '0.5.2';
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Match;
23 use Net::Connection;
24
25 my $connection_args={
26 foreign_host=>'10.0.0.1',
27 foreign_port=>'22',
28 local_host=>'10.0.0.2',
29 local_port=>'12322',
30 proto=>'tcp4',
31 state=>'LISTEN',
32 };
33 my $conn=Net::Connection->new( $connection_args );
34
35 my %args=(
36 checks=>[
37 {
38 type=>'Ports',
39 invert=>0,
40 args=>{
41 ports=>[
42 '22',
43 ],
44 lports=>[
45 '53',
46 ],
47 fports=>[
48 '12345',
49 ],
50 }
51 },
52 {
53 type=>'Protos',
54 invert=>0,
55 args=>{
56 protos=>[
57 'tcp4',
58 ],
59 }
60 }
61 ]
62 );
63
64 my $checker;
65 eval{
66 $checker=Net::Connection::Match->new( \%args );
67 } or die "New failed with...".$@;
68
69 if ( $check->match( $conn ) ){
70 print "It matched!\n";
71 }
72  
73 =head1 METHODS
74  
75 =head2 new
76  
77 This initializes a new check object.
78  
79 It takes one value and thht is a hash ref with the key checks.
80 This is a array of hashes. If the array is empty, it will default
81 to using the All test.
82  
83 If new fails, it will die.
84  
85 =head3 checks hash keys
86  
87 =head4 type
88  
89 This is the name of the check relative to 'Net::Connection::Match::'.
90  
91 So 'Net::Connection::Match::PTR' would become 'PTR'.
92  
93 =head4 args
94  
95 This is a hash or args to pash to the check. These are passed to the new
96 method of the check module.
97  
98 =head4 invert
99  
100 This is either boolean on if the check should be inverted or not.
101  
102 my $mce;
103 eval{
104 $ncm=Net::Connection::Match->new( $args );
105 };
106  
107 =cut
108  
109 3 ▶sub new {
Subroutine new: called CC=18 SCAR=33.2
Pod documented
110 3 my %args;
111 3 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 2 1 3
112 2 %args = %{ $_[1] };
113 }
114  
115 # Provides some basic checks.
116 # Could make these all one if, but this provides more
117 # granularity for some one using it.
118 3 ▶ if ( !defined( $args{checks} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"checks"}) 1 2 3
119 1 die('No check key specified in the argument hash');
120 }
121 2 ▶ if ( ref( $args{checks} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"checks"} ne "ARRAY") 0 2 2
122 0 die('The checks key is not a array');
123 }
124 # Will never match anything.
125 2 ▶ if ( !defined $args{checks}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"checks"}[0]) 0 2 2
126 0 $args{checks}[0] = {
127 type => 'All',
128 invert => 0,
129 args => {}
130 };
131  
132 }
133 2 ▶ if ( ref( $args{checks}[0] ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"checks"}[0] ne "HASH") 0 2 2
134 0 die('The first item in the checks array is not a hash');
135 }
136  
137 2 my $self = {
138 perror => undef,
139 error => undef,
140 errorString => "",
141 testing => 0,
142 errorExtra => {
143 flags => {
144 1 => 'failedCheckInit',
145 2 => 'notNCobj',
146 3 => 'checkErrored',
147 }
148 },
149 checks => [],
150 };
151 2 bless $self;
152  
153 # Loads up each check or dies if it fails to.
154 2 my $check_int = 0;
155 2 ▶ while ( defined( $args{checks}[$check_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"checks"}[$check_int]) 3 2 5
156 3 my %new_check = (
157 type => undef,
158 args => undef,
159 invert => undef,
160 );
161  
162 # make sure we have a check type
163 3 ▶ if ( defined( $args{checks}[$check_int]{'type'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'type'}) { } 3 0 3
164 3 $new_check{type} = $args{checks}[$check_int]{'type'};
165 } else {
166 0 die( 'No type defined for check ' . $check_int );
167 }
168  
169 # does a quick check on the tpye name
170 3 my $type_test = $new_check{type};
171 3 $type_test =~ s/[A-Za-z0-9]//g;
172 3 $type_test =~ s/\:\://g;
173 3 ▶ if ( $type_test !~ /^$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type_test =~ /^$/) 0 3 3
174 0 die 'The type "' . $new_check{type} . '" for check ' . $check_int . ' is not a valid check name';
175 }
176  
177 # makes sure we have a args object and that it is a hash
178 3 ▶ if ( ( defined( $args{checks}[$check_int]{'args'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'args'} and ref $args{'checks'}[$check_int]{'args'} eq 'HASH') { } 3 0 3
Condition33%1/3
defined $args{"checks"}[$check_int]{"args"} and ref $args{"checks"}[$check_int]{"args"} eq "HASH"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $args{"checks"}[$check_int]{"args"} and ref $args{"checks"}[$check_int]{"args"} eq "HASH"
defined $args{"checks"}[$check_int]{"args"} ref $args{"checks"}[$check_int]{"args"} eq "HASH"
Truth table33%1/3
A and B
A= defined $args{"checks"}[$check_int]{"args"}B= ref $args{"checks"}[$check_int]{"args"} eq "HASH"
ABresult
0X0
100
111
179 && ( ref( $args{checks}[$check_int]{'args'} ) eq 'HASH' ) )
180 {
181 3 $new_check{args} = $args{checks}[$check_int]{'args'};
182 } else {
183 0 die( 'No args defined for check ' . $check_int . ' or it is not a HASH' );
184 }
185  
186 # makes sure we have a args object and that it is a hash
187 3 ▶ if ( ( defined( $args{checks}[$check_int]{'invert'} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'invert'} and ref \$args{'checks'}[$check_int]{'invert'} ne 'SCALAR') { } 0 3 3
elsif (defined $args{'checks'}[$check_int]{'invert'} and ref \$args{'checks'}[$check_int]{'invert'} eq 'SCALAR') { } 3 0 3
Condition33%1/3
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
!ll&&!rl&&r
030
MC/DC0%0/2
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
defined $args{"checks"}[$check_int]{"invert"} ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
Truth table33%1/3
A and B
A= defined $args{"checks"}[$check_int]{"invert"}B= ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
ABresult
0X0
100
111
188 && ( ref( \$args{checks}[$check_int]{'invert'} ) ne 'SCALAR' ) )
189 {
190 0 die( 'Invert defined for check ' . $check_int . ' but it is not a SCALAR' );
191 ▶ } elsif ( ( defined( $args{checks}[$check_int]{'invert'} ) )
Condition33%1/3
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
defined $args{"checks"}[$check_int]{"invert"} ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
Truth table33%1/3
A and B
A= defined $args{"checks"}[$check_int]{"invert"}B= ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
ABresult
0X0
100
111
192 && ( ref( \$args{checks}[$check_int]{'invert'} ) eq 'SCALAR' ) )
193 {
194 3 $new_check{invert} = $args{checks}[$check_int]{'invert'};
195 }
196  
197 3 my $check;
198 3 my $check_module = 'Net::Connection::Match::' . $new_check{type};
199 3 eval( 'require ' . $check_module . ';' );
200 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
201 0 die 'Failed to load the module "' . $check_module . '" for check ' . $check_int . '... ' . $@;
202 }
203 3 eval { $check = $check_module->new( $new_check{args} ); };
204  
205 3 ▶ if ( !defined($check) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $check) 0 3 3
206 0 die 'Failed to init the check for ' . $check_int . ' as it returned undef... ' . $@;
207 }
208  
209 3 $new_check{check} = $check;
210  
211 3 push( @{ $self->{checks} }, \%new_check );
212  
213 3 $check_int++;
214 } ## end while ( defined( $args{checks}[$check_int] ) )
215  
216 2 ▶ if ( $args{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"testing"}) 2 0 2
217 2 $self->{testing} = 1;
218 }
219  
220 2 return $self;
221 } ## end sub new
222  
223 =head2 match
224  
225 Checks if a single Net::Connection object matches the stack.
226  
227 One object is argument is taken and that is the Net::Connection to check.
228  
229 The return value is a boolean.
230  
231 if ( $ncm->match( $conn ) ){
232 print "It matched.\n";
233 }
234  
235 =cut
236  
237 5 ▶sub match {
Subroutine match: called CC=12 SCAR=27.3
Pod documented
238 5 my $self = $_[0];
239 5 my $conn = $_[1];
240  
241 5 ▶ if ( !$self->errorblank ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 5 5
242 0 return undef;
243 }
244  
245 5 ▶ if ( ( !defined($conn) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $conn or ref $conn ne "Net::Connection") 2 3 5
Condition100%3/3
not defined $conn or ref $conn ne "Net::Connection"
l!l&&r!l&&!r
113
MC/DC100%2/2
not defined $conn or ref $conn ne "Net::Connection"
not defined $conn ref $conn ne "Net::Connection"
Truth table100%3/3
not(A) or B
A= not defined $connB= ref $conn ne "Net::Connection"
ABresult
1X1
011
000
246 || ( ref($conn) ne 'Net::Connection' ) )
247 {
248 2 $self->{error} = 2;
249 2 $self->{errorString} = 'Either the connection is undefined or is not a Net::Connection object';
250 2 ▶ if ( !$self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"testing"}) 0 2 2
251 0 $self->warn;
252 }
253 2 return undef;
254 } ## end if ( ( !defined($conn) ) || ( ref($conn) ne...))
255  
256 # Stores the number of hits
257 3 my $hits = 0;
258 3 my $required = 0;
259 3 foreach my $check ( @{ $self->{checks} } ) {
260 4 my $hit;
261 4 eval { $hit = $check->{check}->match($conn); };
262  
263 # surface the error instead of silently returning a non-match
264 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
265 0 $self->{error} = 3;
266 0 $self->{errorString} = 'The check "' . $check->{type} . '" died... ' . $@;
267 0 ▶ if ( !$self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"testing"}) 0 0 0
268 0 $self->warn;
269 }
270 0 return undef;
271 }
272  
273 # If $hits is undef, then one of the checks errored and we skip processing the results.
274 # Should only be 0 or 1.
275 4 ▶ if ( defined($hit) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit) 4 0 4
276 # invert if needed
277 4 ▶ if ( $check->{invert} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->{"invert"}) 0 4 4
278 0 $hit = $hit ^ 1;
279 }
280  
281 # increment the hits count if we hit
282 4 ▶ if ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit) 3 1 4
283 3 $hits++;
284 }
285 } ## end if ( defined($hit) )
286  
287 4 $required++;
288 } ## end foreach my $check ( @{ $self->{checks} } )
289  
290 # if these are the same, then we have a match
291 3 ▶ if ( $required == $hits ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required == $hits) 2 1 3
292 2 return 1;
293 }
294  
295 # If we get here, it is not a match
296 1 return 0;
297 } ## end sub match
298  
299 =head1 ERROR HANDLING / FLAGS
300  
301 Error handling is provided by L<Error::Helper>.
302  
303 =head2 2 / notNCobj
304  
305 Not a Net::Connection object. Either is is not defined
306 or what is being passed is not a Net::Connection object.
307  
308 =head2 3 / checkErrored
309  
310 One of the checks died while running match.
311  
312 =head1 AUTHOR
313  
314 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
315  
316 =head1 BUGS
317  
318 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
319 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
320 automatically be notified of progress on your bug as I make changes.
321  
322  
323  
324  
325 =head1 SUPPORT
326  
327 You can find documentation for this module with the perldoc command.
328  
329 perldoc Net::Connection::Match
330  
331  
332 You can also look for information at:
333  
334 =over 4
335  
336 =item * RT: CPAN's request tracker (report bugs here)
337  
338 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
339  
340 =item * AnnoCPAN: Annotated CPAN documentation
341  
342 L<http://annocpan.org/dist/Net-Connection-Match>
343  
344 =item * CPAN Ratings
345  
346 L<https://cpanratings.perl.org/d/Net-Connection-Match>
347  
348 =item * Search CPAN
349  
350 L<https://metacpan.org/release/Net-Connection-Match>
351  
352 =item * Git Repo
353  
354 L<https://gitea.eesdp.org/vvelox/Net-Connection-Match>
355  
356 =back
357  
358  
359 =head1 ACKNOWLEDGEMENTS
360  
361  
362 =head1 LICENSE AND COPYRIGHT
363  
364 Copyright 2019 Zane C. Bowers-Hadley.
365  
366 This program is free software; you can redistribute it and/or modify it
367 under the terms of the the Artistic License (2.0). You may obtain a
368 copy of the full license at:
369  
370 L<http://www.perlfoundation.org/artistic_license_2_0>
371  
372 Any use, modification, and distribution of the Standard or Modified
373 Versions is governed by this Artistic License. By using, modifying or
374 distributing the Package, you accept this license. Do not use, modify,
375 or distribute the Package, if you do not accept this license.
376  
377 If your Modified Version has been derived from a Modified Version made
378 by someone other than you, you are nevertheless required to ensure that
379 your Modified Version complies with the requirements of this license.
380  
381 This license does not grant you the right to use any trademark, service
382 mark, tradename, or logo of the Copyright Holder.
383  
384 This license includes the non-exclusive, worldwide, free-of-charge
385 patent license to make, have made, use, offer to sell, sell, import and
386 otherwise transfer the Package with respect to any patent claims
387 licensable by the Copyright Holder that are necessarily infringed by the
388 Package. If you institute patent litigation (including a cross-claim or
389 counterclaim) against any party alleging that the Package constitutes
390 direct or contributory patent infringement, then this Artistic License
391 to you shall terminate on the date that such litigation is filed.
392  
393 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
394 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
395 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
396 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
397 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
398 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
399 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
400 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
401  
402  
403 =cut
404  
405 2 1; # End of Net::Connection::Match
« Match/PTR.pm ↑ Summary Match/PID.pm »