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.jndi;
18  
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.Hashtable;
23  
24  import javax.naming.Context;
25  import javax.naming.InitialContext;
26  import javax.naming.NamingException;
27  import javax.naming.directory.Attributes;
28  import javax.naming.directory.DirContext;
29  import javax.naming.directory.ModificationItem;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.commons.io.FileUtils;
34  import org.apache.ldap.common.exception.LdapNoPermissionException;
35  import org.apache.ldap.server.configuration.MutableStartupConfiguration;
36  import org.apache.ldap.server.configuration.ShutdownConfiguration;
37  
38  
39  /***
40   * Testing RootDSE lookups and context creation using the empty string.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev: 264732 $
44   */
45  public class RootDSETest extends TestCase
46  {
47      /*** flag whether to delete database files for each test or not */
48      protected boolean doDelete = true;
49  
50  
51      /***
52       * Get's the initial context factory for the provider's ou=system context
53       * root.
54       *
55       * @see junit.framework.TestCase#setUp()
56       */
57      protected void setUp() throws Exception
58      {
59          super.setUp();
60  
61          doDelete( new File( "target" + File.separator + "eve" ) );
62      }
63  
64  
65      /***
66       * Deletes the Eve working directory.
67       *
68       * @throws java.io.IOException if there are failures while deleting.
69       */
70      protected void doDelete( File wkdir ) throws IOException
71      {
72          if ( doDelete )
73          {
74              if ( wkdir.exists() )
75              {
76                  FileUtils.deleteDirectory( wkdir );
77              }
78          }
79      }
80  
81  
82      /***
83       * Sets the system context root to null.
84       *
85       * @see junit.framework.TestCase#tearDown()
86       */
87      protected void tearDown() throws Exception
88      {
89          super.tearDown();
90  
91          Hashtable env = new Hashtable();
92  
93          env.put( Context.PROVIDER_URL, "ou=system" );
94  
95          env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
96  
97          env.putAll( new ShutdownConfiguration().toJndiEnvironment() );
98  
99          env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
100 
101         env.put( Context.SECURITY_CREDENTIALS, "secret" );
102 
103         try { new InitialContext( env ); } catch( Exception e ) {}
104     }
105 
106 
107     /***
108      * Creates an initial context using the empty string for the provider URL.
109      * This should work.
110      *
111      * @throws NamingException if there are any problems
112      */
113     public void testGetInitialContext() throws NamingException
114     {
115         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
116         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
117 
118         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
119         env.put( Context.PROVIDER_URL, "" );
120         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
121         env.put( Context.SECURITY_CREDENTIALS, "secret" );
122         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
123         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
124 
125         InitialContext initCtx = new InitialContext( env );
126         assertNotNull( initCtx );
127     }
128 
129 
130     /***
131      * Gets a DirContext from the InitialContext for the empty string or RootDSE
132      * and checks that none of the operational attributes are returned.
133      *
134      * @throws NamingException if there are any problems
135      */
136     public void testGetInitialContextLookupAttributes() throws NamingException
137     {
138         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
139         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
140 
141         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
142         env.put( Context.PROVIDER_URL, "" );
143         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
144         env.put( Context.SECURITY_CREDENTIALS, "secret" );
145         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
146         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
147 
148         InitialContext initCtx = new InitialContext( env );
149 
150         assertNotNull( initCtx );
151 
152         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
153 
154         Attributes attributes = ctx.getAttributes( "" );
155 
156         // Added some objectClass attributes to the rootDSE
157 
158         assertEquals( 2, attributes.size() );
159     }
160 
161 
162     /***
163      * Checks for namingContexts and vendorName attributes.
164      *
165      * @throws NamingException if there are any problems
166      */
167     public void testGetInitialContextLookupAttributesByName() throws NamingException
168     {
169         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
170         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
171 
172         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
173         env.put( Context.PROVIDER_URL, "" );
174         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
175         env.put( Context.SECURITY_CREDENTIALS, "secret" );
176         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
177         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
178 
179         InitialContext initCtx = new InitialContext( env );
180 
181         assertNotNull( initCtx );
182 
183         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
184 
185         Attributes attributes = ctx.getAttributes( "", new String[]{ "namingContexts", "vendorName" });
186 
187         assertEquals( 2, attributes.size() );
188 
189         assertEquals( "Apache Software Foundation", attributes.get( "vendorName" ).get() );
190 
191         assertTrue( attributes.get( "namingContexts" ).contains( "ou=system" ) );
192     }
193 
194 
195     /***
196      * Checks for lack of permissions to delete this entry.
197      *
198      * @throws NamingException if there are any problems
199      */
200     public void testDelete() throws NamingException
201     {
202         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
203         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
204 
205         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
206         env.put( Context.PROVIDER_URL, "" );
207         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
208         env.put( Context.SECURITY_CREDENTIALS, "secret" );
209         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
210         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
211 
212         InitialContext initCtx = new InitialContext( env );
213 
214         assertNotNull( initCtx );
215 
216         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
217 
218         LdapNoPermissionException notNull = null;
219 
220         try
221         {
222             ctx.destroySubcontext( "" );
223 
224             fail( "we should never get here" );
225         }
226         catch ( LdapNoPermissionException e )
227         {
228             notNull = e;
229         }
230 
231         assertNotNull( notNull );
232     }
233 
234 
235     /***
236      * Checks for lack of permissions to rename or move this entry.
237      *
238      * @throws NamingException if there are any problems
239      */
240     public void testRename() throws NamingException
241     {
242         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
243         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
244 
245         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
246         env.put( Context.PROVIDER_URL, "" );
247         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
248         env.put( Context.SECURITY_CREDENTIALS, "secret" );
249         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
250         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
251 
252         InitialContext initCtx = new InitialContext( env );
253 
254         assertNotNull( initCtx );
255 
256         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
257 
258         LdapNoPermissionException notNull = null;
259 
260         try
261         {
262             ctx.rename( "", "ou=system" );
263 
264             fail( "we should never get here" );
265         }
266         catch ( LdapNoPermissionException e )
267         {
268             notNull = e;
269         }
270 
271         assertNotNull( notNull );
272     }
273 
274 
275     /***
276      * Checks for lack of permissions to modify this entry.
277      *
278      * @throws NamingException if there are any problems
279      */
280     public void testModify() throws NamingException
281     {
282         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
283         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
284 
285         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
286         env.put( Context.PROVIDER_URL, "" );
287         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
288         env.put( Context.SECURITY_CREDENTIALS, "secret" );
289         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
290         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
291 
292         InitialContext initCtx = new InitialContext( env );
293 
294         assertNotNull( initCtx );
295 
296         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
297 
298         LdapNoPermissionException notNull = null;
299 
300         try
301         {
302             ctx.modifyAttributes( "", 0, null );
303 
304             fail( "we should never get here" );
305         }
306         catch ( LdapNoPermissionException e )
307         {
308             notNull = e;
309         }
310 
311         assertNotNull( notNull );
312     }
313 
314 
315     /***
316      * Checks for lack of permissions to modify this entry.
317      *
318      * @throws NamingException if there are any problems
319      */
320     public void testModify2() throws NamingException
321     {
322         MutableStartupConfiguration cfg = new MutableStartupConfiguration();
323         cfg.setWorkingDirectory( new File( "target" + File.separator + "server" ) );
324 
325         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
326         env.put( Context.PROVIDER_URL, "" );
327         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
328         env.put( Context.SECURITY_CREDENTIALS, "secret" );
329         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
330         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
331 
332         InitialContext initCtx = new InitialContext( env );
333 
334         assertNotNull( initCtx );
335 
336         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
337 
338         LdapNoPermissionException notNull = null;
339 
340         try
341         {
342             ctx.modifyAttributes( "", new ModificationItem[]{} );
343 
344             fail( "we should never get here" );
345         }
346         catch ( LdapNoPermissionException e )
347         {
348             notNull = e;
349         }
350 
351         assertNotNull( notNull );
352     }
353 }