blob: 8d8d2793f2d57e87ad55a9a93fed49dfae291a4d [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.directory.server;
import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.apache.directory.server.unit.AbstractServerTest;
import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
/**
* For DIRSERVER-715 and part of DIRSERVER-169. May include other tests
* for binary attribute based searching.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$
*/
public class BinarySearchTest extends AbstractServerTest
{
private LdapContext ctx = null;
/**
* Creation of required attributes of a person entry.
*/
protected Attributes getPersonAttributes( String sn, String cn )
{
Attributes attributes = new LockableAttributesImpl();
Attribute attribute = new LockableAttributeImpl( "objectClass" );
attribute.add( "top" );
attribute.add( "person" );
attributes.put( attribute );
attributes.put( "cn", cn );
attributes.put( "sn", sn );
return attributes;
}
/**
* Create context and a person entry.
*/
public void setUp() throws Exception
{
super.setUp();
Hashtable env = new Hashtable();
env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
env.put( "java.naming.provider.url", "ldap://localhost:" + port + "/ou=system" );
env.put( "java.naming.security.principal", "uid=admin,ou=system" );
env.put( "java.naming.security.credentials", "secret" );
env.put( "java.naming.security.authentication", "simple" );
ctx = new InitialLdapContext( env, null );
assertNotNull( ctx );
}
/**
* Remove person entry and close context.
*/
public void tearDown() throws Exception
{
ctx.close();
ctx = null;
super.tearDown();
}
/**
* TODO re-enable this test after fixing binary attribute searches.
*
* @throws Exception
*/
public void testSearchByBinaryAttribute() throws Exception
{
byte[] certData = new byte[] { 0x34, 0x56, 0x4e, 0x5f };
// First let's add a some binary data representing a userCertificate
Attributes attrs = getPersonAttributes( "Bush", "Kate Bush" );
Attribute objectClasses = attrs.get( "objectClass" );
objectClasses.add( "strongAuthenticationUser" );
attrs.put( "userCertificate", certData );
ctx.createSubcontext( "cn=Kate Bush", attrs );
// Search for kate by cn first
SearchControls controls = new SearchControls();
controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
NamingEnumeration enm = ctx.search( "", "(cn=Kate Bush)", controls );
assertTrue( enm.hasMore() );
SearchResult sr = ( SearchResult ) enm.next();
assertNotNull( sr );
assertFalse( enm.hasMore() );
assertEquals( "cn=Kate Bush", sr.getName() );
// TODO enable this test here
// Failing here below this due to the frontend interpretting the byte[]
// as a String value. I see the value sent to the SearchHandler of the
// filter to be a String looking like: "[B@17210a5".
enm = ctx.search( "", "(userCertificate={0})", new Object[] {certData}, controls );
assertTrue( enm.hasMore() );
sr = ( SearchResult ) enm.next();
assertNotNull( sr );
assertFalse( enm.hasMore() );
assertEquals( "cn=Kate Bush", sr.getName() );
}
}