try { for (UnencryptedEmailPacket packet: packets) outputStream.write(packet.getContent()); Email email = new Email(outputStream.toByteArray()); email.setMessageID(packets[0].getMessageId()); // all packets in the array have the same message ID
@Test public void testEncryptionDecryption() throws Exception { for (int i=0; i<encryptedPackets.length; i++) { EncryptedEmailPacket packet = encryptedPackets[i]; UnencryptedEmailPacket decryptedPacket = packet.decrypt(identities[i]); byte[] arrayA = decryptedPacket.getContent(); byte[] arrayB = message.getBytes(); assertArrayEquals("Email message differs after decryption! CryptoImplementation = " + packet.getCryptoImpl().getName(), arrayA, arrayB); } }
@Test public void testCreateEmailPackets() throws MessagingException, IOException, GeneralSecurityException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, PasswordException { // convert an email to a byte array, convert back, and compare with the original email for (Email email: emails) { EmailIdentity identity = identities.get(email); KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); Collection<UnencryptedEmailPacket> packets = email.createEmailPackets(identity, keyUpdateHandler, null, I2PBotePacket.MAX_DATAGRAM_SIZE); assertTrue("Expected more email packets. #packets = " + packets.size(), packets.size() > email.getText().length()/I2PBotePacket.MAX_DATAGRAM_SIZE/2); // the emails are somewhat compressible, but definitely not by 50% ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); for (UnencryptedEmailPacket packet: packets) outputStream.write(packet.getContent()); Email newEmail = new Email(outputStream.toByteArray()); assertEquals(email.getContent(), newEmail.getContent()); // check packet sizes against size limit for (UnencryptedEmailPacket packet: packets) assertTrue("Email packet exceeds max size!", packet.toByteArray().length <= I2PBotePacket.MAX_DATAGRAM_SIZE); } }
@Test public void testSign() throws MessagingException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException, GeneralSecurityException, PasswordException { assertEquals(emails.size()-1, identities.size()); // -1 for the anonymous email that was not added to the map for (Email email: emails) { EmailIdentity identity = identities.get(email); if (identity == null) continue; // sign and verify signature email.sign(identity, TestUtil.createDummyKeyUpdateHandler()); assertTrue(email.isSignatureValid()); // write the email to a byte array, make a new email from the byte array, and verify the signature ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); for (UnencryptedEmailPacket packet: email.createEmailPackets(identity, keyUpdateHandler, null, I2PBotePacket.MAX_DATAGRAM_SIZE)) outputStream.write(packet.getContent()); email = new Email(outputStream.toByteArray()); assertTrue(email.isSignatureValid()); } }
@Test public void testHeaderRemoval() throws MessagingException, IOException, GeneralSecurityException, PasswordException { Email newEmail; Collection<UnencryptedEmailPacket> packets; ByteArrayOutputStream outputStream; KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); // verify that all BCC addresses are removed when sending to a TO: address EmailIdentity identity2 = identities.get(bccEmail); packets = bccEmail.createEmailPackets(identity2, keyUpdateHandler, null, I2PBotePacket.MAX_DATAGRAM_SIZE); outputStream = new ByteArrayOutputStream(); for (UnencryptedEmailPacket packet: packets) outputStream.write(packet.getContent()); newEmail = new Email(outputStream.toByteArray()); assertNull("BCC headers were not removed!", newEmail.getHeader("BCC")); assertEquals(3, newEmail.getAllRecipients().length); // verify that the recipient is not removed if it is a BCC: addresses packets = bccEmail.createEmailPackets(bccIdentity, keyUpdateHandler, bccEmailDestination, I2PBotePacket.MAX_DATAGRAM_SIZE); // use the plain email dest because that is what the Email class compares against outputStream = new ByteArrayOutputStream(); for (UnencryptedEmailPacket packet: packets) outputStream.write(packet.getContent()); newEmail = new Email(outputStream.toByteArray()); assertNotNull("BCC header expected!", newEmail.getHeader("BCC")); assertEquals("One BCC header expected!", 1, newEmail.getHeader("BCC").length); assertEquals(4, newEmail.getAllRecipients().length); }