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 static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
26  import org.apache.directory.shared.ldap.exception.LdapSchemaViolationException;
27  import org.junit.Test;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertTrue;
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.fail;
32  import static org.junit.Assert.assertNull;
33  import org.junit.runner.RunWith;
34  
35  import javax.naming.CompositeName;
36  import javax.naming.Name;
37  import javax.naming.NamingEnumeration;
38  import javax.naming.NamingException;
39  import javax.naming.directory.Attribute;
40  import javax.naming.directory.Attributes;
41  import javax.naming.directory.BasicAttribute;
42  import javax.naming.directory.BasicAttributes;
43  import javax.naming.directory.DirContext;
44  import javax.naming.directory.SearchControls;
45  import javax.naming.directory.SearchResult;
46  import javax.naming.ldap.LdapContext;
47  
48  
49  /**
50   * Tests the creation of contexts in various ways.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev: 691024 $
54   */
55  @RunWith ( CiRunner.class )
56  public class CreateContextIT
57  {
58      public static DirectoryService service;
59  
60  
61      protected Attributes getPersonAttributes( String sn, String cn )
62      {
63          Attributes attrs = new BasicAttributes( true );
64          Attribute ocls = new BasicAttribute( "objectClass" );
65          ocls.add( "top" );
66          ocls.add( "person" );
67          attrs.put( ocls );
68          attrs.put( "cn", cn );
69          attrs.put( "sn", sn );
70  
71          return attrs;
72      }
73  
74  
75      /**
76       * DIRSERVER-628: Creation of entry with multivalued RDN leads to wrong
77       * attribute value.
78       *
79       * @throws NamingException on error
80       */
81      @Test
82      public void testMultiValuedRdn() throws Exception
83      {
84          LdapContext sysRoot = getSystemContext( service );
85  
86          Attributes attrs = getPersonAttributes( "Bush", "Kate Bush" );
87          String rdn = "cn=Kate Bush+sn=Bush";
88          sysRoot.createSubcontext( rdn, attrs );
89  
90          SearchControls sctls = new SearchControls();
91          sctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
92          String filter = "(sn=Bush)";
93          String base = "";
94  
95          NamingEnumeration<SearchResult> enm = sysRoot.search( base, filter, sctls );
96          
97          while ( enm.hasMore() )
98          {
99              SearchResult sr = enm.next();
100             attrs = sr.getAttributes();
101             Attribute cn = sr.getAttributes().get( "cn" );
102             assertNotNull( cn );
103             assertTrue( cn.contains( "Kate Bush" ) );
104         }
105 
106         sysRoot.destroySubcontext( rdn );
107     }
108 
109 
110     /**
111      * Tests the creation and subsequent read of a new JNDI context under the
112      * system context root.
113      *
114      * @throws javax.naming.NamingException if there are failures
115      */
116     @Test
117     public void testCreateContexts() throws Exception
118     {
119         LdapContext sysRoot = getSystemContext( service );
120 
121         /*
122          * create ou=testing00,ou=system
123          */
124         Attributes attributes = new BasicAttributes( true );
125         Attribute attribute = new BasicAttribute( "objectClass" );
126         attribute.add( "top" );
127         attribute.add( "organizationalUnit" );
128         attributes.put( attribute );
129         attributes.put( "ou", "testing00" );
130         DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
131         assertNotNull( ctx );
132 
133         ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
134         assertNotNull( ctx );
135 
136         attributes = ctx.getAttributes( "" );
137         assertNotNull( attributes );
138         assertEquals( "testing00", attributes.get( "ou" ).get() );
139         attribute = attributes.get( "objectClass" );
140         assertNotNull( attribute );
141         assertTrue( attribute.contains( "top" ) );
142         assertTrue( attribute.contains( "organizationalUnit" ) );
143 
144         /*
145          * create ou=testing01,ou=system
146          */
147         attributes = new BasicAttributes( true );
148         attribute = new BasicAttribute( "objectClass" );
149         attribute.add( "top" );
150         attribute.add( "organizationalUnit" );
151         attributes.put( attribute );
152         attributes.put( "ou", "testing01" );
153         ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
154         assertNotNull( ctx );
155 
156         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
157         assertNotNull( ctx );
158 
159         attributes = ctx.getAttributes( "" );
160         assertNotNull( attributes );
161         assertEquals( "testing01", attributes.get( "ou" ).get() );
162         attribute = attributes.get( "objectClass" );
163         assertNotNull( attribute );
164         assertTrue( attribute.contains( "top" ) );
165         assertTrue( attribute.contains( "organizationalUnit" ) );
166 
167         /*
168          * create ou=testing02,ou=system
169          */
170         attributes = new BasicAttributes( true );
171         attribute = new BasicAttribute( "objectClass" );
172         attribute.add( "top" );
173         attribute.add( "organizationalUnit" );
174         attributes.put( attribute );
175         attributes.put( "ou", "testing02" );
176         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
177         assertNotNull( ctx );
178 
179         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
180         assertNotNull( ctx );
181 
182         attributes = ctx.getAttributes( "" );
183         assertNotNull( attributes );
184         assertEquals( "testing02", attributes.get( "ou" ).get() );
185         attribute = attributes.get( "objectClass" );
186         assertNotNull( attribute );
187         assertTrue( attribute.contains( "top" ) );
188         assertTrue( attribute.contains( "organizationalUnit" ) );
189 
190         /*
191          * create ou=subtest,ou=testing01,ou=system
192          */
193         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
194 
195         attributes = new BasicAttributes( true );
196         attribute = new BasicAttribute( "objectClass" );
197         attribute.add( "top" );
198         attribute.add( "organizationalUnit" );
199         attributes.put( attribute );
200         attributes.put( "ou", "subtest" );
201         ctx = ctx.createSubcontext( "ou=subtest", attributes );
202         assertNotNull( ctx );
203 
204         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
205         assertNotNull( ctx );
206 
207         attributes = ctx.getAttributes( "" );
208         assertNotNull( attributes );
209         assertEquals( "subtest", attributes.get( "ou" ).get() );
210         attribute = attributes.get( "objectClass" );
211         assertNotNull( attribute );
212         assertTrue( attribute.contains( "top" ) );
213         assertTrue( attribute.contains( "organizationalUnit" ) );
214     }
215 
216 
217     @Test
218     public void testFailCreateExisting() throws Exception
219     {
220         LdapContext sysRoot = getSystemContext( service );
221 
222         Attribute attribute;
223         Attributes attributes;
224         DirContext ctx = null;
225 
226         /*
227          * create ou=testing00,ou=system
228          */
229         attributes = new BasicAttributes( true );
230         attribute = new BasicAttribute( "objectClass" );
231         attribute.add( "top" );
232         attribute.add( "organizationalUnit" );
233         attributes.put( attribute );
234         attributes.put( "ou", "testing00" );
235         ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
236         assertNotNull( ctx );
237 
238         ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
239         assertNotNull( ctx );
240 
241         attributes = ctx.getAttributes( "" );
242         assertNotNull( attributes );
243         assertEquals( "testing00", attributes.get( "ou" ).get() );
244         attribute = attributes.get( "objectClass" );
245         assertNotNull( attribute );
246         assertTrue( attribute.contains( "top" ) );
247         assertTrue( attribute.contains( "organizationalUnit" ) );
248 
249         /*
250          * fail on recreate attempt for ou=testing00,ou=system
251          */
252         attributes = new BasicAttributes( true );
253         attribute = new BasicAttribute( "objectClass" );
254         attribute.add( "top" );
255         attribute.add( "organizationalUnit" );
256         attributes.put( attribute );
257         attributes.put( "ou", "testing00" );
258 
259         ctx = null;
260         try
261         {
262             ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
263             fail( "Attempt to create exiting context should fail!" );
264         }
265         catch ( NamingException e )
266         {
267             assertNotNull( e );
268         }
269 
270         assertNull( ctx );
271     }
272     
273     
274     @Test
275     public void testCreateContextWithCompositeName() throws Exception
276     {
277         LdapContext sysRoot = getSystemContext( service );
278 
279         Attributes attrs = new BasicAttributes( true );
280         Attribute objclass = new BasicAttribute( "objectClass" );
281         objclass.add( "top" );
282         objclass.add( "organizationalUnit" );
283         attrs.put( objclass );
284 
285         Name relativeName = new CompositeName( "ou=services" );
286 
287         //sysRoot.createSubcontext(relativeName.toString(), attrs);//Passes!
288         sysRoot.createSubcontext( relativeName, attrs );//Fails!
289     }
290 
291 
292     /**
293      * Tests the creation and subsequent read of a new JNDI context under the
294      * system context root.
295      *
296      * @throws javax.naming.NamingException if there are failures
297      */
298     @Test
299     public void testCreateContextWithBasicAttributesCaseSensitive() throws Exception
300     {
301         LdapContext sysRoot = getSystemContext( service );
302 
303         /*
304          * create ou=testing00,ou=system
305          */
306         Attributes attributes = new BasicAttributes();
307         attributes.put("objectClass", "organizationalUnit");
308         attributes.put("description", "Test OU");
309         attributes.put("OU", "Test");
310         
311         DirContext ctx = sysRoot.createSubcontext( "ou=Test", attributes );
312         assertNotNull( ctx );
313 
314         ctx = ( DirContext ) sysRoot.lookup( "ou=Test" );
315         assertNotNull( ctx );
316 
317         attributes = ctx.getAttributes( "" );
318         assertNotNull( attributes );
319         assertEquals( "Test", attributes.get( "ou" ).get() );
320         assertEquals( "Test OU", attributes.get( "Description" ).get() );
321         Attribute attribute = attributes.get( "objectclass" );
322         assertNotNull( attribute );
323         assertTrue( attribute.contains( "top" ) );
324         assertTrue( attribute.contains( "organizationalUnit" ) );
325 
326         /*
327          * create ou=testing01,ou=system
328          */
329         attributes = new BasicAttributes();
330         attribute = new BasicAttribute( "objectClass" );
331         attribute.add( "top" );
332         attribute.add( "organizationalUnit" );
333         attributes.put( attribute );
334         attributes.put( "ou", "testing01" );
335         ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
336         assertNotNull( ctx );
337 
338         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
339         assertNotNull( ctx );
340 
341         attributes = ctx.getAttributes( "" );
342         assertNotNull( attributes );
343         assertEquals( "testing01", attributes.get( "ou" ).get() );
344         attribute = attributes.get( "objectClass" );
345         assertNotNull( attribute );
346         assertTrue( attribute.contains( "top" ) );
347         assertTrue( attribute.contains( "organizationalUnit" ) );
348 
349         /*
350          * create ou=testing02,ou=system
351          */
352         attributes = new BasicAttributes();
353         attribute = new BasicAttribute( "objectClass" );
354         attribute.add( "top" );
355         attribute.add( "organizationalUnit" );
356         attributes.put( attribute );
357         attributes.put( "ou", "testing02" );
358         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
359         assertNotNull( ctx );
360 
361         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
362         assertNotNull( ctx );
363 
364         attributes = ctx.getAttributes( "" );
365         assertNotNull( attributes );
366         assertEquals( "testing02", attributes.get( "ou" ).get() );
367         attribute = attributes.get( "objectClass" );
368         assertNotNull( attribute );
369         assertTrue( attribute.contains( "top" ) );
370         assertTrue( attribute.contains( "organizationalUnit" ) );
371 
372         /*
373          * create ou=subtest,ou=testing01,ou=system
374          */
375         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
376 
377         attributes = new BasicAttributes();
378         attribute = new BasicAttribute( "objectClass" );
379         attribute.add( "top" );
380         attribute.add( "organizationalUnit" );
381         attributes.put( attribute );
382         attributes.put( "ou", "subtest" );
383         ctx = ctx.createSubcontext( "ou=subtest", attributes );
384         assertNotNull( ctx );
385 
386         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
387         assertNotNull( ctx );
388 
389         attributes = ctx.getAttributes( "" );
390         assertNotNull( attributes );
391         assertEquals( "subtest", attributes.get( "ou" ).get() );
392         attribute = attributes.get( "objectClass" );
393         assertNotNull( attribute );
394         assertTrue( attribute.contains( "top" ) );
395         assertTrue( attribute.contains( "organizationalUnit" ) );
396     }
397 
398 
399     @Test
400     public void testCreateContextWithNoObjectClass() throws Exception
401     {
402         LdapContext sysRoot = getSystemContext( service );
403 
404         Attributes attrs = new BasicAttributes( true );
405 
406         try
407         {
408             sysRoot.createSubcontext( "ou=subtest", attrs );// should Fails!
409             fail( "It is not allowed to create a context with a bad entry");
410         }
411         catch ( NamingException e )
412         {
413             assertNotNull( e );
414         }
415     }
416 
417 
418     @Test
419     public void testCreateJavaContainer() throws Exception
420     {
421         LdapContext sysRoot = getSystemContext( service );
422 
423         DirContext ctx = (DirContext)sysRoot.createSubcontext( "cn=subtest" );
424         assertNotNull( ctx );
425         
426         Attributes attributes = ctx.getAttributes( "" );
427         assertNotNull( attributes );
428         
429         assertEquals( "subtest", attributes.get( "cn" ).get() );
430         Attribute attribute = attributes.get( "objectClass" );
431         assertNotNull( attribute );
432         assertTrue( attribute.contains( "top" ) );
433         assertTrue( attribute.contains( "javaContainer" ) );
434     }
435 
436 
437     @Test
438     public void testCreateJavaContainerBadRDN() throws Exception
439     {
440         LdapContext sysRoot = getSystemContext( service );
441 
442         try
443         {
444             sysRoot.createSubcontext( "ou=subtest" );
445             fail( "It is not allowed to create a context with a bad RDN. CN is mandatory");
446         }
447         catch ( LdapSchemaViolationException ne )
448         {
449             assertTrue( true );
450         }
451     }
452 }