* @throws Exception if an error occurs while running the test.
*/
public void testInputValidator() throws Exception
{
// let's start with a valid username/password pair.
LoginContext context = new LoginContext("testInputValidator", new UsernamePasswordHandler("user", "secret"));
context.login();
assertNotNull(context.getSubject());
context.logout();
// now let's try a username that doesn't conform to the [A-Za-z0-9]* pattern.
context = new LoginContext("testInputValidator", new UsernamePasswordHandler("$user$", "secret"));
try
{
context.login();
fail("Login should have failed as the supplied username does not adhere to the expected pattern");
}
catch(LoginException le)
{
assertEquals("Username or password does not adhere to the acceptable pattern", le.getMessage());
}
// now let's try a password that doesn't conform to the pattern by including a space in the middle of the password).
context = new LoginContext("testInputValidator", new UsernamePasswordHandler("user", "sec ret"));
try
{
context.login();
fail("Login should have failed as the supplied username does not adhere to the expected pattern");
}
catch(LoginException le)
{
assertEquals("Username or password does not adhere to the acceptable pattern", le.getMessage());
}
// finally, let's try a username that has one of the blacklisted tokens.
context = new LoginContext("testInputValidator", new UsernamePasswordHandler("javaINSERTduke", "secret"));
try
{
context.login();
fail("Login should have failed as the supplied username does not adhere to the expected pattern");
}