mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Address review comments for TransportKeyManagerImplTest
This commit is contained in:
@@ -19,8 +19,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
|||||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||||
import org.briarproject.api.transport.KeyManager;
|
import org.briarproject.api.transport.KeyManager;
|
||||||
import org.briarproject.api.transport.StreamContext;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package org.briarproject.api.transport;
|
package org.briarproject.transport;
|
||||||
|
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.crypto.SecretKey;
|
import org.briarproject.api.crypto.SecretKey;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.db.Transaction;
|
import org.briarproject.api.db.Transaction;
|
||||||
|
import org.briarproject.api.transport.StreamContext;
|
||||||
|
|
||||||
public interface TransportKeyManager {
|
interface TransportKeyManager {
|
||||||
|
|
||||||
void start(Transaction txn) throws DbException;
|
void start(Transaction txn) throws DbException;
|
||||||
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package org.briarproject.api.transport;
|
package org.briarproject.transport;
|
||||||
|
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
|
|
||||||
public interface TransportKeyManagerFactory {
|
interface TransportKeyManagerFactory {
|
||||||
|
|
||||||
TransportKeyManager createTransportKeyManager(TransportId transportId,
|
TransportKeyManager createTransportKeyManager(TransportId transportId,
|
||||||
long maxLatency);
|
long maxLatency);
|
||||||
@@ -5,15 +5,13 @@ import org.briarproject.api.crypto.CryptoComponent;
|
|||||||
import org.briarproject.api.db.DatabaseComponent;
|
import org.briarproject.api.db.DatabaseComponent;
|
||||||
import org.briarproject.api.db.DatabaseExecutor;
|
import org.briarproject.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.api.system.Clock;
|
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.Executor;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
public class TransportKeyManagerFactoryImpl implements
|
class TransportKeyManagerFactoryImpl implements
|
||||||
TransportKeyManagerFactory {
|
TransportKeyManagerFactory {
|
||||||
|
|
||||||
private final DatabaseComponent db;
|
private final DatabaseComponent db;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import org.briarproject.api.db.DbException;
|
|||||||
import org.briarproject.api.db.Transaction;
|
import org.briarproject.api.db.Transaction;
|
||||||
import org.briarproject.api.system.Clock;
|
import org.briarproject.api.system.Clock;
|
||||||
import org.briarproject.api.transport.StreamContext;
|
import org.briarproject.api.transport.StreamContext;
|
||||||
import org.briarproject.api.transport.TransportKeyManager;
|
|
||||||
import org.briarproject.api.transport.TransportKeys;
|
import org.briarproject.api.transport.TransportKeys;
|
||||||
import org.briarproject.transport.ReorderingWindow.Change;
|
import org.briarproject.transport.ReorderingWindow.Change;
|
||||||
|
|
||||||
@@ -64,6 +63,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
|||||||
keys = new HashMap<ContactId, MutableTransportKeys>();
|
keys = new HashMap<ContactId, MutableTransportKeys>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void start(Transaction txn) throws DbException {
|
public void start(Transaction txn) throws DbException {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -156,6 +156,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void addContact(Transaction txn, ContactId c, SecretKey master,
|
public void addContact(Transaction txn, ContactId c, SecretKey master,
|
||||||
long timestamp, boolean alice) throws DbException {
|
long timestamp, boolean alice) throws DbException {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -177,6 +178,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void removeContact(ContactId c) {
|
public void removeContact(ContactId c) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
@@ -192,6 +194,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public StreamContext getStreamContext(Transaction txn, ContactId c)
|
public StreamContext getStreamContext(Transaction txn, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -214,6 +217,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public StreamContext getStreamContext(Transaction txn, byte[] tag)
|
public StreamContext getStreamContext(Transaction txn, byte[] tag)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import org.briarproject.api.lifecycle.LifecycleManager;
|
|||||||
import org.briarproject.api.transport.KeyManager;
|
import org.briarproject.api.transport.KeyManager;
|
||||||
import org.briarproject.api.transport.StreamReaderFactory;
|
import org.briarproject.api.transport.StreamReaderFactory;
|
||||||
import org.briarproject.api.transport.StreamWriterFactory;
|
import org.briarproject.api.transport.StreamWriterFactory;
|
||||||
import org.briarproject.api.transport.TransportKeyManagerFactory;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ import org.briarproject.api.identity.AuthorId;
|
|||||||
import org.briarproject.api.plugins.PluginConfig;
|
import org.briarproject.api.plugins.PluginConfig;
|
||||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||||
import org.briarproject.api.transport.StreamContext;
|
import org.briarproject.api.transport.StreamContext;
|
||||||
import org.briarproject.api.transport.TransportKeyManager;
|
|
||||||
import org.briarproject.api.transport.TransportKeyManagerFactory;
|
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.jmock.Mockery;
|
import org.jmock.Mockery;
|
||||||
import org.jmock.lib.concurrent.DeterministicExecutor;
|
import org.jmock.lib.concurrent.DeterministicExecutor;
|
||||||
@@ -29,6 +27,7 @@ import java.util.Collections;
|
|||||||
import static org.briarproject.TestUtils.getRandomBytes;
|
import static org.briarproject.TestUtils.getRandomBytes;
|
||||||
import static org.briarproject.TestUtils.getRandomId;
|
import static org.briarproject.TestUtils.getRandomId;
|
||||||
import static org.briarproject.TestUtils.getSecretKey;
|
import static org.briarproject.TestUtils.getSecretKey;
|
||||||
|
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class KeyManagerImplTest extends BriarTestCase {
|
public class KeyManagerImplTest extends BriarTestCase {
|
||||||
@@ -46,10 +45,11 @@ public class KeyManagerImplTest extends BriarTestCase {
|
|||||||
private final ContactId contactId = new ContactId(42);
|
private final ContactId contactId = new ContactId(42);
|
||||||
private final ContactId inactiveContactId = new ContactId(43);
|
private final ContactId inactiveContactId = new ContactId(43);
|
||||||
private final TransportId transportId = new TransportId("tId");
|
private final TransportId transportId = new TransportId("tId");
|
||||||
|
private final TransportId unknownTransportId = new TransportId("id");
|
||||||
private final StreamContext streamContext =
|
private final StreamContext streamContext =
|
||||||
new StreamContext(contactId, transportId, getSecretKey(),
|
new StreamContext(contactId, transportId, getSecretKey(),
|
||||||
getSecretKey(), 1);
|
getSecretKey(), 1);
|
||||||
private final byte[] tag = getRandomBytes(42);
|
private final byte[] tag = getRandomBytes(TAG_LENGTH);
|
||||||
|
|
||||||
public KeyManagerImplTest() {
|
public KeyManagerImplTest() {
|
||||||
keyManager = new KeyManagerImpl(db, executor, pluginConfig,
|
keyManager = new KeyManagerImpl(db, executor, pluginConfig,
|
||||||
@@ -114,12 +114,19 @@ public class KeyManagerImplTest extends BriarTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStreamContextForContact() throws Exception {
|
public void testGetStreamContextForInactiveContact() throws Exception {
|
||||||
assertEquals(null,
|
assertEquals(null,
|
||||||
keyManager.getStreamContext(inactiveContactId, transportId));
|
keyManager.getStreamContext(inactiveContactId, transportId));
|
||||||
assertEquals(null, keyManager
|
}
|
||||||
.getStreamContext(inactiveContactId, new TransportId("id")));
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStreamContextForUnknownTransport() throws Exception {
|
||||||
|
assertEquals(null, keyManager
|
||||||
|
.getStreamContext(contactId, unknownTransportId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStreamContextForContact() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(db).startTransaction(false);
|
oneOf(db).startTransaction(false);
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
@@ -135,10 +142,14 @@ public class KeyManagerImplTest extends BriarTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStreamContextForTag() throws Exception {
|
public void testGetStreamContextForTagAndUnknownTransport()
|
||||||
assertEquals(null, keyManager
|
throws Exception {
|
||||||
.getStreamContext(new TransportId("id"), tag));
|
assertEquals(null,
|
||||||
|
keyManager.getStreamContext(unknownTransportId, tag));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStreamContextForTag() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(db).startTransaction(false);
|
oneOf(db).startTransaction(false);
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import org.briarproject.api.system.Clock;
|
|||||||
import org.briarproject.api.transport.IncomingKeys;
|
import org.briarproject.api.transport.IncomingKeys;
|
||||||
import org.briarproject.api.transport.OutgoingKeys;
|
import org.briarproject.api.transport.OutgoingKeys;
|
||||||
import org.briarproject.api.transport.StreamContext;
|
import org.briarproject.api.transport.StreamContext;
|
||||||
import org.briarproject.api.transport.TransportKeyManager;
|
|
||||||
import org.briarproject.api.transport.TransportKeys;
|
import org.briarproject.api.transport.TransportKeys;
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
@@ -41,7 +40,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
public class TransportKeyManagerTest extends BriarTestCase {
|
public class TransportKeyManagerImplTest extends BriarTestCase {
|
||||||
|
|
||||||
private final TransportId transportId = new TransportId("id");
|
private final TransportId transportId = new TransportId("id");
|
||||||
private final long maxLatency = 30 * 1000; // 30 seconds
|
private final long maxLatency = 30 * 1000; // 30 seconds
|
||||||
Reference in New Issue
Block a user