Net/Prober/Probe/Base.pm

filter: Statementstmt 88.1% 67 / 76 Branchbran 62.5% 15 / 24 Conditioncond 38.1% 8 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 10 totaltotal 65.4% 104 / 159 CC 23 SCAR 14.2
CC
23
Cov
74%
process_defaults
6.3
probe_failed
6.1
parse_args
5.2
CRAP 4.1
« Net/Prober/ssh.pm ↑ Summary Net/Prober/Probe/TCP.pm »
1 package Net::Prober::Probe::Base;
2 8 $Net::Prober::Probe::Base::VERSION = '0.16';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Sys::Syslog ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $USE_SYSLOG = 0;
11  
12 sub new {
13 8 ▶ my ($class, $opt) = @_;
Subroutine new: called CC=3 SCAR=11.6
Pod undocumented
14  
15 8 ▶ $opt ||= {};
Condition50%1/2
$opt ||= {}
l!l
08
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
Aresult
11
00
16 8 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
17  
18 my $self = {
19 8 %{ $opt },
20 };
21  
22 8 bless $self, $class;
23 }
24  
25 sub name {
26 6 ▶ my ($self) = @_;
Subroutine name: called CC=3 SCAR=12.7
Pod undocumented
27 6 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
28 6 ▶ if ($class =~ m{^ .* :: (?<probename>.*?) $}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^ .* :: (?<probename>.*?) $/x) 6 0 6
29 6 return $+{probename};
30 }
31  
32 0 Carp::croak("Couldn't determine the probe name from class $class");
33 }
34  
35 sub probe_failed {
36 3 ▶ my ($self, %info) = @_;
Subroutine probe_failed: called CC=5 SCAR=18.1
Pod undocumented
37  
38 3 my $name = $self->name();
39  
40 3 ▶ if (! exists $info{time}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $info{"time"}) 2 1 3
41 my $elapsed = exists $self->{_time}
42 2 ▶ ? $self->time_elapsed()
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_time'} ? : 2 0 2
43 : 0.0;
44 2 $info{time} = $elapsed;
45 }
46  
47 3 ▶ if ($USE_SYSLOG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Prober::Probe::Base::USE_SYSLOG) 0 3 3
48 my $msg = sprintf "Probe %s failed, reason %s, elapsed: %3.2f s",
49 $name,
50 $info{reason} || 'unknown',
51 0 ▶ $info{time};
Condition0%0/2
$info{'reason'} || 'unknown'
l!l
00
MC/DC0%0/1
$info{'reason'} || 'unknown'
$info{'reason'}
Truth table0%0/2
A
A= $info{'reason'}
Aresult
11
00
52  
53 0 Sys::Syslog::syslog('warning', $msg);
54 }
55  
56 3 return { ok => 0, %info };
57 }
58  
59 sub probe_ok {
60 3 ▶ my ($self, %info) = @_;
Subroutine probe_ok: called CC=4 SCAR=15.2
Pod undocumented
61  
62 3 my $name = $self->name();
63  
64 3 ▶ if (! exists $info{time}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $info{"time"}) 3 0 3
65 my $elapsed = exists $self->{_time}
66 3 ▶ ? $self->time_elapsed()
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_time'} ? : 3 0 3
67 : 0.0;
68 3 $info{time} = $elapsed;
69 }
70  
71 3 ▶ if ($USE_SYSLOG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Prober::Probe::Base::USE_SYSLOG) 0 3 3
72 my $msg = sprintf "Probe %s ok, elapsed: %3.2f s",
73 0 $name, $info{time};
74 0 Sys::Syslog::syslog('info', $msg);
75 }
76  
77 3 return { ok => 1, %info };
78 }
79  
80 sub time_now {
81 6 ▶ my ($self) = @_;
Subroutine time_now: called CC=1 SCAR=0.0
Pod undocumented
82  
83 6 return $self->{_time} = [ Time::HiRes::gettimeofday() ];
84 }
85  
86 sub time_elapsed {
87 9 ▶ my ($self) = @_;
Subroutine time_elapsed: called CC=3 SCAR=12.3
Pod undocumented
88  
89 9 ▶ if (! exists $self->{_time} || ! defined $self->{_time}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_time"} and defined $self->{"_time"}) 0 9 9
Condition33%1/3
exists $self->{"_time"} and defined $self->{"_time"}
!ll&&!rl&&r
009
MC/DC0%0/2
exists $self->{"_time"} and defined $self->{"_time"}
exists $self->{"_time"} defined $self->{"_time"}
Truth table33%1/3
A and B
A= exists $self->{"_time"}B= defined $self->{"_time"}
ABresult
0X0
100
111
90 0 Carp::croak('time_elapsed() called without a starting time_now()');
91 }
92  
93 9 my $last_mark = $self->{_time};
94 9 my $elapsed = Time::HiRes::tv_interval($last_mark);
95  
96 9 return $elapsed;
97 }
98  
99 sub process_defaults {
100 14 ▶ my ($self, $args) = @_;
Subroutine process_defaults: called CC=6 SCAR=18.5
Pod undocumented
101  
102 14 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
140
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
103 14 my %args_with_defaults = %{ $args };
104  
105 # Process and inject defaults if an arg is not supplied
106 14 my $defaults = $self->defaults;
107  
108 14 ▶ if ($defaults && ref $defaults eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults and ref $defaults eq "HASH") 14 0 14
Condition33%1/3
$defaults and ref $defaults eq "HASH"
!ll&&!rl&&r
0014
MC/DC0%0/2
$defaults and ref $defaults eq "HASH"
$defaults ref $defaults eq "HASH"
Truth table33%1/3
A and B
A= $defaultsB= ref $defaults eq "HASH"
ABresult
0X0
100
111
109 14 for (keys %{ $defaults }) {
110 $args_with_defaults{$_} = $defaults->{$_}
111 98 ▶ if ! exists $args_with_defaults{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args_with_defaults{$_} 58 40 98
112 }
113 }
114  
115 14 return \%args_with_defaults;
116 }
117  
118 sub parse_args {
119 14 ▶ my ($self, $args, @wanted) = @_;
Subroutine parse_args: called CC=5 SCAR=16.4
Pod undocumented
120  
121 14 my $args_with_def = $self->process_defaults($args);
122  
123 14 ▶ if (exists $args_with_def->{port} && defined $args_with_def->{port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args_with_def->{"port"} and defined $args_with_def->{"port"}) 12 2 14
Condition66%2/3
exists $args_with_def->{"port"} and defined $args_with_def->{"port"}
!ll&&!rl&&r
0212
MC/DC50%1/2
exists $args_with_def->{"port"} and defined $args_with_def->{"port"}
exists $args_with_def->{"port"} defined $args_with_def->{"port"}
Truth table66%2/3
A and B
A= exists $args_with_def->{"port"}B= defined $args_with_def->{"port"}
ABresult
0X0
100
111
124 $args_with_def->{port} = Net::Prober::port_name_to_num(
125 $args_with_def->{port}
126 12 );
127 }
128  
129 14 ▶ if (! @wanted) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@wanted) 0 14 14
130 0 return $args_with_def;
131 }
132  
133 14 my @arg_values;
134 14 push @arg_values, $args_with_def->{$_} for @wanted;
135  
136 14 return @arg_values;
137 }
138  
139 sub defaults {
140 0 ▶ Carp::croak("defaults() is an abstract method. You must implement it in your class.");
Subroutine defaults: not called CC=1 SCAR=6.9
Pod undocumented
141 }
142  
143 sub probe {
144 0 ▶ Carp::croak("probe() is an abstract method. You must implement it in your class.");
Subroutine probe: not called CC=1 SCAR=6.9
Pod undocumented
145 }
146  
147 8 1;
148  
149 __END__
« Net/Prober/ssh.pm ↑ Summary Net/Prober/Probe/TCP.pm »