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:
akwizgran
2011-09-21 15:22:25 +01:00
parent 7e58b25618
commit 10c3b21726
10 changed files with 115 additions and 35 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}