Type-safe transport IDs.

This commit is contained in:
akwizgran
2011-09-30 12:52:29 +01:00
parent 7190509ede
commit 72b594d270
32 changed files with 292 additions and 188 deletions

View File

@@ -14,6 +14,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Author;
@@ -66,14 +67,14 @@ public class ProtocolIntegrationTest extends TestCase {
private final ProtocolWriterFactory protocolWriterFactory;
private final CryptoComponent crypto;
private final byte[] aliceSecret, bobSecret;
private final int transportId = 123;
private final long connection = 234L;
private final TransportId transportId = new TransportId(123);
private final long connection = 12345L;
private final Author author;
private final Group group, group1;
private final Message message, message1, message2, message3;
private final String authorName = "Alice";
private final String messageBody = "Hello world";
private final Map<Integer, Map<String, String>> transports;
private final Map<TransportId, Map<String, String>> transports;
public ProtocolIntegrationTest() throws Exception {
super();

View File

@@ -10,6 +10,7 @@ import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseListener;
import net.sf.briar.api.db.DatabaseListener.Event;
@@ -53,7 +54,8 @@ public abstract class DatabaseComponentTest extends TestCase {
private final byte[] raw;
private final Message message, privateMessage;
private final Group group;
private final Map<Integer, Map<String, String>> transports;
private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports;
private final byte[] secret;
public DatabaseComponentTest() {
@@ -72,7 +74,8 @@ public abstract class DatabaseComponentTest extends TestCase {
privateMessage =
new TestMessage(messageId, null, null, null, timestamp, raw);
group = new TestGroup(groupId, "The really exciting group", null);
transports = Collections.singletonMap(123,
transportId = new TransportId(123);
transports = Collections.singletonMap(transportId,
Collections.singletonMap("bar", "baz"));
secret = new byte[123];
}
@@ -112,7 +115,7 @@ public abstract class DatabaseComponentTest extends TestCase {
// getConnectionWindow(contactId, 123)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getConnectionWindow(txn, contactId, 123);
oneOf(database).getConnectionWindow(txn, contactId, transportId);
will(returnValue(connectionWindow));
// getSharedSecret(contactId)
oneOf(database).containsContact(txn, contactId);
@@ -150,7 +153,7 @@ public abstract class DatabaseComponentTest extends TestCase {
// setConnectionWindow(contactId, 123, connectionWindow)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).setConnectionWindow(txn, contactId, 123,
oneOf(database).setConnectionWindow(txn, contactId, transportId,
connectionWindow);
// removeContact(contactId)
oneOf(database).removeContact(txn, contactId);
@@ -166,7 +169,8 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(Rating.UNRATED, db.getRating(authorId));
assertEquals(contactId, db.addContact(transports, secret));
assertEquals(Collections.singletonList(contactId), db.getContacts());
assertEquals(connectionWindow, db.getConnectionWindow(contactId, 123));
assertEquals(connectionWindow,
db.getConnectionWindow(contactId, transportId));
assertEquals(secret, db.getSharedSecret(contactId));
assertEquals(transports, db.getTransports(contactId));
db.subscribe(group); // First time - check listeners are called
@@ -174,7 +178,7 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
db.unsubscribe(groupId); // First time - check listeners are called
db.unsubscribe(groupId); // Second time - check listeners aren't called
db.setConnectionWindow(contactId, 123, connectionWindow);
db.setConnectionWindow(contactId, transportId, connectionWindow);
db.removeContact(contactId);
db.removeListener(listener);
db.close();
@@ -510,7 +514,7 @@ public abstract class DatabaseComponentTest extends TestCase {
} catch(NoSuchContactException expected) {}
try {
db.getConnectionWindow(contactId, 123);
db.getConnectionWindow(contactId, transportId);
fail();
} catch(NoSuchContactException expected) {}
@@ -555,7 +559,7 @@ public abstract class DatabaseComponentTest extends TestCase {
} catch(NoSuchContactException expected) {}
try {
db.setConnectionWindow(contactId, 123, null);
db.setConnectionWindow(contactId, transportId, null);
fail();
} catch(NoSuchContactException expected) {}
@@ -1284,15 +1288,17 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap(123, properties)));
oneOf(database).setTransportProperties(txn, 123, properties1);
will(returnValue(Collections.singletonMap(transportId,
properties)));
oneOf(database).setTransportProperties(txn, transportId,
properties1);
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportProperties(123, properties1);
db.setTransportProperties(transportId, properties1);
context.assertIsSatisfied();
}
@@ -1311,13 +1317,14 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap(123, properties)));
will(returnValue(Collections.singletonMap(transportId,
properties)));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportProperties(123, properties);
db.setTransportProperties(transportId, properties);
context.assertIsSatisfied();
}
@@ -1336,16 +1343,16 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransportConfig(txn, 123);
oneOf(database).getTransportConfig(txn, transportId);
will(returnValue(config));
oneOf(database).setTransportConfig(txn, 123, config1);
oneOf(database).setTransportConfig(txn, transportId, config1);
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportConfig(123, config1);
db.setTransportConfig(transportId, config1);
context.assertIsSatisfied();
}
@@ -1363,14 +1370,14 @@ public abstract class DatabaseComponentTest extends TestCase {
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransportConfig(txn, 123);
oneOf(database).getTransportConfig(txn, transportId);
will(returnValue(config));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransportConfig(123, config);
db.setTransportConfig(transportId, config);
context.assertIsSatisfied();
}

View File

@@ -17,6 +17,7 @@ import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.Status;
@@ -65,7 +66,8 @@ public class H2DatabaseTest extends TestCase {
private final byte[] raw;
private final Message message, privateMessage;
private final Group group;
private final Map<Integer, Map<String, String>> transports;
private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports;
private final Map<Group, Long> subscriptions;
private final byte[] secret;
@@ -91,7 +93,8 @@ public class H2DatabaseTest extends TestCase {
privateMessage =
new TestMessage(privateMessageId, null, null, null, timestamp, raw);
group = groupFactory.createGroup(groupId, "Group name", null);
transports = Collections.singletonMap(123,
transportId = new TransportId(0);
transports = Collections.singletonMap(transportId,
Collections.singletonMap("bar", "baz"));
subscriptions = Collections.singletonMap(group, 0L);
secret = new byte[123];
@@ -991,28 +994,29 @@ public class H2DatabaseTest extends TestCase {
assertEquals(transports, db.getTransports(txn, contactId));
// Replace the transport properties
Map<Integer, Map<String, String>> transports1 =
new TreeMap<Integer, Map<String, String>>();
transports1.put(123, Collections.singletonMap("bar", "baz"));
transports1.put(456, Collections.singletonMap("baz", "quux"));
TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 =
new TreeMap<TransportId, Map<String, String>>();
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
db.setTransports(txn, contactId, transports1, 1);
assertEquals(transports1, db.getTransports(txn, contactId));
// Remove the transport properties
db.setTransports(txn, contactId,
Collections.<Integer, Map<String, String>>emptyMap(), 2);
Collections.<TransportId, Map<String, String>>emptyMap(), 2);
assertEquals(Collections.emptyMap(), db.getTransports(txn, contactId));
// Set the local transport properties
for(Integer transportId : transports.keySet()) {
Map<String, String> properties = transports.get(transportId);
for(TransportId t : transports.keySet()) {
Map<String, String> properties = transports.get(t);
db.setTransportProperties(txn, transportId, properties);
}
assertEquals(transports, db.getTransports(txn));
// Remove the local transport properties
for(Integer transportId : transports.keySet()) {
db.setTransportProperties(txn, transportId,
for(TransportId t : transports.keySet()) {
db.setTransportProperties(txn, t,
Collections.<String, String>emptyMap());
}
assertEquals(Collections.emptyMap(), db.getTransports(txn));
@@ -1030,17 +1034,18 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
// Set the transport config
db.setTransportConfig(txn, 123, config);
assertEquals(config, db.getTransportConfig(txn, 123));
db.setTransportConfig(txn, transportId, config);
assertEquals(config, db.getTransportConfig(txn, transportId));
// Update the transport config
db.setTransportConfig(txn, 123, config1);
assertEquals(config1, db.getTransportConfig(txn, 123));
db.setTransportConfig(txn, transportId, config1);
assertEquals(config1, db.getTransportConfig(txn, transportId));
// Remove the transport config
db.setTransportConfig(txn, 123,
db.setTransportConfig(txn, transportId,
Collections.<String, String>emptyMap());
assertEquals(Collections.emptyMap(), db.getTransportConfig(txn, 123));
assertEquals(Collections.emptyMap(),
db.getTransportConfig(txn, transportId));
db.commitTransaction(txn);
db.close();
@@ -1056,18 +1061,20 @@ public class H2DatabaseTest extends TestCase {
assertEquals(transports, db.getTransports(txn, contactId));
// Replace the transport properties using a timestamp of 2
Map<Integer, Map<String, String>> transports1 =
new TreeMap<Integer, Map<String, String>>();
transports1.put(123, Collections.singletonMap("bar", "baz"));
transports1.put(456, Collections.singletonMap("baz", "quux"));
TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 =
new TreeMap<TransportId, Map<String, String>>();
transports1.put(transportId, Collections.singletonMap("bar", "baz"));
transports1.put(transportId1, Collections.singletonMap("baz", "quux"));
db.setTransports(txn, contactId, transports1, 2);
assertEquals(transports1, db.getTransports(txn, contactId));
// Try to replace the transport properties using a timestamp of 1
Map<Integer, Map<String, String>> transports2 =
new TreeMap<Integer, Map<String, String>>();
transports2.put(456, Collections.singletonMap("baz", "quux"));
transports2.put(789, Collections.singletonMap("quux", "fnord"));
TransportId transportId2 = new TransportId(2);
Map<TransportId, Map<String, String>> transports2 =
new TreeMap<TransportId, Map<String, String>>();
transports2.put(transportId1, Collections.singletonMap("baz", "quux"));
transports2.put(transportId2, Collections.singletonMap("quux", "etc"));
db.setTransports(txn, contactId, transports2, 1);
// The old properties should still be there
@@ -1395,7 +1402,8 @@ public class H2DatabaseTest extends TestCase {
// Add a contact
assertEquals(contactId, db.addContact(txn, transports, secret));
// Get the connection window for a new transport
ConnectionWindow w = db.getConnectionWindow(txn, contactId, 123);
ConnectionWindow w = db.getConnectionWindow(txn, contactId,
transportId);
// The connection window should exist and be in the initial state
assertNotNull(w);
assertEquals(0L, w.getCentre());
@@ -1413,16 +1421,17 @@ public class H2DatabaseTest extends TestCase {
// Add a contact
assertEquals(contactId, db.addContact(txn, transports, secret));
// Get the connection window for a new transport
ConnectionWindow w = db.getConnectionWindow(txn, contactId, 123);
ConnectionWindow w = db.getConnectionWindow(txn, contactId,
transportId);
// The connection window should exist and be in the initial state
assertNotNull(w);
assertEquals(0L, w.getCentre());
assertEquals(0, w.getBitmap());
// Update the connection window and store it
w.setSeen(5L);
db.setConnectionWindow(txn, contactId, 123, w);
db.setConnectionWindow(txn, contactId, transportId, w);
// Check that the connection window was stored
w = db.getConnectionWindow(txn, contactId, 123);
w = db.getConnectionWindow(txn, contactId, transportId);
assertNotNull(w);
assertEquals(6L, w.getCentre());
assertTrue(w.isSeen(5L));

View File

@@ -8,6 +8,7 @@ import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
@@ -47,7 +48,8 @@ public class ProtocolReadWriteTest extends TestCase {
private final String messageBody = "Hello world";
private final BitSet bitSet;
private final Map<Group, Long> subscriptions;
private final Map<Integer, Map<String, String>> transports;
private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports;
private final long timestamp = System.currentTimeMillis();
public ProtocolReadWriteTest() throws Exception {
@@ -67,7 +69,8 @@ public class ProtocolReadWriteTest extends TestCase {
bitSet.set(3);
bitSet.set(7);
subscriptions = Collections.singletonMap(group, 123L);
transports = Collections.singletonMap(123,
transportId = new TransportId(123);
transports = Collections.singletonMap(transportId,
Collections.singletonMap("bar", "baz"));
}

View File

@@ -8,6 +8,7 @@ import java.util.TreeMap;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorFactory;
@@ -190,8 +191,8 @@ public class ConstantsTest extends TestCase {
public void testTransportsFitIntoUpdate() throws Exception {
// Create the maximum number of plugins, each with the maximum number
// of maximum-length properties
Map<Integer, Map<String, String>> transports =
new TreeMap<Integer, Map<String, String>>();
Map<TransportId, Map<String, String>> transports =
new TreeMap<TransportId, Map<String, String>>();
for(int i = 0; i < TransportUpdate.MAX_PLUGINS_PER_UPDATE; i++) {
Map<String, String> properties = new TreeMap<String, String>();
for(int j = 0; j < TransportUpdate.MAX_PROPERTIES_PER_PLUGIN; j++) {
@@ -201,7 +202,7 @@ public class ConstantsTest extends TestCase {
TransportUpdate.MAX_KEY_OR_VALUE_LENGTH);
properties.put(key, value);
}
transports.put(i, properties);
transports.put(new TransportId(i), properties);
}
// Add the transports to an update
ByteArrayOutputStream out = new ByteArrayOutputStream(

View File

@@ -11,6 +11,7 @@ import javax.crypto.spec.IvParameterSpec;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.crypto.CryptoModule;
@@ -26,7 +27,7 @@ public class ConnectionDecrypterImplTest extends TestCase {
private final Cipher ivCipher, frameCipher;
private final SecretKey ivKey, frameKey;
private final int transportId = 1234;
private final TransportId transportId = new TransportId(123);
private final long connection = 12345L;
public ConnectionDecrypterImplTest() {

View File

@@ -10,6 +10,7 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import junit.framework.TestCase;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.crypto.CryptoModule;
@@ -24,7 +25,7 @@ public class ConnectionEncrypterImplTest extends TestCase {
private final Cipher ivCipher, frameCipher;
private final SecretKey ivKey, frameKey;
private final int transportId = 1234;
private final TransportId transportId = new TransportId(123);
private final long connection = 12345L;
public ConnectionEncrypterImplTest() {

View File

@@ -10,6 +10,7 @@ import javax.crypto.SecretKey;
import junit.framework.TestCase;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.transport.ConnectionWindow;
@@ -27,7 +28,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
private final CryptoComponent crypto;
private final ContactId contactId;
private final byte[] secret;
private final int transportId;
private final TransportId transportId;
private final ConnectionWindow connectionWindow;
public ConnectionRecogniserImplTest() {
@@ -36,7 +37,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
crypto = i.getInstance(CryptoComponent.class);
contactId = new ContactId(1);
secret = new byte[18];
transportId = 123;
transportId = new TransportId(123);
connectionWindow = new ConnectionWindowImpl(0L, 0);
}

View File

@@ -7,6 +7,7 @@ import java.io.ByteArrayOutputStream;
import junit.framework.TestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.crypto.CryptoModule;
@@ -23,8 +24,8 @@ public class ConnectionWriterTest extends TestCase {
private final ConnectionWriterFactory connectionWriterFactory;
private final byte[] secret = new byte[100];
private final int transportId = 999;
private final long connection = 1234L;
private final TransportId transportId = new TransportId(123);
private final long connection = 12345L;
public ConnectionWriterTest() throws Exception {
super();

View File

@@ -14,6 +14,7 @@ import javax.crypto.Mac;
import javax.crypto.SecretKey;
import junit.framework.TestCase;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionWriter;
@@ -32,8 +33,8 @@ public class FrameReadWriteTest extends TestCase {
private final Mac mac;
private final Random random;
private final byte[] secret = new byte[100];
private final int transportId = 999;
private final long connection = 1234L;
private final TransportId transportId = new TransportId(123);
private final long connection = 12345L;
public FrameReadWriteTest() {
super();

View File

@@ -12,6 +12,7 @@ import junit.framework.TestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseListener;
import net.sf.briar.api.protocol.Message;
@@ -43,10 +44,10 @@ public class BatchConnectionReadWriteTest extends TestCase {
private final File testDir = TestUtils.getTestDirectory();
private final File aliceDir = new File(testDir, "alice");
private final File bobDir = new File(testDir, "bob");
private final Map<Integer, Map<String, String>> transports =
private final TransportId transportId = new TransportId(123);
private final Map<TransportId, Map<String, String>> transports =
Collections.emptyMap();
private final byte[] aliceSecret, bobSecret;
private final int transportId = 123;
private Injector alice, bob;