blib/lib/Langertha/Holmgang/Sim/Linux/RedisService.pm

filter: Statementstmt 93.2% 97 / 104 Branchbran 69.1% 47 / 68 Conditioncond 50.4% 58 / 115 MC/DCmcdc 21.0% 12 / 57 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 63.5% 227 / 357 CC 83 SCAR 47.3
CC
83
Cov
70%
cli
180.1
_listens
13.5
_conf
8.3
CRAP 112.8
« blib/lib/Langertha/Holmgang/Sim/Linux/Nginx.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Sshd.pm »
1 package Langertha::Holmgang::Sim::Linux::RedisService;
2 # ABSTRACT: Redis from the service alone (services.redis-server.running) — redis-cli connection, auth, PING, CONFIG GET and redis-server -v
3  
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 utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.001';
9  
10  
11 53 ▶sub _svc { my ($dev) = @_; return ( $dev->{services} || {} )->{'redis-server'} }
Subroutine _svc: called CC=2 SCAR=7.2
Condition50%1/2
$$dev{'services'} || {}
l!l
530
MC/DC0%0/1
$$dev{'services'} || {}
$$dev{'services'}
Truth table50%1/2
A
A= $$dev{'services'}
Aresult
11
00
12  
13  
14 14 ▶sub installed { return _svc( $_[0] ) ? 1 : 0 }
Subroutine installed: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_svc($_[0]) ? : 13 1 14
15  
16 28 ▶sub _conf {
Subroutine _conf: called CC=7 SCAR=21.2
17 28 my ($dev) = @_;
18 28 ▶ my $t = ( _svc($dev) || {} )->{running} // '';
Condition50%2/4
(_svc($dev) || {})->{"running"} // ""
l!l
280
_svc($dev) || {}
l!l
280
MC/DC0%0/1
(_svc($dev) || {})->{"running"} // ""
_svc($dev)
Truth table50%1/2
A
A= _svc($dev)
Aresult
11
00
19 28 my %c;
20 28 for my $l ( split /\n/, $t ) {
21 76 $l =~ s/\A\s+//;
22 76 ▶ next if $l eq '' || $l =~ /\A#/;
Branch: 1 branch
BranchTrueFalseTotal
if $l eq "" or $l =~ /\A#/ 0 76 76
Condition33%1/3
$l eq "" or $l =~ /\A#/
l!l&&r!l&&!r
0076
MC/DC0%0/2
$l eq "" or $l =~ /\A#/
$l eq "" $l =~ /\A#/
Truth table33%1/3
A or B
A= $l eq ""B= $l =~ /\A#/
ABresult
1X1
011
000
23 76 ▶ my ( $k, $v ) = $l =~ /\A(\S+)\s*(.*?)\s*\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $l =~ /\A(\S+)\s*(.*?)\s*\z/ or next 76 0 76
24 76 $c{ lc $k } = $v; # die letzte Zeile gewinnt (wie Redis)
25 }
26 28 return \%c;
27 }
28  
29 11 ▶sub _addrs {
Subroutine _addrs: called CC=4 SCAR=14.0
30 11 my ($dev) = @_;
31 11 my @a = ( '127.0.0.1', '::1' );
32 11 ▶ for my $i ( values %{ $dev->{interfaces} || {} } ) { push @a, map { s{/\d+\z}{}r } @{ $i->{addr} || [] } }
Condition50%2/4
$$dev{'interfaces'} || {}
l!l
110
$$i{'addr'} || []
l!l
110
MC/DC0%0/1
$$dev{'interfaces'} || {}
$$dev{'interfaces'}
MC/DC0%0/1
$$i{'addr'} || []
$$i{'addr'}
Truth table50%1/2
A
A= $$dev{'interfaces'}
Aresult
11
00
Truth table50%1/2
A
A= $$i{'addr'}
Aresult
11
00
33 11 return @a;
34 }
35  
36 # Lauscht Redis auf $host?
37 10 ▶sub _listens {
Subroutine _listens: called CC=11 SCAR=26.0
38 10 my ( $dev, $host ) = @_;
39 10 my $c = _conf($dev);
40 10 my $v6 = $host =~ /:/;
41 10 ▶ return 1 unless defined $c->{bind};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$c{"bind"} 0 10 10
42 10 for my $b ( split ' ', $c->{bind} ) {
43 12 $b =~ s/\A-//;
44 12 ▶ return 1 if $b eq '*' && !$v6 || $b eq '0.0.0.0' && !$v6 || ( $b eq '::*' || $b eq '::' ) && $v6;
Branch: 1 branch
BranchTrueFalseTotal
if $b eq "*" and not $v6 or $b eq "0.0.0.0" and not $v6 or $b eq "::*" || $b eq "::" and $v6 6 6 12
Condition55%10/18
$b eq "*" and not $v6 or $b eq "0.0.0.0" and not $v6 or $b eq "::*" || $b eq "::" and $v6
l!l&&r!l&&!r
606
$b eq "*" and not $v6 or $b eq "0.0.0.0" and not $v6
l!l&&r!l&&!r
066
$b eq "*" and not $v6
!ll&&!rl&&r
1200
$b eq "0.0.0.0" and not $v6
!ll&&!rl&&r
516
$b eq "::*" || $b eq "::" and $v6
!ll&&!rl&&r
600
$b eq "::*" or $b eq "::"
l!l&&r!l&&!r
006
MC/DC28%2/7
$b eq "*" and not $v6 or $b eq "0.0.0.0" and not $v6 or $b eq "::*" || $b eq "::" and $v6
$b eq "*" not $v6 $b eq "0.0.0.0" not $v6 $b eq "::*" $b eq "::" $v6
Truth table13%3/23
A and not(B) or C and not(D) or E or F and G
A= $b eq "*"B= not $v6C= $b eq "0.0.0.0"D= not $v6E= $b eq "::*"F= $b eq "::"G= $v6
ABCDEFGresult
11XXXXX1
0X11XXX1
1011XXX1
0X0X1X11
0X0X0111
0X101X11
0X100111
100X1X11
100X0111
10101X11
10100111
0X0X00X0
0X0X1X00
0X0X0100
0X1000X0
0X101X00
0X100100
100X00X0
100X1X00
100X0100
101000X0
10101X00
10100100
45 6 ▶ return 1 if $b eq $host;
Branch: 1 branch
BranchTrueFalseTotal
if $b eq $host 2 4 6
46 }
47 2 return 0;
48 }
49  
50  
51 11 ▶sub cli {
Subroutine cli: called CC=56 SCAR=51.9
Pod documented
52 11 my ( $dev, $av, $host_name ) = @_;
53 11 my @a = @$av;
54 11 my ( $h, $port, $pw, $nowarn, @cmd ) = ( '127.0.0.1', 6379 );
55 11 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 31 0 31
56 31 my $x = shift @a;
57 31 ▶ if ( $x eq '-h' ) { $h = shift(@a) // '' }
Branch: 8 branches
BranchTrueFalseTotal
if ($x eq '-h') { } 6 25 31
elsif ($x eq '-p') { } 1 24 25
elsif ($x eq '-a' or $x eq '--pass') { } 7 17 24
elsif ($x eq '--user' or $x eq '-n' or $x eq '-u') { } 0 17 17
elsif ($x eq '--no-auth-warning') { } 6 11 17
elsif ($x eq '--raw' or $x eq '--no-raw' or $x eq '-c') { } 0 11 11
elsif ($x eq '-v' or $x eq '--version') { } 0 11 11
elsif ($x =~ /\A-/ and not @cmd) { } 0 11 11
Condition50%1/2
shift @a // ""
l!l
60
MC/DC0%0/1
shift @a // ""
shift @a
Truth table50%1/2
A
A= shift @a
Aresult
11
00
58 1 ▶ elsif ( $x eq '-p' ) { $port = shift(@a) // '' }
Condition50%1/2
shift @a // ""
l!l
10
MC/DC0%0/1
shift @a // ""
shift @a
Truth table50%1/2
A
A= shift @a
Aresult
11
00
59 7 ▶ elsif ( $x eq '-a' || $x eq '--pass' ) { $pw = shift(@a) // '' }
Condition60%3/5
$x eq "-a" or $x eq "--pass"
l!l&&r!l&&!r
7017
shift @a // ""
l!l
70
MC/DC50%1/2
$x eq "-a" or $x eq "--pass"
$x eq "-a" $x eq "--pass"
MC/DC0%0/1
shift @a // ""
shift @a
Truth table66%2/3
A or B
A= $x eq "-a"B= $x eq "--pass"
ABresult
1X1
011
000
Truth table50%1/2
A
A= shift @a
Aresult
11
00
60 0 ▶ elsif ( $x eq '--user' || $x eq '-n' || $x eq '-u' ) { shift @a }
Condition33%2/6
$x eq "--user" or $x eq "-n" or $x eq "-u"
l!l&&r!l&&!r
0017
$x eq "--user" or $x eq "-n"
l!l&&r!l&&!r
0017
MC/DC0%0/3
$x eq "--user" or $x eq "-n" or $x eq "-u"
$x eq "--user" $x eq "-n" $x eq "-u"
Truth table25%1/4
A or B or C
A= $x eq "--user"B= $x eq "-n"C= $x eq "-u"
ABCresult
1XX1
01X1
0011
0000
61 6 elsif ( $x eq '--no-auth-warning' ) { $nowarn = 1 }
62 ▶ elsif ( $x eq '--raw' || $x eq '--no-raw' || $x eq '-c' ) { }
Condition33%2/6
$x eq "--raw" or $x eq "--no-raw" or $x eq "-c"
l!l&&r!l&&!r
0011
$x eq "--raw" or $x eq "--no-raw"
l!l&&r!l&&!r
0011
MC/DC0%0/3
$x eq "--raw" or $x eq "--no-raw" or $x eq "-c"
$x eq "--raw" $x eq "--no-raw" $x eq "-c"
Truth table25%1/4
A or B or C
A= $x eq "--raw"B= $x eq "--no-raw"C= $x eq "-c"
ABCresult
1XX1
01X1
0011
0000
63 0 ▶ elsif ( $x eq '-v' || $x eq '--version' ) { return ( 'redis-cli 7.0.15', 1 ) }
Condition33%1/3
$x eq "-v" or $x eq "--version"
l!l&&r!l&&!r
0011
MC/DC0%0/2
$x eq "-v" or $x eq "--version"
$x eq "-v" $x eq "--version"
Truth table33%1/3
A or B
A= $x eq "-v"B= $x eq "--version"
ABresult
1X1
011
000
64 0 ▶ elsif ( $x =~ /\A-/ && !@cmd ) { return ( '', 0, join( ' ', 'redis-cli', @$av ) ) }
Condition33%1/3
$x =~ /\A-/ and not @cmd
!ll&&!rl&&r
1100
MC/DC0%0/2
$x =~ /\A-/ and not @cmd
$x =~ /\A-/ not @cmd
Truth table33%1/3
A and not(B)
A= $x =~ /\A-/B= not @cmd
ABresult
0X0
100
111
65 11 else { push @cmd, $x, @a; last }
66 }
67 11 my @out;
68 11 ▶ push @out, "Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe." if defined $pw && !$nowarn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pw and not $nowarn 1 10 11
Condition100%3/3
defined $pw and not $nowarn
!ll&&!rl&&r
461
MC/DC100%2/2
defined $pw and not $nowarn
defined $pw not $nowarn
Truth table100%3/3
A and not(B)
A= defined $pwB= not $nowarn
ABresult
0X0
100
111
69 11 ▶ $h = '127.0.0.1' if $h eq 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
if $h eq "localhost" 0 11 11
70 11 ▶ $h = '::1' if $h eq 'ip6-localhost';
Branch: 1 branch
BranchTrueFalseTotal
if $h eq "ip6-localhost" 0 11 11
71 11 ▶ my $svc = _svc($dev) || {};
Condition50%1/2
_svc($dev) || {}
l!l
110
MC/DC0%0/1
_svc($dev) || {}
_svc($dev)
Truth table50%1/2
A
A= _svc($dev)
Aresult
11
00
72 11 my $mine = grep { $_ eq $h } _addrs($dev);
73 11 ▶ if ( !$svc->{active} || "$port" ne ( _conf($dev)->{port} // '6379' ) || !$mine || !_listens( $dev, $h ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $$svc{"active"} or "$port" ne (_conf($dev)->{"port"} // 6379) or not $mine or not _listens($dev, $h)) 3 8 11
Condition72%8/11
not $$svc{"active"} or "$port" ne (_conf($dev)->{"port"} // 6379) or not $mine or not _listens($dev, $h)
l!l&&r!l&&!r
128
not $$svc{"active"} or "$port" ne (_conf($dev)->{"port"} // 6379) or not $mine
l!l&&r!l&&!r
1010
not $$svc{"active"} or "$port" ne (_conf($dev)->{"port"} // 6379)
l!l&&r!l&&!r
1010
_conf($dev)->{"port"} // 6379
l!l
010
MC/DC50%2/4
not $$svc{"active"} or "$port" ne (_conf($dev)->{"port"} // 6379) or not $mine or not _listens($dev, $h)
not $$svc{"active"} "$port" ne (_conf($dev)->{"port"} // 6379) not $mine not _listens($dev, $h)
MC/DC0%0/1
_conf($dev)->{"port"} // 6379
_conf($dev)->{"port"}
Truth table60%3/5
not(A) or B or not(C) or not(D)
A= not $$svc{"active"}B= "$port" ne (_conf($dev)->{"port"} // 6379)C= not $mineD= not _listens($dev, $h)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table50%1/2
A
A= _conf($dev)->{"port"}
Aresult
11
00
74 3 ▶ my $hp = $h =~ /:/ ? "$h:$port" : "$h:$port";
Branch: 1 branch
BranchTrueFalseTotal
$h =~ /:/ ? : 1 2 3
75 3 return ( join( "\n", @out, "Could not connect to Redis at $hp: Connection refused" ), 0 );
76 }
77 8 ▶ return ( join( "\n", @out ), 0, join( ' ', 'redis-cli', @$av ) ) unless @cmd; # interaktiv
Branch: 1 branch
BranchTrueFalseTotal
unless @cmd 0 8 8
78 8 my $c = _conf($dev);
79 8 ▶ my $need = defined $c->{requirepass} && length $c->{requirepass};
Condition33%1/3
defined $$c{'requirepass'} && length $$c{'requirepass'}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $$c{'requirepass'} && length $$c{'requirepass'}
defined $$c{'requirepass'} length $$c{'requirepass'}
Truth table33%1/3
A && B
A= defined $$c{'requirepass'}B= length $$c{'requirepass'}
ABresult
0X0
100
111
80 8 ▶ ( my $want = $c->{requirepass} // '' ) =~ s/\A"(.*)"\z/$1/;
Condition50%1/2
$$c{"requirepass"} // ""
l!l
80
MC/DC0%0/1
$$c{"requirepass"} // ""
$$c{"requirepass"}
Truth table50%1/2
A
A= $$c{"requirepass"}
Aresult
11
00
81 8 my $authed = !$need;
82 8 ▶ if ( $need && defined $pw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need and defined $pw) 7 1 8
Condition66%2/3
$need and defined $pw
!ll&&!rl&&r
017
MC/DC50%1/2
$need and defined $pw
$need defined $pw
Truth table66%2/3
A and B
A= $needB= defined $pw
ABresult
0X0
100
111
83 7 ▶ if ( $pw eq $want ) { $authed = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($pw eq $want) { } 6 1 7
84 1 else { push @out, 'AUTH failed: WRONGPASS invalid username-password pair or user is disabled.' }
85 }
86 8 ▶ my $loop = $h eq '127.0.0.1' || $h eq '::1';
Condition100%3/3
$h eq '127.0.0.1' || $h eq '::1'
l!l&&r!l&&!r
512
MC/DC100%2/2
$h eq '127.0.0.1' || $h eq '::1'
$h eq '127.0.0.1' $h eq '::1'
Truth table100%3/3
A || B
A= $h eq '127.0.0.1'B= $h eq '::1'
ABresult
1X1
011
000
87 8 ▶ if ( !$need && !$loop && ( $c->{'protected-mode'} // 'yes' ) eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $need || $loop and ($$c{"protected-mode"} // "yes") eq "yes") 0 8 8
Condition25%2/8
not $need || $loop and ($$c{"protected-mode"} // "yes") eq "yes"
!ll&&!rl&&r
800
$need || $loop
l!l&&r!l&&!r
800
$$c{"protected-mode"} // "yes"
l!l
00
MC/DC0%0/3
not $need || $loop and ($$c{"protected-mode"} // "yes") eq "yes"
$need $loop $$c{"protected-mode"}
Truth table25%1/4
not(A || B) and C
A= $needB= $loopC= $$c{"protected-mode"}
ABCresult
1XX0
01X0
0000
0011
88 0 return ( join( "\n", @out, 'DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user.' ), 0 );
89 }
90 8 my $verb = uc $cmd[0];
91 8 ▶ if ( $verb eq 'AUTH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq "AUTH") 0 8 8
92 0 ▶ my $p = $cmd[-1] // '';
Condition0%0/2
$cmd[-1] // ""
l!l
00
MC/DC0%0/1
$cmd[-1] // ""
$cmd[-1]
Truth table0%0/2
A
A= $cmd[-1]
Aresult
11
00
93 0 ▶ return ( join( "\n", @out, $need && $p eq $want ? 'OK' : $need ? 'WRONGPASS invalid username-password pair or user is disabled.'
Branch: 3 branches
BranchTrueFalseTotal
$need && $p eq $want ? : 0 0 0
$need ? : 0 0 0
$p eq $want ? : 0 0 0
Condition0%0/3
$need and $p eq $want
!ll&&!rl&&r
000
MC/DC0%0/2
$need and $p eq $want
$need $p eq $want
Truth table0%0/3
A and B
A= $needB= $p eq $want
ABresult
0X0
100
111
94 : 'ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?' ), $p eq $want ? 1 : 0 );
95 }
96 8 ▶ return ( join( "\n", @out, 'NOAUTH Authentication required.' ), 0 ) unless $authed;
Branch: 1 branch
BranchTrueFalseTotal
unless $authed 2 6 8
97 6 ▶ return ( join( "\n", @out, @cmd > 1 ? $cmd[1] : 'PONG' ), 1 ) if $verb eq 'PING';
Branch: 2 branches
BranchTrueFalseTotal
if $verb eq "PING" 4 2 6
@cmd > 1 ? : 0 4 4
98 2 ▶ if ( $verb eq 'CONFIG' && uc( $cmd[1] // '' ) eq 'GET' && defined $cmd[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq "CONFIG" and uc($cmd[1] // "") eq "GET" and defined $cmd[2]) 1 1 2
Condition62%5/8
$verb eq "CONFIG" and uc($cmd[1] // "") eq "GET" and defined $cmd[2]
!ll&&!rl&&r
101
$verb eq "CONFIG" and uc($cmd[1] // "") eq "GET"
!ll&&!rl&&r
101
$cmd[1] // ""
l!l
10
MC/DC33%1/3
$verb eq "CONFIG" and uc($cmd[1] // "") eq "GET" and defined $cmd[2]
$verb eq "CONFIG" $cmd[1] defined $cmd[2]
Truth table50%2/4
A and B and C
A= $verb eq "CONFIG"B= $cmd[1]C= defined $cmd[2]
ABCresult
0XX0
10X0
1100
1111
99 1 ( my $re = quotemeta lc $cmd[2] ) =~ s/\\\*/.*/g;
100 1 my %all = ( bind => '* -::*', port => '6379', 'protected-mode' => 'yes', requirepass => '', %$c );
101 1 for my $k ( sort grep {/\A$re\z/} keys %all ) {
102 1 ( my $v = $all{$k} ) =~ s/\A"(.*)"\z/$1/;
103 1 push @out, $k, $v;
104 }
105 1 return ( join( "\n", @out ), 1 );
106 }
107 1 return ( join( "\n", @out ), 0, join( ' ', 'redis-cli', @$av ) );
108 }
109  
110  
111 2 ▶sub server {
Subroutine server: called CC=7 SCAR=20.4
Pod documented
112 2 my ( $dev, $av ) = @_;
113 2 my @a = @$av;
114 2 ▶ return ( 'Redis server v=7.0.15 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=c4d1f6a1d3b5a9e2', 1 )
Branch: 1 branch
BranchTrueFalseTotal
if @a and $a[0] eq "-v" || $a[0] eq "--version" 1 1 2
Condition66%4/6
@a and $a[0] eq "-v" || $a[0] eq "--version"
!ll&&!rl&&r
011
$a[0] eq "-v" or $a[0] eq "--version"
l!l&&r!l&&!r
011
MC/DC33%1/3
@a and $a[0] eq "-v" || $a[0] eq "--version"
@a $a[0] eq "-v" $a[0] eq "--version"
Truth table50%2/4
A and B or C
A= @aB= $a[0] eq "-v"C= $a[0] eq "--version"
ABCresult
0XX0
1000
11X1
1011
115 if @a && ( $a[0] eq '-v' || $a[0] eq '--version' );
116 1 my @opt = grep {/\A--/} @a;
117 1 my %directive = map { $_ => 1 } qw( bind port protected-mode requirepass daemonize supervised loglevel logfile dir save
118 dbfilename maxmemory maxmemory-policy maxclients databases timeout tcp-keepalive tcp-backlog pidfile appendonly );
119 1 for my $o (@opt) {
120 1 ( my $d = $o ) =~ s/\A--//;
121 1 ▶ next if $directive{$d} || $d eq 'test-memory';
Branch: 1 branch
BranchTrueFalseTotal
if $directive{$d} or $d eq "test-memory" 0 1 1
Condition33%1/3
$directive{$d} or $d eq "test-memory"
l!l&&r!l&&!r
001
MC/DC0%0/2
$directive{$d} or $d eq "test-memory"
$directive{$d} $d eq "test-memory"
Truth table33%1/3
A or B
A= $directive{$d}B= $d eq "test-memory"
ABresult
1X1
011
000
122 1 return ( join( "\n", '', '*** FATAL CONFIG FILE ERROR (Redis 7.0.15) ***', 'Reading the configuration file, at line 2', ">>> '$d'",
123 'Bad directive or wrong number of arguments' ), 0 );
124 }
125 0 return ( '', 0, join( ' ', 'redis-server', @$av ) );
126 }
127  
128 2 1;
129  
130 __END__
« blib/lib/Langertha/Holmgang/Sim/Linux/Nginx.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Sshd.pm »