From 0d06ad8bd881940407785536a78143661c56faf5 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 13 Jan 2012 15:56:53 +0000 Subject: [PATCH] Unit tests for tagging every segment. --- .../ConnectionEncrypterImplTest.java | 41 ++++++++++++++++++- .../SegmentedConnectionEncrypterTest.java | 41 ++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java b/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java index eb46a182d1..ddc9325180 100644 --- a/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java +++ b/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java @@ -36,7 +36,7 @@ public class ConnectionEncrypterImplTest extends BriarTestCase { } @Test - public void testEncryption() throws Exception { + public void testEncryptionWithFirstSegmentTagged() throws Exception { // Calculate the expected tag byte[] tag = new byte[TAG_LENGTH]; TagEncoder.encodeTag(tag, 0, tagCipher, tagKey); @@ -69,4 +69,43 @@ public class ConnectionEncrypterImplTest extends BriarTestCase { assertArrayEquals(expected, actual); assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity()); } + + @Test + public void testEncryptionWithEverySegmentTagged() throws Exception { + // Calculate the expected tag for the first segment + byte[] tag = new byte[TAG_LENGTH]; + TagEncoder.encodeTag(tag, 0, tagCipher, tagKey); + // Calculate the expected ciphertext for the first frame + byte[] iv = new byte[frameCipher.getBlockSize()]; + byte[] plaintext = new byte[123 + MAC_LENGTH]; + IvParameterSpec ivSpec = new IvParameterSpec(iv); + frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); + byte[] ciphertext = frameCipher.doFinal(plaintext); + // Calculate the expected tag for the second frame + byte[] tag1 = new byte[TAG_LENGTH]; + TagEncoder.encodeTag(tag1, 1, tagCipher, tagKey); + // Calculate the expected ciphertext for the second frame + byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; + IvEncoder.updateIv(iv, 1L); + ivSpec = new IvParameterSpec(iv); + frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); + byte[] ciphertext1 = frameCipher.doFinal(plaintext1); + // Concatenate the ciphertexts + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write(tag); + out.write(ciphertext); + out.write(tag1); + out.write(ciphertext1); + byte[] expected = out.toByteArray(); + // Use a ConnectionEncrypter to encrypt the plaintext + out.reset(); + ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE, + tagCipher, frameCipher, tagKey, frameKey, true); + e.writeFrame(plaintext, plaintext.length); + e.writeFrame(plaintext1, plaintext1.length); + byte[] actual = out.toByteArray(); + // Check that the actual ciphertext matches the expected ciphertext + assertArrayEquals(expected, actual); + assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity()); + } } diff --git a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java index 8cce6fa072..ab981dbef2 100644 --- a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java +++ b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java @@ -39,7 +39,7 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase { } @Test - public void testEncryption() throws Exception { + public void testEncryptionWithFirstSegmentTagged() throws Exception { // Calculate the expected tag byte[] tag = new byte[TAG_LENGTH]; TagEncoder.encodeTag(tag, 0, tagCipher, tagKey); @@ -75,6 +75,45 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase { assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity()); } + @Test + public void testEncryptionWithEverySegmentTagged() throws Exception { + // Calculate the expected tag for the first frame + byte[] tag = new byte[TAG_LENGTH]; + TagEncoder.encodeTag(tag, 0, tagCipher, tagKey); + // Calculate the expected ciphertext for the first frame + byte[] iv = new byte[frameCipher.getBlockSize()]; + byte[] plaintext = new byte[123 + MAC_LENGTH]; + IvParameterSpec ivSpec = new IvParameterSpec(iv); + frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); + byte[] ciphertext = frameCipher.doFinal(plaintext); + // Calculate the expected tag for the second frame + byte[] tag1 = new byte[TAG_LENGTH]; + TagEncoder.encodeTag(tag1, 1, tagCipher, tagKey); + // Calculate the expected ciphertext for the second frame + byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; + IvEncoder.updateIv(iv, 1L); + ivSpec = new IvParameterSpec(iv); + frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); + byte[] ciphertext1 = frameCipher.doFinal(plaintext1); + // Concatenate the ciphertexts + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write(tag); + out.write(ciphertext); + out.write(tag1); + out.write(ciphertext1); + byte[] expected = out.toByteArray(); + // Use a connection encrypter to encrypt the plaintext + SegmentSink sink = new ByteArraySegmentSink(); + ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink, + Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, true); + e.writeFrame(plaintext, plaintext.length); + e.writeFrame(plaintext1, plaintext1.length); + byte[] actual = out.toByteArray(); + // Check that the actual ciphertext matches the expected ciphertext + assertArrayEquals(expected, actual); + assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity()); + } + private static class ByteArraySegmentSink extends ByteArrayOutputStream implements SegmentSink { -- GitLab