AuthenticationPolicyTestCase.java revision 88f16d892d54fd8c3e190cc1f6363638b11ae1a3
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2011-2015 ForgeRock AS.
* Portions Copyright 2014 ForgeRock AS
*/
/**
* Test authentication policy interaction.
*/
public class AuthenticationPolicyTestCase extends APITestCase
{
/**
* A mock policy which records which methods have been called and their
* parameters.
*/
private final class MockPolicy extends AuthenticationPolicy
{
private final boolean isDisabled;
private final boolean matches;
private boolean isPolicyFinalized;
private boolean isStateFinalized;
private ByteString matchedPassword;
/**
* Returns {@code true} if {@code finalizeAuthenticationPolicy} was called.
*
* @return {@code true} if {@code finalizeAuthenticationPolicy} was called.
*/
public boolean isPolicyFinalized()
{
return isPolicyFinalized;
}
/**
* Returns {@code true} if {@code finalizeStateAfterBind} was called.
*
* @return {@code true} if {@code finalizeStateAfterBind} was called.
*/
public boolean isStateFinalized()
{
return isStateFinalized;
}
/**
* Returns the password which was tested.
*
* @return The password which was tested.
*/
public ByteString getMatchedPassword()
{
return matchedPassword;
}
/**
* Creates a new mock policy.
*
* @param matches
* The result to always return from {@code passwordMatches}.
* @param isDisabled
* The result to return from {@code isDisabled}.
*/
{
this.isDisabled = isDisabled;
}
/**
* {@inheritDoc}
*/
{
return policyDN;
}
/**
* {@inheritDoc}
*/
{
return new AuthenticationPolicyState(userEntry)
{
/**
* {@inheritDoc}
*/
throws DirectoryException
{
return matches;
}
/**
* {@inheritDoc}
*/
public boolean isDisabled()
{
return MockPolicy.this.isDisabled;
}
/**
* {@inheritDoc}
*/
public void finalizeStateAfterBind() throws DirectoryException
{
isStateFinalized = true;
}
/**
* {@inheritDoc}
*/
{
return MockPolicy.this;
}
};
}
/**
* {@inheritDoc}
*/
public void finalizeAuthenticationPolicy()
{
isPolicyFinalized = true;
}
}
/**
* Ensures that the Directory Server is running and creates a test backend
* containing a single test user.
*
* @throws Exception
* If an unexpected problem occurs.
*/
public void beforeClass() throws Exception
{
}
/**
*
*/
public Object[][] testBindData()
{
// @formatter:off
return new Object[][] {
/* password matches, account is disabled */
{ false, false },
{ false, true },
{ true, false },
{ true, true },
};
// @formatter:on
}
/**
* Test simple authentication where password validation succeeds.
*
* @param matches
* The result to always return from {@code passwordMatches}.
* @param isDisabled
* The result to return from {@code isDisabled}.
* @throws Exception
* If an unexpected exception occurred.
*/
throws Exception
{
try
{
// Create an empty test backend 'o=test'
/*
* The test user which who will be authenticated.
*/
/* @formatter:off */
"dn: " + userDNString,
"objectClass: top",
"objectClass: person",
"ds-pwp-password-policy-dn: " + policyDNString,
"userPassword: password",
"sn: user",
"cn: test user"
/* @formatter:on */
);
// Perform the simple bind.
// Check authentication result.
if (!isDisabled)
{
}
else
{
// If the account is disabled then the password should not have been
// checked. This is important because we want to avoid potentially
// expensive password fetches (e.g. PTA).
}
}
finally
{
}
}
/**
* Test simple authentication where password validation succeeds.
*
* @param matches
* The result to always return from {@code passwordMatches}.
* @param isDisabled
* The result to return from {@code isDisabled}.
* @throws Exception
* If an unexpected exception occurred.
*/
throws Exception
{
try
{
// Create an empty test backend 'o=test'
/*
* The test user which who will be authenticated.
*/
/* @formatter:off */
"dn: " + userDNString,
"objectClass: top",
"objectClass: person",
"ds-pwp-password-policy-dn: " + policyDNString,
"userPassword: password",
"sn: user",
"cn: test user"
/* @formatter:on */
);
// Perform the simple bind.
// Check authentication result.
if (!isDisabled)
{
}
else
{
// If the account is disabled then the password should not have been
// checked. This is important because we want to avoid potentially
// expensive password fetches (e.g. PTA).
}
}
finally
{
}
}
}