Match/WChan.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 24 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 92 / 92 CC 15 SCAR 20.1
CC
15
Cov
100%
match
11.0
new
5.0
BEGIN
1.0
CRAP 7.5
« Match/Command.pm ↑ Summary Match/CIDR.pm »
1 package Net::Connection::Match::WChan;
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 Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Connection::Match::WChan - Check if the process wait channel matches fix regexp for a Net::Connection object.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 2 our $VERSION = '0.0.0';
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Match::WChan;
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=>'ESTABLISHED',
32 pid=>0,
33 wchan=>'sigwait',
34 };
35
36 my $conn=Net::Connection->new( $connection_args );
37
38 my %args=(
39 wchanss=>[
40 'sleep',
41 'sigwait',
42 ],
43 );
44
45 my $checker=Net::Connection::Match::WChan->new( \%args );
46
47 if ( $checker->match( $conn ) ){
48 print "It matches.\n";
49 }
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 This intiates the object.
56  
57 The key in the hash ref commands is a array
58 that holds regular expressions to match against
59 the wait channel.
60  
61 If the new method fails, it dies.
62  
63 my %args=(
64 wchans=>[
65 'kernel',
66 ],
67 );
68
69 my $checker=Net::Connection::Match::WChan->new( \%args );
70  
71 =cut
72  
73 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
74 4 my %args;
75 4 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 3 1 4
76 3 %args = %{ $_[1] };
77 }
78  
79 # run some basic checks to make sure we have the minimum stuff required to work
80 4 ▶ if ( !defined( $args{wchans} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"wchans"}) 1 3 4
81 1 die('No wchans key specified in the argument hash');
82 }
83 3 ▶ if ( ref( $args{wchans} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"wchans"} ne "ARRAY") 1 2 3
84 1 die('The wchans key is not a array');
85 }
86 2 ▶ if ( !defined $args{wchans}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"wchans"}[0]) 1 1 2
87 1 die('Nothing defined in the commands array');
88 }
89  
90 1 my $self = { wchans => $args{wchans}, };
91 1 bless $self;
92  
93 1 return $self;
94 } ## end sub new
95  
96 =head2 match
97  
98 Checks if a single Net::Connection object matches the stack.
99  
100 One argument is taken and that is a Net::Connection object.
101  
102 The returned value is a boolean.
103  
104 if ( $checker->match( $conn ) ){
105 print "The connection matches.\n";
106 }
107  
108 =cut
109  
110 10 ▶sub match {
Subroutine match: called CC=11 SCAR=24.0
Pod documented
111 10 my $self = $_[0];
112 10 my $object = $_[1];
113  
114 10 ▶ if ( !defined($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 9 10
115 1 return 0;
116 }
117  
118 9 ▶ if ( ref($object) ne 'Net::Connection' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Net::Connection") 1 8 9
119 1 return 0;
120 }
121  
122 8 my $conn_pid = $object->pid;
123  
124 # don't bother proceeding, the object won't match ever
125 # as it does not have a PID
126 8 ▶ if ( !defined($conn_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conn_pid) 1 7 8
127 1 return 0;
128 }
129  
130 7 my $loop = 0;
131 7 my $wchan;
132 7 ▶ if ( !defined( $object->proc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object->proc) { } 2 5 7
133 # go through each proc and look for a matching pid
134 2 my $proctable = Proc::ProcessTable->new;
135 2 my $procs = $proctable->table;
136 2 my $proc_int = 0;
137 2 my $loop = 1;
138 2 ▶ while ( $loop
Branch: 1 branch
BranchTrueFalseTotal
if ($loop and defined $procs->[$proc_int]) 2 2 4
Condition100%3/3
$loop and defined $procs->[$proc_int]
!ll&&!rl&&r
112
MC/DC100%2/2
$loop and defined $procs->[$proc_int]
$loop defined $procs->[$proc_int]
Truth table100%3/3
A and B
A= $loopB= defined $procs->[$proc_int]
ABresult
0X0
100
111
139 && defined( $procs->[$proc_int] ) )
140 {
141  
142 2 ▶ if ( $conn_pid eq $procs->[$proc_int]->{pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_pid eq $procs->[$proc_int]{"pid"}) 1 1 2
143 1 $wchan = $procs->[$proc_int]->wchan;
144  
145 # exit the loop as we found it
146 1 $loop = 0;
147 }
148  
149 2 $proc_int++;
150 } ## end while ( $loop && defined( $procs->[$proc_int]...))
151 } else {
152 5 $wchan = $object->wchan;
153 }
154  
155 # likely a dead connection that is handing around...
156 # or disappeared since grabbing the connection list
157 # and starting processing
158 7 ▶ if ( !defined($wchan) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wchan) 2 5 7
159 2 return 0;
160 }
161  
162 # check each command regex and see if any of them match
163 5 foreach my $regex ( @{ $self->{wchans} } ) {
164 8 ▶ if ( $wchan =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wchan =~ /$regex/) 3 5 8
165 3 return 1;
166 }
167 }
168  
169 2 return 0;
170 } ## end sub match
171  
172 =head1 AUTHOR
173  
174 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
175  
176 =head1 BUGS
177  
178 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
179 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
180 automatically be notified of progress on your bug as I make changes.
181  
182  
183  
184  
185 =head1 SUPPORT
186  
187 You can find documentation for this module with the perldoc command.
188  
189 perldoc Net::Connection::Match
190  
191  
192 You can also look for information at:
193  
194 =over 4
195  
196 =item * RT: CPAN's request tracker (report bugs here)
197  
198 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
199  
200 =item * AnnoCPAN: Annotated CPAN documentation
201  
202 L<http://annocpan.org/dist/Net-Connection-Match>
203  
204 =item * CPAN Ratings
205  
206 L<https://cpanratings.perl.org/d/Net-Connection-Match>
207  
208 =item * Search CPAN
209  
210 L<https://metacpan.org/release/Net-Connection-Match>
211  
212 =back
213  
214  
215 =head1 ACKNOWLEDGEMENTS
216  
217  
218 =head1 LICENSE AND COPYRIGHT
219  
220 Copyright 2019 Zane C. Bowers-Hadley.
221  
222 This program is free software; you can redistribute it and/or modify it
223 under the terms of the the Artistic License (2.0). You may obtain a
224 copy of the full license at:
225  
226 L<http://www.perlfoundation.org/artistic_license_2_0>
227  
228 Any use, modification, and distribution of the Standard or Modified
229 Versions is governed by this Artistic License. By using, modifying or
230 distributing the Package, you accept this license. Do not use, modify,
231 or distribute the Package, if you do not accept this license.
232  
233 If your Modified Version has been derived from a Modified Version made
234 by someone other than you, you are nevertheless required to ensure that
235 your Modified Version complies with the requirements of this license.
236  
237 This license does not grant you the right to use any trademark, service
238 mark, tradename, or logo of the Copyright Holder.
239  
240 This license includes the non-exclusive, worldwide, free-of-charge
241 patent license to make, have made, use, offer to sell, sell, import and
242 otherwise transfer the Package with respect to any patent claims
243 licensable by the Copyright Holder that are necessarily infringed by the
244 Package. If you institute patent litigation (including a cross-claim or
245 counterclaim) against any party alleging that the Package constitutes
246 direct or contributory patent infringement, then this Artistic License
247 to you shall terminate on the date that such litigation is filed.
248  
249 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
250 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
251 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
252 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
253 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
254 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
255 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
256 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
257  
258  
259 =cut
260  
261 2 1; # End of Net::Connection::Match
« Match/Command.pm ↑ Summary Match/CIDR.pm »