Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ELY-2684] Unit test code refactor (removed duplicates) #2041

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 37 additions & 91 deletions tests/base/src/test/java/org/wildfly/security/sasl/otp/OTPTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -819,106 +819,18 @@ public void testAuthenticationWithInvalidPassPhrase() throws Exception {

@Test
public void testAuthenticationWithLongSeed() throws Exception {
final String algorithm = ALGORITHM_OTP_MD5;
final SaslClientFactory clientFactory = obtainSaslClientFactory(OTPSaslClientFactory.class);
assertNotNull(clientFactory);

PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm);
final Password password = passwordFactory.generatePassword(new OneTimePasswordSpec(CodePointIterator.ofString("505d889f90085847").hexDecode().drain(),
"thisSeedIsTooLong", 500));
final SaslServerBuilder.BuilderReference<SecurityDomain> securityDomainReference = new SaslServerBuilder.BuilderReference<>();
final SaslServerBuilder.BuilderReference<Closeable> closeableReference = new SaslServerBuilder.BuilderReference<>();
try {
final SaslServer saslServer = createSaslServer(password, closeableReference, securityDomainReference);

final CallbackHandler cbh = createClientCallbackHandler("userName", "This is a test.", PASS_PHRASE, algorithm, HEX_RESPONSE);
final SaslClient saslClient = clientFactory.createSaslClient(new String[]{SaslMechanismInformation.Names.OTP}, null, "test", "testserver1.example.com",
Collections.<String, Object>emptyMap(), cbh);

byte[] message = saslClient.evaluateChallenge(new byte[0]);
try {
saslServer.evaluateResponse(message);
fail("Expected SaslException not thrown");
} catch (SaslException expected) {
}
saslClient.dispose();
saslServer.dispose();

// The password should remain unchanged
checkPassword(securityDomainReference, "userName", (OneTimePassword) password, algorithm);
} finally {
closeableReference.getReference().close();
}
testPasswordRemainsUnchanged("thisSeedIsTooLong", 500);
}

@Test
public void testAuthenticationWithNonAlphanumericSeed() throws Exception {
final String algorithm = ALGORITHM_OTP_MD5;

final SaslClientFactory clientFactory = obtainSaslClientFactory(OTPSaslClientFactory.class);
assertNotNull(clientFactory);

PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm);
final Password password = passwordFactory.generatePassword(new OneTimePasswordSpec(CodePointIterator.ofString("505d889f90085847").hexDecode().drain(),
"A seed!", 500));
final SaslServerBuilder.BuilderReference<SecurityDomain> securityDomainReference = new SaslServerBuilder.BuilderReference<>();
final SaslServerBuilder.BuilderReference<Closeable> closeableReference = new SaslServerBuilder.BuilderReference<>();
try {
final SaslServer saslServer = createSaslServer(password, closeableReference, securityDomainReference);

final CallbackHandler cbh = createClientCallbackHandler("userName", "This is a test.", PASS_PHRASE, algorithm, HEX_RESPONSE);
final SaslClient saslClient = clientFactory.createSaslClient(new String[]{SaslMechanismInformation.Names.OTP}, null, "test", "testserver1.example.com",
Collections.<String, Object>emptyMap(), cbh);

byte[] message = saslClient.evaluateChallenge(new byte[0]);
try {
saslServer.evaluateResponse(message);
fail("Expected SaslException not thrown");
} catch (SaslException expected) {
}
saslClient.dispose();
saslServer.dispose();

// The password should remain unchanged
checkPassword(securityDomainReference, "userName", (OneTimePassword) password, algorithm);
} finally {
closeableReference.getReference().close();
}
testPasswordRemainsUnchanged("A seed!", 500);
}


@Test
public void testAuthenticationWithInvalidSequenceNumber() throws Exception {
final String algorithm = ALGORITHM_OTP_MD5;
final SaslClientFactory clientFactory = obtainSaslClientFactory(OTPSaslClientFactory.class);
assertNotNull(clientFactory);

PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm);
final Password password = passwordFactory.generatePassword(new OneTimePasswordSpec(CodePointIterator.ofString("505d889f90085847").hexDecode().drain(),
"ke1234", 0));
final SaslServerBuilder.BuilderReference<SecurityDomain> securityDomainReference = new SaslServerBuilder.BuilderReference<>();
final SaslServerBuilder.BuilderReference<Closeable> closeableReference = new SaslServerBuilder.BuilderReference<>();
try {
final SaslServer saslServer = createSaslServer(password, closeableReference, securityDomainReference);

final CallbackHandler cbh = createClientCallbackHandler("userName", "This is a test.", PASS_PHRASE, algorithm, HEX_RESPONSE);
final SaslClient saslClient = clientFactory.createSaslClient(new String[] { SaslMechanismInformation.Names.OTP }, null, "test", "testserver1.example.com",
Collections.<String, Object>emptyMap(), cbh);

byte[] message = saslClient.evaluateChallenge(new byte[0]);
try {
saslServer.evaluateResponse(message);
fail("Expected SaslException not thrown");
} catch (SaslException expected) {
}
saslClient.dispose();
saslServer.dispose();

// The password should remain unchanged
checkPassword(securityDomainReference, "userName", (OneTimePassword) password, algorithm);
} finally {
closeableReference.getReference().close();
}
testPasswordRemainsUnchanged("ke1234", 0);
}

@Test
Expand Down Expand Up @@ -1082,6 +994,40 @@ private CallbackHandler createClientCallbackHandler(String username, String pass
return ClientUtils.getCallbackHandler(new URI("remote://localhost"), context);
}

private void testPasswordRemainsUnchanged(String seed, int sequenceNumber) throws Exception {
final String algorithm = ALGORITHM_OTP_MD5;
final SaslClientFactory clientFactory = obtainSaslClientFactory(OTPSaslClientFactory.class);
assertNotNull(clientFactory);

PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm);
final Password password = passwordFactory.generatePassword(new OneTimePasswordSpec(CodePointIterator.ofString("505d889f90085847").hexDecode().drain(),
seed, sequenceNumber));
final SaslServerBuilder.BuilderReference<SecurityDomain> securityDomainReference = new SaslServerBuilder.BuilderReference<>();
final SaslServerBuilder.BuilderReference<Closeable> closeableReference = new SaslServerBuilder.BuilderReference<>();
try {
final SaslServer saslServer = createSaslServer(password, closeableReference, securityDomainReference);

final CallbackHandler cbh = createClientCallbackHandler("userName", "This is a test.", PASS_PHRASE, algorithm, HEX_RESPONSE);
final SaslClient saslClient = clientFactory.createSaslClient(new String[]{SaslMechanismInformation.Names.OTP}, null, "test", "testserver1.example.com",
Collections.<String, Object>emptyMap(), cbh);

byte[] message = saslClient.evaluateChallenge(new byte[0]);
try {
saslServer.evaluateResponse(message);
fail("Expected SaslException not thrown");
} catch (SaslException expected) {
}
saslClient.dispose();
saslServer.dispose();

// The password should remain unchanged
checkPassword(securityDomainReference, "userName", (OneTimePassword) password, algorithm);
} finally {
closeableReference.getReference().close();
}

}

private static final String[] ALTERNATE_DICTIONARY = new String[] {
"poel", "qewn", "xlob", "preg", "qome", "zarm", "sas",
"oerk", "sct", "seb", "ilan", "wct", "bp", "sft",
Expand Down
Loading