lemonldap-ng/lemonldap-ng-manager/t/80-attributes.t

207 lines
5.6 KiB
Perl
Raw Normal View History

# Verify that all attributes are positionned in tree and that all leaf
# correspond to an attribute. Verify also that attributes and leafs are uniq
use strict;
use Test::More;
2018-05-07 18:01:52 +02:00
use Data::Dumper;
# CONSTANTS
# Node names used more than one time
my $usedMoreThanOneTimeInTree = qr/^(?:
remoteCookieName
)$/x;
# Attributes not managed in web interface
2016-01-06 23:03:46 +01:00
my @notManagedAttributes = (
# Complex nodes
'samlSPMetaDataOptions', 'samlIDPMetaDataOptions', 'oidcRPMetaDataOptions',
'oidcOPMetaDataOptions', 'casSrvMetaDataOptions', 'casAppMetaDataOptions',
2018-03-08 20:43:50 +01:00
'vhostOptions',
# Second factor engine, lists of 2F modules and other parameters
'sfEngine', 'available2FSelfRegistration', 'available2F', 'max2FDevices',
'max2FDevicesNameLength',
2016-01-06 23:03:46 +01:00
# Handlers
'handlerInternalCache', 'handlerServiceTokenTTL',
2016-01-06 23:03:46 +01:00
# Metadatas (added by manager itself)
'cfgAuthor', 'cfgAuthorIP', 'cfgNum', 'cfgDate', 'cfgLog', 'cfgVersion',
2016-01-06 23:03:46 +01:00
# HTML template parameter (for PSGI) (must be set in lemonldap-ng.ini)
'staticPrefix',
# Loggers
'log4perlConfFile', 'userSyslogFacility', 'logger', 'sentryDsn',
'syslogFacility', 'userLogger', 'logLevel',
# Plugins parameters
2022-02-16 17:43:29 +01:00
'notificationsMaxRetrieve', 'persistentSessionAttributes',
2016-01-06 23:03:46 +01:00
# PSGI/CGI protection (must be set in lemonldap-ng.ini)
'protection',
2018-03-09 18:45:05 +01:00
# SecureToken handler
'secureTokenAllowOnError', 'secureTokenAttribute', 'secureTokenExpiration',
2022-02-16 17:43:29 +01:00
'secureTokenHeader', 'secureTokenMemcachedServers', 'secureTokenUrls',
2018-03-09 18:45:05 +01:00
# Sessions and OTT storage
'configStorage', 'localStorageOptions', 'localStorage',
'forceGlobalStorageUpgradeOTT', 'forceGlobalStorageIssuerOTT',
2019-04-01 14:52:23 +02:00
2019-03-14 16:39:49 +01:00
# Viewer
2019-04-01 14:52:23 +02:00
'viewerHiddenKeys', 'viewerAllowBrowser', 'viewerAllowDiff',
2018-03-09 18:45:05 +01:00
# Zimbra handler
'zimbraAccountKey', 'zimbraBy', 'zimbraPreAuthKey', 'zimbraSsoUrl',
'zimbraUrl',
2018-03-09 18:45:05 +01:00
# Other ini-only prms
2020-05-24 00:04:33 +02:00
'checkTime', 'status', 'soapProxyUrn',
'impersonationPrefix', 'pdataDomain',
'mySessionAuthorizedRWKeys', 'contextSwitchingPrefix'
);
# Words used either as attribute name and node title
my $doubleUsage = qr/^(?:
samlSPMetaDataOptions|
samlIDPMetaDataOptions|
oidcRPMetaDataOptions|
oidcOPMetaDataOptions|
casSrvMetaDataOptions|
casAppMetaDataOptions|
vhostOptions
)$/x;
# TESTS
2016-01-06 23:03:46 +01:00
# 1 - Collect attributes
# Attributes.pm is parsed with open() and not loaded to detect double entries
ok( open( F, 'lib/Lemonldap/NG/Manager/Build/Attributes.pm' ),
'open attributes file' );
my $count = 1;
while ( <F> !~ /sub\s+attributes/ ) { 1 }
my ( %h, %h2 );
while (<F>) {
next unless /^\s{8}["']?(\w+)/;
my $attr = $1;
$h{$attr}++;
ok( $h{$attr} == 1, "$attr is uniq" );
$count++;
}
close F;
2016-01-06 23:03:46 +01:00
# 2 - Parse Tree.pm
use_ok('Lemonldap::NG::Manager::Build::Tree');
my $tree;
ok( $tree = Lemonldap::NG::Manager::Build::Tree::tree(), 'Get tree' );
$count += 3;
scanTree($tree);
2016-01-06 23:03:46 +01:00
# 3 - Parse CTrees.pm
use_ok('Lemonldap::NG::Manager::Build::CTrees');
ok( $tree = Lemonldap::NG::Manager::Build::CTrees::cTrees(),
'Get conditional tree' );
$count++;
foreach my $t ( values %$tree ) {
scanTree($t);
}
2016-01-06 23:03:46 +01:00
# 4 - Check that each leaf correspond to an attribute
foreach ( keys %h2 ) {
s/^\*//;
ok( defined( $h{$_} ), "Leaf $_ exists in attributes" );
delete $h{$_};
$count++;
}
2016-01-06 23:03:46 +01:00
# 5 - Check that attributes that must not be in manager tree are declared in
# Attributes.pm
foreach (@notManagedAttributes) {
ok( defined( $h{$_} ), "Unmanaged attribute '$_' is declared" );
delete $h{$_};
$count++;
}
2016-01-06 23:03:46 +01:00
# 6 - Verify that all attributes have been checked
ok( !%h, "No remaining attributes" )
or print STDERR Dumper( { 'Remaining attributes' => [ keys %h ] } );
$count++;
done_testing($count);
2016-01-06 23:03:46 +01:00
exit;
2016-01-06 23:03:46 +01:00
# 21 / 31 recursive search for leafs
sub scanTree {
my $tree = shift;
2016-01-06 23:03:46 +01:00
# Lists of nodes must be arrays
ok( ref($tree) eq 'ARRAY', 'Tree is an array' );
$count++;
foreach my $leaf (@$tree) {
# Scan if sub element is a node or a leaf
# Case 1: subnode
if ( ref $leaf ) {
# Nodes must be hash
ok( ref($leaf) eq 'HASH' );
my $name;
# Nodes must have a title
ok( $name = $leaf->{title}, "Node has a name" );
2022-02-16 17:43:29 +01:00
ok( $name =~ /^\w+$/, "Name is a string" );
2016-01-06 23:03:46 +01:00
# Nodes must have leafs or subnodes
2019-02-07 09:27:56 +01:00
ok( (
2016-01-06 23:03:46 +01:00
exists( $leaf->{nodes} )
or exists( $leaf->{nodes_cond} )
or exists( $leaf->{group} )
),
"Node $name has leafs"
);
$count += 4;
# Nodes must not use attributes name
unless ( $name =~ $doubleUsage ) {
ok( !exists( $h{$name} ),
"Node title ($name) must not be used as attribute name" );
$count++;
}
2016-01-06 23:03:46 +01:00
foreach my $n (qw(nodes nodes_cond group)) {
# Scan subnodes lists
scanTree( $leaf->{$n} ) if ( exists $leaf->{$n} );
}
}
2016-01-06 23:03:46 +01:00
# Case 2: leaf
# Sub case 21: normal leaf
elsif ( $leaf !~ $usedMoreThanOneTimeInTree ) {
2016-01-06 23:03:46 +01:00
# Check that leaf is a string
2016-01-06 23:03:46 +01:00
ok( $leaf =~ /^\*?\w+/, "Leaf is an attribute name ($leaf)" );
$h2{$leaf}++;
# Check that leaf appears for the first time
ok( $h2{$leaf} == 1, "$leaf is uniq" );
$count += 2;
}
# Sub case 22: $usedMoreThanOneTimeInTree contains leaf used more than
# one time in tree
2016-01-06 23:03:46 +01:00
else {
$h2{$leaf}++;
}
}
}