Use the same maximum frame length for all transports.

This commit is contained in:
akwizgran
2015-01-05 16:24:44 +00:00
parent 358166bc12
commit d3bf2d59a1
60 changed files with 194 additions and 321 deletions

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamDecrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamReaderImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test
public void testEmptyFramesAreSkipped() throws Exception {
@@ -29,7 +29,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
assertEquals(0, r.read()); // Skip the first empty frame, read a byte
assertEquals(0, r.read()); // Read another byte
assertEquals(-1, r.read()); // Skip the second empty frame, reach EOF
@@ -52,7 +52,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Skip the first empty frame, read the two payload bytes
assertEquals(2, r.read(buf));
@@ -74,7 +74,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH / 2];
// Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf));
@@ -96,7 +96,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf, MAX_PAYLOAD_LENGTH / 2,

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamEncrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamWriterImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test
public void testCloseWithoutWritingWritesFinalFrame() throws Exception {
@@ -26,7 +26,7 @@ public class StreamWriterImplTest extends BriarTestCase {
// Flush the stream
oneOf(encrypter).flush();
}});
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
w.close();
context.assertIsSatisfied();
}
@@ -36,7 +36,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a non-final frame with an empty payload
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(0),
@@ -63,7 +63,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a non-final frame with one payload byte
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(1),
@@ -90,7 +90,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testSingleByteWritesWriteFullFrame() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a full non-final frame
oneOf(encrypter).writeFrame(with(any(byte[].class)),
@@ -116,7 +116,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testMultiByteWritesWriteFullFrames() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),
@@ -147,7 +147,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testLargeMultiByteWriteWritesFullFrames() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.EOFException;
import java.io.IOException;
@@ -16,9 +17,9 @@ class TestStreamDecrypter implements StreamDecrypter {
private final InputStream in;
private final byte[] frame;
TestStreamDecrypter(InputStream in, int frameLength) {
TestStreamDecrypter(InputStream in) {
this.in = in;
frame = new byte[frameLength];
frame = new byte[MAX_FRAME_LENGTH];
}
public int readFrame(byte[] payload) throws IOException {

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.OutputStream;
@@ -16,10 +17,10 @@ class TestStreamEncrypter implements StreamEncrypter {
private boolean writeTag = true;
TestStreamEncrypter(OutputStream out, int frameLength, byte[] tag) {
TestStreamEncrypter(OutputStream out, byte[] tag) {
this.out = out;
this.tag = tag;
frame = new byte[frameLength];
frame = new byte[MAX_FRAME_LENGTH];
}
public void writeFrame(byte[] payload, int payloadLength,

View File

@@ -1,5 +1,6 @@
package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -17,8 +18,6 @@ import org.junit.Test;
public class TransportIntegrationTest extends BriarTestCase {
private final int FRAME_LENGTH = 2048;
private final Random random;
public TransportIntegrationTest() {
@@ -40,31 +39,28 @@ public class TransportIntegrationTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH];
random.nextBytes(tag);
// Generate two frames with random payloads
byte[] payload1 = new byte[1234];
byte[] payload1 = new byte[123];
random.nextBytes(payload1);
byte[] payload2 = new byte[321];
random.nextBytes(payload2);
// Write the tag and the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypter encrypter = new TestStreamEncrypter(out, FRAME_LENGTH,
tag);
OutputStream streamWriter = new StreamWriterImpl(encrypter,
FRAME_LENGTH);
StreamEncrypter encrypter = new TestStreamEncrypter(out, tag);
OutputStream streamWriter = new StreamWriterImpl(encrypter);
streamWriter.write(payload1);
streamWriter.flush();
streamWriter.write(payload2);
streamWriter.flush();
byte[] output = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH * 2, output.length);
assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH * 2, output.length);
// Read the tag back
ByteArrayInputStream in = new ByteArrayInputStream(output);
byte[] recoveredTag = new byte[tag.length];
read(in, recoveredTag);
assertArrayEquals(tag, recoveredTag);
// Read the frames back
StreamDecrypter decrypter = new TestStreamDecrypter(in, FRAME_LENGTH);
InputStream streamReader = new StreamReaderImpl(decrypter,
FRAME_LENGTH);
StreamDecrypter decrypter = new TestStreamDecrypter(in);
InputStream streamReader = new StreamReaderImpl(decrypter);
byte[] recoveredPayload1 = new byte[payload1.length];
read(streamReader, recoveredPayload1);
assertArrayEquals(payload1, recoveredPayload1);