mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Add unit tests for KeyManagerImpl and create TransportKeyManager
interface and a factory for that.
This commit is contained in:
@@ -3,7 +3,6 @@ package org.briarproject.transport;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
@@ -18,16 +17,16 @@ import org.briarproject.api.lifecycle.ServiceException;
|
||||
import org.briarproject.api.plugins.PluginConfig;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TransportKeyManager;
|
||||
import org.briarproject.api.transport.TransportKeyManagerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -41,26 +40,21 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
Logger.getLogger(KeyManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final Executor dbExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final PluginConfig pluginConfig;
|
||||
private final Clock clock;
|
||||
private final TransportKeyManagerFactory transportKeyManagerFactory;
|
||||
private final Map<ContactId, Boolean> activeContacts;
|
||||
private final ConcurrentHashMap<TransportId, TransportKeyManager> managers;
|
||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||
|
||||
@Inject
|
||||
KeyManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
ScheduledExecutorService scheduler, PluginConfig pluginConfig,
|
||||
Clock clock) {
|
||||
KeyManagerImpl(DatabaseComponent db, @DatabaseExecutor Executor dbExecutor,
|
||||
PluginConfig pluginConfig,
|
||||
TransportKeyManagerFactory transportKeyManagerFactory) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.scheduler = scheduler;
|
||||
this.pluginConfig = pluginConfig;
|
||||
this.clock = clock;
|
||||
this.transportKeyManagerFactory = transportKeyManagerFactory;
|
||||
// Use a ConcurrentHashMap as a thread-safe set
|
||||
activeContacts = new ConcurrentHashMap<ContactId, Boolean>();
|
||||
managers = new ConcurrentHashMap<TransportId, TransportKeyManager>();
|
||||
@@ -83,9 +77,9 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
for (Entry<TransportId, Integer> e : transports.entrySet())
|
||||
db.addTransport(txn, e.getKey(), e.getValue());
|
||||
for (Entry<TransportId, Integer> e : transports.entrySet()) {
|
||||
TransportKeyManager m = new TransportKeyManager(db, crypto,
|
||||
dbExecutor, scheduler, clock, e.getKey(),
|
||||
e.getValue());
|
||||
TransportKeyManager m = transportKeyManagerFactory
|
||||
.createTransportKeyManager(e.getKey(),
|
||||
e.getValue());
|
||||
managers.put(e.getKey(), m);
|
||||
m.start(txn);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.TransportKeyManager;
|
||||
import org.briarproject.api.transport.TransportKeyManagerFactory;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class TransportKeyManagerFactoryImpl implements
|
||||
TransportKeyManagerFactory {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final Executor dbExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
TransportKeyManagerFactoryImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
ScheduledExecutorService scheduler, Clock clock) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.scheduler = scheduler;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportKeyManager createTransportKeyManager(
|
||||
TransportId transportId, long maxLatency) {
|
||||
return new TransportKeyManagerImpl(db, crypto, dbExecutor, scheduler,
|
||||
clock, transportId, maxLatency);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.StreamContext;
|
||||
import org.briarproject.api.transport.TransportKeyManager;
|
||||
import org.briarproject.api.transport.TransportKeys;
|
||||
import org.briarproject.transport.ReorderingWindow.Change;
|
||||
|
||||
@@ -28,10 +29,10 @@ import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFER
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
class TransportKeyManager {
|
||||
class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(TransportKeyManager.class.getName());
|
||||
Logger.getLogger(TransportKeyManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
@@ -47,7 +48,7 @@ class TransportKeyManager {
|
||||
private final Map<ContactId, MutableOutgoingKeys> outContexts;
|
||||
private final Map<ContactId, MutableTransportKeys> keys;
|
||||
|
||||
TransportKeyManager(DatabaseComponent db, CryptoComponent crypto,
|
||||
TransportKeyManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
Executor dbExecutor, ScheduledExecutorService scheduler,
|
||||
Clock clock, TransportId transportId, long maxLatency) {
|
||||
this.db = db;
|
||||
@@ -63,7 +64,7 @@ class TransportKeyManager {
|
||||
keys = new HashMap<ContactId, MutableTransportKeys>();
|
||||
}
|
||||
|
||||
void start(Transaction txn) throws DbException {
|
||||
public void start(Transaction txn) throws DbException {
|
||||
long now = clock.currentTimeMillis();
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -155,7 +156,7 @@ class TransportKeyManager {
|
||||
});
|
||||
}
|
||||
|
||||
void addContact(Transaction txn, ContactId c, SecretKey master,
|
||||
public void addContact(Transaction txn, ContactId c, SecretKey master,
|
||||
long timestamp, boolean alice) throws DbException {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -176,7 +177,7 @@ class TransportKeyManager {
|
||||
}
|
||||
}
|
||||
|
||||
void removeContact(ContactId c) {
|
||||
public void removeContact(ContactId c) {
|
||||
lock.lock();
|
||||
try {
|
||||
// Remove mutable state for the contact
|
||||
@@ -191,7 +192,7 @@ class TransportKeyManager {
|
||||
}
|
||||
}
|
||||
|
||||
StreamContext getStreamContext(Transaction txn, ContactId c)
|
||||
public StreamContext getStreamContext(Transaction txn, ContactId c)
|
||||
throws DbException {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -213,7 +214,7 @@ class TransportKeyManager {
|
||||
}
|
||||
}
|
||||
|
||||
StreamContext getStreamContext(Transaction txn, byte[] tag)
|
||||
public StreamContext getStreamContext(Transaction txn, byte[] tag)
|
||||
throws DbException {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.transport.KeyManager;
|
||||
import org.briarproject.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.api.transport.TransportKeyManagerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -34,6 +35,12 @@ public class TransportModule {
|
||||
return new StreamWriterFactoryImpl(streamEncrypterFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
TransportKeyManagerFactory provideTransportKeyManagerFactory(
|
||||
TransportKeyManagerFactoryImpl transportKeyManagerFactory) {
|
||||
return transportKeyManagerFactory;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
KeyManager provideKeyManager(LifecycleManager lifecycleManager,
|
||||
|
||||
Reference in New Issue
Block a user