diff --git a/test/net/sf/briar/transport/ConnectionWriterImplTest.java b/test/net/sf/briar/transport/ConnectionWriterImplTest.java index 134b58ab69c9dae97f70f312a550a1036440e2a2..13677f4a9b06a441325cdaabe5b278ff6a71467c 100644 --- a/test/net/sf/briar/transport/ConnectionWriterImplTest.java +++ b/test/net/sf/briar/transport/ConnectionWriterImplTest.java @@ -1,5 +1,7 @@ package net.sf.briar.transport; +import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -30,7 +32,16 @@ public class ConnectionWriterImplTest extends TestCase { mac.init(crypto.generateSecretKey()); } - // FIXME: Test corner cases + @Test + public void testFlushWithoutWriteProducesNothing() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ConnectionEncrypter e = new NullConnectionEncrypter(out); + ConnectionWriter w = new ConnectionWriterImpl(e, mac); + w.getOutputStream().flush(); + w.getOutputStream().flush(); + w.getOutputStream().flush(); + assertEquals(0, out.size()); + } @Test public void testSingleByteFrame() throws Exception { @@ -49,6 +60,24 @@ public class ConnectionWriterImplTest extends TestCase { assertTrue(Arrays.equals(frame, out.toByteArray())); } + @Test + public void testFrameIsWrittenAtMaxLength() throws Exception { + int maxPayloadLength = MAX_FRAME_LENGTH - 6 - mac.getMacLength(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ConnectionEncrypter e = new NullConnectionEncrypter(out); + ConnectionWriter w = new ConnectionWriterImpl(e, mac); + OutputStream out1 = w.getOutputStream(); + // The first maxPayloadLength bytes should be buffered + for(int i = 0; i < maxPayloadLength; i++) out1.write(0); + assertEquals(0, out.size()); + // The next byte should trigger the writing of a frame + out1.write(0); + assertEquals(MAX_FRAME_LENGTH, out.size()); + // Flushing the stream should write a single-byte frame + out1.flush(); + assertEquals(MAX_FRAME_LENGTH + 6 + 1 + mac.getMacLength(), out.size()); + } + @Test public void testMultipleFrames() throws Exception { // First frame: 123-byte payload