コード例 #1
0
  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();
    }
  }
コード例 #2
0
  @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();
    }
  }
コード例 #3
0
  @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());
  }
コード例 #4
0
 private EmbeddedChannel createChannel() {
   EmbeddedChannel channel = new EmbeddedChannel(new VotifierProtocol1Decoder());
   channel.attr(VotifierSession.KEY).set(SESSION);
   channel.attr(VotifierPlugin.KEY).set(TestVotifierPlugin.getI());
   return channel;
 }