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-2699] Update tests in DigestTest to call a common method in order to remove duplicated code #2016

Merged
merged 1 commit into from
Oct 20, 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
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());
}
}
Loading