mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Added a connection registry to avoid creating redundant connections.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package net.sf.briar.api.plugins;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
|
||||
public interface Plugin {
|
||||
@@ -28,10 +30,11 @@ public interface Plugin {
|
||||
long getPollingInterval();
|
||||
|
||||
/**
|
||||
* Attempts to establish connections to all contacts, passing any created
|
||||
* connections to the callback.
|
||||
* Attempts to establish connections to contacts, passing any created
|
||||
* connections to the callback. To avoid creating redundant connections,
|
||||
* the plugin may exclude the given contacts from polling.
|
||||
*/
|
||||
void poll();
|
||||
void poll(Collection<ContactId> connected);
|
||||
|
||||
/** Returns true if the plugin supports exchanging invitations. */
|
||||
boolean supportsInvitations();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.api.protocol.batch;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.transport.BatchTransportReader;
|
||||
import net.sf.briar.api.transport.BatchTransportWriter;
|
||||
@@ -8,9 +9,9 @@ import net.sf.briar.api.transport.ConnectionContext;
|
||||
|
||||
public interface BatchConnectionFactory {
|
||||
|
||||
void createIncomingConnection(ConnectionContext ctx,
|
||||
void createIncomingConnection(ConnectionContext ctx, TransportId t,
|
||||
BatchTransportReader r, byte[] tag);
|
||||
|
||||
void createOutgoingConnection(ContactId c, TransportIndex i,
|
||||
void createOutgoingConnection(ContactId c, TransportId t, TransportIndex i,
|
||||
BatchTransportWriter w);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package net.sf.briar.api.protocol.stream;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
|
||||
public interface StreamConnectionFactory {
|
||||
|
||||
void createIncomingConnection(ConnectionContext ctx,
|
||||
void createIncomingConnection(ConnectionContext ctx, TransportId t,
|
||||
StreamTransportConnection s, byte[] tag);
|
||||
|
||||
void createOutgoingConnection(ContactId c, TransportIndex i,
|
||||
void createOutgoingConnection(ContactId c, TransportId t, TransportIndex i,
|
||||
StreamTransportConnection s);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,11 @@ public interface ConnectionDispatcher {
|
||||
|
||||
void dispatchReader(TransportId t, BatchTransportReader r);
|
||||
|
||||
void dispatchWriter(ContactId c, TransportIndex i, BatchTransportWriter w);
|
||||
void dispatchWriter(ContactId c, TransportId t, TransportIndex i,
|
||||
BatchTransportWriter w);
|
||||
|
||||
void dispatchIncomingConnection(TransportId t, StreamTransportConnection s);
|
||||
|
||||
void dispatchOutgoingConnection(ContactId c, TransportIndex i,
|
||||
StreamTransportConnection s);
|
||||
void dispatchOutgoingConnection(ContactId c, TransportId t,
|
||||
TransportIndex i, StreamTransportConnection s);
|
||||
}
|
||||
|
||||
18
api/net/sf/briar/api/transport/ConnectionRegistry.java
Normal file
18
api/net/sf/briar/api/transport/ConnectionRegistry.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package net.sf.briar.api.transport;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
|
||||
/**
|
||||
* Keeps track of which contacts are currently connected by which transports.
|
||||
*/
|
||||
public interface ConnectionRegistry {
|
||||
|
||||
void registerConnection(ContactId c, TransportId t);
|
||||
|
||||
void unregisterConnection(ContactId c, TransportId t);
|
||||
|
||||
Collection<ContactId> getConnectedContacts(TransportId t);
|
||||
}
|
||||
@@ -51,8 +51,8 @@ class PluginManagerImpl implements PluginManager {
|
||||
"net.sf.briar.plugins.socket.SimpleSocketPluginFactory"
|
||||
};
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final Executor pluginExecutor;
|
||||
private final DatabaseComponent db;
|
||||
private final Poller poller;
|
||||
private final ConnectionDispatcher dispatcher;
|
||||
private final UiCallback uiCallback;
|
||||
@@ -60,11 +60,11 @@ class PluginManagerImpl implements PluginManager {
|
||||
private final List<StreamPlugin> streamPlugins; // Locking: this
|
||||
|
||||
@Inject
|
||||
PluginManagerImpl(DatabaseComponent db,
|
||||
@PluginExecutor Executor pluginExecutor, Poller poller,
|
||||
PluginManagerImpl(@PluginExecutor Executor pluginExecutor,
|
||||
DatabaseComponent db, Poller poller,
|
||||
ConnectionDispatcher dispatcher, UiCallback uiCallback) {
|
||||
this.db = db;
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.db = db;
|
||||
this.poller = poller;
|
||||
this.dispatcher = dispatcher;
|
||||
this.uiCallback = uiCallback;
|
||||
@@ -295,7 +295,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
|
||||
public void writerCreated(ContactId c, BatchTransportWriter w) {
|
||||
assert index != null;
|
||||
dispatcher.dispatchWriter(c, index, w);
|
||||
dispatcher.dispatchWriter(c, id, index, w);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +310,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
public void outgoingConnectionCreated(ContactId c,
|
||||
StreamTransportConnection s) {
|
||||
assert index != null;
|
||||
dispatcher.dispatchOutgoingConnection(c, index, s);
|
||||
dispatcher.dispatchOutgoingConnection(c, id, index, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,14 +6,25 @@ import java.util.TreeSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.plugins.Plugin;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class PollerImpl implements Poller, Runnable {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PollerImpl.class.getName());
|
||||
|
||||
private final SortedSet<PollTime> pollTimes = new TreeSet<PollTime>();
|
||||
private final ConnectionRegistry connRegistry;
|
||||
private final SortedSet<PollTime> pollTimes;
|
||||
|
||||
@Inject
|
||||
PollerImpl(ConnectionRegistry connRegistry) {
|
||||
this.connRegistry = connRegistry;
|
||||
pollTimes = new TreeSet<PollTime>();
|
||||
}
|
||||
|
||||
public synchronized void startPolling(Collection<Plugin> plugins) {
|
||||
for(Plugin plugin : plugins) schedule(plugin);
|
||||
@@ -41,8 +52,10 @@ class PollerImpl implements Poller, Runnable {
|
||||
long now = System.currentTimeMillis();
|
||||
if(now <= p.time) {
|
||||
pollTimes.remove(p);
|
||||
Collection<ContactId> connected =
|
||||
connRegistry.getConnectedContacts(p.plugin.getId());
|
||||
try {
|
||||
p.plugin.poll();
|
||||
p.plugin.poll(connected);
|
||||
} catch(RuntimeException e) {
|
||||
if(LOG.isLoggable(Level.WARNING))
|
||||
LOG.warning("Plugin " + p.plugin.getId() + " " + e);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.plugins.bluetooth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -178,18 +179,18 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
return pollingInterval;
|
||||
}
|
||||
|
||||
public void poll() {
|
||||
public void poll(final Collection<ContactId> connected) {
|
||||
synchronized(this) {
|
||||
if(!running) return;
|
||||
}
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
connectAndCallBack();
|
||||
connectAndCallBack(connected);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void connectAndCallBack() {
|
||||
private void connectAndCallBack(Collection<ContactId> connected) {
|
||||
synchronized(this) {
|
||||
if(!running) return;
|
||||
}
|
||||
@@ -198,6 +199,8 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
Map<ContactId, String> discovered = discoverContactUrls(remote);
|
||||
for(Entry<ContactId, String> e : discovered.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
// Don't create redundant connections
|
||||
if(connected.contains(c)) continue;
|
||||
String url = e.getValue();
|
||||
StreamTransportConnection s = connect(c, url);
|
||||
if(s != null) callback.outgoingConnectionCreated(c, s);
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.concurrent.Executor;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.plugins.BatchPluginCallback;
|
||||
import net.sf.briar.api.plugins.PluginExecutor;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
@@ -59,7 +60,7 @@ implements RemovableDriveMonitor.Callback {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void poll() {
|
||||
public void poll(Collection<ContactId> connected) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,26 +31,15 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(SimpleSocketPlugin.class.getName());
|
||||
|
||||
private final long pollingInterval;
|
||||
|
||||
SimpleSocketPlugin(@PluginExecutor Executor pluginExecutor,
|
||||
StreamPluginCallback callback, long pollingInterval) {
|
||||
super(pluginExecutor, callback);
|
||||
this.pollingInterval = pollingInterval;
|
||||
super(pluginExecutor, callback, pollingInterval);
|
||||
}
|
||||
|
||||
public TransportId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean shouldPoll() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public long getPollingInterval() {
|
||||
return pollingInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Socket createClientSocket() throws IOException {
|
||||
assert running;
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Level;
|
||||
@@ -24,6 +25,8 @@ abstract class SocketPlugin implements StreamPlugin {
|
||||
protected final Executor pluginExecutor;
|
||||
protected final StreamPluginCallback callback;
|
||||
|
||||
private final long pollingInterval;
|
||||
|
||||
protected boolean running = false; // Locking: this
|
||||
protected ServerSocket socket = null; // Locking: this
|
||||
|
||||
@@ -35,9 +38,10 @@ abstract class SocketPlugin implements StreamPlugin {
|
||||
protected abstract SocketAddress getRemoteSocketAddress(ContactId c);
|
||||
|
||||
protected SocketPlugin(@PluginExecutor Executor pluginExecutor,
|
||||
StreamPluginCallback callback) {
|
||||
StreamPluginCallback callback, long pollingInterval) {
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.callback = callback;
|
||||
this.pollingInterval = pollingInterval;
|
||||
}
|
||||
|
||||
public void start() throws IOException {
|
||||
@@ -115,13 +119,22 @@ abstract class SocketPlugin implements StreamPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public void poll() {
|
||||
public boolean shouldPoll() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public long getPollingInterval() {
|
||||
return pollingInterval;
|
||||
}
|
||||
|
||||
public void poll(Collection<ContactId> connected) {
|
||||
synchronized(this) {
|
||||
if(!running) return;
|
||||
}
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
callback.getRemoteProperties();
|
||||
for(final ContactId c : remote.keySet()) {
|
||||
if(connected.contains(c)) continue;
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
connectAndCallBack(c);
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseExecutor;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.protocol.batch.BatchConnectionFactory;
|
||||
@@ -14,6 +15,7 @@ import net.sf.briar.api.transport.BatchTransportReader;
|
||||
import net.sf.briar.api.transport.BatchTransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
@@ -22,6 +24,7 @@ class BatchConnectionFactoryImpl implements BatchConnectionFactory {
|
||||
|
||||
private final Executor dbExecutor, verificationExecutor;
|
||||
private final DatabaseComponent db;
|
||||
private final ConnectionRegistry connRegistry;
|
||||
private final ConnectionReaderFactory connReaderFactory;
|
||||
private final ConnectionWriterFactory connWriterFactory;
|
||||
private final ProtocolReaderFactory protoReaderFactory;
|
||||
@@ -30,24 +33,26 @@ class BatchConnectionFactoryImpl implements BatchConnectionFactory {
|
||||
@Inject
|
||||
BatchConnectionFactoryImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connReaderFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connReaderFactory,
|
||||
ConnectionWriterFactory connWriterFactory,
|
||||
ProtocolReaderFactory protoReaderFactory,
|
||||
ProtocolWriterFactory protoWriterFactory) {
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.verificationExecutor = verificationExecutor;
|
||||
this.db = db;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connReaderFactory = connReaderFactory;
|
||||
this.connWriterFactory = connWriterFactory;
|
||||
this.protoReaderFactory = protoReaderFactory;
|
||||
this.protoWriterFactory = protoWriterFactory;
|
||||
}
|
||||
|
||||
public void createIncomingConnection(ConnectionContext ctx,
|
||||
public void createIncomingConnection(ConnectionContext ctx, TransportId t,
|
||||
BatchTransportReader r, byte[] tag) {
|
||||
final IncomingBatchConnection conn = new IncomingBatchConnection(
|
||||
dbExecutor, verificationExecutor, db, connReaderFactory,
|
||||
protoReaderFactory, ctx, r, tag);
|
||||
dbExecutor, verificationExecutor, db, connRegistry,
|
||||
connReaderFactory, protoReaderFactory, ctx, t, r, tag);
|
||||
Runnable read = new Runnable() {
|
||||
public void run() {
|
||||
conn.read();
|
||||
@@ -56,10 +61,11 @@ class BatchConnectionFactoryImpl implements BatchConnectionFactory {
|
||||
new Thread(read).start();
|
||||
}
|
||||
|
||||
public void createOutgoingConnection(ContactId c, TransportIndex i,
|
||||
BatchTransportWriter w) {
|
||||
public void createOutgoingConnection(ContactId c, TransportId t,
|
||||
TransportIndex i, BatchTransportWriter w) {
|
||||
final OutgoingBatchConnection conn = new OutgoingBatchConnection(db,
|
||||
connWriterFactory, protoWriterFactory, c, i, w);
|
||||
connRegistry, connWriterFactory, protoWriterFactory,
|
||||
c, t, i, w);
|
||||
Runnable write = new Runnable() {
|
||||
public void run() {
|
||||
conn.write();
|
||||
|
||||
@@ -17,6 +17,7 @@ import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.ProtocolReader;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
@@ -24,6 +25,7 @@ import net.sf.briar.api.transport.BatchTransportReader;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
|
||||
class IncomingBatchConnection {
|
||||
|
||||
@@ -31,31 +33,38 @@ class IncomingBatchConnection {
|
||||
Logger.getLogger(IncomingBatchConnection.class.getName());
|
||||
|
||||
private final Executor dbExecutor, verificationExecutor;
|
||||
private final ConnectionReaderFactory connFactory;
|
||||
private final DatabaseComponent db;
|
||||
private final ConnectionRegistry connRegistry;
|
||||
private final ConnectionReaderFactory connFactory;
|
||||
private final ProtocolReaderFactory protoFactory;
|
||||
private final ConnectionContext ctx;
|
||||
private final TransportId transportId;
|
||||
private final BatchTransportReader transport;
|
||||
private final byte[] tag;
|
||||
private final ContactId contactId;
|
||||
|
||||
IncomingBatchConnection(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connFactory,
|
||||
ProtocolReaderFactory protoFactory, ConnectionContext ctx,
|
||||
BatchTransportReader transport, byte[] tag) {
|
||||
TransportId transportId, BatchTransportReader transport,
|
||||
byte[] tag) {
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.verificationExecutor = verificationExecutor;
|
||||
this.connFactory = connFactory;
|
||||
this.db = db;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connFactory = connFactory;
|
||||
this.protoFactory = protoFactory;
|
||||
this.ctx = ctx;
|
||||
this.transportId = transportId;
|
||||
this.transport = transport;
|
||||
this.tag = tag;
|
||||
contactId = ctx.getContactId();
|
||||
}
|
||||
|
||||
void read() {
|
||||
connRegistry.registerConnection(contactId, transportId);
|
||||
try {
|
||||
ConnectionReader conn = connFactory.createConnectionReader(
|
||||
transport.getInputStream(), ctx.getSecret(), tag);
|
||||
@@ -83,6 +92,8 @@ class IncomingBatchConnection {
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||
transport.dispose(true, true);
|
||||
} finally {
|
||||
connRegistry.unregisterConnection(contactId, transportId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,12 @@ import net.sf.briar.api.protocol.ProtocolWriter;
|
||||
import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.RawBatch;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.BatchTransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
|
||||
@@ -29,25 +31,32 @@ class OutgoingBatchConnection {
|
||||
Logger.getLogger(OutgoingBatchConnection.class.getName());
|
||||
|
||||
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 BatchTransportWriter transport;
|
||||
|
||||
OutgoingBatchConnection(DatabaseComponent db,
|
||||
ConnectionRegistry connRegistry,
|
||||
ConnectionWriterFactory connFactory,
|
||||
ProtocolWriterFactory protoFactory, ContactId contactId,
|
||||
TransportIndex transportIndex, BatchTransportWriter transport) {
|
||||
TransportId transportId, TransportIndex transportIndex,
|
||||
BatchTransportWriter transport) {
|
||||
this.db = db;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connFactory = connFactory;
|
||||
this.protoFactory = protoFactory;
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
this.transportIndex = transportIndex;
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
void write() {
|
||||
connRegistry.registerConnection(contactId, transportId);
|
||||
try {
|
||||
ConnectionContext ctx = db.getConnectionContext(contactId,
|
||||
transportIndex);
|
||||
@@ -97,6 +106,8 @@ class OutgoingBatchConnection {
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||
transport.dispose(true);
|
||||
} finally {
|
||||
connRegistry.unregisterConnection(contactId, transportId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseExecutor;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
@@ -22,15 +24,16 @@ class IncomingStreamConnection extends StreamConnection {
|
||||
|
||||
IncomingStreamConnection(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connReaderFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connReaderFactory,
|
||||
ConnectionWriterFactory connWriterFactory,
|
||||
ProtocolReaderFactory protoReaderFactory,
|
||||
ProtocolWriterFactory protoWriterFactory,
|
||||
ConnectionContext ctx, StreamTransportConnection transport,
|
||||
byte[] tag) {
|
||||
super(dbExecutor, verificationExecutor, db, connReaderFactory,
|
||||
connWriterFactory, protoReaderFactory, protoWriterFactory,
|
||||
ctx.getContactId(), transport);
|
||||
ConnectionContext ctx, TransportId transportId,
|
||||
StreamTransportConnection transport, byte[] tag) {
|
||||
super(dbExecutor, verificationExecutor, db, connRegistry,
|
||||
connReaderFactory, connWriterFactory, protoReaderFactory,
|
||||
protoWriterFactory, ctx.getContactId(), transportId, transport);
|
||||
this.ctx = ctx;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
@@ -9,11 +9,13 @@ import net.sf.briar.api.db.DatabaseExecutor;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
@@ -26,15 +28,16 @@ class OutgoingStreamConnection extends StreamConnection {
|
||||
|
||||
OutgoingStreamConnection(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connReaderFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connReaderFactory,
|
||||
ConnectionWriterFactory connWriterFactory,
|
||||
ProtocolReaderFactory protoReaderFactory,
|
||||
ProtocolWriterFactory protoWriterFactory, ContactId contactId,
|
||||
TransportIndex transportIndex,
|
||||
TransportId transportId, TransportIndex transportIndex,
|
||||
StreamTransportConnection transport) {
|
||||
super(dbExecutor, verificationExecutor, db, connReaderFactory,
|
||||
connWriterFactory, protoReaderFactory, protoWriterFactory,
|
||||
contactId, transport);
|
||||
super(dbExecutor, verificationExecutor, db, connRegistry,
|
||||
connReaderFactory, connWriterFactory, protoReaderFactory,
|
||||
protoWriterFactory, contactId, transportId, transport);
|
||||
this.transportIndex = transportIndex;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,11 +40,13 @@ import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.RawBatch;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
@@ -59,11 +61,13 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
};
|
||||
|
||||
protected final DatabaseComponent db;
|
||||
protected final ConnectionRegistry connRegistry;
|
||||
protected final ConnectionReaderFactory connReaderFactory;
|
||||
protected final ConnectionWriterFactory connWriterFactory;
|
||||
protected final ProtocolReaderFactory protoReaderFactory;
|
||||
protected final ProtocolWriterFactory protoWriterFactory;
|
||||
protected final ContactId contactId;
|
||||
protected final TransportId transportId;
|
||||
protected final StreamTransportConnection transport;
|
||||
|
||||
private final Executor dbExecutor, verificationExecutor;
|
||||
@@ -76,19 +80,22 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
|
||||
StreamConnection(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connReaderFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connReaderFactory,
|
||||
ConnectionWriterFactory connWriterFactory,
|
||||
ProtocolReaderFactory protoReaderFactory,
|
||||
ProtocolWriterFactory protoWriterFactory, ContactId contactId,
|
||||
StreamTransportConnection transport) {
|
||||
TransportId transportId, StreamTransportConnection transport) {
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.verificationExecutor = verificationExecutor;
|
||||
this.db = db;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connReaderFactory = connReaderFactory;
|
||||
this.connWriterFactory = connWriterFactory;
|
||||
this.protoReaderFactory = protoReaderFactory;
|
||||
this.protoWriterFactory = protoWriterFactory;
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
this.transport = transport;
|
||||
canSendOffer = new AtomicBoolean(false);
|
||||
disposed = new AtomicBoolean(false);
|
||||
@@ -188,8 +195,9 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
}
|
||||
|
||||
void write() {
|
||||
connRegistry.registerConnection(contactId, transportId);
|
||||
db.addListener(this);
|
||||
try {
|
||||
db.addListener(this);
|
||||
OutputStream out = createConnectionWriter().getOutputStream();
|
||||
writer = protoWriterFactory.createProtocolWriter(out,
|
||||
transport.shouldFlush());
|
||||
@@ -217,6 +225,7 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||
if(!disposed.getAndSet(true)) transport.dispose(true, true);
|
||||
} finally {
|
||||
connRegistry.unregisterConnection(contactId, transportId);
|
||||
db.removeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseExecutor;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.protocol.stream.StreamConnectionFactory;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
|
||||
@@ -21,6 +23,7 @@ class StreamConnectionFactoryImpl implements StreamConnectionFactory {
|
||||
|
||||
private final Executor dbExecutor, verificationExecutor;
|
||||
private final DatabaseComponent db;
|
||||
private final ConnectionRegistry connRegistry;
|
||||
private final ConnectionReaderFactory connReaderFactory;
|
||||
private final ConnectionWriterFactory connWriterFactory;
|
||||
private final ProtocolReaderFactory protoReaderFactory;
|
||||
@@ -29,24 +32,27 @@ class StreamConnectionFactoryImpl implements StreamConnectionFactory {
|
||||
@Inject
|
||||
StreamConnectionFactoryImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
@VerificationExecutor Executor verificationExecutor,
|
||||
DatabaseComponent db, ConnectionReaderFactory connReaderFactory,
|
||||
DatabaseComponent db, ConnectionRegistry connRegistry,
|
||||
ConnectionReaderFactory connReaderFactory,
|
||||
ConnectionWriterFactory connWriterFactory,
|
||||
ProtocolReaderFactory protoReaderFactory,
|
||||
ProtocolWriterFactory protoWriterFactory) {
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.verificationExecutor = verificationExecutor;
|
||||
this.db = db;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connReaderFactory = connReaderFactory;
|
||||
this.connWriterFactory = connWriterFactory;
|
||||
this.protoReaderFactory = protoReaderFactory;
|
||||
this.protoWriterFactory = protoWriterFactory;
|
||||
}
|
||||
|
||||
public void createIncomingConnection(ConnectionContext ctx,
|
||||
public void createIncomingConnection(ConnectionContext ctx, TransportId t,
|
||||
StreamTransportConnection s, byte[] tag) {
|
||||
final StreamConnection conn = new IncomingStreamConnection(dbExecutor,
|
||||
verificationExecutor, db, connReaderFactory, connWriterFactory,
|
||||
protoReaderFactory, protoWriterFactory, ctx, s, tag);
|
||||
verificationExecutor, db, connRegistry, connReaderFactory,
|
||||
connWriterFactory, protoReaderFactory, protoWriterFactory,
|
||||
ctx, t, s, tag);
|
||||
Runnable write = new Runnable() {
|
||||
public void run() {
|
||||
conn.write();
|
||||
@@ -61,11 +67,12 @@ class StreamConnectionFactoryImpl implements StreamConnectionFactory {
|
||||
new Thread(read).start();
|
||||
}
|
||||
|
||||
public void createOutgoingConnection(ContactId c, TransportIndex i,
|
||||
StreamTransportConnection s) {
|
||||
public void createOutgoingConnection(ContactId c, TransportId t,
|
||||
TransportIndex i, StreamTransportConnection s) {
|
||||
final StreamConnection conn = new OutgoingStreamConnection(dbExecutor,
|
||||
verificationExecutor, db, connReaderFactory, connWriterFactory,
|
||||
protoReaderFactory, protoWriterFactory, c, i, s);
|
||||
verificationExecutor, db, connRegistry, connReaderFactory,
|
||||
connWriterFactory, protoReaderFactory, protoWriterFactory,
|
||||
c, t, i, s);
|
||||
Runnable write = new Runnable() {
|
||||
public void run() {
|
||||
conn.write();
|
||||
|
||||
@@ -49,9 +49,9 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
executor.execute(new DispatchBatchConnection(t, r));
|
||||
}
|
||||
|
||||
public void dispatchWriter(ContactId c, TransportIndex i,
|
||||
public void dispatchWriter(ContactId c, TransportId t, TransportIndex i,
|
||||
BatchTransportWriter w) {
|
||||
batchConnFactory.createOutgoingConnection(c, i, w);
|
||||
batchConnFactory.createOutgoingConnection(c, t, i, w);
|
||||
}
|
||||
|
||||
public void dispatchIncomingConnection(TransportId t,
|
||||
@@ -59,9 +59,9 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
executor.execute(new DispatchStreamConnection(t, s));
|
||||
}
|
||||
|
||||
public void dispatchOutgoingConnection(ContactId c, TransportIndex i,
|
||||
StreamTransportConnection s) {
|
||||
streamConnFactory.createOutgoingConnection(c, i, s);
|
||||
public void dispatchOutgoingConnection(ContactId c, TransportId t,
|
||||
TransportIndex i, StreamTransportConnection s) {
|
||||
streamConnFactory.createOutgoingConnection(c, t, i, s);
|
||||
}
|
||||
|
||||
private byte[] readTag(InputStream in) throws IOException {
|
||||
@@ -92,8 +92,8 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
ConnectionContext ctx = recogniser.acceptConnection(transportId,
|
||||
tag);
|
||||
if(ctx == null) transport.dispose(false, false);
|
||||
else batchConnFactory.createIncomingConnection(ctx, transport,
|
||||
tag);
|
||||
else batchConnFactory.createIncomingConnection(ctx, transportId,
|
||||
transport, tag);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||
transport.dispose(true, false);
|
||||
@@ -121,8 +121,8 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
ConnectionContext ctx = recogniser.acceptConnection(transportId,
|
||||
tag);
|
||||
if(ctx == null) transport.dispose(false, false);
|
||||
else streamConnFactory.createIncomingConnection(ctx, transport,
|
||||
tag);
|
||||
else streamConnFactory.createIncomingConnection(ctx,
|
||||
transportId, transport, tag);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||
transport.dispose(true, false);
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
|
||||
public class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
|
||||
// Locking: this
|
||||
private final Map<TransportId, Map<ContactId, Integer>> connections;
|
||||
|
||||
ConnectionRegistryImpl() {
|
||||
connections = new HashMap<TransportId, Map<ContactId, Integer>>();
|
||||
}
|
||||
|
||||
public synchronized void registerConnection(ContactId c, TransportId t) {
|
||||
Map<ContactId, Integer> m = connections.get(t);
|
||||
if(m == null) {
|
||||
m = new HashMap<ContactId, Integer>();
|
||||
connections.put(t, m);
|
||||
}
|
||||
Integer count = m.get(c);
|
||||
if(count == null) m.put(c, 1);
|
||||
else m.put(c, count + 1);
|
||||
}
|
||||
|
||||
public synchronized void unregisterConnection(ContactId c, TransportId t) {
|
||||
Map<ContactId, Integer> m = connections.get(t);
|
||||
if(m == null) throw new IllegalArgumentException();
|
||||
Integer count = m.remove(c);
|
||||
if(count == null) throw new IllegalArgumentException();
|
||||
if(count == 1) {
|
||||
if(m.isEmpty()) connections.remove(t);
|
||||
} else {
|
||||
m.put(c, count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Collection<ContactId> getConnectedContacts(
|
||||
TransportId t) {
|
||||
Map<ContactId, Integer> m = connections.get(t);
|
||||
if(m == null) return Collections.emptyList();
|
||||
List<ContactId> keys = new ArrayList<ContactId>(m.keySet());
|
||||
return Collections.unmodifiableList(keys);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import net.sf.briar.api.transport.ConnectionDispatcher;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniserExecutor;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
|
||||
@@ -23,6 +24,7 @@ public class TransportModule extends AbstractModule {
|
||||
bind(ConnectionReaderFactory.class).to(
|
||||
ConnectionReaderFactoryImpl.class);
|
||||
bind(ConnectionRecogniser.class).to(ConnectionRecogniserImpl.class);
|
||||
bind(ConnectionRegistry.class).toInstance(new ConnectionRegistryImpl());
|
||||
bind(ConnectionWindowFactory.class).to(
|
||||
ConnectionWindowFactoryImpl.class);
|
||||
bind(ConnectionWriterFactory.class).to(
|
||||
|
||||
@@ -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'/>
|
||||
|
||||
19
test/net/sf/briar/BriarTestCase.java
Normal file
19
test/net/sf/briar/BriarTestCase.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
73
test/net/sf/briar/transport/ConnectionRegistryImplTest.java
Normal file
73
test/net/sf/briar/transport/ConnectionRegistryImplTest.java
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user