### Question:
PlainSaslServer implements SaslServer { @Override public byte[] evaluateResponse(byte[] response) throws SaslException { String[] tokens; try { tokens = new String(response, StandardCharsets.UTF_8).split("\u0000"); } catch (Exception e) { throw new SaslException("UTF-8 encoding not supported", e); } if (tokens.length != 3) throw new SaslException("Invalid SASL/PLAIN response: expected 3 tokens, got " + tokens.length); String authorizationIdFromClient = tokens[0]; String username = tokens[1]; String password = tokens[2]; if (username.isEmpty()) { throw new SaslException("Authentication failed: username not specified"); } if (password.isEmpty()) { throw new SaslException("Authentication failed: password not specified"); } NameCallback nameCallback = new NameCallback("username", username); PlainAuthenticateCallback authenticateCallback = new PlainAuthenticateCallback(password.toCharArray()); try { callbackHandler.handle(new Callback[] { nameCallback, authenticateCallback }); } catch (Exception e) { throw new SaslAuthenticationException("Authentication failed: credentials for user could not be verified", e); } if (!authenticateCallback.authenticated()) throw new SaslAuthenticationException("Authentication failed: Invalid username or password."); if (!authorizationIdFromClient.isEmpty() && !authorizationIdFromClient.equals(username)) throw new SaslAuthenticationException( "Authentication failed: Client requested an authorization id that is different from username"); this.authorizationId = username; complete = true; return new byte[0]; } PlainSaslServer(PlainServerCallbackHandler callbackHandler); @Override byte[] evaluateResponse(byte[] response); @Override String getAuthorizationID(); @Override String getMechanismName(); @Override Object getNegotiatedProperty(String propName); @Override boolean isComplete(); @Override byte[] unwrap(byte[] incoming, int offset, int len); @Override byte[] wrap(byte[] outgoing, int offset, int len); @Override void dispose(); static final String PLAIN_MECHANISM; static final String INCOMPLETEEXCHANGE_MSG; }### Answer:
@Test public void noAuthorizationIdSpecified() throws Exception { byte[] nextChallenge = saslServer.evaluateResponse(saslMessage("", USER_A, PASSWORD_A)); assertEquals(0, nextChallenge.length); }
@Test public void authorizatonIdEqualsAuthenticationId() throws Exception { byte[] nextChallenge = saslServer.evaluateResponse(saslMessage(USER_A, USER_A, PASSWORD_A)); assertEquals(0, nextChallenge.length); }
@Test(expected = SaslAuthenticationException.class) public void authorizatonIdNotEqualsAuthenticationId() throws Exception { saslServer.evaluateResponse(saslMessage(USER_B, USER_A, PASSWORD_A)); }