1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.operational;
18  
19  
20  import org.apache.ldap.server.AbstractCoreTest;
21  
22  import javax.naming.NamingException;
23  import javax.naming.directory.Attribute;
24  import javax.naming.directory.Attributes;
25  import javax.naming.directory.BasicAttributes;
26  import javax.naming.directory.DirContext;
27  
28  
29  /***
30   * Tests to see that the binary property filtering in the schema service's
31   * filter class {@link org.apache.ldap.server.schema.SchemaService} is working
32   * properly.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 165059 $
36   */
37  public class BinaryAttributeFilterTest extends AbstractCoreTest
38  {
39      private static final String BINARY_KEY = "java.naming.ldap.attributes.binary";
40  
41  
42      public void testBinaryExtension() throws NamingException
43      {
44          Attributes attributes = new BasicAttributes();
45          attributes.put( "objectClass", "top" );
46          attributes.put( "objectClass", "organizationalUnit" );
47          attributes.put( "objectClass", "extensibleObject" );
48          attributes.put( "ou", "testing" );
49          sysRoot.createSubcontext( "ou=test", attributes );
50  
51          // test without turning on the property
52          DirContext ctx = ( DirContext ) sysRoot.lookup( "ou=test" ) ;
53          Attribute ou = ctx.getAttributes( "" ).get( "ou" );
54          Object value = ou.get();
55          assertTrue( value instanceof String );
56  
57          // test with the property now making ou into a binary value
58          sysRoot.addToEnvironment( BINARY_KEY, "ou" );
59          ctx = ( DirContext ) sysRoot.lookup( "ou=test" ) ;
60          ou = ctx.getAttributes( "" ).get( "ou" );
61          value = ou.get();
62          assertTrue( value instanceof byte[] );
63  
64          // try krb5Key which should be binary automatically - use ou as control
65          byte[] keyValue = new byte[] { 0x45, 0x23, 0x7d, 0x7f };
66          attributes.put( "jpegPhoto", keyValue );
67          sysRoot.createSubcontext( "ou=anothertest", attributes );
68          ctx = ( DirContext ) sysRoot.lookup( "ou=anothertest" ) ;
69          ou = ctx.getAttributes( "" ).get( "ou" );
70          value = ou.get();
71          assertTrue( value instanceof byte[] );
72          Attribute jpegPhoto = ctx.getAttributes( "" ).get( "jpegPhoto" );
73          value = jpegPhoto.get();
74          assertTrue( value instanceof byte[] );
75  
76          // try jpegPhoto which should be binary automatically but use String to
77          // create so we should still get back a byte[] - use ou as control
78          attributes.remove( "jpegPhoto" );
79          attributes.put( "jpegPhoto", "testing a string" );
80          sysRoot.createSubcontext( "ou=yetanothertest", attributes );
81          ctx = ( DirContext ) sysRoot.lookup( "ou=yetanothertest" ) ;
82          ou = ctx.getAttributes( "" ).get( "ou" );
83          value = ou.get();
84          assertTrue( value instanceof byte[] );
85          jpegPhoto = ctx.getAttributes( "" ).get( "jpegPhoto" );
86          value = jpegPhoto.get();
87          assertTrue( value instanceof byte[] );
88      }
89  }