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.jndi;
21
22
23 import java.util.Hashtable;
24
25 import javax.naming.Context;
26
27 import org.apache.commons.lang.ArrayUtils;
28 import org.apache.directory.server.core.jndi.LdapJndiProperties;
29 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
30 import org.apache.directory.shared.ldap.exception.LdapConfigurationException;
31 import org.apache.directory.shared.ldap.util.StringTools;
32
33 import junit.framework.TestCase;
34
35
36
37
38
39
40
41
42 public class LdapJndiPropertiesTest extends TestCase
43 {
44 public void testEmptyEnv() throws Exception
45 {
46 try
47 {
48 LdapJndiProperties.getLdapJndiProperties( new Hashtable<String,Object>() );
49 fail( "should never get here" );
50 }
51 catch ( LdapConfigurationException e )
52 {
53 }
54 }
55
56
57 public void testNullEnv() throws Exception
58 {
59 try
60 {
61 LdapJndiProperties.getLdapJndiProperties( null );
62 fail( "should never get here" );
63 }
64 catch ( LdapConfigurationException e )
65 {
66 }
67 }
68
69
70 public void testNoAuthWithCredsEnv() throws Exception
71 {
72 Hashtable<String,Object> env = new Hashtable<String,Object>();
73 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
74 env.put( Context.SECURITY_CREDENTIALS, "asdf" );
75 env.put( Context.PROVIDER_URL, "" );
76 LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
77 assertEquals( AuthenticationLevel.SIMPLE, props.getAuthenticationLevel() );
78 assertTrue( ArrayUtils.isEquals( StringTools.getBytesUtf8( "asdf" ), props.getCredentials() ) );
79 }
80
81
82 public void testNoAuthWithNoCredsEnv() throws Exception
83 {
84 Hashtable<String,Object> env = new Hashtable<String,Object>();
85 env.put( Context.SECURITY_PRINCIPAL, "" );
86 env.put( Context.PROVIDER_URL, "" );
87 LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
88 assertEquals( AuthenticationLevel.NONE, props.getAuthenticationLevel() );
89 assertTrue( props.getCredentials() == null );
90 }
91
92
93 public void testAuthWithNoCredsEnv() throws Exception
94 {
95 Hashtable<String,Object> env = new Hashtable<String,Object>();
96 env.put( Context.SECURITY_PRINCIPAL, "" );
97 env.put( Context.SECURITY_AUTHENTICATION, "simple" );
98 try
99 {
100 LdapJndiProperties.getLdapJndiProperties( env );
101 fail( "should never get here" );
102 }
103 catch ( LdapConfigurationException e )
104 {
105 }
106 }
107
108
109 public void testAuthWithNoCredsStrong() throws Exception
110 {
111 Hashtable<String,Object> env = new Hashtable<String,Object>();
112 env.put( Context.SECURITY_PRINCIPAL, "" );
113 env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5 CRAM-MD5" );
114 env.put( Context.PROVIDER_URL, "" );
115 LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
116 assertEquals( AuthenticationLevel.STRONG, props.getAuthenticationLevel() );
117 assertTrue( props.getCredentials() == null );
118 }
119
120
121 public void testAuthWithCredsStrong() throws Exception
122 {
123 Hashtable<String,Object> env = new Hashtable<String,Object>();
124 env.put( Context.SECURITY_PRINCIPAL, "" );
125 env.put( Context.SECURITY_CREDENTIALS, "asdf" );
126 env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5 CRAM-MD5" );
127 env.put( Context.PROVIDER_URL, "" );
128 LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
129 assertEquals( AuthenticationLevel.STRONG, props.getAuthenticationLevel() );
130 assertTrue( ArrayUtils.isEquals( StringTools.getBytesUtf8( "asdf" ), props.getCredentials() ) );
131 }
132 }