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  import java.util.Hashtable;
20  
21  import javax.naming.CommunicationException;
22  import javax.naming.NamingException;
23  import javax.naming.ldap.ExtendedRequest;
24  import javax.naming.ldap.ExtendedResponse;
25  import javax.naming.ldap.InitialLdapContext;
26  import javax.naming.ldap.LdapContext;
27  
28  /***
29   * Check the behaviour of the server for an unknown extended operation. Created
30   * to demonstrate DIREVE-256 ("Extended operation causes client to hang.").
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev$
34   */
35  public class UnknownExtendedOperationTest extends AbstractServerTest
36  {
37      private LdapContext ctx = null;
38  
39      /***
40       * Create context.
41       */
42      public void setUp() throws Exception
43      {
44          super.setUp();
45  
46          Hashtable env = new Hashtable();
47          env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
48          env.put("java.naming.provider.url", "ldap://localhost:" + port + "/ou=system");
49          env.put("java.naming.security.principal", "uid=admin,ou=system");
50          env.put("java.naming.security.credentials", "secret");
51          env.put("java.naming.security.authentication", "simple");
52  
53          ctx = new InitialLdapContext(env, null);
54          assertNotNull(ctx);
55      }
56  
57      /***
58       * Close context.
59       */
60      public void tearDown() throws Exception
61      {
62          ctx.close();
63          ctx = null;
64          super.tearDown();
65      }
66  
67      /***
68       * Calls an extended exception, which does not exist. Expected behaviour is
69       * a CommunicationException.
70       */
71      public void testUnknownExtendedOperation() throws NamingException
72      {
73          try {
74              ctx.extendedOperation(new UnknownExtendedOperationRequest());
75              fail("Calling an unknown extended operation should fail.");
76          } catch (CommunicationException ce) {
77              // expected behaviour
78          }
79      }
80  
81      /***
82       * Class for the request of an extended operation which does not exist.
83       */
84      private class UnknownExtendedOperationRequest implements ExtendedRequest
85      {
86  
87          private static final long serialVersionUID = 1L;
88  
89          public String getID()
90          {
91              return "1.1"; // Never an OID for an extended operation
92          }
93  
94          public byte[] getEncodedValue()
95          {
96              return null;
97          }
98  
99          public ExtendedResponse createExtendedResponse(String id, byte[] berValue, int offset, int length)
100                 throws NamingException
101         {
102             return null;
103         }
104     }
105 
106 }