You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2013/09/17 08:30:44 UTC
svn commit: r1523925 -
/directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java
Author: kayyagari
Date: Tue Sep 17 06:30:44 2013
New Revision: 1523925
URL: http://svn.apache.org/r1523925
Log:
tests for all the password hashing interceptors
Modified:
directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java
Modified: directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java?rev=1523925&r1=1523924&r2=1523925&view=diff
==============================================================================
--- directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java (original)
+++ directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/operations/add/PasswordHashingInterceptorTest.java Tue Sep 17 06:30:44 2013
@@ -25,7 +25,9 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
import org.apache.directory.api.ldap.model.constants.SchemaConstants;
@@ -40,9 +42,20 @@ import org.apache.directory.api.ldap.mod
import org.apache.directory.api.ldap.model.password.PasswordUtil;
import org.apache.directory.api.ldap.model.schema.AttributeType;
import org.apache.directory.ldap.client.api.LdapConnection;
-import org.apache.directory.server.core.annotations.ApplyLdifs;
import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.api.interceptor.Interceptor;
+import org.apache.directory.server.core.hash.CryptPasswordHashingInterceptor;
import org.apache.directory.server.core.hash.Md5PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Pkcs5s2PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Sha256PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Sha384PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Sha512PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.ShaPasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Smd5PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Ssha256PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Ssha384PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.Ssha512PasswordHashingInterceptor;
+import org.apache.directory.server.core.hash.SshaPasswordHashingInterceptor;
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
import org.apache.directory.server.core.integ.FrameworkRunner;
import org.apache.directory.server.core.integ.IntegrationUtils;
@@ -56,129 +69,175 @@ import org.junit.runner.RunWith;
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
@RunWith(FrameworkRunner.class)
-@CreateDS(name = "PasswordHashingInterceptorTest-DS", additionalInterceptors = Md5PasswordHashingInterceptor.class)
-@ApplyLdifs(
- {
- "dn: cn=test,ou=system",
- "objectClass: person",
- "cn: test",
- "sn: sn_test",
- "userPassword: secret",
- "userPassword: secret2"
-})
+@CreateDS(name = "PasswordHashingInterceptorTest-DS")
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" );
+ @Test
+ public void testAllMechanisms() throws Exception
+ {
+ List<Class<?>> allMechanism = new ArrayList<Class<?>>();
+ allMechanism.add( CryptPasswordHashingInterceptor.class );
+ allMechanism.add( Md5PasswordHashingInterceptor.class );
+ allMechanism.add( Sha256PasswordHashingInterceptor.class );
+ allMechanism.add( Sha384PasswordHashingInterceptor.class );
+ allMechanism.add( Sha512PasswordHashingInterceptor.class );
+ allMechanism.add( ShaPasswordHashingInterceptor.class );
+ allMechanism.add( Smd5PasswordHashingInterceptor.class );
+ allMechanism.add( Ssha256PasswordHashingInterceptor.class );
+ allMechanism.add( Ssha384PasswordHashingInterceptor.class );
+ allMechanism.add( Ssha512PasswordHashingInterceptor.class );
+ allMechanism.add( SshaPasswordHashingInterceptor.class );
+ allMechanism.add( Pkcs5s2PasswordHashingInterceptor.class );
+
+ Entry entry = new DefaultEntry( service.getSchemaManager(), "cn=test,ou=system",
+ "objectClass: person",
+ "cn: test",
+ "sn: sn_test",
+ "userPassword: secret" );
+
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+
+ List<Interceptor> interceptors = service.getInterceptors();
+
+ for ( int i = 0; i < allMechanism.size(); i++ )
+ {
+ Interceptor hashMech = ( Interceptor ) allMechanism.get( i ).newInstance();
+ hashMech.init( service );
+
+ // make sure to remove the last added mechanism
+ if ( i > 0 )
+ {
+ interceptors.remove( interceptors.size() - 1 );
+ }
+
+ interceptors.add( hashMech );
+
+ service.setInterceptors( interceptors );
+
+ connection.add( entry );
+
+ // System.out.println( "using hash mechanism " + hashMech.getName() );
+
+ testAddWithPlainPassword();
+ testModifyWithPlainPassword();
+ testModifyWithEmptyPassword();
+ testAddWithHashedPassword();
+ testModifyWithHashedPassword();
+
+ connection.delete( entry.getDn() );
+ }
+ }
- 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() ) );
-}
+ public void testAddWithPlainPassword() throws Exception
+ {
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+ byte[] plainPwd = "secret".getBytes();
+ Dn dn = new Dn( "cn=test,ou=system" );
-@Test
-public void testModifyWithPlainPassword() throws Exception
-{
- LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+ Entry entry = connection.lookup( dn );
+ Attribute pwdAt = entry.get( SchemaConstants.USER_PASSWORD_AT );
- byte[] plainPwd = "newsecret".getBytes();
- Dn dn = new Dn( "cn=test,ou=system" );
+ assertFalse( Arrays.equals( plainPwd, pwdAt.getBytes() ) );
+ assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
+ }
- AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
- SchemaConstants.USER_PASSWORD_AT );
- Attribute pwdAt = new DefaultAttribute( pwdAtType );
- pwdAt.add( plainPwd );
+ public void testModifyWithPlainPassword() throws Exception
+ {
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
- Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
- connection.modify( dn, mod );
+ byte[] plainPwd = "newsecret".getBytes();
+ Dn dn = new Dn( "cn=test,ou=system" );
- Entry entry = connection.lookup( dn );
- pwdAt = entry.get( pwdAtType );
+ AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
+ SchemaConstants.USER_PASSWORD_AT );
- assertFalse( Arrays.equals( plainPwd, pwdAt.getBytes() ) );
- assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
-}
+ Attribute pwdAt = new DefaultAttribute( pwdAtType );
+ pwdAt.add( plainPwd );
+ Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
+ connection.modify( dn, mod );
-@Test
-public void testModifyWithEmptyPassword() throws Exception
-{
- LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+ Entry entry = connection.lookup( dn );
+ pwdAt = entry.get( pwdAtType );
- Dn dn = new Dn( "cn=test,ou=system" );
+ assertFalse( Arrays.equals( plainPwd, pwdAt.getBytes() ) );
+ assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
+ }
- AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
- SchemaConstants.USER_PASSWORD_AT );
- Attribute pwdAt = new DefaultAttribute( pwdAtType );
- pwdAt.add( ( byte[] ) null );
+ public void testModifyWithEmptyPassword() throws Exception
+ {
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
- Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
- connection.modify( dn, mod );
+ Dn dn = new Dn( "cn=test,ou=system" );
- Entry entry = connection.lookup( dn );
- pwdAt = entry.get( pwdAtType );
+ AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
+ SchemaConstants.USER_PASSWORD_AT );
- assertNull( pwdAt );
-}
+ Attribute pwdAt = new DefaultAttribute( pwdAtType );
+ pwdAt.add( ( byte[] ) null );
+ Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
+ connection.modify( dn, mod );
-@Test
-public void testAddWithHashedPassword() throws Exception
-{
- LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+ Entry entry = connection.lookup( dn );
+ pwdAt = entry.get( pwdAtType );
- byte[] plainPwd = "secret".getBytes();
- byte[] hashedPwd = PasswordUtil.createStoragePassword( plainPwd, LdapSecurityConstants.HASH_METHOD_SSHA );
+ assertNull( pwdAt );
+ }
- 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() ) );
-}
+ public void testAddWithHashedPassword() throws Exception
+ {
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
-@Test
-public void testModifyWithHashedPassword() throws Exception
-{
- LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
+ byte[] plainPwd = "secret".getBytes();
+ byte[] hashedPwd = PasswordUtil.createStoragePassword( plainPwd, LdapSecurityConstants.HASH_METHOD_SSHA );
- byte[] plainPwd = "xyzsecret".getBytes();
- byte[] hashedPwd = PasswordUtil.createStoragePassword( plainPwd, LdapSecurityConstants.HASH_METHOD_SSHA256 );
+ 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 );
- Dn dn = new Dn( "cn=test,ou=system" );
+ connection.add( entry );
- AttributeType pwdAtType = getService().getSchemaManager().lookupAttributeTypeRegistry(
- SchemaConstants.USER_PASSWORD_AT );
+ entry = connection.lookup( dn );
+ Attribute pwdAt = entry.get( SchemaConstants.USER_PASSWORD_AT );
+ assertTrue( Arrays.equals( hashedPwd, pwdAt.getBytes() ) );
+ assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
- Attribute pwdAt = new DefaultAttribute( pwdAtType );
- pwdAt.add( hashedPwd );
+ connection.delete( dn );
+ }
- Modification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, pwdAt );
- connection.modify( dn, mod );
- Entry entry = connection.lookup( dn );
- pwdAt = entry.get( pwdAtType );
+ public void testModifyWithHashedPassword() throws Exception
+ {
+ LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );
- assertTrue( Arrays.equals( hashedPwd, pwdAt.getBytes() ) );
- assertTrue( PasswordUtil.compareCredentials( plainPwd, pwdAt.getBytes() ) );
-}
+ 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() ) );
+ }
}