@Test public void testCompression() throws MessagingException, GeneralSecurityException, PasswordException { // create a 500,000-char string that should compress to one packet StringBuilder stringBuilder = new StringBuilder(); for (int i=0; i<50000; i++) stringBuilder.append("0123456789"); Email newEmail = new Email(true); newEmail.setText(stringBuilder.toString()); KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); Collection<UnencryptedEmailPacket> packets = newEmail.createEmailPackets(bccIdentity, keyUpdateHandler, null, I2PBotePacket.MAX_DATAGRAM_SIZE); assertEquals("The email was not compressed into one email packet.", 1, packets.size()); }
@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()); } }
private void testAddEmail(String mailContent, int expectedNumPackets) throws Exception { Email email = new Email(true); String recipient = "test@bote.i2p"; email.addRecipient(RecipientType.TO, new InternetAddress(recipient)); email.setText(mailContent); EmailIdentity identity = new EmailIdentity("DVkhqF6R9SHB5svViGtqRYZO7oI-0-omnIFtae29fNnNtTTH2j37Fr5fWp4t6rseTjiJ8gwg08DnbA4qP72aSQcDQPSErOELOMSU5BUTtsT8hnv1-DKdhIn~1qoIjxzIFHbxT3xnR3nFI7lKd6couscilzPBCjoFDUKb5ds2u23RO29K7~EKxU1O7Ltu6sT5etXkJkhAziOcuyfZyxJXqH1caYX5e2aWIhY3D2ESfy4nMK66r5KcDVQOPTzCkJq6d1FFOmnDGrlJjN~HgHmfUCtLbO~TLugWx9FCiDGfPkBb-3ODYTDaUR1zobOj1tiffV3Nm73PsYddRt84emLKzIRsC77JJpflw~h8UIRYJ29vJDf4VQ54BhZcelmN192sIrWr2nKN8n6PpSP4LI4RAuG2UvLytnDYzFM7O9WcnFP2-Qs3t1lD9aF72JVTYTpH5PZupnB1cglSsdRg8RmtRa41Fseyx8D3EdH~DCdpMGmfupaWp9~dKpFMleqk9scRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABTjDxn3wEOjCjJ4APg~2IGpqWwy2Hw728aZ3eCC5l0MP913BLdIfSUiXPbs6sN9A2"); KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); Collection<UnencryptedEmailPacket> packets = email.createEmailPackets(identity, keyUpdateHandler, recipient, I2PBotePacket.MAX_DATAGRAM_SIZE); assertTrue("Expected " + expectedNumPackets + " email packets, got " + packets.size(), packets.size() == expectedNumPackets); assertTrue("The inbox should be empty at this point!", inbox.getElements().size() == 0); for (UnencryptedEmailPacket emailPacket: packets) incompleteFolder.addEmailPacket(emailPacket); assertTrue("The incomplete emails folder is not empty!", incompleteFolder.getElements().size() == 0); assertTrue("Expected: one email in the inbox, actual number = " + inbox.getElements().size(), inbox.getElements().size() == 1); // Verify that the original email and the email in the folder are the same except for the signature header Email storedEmail = inbox.getElements().iterator().next(); storedEmail.removeHeader("X-I2PBote-Sig-Valid"); TestUtil.assertEquals("Stored email differs from original email!", email, storedEmail); } }
@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); }
Collection<UnencryptedEmailPacket> emailPackets = email.createEmailPackets(senderIdentity, identities, recipient, maxPacketSize);