blib/lib/Langertha/Holmgang/World.pm

filter: Statementstmt 75.7% 338 / 446 Branchbran 61.1% 197 / 322 Conditioncond 47.5% 98 / 206 MC/DCmcdc 29.3% 32 / 109 Subroutinesub 79.5% 39 / 49 Podpod 100% 10 / 10 totaltotal 62.5% 714 / 1142 CC 403 SCAR 39.2
CC
403
Cov
65%
_observed_command
185.5
_observed_command
185.5
_observe_wget
83.7
CRAP 50.3
« blib/lib/Langertha/Holmgang/Sim/Linux/Logrotate.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Postfix.pm »
1 package Langertha::Holmgang::World;
2 # ABSTRACT: The simulated world of a case — state, tool dispatch, handlers, end-state diff
3  
4 43 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 43 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 ▶use Langertha::Holmgang::Expectation qw( condition element_match path get flat filter_value interp_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 43 ▶use Langertha::Holmgang::Shell ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 43 ▶use Langertha::Holmgang::Shell::Filter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 43 ▶use Langertha::Holmgang::Simulators;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 43 ▶use Langertha::Holmgang::Target ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 43 ▶use Langertha::Holmgang::Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 43 ▶use Langertha::Holmgang::ToolSchema qw( schema_errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 43 our $VERSION = '0.002';
18  
19 # the state changes of a handler's apply, in format 2; a true constant
20 43 our @OPS = qw( set push insert delete remove move );
21  
22 43 my $JSON = JSON::MaybeXS->new( utf8 => 0, canonical => 1, allow_nonref => 1 );
23  
24 # what the model gets for a simulator that fails in Perl, and how such a text looks
25 # (" at FILE line N.", as die without "\n" ends); a line only counts when FILE is a
26 # source file of this process (_own_file) — simulated log text with Perl-style lines
27 # of invented paths is output the case asks for (ADR 0004)
28 43 my $FAULT = 'Fehler: interner Fehler beim Ausführen des Aufrufs.';
29 43 my $PERL_ERROR = qr/^((.*?$Langertha::Holmgang::Target::LOCATION\.)[ \t]*)$/m;
30  
31  
32 43 has state => (
33 is => 'ro',
34 writer => '_set_state',
35 required => 1,
36 );
37  
38  
39 has tools => (
40 is => 'ro',
41 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 43 );
43  
44  
45 has observe => (
46 is => 'ro',
47 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 43 );
49  
50  
51 43 has simulators => (
52 is => 'ro',
53 lazy => 1,
54 builder => '_build_simulators',
55 );
56 28 ▶sub _build_simulators { Langertha::Holmgang::Simulators->new }
Subroutine _build_simulators: called CC=1 SCAR=0.0
57  
58  
59 2234 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
60 2234 my ( $orig, $class, @args ) = @_;
61 2234 my $a = $class->$orig(@args);
62 2234 ▶ $a->{state} = $class->clone( $a->{state} // {} );
Condition50%1/2
$a->{"state"} // {}
l!l
22340
MC/DC0%0/1
$a->{"state"} // {}
$a->{"state"}
Truth table50%1/2
A
A= $a->{"state"}
Aresult
11
00
63 2234 ▶ $a->{tools} //= [];
Condition50%1/2
$a->{"tools"} //= []
l!l
22340
MC/DC0%0/1
$a->{"tools"} //= []
$a->{"tools"}
Truth table50%1/2
A
A= $a->{"tools"}
Aresult
11
00
64 2234 ▶ $a->{observe} //= [];
Condition100%2/2
$a->{"observe"} //= []
l!l
221618
MC/DC100%1/1
$a->{"observe"} //= []
$a->{"observe"}
Truth table100%2/2
A
A= $a->{"observe"}
Aresult
11
00
65 2234 return $a;
66 43 };
67  
68  
69 4919 ▶sub clone {
Subroutine clone: called CC=1 SCAR=1.2
Pod documented
70 0 ▶ my ( $self, $d ) = @_;
Subroutine clone: not called CC=1 SCAR=1.2
Pod documented
71 0 return $JSON->decode( $JSON->encode($d) );
72 }
73  
74  
75 1163 ▶sub offer {
Subroutine offer: called CC=1 SCAR=1.2
Pod documented
76 0 ▶ my ( $self ) = @_;
Subroutine offer: not called CC=1 SCAR=1.2
Pod documented
77 0 return [ map { { name => $_->{name}, description => $_->{description}, parameters => $_->{parameters} } } @{ $self->tools } ];
78 }
79  
80  
81 2315 ▶sub call {
Subroutine call: called CC=11 SCAR=35.4
Pod documented
82 0 ▶ my ( $self, $name, $raw ) = @_;
Subroutine call: not called CC=11 SCAR=35.4
Pod documented
83 0 ▶ my ( $tool ) = grep { $_->{name} eq ( $name // '' ) } @{ $self->tools };
Condition25%1/4
$name // ""
l!l
45540
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
Truth table0%0/2
A
A= $name
Aresult
11
00
84 0 ▶ return ( "Fehler: unbekanntes Werkzeug '".( $name // '' )."'.", {}, 0 ) unless $tool;
Branch: 2 branches
BranchTrueFalseTotal
unless $tool 2 2313 2315
unless $tool 0 0 0
Condition25%1/4
$name // ""
l!l
20
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
Truth table0%0/2
A
A= $name
Aresult
11
00
85  
86 0 my $args = {};
87 0 ▶ if ( ref $raw eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $raw eq 'HASH') { } 2311 2 2313
elsif (defined $raw and length $raw) { } 2 0 2
if (ref $raw eq 'HASH') { } 0 0 0
elsif (defined $raw and length $raw) { } 0 0 0
88 0 $args = $raw;
89 }
90 ▶ elsif ( defined $raw && length $raw ) {
Condition16%1/6
defined $raw and length $raw
!ll&&!rl&&r
002
defined $raw and length $raw
!ll&&!rl&&r
000
MC/DC0%0/2
defined $raw and length $raw
defined $raw length $raw
MC/DC0%0/2
defined $raw and length $raw
defined $raw length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
91 0 my $text = $raw;
92 0 ▶ utf8::decode($text) unless utf8::is_utf8($text);
Branch: 2 branches
BranchTrueFalseTotal
unless utf8::is_utf8($text) 2 0 2
unless utf8::is_utf8($text) 0 0 0
93 0 $args = eval { $JSON->decode($text) };
94 0 ▶ return ( 'Fehler: Argumente sind kein JSON-Objekt.', {}, 0 ) unless ref $args eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $args eq "HASH" 1 1 2
unless ref $args eq "HASH" 0 0 0
95 }
96 0 ▶ if ( my @e = schema_errors( $args, $tool->{parameters} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@e) = schema_errors($args, $tool->{"parameters"})) 5 2307 2312
if (my(@e) = schema_errors($args, $tool->{"parameters"})) 0 0 0
97 0 return ( 'Fehler: Argumente passen nicht zum Schema: '.join( '; ', @e ), $args, 0 );
98 }
99 0 ▶ return $self->_simulate( $tool, $args ) if defined $tool->{simulator};
Branch: 2 branches
BranchTrueFalseTotal
if defined $tool->{"simulator"} 440 1867 2307
if defined $tool->{"simulator"} 0 0 0
100 0 return $self->_handle( $tool, $args );
101 }
102  
103 440 ▶sub _simulate {
Subroutine _simulate: called CC=5 SCAR=20.9
104 0 ▶ my ( $self, $tool, $args ) = @_;
Subroutine _simulate: not called CC=5 SCAR=20.9
105 0 my $sim = $tool->{simulator};
106 0 my $sims = $self->simulators;
107 0 ▶ if ( $sim eq 'run_command' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sim eq "run_command") 183 257 440
if ($sim eq "run_command") 0 0 0
108 0 my @r = $self->_observed_command($args);
109 0 ▶ return @r if @r;
Branch: 2 branches
BranchTrueFalseTotal
if @r 27 156 183
if @r 0 0 0
110 }
111 0 my $next = $self->clone( $self->state );
112 413 ▶ my ( $died, $out, $ok, $gap ) = _guarded( sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
113 0 ▶ $sim eq 'write_file' ? $sims->write_file( $next, @{$args}{qw( host path content )} )
Branch: 2 branches
BranchTrueFalseTotal
$sim eq 'run_command' ? : 156 136 292
$sim eq 'run_command' ? : 0 0 0
114 0 : $sim eq 'run_command' ? $sims->run_command( $next, @{$args}{qw( host command )} )
115 0 ▶ : $sims->tool_call( $next, $sim, $tool->{name}, $args );
Subroutine __ANON__: not called CC=3 SCAR=14.2
Branch: 2 branches
BranchTrueFalseTotal
$sim eq 'write_file' ? : 121 292 413
$sim eq 'write_file' ? : 0 0 0
116 0 } );
117 0 ▶ $self->_set_state($next) unless $died;
Branch: 2 branches
BranchTrueFalseTotal
unless $died 411 2 413
unless $died 0 0 0
118 0 ▶ return ( $out, $args, $ok ? 1 : 0, $gap );
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 394 19 413
$ok ? : 0 0 0
119 }
120  
121 # a file this process runs from: a loaded module (%INC) or the script; only these
122 # paths are a host leak (never touches the file system)
123 8 ▶sub _own_file {
Subroutine _own_file: called CC=3 SCAR=15.3
124 0 ▶ my ( $file ) = @_;
Subroutine _own_file: not called CC=3 SCAR=15.3
125 0 ▶ return $file eq $0 || scalar grep { defined && $_ eq $file } values %INC;
Condition40%4/10
$file eq $0 || scalar grep({defined $_ and $_ eq $file;} values %INC)
l!l
35
defined $_ && $_ eq $file
!ll&&!rl&&r
016131
$file eq $0 || scalar grep({defined $_ and $_ eq $file;} values %INC)
l!l
00
defined $_ && $_ eq $file
!ll&&!rl&&r
000
MC/DC100%1/1
$file eq $0 || scalar grep({defined $_ and $_ eq $file;} values %INC)
$file eq $0
MC/DC50%1/2
defined $_ && $_ eq $file
defined $_ $_ eq $file
MC/DC0%0/1
$file eq $0 || scalar grep({defined $_ and $_ eq $file;} values %INC)
$file eq $0
MC/DC0%0/2
defined $_ && $_ eq $file
defined $_ $_ eq $file
Truth table100%2/2
A
A= $file eq $0
Aresult
11
00
Truth table66%2/3
A && B
A= defined $_B= $_ eq $file
ABresult
0X0
100
111
Truth table0%0/2
A
A= $file eq $0
Aresult
11
00
Truth table0%0/3
A && B
A= defined $_B= $_ eq $file
ABresult
0X0
100
111
126 }
127  
128 # runs a simulator call; ( died, output, ok, gap ) with a Perl error of the simulator —
129 # its die, or such a text in its output — turned into $FAULT and the original into the gap
130 423 ▶sub _guarded {
Subroutine _guarded: called CC=6 SCAR=24.9
131 0 ▶ my ( $code ) = @_;
Subroutine _guarded: not called CC=6 SCAR=24.9
132 0 my ( $out, $ok, $gap );
133 0 ▶ unless ( eval { ( $out, $ok, $gap ) = $code->(); 1 } ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (eval { do { () = &$code(); 1 } }) 2 421 423
unless (eval { do { () = &$code(); 1 } }) 0 0 0
134 0 my $err = $@ =~ s/\s+\z//r;
135 0 return ( 1, $FAULT, 0, join( '; ', grep { defined } $gap, 'simulator error: '.$err ) );
136 }
137 0 my @faults;
138 0 ▶ $out =~ s/$PERL_ERROR/_own_file($3) ? do { push @faults, $2; $FAULT } : $1/ge if defined $out && !ref $out;
Branch: 4 branches
BranchTrueFalseTotal
if defined $out and not ref $out 421 0 421
_own_file($3) ? : 4 4 8
if defined $out and not ref $out 0 0 0
_own_file($3) ? : 0 0 0
Condition16%1/6
defined $out and not ref $out
!ll&&!rl&&r
00421
defined $out and not ref $out
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and not ref $out
defined $out not ref $out
MC/DC0%0/2
defined $out and not ref $out
defined $out not ref $out
Truth table33%1/3
A and not(B)
A= defined $outB= not ref $out
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $outB= not ref $out
ABresult
0X0
100
111
139 0 ▶ return ( 0, $out, $ok, $gap ) unless @faults;
Branch: 2 branches
BranchTrueFalseTotal
unless @faults 417 4 421
unless @faults 0 0 0
140 0 return ( 0, $out, 0, join( '; ', grep { defined } $gap, map { 'simulator error: '.$_ } @faults ) );
141 }
142  
143 # run_command with observations: a single command an observation answers, or a chain on
144 # a linux device of which at least one link is observed; else the empty list
145 183 ▶sub _observed_command {
Subroutine _observed_command: called CC=28 SCAR=52.2
146 0 ▶ my ( $self, $args ) = @_;
Subroutine _observed_command: not called CC=28 SCAR=52.2
147 0 ▶ return unless @{ $self->observe };
Branch: 2 branches
BranchTrueFalseTotal
unless @{$self->observe;} 137 46 183
unless @{$self->observe;} 0 0 0
148 0 my $host = $args->{host};
149 0 ▶ my $dev = defined $host && !ref $host ? $self->state->{$host} : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $host && !ref($host) ? : 46 0 46
defined $host && !ref($host) ? : 0 0 0
Condition16%1/6
defined $host and not ref $host
!ll&&!rl&&r
0046
defined $host and not ref $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and not ref $host
defined $host not ref $host
MC/DC0%0/2
defined $host and not ref $host
defined $host not ref $host
Truth table33%1/3
A and not(B)
A= defined $hostB= not ref $host
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $hostB= not ref $host
ABresult
0X0
100
111
150 # a chain on a linux shell (a && b; c): observations per link — first, so that a pattern
151 # without an end anchor does not answer the whole chain
152 my @chain = ref $dev eq 'HASH' && ( $dev->{type} // '' ) eq 'linux' && defined $args->{command} && !ref $args->{command}
153 0 ▶ ? Langertha::Holmgang::Shell::split_chain( $args->{command} ) : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $dev eq 'HASH' && ($dev->{'type'} // '') eq 'linux' && defined $args->{'command'} && !ref($args->{'command'}) ? : 44 2 46
ref $dev eq 'HASH' && ($dev->{'type'} // '') eq 'linux' && defined $args->{'command'} && !ref($args->{'command'}) ? : 0 0 0
Condition31%7/22
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"} and not ref $args->{"command"}
!ll&&!rl&&r
2044
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"}
!ll&&!rl&&r
2044
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux"
!ll&&!rl&&r
2044
$dev->{"type"} // ""
l!l
440
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"} and not ref $args->{"command"}
!ll&&!rl&&r
000
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"}
!ll&&!rl&&r
000
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux"
!ll&&!rl&&r
000
$dev->{"type"} // ""
l!l
00
MC/DC25%1/4
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"} and not ref $args->{"command"}
ref $dev eq "HASH" $dev->{"type"} defined $args->{"command"} not ref $args->{"command"}
MC/DC0%0/4
ref $dev eq "HASH" and ($dev->{"type"} // "") eq "linux" and defined $args->{"command"} and not ref $args->{"command"}
ref $dev eq "HASH" $dev->{"type"} defined $args->{"command"} not ref $args->{"command"}
Truth table40%2/5
A and B and C and not(D)
A= ref $dev eq "HASH"B= $dev->{"type"}C= defined $args->{"command"}D= not ref $args->{"command"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and not(D)
A= ref $dev eq "HASH"B= $dev->{"type"}C= defined $args->{"command"}D= not ref $args->{"command"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
154 0 ▶ if ( @chain <= 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@chain <= 1) 39 7 46
if (@chain <= 1) 0 0 0
155 0 my ( $obs, $obs_ok ) = $self->_observe($args);
156 0 ▶ return defined $obs ? ( $obs, $args, $obs_ok ) : ();
Branch: 2 branches
BranchTrueFalseTotal
defined $obs ? : 20 19 39
defined $obs ? : 0 0 0
157 }
158 0 ▶ return unless grep { defined( ( $self->_observe( { %$args, command => $_->[1] } ) )[0] ) } @chain;
Branch: 2 branches
BranchTrueFalseTotal
unless grep {defined(($self->_observe({%$args, "command", $_->[1]}))[0]);} @chain 0 7 7
unless grep {defined(($self->_observe({%$args, "command", $_->[1]}))[0]);} @chain 0 0 0
159 0 my ( @out, @gaps );
160 0 my $ok = 1;
161 0 for my $g ( @chain ) {
162 0 my ( $op, $cmd ) = @$g;
163 0 ▶ next if $op eq '&&' && !$ok || $op eq '||' && $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $op eq "&&" and not $ok or $op eq "||" and $ok 1 17 18
if $op eq "&&" and not $ok or $op eq "||" and $ok 0 0 0
Condition38%7/18
$op eq "&&" and not $ok or $op eq "||" and $ok
l!l&&r!l&&!r
1017
$op eq "&&" and not $ok
!ll&&!rl&&r
1611
$op eq "||" and $ok
!ll&&!rl&&r
1520
$op eq "&&" and not $ok or $op eq "||" and $ok
l!l&&r!l&&!r
000
$op eq "&&" and not $ok
!ll&&!rl&&r
000
$op eq "||" and $ok
!ll&&!rl&&r
000
MC/DC50%2/4
$op eq "&&" and not $ok or $op eq "||" and $ok
$op eq "&&" not $ok $op eq "||" $ok
MC/DC0%0/4
$op eq "&&" and not $ok or $op eq "||" and $ok
$op eq "&&" not $ok $op eq "||" $ok
Truth table57%4/7
A and not(B) or C and D
A= $op eq "&&"B= not $okC= $op eq "||"D= $ok
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
Truth table0%0/7
A and not(B) or C and D
A= $op eq "&&"B= not $okC= $op eq "||"D= $ok
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
164 0 my ( $o, $o_ok ) = $self->_observe( { %$args, command => $cmd } );
165 0 ▶ if ( defined $o ) { push @out, $o if length $o; $ok = $o_ok; next }
Branch: 4 branches
BranchTrueFalseTotal
if (defined $o) 7 10 17
if length $o 6 1 7
if (defined $o) 0 0 0
if length $o 0 0 0
166 0 my $next = $self->clone( $self->state );
167 # status of the previous link as $? (a trace the simulator takes out again)
168 0 ▶ $next->{$host}{_rc} = $ok ? 0 : 1 if ref $next->{$host} eq 'HASH';
Branch: 4 branches
BranchTrueFalseTotal
if ref $next->{$host} eq "HASH" 10 0 10
$ok ? : 6 4 10
if ref $next->{$host} eq "HASH" 0 0 0
$ok ? : 0 0 0
169 0 ▶ my ( $died, $t, $k, $gap ) = _guarded( sub { $self->simulators->run_command( $next, $host, $cmd ) } );
Subroutine __ANON__: called CC=1 SCAR=0.4
170 0 ▶ delete $next->{$host}{_rc} if ref $next->{$host} eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $next->{$host} eq "HASH" 10 0 10
if ref $next->{$host} eq "HASH" 0 0 0
171 0 ▶ $self->_set_state($next) unless $died;
Branch: 2 branches
BranchTrueFalseTotal
unless $died 10 0 10
unless $died 0 0 0
172 0 ▶ push @out, $t if defined $t && length $t;
Branch: 2 branches
BranchTrueFalseTotal
if defined $t and length $t 8 2 10
if defined $t and length $t 0 0 0
Condition33%2/6
defined $t and length $t
!ll&&!rl&&r
028
defined $t and length $t
!ll&&!rl&&r
000
MC/DC50%1/2
defined $t and length $t
defined $t length $t
MC/DC0%0/2
defined $t and length $t
defined $t length $t
Truth table66%2/3
A and B
A= defined $tB= length $t
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $tB= length $t
ABresult
0X0
100
111
173 0 ▶ push @gaps, $gap if defined $gap;
Branch: 2 branches
BranchTrueFalseTotal
if defined $gap 0 10 10
if defined $gap 0 0 0
174 0 ▶ $ok = $k ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$k ? : 9 1 10
$k ? : 0 0 0
175 }
176 0 ▶ return ( join( "\n", @out ), $args, $ok, @gaps ? join( '; ', @gaps ) : undef );
Branch: 2 branches
BranchTrueFalseTotal
@gaps ? : 0 7 7
@gaps ? : 0 0 0
177 }
178  
179 # the first fitting observation for these arguments (rendered, pipe filters applied) and
180 # whether the command succeeds; ( undef, 0 ) when none fits
181 74 ▶sub _observe {
Subroutine _observe: called CC=4 SCAR=13.9
182 74 my ( $self, $args ) = @_;
183 74 for my $h ( @{ $self->observe } ) {
184 148 ▶ my $with = $self->_fits( $h, $args ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $with = $self->_fits($h, $args) or next 23 125 148
185 23 my $out = $self->_pipe_filters( $self->_render( $h->{output}, $with ), $args->{command} );
186 23 ▶ return ( $out, $h->{error} ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$h->{'error'} ? : 9 14 23
187 }
188 51 return $self->_observe_wget($args);
189 }
190  
191 # wget as a client of the curl observations (ADR 0013): a case answers an HTTP
192 # request for curl; the same request through wget gets the same answer in wget's
193 # words. Only the forms whose answer follows from the curl answer: the body to
194 # standard output. ( undef, 0 ) when the command is no such wget or no
195 # observation answers its curl form.
196 51 ▶sub _observe_wget {
Subroutine _observe_wget: called CC=50 SCAR=44.3
197 51 my ( $self, $args ) = @_;
198 51 my $command = $args->{command};
199 51 ▶ return ( undef, 0 ) unless defined $command && !ref $command
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command and not ref $command and $command =~ /\A\s*(?:sudo\s+)?wget\s/ and not $command =~ /[;&`]|\$\(|\|\|/ 34 17 51
Condition55%5/9
defined $command and not ref $command and $command =~ /\A\s*(?:sudo\s+)?wget\s/ and not $command =~ /[;&`]|\$\(|\|\|/
!ll&&!rl&&r
34017
defined $command and not ref $command and $command =~ /\A\s*(?:sudo\s+)?wget\s/
!ll&&!rl&&r
03417
defined $command and not ref $command
!ll&&!rl&&r
0051
MC/DC25%1/4
defined $command and not ref $command and $command =~ /\A\s*(?:sudo\s+)?wget\s/ and not $command =~ /[;&`]|\$\(|\|\|/
defined $command not ref $command $command =~ /\A\s*(?:sudo\s+)?wget\s/ not $command =~ /[;&`]|\$\(|\|\|/
Truth table40%2/5
A and not(B) and C and not(D)
A= defined $commandB= not ref $commandC= $command =~ /\A\s*(?:sudo\s+)?wget\s/D= not $command =~ /[;&`]|\$\(|\|\|/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
200 && $command =~ /\A\s*(?:sudo\s+)?wget\s/ && $command !~ /[;&`]|\$\(|\|\|/;
201 17 ▶ my @w = eval { Langertha::Holmgang::Shell::shell_words($command) } or return ( undef, 0 );
Branch: 1 branch
BranchTrueFalseTotal
my(@w) = eval { do { Langertha::Holmgang::Shell::shell_words($command) } } or return undef, 0 17 0 17
202 17 my ( @cmd, @tail );
203 17 for ( @w ) {
204 66 ▶ if ( @tail || ( ref $_ && $_->{op} eq '|' ) ) { push @tail, $_; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@tail or ref $_ and $_->{"op"} eq "|") 4 62 66
Condition83%5/6
@tail or ref $_ and $_->{"op"} eq "|"
l!l&&r!l&&!r
3162
ref $_ and $_->{"op"} eq "|"
!ll&&!rl&&r
6201
MC/DC66%2/3
@tail or ref $_ and $_->{"op"} eq "|"
@tail ref $_ $_->{"op"} eq "|"
Truth table75%3/4
A or B and C
A= @tailB= ref $_C= $_->{"op"} eq "|"
ABCresult
1XX1
0111
00X0
0100
205 62 ▶ return ( undef, 0 ) if ref $_; # a redirection: the simulator's business
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 0 62 62
206 62 push @cmd, $_;
207 }
208 17 ▶ shift @cmd while @cmd && $cmd[0] eq 'sudo';
Branch: 1 branch
BranchTrueFalseTotal
@cmd and $cmd[0] eq "sudo" and do { shift @cmd } 1 17 18
Condition66%2/3
@cmd and $cmd[0] eq "sudo"
!ll&&!rl&&r
0171
MC/DC50%1/2
@cmd and $cmd[0] eq "sudo"
@cmd $cmd[0] eq "sudo"
Truth table66%2/3
A and B
A= @cmdB= $cmd[0] eq "sudo"
ABresult
0X0
100
111
209 17 shift @cmd;
210 17 my ( $url, $out, $quiet, $insecure, @headers );
211 17 ▶ while ( @cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cmd) 36 14 50
212 36 my $x = shift @cmd;
213 36 ▶ if ( my ( $fl, $to ) = $x =~ /\A-([qSv]*)O(.*)\z/ ) { $quiet = 1 if $fl =~ /q/; return ( undef, 0 ) if $fl =~ /S/; $out = length $to ? $to : shift @cmd }
Branch: 14 branches
BranchTrueFalseTotal
if (() = $x =~ /\A-([qSv]*)O(.*)\z/) { } 13 23 36
elsif ($x =~ /\A--output-document=(.*)\z/) { } 1 22 23
elsif ($x eq '--output-document') { } 0 22 22
elsif ($x eq '-q' or $x eq '--quiet' or $x eq '-nv' or $x eq '--no-verbose') { } 2 20 22
elsif ($x eq '--no-check-certificate') { } 0 20 20
elsif ($x =~ /\A--header=(.*)\z/s) { } 1 19 20
elsif ($x eq '--header') { } 0 19 19
elsif ($x =~ /\A(?:-T|-t|--timeout|--tries|--connect-timeout|--read-timeout)\z/) { } 1 18 19
elsif ($x =~ /\A(?:--timeout|--tries|--connect-timeout|--read-timeout)=|\A-[Tt]\d/) { } 1 17 18
elsif ($x =~ /\A-/) { } 3 14 17
elsif (defined $url) { } 0 14 14
if $fl =~ /q/ 7 6 13
if $fl =~ /S/ 0 13 13
length $to ? : 11 2 13
214 1 elsif ( $x =~ /\A--output-document=(.*)\z/ ) { $out = $1 }
215 0 elsif ( $x eq '--output-document' ) { $out = shift @cmd }
216 2 ▶ elsif ( $x eq '-q' || $x eq '--quiet' || $x eq '-nv' || $x eq '--no-verbose' ) { $quiet = 1 }
Condition77%7/9
$x eq "-q" or $x eq "--quiet" or $x eq "-nv" or $x eq "--no-verbose"
l!l&&r!l&&!r
2020
$x eq "-q" or $x eq "--quiet" or $x eq "-nv"
l!l&&r!l&&!r
2020
$x eq "-q" or $x eq "--quiet"
l!l&&r!l&&!r
1120
MC/DC50%2/4
$x eq "-q" or $x eq "--quiet" or $x eq "-nv" or $x eq "--no-verbose"
$x eq "-q" $x eq "--quiet" $x eq "-nv" $x eq "--no-verbose"
Truth table60%3/5
A or B or C or D
A= $x eq "-q"B= $x eq "--quiet"C= $x eq "-nv"D= $x eq "--no-verbose"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
217 0 elsif ( $x eq '--no-check-certificate' ) { $insecure = 1 }
218 1 elsif ( $x =~ /\A--header=(.*)\z/s ) { push @headers, $1 }
219 0 ▶ elsif ( $x eq '--header' ) { push @headers, shift(@cmd) // '' }
Condition0%0/2
shift @cmd // ""
l!l
00
MC/DC0%0/1
shift @cmd // ""
shift @cmd
Truth table0%0/2
A
A= shift @cmd
Aresult
11
00
220 1 elsif ( $x =~ /\A(?:-T|-t|--timeout|--tries|--connect-timeout|--read-timeout)\z/ ) { shift @cmd }
221 elsif ( $x =~ /\A(?:--timeout|--tries|--connect-timeout|--read-timeout)=|\A-[Tt]\d/ ) { }
222 3 elsif ( $x =~ /\A-/ ) { return ( undef, 0 ) } # -S, --spider, --post-data …: the answer needs more than the body
223 0 elsif ( defined $url ) { return ( undef, 0 ) }
224 14 else { $url = $x }
225 }
226 14 ▶ return ( undef, 0 ) unless defined $url && defined $out && $out eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and defined $out and $out eq "-" 2 12 14
Condition83%5/6
defined $url and defined $out and $out eq "-"
!ll&&!rl&&r
1112
defined $url and defined $out
!ll&&!rl&&r
0113
MC/DC66%2/3
defined $url and defined $out and $out eq "-"
defined $url defined $out $out eq "-"
Truth table75%3/4
A and B and C
A= defined $urlB= defined $outC= $out eq "-"
ABCresult
0XX0
10X0
1100
1111
227 12 ▶ my $q = sub { my ( $s ) = @_; $s =~ s/'/'\\''/g; return "'$s'" };
Subroutine __ANON__: called CC=1 SCAR=0.0
228 12 ▶ my $curl = join ' ', 'curl', '-s', ( $insecure ? '-k' : () ), ( map { ( '-H', $q->($_) ) } @headers ), $q->($url);
Branch: 1 branch
BranchTrueFalseTotal
$insecure ? : 0 12 12
229 12 for my $form ( $curl, $curl =~ s/'([^' ]+)'\z/$1/r ) {
230 13 for my $h ( @{ $self->observe } ) {
231 22 ▶ my $with = $self->_fits( $h, { %$args, command => $form } ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $with = $self->_fits($h, {%$args, "command", $form}) or next 11 11 22
232 11 my $body = $self->_render( $h->{output}, $with );
233 11 my ( $host, $port ) = $url =~ m{\A(?:https?://)?([^/:]+)(?::(\d+))?};
234 11 ▶ $port //= $url =~ m{\Ahttps://} ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[\Ahttps://] ? : 0 1 1
Condition100%2/2
$port //= $url =~ m[\Ahttps://] ? 443 : 80
l!l
101
MC/DC100%1/1
$port //= $url =~ m[\Ahttps://] ? 443 : 80
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
235 11 ▶ my $fail = sub { my ( $text ) = @_; return ( $quiet ? '' : $text, 0 ) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$quiet ? : 1 3 4
236 11 ▶ if ( $body =~ /\Acurl: \((\d+)\) (.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\Acurl: \((\d+)\) (.*)/) 3 8 11
237 3 my ( $code, $msg ) = ( $1, $2 );
238 3 ▶ return $fail->("Connecting to $host:$port... failed: Connection refused.") if $code == 7;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 7 3 0 3
239 0 ▶ return $fail->("Connecting to $host:$port... failed: Connection timed out.") if $code == 28;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 28 0 0 0
240 0 ▶ return $fail->("wget: unable to resolve host address \x{2018}$host\x{2019}") if $code == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 6 0 0 0
241 0 ▶ if ( $code == 22 && ( my ( $st ) = $msg =~ /error:? (\d{3})/ ) ) { return $fail->("HTTP request sent, awaiting response... $st\nERROR $st: Error.") }
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 22 and my($st) = $msg =~ /error:? (\d{3})/) 0 0 0
Condition0%0/3
$code == 22 and my($st) = $msg =~ /error:? (\d{3})/
!ll&&!rl&&r
000
MC/DC0%0/2
$code == 22 and my($st) = $msg =~ /error:? (\d{3})/
$code == 22 my($st) = $msg =~ /error:? (\d{3})/
Truth table0%0/3
A and B
A= $code == 22B= my($st) = $msg =~ /error:? (\d{3})/
ABresult
0X0
100
111
242 0 return ( undef, 0 );
243 }
244 8 ▶ return ( undef, 0 ) if $h->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"error"} 0 8 8
245 # an error page: wget prints no body for a status from 400 on
246 8 ▶ if ( my ( $st, $why ) = $body =~ m{<title>\s*([45]\d\d) ([^<]+?)\s*</title>}i ) { return $fail->("HTTP request sent, awaiting response... $st $why\nERROR $st: $why.") }
Branch: 1 branch
BranchTrueFalseTotal
if (() = $body =~ m[<title>\s*([45]\d\d) ([^<]+?)\s*</title>]i) 1 7 8
247 7 ▶ my $tail = @tail ? $command =~ s/\A[^|]*//r : '';
Branch: 1 branch
BranchTrueFalseTotal
@tail ? : 1 6 7
248 7 return ( $self->_pipe_filters( $body, 'wget'.$tail ), 1 );
249 }
250 }
251 1 return ( undef, 0 );
252 }
253  
254 1867 ▶sub _handle {
Subroutine _handle: called CC=9 SCAR=22.0
255 1867 my ( $self, $tool, $args ) = @_;
256 1867 ▶ for my $h ( @{ $tool->{handlers} || [] } ) {
Condition50%1/2
$tool->{'handlers'} || []
l!l
18670
MC/DC0%0/1
$tool->{'handlers'} || []
$tool->{'handlers'}
Truth table50%1/2
A
A= $tool->{'handlers'}
Aresult
11
00
257 1869 ▶ my $with = $self->_fits( $h, $args ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $with = $self->_fits($h, $args) or next 1864 5 1869
258 1864 ▶ my @apply = @{ $h->{apply} || [] };
Condition100%2/2
$h->{'apply'} || []
l!l
183826
MC/DC100%1/1
$h->{'apply'} || []
$h->{'apply'}
Truth table100%2/2
A
A= $h->{'apply'}
Aresult
11
00
259 1864 ▶ my $next = @apply ? $self->clone( $self->state ) : $self->state;
Branch: 1 branch
BranchTrueFalseTotal
@apply ? : 1838 26 1864
260 1864 for my $op ( @apply ) {
261 1839 my $err = $self->_apply( $next, $op, $with );
262 1839 ▶ return ( 'Fehler: '.$err, $args, 0 ) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 8 1831 1839
263 }
264 1856 $self->_set_state($next);
265 1856 ▶ return ( $self->_render( $h->{output}, $with ), $args, $h->{error} ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$h->{'error'} ? : 2 1854 1856
266 }
267 3 return ( 'Fehler: dieser Aufruf ist in diesem Szenario nicht vorgesehen.', $args, 0 );
268 }
269  
270 # match and when of a handler or an observation: the arguments with the named captures,
271 # or undef; a when that cannot be evaluated (an unknown filter in its path, only past
272 # lint) does not hold -- never a die out of call (k39)
273 2039 ▶sub _fits {
Subroutine _fits: called CC=5 SCAR=16.1
274 2039 my ( $self, $h, $args ) = @_;
275 2039 ▶ my $cap = $self->_args_match( $h->{match}, $args ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cap = $self->_args_match($h->{"match"}, $args) or return 1903 136 2039
276 1903 my $with = { %$args, %$cap };
277 1903 ▶ for my $w ( @{ $h->{when} || [] } ) {
Condition100%2/2
$h->{'when'} || []
l!l
141889
MC/DC100%1/1
$h->{'when'} || []
$h->{'when'}
Truth table100%2/2
A
A= $h->{'when'}
Aresult
11
00
278 14 my $holds = eval { ( condition( $self->state, $w, $with ) )[0] };
279 14 ▶ return unless $holds;
Branch: 1 branch
BranchTrueFalseTotal
unless $holds 5 9 14
280 }
281 1898 return $with;
282 }
283  
284 1890 ▶sub _render {
Subroutine _render: called CC=1 SCAR=0.0
285 1890 my ( $self, $tmpl, $args ) = @_;
286 1890 return Langertha::Holmgang::Template::render( $tmpl, $self->state, $args, $self->simulators );
287 }
288  
289 # filters behind a pipe (| grep, | head …) on the output of an observation
290 30 ▶sub _pipe_filters {
Subroutine _pipe_filters: called CC=13 SCAR=27.0
291 30 my ( $self, $out, $command ) = @_;
292 30 ▶ return $out unless defined $command && $command =~ /\|/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command and $command =~ /\|/ 27 3 30
Condition66%2/3
defined $command and $command =~ /\|/
!ll&&!rl&&r
0273
MC/DC50%1/2
defined $command and $command =~ /\|/
defined $command $command =~ /\|/
Truth table66%2/3
A and B
A= defined $commandB= $command =~ /\|/
ABresult
0X0
100
111
293 3 ▶ my @w = eval { Langertha::Holmgang::Shell::shell_words($command) } or return $out;
Branch: 1 branch
BranchTrueFalseTotal
my(@w) = eval { do { Langertha::Holmgang::Shell::shell_words($command) } } or return $out 3 0 3
294 3 my @stages = ( [] );
295 3 for ( @w ) {
296 15 ▶ if ( ref $_ && $_->{op} eq '|' ) { push @stages, [] }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ and $_->{'op'} eq '|') { } 3 12 15
elsif (not ref $_) { } 12 0 12
Condition66%2/3
ref $_ and $_->{"op"} eq "|"
!ll&&!rl&&r
1203
MC/DC50%1/2
ref $_ and $_->{"op"} eq "|"
ref $_ $_->{"op"} eq "|"
Truth table66%2/3
A and B
A= ref $_B= $_->{"op"} eq "|"
ABresult
0X0
100
111
297 12 elsif ( !ref $_ ) { push @{ $stages[-1] }, $_ }
298 }
299 3 shift @stages;
300 3 for my $st ( @stages ) {
301 3 ▶ shift @$st while @$st && $st->[0] eq 'sudo';
Branch: 1 branch
BranchTrueFalseTotal
@$st and $st->[0] eq "sudo" and do { shift @$st } 0 3 3
Condition33%1/3
@$st and $st->[0] eq "sudo"
!ll&&!rl&&r
030
MC/DC0%0/2
@$st and $st->[0] eq "sudo"
@$st $st->[0] eq "sudo"
Truth table33%1/3
A and B
A= @$stB= $st->[0] eq "sudo"
ABresult
0X0
100
111
302 3 ▶ next unless @$st;
Branch: 1 branch
BranchTrueFalseTotal
unless @$st 0 3 3
303 3 my ( $t ) = Langertha::Holmgang::Shell::Filter::filter_output( $out, $st );
304 3 ▶ $out = $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 3 0 3
305 }
306 3 return $out;
307 }
308  
309 # the patterns per argument: a hash of the named captures (true, maybe empty) or undef
310 2039 ▶sub _args_match {
Subroutine _args_match: called CC=5 SCAR=16.1
311 2039 my ( $self, $m, $args ) = @_;
312 2039 my %cap;
313 2039 ▶ for my $k ( sort keys %{ $m || {} } ) {
Condition100%2/2
$m || {}
l!l
6751364
MC/DC100%1/1
$m || {}
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
314 711 my $v = flat( $args->{$k} );
315 711 ▶ return undef unless defined $v && $v =~ /$m->{$k}/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v =~ /$m->{$k}/ 136 575 711
Condition66%2/3
defined $v and $v =~ /$m->{$k}/
!ll&&!rl&&r
0136575
MC/DC50%1/2
defined $v and $v =~ /$m->{$k}/
defined $v $v =~ /$m->{$k}/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /$m->{$k}/
ABresult
0X0
100
111
316 575 %cap = ( %cap, %+ );
317 }
318 1903 return \%cap;
319 }
320  
321  
322 1900 ▶sub value {
Subroutine value: called CC=7 SCAR=21.0
Pod documented
323 1900 my ( $self, $t, $args ) = @_;
324 1900 ▶ return { map { $_ => $self->value( $t->{$_}, $args ) } keys %$t } if ref $t eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t eq "HASH" 32 1868 1900
325 0 ▶ return [ map { $self->value( $_, $args ) } @$t ] if ref $t eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t eq "ARRAY" 0 1868 1868
326 1868 ▶ return $t if !defined $t || ref $t;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $t or ref $t 0 1868 1868
Condition33%1/3
not defined $t or ref $t
l!l&&r!l&&!r
001868
MC/DC0%0/2
not defined $t or ref $t
not defined $t ref $t
Truth table33%1/3
not(A) or B
A= not defined $tB= ref $t
ABresult
1X1
011
000
327 1868 ▶ return filter_value( $args->{$1}, $2 ) if $t =~ /\A\{([A-Za-z_][A-Za-z0-9_]*)(?:\|([a-z]+))?\}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A\{([A-Za-z_][A-Za-z0-9_]*)(?:\|([a-z]+))?\}\z/ 1595 273 1868
328 273 ▶ return $t =~ /\{[A-Za-z_]/ ? interp_text( $t, $args ) : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /\{[A-Za-z_]/ ? : 0 273 273
329 }
330  
331 # the container at a path without its last segment; hashes on the way are created
332 1839 ▶sub _parent {
Subroutine _parent: called CC=7 SCAR=27.8
333 1839 my ( $self, $d, @seg ) = @_;
334 1839 my $last = pop @seg;
335 1839 for my $k ( @seg ) {
336 1214 ▶ if ( ref $d eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $d eq 'HASH') { } 1214 0 1214
elsif (ref $d eq 'ARRAY' and $k =~ /\A\d+\z/ and $k < @$d) { } 0 0 0
337 1214 ▶ $d->{$k} //= {};
Condition50%1/2
$d->{$k} //= {}
l!l
12140
MC/DC0%0/1
$d->{$k} //= {}
$d->{$k}
Truth table50%1/2
A
A= $d->{$k}
Aresult
11
00
338 1214 $d = $d->{$k};
339 }
340 ▶ elsif ( ref $d eq 'ARRAY' && $k =~ /\A\d+\z/ && $k < @$d ) {
Condition0%0/6
ref $d eq "ARRAY" and $k =~ /\A\d+\z/ and $k < @$d
!ll&&!rl&&r
000
ref $d eq "ARRAY" and $k =~ /\A\d+\z/
!ll&&!rl&&r
000
MC/DC0%0/3
ref $d eq "ARRAY" and $k =~ /\A\d+\z/ and $k < @$d
ref $d eq "ARRAY" $k =~ /\A\d+\z/ $k < @$d
Truth table0%0/4
A and B and C
A= ref $d eq "ARRAY"B= $k =~ /\A\d+\z/C= $k < @$d
ABCresult
0XX0
10X0
1100
1111
341 0 $d = $d->[$k];
342 }
343 0 else { return ( undef, $last ) }
344 }
345 1839 return ( $d, $last );
346 }
347  
348 44 ▶sub _list_at {
Subroutine _list_at: called CC=8 SCAR=27.1
349 44 my ( $self, $state, $seg, $create ) = @_;
350 44 my ( $p, $last ) = $self->_parent( $state, @$seg );
351 44 ▶ return undef unless ref $p;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p 0 44 44
352 44 ▶ if ( ref $p eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "HASH") 44 0 44
353 44 ▶ $p->{$last} = [] if $create && !defined $p->{$last};
Branch: 1 branch
BranchTrueFalseTotal
if $create and not defined $p->{$last} 0 44 44
Condition66%2/3
$create and not defined $p->{$last}
!ll&&!rl&&r
4400
MC/DC0%0/2
$create and not defined $p->{$last}
$create not defined $p->{$last}
Truth table66%2/3
A and not(B)
A= $createB= not defined $p->{$last}
ABresult
0X0
100
111
354 44 ▶ return ref $p->{$last} eq 'ARRAY' ? $p->{$last} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $p->{$last} eq 'ARRAY' ? : 44 0 44
355 }
356 0 ▶ return $last =~ /\A\d+\z/ && ref $p->[$last] eq 'ARRAY' ? $p->[$last] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$last =~ /\A\d+\z/ && ref $p->[$last] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
$last =~ /\A\d+\z/ and ref $p->[$last] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$last =~ /\A\d+\z/ and ref $p->[$last] eq "ARRAY"
$last =~ /\A\d+\z/ ref $p->[$last] eq "ARRAY"
Truth table0%0/3
A and B
A= $last =~ /\A\d+\z/B= ref $p->[$last] eq "ARRAY"
ABresult
0X0
100
111
357 }
358  
359 3 ▶sub _index {
Subroutine _index: called CC=6 SCAR=19.6
360 3 my ( $self, $v, $list, $what, $allow_end ) = @_;
361 3 ▶ my $max = @$list - ( $allow_end ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$allow_end ? : 1 2 3
362 3 ▶ die $what." '".( $v // '' )."' ist keine Nummer\n" unless defined $v && "$v" =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and "$v" =~ /\A\d+\z/ 0 3 3
Condition20%1/5
defined $v and "$v" =~ /\A\d+\z/
!ll&&!rl&&r
003
$v // ""
l!l
00
MC/DC0%0/2
defined $v and "$v" =~ /\A\d+\z/
defined $v "$v" =~ /\A\d+\z/
MC/DC0%0/1
$v // ""
$v
Truth table33%1/3
A and B
A= defined $vB= "$v" =~ /\A\d+\z/
ABresult
0X0
100
111
Truth table0%0/2
A
A= $v
Aresult
11
00
363 3 ▶ die $what.' '.$v.' gibt es nicht (0 bis '.$max.")\n" if $v > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $max 1 2 3
364 2 return 0 + $v;
365 }
366  
367 # one change of a handler's apply, as format 2 writes it ({ set => PATH, value => … });
368 # returns undef or the error text for the model
369 1839 ▶sub _apply {
Subroutine _apply: called CC=21 SCAR=36.5
370 1839 my ( $self, $state, $op, $args ) = @_;
371 1839 my ( $kind ) = grep { exists $op->{$_} } @OPS;
372 1839 my $err;
373 1839 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { die 'unbekannte Aenderung ' . join(",", sort(keys %$op)) . "\n" unless defined $kind; my(@seg) = path($op->{$kind}, $args); if ($kind eq "set") { () = $self->_parent($state, @seg); die "Pfad nicht vorhanden\n" unless ref $p; if (ref $p eq "HASH") { $p->{$last} = $self->value($op->{"value"}, $args); } else { $$p[$self->_index($last, $p, "Eintrag")] = $self->value($op->{"value"}, $args); } ; } elsif ($kind eq "push") { my $l = $self->_list_at($state, \@seg, 1) or die "keine Liste\n"; push @$l, $self->value($op->{"value"}, $args); } elsif ($kind eq "insert") { my $l = $self->_list_at($state, \@seg, 1) or die "keine Liste\n"; my $at = $self->_index($self->value($op->{"at"}, $args), $l, "Position", 1); splice @$l, $at, 0, $self->value($op->{"value"}, $args); } elsif ($kind eq "delete") { () = $self->_parent($state, @seg); die "nicht vorhanden\n" unless ref $p eq "HASH" and exists $p->{$last} or ref $p eq "ARRAY" and $last =~ /\A\d+\z/ and $last < @$p; if (ref $p eq "HASH") { delete $p->{$last}; } else { splice @$p, $last, 1; } ; } elsif ($kind eq "remove") { my $l = $self->_list_at($state, \@seg, 0) or die "keine Liste\n"; my(@keep) = grep({not element_match($_, $op->{"where"}, $args);} @$l); die "kein passender Eintrag\n" if @keep == @$l; @$l = @keep; } else { my $l = $self->_list_at($state, \@seg, 0) or die "keine Liste\n"; my $from = $self->_index($self->value($op->{"from"}, $args), $l, "Eintrag"); my($item) = splice(@$l, $from, 1); my $to = $self->_index($self->value($op->{"to"}, $args), $l, "Ziel", 1); splice @$l, $to, 0, $item; } ; 1 } } or $err = ($@ =~ s/$Langertha::Holmgang::Target::LOCATION\.?\n?\z//r) =~ s/\s+\z//r 1831 8 1839
374 1839 ▶ die 'unbekannte Aenderung '.join( ',', sort keys %$op )."\n" unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 1839 1839
375 1839 my @seg = path( $op->{$kind}, $args );
376 1839 ▶ if ( $kind eq 'set' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($kind eq 'set') { } 1794 45 1839
elsif ($kind eq 'push') { } 40 5 45
elsif ($kind eq 'insert') { } 0 5 5
elsif ($kind eq 'delete') { } 1 4 5
elsif ($kind eq 'remove') { } 2 2 4
377 1794 my ( $p, $last ) = $self->_parent( $state, @seg );
378 1794 ▶ die "Pfad nicht vorhanden\n" unless ref $p;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p 0 1794 1794
379 1794 ▶ if ( ref $p eq 'HASH' ) { $p->{$last} = $self->value( $op->{value}, $args ) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 1794 0 1794
380 0 else { $p->[ $self->_index( $last, $p, 'Eintrag' ) ] = $self->value( $op->{value}, $args ) }
381 }
382 elsif ( $kind eq 'push' ) {
383 40 ▶ my $l = $self->_list_at( $state, \@seg, 1 ) or die "keine Liste\n";
Branch: 1 branch
BranchTrueFalseTotal
my $l = $self->_list_at($state, \@seg, 1) or die "keine Liste\n" 40 0 40
384 40 push @$l, $self->value( $op->{value}, $args );
385 }
386 elsif ( $kind eq 'insert' ) {
387 0 ▶ my $l = $self->_list_at( $state, \@seg, 1 ) or die "keine Liste\n";
Branch: 1 branch
BranchTrueFalseTotal
my $l = $self->_list_at($state, \@seg, 1) or die "keine Liste\n" 0 0 0
388 0 my $at = $self->_index( $self->value( $op->{at}, $args ), $l, 'Position', 1 );
389 0 splice @$l, $at, 0, $self->value( $op->{value}, $args );
390 }
391 elsif ( $kind eq 'delete' ) {
392 1 my ( $p, $last ) = $self->_parent( $state, @seg );
393 1 ▶ die "nicht vorhanden\n" unless ref $p eq 'HASH' && exists $p->{$last}
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" and exists $p->{$last} or ref $p eq "ARRAY" and $last =~ /\A\d+\z/ and $last < @$p 1 0 1
Condition33%4/12
ref $p eq "HASH" and exists $p->{$last} or ref $p eq "ARRAY" and $last =~ /\A\d+\z/ and $last < @$p
l!l&&r!l&&!r
001
ref $p eq "HASH" and exists $p->{$last}
!ll&&!rl&&r
010
ref $p eq "ARRAY" and $last =~ /\A\d+\z/ and $last < @$p
!ll&&!rl&&r
100
ref $p eq "ARRAY" and $last =~ /\A\d+\z/
!ll&&!rl&&r
100
MC/DC0%0/5
ref $p eq "HASH" and exists $p->{$last} or ref $p eq "ARRAY" and $last =~ /\A\d+\z/ and $last < @$p
ref $p eq "HASH" exists $p->{$last} ref $p eq "ARRAY" $last =~ /\A\d+\z/ $last < @$p
Truth table11%1/9
A and B or C and D and E
A= ref $p eq "HASH"B= exists $p->{$last}C= ref $p eq "ARRAY"D= $last =~ /\A\d+\z/E= $last < @$p
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
394 || ref $p eq 'ARRAY' && $last =~ /\A\d+\z/ && $last < @$p;
395 0 ▶ if ( ref $p eq 'HASH' ) { delete $p->{$last} }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 0 0 0
396 0 else { splice @$p, $last, 1 }
397 }
398 elsif ( $kind eq 'remove' ) {
399 2 ▶ my $l = $self->_list_at( $state, \@seg, 0 ) or die "keine Liste\n";
Branch: 1 branch
BranchTrueFalseTotal
my $l = $self->_list_at($state, \@seg, 0) or die "keine Liste\n" 2 0 2
400 2 my @keep = grep { !element_match( $_, $op->{where}, $args ) } @$l;
401 2 ▶ die "kein passender Eintrag\n" if @keep == @$l;
Branch: 1 branch
BranchTrueFalseTotal
if @keep == @$l 1 1 2
402 1 @$l = @keep;
403 }
404 else { # move
405 2 ▶ my $l = $self->_list_at( $state, \@seg, 0 ) or die "keine Liste\n";
Branch: 1 branch
BranchTrueFalseTotal
my $l = $self->_list_at($state, \@seg, 0) or die "keine Liste\n" 2 0 2
406 2 my $from = $self->_index( $self->value( $op->{from}, $args ), $l, 'Eintrag' );
407 1 my ( $item ) = splice @$l, $from, 1;
408 1 my $to = $self->_index( $self->value( $op->{to}, $args ), $l, 'Ziel', 1 );
409 1 splice @$l, $to, 0, $item;
410 }
411 1831 1;
412 } or $err = ( $@ =~ s/$Langertha::Holmgang::Target::LOCATION\.?\n?\z//r =~ s/\s+\z//r );
413 1839 return $err;
414 }
415  
416  
417 26351 ▶sub diff {
Subroutine diff: called CC=15 SCAR=27.1
Pod documented
418 26351 my ( $self, $a, $b, @at ) = @_;
419 26351 ▶ if ( ref $a eq 'HASH' && ref $b eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "HASH" and ref $b eq "HASH") 11137 15214 26351
Condition66%2/3
ref $a eq "HASH" and ref $b eq "HASH"
!ll&&!rl&&r
15214011137
MC/DC50%1/2
ref $a eq "HASH" and ref $b eq "HASH"
ref $a eq "HASH" ref $b eq "HASH"
Truth table66%2/3
A and B
A= ref $a eq "HASH"B= ref $b eq "HASH"
ABresult
0X0
100
111
420 11137 my %k = map { $_ => 1 } grep { !/\A_/ } keys %$a, keys %$b;
421 11137 my @out;
422 11137 for my $k ( sort keys %k ) {
423 # a new or removed hash is shown key by key
424 22244 ▶ if ( !exists $b->{$k} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $b->{$k}) { } 4 22240 22244
elsif (not exists $a->{$k}) { } 889 21351 22240
425 1 push @out, ref $a->{$k} eq 'HASH' && %{ $a->{$k} } ? $self->diff( $a->{$k}, {}, @at, $k )
426 4 ▶ : { path => [ @at, $k ], before => $a->{$k} };
Branch: 1 branch
BranchTrueFalseTotal
ref $a->{$k} eq 'HASH' && %{$a->{$k};} ? : 1 3 4
Condition66%2/3
ref $a->{$k} eq "HASH" and %{$a->{$k};}
!ll&&!rl&&r
301
MC/DC50%1/2
ref $a->{$k} eq "HASH" and %{$a->{$k};}
ref $a->{$k} eq "HASH" %{$a->{$k};}
Truth table66%2/3
A and B
A= ref $a->{$k} eq "HASH"B= %{$a->{$k};}
ABresult
0X0
100
111
427 }
428 elsif ( !exists $a->{$k} ) {
429 229 push @out, ref $b->{$k} eq 'HASH' && %{ $b->{$k} } ? $self->diff( {}, $b->{$k}, @at, $k )
430 889 ▶ : { path => [ @at, $k ], after => $b->{$k} };
Branch: 1 branch
BranchTrueFalseTotal
ref $b->{$k} eq 'HASH' && %{$b->{$k};} ? : 229 660 889
Condition66%2/3
ref $b->{$k} eq "HASH" and %{$b->{$k};}
!ll&&!rl&&r
6600229
MC/DC50%1/2
ref $b->{$k} eq "HASH" and %{$b->{$k};}
ref $b->{$k} eq "HASH" %{$b->{$k};}
Truth table66%2/3
A and B
A= ref $b->{$k} eq "HASH"B= %{$b->{$k};}
ABresult
0X0
100
111
431 }
432 21351 else { push @out, $self->diff( $a->{$k}, $b->{$k}, @at, $k ) }
433 }
434 11137 return @out;
435 }
436 15214 ▶ if ( ref $a eq 'ARRAY' && ref $b eq 'ARRAY' && @$a == @$b && grep { ref } @$a, @$b ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "ARRAY" and ref $b eq "ARRAY" and @$a == @$b and grep {ref $_;} @$a, @$b) 10 15204 15214
Condition88%8/9
ref $a eq "ARRAY" and ref $b eq "ARRAY" and @$a == @$b and grep {ref $_;} @$a, @$b
!ll&&!rl&&r
1443477010
ref $a eq "ARRAY" and ref $b eq "ARRAY" and @$a == @$b
!ll&&!rl&&r
14328106780
ref $a eq "ARRAY" and ref $b eq "ARRAY"
!ll&&!rl&&r
143280886
MC/DC75%3/4
ref $a eq "ARRAY" and ref $b eq "ARRAY" and @$a == @$b and grep {ref $_;} @$a, @$b
ref $a eq "ARRAY" ref $b eq "ARRAY" @$a == @$b grep {ref $_;} @$a, @$b
Truth table80%4/5
A and B and C and D
A= ref $a eq "ARRAY"B= ref $b eq "ARRAY"C= @$a == @$bD= grep {ref $_;} @$a, @$b
ABCDresult
0XXX0
10XX0
110X0
11100
11111
437 10 return map { $self->diff( $a->[$_], $b->[$_], @at, $_ ) } 0 .. $#$a;
438 }
439 15204 ▶ return () if $JSON->encode($a) eq $JSON->encode($b);
Branch: 1 branch
BranchTrueFalseTotal
if $JSON->encode($a) eq $JSON->encode($b) 11721 3483 15204
440 3483 return { path => \@at, before => $a, after => $b };
441 }
442  
443  
444 2314 ▶sub path_text {
Subroutine path_text: called CC=5 SCAR=16.1
Pod documented
445 2314 my ( $self, $seg ) = @_;
446 2314 my $s = '';
447 2314 for my $k ( @$seg ) {
448 5185 ▶ if ( $k =~ /[.\s"\[\]]/ || !length $k ) { $s .= '['.$JSON->encode("$k").']' }
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /[.\s"\[\]]/ or not length $k) { } 10 5175 5185
Condition100%3/3
$k =~ /[.\s"\[\]]/ or not length $k
l!l&&r!l&&!r
915175
MC/DC100%2/2
$k =~ /[.\s"\[\]]/ or not length $k
$k =~ /[.\s"\[\]]/ not length $k
Truth table100%3/3
A or not(B)
A= $k =~ /[.\s"\[\]]/B= not length $k
ABresult
1X1
011
000
449 5175 ▶ else { $s .= ( length $s ? '.' : '' ).$k }
Branch: 1 branch
BranchTrueFalseTotal
length $s ? : 2861 2314 5175
450 }
451 2314 return $s;
452 }
453  
454  
455 1928 ▶sub covered {
Subroutine covered: called CC=8 SCAR=20.8
Pod documented
456 1928 my ( $self, $seg, $allowed ) = @_;
457 1928 ALLOWED: for my $al ( @$allowed ) {
458 2083 ▶ next if @$al > @$seg;
Branch: 1 branch
BranchTrueFalseTotal
if @$al > @$seg 30 2053 2083
459 2053 for my $i ( 0 .. $#$al ) {
460 3955 ▶ next if $al->[$i] eq '*' || $al->[$i] eq $seg->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $al->[$i] eq "*" or $al->[$i] eq $seg->[$i] 3733 222 3955
Condition100%3/3
$al->[$i] eq "*" or $al->[$i] eq $seg->[$i]
l!l&&r!l&&!r
13732222
MC/DC100%2/2
$al->[$i] eq "*" or $al->[$i] eq $seg->[$i]
$al->[$i] eq "*" $al->[$i] eq $seg->[$i]
Truth table100%3/3
A or B
A= $al->[$i] eq "*"B= $al->[$i] eq $seg->[$i]
ABresult
1X1
011
000
461 222 ▶ next ALLOWED unless index( $al->[$i], '*' ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($al->[$i], "*") != -1 219 3 222
462 3 my $re = join '[^/]*', map { quotemeta } split /\*/, $al->[$i], -1;
463 3 ▶ next ALLOWED unless $seg->[$i] =~ /\A$re\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seg->[$i] =~ /\A$re\z/ 2 1 3
464 }
465 1832 return 1;
466 }
467 96 return 0;
468 }
469  
470 43 __PACKAGE__->meta->make_immutable;
471  
472 __END__
« blib/lib/Langertha/Holmgang/Sim/Linux/Logrotate.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Postfix.pm »