blib/lib/Lemonldap/NG/Common/Combination/Parser.pm

filter: Statementstmt 90.3% 112 / 124 Branchbran 61.6% 37 / 60 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 7 totaltotal 74.8% 170 / 227 CC 39 SCAR 20.2
CC
39
Cov
79%
__ANON__
16.8
parse
11.1
__ANON__
8.8
CRAP 7.6
« blib/lib/Lemonldap/NG/Common/Apache/Session.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Backends/_DBI.pm »
1 package Lemonldap::NG::Common::Combination::Parser;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use constant PE_OK => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '2.0.14';
9  
10 # Handle "if then else" (used during init)
11 # return a sub that can be called with ($req) to get a [array] of combination
12 #
13 # During auth, these combinations represents "or" (like Multi)
14 # Each combination is a [authSub,userSub] called like this:
15 # $authSub->('authenticate',$req)
16 # This means that the 'authenticate' method of the real auth module will be
17 # called with $req
18  
19 51 ▶sub parse {
Subroutine parse: called CC=9 SCAR=24.1
Pod undocumented
20 51 my ( $self, $moduleList, $expr ) = @_;
21  
22 51 my $sub = '';
23 51 my $rest = $expr;
24 51 ▶ if ( $rest =~ s/^\s*if\s*\(// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/^\s*if\s*\(//) { } 21 30 51
25 21 my ( $cond, $then, $else );
26 21 ( $cond, $rest ) = $self->findB( $rest, ')' );
27 21 ▶ unless ( length $cond ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $cond) 0 21 21
28 0 die('Bad combination: unmatched bracket');
29 }
30 21 ▶ unless ( $rest =~ s/^\s*\bthen\b\s*// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rest =~ s/^\s*\bthen\b\s*//) 0 21 21
31 0 die('Bad combination: missing "then"');
32 }
33 21 ▶ if ( $rest =~ /(.*?)\s*\belse\b\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /(.*?)\s*\belse\b\s*(.*)$/) { } 21 0 21
34 21 ( $then, $else ) = ( $1, $2 );
35 }
36 else {
37 0 die('Bad combination: missing "else"');
38 }
39 21 ▶ unless ($then) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($then) 0 21 21
40 0 die('Bad combination: missing "then" content');
41 }
42 21 ▶ unless ($else) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($else) 0 21 21
43 0 die('Bad combination: missing "else" content');
44 }
45  
46 21 $cond = $self->buildSub($cond);
47 21 $then = $self->parseOr( $moduleList, $then );
48 20 $else = $self->parse( $moduleList, $else );
49 20 ▶ unless ( $then and $else ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($then and $else) 0 20 20
Condition33%1/3
$then and $else
!ll&&!rl&&r
0020
MC/DC0%0/2
$then and $else
$then $else
Truth table33%1/3
A and B
A= $thenB= $else
ABresult
0X0
100
111
50 0 die('Bad combination: bad then or else');
51 }
52 20 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
53 20 my ($env) = @_;
54 20 ▶ return ( $cond->($env) ? $then : $else->($env) );
Branch: 1 branch
BranchTrueFalseTotal
&$cond($env) ? : 9 11 20
55 20 };
56 }
57 else {
58 30 my $res = $self->parseOr( $moduleList, $rest );
59 29 ▶ return sub { $res };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61 }
62  
63 # Internal request to manage "or" boolean expr.
64 # Returns [ [authSub,userSub], [authSub,userSub] ] array
65 51 ▶sub parseOr {
Subroutine parseOr: called CC=2 SCAR=6.9
Pod undocumented
66 51 my ( $self, $moduleList, $expr ) = @_;
67 51 my @res;
68 51 foreach my $part ( split /\s+or\s+/, $expr ) {
69 57 push @res, $self->parseAnd( $moduleList, $part );
70 }
71 49 return \@res;
72 }
73  
74 # Internal request to manage "and" boolean expr
75 # Returns [authSub,userSub] array
76 57 ▶sub parseAnd {
Subroutine parseAnd: called CC=4 SCAR=14.7
Pod undocumented
77 57 my ( $self, $moduleList, $expr ) = @_;
78 57 ▶ if ( $expr =~ /\]\s*and\s*\[/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\]\s*and\s*\[/) { } 2 55 57
79 2 my @mod = ( [], [] );
80 2 foreach my $part ( split /\s*and\s*/, $expr ) {
81 4 my $tmp = $self->parseBlock( $moduleList, $part );
82 4 push @{ $mod[0] }, $tmp->[0];
83 4 push @{ $mod[1] }, $tmp->[1];
84 }
85 2 my @res;
86 2 foreach my $type (@mod) {
87 2 ▶ push @res, sub {
Subroutine __ANON__: called CC=9 SCAR=28.2
88 2 my %str;
89 2 foreach my $obj (@$type) {
90 4 my ( $r, $name ) = $obj->(@_);
91  
92 # Case "string" (form type)
93 4 ▶ if ( $r && $r & ~$r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and $r & ~$r) { } 0 4 4
Condition33%1/3
$r and $r & ~$r
!ll&&!rl&&r
400
MC/DC0%0/2
$r and $r & ~$r
$r $r & ~$r
Truth table33%1/3
A and B
A= $rB= $r & ~$r
ABresult
0X0
100
111
94 0 $str{$r}++;
95 }
96 else {
97 4 ▶ return ( wantarray ? ( $r, $name ) : $r )
Branch: 2 branches
BranchTrueFalseTotal
unless not $r or $r == 0 0 4 4
wantarray ? : 0 0 0
Condition33%1/3
not $r or $r == 0
l!l&&r!l&&!r
400
MC/DC0%0/2
not $r or $r == 0
not $r $r == 0
Truth table33%1/3
not(A) or B
A= not $rB= $r == 0
ABresult
1X1
011
000
98 unless ( !$r || $r == PE_OK );
99 }
100 }
101 2 ▶ my $res = %str ? join( ',', keys %str ) : PE_OK;
Branch: 1 branch
BranchTrueFalseTotal
%str ? : 0 2 2
102 2 ▶ return wantarray ? ( $res, $expr ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
103 4 };
104 }
105 2 return \@res;
106 }
107 else {
108 55 return $self->parseBlock( $moduleList, $expr );
109 }
110 }
111  
112 # Internal method to parse [AuthModule,UserModule] expr
113 # Returns [authSub,userSub] array
114 59 ▶sub parseBlock {
Subroutine parseBlock: called CC=3 SCAR=11.0
Pod undocumented
115 59 my ( $self, $moduleList, $expr ) = @_;
116 59 ▶ if ( $expr =~ /^\s*\[(.*?)\s*(?:,\s*(.*?))?\s*\]\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\s*\[(.*?)\s*(?:,\s*(.*?))?\s*\]\s*$/) { } 57 2 59
117 57 ▶ my @res = ( $1, $2 || $1 );
Condition66%2/3
$2 || $1
l!l&&r!l&&!r
5160
MC/DC0%0/2
$2 || $1
$2 $1
Truth table66%2/3
A || B
A= $2B= $1
ABresult
1X1
011
000
118 57 @res = (
119 $self->parseMod( $moduleList, 0, $res[0] ),
120 $self->parseMod( $moduleList, 1, $res[1] )
121 );
122 57 return \@res;
123 }
124 else {
125 2 die "Bad expression: $expr";
126 }
127 }
128  
129 # Internal method to parse auth or userDB expr
130 # These expressions can be "LDAP" or "LDAP and DBI"
131 # Return sub
132 114 ▶sub parseMod {
Subroutine parseMod: called CC=3 SCAR=11.2
Pod undocumented
133 114 my ( $self, $moduleList, $type, $expr ) = @_;
134 114 my @list = split( /\s+and\s+/, $expr );
135 my @mods = map {
136 114 die "Undeclared module $_"
137 115 ▶ unless ( $moduleList->{$_}->[$type] );
Branch: 1 branch
BranchTrueFalseTotal
unless $moduleList->{$_}[$type] 0 115 115
138 115 $moduleList->{$_}->[$type]
139 } @list;
140 114 ▶ if ( @mods == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mods == 1) 113 1 114
141 113 my ($m) = @mods;
142 26 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
143 26 my $sub = shift;
144 26 my $res = $m->$sub(@_);
145 26 ▶ return wantarray ? ( $res, $expr ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 22 26
146 113 };
147 }
148 1 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
149 1 my $sub = shift;
150 1 my %str;
151 1 ▶ for ( my $i = 0 ; $i < @list ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @list) 2 1 3
152 2 my $res = $mods[$i]->$sub(@_);
153  
154 # Case "string" (form type)
155 2 ▶ if ( $res & ~$res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res & ~$res) { } 0 2 2
156 0 $str{$res}++;
157 }
158 else {
159 2 ▶ return ( wantarray ? ( $res, $list[$i] ) : $res )
Branch: 2 branches
BranchTrueFalseTotal
unless $res == 0 0 2 2
wantarray ? : 0 0 0
160 unless ( $res == PE_OK );
161 }
162 }
163 1 ▶ my $res = %str ? join( ',', keys %str ) : PE_OK;
Branch: 1 branch
BranchTrueFalseTotal
%str ? : 0 1 1
164 1 ▶ return wantarray ? ( $res, $expr ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
165 1 };
166 }
167  
168 # Internal request to find brackets
169 25 ▶sub findB {
Subroutine findB: called CC=6 SCAR=18.5
Pod undocumented
170 25 my ( $self, $expr, $char ) = @_;
171 25 my $res;
172 25 my @chars = split //, $expr;
173 25 ▶ while (@chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars) 88 0 88
174 88 my $c = shift @chars;
175 88 ▶ if ( $c eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 88 88
176 0 $res .= $c . shift(@chars);
177 0 next;
178 }
179 88 ▶ if ( $c eq $char ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $char) 25 63 88
180 25 my $rest = join( '', @chars );
181 25 $res =~ s/^\s*(.*?)\s*/$1/;
182 25 $rest =~ s/^\s*(.*?)\s*/$1/;
183 25 return ( $res, $rest );
184 }
185 63 ▶ if ( $c =~ /^(?:\(|\{|\[|'|")$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^(?:\(|\{|\[|'|")$/) 4 59 63
186 my $wanted = {
187 '(' => ')',
188 '{' => '}',
189 '[' => ']',
190 "'" => "'",
191 '"' => '"'
192 4 }->{$c};
193 4 my ( $m, $rest ) =
194 $self->findB( join( '', @chars ), $wanted );
195 4 ▶ unless ( length $m ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $m) 0 4 4
196 0 die("Bad combination: unmatched $c");
197 }
198 4 $res .= "$c$m$wanted";
199 4 @chars = split //, $rest;
200 4 next;
201 }
202 59 $res .= $c;
203 }
204 0 return undef;
205 }
206  
207 # Compiles condition into sub
208 21 ▶sub buildSub {
Subroutine buildSub: called CC=2 SCAR=7.0
Pod undocumented
209 21 my ( $self, $cond ) = @_;
210 21 my $safe = Safe->new;
211 21 my $res = $safe->reval("sub{my(\$env)=\@_;local *_;return ($cond)}");
212 21 ▶ die "Bad condition $cond: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 21 21
213 21 return $res;
214 }
215  
216 1 1;
« blib/lib/Lemonldap/NG/Common/Apache/Session.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Backends/_DBI.pm »