1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * Tests the LdapJndiProperties.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 679219 $
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 }