private void verifyFailure(String bad) throws Exception { // Send the bad vote EmbeddedChannel channel = createChannel(); byte[] encrypted = RSA.encrypt( bad.getBytes(StandardCharsets.UTF_8), TestVotifierPlugin.getI().getProtocolV1Key().getPublic()); ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted); try { channel.writeInbound(encryptedByteBuf); } finally { channel.close(); } }
@Test(expected = CorruptedFrameException.class) public void testFailureDecodeBadRsa() throws Exception { // Decode our bad RSA key KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(TestVotifierPlugin.r("/bad_public.key")); PublicKey badPublicKey = keyFactory.generatePublic(publicKeySpec); // Send the bad vote EmbeddedChannel channel = createChannel(); String voteString = "VOTE\nTest\ntest\ntest\ntest\n"; byte[] encrypted = RSA.encrypt(voteString.getBytes(StandardCharsets.UTF_8), badPublicKey); ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted); try { channel.writeInbound(encryptedByteBuf); } finally { channel.close(); } }
@Test public void testSuccessfulDecode() throws Exception { // Encode a test vote. String voteString = "VOTE\nTest\ntest\ntest\ntest\n"; // For reference, this is the same vote as a POJO: Vote votePojo = new Vote("Test", "test", "test", "test"); // Send the vote EmbeddedChannel channel = createChannel(); byte[] encrypted = RSA.encrypt( voteString.getBytes(StandardCharsets.UTF_8), TestVotifierPlugin.getI().getProtocolV1Key().getPublic()); ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted); assertTrue(channel.writeInbound(encryptedByteBuf)); Object presumedVote = channel.readInbound(); assertEquals(votePojo, presumedVote); assertFalse(channel.finish()); }
private EmbeddedChannel createChannel() { EmbeddedChannel channel = new EmbeddedChannel(new VotifierProtocol1Decoder()); channel.attr(VotifierSession.KEY).set(SESSION); channel.attr(VotifierPlugin.KEY).set(TestVotifierPlugin.getI()); return channel; }