Skip to content

Commit

Permalink
Merge pull request #2016 from lvydra/ELY-2699
Browse files Browse the repository at this point in the history
[ELY-2699] Update tests in DigestTest to call a common method in order to remove duplicated code
  • Loading branch information
Skyllarr authored Oct 20, 2023
2 parents 3888014 + 289ba44 commit 5ed4c6c
Showing 1 changed file with 19 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.Sasl;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
import javax.security.sasl.SaslServerFactory;

Expand Down Expand Up @@ -146,14 +147,7 @@ public void testSuccessfulExchange() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", "TestRealm");
SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST }, "George", "TestProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.ISO_8859_1));
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}
/**
* Test a successful exchange using the DIGEST mechanism but the default realm.
Expand All @@ -171,15 +165,7 @@ public void testSuccessfulExchange_DefaultRealm() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", null);
SaslClient client = Sasl.createSaslClient(new String[]{DIGEST}, "George", "TestProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.ISO_8859_1));

server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}

/**
Expand All @@ -201,15 +187,7 @@ public void testSuccessfulExchange_AlternativeProtocol() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", null);
SaslClient client = Sasl.createSaslClient(new String[]{DIGEST}, "George", "OtherProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.UTF_8));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.UTF_8));

server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}

/**
Expand Down Expand Up @@ -318,14 +296,7 @@ public void testRealmSelection() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", "last\\ ");
SaslClient client = Sasl.createSaslClient(new String[]{DIGEST}, "George", "TestProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:" + new String(message, StandardCharsets.ISO_8859_1));
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}

/*
Expand Down Expand Up @@ -515,13 +486,7 @@ public void testSuccessfulExchange_PreHashedClient() throws Exception {

SaslClient client = Sasl.createSaslClient(new String[]{DIGEST}, "George", "TestProtocol", "TestServer", clientProps, clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);

message = client.evaluateChallenge(message);
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server,client);
}

/**
Expand All @@ -544,13 +509,7 @@ public void testSuccessfulExchange_DefaultRealm_PreHashedClient() throws Excepti
clientProps.put(PRE_DIGESTED_PROPERTY, "true");
SaslClient client = Sasl.createSaslClient(new String[]{DIGEST}, "George", "TestProtocol", "TestServer", clientProps, clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
message = client.evaluateChallenge(message);

server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server,client);
}

/**
Expand Down Expand Up @@ -753,14 +712,7 @@ public void testSuccessfulExchangeNullAuthorizationId() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", "TestRealm");
SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST }, null, "TestProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.ISO_8859_1));
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}

/**
Expand All @@ -779,14 +731,7 @@ public void testSuccessfulExchangeEmptyAuthorizationId() throws Exception {
CallbackHandler clientCallback = createClearPwdClientCallbackHandler("George", "gpwd", "TestRealm");
SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST }, "", "TestProtocol", "TestServer", Collections.<String, Object>emptyMap(), clientCallback);

assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.ISO_8859_1));
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
assertExchange(server, client);
}

private KeySpec getDigestKeySpec(String username, String password, String realm) throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -826,4 +771,14 @@ public void testUnboundServerName() throws Exception {
assertEquals("TestServer5", server.getNegotiatedProperty(Sasl.BOUND_SERVER_NAME));
}

private void assertExchange(SaslServer server, SaslClient client) throws SaslException {
assertFalse(client.hasInitialResponse());
byte[] message = server.evaluateResponse(new byte[0]);
log.debug("Challenge:"+ new String(message, StandardCharsets.ISO_8859_1));
message = client.evaluateChallenge(message);
log.debug("Client response:"+ new String(message, StandardCharsets.ISO_8859_1));
server.evaluateResponse(message);
assertTrue(server.isComplete());
assertEquals("George", server.getAuthorizationID());
}
}

0 comments on commit 5ed4c6c

Please sign in to comment.