Moved stream crypto to crypto component.

This commit is contained in:
akwizgran
2014-12-29 19:55:05 +00:00
parent 02a485ace0
commit f316d64afa
33 changed files with 504 additions and 354 deletions

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamDecrypter;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
@@ -17,18 +18,18 @@ public class StreamReaderImplTest extends BriarTestCase {
@Test
public void testEmptyFramesAreSkipped() throws Exception {
Mockery context = new Mockery();
final FrameReader reader = context.mock(FrameReader.class);
final StreamDecrypter decrypter = context.mock(StreamDecrypter.class);
context.checking(new Expectations() {{
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(0)); // Empty frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(2)); // Non-empty frame with two payload bytes
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(0)); // Empty frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(reader, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
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
@@ -40,18 +41,18 @@ public class StreamReaderImplTest extends BriarTestCase {
@Test
public void testEmptyFramesAreSkippedWithBuffer() throws Exception {
Mockery context = new Mockery();
final FrameReader reader = context.mock(FrameReader.class);
final StreamDecrypter decrypter = context.mock(StreamDecrypter.class);
context.checking(new Expectations() {{
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(0)); // Empty frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(2)); // Non-empty frame with two payload bytes
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(0)); // Empty frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(reader, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Skip the first empty frame, read the two payload bytes
assertEquals(2, r.read(buf));
@@ -66,14 +67,14 @@ public class StreamReaderImplTest extends BriarTestCase {
@Test
public void testMultipleReadsPerFrame() throws Exception {
Mockery context = new Mockery();
final FrameReader reader = context.mock(FrameReader.class);
final StreamDecrypter decrypter = context.mock(StreamDecrypter.class);
context.checking(new Expectations() {{
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(MAX_PAYLOAD_LENGTH)); // Nice long frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(reader, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH / 2];
// Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf));
@@ -88,14 +89,14 @@ public class StreamReaderImplTest extends BriarTestCase {
@Test
public void testMultipleReadsPerFrameWithOffsets() throws Exception {
Mockery context = new Mockery();
final FrameReader reader = context.mock(FrameReader.class);
final StreamDecrypter decrypter = context.mock(StreamDecrypter.class);
context.checking(new Expectations() {{
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(MAX_PAYLOAD_LENGTH)); // Nice long frame
oneOf(reader).readFrame(with(any(byte[].class)));
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(reader, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
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,