MySQLAuthenticationEngine implements AuthenticationEngine { @Override public AuthenticationResult auth(final ChannelHandlerContext context, final PacketPayload payload) { if (MySQLConnectionPhase.AUTH_PHASE_FAST_PATH == connectionPhase) { currentAuthResult = authPhaseFastPath(context, payload); if (!currentAuthResult.isFinished()) { return currentAuthResult; } } else if (MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH == connectionPhase) { authenticationMethodMismatch((MySQLPacketPayload) payload); } Optional<MySQLServerErrorCode> errorCode = authenticationHandler.login(currentAuthResult.getUsername(), authResponse, currentAuthResult.getDatabase()); context.writeAndFlush(errorCode.isPresent() ? createErrorPacket(errorCode.get(), context) : new MySQLOKPacket(++sequenceId)); return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), currentAuthResult.getDatabase()); }  @Override int handshake(final ChannelHandlerContext context); @Override AuthenticationResult auth(final ChannelHandlerContext context, final PacketPayload payload);  }
@Test public void assertAuthenticationMethodMismatch() { setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH); MySQLPacketPayload payload = mock(MySQLPacketPayload.class); ChannelHandlerContext channelHandlerContext = mock(ChannelHandlerContext.class); when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue()); authenticationEngine.auth(channelHandlerContext, payload); assertThat(getConnectionPhase(), is(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH)); }
@Test public void assertAuthSwitchResponse() { setConnectionPhase(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH); MySQLPacketPayload payload = mock(MySQLPacketPayload.class); ChannelHandlerContext channelHandlerContext = mock(ChannelHandlerContext.class); when(payload.readStringEOFByBytes()).thenReturn(authResponse); setAuthenticationResult(); authenticationEngine.auth(channelHandlerContext, payload); assertThat(getAuthResponse(), is(authResponse)); }
@Test public void assertAuthWithLoginFail() throws NoSuchFieldException, IllegalAccessException { setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH); ChannelHandlerContext context = getContext(); setSchemas(); when(authenticationHandler.login(anyString(), any(), anyString())).thenReturn(Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR)); authenticationEngine.auth(context, getPayload("root", "sharding_db", authResponse)); verify(context).writeAndFlush(any(MySQLErrPacket.class)); }
@Test public void assertAuthWithAbsentDatabase() throws NoSuchFieldException, IllegalAccessException { ChannelHandlerContext context = getContext(); setSchemas(); setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH); authenticationEngine.auth(context, getPayload("root", "ABSENT DATABASE", authResponse)); verify(context).writeAndFlush(any(MySQLErrPacket.class)); }
@Test public void assertAuth() throws NoSuchFieldException, IllegalAccessException { setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH); ChannelHandlerContext context = getContext(); when(authenticationHandler.login(anyString(), any(), anyString())).thenReturn(Optional.empty()); setSchemas(); authenticationEngine.auth(context, getPayload("root", "sharding_db", authResponse)); verify(context).writeAndFlush(any(MySQLOKPacket.class)); }