blib/lib/Net/Appliance/Session/Engine.pm

filter: Statementstmt 17.5% 14 / 80 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 3 / 9 Podpod 0.0% 0 / 6 totaltotal 8.9% 17 / 189 SCAR 72.4
CC
42
Cov
8%
begin_privileged
182.0
end_configure
90.0
enable_paging
56.0
CRAP 1396.3
« blib/lib/Net/Appliance/Session/Scripting.pm ↑ Summary blib/lib/Net/Appliance/Session/Transport.pm »
1 package Net::Appliance::Session::Engine;
2 1 { $Net::Appliance::Session::Engine::VERSION = '4.300003' }
3  
4 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooX::Types::MooseLike::Base qw(Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has 'pager_enable_lines' => (
9 is => 'rw',
10 isa => Int,
11 required => 0,
12 default => quote_sub('24'),
13 );
14  
15 1 has 'pager_disable_lines' => (
16 is => 'rw',
17 isa => Int,
18 required => 0,
19 default => quote_sub('0'),
20 );
21  
22 sub enable_paging {
23 0 ▶ my $self = shift;
Subroutine enable_paging: not called CC=7 SCAR=40.3
Pod undocumented
24  
25 0 ▶ return unless $self->do_paging;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_paging 0 0 0
26 0 ▶ return unless $self->logged_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
27  
28 0 my $privstate = $self->in_privileged_mode;
29 0 ▶ $self->begin_privileged if $self->privileged_paging;
Branch: 1 branch
BranchTrueFalseTotal
if $self->privileged_paging 0 0 0
30  
31 0 ▶ my $pagercmd = ($self->nci->phrasebook->has_macro('enable_paging')
Branch: 1 branch
BranchTrueFalseTotal
$self->nci->phrasebook->has_macro('enable_paging') ? : 0 0 0
32 ? 'enable_paging' : 'paging');
33  
34 0 $self->macro($pagercmd, { params => [
35 $self->pager_enable_lines
36 ]} );
37  
38 0 ▶ $self->end_privileged
Branch: 1 branch
BranchTrueFalseTotal
if $self->privileged_paging and not $privstate 0 0 0
Condition0%0/3
$self->privileged_paging and not $privstate
!ll&&!rl&&r
000
MC/DC0%0/2
$self->privileged_paging and not $privstate
$self->privileged_paging not $privstate
Truth table0%0/3
A and not(B)
A= $self->privileged_pagingB= not $privstate
ABresult
0X0
100
111
39 if $self->privileged_paging and not $privstate;
40 }
41  
42 sub disable_paging {
43 0 ▶ my $self = shift;
Subroutine disable_paging: not called CC=7 SCAR=40.3
Pod undocumented
44  
45 0 ▶ return unless $self->do_paging;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_paging 0 0 0
46 0 ▶ return unless $self->logged_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
47  
48 0 my $privstate = $self->in_privileged_mode;
49 0 ▶ $self->begin_privileged if $self->privileged_paging;
Branch: 1 branch
BranchTrueFalseTotal
if $self->privileged_paging 0 0 0
50  
51 0 ▶ my $pagercmd = ($self->nci->phrasebook->has_macro('disable_paging')
Branch: 1 branch
BranchTrueFalseTotal
$self->nci->phrasebook->has_macro('disable_paging') ? : 0 0 0
52 ? 'disable_paging' : 'paging');
53  
54 0 $self->macro($pagercmd, { params => [
55 $self->pager_disable_lines
56 ]} );
57  
58 0 ▶ $self->end_privileged
Branch: 1 branch
BranchTrueFalseTotal
if $self->privileged_paging and not $privstate 0 0 0
Condition0%0/3
$self->privileged_paging and not $privstate
!ll&&!rl&&r
000
MC/DC0%0/2
$self->privileged_paging and not $privstate
$self->privileged_paging not $privstate
Truth table0%0/3
A and not(B)
A= $self->privileged_pagingB= not $privstate
ABresult
0X0
100
111
59 if $self->privileged_paging and not $privstate;
60 }
61  
62 # method to enter privileged mode on the remote device.
63 # optionally, use a different username and password to those
64 # used at login time. if using a different username then we'll
65 # explicily login rather than privileged.
66  
67 sub begin_privileged {
68 0 ▶ my $self = shift;
Subroutine begin_privileged: not called CC=13 SCAR=52.0
Pod undocumented
69 0 my $options = Net::Appliance::Session::Transport::ConnectOptions->new(@_);
70  
71 0 ▶ return unless $self->do_privileged_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_privileged_mode 0 0 0
72 0 ▶ return if $self->in_privileged_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_privileged_mode 0 0 0
73  
74 0 ▶ die 'must connect before you can begin_privileged'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
75 unless $self->logged_in;
76  
77 # rt.cpan#47214 check if we are already enabled by peeking the prompt
78 0 ▶ if ($self->prompt_looks_like('privileged')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_looks_like("privileged")) 0 0 0
79 0 $self->in_privileged_mode(1);
80 0 return;
81 }
82  
83 # default is to re-use login credentials
84 0 ▶ my $username = $options->has_username ? $options->username : $self->get_username;
Branch: 1 branch
BranchTrueFalseTotal
$options->has_username ? : 0 0 0
85  
86 # rt.cpan#69139 support passing of privileged_password to the constructor
87 0 ▶ my $password = $options->has_password ? $options->password :
Branch: 2 branches
BranchTrueFalseTotal
$options->has_password ? : 0 0 0
$self->has_privileged_password ? : 0 0 0
88 $self->has_privileged_password ? $self->get_privileged_password
89 : $self->get_password;
90  
91 0 $self->macro('begin_privileged');
92  
93 # whether login or enable, we still must be prepared for username
94 0 ▶ if ($self->prompt_looks_like('user')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_looks_like("user")) 0 0 0
95 0 ▶ die 'a set username is required to enter priv on this host'
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
96 if not $username;
97
98 0 $self->cmd($username, { match => 'pass' });
99 }
100  
101 0 ▶ if ($self->prompt_looks_like('pass')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_looks_like("pass")) 0 0 0
102 0 ▶ die 'a set password is required before begin_privileged'
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 0 0
103 if not $password;
104  
105 # rt.cpan#92376 timeout when incorrect password
106 0 $self->cmd($password, { match => 'generic' });
107 }
108  
109 0 ▶ $self->prompt_looks_like('privileged')
Branch: 1 branch
BranchTrueFalseTotal
$self->prompt_looks_like("privileged") or die "should be in privileged mode but prompt does not match" 0 0 0
110 or die 'should be in privileged mode but prompt does not match';
111 0 $self->in_privileged_mode(1);
112 }
113  
114 sub end_privileged {
115 0 ▶ my $self = shift;
Subroutine end_privileged: not called CC=5 SCAR=34.0
Pod undocumented
116
117 0 ▶ return unless $self->do_privileged_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_privileged_mode 0 0 0
118 0 ▶ return unless $self->in_privileged_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_privileged_mode 0 0 0
119  
120 0 ▶ die 'must leave configure mode before leaving privileged mode'
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_configure_mode 0 0 0
121 if $self->in_configure_mode;
122  
123 0 $self->macro('end_privileged');
124  
125 0 ▶ not $self->prompt_looks_like('privileged')
Branch: 1 branch
BranchTrueFalseTotal
$self->prompt_looks_like("privileged") and die "should have left privileged mode but prompt still matches" 0 0 0
126 or die 'should have left privileged mode but prompt still matches';
127 0 $self->in_privileged_mode(0);
128 }
129  
130 sub begin_configure {
131 0 ▶ my $self = shift;
Subroutine begin_configure: not called CC=6 SCAR=37.4
Pod undocumented
132  
133 0 ▶ return unless $self->do_configure_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_configure_mode 0 0 0
134 0 ▶ return if $self->in_configure_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_configure_mode 0 0 0
135  
136 0 ▶ die 'must enter privileged mode before configure mode'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_privileged_mode 0 0 0
137 unless $self->in_privileged_mode;
138  
139 # rt.cpan#47214 check if we are already in config by peeking the prompt
140 0 ▶ if ($self->prompt_looks_like('configure')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_looks_like("configure")) 0 0 0
141 0 $self->in_configure_mode(1);
142 0 return;
143 }
144  
145 0 $self->macro('begin_configure');
146  
147 0 ▶ $self->prompt_looks_like('configure')
Branch: 1 branch
BranchTrueFalseTotal
$self->prompt_looks_like("configure") or die "should be in configure mode but prompt does not match" 0 0 0
148 or die 'should be in configure mode but prompt does not match';
149 0 $self->in_configure_mode(1);
150 }
151  
152 sub end_configure {
153 0 ▶ my $self = shift;
Subroutine end_configure: not called CC=9 SCAR=45.0
Pod undocumented
154  
155 0 ▶ return unless $self->do_configure_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_configure_mode 0 0 0
156 0 ▶ return unless $self->in_configure_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_configure_mode 0 0 0
157  
158 0 $self->macro('end_configure');
159  
160 # we didn't manage to escape configure mode (must be nested?)
161 0 ▶ if ($self->prompt_looks_like('configure')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_looks_like("configure")) 0 0 0
162 0 my $caller3 = (caller(3))[3];
163  
164 # max out at three tries to exit configure mode
165 0 ▶ if ( $caller3 and $caller3 =~ m/end_configure$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller3 and $caller3 =~ /end_configure$/) { } 0 0 0
Condition0%0/3
$caller3 and $caller3 =~ /end_configure$/
!ll&&!rl&&r
000
MC/DC0%0/2
$caller3 and $caller3 =~ /end_configure$/
$caller3 $caller3 =~ /end_configure$/
Truth table0%0/3
A and B
A= $caller3B= $caller3 =~ /end_configure$/
ABresult
0X0
100
111
166 0 die 'failed to leave configure mode';
167 }
168 # try again to exit configure mode
169 else {
170 0 $self->end_configure;
171 }
172 }
173  
174 # return if recursively called
175 0 my $caller1 = (caller(1))[3];
176 0 ▶ if ( defined $caller1 and $caller1 =~ m/end_configure$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $caller1 and $caller1 =~ /end_configure$/) 0 0 0
Condition0%0/3
defined $caller1 and $caller1 =~ /end_configure$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $caller1 and $caller1 =~ /end_configure$/
defined $caller1 $caller1 =~ /end_configure$/
Truth table0%0/3
A and B
A= defined $caller1B= $caller1 =~ /end_configure$/
ABresult
0X0
100
111
177 0 return;
178 }
179  
180 0 ▶ not $self->prompt_looks_like('configure')
Branch: 1 branch
BranchTrueFalseTotal
$self->prompt_looks_like("configure") and die "should have exited configure mode but prompt still matches" 0 0 0
181 or die 'should have exited configure mode but prompt still matches';
182 0 $self->in_configure_mode(0);
183 }
184  
185 1 1;
« blib/lib/Net/Appliance/Session/Scripting.pm ↑ Summary blib/lib/Net/Appliance/Session/Transport.pm »