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.operations.delete;
21  
22  
23  import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredConnection;
24  import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContextThrowOnRefferal;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import javax.naming.NameNotFoundException;
30  import javax.naming.ReferralException;
31  import javax.naming.ldap.LdapContext;
32  
33  import netscape.ldap.LDAPConnection;
34  import netscape.ldap.LDAPConstraints;
35  import netscape.ldap.LDAPControl;
36  import netscape.ldap.LDAPException;
37  import netscape.ldap.LDAPResponse;
38  import netscape.ldap.LDAPResponseListener;
39  import netscape.ldap.LDAPSearchConstraints;
40  
41  import org.apache.directory.server.core.integ.Level;
42  import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
43  import org.apache.directory.server.core.integ.annotations.CleanupLevel;
44  import org.apache.directory.server.integ.SiRunner;
45  import org.apache.directory.server.ldap.LdapService;
46  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
47  import org.junit.Test;
48  import org.junit.runner.RunWith;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  
53  /**
54   * Integration tests for delete operations.
55   *
56   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
57   * @version $Rev$, $Date$
58   */
59  @RunWith ( SiRunner.class ) 
60  @CleanupLevel ( Level.SUITE )
61  @ApplyLdifs( {
62      // Entry # 1
63      "dn: uid=akarasulu,ou=users,ou=system\n" +
64      "objectClass: uidObject\n" +
65      "objectClass: person\n" +
66      "objectClass: top\n" +
67      "uid: akarasulu\n" +
68      "cn: Alex Karasulu\n" +
69      "sn: karasulu\n\n" + 
70      // Entry # 2
71      "dn: ou=Computers,uid=akarasulu,ou=users,ou=system\n" +
72      "objectClass: organizationalUnit\n" +
73      "objectClass: top\n" +
74      "ou: computers\n" +
75      "description: Computers for Alex\n" +
76      "seeAlso: ou=Machines,uid=akarasulu,ou=users,ou=system\n\n" + 
77      // Entry # 3
78      "dn: uid=akarasuluref,ou=users,ou=system\n" +
79      "objectClass: uidObject\n" +
80      "objectClass: referral\n" +
81      "objectClass: top\n" +
82      "uid: akarasuluref\n" +
83      "ref: ldap://localhost:10389/uid=akarasulu,ou=users,ou=system\n" + 
84      "ref: ldap://foo:10389/uid=akarasulu,ou=users,ou=system\n" +
85      "ref: ldap://bar:10389/uid=akarasulu,ou=users,ou=system\n\n" 
86      }
87  )
88  public class DeleteIT
89  {
90      private static final Logger LOG = LoggerFactory.getLogger( DeleteIT.class );
91      
92      public static LdapService ldapService;
93      
94  
95      /**
96       * Tests normal delete operation on normal non-referral entries without 
97       * the ManageDsaIT control.
98       */
99      @Test
100     public void testNormalDeleteFailContextNotEmpty() throws Exception
101     {
102         LDAPConnection conn = getWiredConnection( ldapService );
103         
104         // delete failure on non-leaf entry
105         try
106         {
107             conn.delete( "uid=akarasulu,ou=users,ou=system" );
108             fail( "Should never get here." );
109         }
110         catch ( LDAPException e )
111         {
112             assertEquals( ResultCodeEnum.NOT_ALLOWED_ON_NON_LEAF.getValue(), e.getLDAPResultCode() );
113         }
114         
115         conn.disconnect();
116     }
117     
118     
119     /**
120      * Tests normal delete operation on normal non-referral entries without 
121      * the ManageDsaIT control.
122      */
123     @Test
124     public void testNormalDelete() throws Exception
125     {
126         LDAPConnection conn = getWiredConnection( ldapService );
127         
128         // delete success
129         conn.delete( "ou=computers,uid=akarasulu,ou=users,ou=system" );
130 
131         // delete failure non-existant entry
132         try
133         {
134             conn.delete( "uid=elecharny,ou=users,ou=system" );
135             fail( "Should never get here." );
136         }
137         catch ( LDAPException e )
138         {
139             assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
140         }
141         
142         conn.disconnect();
143     }
144     
145     
146     /**
147      * Tests normal delete operation on non-existent entries without 
148      * the ManageDsaIT control.
149      */
150     @Test
151     public void testDeleteNonExistent() throws Exception
152     {
153         LDAPConnection conn = getWiredConnection( ldapService );
154         
155         // delete failure non-existent entry
156         try
157         {
158             conn.delete( "uid=elecharny,ou=users,ou=system" );
159             fail( "Should never get here." );
160         }
161         catch ( LDAPException e )
162         {
163             assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
164         }
165         
166         conn.disconnect();
167     }
168     
169     
170     /**
171      * Tests delete operation on referral entry with the ManageDsaIT control.
172      */
173     @Test
174     public void testOnReferralWithManageDsaITControl() throws Exception
175     {
176         LDAPConnection conn = getWiredConnection( ldapService );
177         LDAPConstraints constraints = new LDAPSearchConstraints();
178         constraints.setClientControls( new LDAPControl( LDAPControl.MANAGEDSAIT, true, new byte[0] ) );
179         constraints.setServerControls( new LDAPControl( LDAPControl.MANAGEDSAIT, true, new byte[0] ) );
180         conn.setConstraints( constraints );
181         
182         // delete success
183         conn.delete( "uid=akarasuluref,ou=users,ou=system", constraints );
184         
185         try
186         {
187             conn.read( "uid=akarasuluref,ou=users,ou=system", ( LDAPSearchConstraints ) constraints );
188             fail( "Should never get here." );
189         }
190         catch ( LDAPException e )
191         {
192             assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
193         }
194         
195         conn.disconnect();
196     }
197     
198     
199     /**
200      * Tests delete operation on normal and referral entries without the 
201      * ManageDsaIT control. Referrals are sent back to the client with a
202      * non-success result code.
203      */
204     @Test
205     public void testOnReferral() throws Exception
206     {
207         LDAPConnection conn = getWiredConnection( ldapService );
208         LDAPConstraints constraints = new LDAPConstraints();
209         constraints.setReferrals( false );
210         conn.setConstraints( constraints );
211         
212         // referrals failure
213         LDAPResponseListener listener = null;
214         LDAPResponse response = null;
215 
216         listener = conn.delete( "uid=akarasuluref,ou=users,ou=system", null, constraints );
217         response = listener.getResponse();
218         assertEquals( ResultCodeEnum.REFERRAL.getValue(), response.getResultCode() );
219 
220         assertEquals( "ldap://localhost:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[0] );
221         assertEquals( "ldap://foo:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[1] );
222         assertEquals( "ldap://bar:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[2] );
223 
224         conn.disconnect();
225     }
226     
227     
228     /**
229      * Tests delete operation on normal and referral entries without the 
230      * ManageDsaIT control using JNDI instead of the Netscape API. Referrals 
231      * are sent back to the client with a non-success result code.
232      */
233     @Test
234     public void testThrowOnReferralWithJndi() throws Exception
235     {
236         LdapContext ctx = getWiredContextThrowOnRefferal( ldapService );
237         
238         // delete success
239         ctx.destroySubcontext( "ou=computers,uid=akarasulu,ou=users,ou=system" );
240         
241         try
242         {
243             ctx.lookup( "ou=computers,uid=akarasulu,ou=users,ou=system" );
244             fail( "Should never get here." );
245         }
246         catch ( NameNotFoundException e )
247         {
248         }
249 
250         // referrals failure on delete
251         try
252         {
253             ctx.destroySubcontext( "uid=akarasuluref,ou=users,ou=system" );
254             fail( "Should never get here" );
255         }
256         catch ( ReferralException e )
257         {
258             // seems JNDI only returns the first referral URL and not all so we test for it
259             assertEquals( "ldap://localhost:10389/uid=akarasulu,ou=users,ou=system", e.getReferralInfo() );
260         }
261 
262         ctx.close();
263     }
264     
265     
266     /**
267      * Tests referral handling when an ancestor is a referral.
268      */
269     @Test 
270     public void testAncestorReferral() throws Exception
271     {
272         LOG.debug( "" );
273 
274         LDAPConnection conn = getWiredConnection( ldapService );
275         LDAPConstraints constraints = new LDAPConstraints();
276         conn.setConstraints( constraints );
277 
278         // referrals failure
279         LDAPResponseListener listener = null;
280         LDAPResponse response = null;
281 
282         listener = conn.delete( "ou=Computers,uid=akarasuluref,ou=users,ou=system", null, constraints );
283         response = listener.getResponse();
284         assertEquals( ResultCodeEnum.REFERRAL.getValue(), response.getResultCode() );
285 
286         assertEquals( "ldap://localhost:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[0] );
287         assertEquals( "ldap://foo:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[1] );
288         assertEquals( "ldap://bar:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[2] );
289 
290         conn.disconnect();
291     }
292     
293     
294 
295     /**
296      * Try to delete an entry with invalid DN. Expected result code is 32
297      * (NO_SUCH_OBJECT) or 34 (INVALID_DN_SYNTAX).
298      */
299     public void testDeleteWithIllegalName() throws Exception 
300     {
301         LDAPConnection conn = getWiredConnection( ldapService );
302         
303         try 
304         {
305             conn.delete("This is an illegal name,dc=example,dc=com" );
306             fail( "deletion should fail" );
307         } 
308         catch ( LDAPException e ) 
309         {
310             assertTrue( e.getLDAPResultCode() == LDAPException.INVALID_DN_SYNTAX || 
311                         e.getLDAPResultCode() == LDAPException.NO_SUCH_OBJECT );
312         }
313     }
314 }