Split the functionality of ConnectionWriterImpl into layers.

This commit is contained in:
akwizgran
2012-01-19 20:51:45 +00:00
parent 12393581f9
commit aabb8fb5b3
10 changed files with 190 additions and 98 deletions

View File

@@ -217,13 +217,14 @@ public class ConnectionReaderImplTest extends TransportTest {
}
private ConnectionReader createConnectionReader(InputStream in) {
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
IncomingErrorCorrectionLayer correcter =
new NullIncomingErrorCorrectionLayer(decrypter);
IncomingAuthenticationLayer authenticator =
new IncomingAuthenticationLayerImpl(correcter, mac, macKey);
IncomingEncryptionLayer encryption =
new NullIncomingEncryptionLayer(in);
IncomingErrorCorrectionLayer correction =
new NullIncomingErrorCorrectionLayer(encryption);
IncomingAuthenticationLayer authentication =
new IncomingAuthenticationLayerImpl(correction, mac, macKey);
IncomingReliabilityLayer reliability =
new NullIncomingReliabilityLayer(authenticator);
new NullIncomingReliabilityLayer(authentication);
return new ConnectionReaderImpl(reliability, false);
}
}

View File

@@ -12,6 +12,7 @@ import net.sf.briar.api.transport.ConnectionWriter;
import org.junit.Test;
// FIXME: This test covers too many classes
public class ConnectionWriterImplTest extends TransportTest {
public ConnectionWriterImplTest() throws Exception {
@@ -21,11 +22,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testFlushWithoutWriteProducesNothing() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
ConnectionWriter w = createConnectionWriter(out);
w.getOutputStream().flush();
w.getOutputStream().flush();
w.getOutputStream().flush();
@@ -44,11 +41,7 @@ public class ConnectionWriterImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Check that the ConnectionWriter gets the same results
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
ConnectionWriter w = createConnectionWriter(out);
w.getOutputStream().write(0);
w.getOutputStream().flush();
assertArrayEquals(frame, out.toByteArray());
@@ -57,11 +50,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteByteToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
ConnectionWriter w = createConnectionWriter(out);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
for(int i = 0; i < MAX_PAYLOAD_LENGTH - 1; i++) out1.write(0);
@@ -74,11 +63,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
ConnectionWriter w = createConnectionWriter(out);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
out1.write(new byte[MAX_PAYLOAD_LENGTH - 1]);
@@ -112,11 +97,7 @@ public class ConnectionWriterImplTest extends TransportTest {
byte[] expected = out.toByteArray();
// Check that the ConnectionWriter gets the same results
out.reset();
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
ConnectionWriter w = createConnectionWriter(out);
w.getOutputStream().write(new byte[123]);
w.getOutputStream().flush();
w.getOutputStream().write(new byte[1234]);
@@ -124,4 +105,16 @@ public class ConnectionWriterImplTest extends TransportTest {
byte[] actual = out.toByteArray();
assertArrayEquals(expected, actual);
}
private ConnectionWriter createConnectionWriter(OutputStream out) {
OutgoingEncryptionLayer encryption =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correction =
new NullOutgoingErrorCorrectionLayer(encryption);
OutgoingAuthenticationLayer authentication =
new OutgoingAuthenticationLayerImpl(correction, mac, macKey);
OutgoingReliabilityLayer reliability =
new NullOutgoingReliabilityLayer(authentication);
return new ConnectionWriterImpl(reliability);
}
}

View File

@@ -74,13 +74,16 @@ public class FrameReadWriteTest extends BriarTestCase {
ErasableKey macCopy = macKey.copy();
// Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, segCipher, tagCopy, segCopy,
OutgoingEncryptionLayer encryptionOut = new OutgoingEncryptionLayerImpl(
out, Long.MAX_VALUE, tagCipher, segCipher, tagCopy, segCopy,
false);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter writer = new ConnectionWriterImpl(correcter, mac,
macCopy);
OutgoingErrorCorrectionLayer correctionOut =
new NullOutgoingErrorCorrectionLayer(encryptionOut);
OutgoingAuthenticationLayer authenticationOut =
new OutgoingAuthenticationLayerImpl(correctionOut, mac, macCopy);
OutgoingReliabilityLayer reliabilityOut =
new NullOutgoingReliabilityLayer(authenticationOut);
ConnectionWriter writer = new ConnectionWriterImpl(reliabilityOut);
OutputStream out1 = writer.getOutputStream();
out1.write(frame);
out1.flush();
@@ -93,15 +96,16 @@ public class FrameReadWriteTest extends BriarTestCase {
assertArrayEquals(tag, recoveredTag);
assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey));
// Read the frames back
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, segCipher, tagKey, segKey, false, recoveredTag);
IncomingErrorCorrectionLayer correcter1 =
new NullIncomingErrorCorrectionLayer(decrypter);
IncomingAuthenticationLayer authenticator =
new IncomingAuthenticationLayerImpl(correcter1, mac, macKey);
IncomingReliabilityLayer reliability =
new NullIncomingReliabilityLayer(authenticator);
ConnectionReader reader = new ConnectionReaderImpl(reliability, false);
IncomingEncryptionLayer encryptionIn = new IncomingEncryptionLayerImpl(
in, tagCipher, segCipher, tagKey, segKey, false, recoveredTag);
IncomingErrorCorrectionLayer correctionIn =
new NullIncomingErrorCorrectionLayer(encryptionIn);
IncomingAuthenticationLayer authenticationIn =
new IncomingAuthenticationLayerImpl(correctionIn, mac, macKey);
IncomingReliabilityLayer reliabilityIn =
new NullIncomingReliabilityLayer(authenticationIn);
ConnectionReader reader = new ConnectionReaderImpl(reliabilityIn,
false);
InputStream in1 = reader.getInputStream();
byte[] recovered = new byte[frame.length];
int offset = 0;