lib/Rex/Interface/Exec/Base.pm

filter: Statementstmt 69.0% 58 / 84 Branchbran 40.0% 8 / 20 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 6 totaltotal 54.9% 84 / 153 CC 20 SCAR 16.8
CC
20
Cov
59%
direct_exec
10.8
_end_if_matched
7.5
can_run
6.2
CRAP 5.4
« lib/Rex/Commands/Gather.pm ↑ Summary blib/lib/Rex/Interface/Fs/OpenSSH.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Exec::Base;
6  
7 60 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 60 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 60 ▶use Rex::Interface::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 60 our $VERSION = '1.16.1'; # VERSION
15  
16 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my $that = shift;
18 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
19 0 my $self = {@_};
20  
21 0 bless( $self, $proto );
22  
23 0 return $self;
24 }
25  
26 0 ▶sub exec { die("Must be implemented by Interface Class"); }
Subroutine exec: not called CC=1 SCAR=6.9
Pod undocumented
27  
28 5231 ▶sub _continuous_read {
Subroutine _continuous_read: called CC=4 SCAR=17.1
29 5231 my ( $self, $line, $option ) = @_;
30 5231 ▶ my $cb = $option->{continuous_read} || undef;
Condition50%1/2
$option->{'continuous_read'} || undef
l!l
05231
MC/DC0%0/1
$option->{'continuous_read'} || undef
$option->{'continuous_read'}
Truth table50%1/2
A
A= $option->{'continuous_read'}
Aresult
11
00
31  
32 5231 ▶ if ( defined($cb) && ref($cb) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb and ref $cb eq "CODE") 0 5231 5231
Condition33%1/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
523100
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table33%1/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
33 0 &$cb($line);
34 }
35 }
36  
37 5231 ▶sub _end_if_matched {
Subroutine _end_if_matched: called CC=5 SCAR=20.2
38 5231 my ( $self, $line, $option ) = @_;
39 5231 ▶ my $regex = $option->{end_if_matched} || undef;
Condition50%1/2
$option->{'end_if_matched'} || undef
l!l
05231
MC/DC0%0/1
$option->{'end_if_matched'} || undef
$option->{'end_if_matched'}
Truth table50%1/2
A
A= $option->{'end_if_matched'}
Aresult
11
00
40  
41 5231 ▶ if ( defined($regex) && ref($regex) eq 'Regexp' && $line =~ m/$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex and ref $regex eq "Regexp" and $line =~ /$regex/u) 0 5231 5231
Condition33%2/6
defined $regex and ref $regex eq "Regexp" and $line =~ /$regex/u
!ll&&!rl&&r
523100
defined $regex and ref $regex eq "Regexp"
!ll&&!rl&&r
523100
MC/DC0%0/3
defined $regex and ref $regex eq "Regexp" and $line =~ /$regex/u
defined $regex ref $regex eq "Regexp" $line =~ /$regex/u
Truth table25%1/4
A and B and C
A= defined $regexB= ref $regex eq "Regexp"C= $line =~ /$regex/u
ABCresult
0XX0
10X0
1100
1111
42 0 return 1;
43 }
44 5231 return;
45 }
46  
47 5231 ▶sub execute_line_based_operation {
Subroutine execute_line_based_operation: called CC=1 SCAR=0.0
Pod undocumented
48 5231 my ( $self, $line, $option ) = @_;
49  
50 5231 $self->_continuous_read( $line, $option );
51 5231 return $self->_end_if_matched( $line, $option );
52 }
53  
54 556 ▶sub can_run {
Subroutine can_run: called CC=6 SCAR=18.2
Pod undocumented
55 556 my ( $self, $commands_to_check, $check_with_command ) = @_;
56  
57 556 ▶ $check_with_command ||= "command -v";
Condition50%1/2
$check_with_command ||= "command -v"
l!l
5560
MC/DC0%0/1
$check_with_command ||= "command -v"
$check_with_command
Truth table50%1/2
A
A= $check_with_command
Aresult
11
00
58  
59 556 my $exec = Rex::Interface::Exec->create;
60 556 my $cache = Rex::get_cache();
61  
62 556 for my $command ( @{$commands_to_check} ) {
63  
64 557 my $cache_key_name = $cache->gen_key_name("can_run.cmd/$command");
65 557 ▶ if ( $cache->valid($cache_key_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid($cache_key_name)) 0 557 557
66 0 return $cache->get($cache_key_name);
67 }
68  
69 557 my @output = Rex::Helper::Run::i_run "$check_with_command $command",
70 fail_ok => 1;
71  
72 557 ▶ next if ( $? != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 364 193 557
73  
74 193 ▶ next if ( !is_file( $output[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless is_file($output[0]) 0 193 193
75  
76 193 $cache->set( $cache_key_name, $output[0] );
77  
78 193 return $output[0];
79 }
80  
81 363 return undef;
82 }
83  
84 2 ▶sub direct_exec {
Subroutine direct_exec: called CC=4 SCAR=23.7
Pod undocumented
85 2 my ( $self, $exec, $option ) = @_;
86  
87 2 Rex::Commands::profiler()->start("direct_exec: $exec");
88  
89 2 my $class_name = ref $self;
90  
91 2 Rex::Logger::debug("$class_name/executing: $exec");
92 2 my ( $out, $err ) = $self->_exec( $exec, $option );
93  
94 0 Rex::Commands::profiler()->end("direct_exec: $exec");
95  
96 0 ▶ Rex::Logger::debug($out) if ($out);
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 0 0
97 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
98 0 Rex::Logger::debug("========= ERR ============");
99 0 Rex::Logger::debug($err);
100 0 Rex::Logger::debug("========= ERR ============");
101 }
102  
103 0 ▶ if (wantarray) { return ( $out, $err ); }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
104  
105 0 return $out;
106 }
107  
108 0 ▶sub _exec {
Subroutine _exec: not called CC=1 SCAR=6.9
109 0 my ($self) = @_;
110 0 my $class_name = ref $self;
111 0 die("_exec method must be overwritten by class ($class_name).");
112 }
113  
114 2 ▶sub shell {
Subroutine shell: called CC=4 SCAR=15.7
Pod undocumented
115 2 my ($self) = @_;
116  
117 2 Rex::Logger::debug("Detecting shell...");
118  
119 2 my $cache = Rex::get_cache();
120 2 ▶ if ( $cache->valid("shell") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid("shell")) 0 2 2
121 0 Rex::Logger::debug( "Found shell in cache: " . $cache->get("shell") );
122 0 return Rex::Interface::Shell->create( $cache->get("shell") );
123 }
124  
125 2 my %shells = Rex::Interface::Shell->get_shell_provider;
126 2 for my $shell ( keys %shells ) {
127 16 Rex::Logger::debug( "Searching for shell: " . $shell );
128 16 $shells{$shell}->require;
129 16 ▶ if ( $shells{$shell}->detect($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shells{$shell}->detect($self)) 0 16 16
130 0 Rex::Logger::debug( "Found shell and using: " . $shell );
131 0 $cache->set( "shell", $shell );
132 0 return Rex::Interface::Shell->create($shell);
133 }
134 }
135  
136 2 return Rex::Interface::Shell->create("sh");
137 }
138  
139 60 1;
« lib/Rex/Commands/Gather.pm ↑ Summary blib/lib/Rex/Interface/Fs/OpenSSH.pm »