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 java.util.Hashtable;
21  
22  import javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.NoPermissionException;
25  
26  
27  /***
28   * A set of simple tests to make sure simple authentication is working as it
29   * should.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 264732 $
33   */
34  public class DisableAnonBindTest extends AbstractServerTest
35  {
36      /***
37       * Cleans up old database files on creation.
38       */
39      public DisableAnonBindTest()
40      {
41      }
42  
43  
44      /***
45       * Customizes setup for each test case.
46       *
47       * @throws Exception
48       */
49      public void setUp() throws Exception
50      {
51          configuration.setAllowAnonymousAccess( false );
52          super.setUp();
53      }
54  
55  
56      /***
57       * Test to make sure anonymous binds are disabled when going through
58       * the wire protocol.
59       *
60       * @throws Exception if anything goes wrong
61       */
62      public void testDisableAnonymousBinds() throws Exception
63      {
64          // Use the SUN JNDI provider to hit server port and bind as anonymous
65  
66          final Hashtable env = new Hashtable();
67  
68          env.put( Context.PROVIDER_URL, "ldap://localhost:" + port + "/ou=system" );
69          env.put( Context.SECURITY_AUTHENTICATION, "none" );
70          env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
71  
72          try
73          {
74              new InitialContext( env );
75              fail( "If anonymous binds are disabled we should never get here!" );
76          }
77          catch ( NoPermissionException e )
78          {
79          }
80      }
81  }