MySQLAuthenticationHandler { public Optional<MySQLServerErrorCode> login(final String username, final byte[] authResponse, final String database) { Optional<ProxyUser> user = getUser(username); if (!user.isPresent() || !isPasswordRight(user.get().getPassword(), authResponse)) { return Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR); } if (!isAuthorizedSchema(user.get().getAuthorizedSchemas(), database)) { return Optional.of(MySQLServerErrorCode.ER_DBACCESS_DENIED_ERROR); } return Optional.empty(); }  Optional<MySQLServerErrorCode> login(final String username, final byte[] authResponse, final String database);  }
@Test public void assertLoginWithPassword() { setAuthentication(new ProxyUser("root", Collections.singleton("db1"))); byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113}; assertFalse(authenticationHandler.login("root", authResponse, "db1").isPresent()); }
@Test public void assertLoginWithAbsentUser() { setAuthentication(new ProxyUser("root", Collections.singleton("db1"))); byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113}; assertThat(authenticationHandler.login("root1", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR)); }
@Test public void assertLoginWithIncorrectPassword() { setAuthentication(new ProxyUser("root", Collections.singleton("db1"))); byte[] authResponse = {0, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113}; assertThat(authenticationHandler.login("root", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR)); }
@Test public void assertLoginWithoutPassword() { setAuthentication(new ProxyUser(null, null)); byte[] authResponse = {}; assertFalse(authenticationHandler.login("root", authResponse, "db1").isPresent()); }
@Test public void assertLoginWithUnauthorizedSchema() { setAuthentication(new ProxyUser("root", Collections.singleton("db1"))); byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113}; assertThat(authenticationHandler.login("root", authResponse, "db2").orElse(null), is(MySQLServerErrorCode.ER_DBACCESS_DENIED_ERROR)); }