mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Code cleanup for the transport protocol.
This commit is contained in:
@@ -55,8 +55,8 @@
|
||||
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
|
||||
<test name='net.sf.briar.transport.ConnectionWriterTest'/>
|
||||
<test name='net.sf.briar.transport.FrameReadWriteTest'/>
|
||||
<test name='net.sf.briar.transport.IncomingEncryptionLayerImplTest'/>
|
||||
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
|
||||
<test name='net.sf.briar.transport.IncomingEncryptionLayerTest'/>
|
||||
<test name='net.sf.briar.transport.OutgoingEncryptionLayerTest'/>
|
||||
<test name='net.sf.briar.util.ByteUtilsTest'/>
|
||||
<test name='net.sf.briar.util.FileUtilsTest'/>
|
||||
<test name='net.sf.briar.util.StringUtilsTest'/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
@@ -8,6 +8,7 @@ import static org.junit.Assert.assertArrayEquals;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.TestUtils;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
@@ -15,7 +16,10 @@ import net.sf.briar.api.transport.ConnectionReader;
|
||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConnectionReaderImplTest extends TransportTest {
|
||||
public class ConnectionReaderImplTest extends BriarTestCase {
|
||||
|
||||
private static final int MAX_PAYLOAD_LENGTH =
|
||||
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
|
||||
|
||||
public ConnectionReaderImplTest() throws Exception {
|
||||
super();
|
||||
@@ -23,7 +27,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
|
||||
@Test
|
||||
public void testLengthZero() throws Exception {
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, 0, 0, true);
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
@@ -34,7 +38,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
|
||||
@Test
|
||||
public void testLengthOne() throws Exception {
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + 1 + MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + 1 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, 1, 0, true);
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
@@ -100,12 +104,12 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testNonZeroPadding() throws Exception {
|
||||
int payloadLength = 10, paddingLength = 10;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ paddingLength + MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + payloadLength + paddingLength
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, paddingLength,
|
||||
false);
|
||||
// Set a byte of the padding to a non-zero value
|
||||
frame[FRAME_HEADER_LENGTH + payloadLength] = 1;
|
||||
frame[HEADER_LENGTH + payloadLength] = 1;
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
ConnectionReader r = createConnectionReader(in);
|
||||
@@ -120,13 +124,11 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testMultipleFrames() throws Exception {
|
||||
// First frame: 123-byte payload
|
||||
int payloadLength = 123;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
||||
// Second frame: 1234-byte payload
|
||||
int payloadLength1 = 1234;
|
||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
||||
+ MAC_LENGTH];
|
||||
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, true);
|
||||
// Concatenate the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -148,13 +150,11 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testLastFrameNotMarkedAsSuch() throws Exception {
|
||||
// First frame: 123-byte payload
|
||||
int payloadLength = 123;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
||||
// Second frame: 1234-byte payload
|
||||
int payloadLength1 = 1234;
|
||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
||||
+ MAC_LENGTH];
|
||||
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, false);
|
||||
// Concatenate the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
@@ -8,11 +8,15 @@ import static org.junit.Assert.assertArrayEquals;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConnectionWriterImplTest extends TransportTest {
|
||||
public class ConnectionWriterImplTest extends BriarTestCase {
|
||||
|
||||
private static final int MAX_PAYLOAD_LENGTH =
|
||||
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
|
||||
|
||||
public ConnectionWriterImplTest() throws Exception {
|
||||
super();
|
||||
@@ -31,7 +35,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testSingleByteFrame() throws Exception {
|
||||
// Create a single-byte frame
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + 1 + MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + 1 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, 1, 0, false);
|
||||
// Check that the ConnectionWriter gets the same results
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -70,10 +74,10 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testMultipleFrames() throws Exception {
|
||||
// First frame: 123-byte payload
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
byte[] frame = new byte[HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, 123, 0, false);
|
||||
// Second frame: 1234-byte payload
|
||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
byte[] frame1 = new byte[HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, 1234, 0, false);
|
||||
// Concatenate the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
@@ -74,7 +74,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
ErasableKey frameCopy = frameKey.copy();
|
||||
// Write the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
FrameWriter encryptionOut = new OutgoingEncryptionLayerImpl(out,
|
||||
FrameWriter encryptionOut = new OutgoingEncryptionLayer(out,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagCopy,
|
||||
frameCopy);
|
||||
ConnectionWriter writer = new ConnectionWriterImpl(encryptionOut);
|
||||
@@ -90,7 +90,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
assertArrayEquals(tag, recoveredTag);
|
||||
assertTrue(TagEncoder.decodeTag(recoveredTag, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
FrameReader encryptionIn = new IncomingEncryptionLayerImpl(in,
|
||||
FrameReader encryptionIn = new IncomingEncryptionLayer(in,
|
||||
tagCipher, frameCipher, framePeekingCipher, frameIvEncoder,
|
||||
framePeekingIvEncoder, tagKey, frameKey, false);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(encryptionIn);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -20,13 +21,13 @@ import org.junit.Test;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
public class IncomingEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
||||
private final IvEncoder frameIvEncoder, framePeekingIvEncoder;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
public IncomingEncryptionLayerImplTest() {
|
||||
public IncomingEncryptionLayerTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
@@ -45,14 +46,14 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123];
|
||||
byte[] plaintext = new byte[HEADER_LENGTH + 123];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
|
||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the ciphertext for the second frame
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234];
|
||||
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
|
||||
frameIvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
@@ -66,39 +67,45 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
||||
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
|
||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||
framePeekingIvEncoder, tagKey, frameKey, true);
|
||||
// First frame
|
||||
Frame f = new Frame();
|
||||
assertTrue(decrypter.readFrame(f));
|
||||
assertEquals(plaintext.length, f.getLength());
|
||||
byte[] decrypted = f.getBuffer();
|
||||
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
|
||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||
assertTrue(decrypter.readFrame(frame));
|
||||
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
assertEquals(123, payload);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
assertEquals(0, padding);
|
||||
assertEquals(plaintext.length, HEADER_LENGTH + payload + padding);
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
assertEquals(plaintext[i], frame[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertTrue(decrypter.readFrame(f));
|
||||
assertEquals(plaintext1.length, f.getLength());
|
||||
decrypted = f.getBuffer();
|
||||
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
|
||||
assertTrue(decrypter.readFrame(frame));
|
||||
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
|
||||
payload = HeaderEncoder.getPayloadLength(frame);
|
||||
assertEquals(1234, payload);
|
||||
padding = HeaderEncoder.getPaddingLength(frame);
|
||||
assertEquals(0, padding);
|
||||
assertEquals(plaintext1.length, HEADER_LENGTH + payload + padding);
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
assertEquals(plaintext1[i], frame[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithoutTag() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123];
|
||||
byte[] plaintext = new byte[HEADER_LENGTH + 123];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
|
||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the ciphertext for the second frame
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234];
|
||||
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
|
||||
frameIvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
@@ -111,25 +118,31 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
||||
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
|
||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||
framePeekingIvEncoder, tagKey, frameKey, false);
|
||||
// First frame
|
||||
Frame f = new Frame();
|
||||
assertTrue(decrypter.readFrame(f));
|
||||
assertEquals(plaintext.length, f.getLength());
|
||||
byte[] decrypted = f.getBuffer();
|
||||
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
|
||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||
assertTrue(decrypter.readFrame(frame));
|
||||
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
assertEquals(123, payload);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
assertEquals(0, padding);
|
||||
assertEquals(plaintext.length, HEADER_LENGTH + payload + padding);
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
assertEquals(plaintext[i], frame[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertTrue(decrypter.readFrame(f));
|
||||
assertEquals(plaintext1.length, f.getLength());
|
||||
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
|
||||
decrypted = f.getBuffer();
|
||||
assertTrue(decrypter.readFrame(frame));
|
||||
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
|
||||
payload = HeaderEncoder.getPayloadLength(frame);
|
||||
assertEquals(1234, payload);
|
||||
padding = HeaderEncoder.getPaddingLength(frame);
|
||||
assertEquals(0, padding);
|
||||
assertEquals(plaintext1.length, HEADER_LENGTH + payload + padding);
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
assertEquals(plaintext1[i], frame[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
@@ -19,12 +19,11 @@ class NullIncomingEncryptionLayer implements FrameReader {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
public boolean readFrame(Frame f) throws IOException {
|
||||
byte[] buf = f.getBuffer();
|
||||
public boolean readFrame(byte[] frame) throws IOException {
|
||||
// Read the frame header
|
||||
int offset = 0, length = FRAME_HEADER_LENGTH;
|
||||
int offset = 0, length = HEADER_LENGTH;
|
||||
while(offset < length) {
|
||||
int read = in.read(buf, offset, length - offset);
|
||||
int read = in.read(frame, offset, length - offset);
|
||||
if(read == -1) {
|
||||
if(offset == 0) return false;
|
||||
throw new EOFException();
|
||||
@@ -32,17 +31,16 @@ class NullIncomingEncryptionLayer implements FrameReader {
|
||||
offset += read;
|
||||
}
|
||||
// Parse the frame header
|
||||
int payload = HeaderEncoder.getPayloadLength(buf);
|
||||
int padding = HeaderEncoder.getPaddingLength(buf);
|
||||
length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
// Read the remainder of the frame
|
||||
while(offset < length) {
|
||||
int read = in.read(buf, offset, length - offset);
|
||||
int read = in.read(frame, offset, length - offset);
|
||||
if(read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
f.setLength(length - MAC_LENGTH);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -22,9 +23,12 @@ class NullOutgoingEncryptionLayer implements FrameWriter {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public void writeFrame(Frame f) throws IOException {
|
||||
out.write(f.getBuffer(), 0, f.getLength() + MAC_LENGTH);
|
||||
capacity -= f.getLength();
|
||||
public void writeFrame(byte[] frame) throws IOException {
|
||||
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||
int length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
out.write(frame, 0, length);
|
||||
capacity -= length;
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
@@ -19,13 +20,13 @@ import org.junit.Test;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
public class OutgoingEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final IvEncoder frameIvEncoder;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
public OutgoingEncryptionLayerImplTest() {
|
||||
public OutgoingEncryptionLayerTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
@@ -43,12 +44,14 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
||||
byte[] plaintext = new byte[123];
|
||||
byte[] plaintext = new byte[HEADER_LENGTH + 123];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the expected ciphertext for the second frame
|
||||
byte[] plaintext1 = new byte[1234];
|
||||
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, true);
|
||||
frameIvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
@@ -61,16 +64,10 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
byte[] expected = out.toByteArray();
|
||||
// Use the encryption layer to encrypt the plaintext
|
||||
out.reset();
|
||||
FrameWriter encrypter = new OutgoingEncryptionLayerImpl(out,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagKey,
|
||||
frameKey);
|
||||
Frame f = new Frame();
|
||||
System.arraycopy(plaintext, 0, f.getBuffer(), 0, plaintext.length);
|
||||
f.setLength(plaintext.length);
|
||||
encrypter.writeFrame(f);
|
||||
System.arraycopy(plaintext1, 0, f.getBuffer(), 0, plaintext1.length);
|
||||
f.setLength(plaintext1.length);
|
||||
encrypter.writeFrame(f);
|
||||
FrameWriter encrypter = new OutgoingEncryptionLayer(out, Long.MAX_VALUE,
|
||||
tagCipher, frameCipher, frameIvEncoder, tagKey, frameKey);
|
||||
encrypter.writeFrame(plaintext);
|
||||
encrypter.writeFrame(plaintext1);
|
||||
byte[] actual = out.toByteArray();
|
||||
// Check that the actual ciphertext matches the expected ciphertext
|
||||
assertArrayEquals(expected, actual);
|
||||
@@ -1,32 +0,0 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public abstract class TransportTest extends BriarTestCase {
|
||||
|
||||
static final int MAX_PAYLOAD_LENGTH =
|
||||
MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - MAC_LENGTH;
|
||||
|
||||
protected final Cipher frameCipher;
|
||||
protected final ErasableKey frameKey;
|
||||
|
||||
public TransportTest() throws Exception {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
frameKey = crypto.generateTestKey();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user