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 org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.integ.CiRunner;
25  import org.apache.directory.shared.ldap.exception.LdapNoPermissionException;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.fail;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  
33  import javax.naming.Context;
34  import javax.naming.InitialContext;
35  import javax.naming.NamingException;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.DirContext;
38  import javax.naming.directory.ModificationItem;
39  
40  import java.util.Hashtable;
41  
42  
43  /**
44   * Testing RootDSE lookups and context creation using the empty string.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev: 691179 $
48   */
49  @RunWith ( CiRunner.class )
50  public class RootDSEIT
51  {
52      public static DirectoryService service;
53  
54  
55      /**
56       * Creates an initial context using the empty string for the provider URL.
57       * This should work.
58       *
59       * @throws NamingException if there are any problems
60       */
61      @Test
62      public void testGetInitialContext() throws NamingException
63      {
64          Hashtable<String,Object> env = new Hashtable<String,Object>();
65          env.put( DirectoryService.JNDI_KEY, service );
66          env.put( Context.PROVIDER_URL, "" );
67          env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
68          env.put( Context.SECURITY_CREDENTIALS, "secret" );
69          env.put( Context.SECURITY_AUTHENTICATION, "simple" );
70          env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
71  
72          InitialContext initCtx = new InitialContext( env );
73          assertNotNull( initCtx );
74      }
75  
76  
77      /**
78       * Gets a DirContext from the InitialContext for the empty string or RootDSE
79       * and checks that none of the operational attributes are returned.
80       *
81       * @throws NamingException if there are any problems
82       */
83      @Test
84      public void testGetInitialContextLookupAttributes() throws NamingException
85      {
86          Hashtable<String,Object> env = new Hashtable<String,Object>();
87          env.put( DirectoryService.JNDI_KEY, service );
88          env.put( Context.PROVIDER_URL, "" );
89          env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
90          env.put( Context.SECURITY_CREDENTIALS, "secret" );
91          env.put( Context.SECURITY_AUTHENTICATION, "simple" );
92          env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
93  
94          InitialContext initCtx = new InitialContext( env );
95          assertNotNull( initCtx );
96  
97          DirContext ctx = ( DirContext ) initCtx.lookup( "" );
98          Attributes attributes = ctx.getAttributes( "" );
99  
100         // Added some objectClass attributes to the rootDSE
101         assertEquals( 1, attributes.size() );
102     }
103 
104 
105     /**
106      * Checks for namingContexts and vendorName attributes.
107      *
108      * @throws NamingException if there are any problems
109      */
110     @Test
111     public void testGetInitialContextLookupAttributesByName() throws NamingException
112     {
113         Hashtable<String,Object> env = new Hashtable<String,Object>();
114         env.put( DirectoryService.JNDI_KEY, service );
115         env.put( Context.PROVIDER_URL, "" );
116         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
117         env.put( Context.SECURITY_CREDENTIALS, "secret" );
118         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
119         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
120 
121         InitialContext initCtx = new InitialContext( env );
122         assertNotNull( initCtx );
123         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
124 
125         Attributes attributes = ctx.getAttributes( "", new String[]
126             { "namingContexts", "VENDORNAME" } );
127         assertEquals( 2, attributes.size() );
128         assertEquals( "Apache Software Foundation", attributes.get( "vendorName" ).get() );
129         assertTrue( attributes.get( "namingContexts" ).contains( "ou=system" ) );
130     }
131 
132 
133     /**
134      * Checks for ObjectClass, namingContexts and vendorName attributes.
135      *
136      * @throws NamingException if there are any problems
137      */
138     @Test
139     public void testGetInitialContextLookupAttributesByNameWithOC() throws NamingException
140     {
141         Hashtable<String,Object> env = new Hashtable<String,Object>();
142         env.put( DirectoryService.JNDI_KEY, service );
143         env.put( Context.PROVIDER_URL, "" );
144         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
145         env.put( Context.SECURITY_CREDENTIALS, "secret" );
146         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
147         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
148 
149         InitialContext initCtx = new InitialContext( env );
150         assertNotNull( initCtx );
151         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
152 
153         Attributes attributes = ctx.getAttributes( "", new String[]
154             { "ObjectClass", "namingContexts", "VENDORNAME" } );
155         assertEquals( 3, attributes.size() );
156         assertEquals( "Apache Software Foundation", attributes.get( "vendorName" ).get() );
157         assertTrue( attributes.get( "namingContexts" ).contains( "ou=system" ) );
158     }
159 
160 
161     /**
162      * Checks for lack of permissions to delete this entry.
163      *
164      * @throws NamingException if there are any problems
165      */
166     @Test
167     public void testDelete() throws NamingException
168     {
169         Hashtable<String,Object> env = new Hashtable<String,Object>();
170         env.put( DirectoryService.JNDI_KEY, service );
171         env.put( Context.PROVIDER_URL, "" );
172         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
173         env.put( Context.SECURITY_CREDENTIALS, "secret" );
174         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
175         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
176 
177         InitialContext initCtx = new InitialContext( env );
178         assertNotNull( initCtx );
179         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
180         LdapNoPermissionException notNull = null;
181 
182         try
183         {
184             ctx.destroySubcontext( "" );
185             fail( "we should never get here" );
186         }
187         catch ( LdapNoPermissionException e )
188         {
189             notNull = e;
190         }
191 
192         assertNotNull( notNull );
193     }
194 
195 
196     /**
197      * Checks for lack of permissions to rename or move this entry.
198      *
199      * @throws NamingException if there are any problems
200      */
201     @Test
202     public void testRename() throws NamingException
203     {
204         Hashtable<String,Object> env = new Hashtable<String,Object>();
205         env.put( DirectoryService.JNDI_KEY, service );
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         assertNotNull( initCtx );
214         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
215         LdapNoPermissionException notNull = null;
216 
217         try
218         {
219             ctx.rename( "", "ou=system" );
220             fail( "we should never get here" );
221         }
222         catch ( LdapNoPermissionException e )
223         {
224             notNull = e;
225         }
226 
227         assertNotNull( notNull );
228     }
229 
230 
231     /**
232      * Checks for lack of permissions to modify this entry.
233      *
234      * @throws NamingException if there are any problems
235      */
236     @Test
237     public void testModify() throws NamingException
238     {
239         Hashtable<String,Object> env = new Hashtable<String,Object>();
240         env.put( DirectoryService.JNDI_KEY, service );
241         env.put( Context.PROVIDER_URL, "" );
242         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
243         env.put( Context.SECURITY_CREDENTIALS, "secret" );
244         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
245         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
246 
247         InitialContext initCtx = new InitialContext( env );
248         assertNotNull( initCtx );
249         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
250         LdapNoPermissionException notNull = null;
251 
252         try
253         {
254             ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, null );
255             fail( "we should never get here" );
256         }
257         catch ( LdapNoPermissionException e )
258         {
259             notNull = e;
260         }
261 
262         assertNotNull( notNull );
263     }
264 
265 
266     /**
267      * Checks for lack of permissions to modify this entry.
268      *
269      * @throws NamingException if there are any problems
270      */
271     @Test
272     public void testModify2() throws NamingException
273     {
274         Hashtable<String,Object> env = new Hashtable<String,Object>();
275         env.put( DirectoryService.JNDI_KEY, service );
276         env.put( Context.PROVIDER_URL, "" );
277         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
278         env.put( Context.SECURITY_CREDENTIALS, "secret" );
279         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
280         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
281 
282         InitialContext initCtx = new InitialContext( env );
283 
284         assertNotNull( initCtx );
285 
286         DirContext ctx = ( DirContext ) initCtx.lookup( "" );
287 
288         LdapNoPermissionException notNull = null;
289 
290         try
291         {
292             ctx.modifyAttributes( "", new ModificationItem[]
293                 {} );
294 
295             fail( "we should never get here" );
296         }
297         catch ( LdapNoPermissionException e )
298         {
299             notNull = e;
300         }
301 
302         assertNotNull( notNull );
303     }
304 }