From b59b728e6aa73f0dedc528e62886093506f48da3 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Wed, 3 Feb 2016 21:30:32 +0000 Subject: [PATCH] Start hash parameters test (#820) --- .../lib/Lemonldap/NG/Manager/Attributes.pm | 6 +- .../Lemonldap/NG/Manager/Build/Attributes.pm | 2 + .../lib/Lemonldap/NG/Manager/Conf/Parser.pm | 90 +++++++++++-------- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Attributes.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Attributes.pm index 820d09da7..ba2faf086 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Attributes.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Attributes.pm @@ -240,7 +240,8 @@ sub attributes { 'type' => 'category' } }, - 'type' => 'catAndAppList' + 'keyTest' => qr/\w/, + 'type' => 'catAndAppList' }, 'authChoiceModules' => { 'keyTest' => qr/^(\d*)?[a-zA-Z0-9_]+$/, @@ -1408,7 +1409,8 @@ qr/^(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0- 'loa-4' => 4, 'loa-5' => 5 }, - 'type' => 'keyTextContainer' + 'keyTest' => qr/\w/, + 'type' => 'keyTextContainer' }, 'oidcServiceMetaDataAuthorizeURI' => { 'default' => 'authorize', diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Attributes.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Attributes.pm index 52ac1bf29..67e89abdc 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Attributes.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Attributes.pm @@ -300,6 +300,7 @@ sub attributes { }, applicationList => { type => 'catAndAppList', + keyTest => qr/\w/, help => 'portalmenu.html#categories_and_applications', default => { default => { catname => 'Default category', type => "category" } @@ -2103,6 +2104,7 @@ m{^(?:ldapi://[^/]*/?|\w[\w\-\.]*(?::\d{1,5})?|ldap(?:s|\+tls)?://\w[\w\-\.]*(?: }, oidcServiceMetaDataAuthnContext => { type => 'keyTextContainer', + keyTest => qr/\w/, default => { 'loa-1' => 1, 'loa-2' => 2, diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm index c583e2129..111a7a7bf 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm @@ -828,7 +828,28 @@ sub _unitTest { $res = 0; next; } + + # Hash parameters + if ( $key =~ $simpleHashKeys or $attr->{type} =~ /Container$/ ) { + $conf->{$key} //= {}; + unless ( ref $conf->{$key} eq 'HASH' ) { + push @{ $self->errors }, + { message => "$key is not a hash ref" }; + $res = 0; + next; + } + } if ( $key =~ $simpleHashKeys ) { + foreach my $k ( keys %{ $conf->{$key} } ) { + my $keyMsg = $attr->{keyMsgFail} // $type->{keyMsgFail} + // 'Bad hash key'; + $res = 0 + unless $self->_execTest( $attr->{keyTest} + // $type->{keyTest} // qr/^\w+$/, + $k, "$key/$k", $attr, $keyMsg, $conf ); + } + } + elsif ( $attr->{type} =~ /Container$/ ) { #TODO } @@ -836,53 +857,46 @@ sub _unitTest { #TODO } - elsif ( $attr->{type} =~ /Container$/ ) { - - #TODO - } else { die "Unkown type $attr->{type}" unless ( $type = $types->{ $attr->{type} } ); - my $test = $attr->{test} // $type->{test}; - my $msg = $attr->{msgFail} // $type->{msgFail}; - if ( my $ref = ref($test) ) { - if ( $ref eq 'CODE' ) { - my ( $r, $w ) = - $test->( $conf->{$key}, $conf, $attr ); - unless ($r) { - push @{ $self->errors }, - { message => "$key: " . ( $w ? $w : $msg ) }; - $res = 0; - } - elsif ($w) { - push @{ $self->warnings }, - { message => "$key: $w" }; - } - } - elsif ( $ref eq 'Regexp' ) { - die "msgFail undefined for type \"$attr->{type}\"" - unless ( defined $msg ); - unless ( $conf->{$key} =~ $test ) { - push @{ $self->errors }, - { message => "$key: $msg ($conf->{$key})" }; - $res = 0; - } - } - else { - die -"Malformed test: only regexp ref or sub are accepted (type \"$ref\")"; - } - } - else { - die -"Malformed test: only regexp ref or sub are accepted (\"$test\")"; - } + my $msg = $attr->{msgFail} // $type->{msgFail}; + $res = 0 + unless ( + $self->_execTest( + $attr->{test} // $type->{test}, + $conf->{$key}, $key, $attr, $msg, $conf + ) + ); } } } return $res; } +##@method private boolean _execTest($test, $value) +# Execute the given test with value +#@param test that can be a code-ref, or a regexp +#@return result of test +sub _execTest { + my ( $self, $test, $value, $key, $attr, $msg, $conf ) = @_; + my $ref; + die "Malformed test: only regexp ref or sub are accepted (type \"$ref\")" + unless ( $ref = ref($test) and $ref =~ /^(CODE|Regexp)$/ ); + if ( $1 eq 'CODE' ) { + my ( $r, $m ) = ( $test->( $value, $conf, $attr ) ); + push @{ $self->{ ( $r ? 'warnings' : 'error' ) } }, + { message => "$key: $m" } + if ($m); + return $r; + } + else { + my $r = $value =~ $test; + push @{ $self->errors }, { message => "$key: $msg" } unless ($r); + return $r; + } +} + ##@method private boolean _globalTest() # Launch all tests declared in Lemonldap::NG::Manager::Conf::Tests::tests() #