john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

LdapSearchTest

// 2012-10-08 johnpfeiffer
// TODO: requires and AD/LDAP server, could use jmock / easymock?

package net.kittyandbear.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;

import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;

import org.junit.Before;
import org.junit.Test;


public class LdapSearchTest
{
    private static final String CLASSVERSION = "0.35";
    private static final String DEFAULTHOST = "10.10.10.228";
    private static final int DEFAULTPORT = 389;
    private static final String DEFAULTBINDUSERDN = "uid=example,ou=system";
    private static final String DEFAULTBINDUSERPASSWORD = "ThePassword";
    private static final String DEFAULTBASEDN = "ou=system";
    private static final String DEFAULTUSERATTRIBUTE = "uid"; // AD uses sAMAccountName
    private static final String DEFAULTSEARCHBASE = "";

    private LdapName defaultBaseDN;
    private LdapName defaultBindUserDN;
    private LdapName defaultSearchBase;
    private LdapSearch ldapSearch = null;

    @Before
    public void setUp() throws Exception
    {
        defaultBindUserDN = new LdapName( DEFAULTBINDUSERDN );
        defaultBaseDN = new LdapName( DEFAULTBASEDN );
        defaultSearchBase = new LdapName( DEFAULTSEARCHBASE );
        ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
    }

    @Test
    public void getVersion()
    {
        assertEquals( CLASSVERSION , LdapSearch.CLASSVERSION );
    }

    /*
     * @Test
     * public void testQueryForLdapName()
     * {
     * fail( "Not yet implemented" );
     * }
     * @Test
     * public void testQueryForBindingUser()
     * {
     * fail( "Not yet implemented" );
     * }
     */

    @Test
    public void testLdapSearchAD() throws InvalidNameException
    {
        String constructorHostname = "10.10.10.235";
        int constructorPort = 389;
        LdapName constructorBaseDN = new LdapName( "dc=company,dc=com" );
        LdapName constructorBindUserDN = new LdapName( "cn=administrator,ou=service,dc=company,dc=com" );
        String constructorPassword = "ThePassword";
        String constructorUserAttribute = "samaccountName";
        LdapName constructorSearchBase = new LdapName( "" );
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( constructorHostname ).port( constructorPort ).baseDN( constructorBaseDN )
                    .bindUserDN( constructorBindUserDN ).password( constructorPassword ).userAttribute( constructorUserAttribute ).searchBase( constructorSearchBase )
                    .build();
            LdapName ldapName = new LdapName( "cn=production_admin" );
            ArrayList <String> results = ldapSearch.queryForLdapName( ldapName );
            System.out.println( "AD User Query Test: " + results.toString() );

        }catch( Exception e )
        {
            System.out.println( e.getMessage() );
        }
    }

    @Test
    public void testLdapSearchLDAP() throws InvalidNameException
    {
        String constructorHostname = "10.10.10.228";
        int constructorPort = 389;
        LdapName constructorBaseDN = new LdapName( "ou=system" );
        LdapName constructorBindUserDN = new LdapName( "uid=example,ou=system" );
        String constructorPassword = "ThePassword";
        String constructorUserAttribute = "uid";
        LdapName constructorSearchBase = new LdapName( "" );
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( constructorHostname ).port( constructorPort ).baseDN( constructorBaseDN )
                    .bindUserDN( constructorBindUserDN ).password( constructorPassword ).userAttribute( constructorUserAttribute ).searchBase( constructorSearchBase )
                    .build();
            LdapName ldapName = new LdapName( "uid=example" );
            ArrayList <String> results = ldapSearch.queryForLdapName( ldapName );
            System.out.println( "LDAP User Query Test: " + results.toString() );

        }catch( Exception e )
        {
            System.out.println( e.getMessage() );
        }
    }

    @Test
    public void testLdapSearchForBindingUserAD() throws InvalidNameException
    {
        String constructorHostname = "10.10.10.235";
        int constructorPort = 389;
        LdapName constructorBaseDN = new LdapName( "dc=company,dc=com" );
        LdapName constructorBindUserDN = new LdapName( "cn=administrator,ou=service,dc=company,dc=com" );
        String constructorPassword = "ThePassword";
        String constructorUserAttribute = "samaccountName";
        LdapName constructorSearchBase = new LdapName( "" );
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( constructorHostname ).port( constructorPort ).baseDN( constructorBaseDN )
                    .bindUserDN( constructorBindUserDN ).password( constructorPassword ).userAttribute( constructorUserAttribute ).searchBase( constructorSearchBase )
                    .build();
            ArrayList <String> results = ldapSearch.queryForBindingUser();
            System.out.println( "AD Binding User Test: " + results.toString() );

        }catch( Exception e )
        {
            System.out.println( e.getMessage() );
        }
    }

    @Test
    public void testLdapSearchForBindingUserLDAP() throws InvalidNameException
    {
        String constructorHostname = "10.10.10.228";
        int constructorPort = 389;
        LdapName constructorBaseDN = new LdapName( "ou=system" );
        LdapName constructorBindUserDN = new LdapName( "uid=example,ou=system" );
        String constructorPassword = "ThePassword";
        String constructorUserAttribute = "uid";
        LdapName constructorSearchBase = new LdapName( "" );
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( constructorHostname ).port( constructorPort ).baseDN( constructorBaseDN )
                    .bindUserDN( constructorBindUserDN ).password( constructorPassword ).userAttribute( constructorUserAttribute ).searchBase( constructorSearchBase )
                    .build();
            ArrayList <String> results = ldapSearch.queryForBindingUser();
            System.out.println( "LDAP Binding User Test: " + results.toString() );

        }catch( Exception e )
        {
            System.out.println( e.getMessage() );
        }
    }

    @Test
    public void testLdapSearchHostnameNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( null ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: hostname cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchPortNegative() throws InvalidNameException
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( -1 ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: port number too small, must be between 0 and 65535" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchPortTooLarge()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( 65536 ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: port number too large, must be between 0 and 65535" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchBaseDNNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( null ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: baseDN cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchBindUserDNNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( null )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: binding user DN cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchPasswordNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN ).password( null )
                    .userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: binding user password cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchUserAttributeNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( null ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: user attribute cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchUserAttributeEmpty()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( "" ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: userAttribute cannot be empty" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchSearchBaseNull()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( null ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: search base cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testLdapSearchUserAttributeNotDefined()
    {
        try
        {
            ldapSearch = new LdapSearch.Builder().hostname( DEFAULTHOST ).port( DEFAULTPORT ).baseDN( defaultBaseDN ).bindUserDN( defaultBindUserDN )
                    .password( DEFAULTBINDUSERPASSWORD ).searchBase( defaultSearchBase ).build();
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: user attribute cannot be null" , e.getMessage() );
            return;
        }
        fail( "Expected IllegalArgumentException" );

    }

    // TODO
    /*
     * @Test
     * public void testLdapSearchLDAPTimeout() throws InvalidNameException
     * {
     * String unvailableHostname = "10.10.10.229";
     * int constructorPort = 389;
     * LdapName constructorBaseDN = new LdapName( "ou=system" );
     * LdapName constructorBindUserDN = new LdapName( "uid=example,ou=system" );
     * String constructorPassword = "ThePassword";
     * String constructorUserAttribute = "uid";
     * LdapName constructorSearchBase = new LdapName( "" );
     * ldapSearch = new LdapSearch.Builder().hostname( unvailableHostname ).port( DEFAULTPORT ).baseDN( constructorBaseDN ).bindUserDN( constructorBindUserDN )
     * .password( DEFAULTBINDUSERPASSWORD ).userAttribute( DEFAULTUSERATTRIBUTE ).searchBase( constructorSearchBase ).build();
     * ArrayList <String> results;
     * try
     * {
     * results = ldapSearch.queryForLdapName( constructorBindUserDN );
     * }catch( SocketTimeoutException e )
     * {
     * return;
     * }
     * System.out.println( "AD User Query Test: " + results.toString() );
     * fail( "Expected SocketTimeoutException" );
     * }
     */
} // end class

  • « LdapSearch
  • LdapSearch.pom.xml »

Published

Oct 8, 2012

Category

java-classes

~875 words

Tags

  • classes 92
  • java 252
  • ldapsearchtest 1