mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
96 lines
3.6 KiB
Java
96 lines
3.6 KiB
Java
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 static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.util.Random;
|
|
|
|
import net.sf.briar.BriarTestCase;
|
|
import net.sf.briar.TestDatabaseModule;
|
|
import net.sf.briar.TestUtils;
|
|
import net.sf.briar.api.ContactId;
|
|
import net.sf.briar.api.protocol.TransportId;
|
|
import net.sf.briar.api.transport.ConnectionContext;
|
|
import net.sf.briar.api.transport.ConnectionWriter;
|
|
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
|
import net.sf.briar.clock.ClockModule;
|
|
import net.sf.briar.crypto.CryptoModule;
|
|
import net.sf.briar.db.DatabaseModule;
|
|
import net.sf.briar.lifecycle.LifecycleModule;
|
|
import net.sf.briar.protocol.ProtocolModule;
|
|
import net.sf.briar.protocol.duplex.DuplexProtocolModule;
|
|
import net.sf.briar.protocol.simplex.SimplexProtocolModule;
|
|
import net.sf.briar.serial.SerialModule;
|
|
|
|
import org.junit.Test;
|
|
|
|
import com.google.inject.Guice;
|
|
import com.google.inject.Injector;
|
|
|
|
public class ConnectionWriterTest extends BriarTestCase {
|
|
|
|
private final ConnectionWriterFactory connectionWriterFactory;
|
|
private final ContactId contactId;
|
|
private final TransportId transportId;
|
|
private final byte[] secret;
|
|
|
|
public ConnectionWriterTest() throws Exception {
|
|
super();
|
|
Injector i = Guice.createInjector(new ClockModule(), new CryptoModule(),
|
|
new DatabaseModule(), new LifecycleModule(),
|
|
new ProtocolModule(), new SerialModule(),
|
|
new TestDatabaseModule(), new SimplexProtocolModule(),
|
|
new TransportModule(), new DuplexProtocolModule());
|
|
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
|
contactId = new ContactId(234);
|
|
transportId = new TransportId(TestUtils.getRandomId());
|
|
secret = new byte[32];
|
|
new Random().nextBytes(secret);
|
|
}
|
|
|
|
@Test
|
|
public void testOverheadWithTag() throws Exception {
|
|
ByteArrayOutputStream out =
|
|
new ByteArrayOutputStream(MIN_CONNECTION_LENGTH);
|
|
byte[] tag = new byte[TAG_LENGTH];
|
|
ConnectionContext ctx = new ConnectionContext(contactId, transportId,
|
|
tag, secret, 0L, true);
|
|
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
|
MIN_CONNECTION_LENGTH, ctx, true);
|
|
// Check that the connection writer thinks there's room for a packet
|
|
long capacity = w.getRemainingCapacity();
|
|
assertTrue(capacity > MAX_PACKET_LENGTH);
|
|
assertTrue(capacity < MIN_CONNECTION_LENGTH);
|
|
// Check that there really is room for a packet
|
|
byte[] payload = new byte[MAX_PACKET_LENGTH];
|
|
w.getOutputStream().write(payload);
|
|
w.getOutputStream().close();
|
|
long used = out.size();
|
|
assertTrue(used > MAX_PACKET_LENGTH);
|
|
assertTrue(used <= MIN_CONNECTION_LENGTH);
|
|
}
|
|
|
|
@Test
|
|
public void testOverheadWithoutTag() throws Exception {
|
|
ByteArrayOutputStream out =
|
|
new ByteArrayOutputStream(MIN_CONNECTION_LENGTH);
|
|
ConnectionContext ctx = new ConnectionContext(contactId, transportId,
|
|
null, secret, 0L, true);
|
|
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
|
MIN_CONNECTION_LENGTH, ctx, false);
|
|
// Check that the connection writer thinks there's room for a packet
|
|
long capacity = w.getRemainingCapacity();
|
|
assertTrue(capacity > MAX_PACKET_LENGTH);
|
|
assertTrue(capacity < MIN_CONNECTION_LENGTH);
|
|
// Check that there really is room for a packet
|
|
byte[] payload = new byte[MAX_PACKET_LENGTH];
|
|
w.getOutputStream().write(payload);
|
|
w.getOutputStream().close();
|
|
long used = out.size();
|
|
assertTrue(used > MAX_PACKET_LENGTH);
|
|
assertTrue(used <= MIN_CONNECTION_LENGTH);
|
|
}
|
|
}
|