### Question:
HandshakeHandler extends Handler { @Override public CONNECTION_PHASE handlePacket(PacketBuffer packetBuffer, Session session) { try { if (session.isExpecting(PACKETS.HANDSHAKE_OPEN)) { HandshakePacket handshakePacket = (HandshakePacket) runPacketParser(packetBuffer, null, session.getExpecting()); HandshakePacket.Flags flags = handshakePacket.getFlags(); if (flags.isRegistered()) { session.getCommunications().postError("Registered users are not supported"); return FAILED; } Key remoteKey = handshakePacket.getKey(); if (remoteKey.getPem().getPublicExponent().compareTo(3) < 0 || remoteKey.getPem().getPublicExponent().compareTo(65537) > 0 || remoteKey.getPem().getModulus().getInternalBig().bitCount() < 0 || remoteKey.getPem().getModulus().getInternalBig().bitCount() > 512) { session.getCommunications().postError("Invalid key size detected; Terminating Connection"); return FAILED; } else { session.attachUser(remoteKey); } if (flags.isRegistered()) { throw new RuntimeException("Registered users are not implemented"); } else { session.setExpecting(PACKETS.REQUEST); HandshakePacket acceptPacket = new HandshakePacket(); acceptPacket.getFlags().setHandshakeAccepted(true); acceptPacket.setKey(session.getServerKey()); session.send(acceptPacket); return HANDSHAKE_ACCEPTED; } } else { session.getCommunications().postError("Handshake handler invoked, but session is not expecting a handshake packet. Closing session."); return FAILED; } } catch (PacketParserException | IOException e) { session.getCommunications().postError("A(n) " + e.getClass().getName() + " Exception has occurred. Closing session."); return FAILED; } } HandshakeHandler(); @Override Packet runPacketParser(PacketBuffer packetBuffer, Session session, PACKETS expecting); @Override CONNECTION_PHASE handlePacket(PacketBuffer packetBuffer, Session session);  }### Answer:
@Test public void testNotExpected() throws Exception { Session session = new Session(communications, options, new HandshakeHandler(), new RequestHandler(screen)); session.setExpecting(PACKETS.HANDSHAKE_CHALLENGE); HandshakePacket handshakePacket = new HandshakePacket(); handshakePacket.getFlags().setHandshakeRequest(true).setRegistered(true); PublicKey key = new PublicKey(); key.setFingerPrint(new byte[32]); handshakePacket.setKey(key); PacketBuffer packetBuffer = new PacketBuffer(); handler.initPacketBuffer(packetBuffer); packetBuffer.write(handshakePacket.getParser().serialize()); assertEquals(CONNECTION_PHASE.FAILED, handler.handlePacket(packetBuffer, session)); }
@Test public void testAcceptHandshake() throws Exception { Session session = new Session(communications, options, new HandshakeHandler(), new RequestHandler(screen)); SocketChannel channel = createNiceMock(SocketChannel.class); SocketAddress addr = createNiceMock(SocketAddress.class); Socket socket = createNiceMock(Socket.class); channel.getRemoteAddress(); expectLastCall().andReturn(addr); channel.socket(); expectLastCall().andReturn(socket); socket.getInputStream(); expectLastCall().andReturn(new ByteArrayInputStream("".getBytes())); replay(channel, addr, socket); session.handle(channel); session.setExpecting(PACKETS.HANDSHAKE_OPEN); HandshakePacket handshakePacket = new HandshakePacket(); handshakePacket.getFlags().setHandshakeRequest(true).setRegistered(false); handshakePacket.setKey(new PublicKey(new OpenSSLRSAPEM(OpenSSLRSAPEM.INTEGER.valueOf(65537), OpenSSLRSAPEM.INTEGER.randomINTEGER(64)))); PacketBuffer packetBuffer = new PacketBuffer(); handler.initPacketBuffer(packetBuffer); packetBuffer.write(handshakePacket.getParser().serialize()); assertEquals(CONNECTION_PHASE.HANDSHAKE_ACCEPTED, handler.handlePacket(packetBuffer, session)); }