Debian Codegraph @

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

raw
1 # Verify that all attributes are positionned in tree and that all leaf
2 # correspond to an attribute. Verify also that attributes and leafs are uniq
3
4 use warnings;
5 use strict;
6 use Test::More;
7 use Data::Dumper;
8
9 # CONSTANTS
10
11 # Node names used more than one time
12 my $usedMoreThanOneTimeInTree = qr/^(?:
13 remoteCookieName
14 )$/x;
15
16 # Attributes not managed in web interface
17 my @notManagedAttributes = (
18
19 # Complex nodes
20 'samlSPMetaDataOptions', 'samlIDPMetaDataOptions', 'oidcRPMetaDataOptions',
21 'oidcOPMetaDataOptions', 'casSrvMetaDataOptions', 'casAppMetaDataOptions',
22 'vhostOptions', 'keys',
23
24 # Second factor engine, lists of 2F modules and other parameters
25 'sfEngine', 'available2FSelfRegistration', 'available2F', 'max2FDevices',
26 'max2FDevicesNameLength',
27
28 # Handlers
29 'handlerInternalCache', 'handlerServiceTokenTTL',
30
31 # Metadatas (added by manager itself)
32 'cfgAuthor', 'cfgAuthorIP', 'cfgNum', 'cfgDate', 'cfgLog', 'cfgVersion',
33
34 # HTML template parameter (for PSGI) (must be set in lemonldap-ng.ini)
35 'staticPrefix',
36
37 # Loggers
38 'log4perlConfFile', 'userSyslogFacility', 'logger', 'sentryDsn',
39 'syslogFacility', 'syslogSockOptions', 'userLogger', 'logLevel',
40 'lokiUserService', 'lokiTenant', 'lokiUrl', 'lokiTenantHeader',
41 'lokiLabel', 'lokiService', 'lokiInstance', 'lokiEnv', 'lokiAuthorization',
42
43 # Plugins parameters
44 'notificationsMaxRetrieve', 'persistentSessionAttributes',
45
46 # PSGI/CGI protection (must be set in lemonldap-ng.ini)
47 'protection',
48
49 # SecureToken handler
50 'secureTokenAllowOnError', 'secureTokenAttribute', 'secureTokenExpiration',
51 'secureTokenHeader', 'secureTokenMemcachedServers', 'secureTokenUrls',
52
53 # Sessions and OTT storage
54 'configStorage', 'localStorageOptions', 'localStorage',
55 'forceGlobalStorageUpgradeOTT', 'forceGlobalStorageIssuerOTT',
56
57 # Viewer
58 'viewerHiddenKeys', 'viewerAllowBrowser', 'viewerAllowDiff',
59
60 # Zimbra handler
61 'zimbraAccountKey', 'zimbraBy', 'zimbraPreAuthKey', 'zimbraSsoUrl',
62 'zimbraUrl',
63
64 # Other ini-only prms
65 'checkTime', 'eventStatus', 'statusQueueName', 'soapProxyUrn',
66 'impersonationPrefix', 'pdataDomain',
67 'mySessionAuthorizedRWKeys', 'contextSwitchingPrefix',
68 'defaultNewKeySize', 'checkMsg',
69 );
70
71 # Words used either as attribute name and node title
72 my $doubleUsage = qr/^(?:
73 samlSPMetaDataOptions|
74 samlIDPMetaDataOptions|
75 oidcRPMetaDataOptions|
76 oidcOPMetaDataOptions|
77 casSrvMetaDataOptions|
78 casAppMetaDataOptions|
79 vhostOptions
80 )$/x;
81
82 # TESTS
83
84 # 1 - Collect attributes
85
86 # Attributes.pm is parsed with open() and not loaded to detect double entries
87 ok( open( F, 'lib/Lemonldap/NG/Manager/Build/Attributes.pm' ),
88 'open attributes file' );
89 my $count = 1;
90
91 while ( <F> !~ /sub\s+attributes/ ) { 1 }
92
93 my ( %h, %h2 );
94
95 while (<F>) {
96 next unless /^\s{8}["']?(\w+)/;
97 my $attr = $1;
98 $h{$attr}++;
99 ok( $h{$attr} == 1, "$attr is uniq" );
100 $count++;
101 }
102 close F;
103
104 # 2 - Parse Tree.pm
105 use_ok('Lemonldap::NG::Manager::Build::Tree');
106 my $tree;
107 ok( $tree = Lemonldap::NG::Manager::Build::Tree::tree(), 'Get tree' );
108 $count += 3;
109 scanTree($tree);
110
111 # 3 - Parse CTrees.pm
112 use_ok('Lemonldap::NG::Manager::Build::CTrees');
113 ok( $tree = Lemonldap::NG::Manager::Build::CTrees::cTrees(),
114 'Get conditional tree' );
115 $count++;
116 foreach my $t ( values %$tree ) {
117 scanTree($t);
118 }
119
120 # 4 - Check that each leaf correspond to an attribute
121 foreach ( keys %h2 ) {
122 s/^\*//;
123 ok( defined( $h{$_} ), "Leaf $_ exists in attributes" );
124 delete $h{$_};
125 $count++;
126 }
127
128 # 5 - Check that attributes that must not be in manager tree are declared in
129 # Attributes.pm
130 foreach (@notManagedAttributes) {
131 ok( defined( $h{$_} ), "Unmanaged attribute '$_' is declared" );
132 delete $h{$_};
133 $count++;
134 }
135
136 # 6 - Verify that all attributes have been checked
137 ok( !%h, "No remaining attributes" )
138 or print STDERR Dumper( { 'Remaining attributes' => [ keys %h ] } );
139 $count++;
140
141 done_testing($count);
142 exit;
143
144 # 21 / 31 recursive search for leafs
145 sub scanTree {
146 my $tree = shift;
147
148 # Lists of nodes must be arrays
149 ok( ref($tree) eq 'ARRAY', 'Tree is an array' );
150 $count++;
151 foreach my $leaf (@$tree) {
152
153 # Scan if sub element is a node or a leaf
154
155 # Case 1: subnode
156 if ( ref $leaf ) {
157
158 # Nodes must be hash
159 ok( ref($leaf) eq 'HASH' );
160 my $name;
161
162 # Nodes must have a title
163 ok( $name = $leaf->{title}, "Node has a name" );
164 ok( $name =~ /^\w+$/, "Name is a string" );
165
166 # Nodes must have leafs or subnodes
167 ok( (
168 exists( $leaf->{nodes} )
169 or exists( $leaf->{nodes_cond} )
170 or exists( $leaf->{group} )
171 ),
172 "Node $name has leafs"
173 );
174 $count += 4;
175
176 # Nodes must not use attributes name
177 unless ( $name =~ $doubleUsage ) {
178 ok( !exists( $h{$name} ),
179 "Node title ($name) must not be used as attribute name" );
180 $count++;
181 }
182
183 foreach my $n (qw(nodes nodes_cond group)) {
184
185 # Scan subnodes lists
186 scanTree( $leaf->{$n} ) if ( exists $leaf->{$n} );
187 }
188 }
189
190 # Case 2: leaf
191
192 # Sub case 21: normal leaf
193 elsif ( $leaf !~ $usedMoreThanOneTimeInTree ) {
194
195 # Check that leaf is a string
196 ok( $leaf =~ /^\*?\w+/, "Leaf is an attribute name ($leaf)" );
197 $h2{$leaf}++;
198
199 # Check that leaf appears for the first time
200 ok( $h2{$leaf} == 1, "$leaf is uniq" );
201 $count += 2;
202 }
203
204 # Sub case 22: $usedMoreThanOneTimeInTree contains leaf used more than
205 # one time in tree
206 else {
207 $h2{$leaf}++;
208 }
209 }
210 }