Added a connection registry to avoid creating redundant connections.

This commit is contained in:
akwizgran
2011-12-09 17:34:58 +00:00
parent 9abe920edb
commit f9f41acde9
69 changed files with 435 additions and 182 deletions

View File

@@ -53,6 +53,7 @@
<test name='net.sf.briar.transport.ConnectionEncrypterImplTest'/>
<test name='net.sf.briar.transport.ConnectionReaderImplTest'/>
<test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/>
<test name='net.sf.briar.transport.ConnectionRegistryImplTest'/>
<test name='net.sf.briar.transport.ConnectionWindowImplTest'/>
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
<test name='net.sf.briar.transport.ConnectionWriterTest'/>

View File

@@ -0,0 +1,19 @@
package net.sf.briar;
import java.lang.Thread.UncaughtExceptionHandler;
import junit.framework.TestCase;
public abstract class BriarTestCase extends TestCase {
public BriarTestCase() {
super();
// Ensure exceptions thrown on worker threads cause tests to fail
UncaughtExceptionHandler fail = new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable throwable) {
fail();
}
};
Thread.setDefaultUncaughtExceptionHandler(fail);
}
}

View File

@@ -4,12 +4,10 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Test;
public class LockFairnessTest extends TestCase {
public class LockFairnessTest extends BriarTestCase {
private final ReentrantReadWriteLock lock =
new ReentrantReadWriteLock(true); // Fair

View File

@@ -17,7 +17,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Author;
@@ -60,7 +59,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ProtocolIntegrationTest extends TestCase {
public class ProtocolIntegrationTest extends BriarTestCase {
private final BatchId ack = new BatchId(TestUtils.getRandomId());
private final long timestamp = System.currentTimeMillis();

View File

@@ -10,13 +10,13 @@ import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.Bytes;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;
public class CounterModeTest extends TestCase {
public class CounterModeTest extends BriarTestCase {
private static final String CIPHER_ALGO = "AES";
private static final String CIPHER_MODE = "AES/CTR/NoPadding";

View File

@@ -8,12 +8,12 @@ import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.ErasableKey;
import org.junit.Test;
public class ErasableKeyTest extends TestCase {
public class ErasableKeyTest extends BriarTestCase {
private static final String CIPHER = "AES";
private static final String CIPHER_MODE = "AES/CTR/NoPadding";

View File

@@ -5,14 +5,14 @@ import java.util.Arrays;
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.protocol.ProtocolConstants;
import org.junit.Test;
public class KeyDerivationTest extends TestCase {
public class KeyDerivationTest extends BriarTestCase {
private final CryptoComponent crypto;
private final byte[] secret;

View File

@@ -9,14 +9,14 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BasicH2Test extends TestCase {
public class BasicH2Test extends BriarTestCase {
private static final String CREATE_TABLE =
"CREATE TABLE foo"

View File

@@ -3,13 +3,13 @@ package net.sf.briar.db;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.db.DbException;
import net.sf.briar.db.DatabaseCleaner.Callback;
import org.junit.Test;
public class DatabaseCleanerImplTest extends TestCase {
public class DatabaseCleanerImplTest extends BriarTestCase {
@Test
public void testStoppingCleanerWakesItUp() throws Exception {

View File

@@ -8,7 +8,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
@@ -47,7 +47,7 @@ import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
public abstract class DatabaseComponentTest extends TestCase {
public abstract class DatabaseComponentTest extends BriarTestCase {
protected final Object txn = new Object();
protected final AuthorId authorId;

View File

@@ -16,7 +16,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
@@ -57,7 +57,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class H2DatabaseTest extends TestCase {
public class H2DatabaseTest extends BriarTestCase {
private static final int ONE_MEGABYTE = 1024 * 1024;
private static final int MAX_SIZE = 5 * ONE_MEGABYTE;

View File

@@ -3,13 +3,13 @@ import java.awt.Font;
import java.io.File;
import java.util.Locale;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.i18n.FontManager;
import org.junit.Test;
public class FontManagerTest extends TestCase {
public class FontManagerTest extends BriarTestCase {
private final File fontDir = TestUtils.getFontDirectory();

View File

@@ -5,7 +5,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Locale;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.i18n.I18n;
@@ -16,7 +16,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class I18nTest extends TestCase {
public class I18nTest extends BriarTestCase {
private final File base =
new File(TestUtils.getBuildDirectory(), "i18n.properties");

View File

@@ -8,7 +8,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
@@ -26,7 +26,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class InvitationWorkerTest extends TestCase {
public class InvitationWorkerTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();

View File

@@ -3,12 +3,12 @@ package net.sf.briar.lifecycle;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.lifecycle.ShutdownManager;
import org.junit.Test;
public class ShutdownManagerImplTest extends TestCase {
public class ShutdownManagerImplTest extends BriarTestCase {
@Test
public void testAddAndRemove() {

View File

@@ -1,10 +1,11 @@
package net.sf.briar.plugins;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.protocol.TransportId;
@@ -16,17 +17,20 @@ import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
public class PluginManagerImplTest extends TestCase {
public class PluginManagerImplTest extends BriarTestCase {
@SuppressWarnings("unchecked")
@Test
public void testStartAndStop() throws Exception {
Mockery context = new Mockery();
final DatabaseComponent db = context.mock(DatabaseComponent.class);
final Poller poller = context.mock(Poller.class);
final ConnectionDispatcher dispatcher =
context.mock(ConnectionDispatcher.class);
final UiCallback uiCallback = context.mock(UiCallback.class);
final AtomicInteger index = new AtomicInteger(0);
context.checking(new Expectations() {{
oneOf(poller).startPolling(with(any(Collection.class)));
allowing(db).getLocalIndex(with(any(TransportId.class)));
will(returnValue(null));
allowing(db).addTransport(with(any(TransportId.class)));
@@ -37,10 +41,10 @@ public class PluginManagerImplTest extends TestCase {
will(returnValue(new TransportProperties()));
allowing(db).setLocalProperties(with(any(TransportId.class)),
with(any(TransportProperties.class)));
oneOf(poller).stopPolling();
}});
Executor executor = Executors.newCachedThreadPool();
Poller poller = new PollerImpl();
PluginManagerImpl p = new PluginManagerImpl(db, executor, poller,
PluginManagerImpl p = new PluginManagerImpl(executor, db, poller,
dispatcher, uiCallback);
// We expect either 2 or 3 plugins to be started, depending on whether
// the test machine has a Bluetooth device

View File

@@ -1,10 +1,10 @@
package net.sf.briar.plugins.file;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import org.junit.Test;
public class LinuxRemovableDriveFinderTest extends TestCase {
public class LinuxRemovableDriveFinderTest extends BriarTestCase {
@Test
public void testParseMountPoint() {

View File

@@ -1,10 +1,10 @@
package net.sf.briar.plugins.file;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import org.junit.Test;
public class MacRemovableDriveFinderTest extends TestCase {
public class MacRemovableDriveFinderTest extends BriarTestCase {
@Test
public void testParseMountPoint() {

View File

@@ -9,15 +9,14 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
public class PollingRemovableDriveMonitorTest extends TestCase {
public class PollingRemovableDriveMonitorTest extends BriarTestCase {
@Test
public void testOneCallbackPerFile() throws Exception {

View File

@@ -10,7 +10,7 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.plugins.BatchPluginCallback;
@@ -25,7 +25,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RemovableDrivePluginTest extends TestCase {
public class RemovableDrivePluginTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
private final ContactId contactId = new ContactId(0);

View File

@@ -6,7 +6,7 @@ import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
import net.sf.briar.util.OsUtils;
@@ -15,7 +15,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class UnixRemovableDriveMonitorTest extends TestCase {
public class UnixRemovableDriveMonitorTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();

View File

@@ -11,7 +11,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
@@ -20,7 +20,7 @@ import net.sf.briar.api.transport.StreamTransportConnection;
import org.junit.Test;
public class SimpleSocketPluginTest extends TestCase {
public class SimpleSocketPluginTest extends BriarTestCase {
private final ContactId contactId = new ContactId(0);

View File

@@ -4,7 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Ack;
@@ -25,7 +25,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class AckReaderTest extends TestCase {
public class AckReaderTest extends BriarTestCase {
private final SerialComponent serial;
private final ReaderFactory readerFactory;

View File

@@ -5,7 +5,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Types;
@@ -24,7 +24,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class BatchReaderTest extends TestCase {
public class BatchReaderTest extends BriarTestCase {
private final ReaderFactory readerFactory;
private final WriterFactory writerFactory;

View File

@@ -19,7 +19,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Ack;
@@ -50,7 +50,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConstantsTest extends TestCase {
public class ConstantsTest extends BriarTestCase {
private final CryptoComponent crypto;
private final GroupFactory groupFactory;

View File

@@ -4,7 +4,7 @@ import static org.junit.Assert.assertArrayEquals;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.MessageDigest;
@@ -19,7 +19,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConsumersTest extends TestCase {
public class ConsumersTest extends BriarTestCase {
private CryptoComponent crypto = null;

View File

@@ -4,7 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Offer;
@@ -25,7 +25,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class OfferReaderTest extends TestCase {
public class OfferReaderTest extends BriarTestCase {
private final SerialComponent serial;
private final ReaderFactory readerFactory;

View File

@@ -7,7 +7,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Batch;
@@ -37,7 +37,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ProtocolReadWriteTest extends TestCase {
public class ProtocolReadWriteTest extends BriarTestCase {
private final ProtocolReaderFactory readerFactory;
private final ProtocolWriterFactory writerFactory;

View File

@@ -4,7 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.BitSet;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.ProtocolWriter;
import net.sf.briar.api.protocol.Request;
@@ -19,7 +19,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ProtocolWriterImplTest extends TestCase {
public class ProtocolWriterImplTest extends BriarTestCase {
private final PacketFactory packetFactory;
private final SerialComponent serial;

View File

@@ -4,7 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.BitSet;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.ProtocolConstants;
@@ -24,7 +24,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class RequestReaderTest extends TestCase {
public class RequestReaderTest extends BriarTestCase {
private final ReaderFactory readerFactory;
private final WriterFactory writerFactory;

View File

@@ -8,7 +8,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.MessageDigest;
@@ -30,7 +30,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class UnverifiedBatchImplTest extends TestCase {
public class UnverifiedBatchImplTest extends BriarTestCase {
private final CryptoComponent crypto;
private final byte[] raw, raw1;

View File

@@ -9,7 +9,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
@@ -28,6 +28,7 @@ import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.ConnectionRegistry;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.db.DatabaseModule;
@@ -45,7 +46,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class BatchConnectionReadWriteTest extends TestCase {
public class BatchConnectionReadWriteTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
private final File aliceDir = new File(testDir, "alice");
@@ -107,6 +108,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
db.addLocalPrivateMessage(message, contactId);
// Create an outgoing batch connection
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionRegistry connRegistry =
alice.getInstance(ConnectionRegistry.class);
ConnectionWriterFactory connFactory =
alice.getInstance(ConnectionWriterFactory.class);
ProtocolWriterFactory protoFactory =
@@ -114,8 +117,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
Long.MAX_VALUE, false);
OutgoingBatchConnection batchOut = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
connRegistry, connFactory, protoFactory, contactId, transportId,
transportIndex, transport);
// Write whatever needs to be written
batchOut.write();
assertTrue(transport.getDisposed());
@@ -161,6 +164,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
assertEquals(contactId, ctx.getContactId());
assertEquals(transportIndex, ctx.getTransportIndex());
// Create an incoming batch connection
ConnectionRegistry connRegistry =
bob.getInstance(ConnectionRegistry.class);
ConnectionReaderFactory connFactory =
bob.getInstance(ConnectionReaderFactory.class);
ProtocolReaderFactory protoFactory =
@@ -168,7 +173,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
TestBatchTransportReader transport = new TestBatchTransportReader(in);
IncomingBatchConnection batchIn = new IncomingBatchConnection(
new ImmediateExecutor(), new ImmediateExecutor(), db,
connFactory, protoFactory, ctx, transport, tag);
connRegistry, connFactory, protoFactory, ctx, transportId,
transport, tag);
// No messages should have been added yet
assertFalse(listener.messagesAdded);
// Read whatever needs to be read

View File

@@ -5,7 +5,7 @@ import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.db.DatabaseComponent;
@@ -15,9 +15,11 @@ import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.ProtocolWriterFactory;
import net.sf.briar.api.protocol.RawBatch;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionRegistry;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.api.transport.TransportConstants;
import net.sf.briar.crypto.CryptoModule;
@@ -35,13 +37,15 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class OutgoingBatchConnectionTest extends TestCase {
public class OutgoingBatchConnectionTest extends BriarTestCase {
private final Mockery context;
private final DatabaseComponent db;
private final ConnectionRegistry connRegistry;
private final ConnectionWriterFactory connFactory;
private final ProtocolWriterFactory protoFactory;
private final ContactId contactId;
private final TransportId transportId;
private final TransportIndex transportIndex;
private final byte[] secret;
@@ -62,9 +66,11 @@ public class OutgoingBatchConnectionTest extends TestCase {
new SerialModule(), new TransportModule(),
new ProtocolBatchModule(), new ProtocolModule(),
new ProtocolStreamModule());
connRegistry = i.getInstance(ConnectionRegistry.class);
connFactory = i.getInstance(ConnectionWriterFactory.class);
protoFactory = i.getInstance(ProtocolWriterFactory.class);
contactId = new ContactId(1);
transportId = new TransportId(TestUtils.getRandomId());
transportIndex = new TransportIndex(13);
secret = new byte[32];
}
@@ -75,8 +81,8 @@ public class OutgoingBatchConnectionTest extends TestCase {
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
ProtocolConstants.MAX_PACKET_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
connRegistry, connFactory, protoFactory, contactId, transportId,
transportIndex, transport);
final ConnectionContext ctx = context.mock(ConnectionContext.class);
context.checking(new Expectations() {{
oneOf(db).getConnectionContext(contactId, transportIndex);
@@ -99,8 +105,8 @@ public class OutgoingBatchConnectionTest extends TestCase {
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
TransportConstants.MIN_CONNECTION_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
connRegistry, connFactory, protoFactory, contactId, transportId,
transportIndex, transport);
final ConnectionContext ctx = context.mock(ConnectionContext.class);
context.checking(new Expectations() {{
oneOf(db).getConnectionContext(contactId, transportIndex);
@@ -135,8 +141,8 @@ public class OutgoingBatchConnectionTest extends TestCase {
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
TransportConstants.MIN_CONNECTION_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
connRegistry, connFactory, protoFactory, contactId, transportId,
transportIndex, transport);
final ConnectionContext ctx = context.mock(ConnectionContext.class);
final Ack ack = context.mock(Ack.class);
final BatchId batchId = new BatchId(TestUtils.getRandomId());

View File

@@ -10,7 +10,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.Bytes;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.serial.Consumer;
@@ -20,7 +20,7 @@ import net.sf.briar.util.StringUtils;
import org.junit.Test;
public class ReaderImplTest extends TestCase {
public class ReaderImplTest extends BriarTestCase {
private ByteArrayInputStream in = null;
private ReaderImpl r = null;

View File

@@ -8,13 +8,13 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.util.StringUtils;
import org.junit.Before;
import org.junit.Test;
public class WriterImplTest extends TestCase {
public class WriterImplTest extends BriarTestCase {
private ByteArrayOutputStream out = null;
private WriterImpl w = null;

View File

@@ -5,7 +5,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.setup.SetupCallback;
@@ -18,7 +18,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SetupWorkerTest extends TestCase {
public class SetupWorkerTest extends BriarTestCase {
private static final int HEADER_SIZE = 1234;

View File

@@ -7,7 +7,7 @@ import java.io.ByteArrayInputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
@@ -19,7 +19,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionDecrypterImplTest extends TestCase {
public class ConnectionDecrypterImplTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;

View File

@@ -7,7 +7,7 @@ import java.io.ByteArrayOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.crypto.CryptoModule;
@@ -17,7 +17,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionEncrypterImplTest extends TestCase {
public class ConnectionEncrypterImplTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;

View File

@@ -11,7 +11,7 @@ import java.util.concurrent.Executor;
import javax.crypto.Cipher;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.crypto.CryptoComponent;
@@ -35,7 +35,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionRecogniserImplTest extends TestCase {
public class ConnectionRecogniserImplTest extends BriarTestCase {
private final CryptoComponent crypto;
private final ContactId contactId;

View File

@@ -0,0 +1,73 @@
package net.sf.briar.transport;
import java.util.Arrays;
import java.util.Collections;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.transport.ConnectionRegistry;
import org.junit.Test;
public class ConnectionRegistryImplTest extends BriarTestCase {
private final ContactId contactId, contactId1;
private final TransportId transportId, transportId1;
public ConnectionRegistryImplTest() {
super();
contactId = new ContactId(1);
contactId1 = new ContactId(2);
transportId = new TransportId(TestUtils.getRandomId());
transportId1 = new TransportId(TestUtils.getRandomId());
}
@Test
public void testRegisterAndUnregister() {
ConnectionRegistry c = new ConnectionRegistryImpl();
// The registry should be empty
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId));
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId1));
// Check that a registered connection shows up
c.registerConnection(contactId, transportId);
assertEquals(Collections.singletonList(contactId),
c.getConnectedContacts(transportId));
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId1));
// Register an identical connection - lookup should be unaffected
c.registerConnection(contactId, transportId);
assertEquals(Collections.singletonList(contactId),
c.getConnectedContacts(transportId));
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId1));
// Unregister one of the connections - lookup should be unaffected
c.unregisterConnection(contactId, transportId);
assertEquals(Collections.singletonList(contactId),
c.getConnectedContacts(transportId));
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId1));
// Unregister the other connection - lookup should be affected
c.unregisterConnection(contactId, transportId);
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId));
assertEquals(Collections.emptyList(),
c.getConnectedContacts(transportId1));
// Try to unregister the connection again - exception should be thrown
try {
c.unregisterConnection(contactId, transportId);
fail();
} catch(IllegalArgumentException expected) {}
// Register both contacts with one transport, one contact with both
c.registerConnection(contactId, transportId);
c.registerConnection(contactId1, transportId);
c.registerConnection(contactId1, transportId1);
assertEquals(Arrays.asList(new ContactId[] {contactId, contactId1}),
c.getConnectedContacts(transportId));
assertEquals(Collections.singletonList(contactId1),
c.getConnectedContacts(transportId1));
}
}

View File

@@ -4,7 +4,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionWindow;
@@ -16,7 +16,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionWindowImplTest extends TestCase {
public class ConnectionWindowImplTest extends BriarTestCase {
private final CryptoComponent crypto;
private final byte[] secret;

View File

@@ -6,7 +6,7 @@ import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGT
import java.io.ByteArrayOutputStream;
import java.util.Random;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
@@ -23,7 +23,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionWriterTest extends TestCase {
public class ConnectionWriterTest extends BriarTestCase {
private final ConnectionWriterFactory connectionWriterFactory;
private final byte[] secret;

View File

@@ -12,7 +12,7 @@ import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.transport.ConnectionReader;
@@ -24,7 +24,7 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class FrameReadWriteTest extends TestCase {
public class FrameReadWriteTest extends BriarTestCase {
private final CryptoComponent crypto;
private final Cipher tagCipher, frameCipher;

View File

@@ -5,7 +5,7 @@ import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import javax.crypto.Mac;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.crypto.CryptoModule;
@@ -13,7 +13,7 @@ import net.sf.briar.crypto.CryptoModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public abstract class TransportTest extends TestCase {
public abstract class TransportTest extends BriarTestCase {
protected final Mac mac;
protected final ErasableKey macKey;

View File

@@ -1,10 +1,10 @@
package net.sf.briar.util;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import org.junit.Test;
public class ByteUtilsTest extends TestCase {
public class ByteUtilsTest extends BriarTestCase {
@Test
public void testReadUint16() {

View File

@@ -6,7 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.FileUtils.Callback;
@@ -16,7 +16,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class FileUtilsTest extends TestCase {
public class FileUtilsTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();

View File

@@ -1,11 +1,11 @@
package net.sf.briar.util;
import static org.junit.Assert.assertArrayEquals;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import org.junit.Test;
public class StringUtilsTest extends TestCase {
public class StringUtilsTest extends BriarTestCase {
@Test
public void testHead() {

View File

@@ -13,7 +13,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import junit.framework.TestCase;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.ZipUtils.Callback;
@@ -23,7 +23,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ZipUtilsTest extends TestCase {
public class ZipUtilsTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();