mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Refactored transport component and renamed WritersModule.
The goal of the refactoring was to clean up the dependencies of IncomingBatchConnection and OutgoingBatchConnection.
This commit is contained in:
@@ -46,7 +46,7 @@ import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
import net.sf.briar.protocol.ProtocolModule;
|
||||
import net.sf.briar.protocol.writers.WritersModule;
|
||||
import net.sf.briar.protocol.writers.ProtocolWritersModule;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
import net.sf.briar.transport.TransportModule;
|
||||
|
||||
@@ -83,8 +83,8 @@ public class FileReadWriteTest extends TestCase {
|
||||
public FileReadWriteTest() throws Exception {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule(),
|
||||
new ProtocolModule(), new SerialModule(), new TransportModule(),
|
||||
new WritersModule());
|
||||
new ProtocolModule(), new ProtocolWritersModule(),
|
||||
new SerialModule(), new TransportModule());
|
||||
connectionReaderFactory = i.getInstance(ConnectionReaderFactory.class);
|
||||
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
||||
protocolReaderFactory = i.getInstance(ProtocolReaderFactory.class);
|
||||
@@ -132,7 +132,7 @@ public class FileReadWriteTest extends TestCase {
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
// Use Alice's secret for writing
|
||||
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
||||
true, transportId, connection, aliceSecret);
|
||||
Long.MAX_VALUE, true, transportId, connection, aliceSecret);
|
||||
out = w.getOutputStream();
|
||||
|
||||
AckWriter a = protocolWriterFactory.createAckWriter(out);
|
||||
|
||||
@@ -29,7 +29,7 @@ import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
|
||||
import net.sf.briar.api.protocol.writers.TransportWriter;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
import net.sf.briar.protocol.writers.WritersModule;
|
||||
import net.sf.briar.protocol.writers.ProtocolWritersModule;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -53,7 +53,8 @@ public class ProtocolReadWriteTest extends TestCase {
|
||||
public ProtocolReadWriteTest() throws Exception {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule(),
|
||||
new ProtocolModule(), new SerialModule(), new WritersModule());
|
||||
new ProtocolModule(), new ProtocolWritersModule(),
|
||||
new SerialModule());
|
||||
readerFactory = i.getInstance(ProtocolReaderFactory.class);
|
||||
writerFactory = i.getInstance(ProtocolWriterFactory.class);
|
||||
batchId = new BatchId(TestUtils.getRandomId());
|
||||
|
||||
@@ -40,12 +40,14 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
@Test
|
||||
public void testSingleByteFrame() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, true,
|
||||
transportId, connection, ivCipher, frameCipher, ivKey,
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
||||
true, transportId, connection, ivCipher, frameCipher, ivKey,
|
||||
frameKey);
|
||||
e.getOutputStream().write((byte) 0);
|
||||
e.writeMac(new byte[MAC_LENGTH]);
|
||||
assertEquals(IV_LENGTH + 1 + MAC_LENGTH, out.toByteArray().length);
|
||||
byte[] ciphertext = out.toByteArray();
|
||||
assertEquals(IV_LENGTH + 1 + MAC_LENGTH, ciphertext.length);
|
||||
assertEquals(Long.MAX_VALUE - ciphertext.length, e.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -93,9 +95,9 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
byte[] expected = out.toByteArray();
|
||||
// Use a ConnectionEncrypter to encrypt the plaintext
|
||||
out.reset();
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, initiator,
|
||||
transportId, connection, ivCipher, frameCipher, ivKey,
|
||||
frameKey);
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
||||
initiator, transportId, connection, ivCipher, frameCipher,
|
||||
ivKey, frameKey);
|
||||
e.getOutputStream().write(plaintext);
|
||||
e.writeMac(plaintextMac);
|
||||
e.getOutputStream().write(plaintext1);
|
||||
@@ -103,5 +105,6 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
byte[] actual = out.toByteArray();
|
||||
// Check that the actual ciphertext matches the expected ciphertext
|
||||
assertTrue(Arrays.equals(expected, actual));
|
||||
assertEquals(Long.MAX_VALUE - actual.length, e.getCapacity());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,32 +100,4 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
byte[] actual = out.toByteArray();
|
||||
assertTrue(Arrays.equals(expected, actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCapacity() throws Exception {
|
||||
int overheadPerFrame = headerLength + macLength;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriterImpl w = new ConnectionWriterImpl(e, mac);
|
||||
// Full frame
|
||||
long capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - overheadPerFrame, capacity);
|
||||
// Partial frame
|
||||
capacity = w.getCapacity(overheadPerFrame + 1);
|
||||
assertEquals(1, capacity);
|
||||
// Full frame and partial frame
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH + 1);
|
||||
assertEquals(MAX_FRAME_LENGTH + 1 - 2 * overheadPerFrame, capacity);
|
||||
// Buffer some output
|
||||
w.getOutputStream().write(0);
|
||||
// Full frame minus buffered frame
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - 1 - 2 * overheadPerFrame, capacity);
|
||||
// Flush the buffer
|
||||
w.flush();
|
||||
assertEquals(1 + overheadPerFrame, out.size());
|
||||
// Back to square one
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - overheadPerFrame, capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGTH;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.TransportConstants;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -30,20 +31,20 @@ public class ConnectionWriterTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testOverhead() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(
|
||||
TransportConstants.MIN_CONNECTION_LENGTH);
|
||||
ByteArrayOutputStream out =
|
||||
new ByteArrayOutputStream(MIN_CONNECTION_LENGTH);
|
||||
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
||||
true, transportId, connection, secret);
|
||||
MIN_CONNECTION_LENGTH, true, transportId, connection, secret);
|
||||
// Check that the connection writer thinks there's room for a packet
|
||||
long capacity = w.getCapacity(TransportConstants.MIN_CONNECTION_LENGTH);
|
||||
assertTrue(capacity >= ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
assertTrue(capacity <= TransportConstants.MIN_CONNECTION_LENGTH);
|
||||
long capacity = w.getCapacity();
|
||||
assertTrue(capacity >= MAX_PACKET_LENGTH);
|
||||
assertTrue(capacity <= MIN_CONNECTION_LENGTH);
|
||||
// Check that there really is room for a packet
|
||||
byte[] payload = new byte[ProtocolConstants.MAX_PACKET_LENGTH];
|
||||
byte[] payload = new byte[MAX_PACKET_LENGTH];
|
||||
w.getOutputStream().write(payload);
|
||||
w.getOutputStream().flush();
|
||||
long used = out.size();
|
||||
assertTrue(used >= ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
assertTrue(used <= TransportConstants.MIN_CONNECTION_LENGTH);
|
||||
assertTrue(used >= MAX_PACKET_LENGTH);
|
||||
assertTrue(used <= MIN_CONNECTION_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ public class FrameReadWriteTest extends TestCase {
|
||||
// Write the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||
initiator, transportId, connection, ivCipher, frameCipher,
|
||||
ivKey, frameKey);
|
||||
Long.MAX_VALUE, initiator, transportId, connection, ivCipher,
|
||||
frameCipher, ivKey, frameKey);
|
||||
mac.init(macKey);
|
||||
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac);
|
||||
OutputStream out1 = writer.getOutputStream();
|
||||
|
||||
@@ -1,26 +1,51 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/** A ConnectionEncrypter that performs no encryption. */
|
||||
class NullConnectionEncrypter implements ConnectionEncrypter {
|
||||
class NullConnectionEncrypter extends FilterOutputStream
|
||||
implements ConnectionEncrypter {
|
||||
|
||||
private final OutputStream out;
|
||||
private long capacity;
|
||||
|
||||
NullConnectionEncrypter(OutputStream out) {
|
||||
this.out = out;
|
||||
this(out, Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
NullConnectionEncrypter(OutputStream out, long capacity) {
|
||||
super(out);
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() {
|
||||
return out;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void writeMac(byte[] mac) throws IOException {
|
||||
out.write(mac);
|
||||
capacity -= mac.length;
|
||||
}
|
||||
|
||||
public long getCapacity(long capacity) {
|
||||
public long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
out.write(b);
|
||||
capacity--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
out.write(b, off, len);
|
||||
capacity -= len;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteByteDoesNotBlockUntilBufferIsFull() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out, Long.MAX_VALUE);
|
||||
ConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
final OutputStream out1 = w.getOutputStream();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -52,7 +52,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteByteBlocksWhenBufferIsFull() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out, Long.MAX_VALUE);
|
||||
PaddedConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
final OutputStream out1 = w.getOutputStream();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -86,7 +86,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteArrayDoesNotBlockUntilBufferIsFull() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out, Long.MAX_VALUE);
|
||||
ConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
final OutputStream out1 = w.getOutputStream();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -115,7 +115,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteArrayBlocksWhenBufferIsFull() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out, Long.MAX_VALUE);
|
||||
PaddedConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
final OutputStream out1 = w.getOutputStream();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -149,7 +149,7 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteFullFrameInsertsPadding() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out, Long.MAX_VALUE);
|
||||
PaddedConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
w.getOutputStream().write(0);
|
||||
w.writeFullFrame();
|
||||
@@ -160,32 +160,4 @@ public class PaddedConnectionWriterTest extends TransportTest {
|
||||
assertEquals(1, ByteUtils.readUint16(frame, 0)); // Payload length
|
||||
assertEquals(maxPayloadLength - 1, ByteUtils.readUint16(frame, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCapacity() throws Exception {
|
||||
int overheadPerFrame = headerLength + macLength;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
||||
PaddedConnectionWriter w = new PaddedConnectionWriter(e, mac);
|
||||
// Full frame
|
||||
long capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - overheadPerFrame, capacity);
|
||||
// Partial frame
|
||||
capacity = w.getCapacity(overheadPerFrame + 1);
|
||||
assertEquals(1, capacity);
|
||||
// Full frame and partial frame
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH + 1);
|
||||
assertEquals(MAX_FRAME_LENGTH + 1 - 2 * overheadPerFrame, capacity);
|
||||
// Buffer some output
|
||||
w.getOutputStream().write(0);
|
||||
// Full frame minus buffered frame
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - 1 - 2 * overheadPerFrame, capacity);
|
||||
// Flush the buffer
|
||||
w.writeFullFrame();
|
||||
assertEquals(MAX_FRAME_LENGTH, out.size());
|
||||
// Back to square one
|
||||
capacity = w.getCapacity(MAX_FRAME_LENGTH);
|
||||
assertEquals(MAX_FRAME_LENGTH - overheadPerFrame, capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ implements BatchTransportReader {
|
||||
super(in);
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
public InputStream getInputStream() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@ implements BatchTransportWriter {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public long getCapacity() throws IOException {
|
||||
public long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
public OutputStream getOutputStream() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user