mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
106 lines
3.7 KiB
Java
106 lines
3.7 KiB
Java
package net.sf.briar.transport;
|
|
|
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
|
import static org.junit.Assert.assertArrayEquals;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.OutputStream;
|
|
|
|
import net.sf.briar.api.transport.ConnectionWriter;
|
|
|
|
import org.junit.Test;
|
|
|
|
public class ConnectionWriterImplTest extends TransportTest {
|
|
|
|
public ConnectionWriterImplTest() throws Exception {
|
|
super();
|
|
}
|
|
|
|
@Test
|
|
public void testFlushWithoutWriteProducesNothing() throws Exception {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
|
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
|
w.getOutputStream().flush();
|
|
w.getOutputStream().flush();
|
|
w.getOutputStream().flush();
|
|
assertEquals(0, out.size());
|
|
}
|
|
|
|
@Test
|
|
public void testSingleByteFrame() throws Exception {
|
|
int payloadLength = 1;
|
|
byte[] frame = new byte[headerLength + payloadLength + macLength];
|
|
writeHeader(frame, payloadLength, 0);
|
|
// Calculate the MAC
|
|
mac.init(macKey);
|
|
mac.update(frame, 0, headerLength + payloadLength);
|
|
mac.doFinal(frame, headerLength + payloadLength);
|
|
// Check that the ConnectionWriter gets the same results
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
|
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
|
w.getOutputStream().write(0);
|
|
w.getOutputStream().flush();
|
|
assertArrayEquals(frame, out.toByteArray());
|
|
}
|
|
|
|
@Test
|
|
public void testWriteByteToMaxLengthWritesFrame() throws Exception {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
|
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
|
OutputStream out1 = w.getOutputStream();
|
|
// The first maxPayloadLength - 1 bytes should be buffered
|
|
for(int i = 0; i < maxPayloadLength - 1; 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());
|
|
}
|
|
|
|
@Test
|
|
public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
|
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
|
OutputStream out1 = w.getOutputStream();
|
|
// The first maxPayloadLength - 1 bytes should be buffered
|
|
out1.write(new byte[maxPayloadLength - 1]);
|
|
assertEquals(0, out.size());
|
|
// The next maxPayloadLength + 1 bytes should trigger two frames
|
|
out1.write(new byte[maxPayloadLength + 1]);
|
|
assertEquals(MAX_FRAME_LENGTH * 2, out.size());
|
|
}
|
|
|
|
@Test
|
|
public void testMultipleFrames() throws Exception {
|
|
// First frame: 123-byte payload
|
|
byte[] frame = new byte[headerLength + 123 + macLength];
|
|
writeHeader(frame, 123, 0);
|
|
mac.init(macKey);
|
|
mac.update(frame, 0, headerLength + 123);
|
|
mac.doFinal(frame, headerLength + 123);
|
|
// Second frame: 1234-byte payload
|
|
byte[] frame1 = new byte[headerLength + 1234 + macLength];
|
|
writeHeader(frame1, 1234, 0);
|
|
mac.update(frame1, 0, headerLength + 1234);
|
|
mac.doFinal(frame1, headerLength + 1234);
|
|
// Concatenate the frames
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
out.write(frame);
|
|
out.write(frame1);
|
|
byte[] expected = out.toByteArray();
|
|
// Check that the ConnectionWriter gets the same results
|
|
out.reset();
|
|
ConnectionEncrypter e = new NullConnectionEncrypter(out);
|
|
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
|
w.getOutputStream().write(new byte[123]);
|
|
w.getOutputStream().flush();
|
|
w.getOutputStream().write(new byte[1234]);
|
|
w.getOutputStream().flush();
|
|
byte[] actual = out.toByteArray();
|
|
assertArrayEquals(expected, actual);
|
|
}
|
|
}
|