Package org.apache.directory.ldap.client.api

Examples of org.apache.directory.ldap.client.api.LdapConnection


     * test a search request perf.
     */
    @Test
    public void testSearchRequestSubtreeLevelScopePerf() throws Exception
    {
        LdapConnection connection = new LdapNetworkConnection( "localhost", getLdapServer().getPort() );
        connection.setTimeOut( 0 );

        try
        {
            // Use the client API as JNDI cannot be used to do a search without
            // first binding. (hmmm, even client API won't allow searching without binding)
            connection.bind( "uid=admin,ou=system", "secret" );

            // Searches for all the entries in ou=system
            EntryCursor cursor = connection.search( "ou=system", "(ObjectClass=*)",
                SearchScope.SUBTREE, "*" );

            int i = 0;

            while ( cursor.next() )
            {
                cursor.get();
                ++i;
            }

            cursor.close();
            assertEquals( 10, i );

            for ( int j = 0; j < 10000; j++ )
            {
                cursor = connection.search( "ou=system", "(ObjectClass=*)", SearchScope.SUBTREE, "*" );

                while ( cursor.next() )
                {
                    cursor.get();
                }

                cursor.close();
            }

            Dn dn = new Dn( getService().getSchemaManager(), "uid=admin,ou=system" );

            SearchRequest searchRequest = new SearchRequestImpl();

            searchRequest.setBase( dn );
            searchRequest.setFilter( "(ObjectClass=*)" );
            searchRequest.setScope( SearchScope.SUBTREE );
            searchRequest.addAttributes( "*" );
            searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

            long t0 = System.currentTimeMillis();
            long t00 = 0L;
            long tt0 = System.currentTimeMillis();
            int nbIterations = 200000;
            int count = 0;

            for ( int j = 0; j < nbIterations; j++ )
            {
                if ( j % 10000 == 0 )
                {
                    long tt1 = System.currentTimeMillis();

                    System.out.println( j + ", " + ( tt1 - tt0 ) );
                    tt0 = tt1;
                }

                if ( j == 50000 )
                {
                    t00 = System.currentTimeMillis();
                }

                cursor = connection.search( "ou=system", "(ObjectClass=*)", SearchScope.SUBTREE, "*" );

                while ( cursor.next() )
                {
                    count++;
                    cursor.get();
                }

                cursor.close();
            }

            long t1 = System.currentTimeMillis();

            Long deltaWarmed = ( t1 - t00 );
            System.out.println( "SUB level - Delta : " + deltaWarmed + "( " + ( ( ( nbIterations - 50000 ) * 1000 ) / deltaWarmed )
                * 10
                + " per s ) /" + ( t1 - t0 ) + ", count : " + count );
        }
        catch ( LdapException e )
        {
            e.printStackTrace();
            fail( "Should not have caught exception." );
        }
        finally
        {
            connection.unBind();
            connection.close();
        }
    }
View Full Code Here



    @Test
    public void testSearch100kUsers() throws LdapException, CursorException
    {
        LdapConnection connection = new LdapNetworkConnection( "localhost", getLdapServer().getPort() );
        connection.bind( "uid=admin,ou=system", "secret" );

        Entry rootPeople = new DefaultEntry(
            "ou=People,dc=example,dc=com",
            "objectClass: top",
            "objectClass: organizationalUnit",
            "ou: People" );

        connection.add( rootPeople );

        long tadd0 = System.currentTimeMillis();
        for ( int i = 0; i < 10000; i++ )
        {
            Entry user = new DefaultEntry(
                "uid=user." + i + ",ou=People,dc=example,dc=com",
                "objectClass: top",
                "objectClass: person",
                "objectClass: organizationalPerson",
                "objectClass: inetOrgPerson",
                "givenName: Aaccf",
                "sn: Amar",
                "cn", "user" + i,
                "initials: AA",
                "uid", "user." + i,
                "mail: user.1@cs.hacettepe.edu.tr",
                "userPassword: password",
                "telephoneNumber: 314-796-3178",
                "homePhone: 514-847-0518",
                "pager: 784-600-5445",
                "mobile: 801-755-4931",
                "street: 00599 First Street",
                "l: Augusta",
                "st: MN",
                "postalCode: 30667",
                "postalAddress: Aaccf Amar$00599 First Street$Augusta, MN  30667",
                "description: This is the description for Aaccf Amar." );

            connection.add( user );

            if ( i % 100 == 0 )
            {
                System.out.println( "Injected " + i );
            }
        }
        long tadd1 = System.currentTimeMillis();

        System.out.println( "Time to inject 100k entries : " + ( ( tadd1 - tadd0 ) / 1000 ) + "s" );

        // Now do a random search
        SearchRequest searchRequest = new SearchRequestImpl();

        searchRequest.setBase( new Dn( "dc=example,dc=com" ) );
        searchRequest.setScope( SearchScope.SUBTREE );
        searchRequest.addAttributes( "*" );
        searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

        long t0 = System.currentTimeMillis();
        long t00 = 0L;
        long tt0 = System.currentTimeMillis();
        int nbIterations = 200000;
        int count = 0;
        Random random = new Random();

        for ( int j = 0; j < nbIterations; j++ )
        {
            if ( j % 10000 == 0 )
            {
                long tt1 = System.currentTimeMillis();

                System.out.println( j + ", " + ( tt1 - tt0 ) );
                tt0 = tt1;
            }

            if ( j == 50000 )
            {
                t00 = System.currentTimeMillis();
            }

            searchRequest.setFilter( "(cn=user" + random.nextInt( 10000 ) + ")" );

            SearchCursor cursor = connection.search( searchRequest );

            while ( cursor.next() )
            {
                count++;
                cursor.getEntry();
View Full Code Here

public class PasswordHashingInterceptorTest extends AbstractLdapTestUnit
{
@Test
public void testAddWithPlainPassword() throws Exception
{
    LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

    byte[] plainPwd = "secret".getBytes();
    Dn dn = new Dn( "cn=test,ou=system" );

    Entry entry = connection.lookup( dn );
    Attribute pwdAt = entry.get( SchemaConstants.USER_PASSWORD_AT );

    assertFalse( Arrays.equals( plainPwd, pwdAt.getBytes() ) );
    assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
}
View Full Code Here


@Test
public void testModifyWithPlainPassword() throws Exception
{
    LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

    byte[] plainPwd = "newsecret".getBytes();
    Dn dn = new Dn( "cn=test,ou=system" );

    AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
        SchemaConstants.USER_PASSWORD_AT );

    Attribute pwdAt = new DefaultAttribute( pwdAtType );
    pwdAt.add( plainPwd );

    Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
    connection.modify( dn, mod );

    Entry entry = connection.lookup( dn );
    pwdAt = entry.get( pwdAtType );

    assertFalse( Arrays.equals( plainPwd, pwdAt.getBytes() ) );
    assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
}
View Full Code Here


@Test
public void testModifyWithEmptyPassword() throws Exception
{
    LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

    Dn dn = new Dn( "cn=test,ou=system" );

    AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
        SchemaConstants.USER_PASSWORD_AT );

    Attribute pwdAt = new DefaultAttribute( pwdAtType );
    pwdAt.add( ( byte[] ) null );

    Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
    connection.modify( dn, mod );

    Entry entry = connection.lookup( dn );
    pwdAt = entry.get( pwdAtType );

    assertNull( pwdAt );
}
View Full Code Here


@Test
public void testAddWithHashedPassword() throws Exception
{
    LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

    byte[] plainPwd = "secret".getBytes();
    byte[] hashedPwd = PasswordUtil.createStoragePassword( plainPwd, LdapSecurityConstants.HASH_METHOD_SSHA );

    Dn dn = new Dn( "cn=testHash,ou=system" );
    Entry entry = new DefaultEntry( getService().getSchemaManager(), dn );
    entry.add( "ObjectClass", "top", "person" );
    entry.add( "sn", "TEST" );
    entry.add( "cn", "testHash" );
    entry.add( SchemaConstants.USER_PASSWORD_AT, hashedPwd );

    connection.add( entry );

    entry = connection.lookup( dn );
    Attribute pwdAt = entry.get( SchemaConstants.USER_PASSWORD_AT );
    assertTrue( Arrays.equals( hashedPwd, pwdAt.getBytes() ) );
    assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
}
View Full Code Here


@Test
public void testModifyWithHashedPassword() throws Exception
{
    LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

    byte[] plainPwd = "xyzsecret".getBytes();
    byte[] hashedPwd = PasswordUtil.createStoragePassword( plainPwd, LdapSecurityConstants.HASH_METHOD_SSHA256 );

    Dn dn = new Dn( "cn=test,ou=system" );

    AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
        SchemaConstants.USER_PASSWORD_AT );

    Attribute pwdAt = new DefaultAttribute( pwdAtType );
    pwdAt.add( hashedPwd );

    Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
    connection.modify( dn, mod );

    Entry entry = connection.lookup( dn );
    pwdAt = entry.get( pwdAtType );

    assertTrue( Arrays.equals( hashedPwd, pwdAt.getBytes() ) );
    assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
}
View Full Code Here

     * test an abandonned search request.
     */
    @Test
    public void testAbandonnedRequest() throws Exception
    {
        LdapConnection asyncCnx = new LdapNetworkConnection( "localhost", getLdapServer().getPort() );
        EntryCursor cursor = null;

        try
        {
            // Use the client API as JNDI cannot be used to do a search without
            // first binding. (hmmm, even client API won't allow searching without binding)
            asyncCnx.bind( "uid=admin,ou=system", "secret" );

            // First, add 100 entries in the server
            for ( int i = 0; i < 100; i++ )
            {
                String dn = "cn=user" + i + "," + BASE;
                Entry kate = new DefaultEntry( dn );

                kate.add( "objectclass", "top", "person" );
                kate.add( "sn", "Bush" );
                kate.add( "cn", "user" + i );

                asyncCnx.add( kate );
            }

            // Searches for all the entries in ou=system
            cursor = asyncCnx.search( "ou=system", "(ObjectClass=*)", SearchScope.SUBTREE, "*" );

            // Now loop on all the elements found, and abandon after 10 elements returned
            int count = 0;

            while ( cursor.next() )
            {
                count++;

                if ( count == 10 )
                {
                    // the message ID = 1 bind op + 100 add ops + 1 search op
                    asyncCnx.abandon( 102 );
                }
            }

            assertEquals( 10, count );
        }
        catch ( LdapException e )
        {
            e.printStackTrace();
            fail( "Should not have caught exception." );
        }
        finally
        {
            asyncCnx.unBind();
            asyncCnx.close();
            cursor.close();
        }
    }
View Full Code Here

    @Test
    public void testSearchSizeLimit() throws Exception
    {
        long sizeLimit = 7;
        LdapConnection connection = getAdminConnection( getLdapServer() );
        SearchRequest req = new org.apache.directory.api.ldap.model.message.SearchRequestImpl();
        req.setBase( new Dn( "ou=system" ) );
        req.setFilter( "(ou=*)" );
        req.setScope( SearchScope.SUBTREE );
        req.setSizeLimit( sizeLimit );

        Cursor<Response> cursor = connection.search( req );
        long i = 0;

        // Equivalent to : while ( cursor.next() )
        for ( Response response : cursor )
        {
            ++i;
        }

        cursor.close();

        assertEquals( sizeLimit, i );
        connection.close();
    }
View Full Code Here

    @Test
    @Ignore("This test is failing because of the timing issue. Note that the SearchHandler handles time based searches correctly, this is just the below test's problem")
    public void testSearchTimeLimit() throws Exception, InterruptedException
    {
        LdapConnection connection = getAdminConnection( getLdapServer() );
        SearchRequest req = new SearchRequestImpl();
        req.setBase( new Dn( "ou=schema" ) );
        req.setFilter( "(objectClass=*)" );
        req.setScope( SearchScope.SUBTREE );

        Cursor<Response> cursor = connection.search( req );
        int count = 0;

        while ( cursor.next() )
        {
            ++count;
        }

        cursor.close();

        req.setTimeLimit( 1 );
        cursor = connection.search( req );
        int newCount = 0;

        while ( cursor.next() )
        {
            ++newCount;
        }

        assertTrue( newCount < count );
        connection.close();
    }
View Full Code Here

TOP

Related Classes of org.apache.directory.ldap.client.api.LdapConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.