1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server;
18  
19  
20  import org.apache.ldap.server.jndi.EnvKeys;
21  
22  import javax.naming.*;
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.Hashtable;
26  
27  
28  /***
29   * A set of simple tests to make sure simple authentication is working as it
30   * should.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 165195 $
34   */
35  public class DisableAnonBindTest extends AbstractServerTest
36  {
37      /***
38       * Cleans up old database files on creation.
39       * @throws IOException 
40       */
41      public DisableAnonBindTest() throws IOException
42      {
43          doDelete( new File( "target" + File.separator + "server" ) );
44      }
45  
46  
47      /***
48       * Customizes setup for each test case.
49       *
50       * @throws Exception
51       */
52      protected void setUp() throws Exception
53      {
54          if ( getName().equals( "testDisableAnonymousBinds" ) )
55          {
56              extras.put( EnvKeys.DISABLE_ANONYMOUS, "true" );
57          }
58  
59          super.setUp();
60      }
61  
62  
63      /***
64       * Test to make sure anonymous binds are disabled when going through
65       * the wire protocol.
66       *
67       * @throws Exception if anything goes wrong
68       */
69      public void testDisableAnonymousBinds() throws Exception
70      {
71          // Use the SUN JNDI provider to hit server port and bind as anonymous
72  
73          final Hashtable env = new Hashtable();
74  
75          env.put( Context.PROVIDER_URL, "ldap://localhost:" + port + "/ou=system" );
76  
77          env.put( Context.SECURITY_AUTHENTICATION, "none" );
78  
79          env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
80  
81          InitialContext ctx = null;
82  
83          try
84          {
85              ctx = new InitialContext( env );
86  
87              fail( "If anonymous binds are disabled we should never get here!" );
88          }
89          catch ( NoPermissionException e )
90          {
91              assertNull( ctx );
92  
93              assertNotNull( e );
94          }
95      }
96  }