1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.interceptor;
21
22
23 import junit.framework.TestCase;
24
25 import org.apache.directory.server.core.CoreSession;
26 import org.apache.directory.server.core.DefaultCoreSession;
27 import org.apache.directory.server.core.DirectoryService;
28 import org.apache.directory.server.core.OperationManager;
29 import org.apache.directory.server.core.authn.LdapPrincipal;
30 import org.apache.directory.server.core.changelog.ChangeLog;
31 import org.apache.directory.server.core.entry.ServerEntry;
32 import org.apache.directory.server.core.event.EventService;
33 import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
34 import org.apache.directory.server.core.invocation.InvocationStack;
35 import org.apache.directory.server.core.partition.ByPassConstants;
36 import org.apache.directory.server.core.partition.Partition;
37 import org.apache.directory.server.core.partition.PartitionNexus;
38 import org.apache.directory.server.core.schema.SchemaOperationControl;
39 import org.apache.directory.server.core.schema.SchemaService;
40 import org.apache.directory.server.schema.registries.Registries;
41 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
42 import org.apache.directory.shared.ldap.ldif.LdifEntry;
43 import org.apache.directory.shared.ldap.name.LdapDN;
44
45 import javax.naming.NamingException;
46 import javax.naming.ldap.LdapContext;
47 import java.io.File;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.HashSet;
51 import java.util.Hashtable;
52 import java.util.List;
53 import java.util.Set;
54
55
56
57
58
59
60
61
62
63 public class InterceptorChainTest extends TestCase
64 {
65 private static final int INTERCEPTOR_COUNT = 5;
66 private InterceptorChain chain;
67 List<MockInterceptor> interceptors = new ArrayList<MockInterceptor>( INTERCEPTOR_COUNT );
68
69
70 public InterceptorChainTest()
71 {
72 }
73
74
75 protected void setUp() throws Exception
76 {
77 chain = new InterceptorChain();
78
79 for ( int ii = 0; ii < INTERCEPTOR_COUNT; ii++ )
80 {
81 MockInterceptor interceptor = new MockInterceptor();
82 interceptor.setTest( this );
83 interceptor.setName( Integer.toString( ii ) );
84 chain.addLast(interceptor);
85 }
86
87 }
88
89
90 protected void tearDown() throws Exception
91 {
92 chain = null;
93 interceptors.clear();
94 }
95
96
97 public void testNoBypass() throws Exception
98 {
99 LdapDN dn = new LdapDN( "ou=system" );
100 DirectoryService ds = new MockDirectoryService();
101 DefaultCoreSession session = new DefaultCoreSession(
102 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
103 LookupOperationContext opContext = new LookupOperationContext( session, dn );
104 InvocationStack.getInstance().push( opContext );
105
106 try
107 {
108 chain.lookup( opContext );
109 }
110 catch ( Exception e )
111 {
112 }
113
114 assertEquals( INTERCEPTOR_COUNT, interceptors.size() );
115 for ( int ii = 0; ii < INTERCEPTOR_COUNT; ii++ )
116 {
117 assertEquals( Integer.toString( ii ), interceptors.get( ii ).getName() );
118 }
119 }
120
121
122 public void testSingleBypass() throws Exception
123 {
124 LdapDN dn = new LdapDN( "ou=system" );
125 DirectoryService ds = new MockDirectoryService();
126 DefaultCoreSession session = new DefaultCoreSession(
127 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
128 LookupOperationContext opContext = new LookupOperationContext( session, dn );
129 opContext.setByPassed( Collections.singleton( "0" ) );
130 InvocationStack.getInstance().push( opContext );
131
132 try
133 {
134 chain.lookup( opContext );
135 }
136 catch ( Exception e )
137 {
138 }
139
140 assertEquals( INTERCEPTOR_COUNT - 1, interceptors.size() );
141 for ( int ii = 1; ii < INTERCEPTOR_COUNT; ii++ )
142 {
143 assertEquals( Integer.toString( ii ), interceptors.get( ii - 1 ).getName() );
144 }
145 }
146
147
148 public void testAdjacentDoubleBypass() throws Exception
149 {
150 LdapDN dn = new LdapDN( "ou=system" );
151 DirectoryService ds = new MockDirectoryService();
152 DefaultCoreSession session = new DefaultCoreSession(
153 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
154 LookupOperationContext opContext = new LookupOperationContext( session, dn );
155 Set<String> bypass = new HashSet<String>();
156 bypass.add( "0" );
157 bypass.add( "1" );
158 opContext.setByPassed( bypass );
159 InvocationStack.getInstance().push( opContext );
160
161 try
162 {
163 chain.lookup( opContext );
164 }
165 catch ( Exception e )
166 {
167 }
168
169 assertEquals( INTERCEPTOR_COUNT - 2, interceptors.size() );
170 for ( int ii = 2; ii < INTERCEPTOR_COUNT; ii++ )
171 {
172 assertEquals( Integer.toString( ii ), interceptors.get( ii - 2 ).getName() );
173 }
174 }
175
176
177 public void testFrontAndBackDoubleBypass() throws Exception
178 {
179 LdapDN dn = new LdapDN( "ou=system" );
180 DirectoryService ds = new MockDirectoryService();
181 DefaultCoreSession session = new DefaultCoreSession(
182 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
183 LookupOperationContext opContext = new LookupOperationContext( session, dn );
184 Set<String> bypass = new HashSet<String>();
185 bypass.add( "0" );
186 bypass.add( "4" );
187 opContext.setByPassed( bypass );
188 InvocationStack.getInstance().push( opContext );
189
190 try
191 {
192 chain.lookup( opContext );
193 }
194 catch ( Exception e )
195 {
196 }
197
198 assertEquals( INTERCEPTOR_COUNT - 2, interceptors.size() );
199 assertEquals( "1", interceptors.get( 0 ).getName() );
200 assertEquals( "2", interceptors.get( 1 ).getName() );
201 assertEquals( "3", interceptors.get( 2 ).getName() );
202 }
203
204
205 public void testDoubleBypass() throws Exception
206 {
207 LdapDN dn = new LdapDN( "ou=system" );
208 DirectoryService ds = new MockDirectoryService();
209 DefaultCoreSession session = new DefaultCoreSession(
210 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
211 LookupOperationContext opContext = new LookupOperationContext( session, dn );
212 Set<String> bypass = new HashSet<String>();
213 bypass.add( "1" );
214 bypass.add( "3" );
215 opContext.setByPassed( bypass );
216 InvocationStack.getInstance().push( opContext );
217
218 try
219 {
220 chain.lookup( opContext );
221 }
222 catch ( Exception e )
223 {
224 }
225
226 assertEquals( INTERCEPTOR_COUNT - 2, interceptors.size() );
227 assertEquals( "0", interceptors.get( 0 ).getName() );
228 assertEquals( "2", interceptors.get( 1 ).getName() );
229 assertEquals( "4", interceptors.get( 2 ).getName() );
230 }
231
232
233 public void testCompleteBypass() throws Exception
234 {
235 LdapDN dn = new LdapDN( "ou=system" );
236 DirectoryService ds = new MockDirectoryService();
237 DefaultCoreSession session = new DefaultCoreSession(
238 new LdapPrincipal( new LdapDN(), AuthenticationLevel.STRONG ), ds );
239 LookupOperationContext opContext = new LookupOperationContext( session, dn );
240 opContext.setByPassed( ByPassConstants.BYPASS_ALL_COLLECTION );
241 InvocationStack.getInstance().push( opContext );
242
243 try
244 {
245 chain.lookup( opContext );
246 }
247 catch ( Exception e )
248 {
249 }
250
251 assertEquals( 0, interceptors.size() );
252 }
253
254
255 class MockDirectoryService implements DirectoryService
256 {
257 public Hashtable<String, Object> getEnvironment()
258 {
259 return null;
260 }
261
262
263 public void setEnvironment( Hashtable<String, Object> environment )
264 {
265 }
266
267
268 public long revert( long revision ) throws NamingException
269 {
270 return 0;
271 }
272
273
274 public long revert() throws NamingException
275 {
276 return 0;
277 }
278
279
280 public PartitionNexus getPartitionNexus()
281 {
282 return null;
283 }
284
285
286 public InterceptorChain getInterceptorChain()
287 {
288 return null;
289 }
290
291
292 public void addPartition( Partition partition ) throws NamingException
293 {
294 }
295
296
297 public void removePartition( Partition partition ) throws NamingException
298 {
299 }
300
301
302 public Registries getRegistries()
303 {
304 return null;
305 }
306
307
308 public void setRegistries( Registries registries )
309 {
310 }
311
312
313 public SchemaService getSchemaService()
314 {
315 return null;
316 }
317
318
319 public void setSchemaService( SchemaService schemaService )
320 {
321
322 }
323
324
325 public SchemaOperationControl getSchemaManager()
326 {
327 return null;
328 }
329
330
331 public void setSchemaManager( SchemaOperationControl schemaManager )
332 {
333 }
334
335
336 public void startup() throws NamingException
337 {
338 }
339
340
341 public void shutdown() throws NamingException
342 {
343 }
344
345
346 public void sync() throws NamingException
347 {
348 }
349
350
351 public boolean isStarted()
352 {
353 return false;
354 }
355
356
357 public LdapContext getJndiContext() throws NamingException
358 {
359 return null;
360 }
361
362
363 public DirectoryService getDirectoryService()
364 {
365 return null;
366 }
367
368
369 public LdapContext getJndiContext( String baseName ) throws NamingException
370 {
371 return null;
372 }
373
374
375 public LdapContext getJndiContext( LdapPrincipal principal ) throws NamingException
376 {
377 return null;
378 }
379
380
381 public LdapContext getJndiContext( LdapPrincipal principal, String dn ) throws NamingException
382 {
383 return null;
384 }
385
386
387 public LdapContext getJndiContext( LdapDN principalDn, String principal, byte[] credential,
388 String authentication, String baseName ) throws NamingException
389 {
390 return null;
391 }
392
393
394 public void setInstanceId( String instanceId )
395 {
396 }
397
398
399 public String getInstanceId()
400 {
401 return null;
402 }
403
404
405 public Set<? extends Partition> getPartitions()
406 {
407 return null;
408 }
409
410
411 public void setPartitions( Set<? extends Partition> partitions )
412 {
413 }
414
415
416 public boolean isAccessControlEnabled()
417 {
418 return false;
419 }
420
421
422 public void setAccessControlEnabled( boolean accessControlEnabled )
423 {
424 }
425
426
427 public boolean isAllowAnonymousAccess()
428 {
429 return false;
430 }
431
432
433 public void setAllowAnonymousAccess( boolean enableAnonymousAccess )
434 {
435 }
436
437
438 public List<Interceptor> getInterceptors()
439 {
440 return null;
441 }
442
443
444 public void setInterceptors( List<Interceptor> interceptors )
445 {
446 }
447
448
449 public List<LdifEntry> getTestEntries()
450 {
451 return null;
452 }
453
454
455 public void setTestEntries( List<? extends LdifEntry> testEntries )
456 {
457 }
458
459
460 public File getWorkingDirectory()
461 {
462 return null;
463 }
464
465
466 public void setWorkingDirectory( File workingDirectory )
467 {
468 }
469
470
471 public void validate()
472 {
473 }
474
475
476 public void setShutdownHookEnabled( boolean shutdownHookEnabled )
477 {
478 }
479
480
481 public boolean isShutdownHookEnabled()
482 {
483 return false;
484 }
485
486
487 public void setExitVmOnShutdown( boolean exitVmOnShutdown )
488 {
489 }
490
491
492 public boolean isExitVmOnShutdown()
493 {
494 return false;
495 }
496
497
498 public void setMaxSizeLimit( int maxSizeLimit )
499 {
500 }
501
502
503 public int getMaxSizeLimit()
504 {
505 return 0;
506 }
507
508
509 public void setMaxTimeLimit( int maxTimeLimit )
510 {
511 }
512
513
514 public int getMaxTimeLimit()
515 {
516 return 0;
517 }
518
519
520 public void setSystemPartition( Partition systemPartition )
521 {
522 }
523
524
525 public Partition getSystemPartition()
526 {
527 return null;
528 }
529
530
531 public boolean isDenormalizeOpAttrsEnabled()
532 {
533 return false;
534 }
535
536
537 public void setDenormalizeOpAttrsEnabled( boolean denormalizeOpAttrsEnabled )
538 {
539 }
540
541 public void setChangeLog( ChangeLog changeLog )
542 {
543
544 }
545
546 public ChangeLog getChangeLog()
547 {
548 return null;
549 }
550
551
552 public ServerEntry newEntry( LdapDN dn ) throws NamingException
553 {
554 return null;
555 }
556
557
558 public ServerEntry newEntry( String ldif, String dn )
559 {
560 return null;
561 }
562
563
564 public OperationManager getOperationManager()
565 {
566 return null;
567 }
568
569
570 public CoreSession getSession() throws Exception
571 {
572 return null;
573 }
574
575
576 public CoreSession getSession( LdapPrincipal principal ) throws Exception
577 {
578 return null;
579 }
580
581
582 public CoreSession getSession( LdapDN principalDn, byte[] credentials ) throws Exception
583 {
584 return null;
585 }
586
587
588 public CoreSession getSession( LdapDN principalDn, byte[] credentials, String saslMechanism, String saslAuthId )
589 throws Exception
590 {
591 return null;
592 }
593
594
595 public CoreSession getAdminSession() throws Exception
596 {
597 return null;
598 }
599
600
601 public EventService getEventService()
602 {
603
604 return null;
605 }
606
607
608 public void setEventService( EventService eventService )
609 {
610
611
612 }
613 }
614 }