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.constants.JndiPropertyConstants;
27  import org.apache.directory.shared.ldap.message.AliasDerefMode;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  
34  import javax.naming.NameNotFoundException;
35  import javax.naming.NamingException;
36  import javax.naming.directory.Attribute;
37  import javax.naming.directory.Attributes;
38  import javax.naming.directory.BasicAttribute;
39  import javax.naming.directory.BasicAttributes;
40  import javax.naming.directory.DirContext;
41  import javax.naming.directory.SearchControls;
42  import javax.naming.ldap.LdapContext;
43  
44  
45  /**
46   * Tests the search() methods of the provider.
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   * @version $Rev: 493916 $
50   */
51  @RunWith ( CiRunner.class )
52  public class DIRSERVER759IT
53  {
54      public static DirectoryService service;
55  
56  
57      /**
58       * @todo replace with ldif annotations
59       *
60       * @throws NamingException on errors
61       */
62      protected void createData() throws Exception
63      {
64          LdapContext sysRoot = getSystemContext( service );
65  
66          /*
67           * create ou=testing00,ou=system
68           */
69          Attributes attributes = new BasicAttributes( true );
70          Attribute attribute = new BasicAttribute( "objectClass" );
71          attribute.add( "top" );
72          attribute.add( "organizationalUnit" );
73          attributes.put( attribute );
74          attributes.put( "ou", "testing00" );
75  
76          DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
77          assertNotNull( ctx );
78  
79          ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
80          assertNotNull( ctx );
81          attributes = ctx.getAttributes( "" );
82          assertNotNull( attributes );
83          assertEquals( "testing00", attributes.get( "ou" ).get() );
84          attribute = attributes.get( "objectClass" );
85          assertNotNull( attribute );
86          assertTrue( attribute.contains( "top" ) );
87          assertTrue( attribute.contains( "organizationalUnit" ) );
88  
89          /*
90           * create ou=testing01,ou=system
91           */
92          attributes = new BasicAttributes( true );
93          attribute = new BasicAttribute( "objectClass" );
94          attribute.add( "top" );
95          attribute.add( "organizationalUnit" );
96          attributes.put( attribute );
97          attributes.put( "ou", "testing01" );
98  
99          ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
100         assertNotNull( ctx );
101 
102         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
103         assertNotNull( ctx );
104         attributes = ctx.getAttributes( "" );
105         assertNotNull( attributes );
106         assertEquals( "testing01", attributes.get( "ou" ).get() );
107         attribute = attributes.get( "objectClass" );
108         assertNotNull( attribute );
109         assertTrue( attribute.contains( "top" ) );
110         assertTrue( attribute.contains( "organizationalUnit" ) );
111 
112         /*
113          * create ou=testing02,ou=system
114          */
115         attributes = new BasicAttributes( true );
116         attribute = new BasicAttribute( "objectClass" );
117         attribute.add( "top" );
118         attribute.add( "organizationalUnit" );
119         attributes.put( attribute );
120         attributes.put( "ou", "testing02" );
121         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
122         assertNotNull( ctx );
123 
124         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
125         assertNotNull( ctx );
126 
127         attributes = ctx.getAttributes( "" );
128         assertNotNull( attributes );
129         assertEquals( "testing02", attributes.get( "ou" ).get() );
130 
131         attribute = attributes.get( "objectClass" );
132         assertNotNull( attribute );
133         assertTrue( attribute.contains( "top" ) );
134         assertTrue( attribute.contains( "organizationalUnit" ) );
135 
136         /*
137          * create ou=subtest,ou=testing01,ou=system
138          */
139         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
140 
141         attributes = new BasicAttributes( true );
142         attribute = new BasicAttribute( "objectClass" );
143         attribute.add( "top" );
144         attribute.add( "organizationalUnit" );
145         attributes.put( attribute );
146         attributes.put( "ou", "subtest" );
147 
148         ctx = ctx.createSubcontext( "ou=subtest", attributes );
149         assertNotNull( ctx );
150 
151         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
152         assertNotNull( ctx );
153 
154         attributes = ctx.getAttributes( "" );
155         assertNotNull( attributes );
156         assertEquals( "subtest", attributes.get( "ou" ).get() );
157 
158         attribute = attributes.get( "objectClass" );
159         assertNotNull( attribute );
160         assertTrue( attribute.contains( "top" ) );
161         assertTrue( attribute.contains( "organizationalUnit" ) );
162     }
163 
164 
165     @Test
166     public void testSearchBadDN() throws Exception
167     {
168         createData();
169         LdapContext sysRoot = getSystemContext( service );
170 
171         SearchControls controls = new SearchControls();
172         controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
173         controls.setDerefLinkFlag( false );
174         sysRoot.addToEnvironment( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES,
175                 AliasDerefMode.NEVER_DEREF_ALIASES.getJndiValue() );
176 
177         try
178         {
179             sysRoot.search( "cn=admin", "(objectClass=*)", controls );
180         }
181         catch ( NameNotFoundException nnfe )
182         {
183             assertTrue( true );
184         }
185     }
186 }