mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Expose the encryption and authentication overhead without breaking
encapsulation. This should allow callers to calculate maximum packet sizes without knowing the details of the transport protocol.
This commit is contained in:
@@ -100,4 +100,32 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
byte[] actual = out.toByteArray();
|
||||
assertTrue(Arrays.equals(expected, actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCapacity() throws Exception {
|
||||
int overheadPerFrame = 4 + mac.getMacLength();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ class NullConnectionEncrypter implements ConnectionEncrypter {
|
||||
public void writeMac(byte[] mac) throws IOException {
|
||||
out.write(mac);
|
||||
}
|
||||
|
||||
public long getCapacity(long capacity) {
|
||||
return capacity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,4 +160,32 @@ 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 = 4 + mac.getMacLength();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user