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.modify;
21  
22  
23  import org.apache.directory.server.core.integ.Level;
24  import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
25  import org.apache.directory.server.core.integ.annotations.CleanupLevel;
26  import org.apache.directory.server.integ.SiRunner;
27  import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredConnection;
28  
29  import org.apache.directory.server.ldap.LdapService;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import static org.junit.Assert.fail;
33  import static org.junit.Assert.assertEquals;
34  
35  import netscape.ldap.LDAPAttribute;
36  import netscape.ldap.LDAPConnection;
37  import netscape.ldap.LDAPEntry;
38  import netscape.ldap.LDAPException;
39  import netscape.ldap.LDAPModification;
40  
41  
42  /** 
43   * A test taken from DIRSERVER-630: If one tries to add an attribute to an 
44   * entry, and does not provide a value, it is assumed that the server does 
45   * not modify the entry. We have a situation here using Sun ONE Directory 
46   * SDK for Java, where adding a description attribute without value to a 
47   * person entry like this,
48   * <code>
49   * dn: cn=Kate Bush,dc=example,dc=com
50   * objectclass: person
51   * objectclass: top
52   * sn: Bush
53   * cn: Kate Bush
54   * </code> 
55   * does not fail (modify call does not result in an exception). Instead, a 
56   * description attribute is created within the entry. At least the new 
57   * attribute is readable with Netscape SDK (it is not visible to most UIs, 
58   * because it is invalid ...). 
59   * 
60   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
61   * @version $Rev: $
62   */
63  @RunWith ( SiRunner.class ) 
64  @CleanupLevel ( Level.SUITE )
65  @ApplyLdifs( {
66      // Entry # 1
67      "dn: cn=Kate Bush,ou=system\n" +
68      "objectClass: person\n" +
69      "objectClass: top\n" +
70      "cn: Kate Bush\n" +
71      "sn: Bush\n\n" 
72      }
73  )
74  public class IllegalModificationIT 
75  {
76      private static final String DN = "cn=Kate Bush,ou=system";
77  
78      public static LdapService ldapService;
79      
80  
81      @Test
82      public void testIllegalModification() throws Exception
83      {
84          LDAPConnection con = getWiredConnection( ldapService );
85          LDAPAttribute attr = new LDAPAttribute( "description" );
86          LDAPModification mod = new LDAPModification( LDAPModification.ADD, attr );
87  
88          try
89          {
90              con.modify( "cn=Kate Bush,ou=system", mod );
91              fail( "error expected due to empty attribute value" );
92          }
93          catch ( LDAPException e )
94          {
95              // expected
96          }
97  
98          // Check whether entry is unmodified, i.e. no description
99          LDAPEntry entry = con.read( DN );
100         assertEquals( "description exists?", null, entry.getAttribute( "description" ) );
101     }
102     
103     
104     @Test
105     public void testIllegalModification2() throws Exception
106     {
107         LDAPConnection con = getWiredConnection( ldapService );
108 
109         // first a valid attribute
110         LDAPAttribute attr = new LDAPAttribute( "description", "The description" );
111         LDAPModification mod = new LDAPModification( LDAPModification.ADD, attr );
112         // then an invalid one without any value
113         attr = new LDAPAttribute( "displayName" );
114         LDAPModification mod2 = new LDAPModification( LDAPModification.ADD, attr );
115 
116         try
117         {
118             con.modify( "cn=Kate Bush,ou=system", new LDAPModification[] { mod, mod2 } );
119             fail( "error expected due to empty attribute value" );
120         }
121         catch ( LDAPException e )
122         {
123             // expected
124         }
125 
126         // Check whether entry is unmodified, i.e. no displayName
127         LDAPEntry entry = con.read( DN );
128         assertEquals( "displayName exists?", null, entry.getAttribute( "displayName" ) );
129     }
130 }