Fixed existing unit tests for H2Database; new tests are needed.

This commit is contained in:
akwizgran
2013-01-29 16:43:41 +00:00
parent 7ecda94340
commit 33d0f19f26

View File

@@ -25,7 +25,6 @@ import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.clock.SystemClock;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.MessageHeader; import net.sf.briar.api.db.MessageHeader;
import net.sf.briar.api.protocol.AuthorId; import net.sf.briar.api.protocol.AuthorId;
@@ -33,7 +32,6 @@ import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupId; import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message; import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId; import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.transport.Endpoint; import net.sf.briar.api.transport.Endpoint;
import net.sf.briar.api.transport.TemporarySecret; import net.sf.briar.api.transport.TemporarySecret;
@@ -298,7 +296,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW); db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -336,7 +334,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1); db.setSendability(txn, messageId, 1);
@@ -389,7 +387,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertFalse(it.hasNext()); assertFalse(it.hasNext());
// The contact subscribing should make the message sendable // The contact subscribing should make the message sendable
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
assertTrue(db.hasSendableMessages(txn, contactId)); assertTrue(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator(); it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext()); assertTrue(it.hasNext());
@@ -397,7 +395,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertFalse(it.hasNext()); assertFalse(it.hasNext());
// The contact unsubscribing should make the message unsendable // The contact unsubscribing should make the message unsendable
db.removeSubscriptions(txn, contactId, null, null); db.setSubscriptions(txn, contactId, Collections.<Group>emptyList(), 2L);
assertFalse(db.hasSendableMessages(txn, contactId)); assertFalse(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator(); it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext()); assertFalse(it.hasNext());
@@ -406,41 +404,6 @@ public class H2DatabaseTest extends BriarTestCase {
db.close(); db.close();
} }
@Test
public void testSendableGroupMessagesMustBeNewerThanSubscriptions()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
// The message is older than the contact's subscription, so it should
// not be sendable
db.addSubscription(txn, contactId, group, timestamp + 1);
assertFalse(db.hasSendableMessages(txn, contactId));
Iterator<MessageId> it =
db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
// Changing the contact's subscription should make the message sendable
db.removeSubscriptions(txn, contactId, null, null);
db.addSubscription(txn, contactId, group, timestamp);
assertTrue(db.hasSendableMessages(txn, contactId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext());
assertEquals(messageId, it.next());
assertFalse(it.hasNext());
db.commitTransaction(txn);
db.close();
}
@Test @Test
public void testSendableGroupMessagesMustFitCapacity() throws Exception { public void testSendableGroupMessagesMustFitCapacity() throws Exception {
Database<Connection> db = open(false); Database<Connection> db = open(false);
@@ -450,7 +413,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1); db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW); db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -480,7 +443,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact, subscribe to a group and store a message // Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1); db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW); db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -564,7 +527,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.setSendability(txn, messageId, 1); db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW); db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -795,34 +758,28 @@ public class H2DatabaseTest extends BriarTestCase {
} }
@Test @Test
public void testUpdateTransportProperties() throws Exception { public void testUpdateRemoteTransportProperties() throws Exception {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add a contact with a transport // Add a contact with a transport
TransportProperties properties = TransportProperties p = new TransportProperties(
new TransportProperties(Collections.singletonMap("foo", "bar")); Collections.singletonMap("foo", "bar"));
Transport transport = new Transport(transportId, properties);
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.setTransports(txn, contactId, db.setRemoteProperties(txn, contactId, transportId, p, 1L);
Collections.singletonList(transport), 1); assertEquals(Collections.singletonMap(contactId, p),
assertEquals(Collections.singletonMap(contactId, properties),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
// Replace the transport properties // Replace the transport properties
TransportProperties properties1 = TransportProperties p1 = new TransportProperties(
new TransportProperties(Collections.singletonMap("baz", "bam")); Collections.singletonMap("baz", "bam"));
Transport transport1 = new Transport(transportId, properties1); db.setRemoteProperties(txn, contactId, transportId, p1, 2L);
db.setTransports(txn, contactId, assertEquals(Collections.singletonMap(contactId, p1),
Collections.singletonList(transport1), 2);
assertEquals(Collections.singletonMap(contactId, properties1),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
// Remove the transport properties // Remove the transport properties
TransportProperties properties2 = new TransportProperties(); TransportProperties p2 = new TransportProperties();
Transport transport2 = new Transport(transportId, properties2); db.setRemoteProperties(txn, contactId, transportId, p2, 3L);
db.setTransports(txn, contactId,
Collections.singletonList(transport2), 3);
assertEquals(Collections.emptyMap(), assertEquals(Collections.emptyMap(),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
@@ -831,31 +788,30 @@ public class H2DatabaseTest extends BriarTestCase {
} }
@Test @Test
public void testLocalTransports() throws Exception { public void testUpdateLocalTransportProperties() throws Exception {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add a transport to the database
db.addTransport(txn, transportId);
// Set the transport properties // Set the transport properties
TransportProperties properties = new TransportProperties(); TransportProperties p = new TransportProperties();
properties.put("foo", "foo"); p.put("foo", "foo");
properties.put("bar", "bar"); p.put("bar", "bar");
db.mergeLocalProperties(txn, transportId, properties); db.mergeLocalProperties(txn, transportId, p);
Transport transport = new Transport(transportId, properties); assertEquals(p, db.getLocalProperties(txn, transportId));
assertEquals(Collections.singletonList(transport),
db.getLocalTransports(txn));
// Update one of the properties and add another // Update one of the properties and add another
TransportProperties properties1 = new TransportProperties(); TransportProperties p1 = new TransportProperties();
properties1.put("bar", "baz"); p1.put("bar", "baz");
properties1.put("bam", "bam"); p1.put("bam", "bam");
db.mergeLocalProperties(txn, transportId, properties1); db.mergeLocalProperties(txn, transportId, p1);
TransportProperties merged = new TransportProperties(); TransportProperties merged = new TransportProperties();
merged.put("foo", "foo"); merged.put("foo", "foo");
merged.put("bar", "baz"); merged.put("bar", "baz");
merged.put("bam", "bam"); merged.put("bam", "bam");
Transport transport1 = new Transport(transportId, merged); assertEquals(merged, db.getLocalProperties(txn, transportId));
assertEquals(Collections.singletonList(transport1),
db.getLocalTransports(txn));
db.commitTransaction(txn); db.commitTransaction(txn);
db.close(); db.close();
@@ -866,61 +822,58 @@ public class H2DatabaseTest extends BriarTestCase {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add a transport to the database
db.addTransport(txn, transportId);
// Set the transport config // Set the transport config
TransportConfig config = new TransportConfig(); TransportConfig c = new TransportConfig();
config.put("foo", "foo"); c.put("foo", "foo");
config.put("bar", "bar"); c.put("bar", "bar");
db.mergeConfig(txn, transportId, config); db.mergeConfig(txn, transportId, c);
assertEquals(config, db.getConfig(txn, transportId)); assertEquals(c, db.getConfig(txn, transportId));
// Update one of the properties and add another // Update one of the properties and add another
TransportConfig config1 = new TransportConfig(); TransportConfig c1 = new TransportConfig();
config1.put("bar", "baz"); c1.put("bar", "baz");
config1.put("bam", "bam"); c1.put("bam", "bam");
db.mergeConfig(txn, transportId, config1); db.mergeConfig(txn, transportId, c1);
TransportConfig expected = new TransportConfig(); TransportConfig merged = new TransportConfig();
expected.put("foo", "foo"); merged.put("foo", "foo");
expected.put("bar", "baz"); merged.put("bar", "baz");
expected.put("bam", "bam"); merged.put("bam", "bam");
assertEquals(expected, db.getConfig(txn, transportId)); assertEquals(merged, db.getConfig(txn, transportId));
db.commitTransaction(txn); db.commitTransaction(txn);
db.close(); db.close();
} }
@Test @Test
public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception { public void testTransportsNotUpdatedIfVersionIsOld() throws Exception {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add a contact with a transport // Initialise the transport properties with version 1
TransportProperties properties = TransportProperties p = new TransportProperties(
new TransportProperties(Collections.singletonMap("foo", "bar")); Collections.singletonMap("foo", "bar"));
Transport transport = new Transport(transportId, properties);
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.setTransports(txn, contactId, db.setRemoteProperties(txn, contactId, transportId, p, 1L);
Collections.singletonList(transport), 1); assertEquals(Collections.singletonMap(contactId, p),
assertEquals(Collections.singletonMap(contactId, properties),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
// Replace the transport properties using a timestamp of 2 // Replace the transport properties with version 2
TransportProperties properties1 = TransportProperties p1 = new TransportProperties(
new TransportProperties(Collections.singletonMap("baz", "bam")); Collections.singletonMap("baz", "bam"));
Transport transport1 = new Transport(transportId, properties1); db.setRemoteProperties(txn, contactId, transportId, p1, 2L);
db.setTransports(txn, contactId, assertEquals(Collections.singletonMap(contactId, p1),
Collections.singletonList(transport1), 2);
assertEquals(Collections.singletonMap(contactId, properties1),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
// Try to replace the transport properties using a timestamp of 1 // Try to replace the transport properties with version 1
TransportProperties properties2 = TransportProperties p2 = new TransportProperties(
new TransportProperties(Collections.singletonMap("quux", "etc")); Collections.singletonMap("quux", "etc"));
Transport transport2 = new Transport(transportId, properties2); db.setRemoteProperties(txn, contactId, transportId, p2, 1L);
db.setTransports(txn, contactId,
Collections.singletonList(transport2), 1);
// The old properties should still be there // Version 2 of the properties should still be there
assertEquals(Collections.singletonMap(contactId, properties1), assertEquals(Collections.singletonMap(contactId, p1),
db.getRemoteProperties(txn, transportId)); db.getRemoteProperties(txn, transportId));
db.commitTransaction(txn); db.commitTransaction(txn);
@@ -936,7 +889,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact and subscribe to a group // Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
// The message is not in the database // The message is not in the database
assertNull(db.getMessageIfSendable(txn, contactId, messageId)); assertNull(db.getMessageIfSendable(txn, contactId, messageId));
@@ -953,7 +906,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact, subscribe to a group and store a message // Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// Set the sendability to > 0 and the status to SEEN // Set the sendability to > 0 and the status to SEEN
@@ -976,7 +929,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact, subscribe to a group and store a message // Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// Set the sendability to 0 and the status to NEW // Set the sendability to 0 and the status to NEW
@@ -996,11 +949,12 @@ public class H2DatabaseTest extends BriarTestCase {
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message - // Add a contact, subscribe to a group and store a message -
// the message is older than the contact's subscription // the message is older than the contact's retention time
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, timestamp + 1); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.setRetentionTime(txn, contactId, timestamp + 1, 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// Set the sendability to > 0 and the status to NEW // Set the sendability to > 0 and the status to NEW
@@ -1023,7 +977,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// Set the sendability to > 0 and the status to NEW // Set the sendability to > 0 and the status to NEW
@@ -1048,7 +1002,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
// The message is not in the database // The message is not in the database
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId)); assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
@@ -1065,7 +1019,7 @@ public class H2DatabaseTest extends BriarTestCase {
// Add a contact with a subscription // Add a contact with a subscription
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
// There's no local subscription for the group // There's no local subscription for the group
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId)); assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
@@ -1103,7 +1057,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.setStatus(txn, contactId, messageId, Status.NEW); db.setStatus(txn, contactId, messageId, Status.NEW);
// The subscription is not visible // The subscription is not visible
@@ -1123,7 +1077,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// The message has already been seen by the contact // The message has already been seen by the contact
@@ -1145,7 +1099,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group); db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId); db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L); db.setSubscriptions(txn, contactId, Arrays.asList(group), 1L);
db.addGroupMessage(txn, message); db.addGroupMessage(txn, message);
// The message has not been seen by the contact // The message has not been seen by the contact
@@ -1556,14 +1510,14 @@ public class H2DatabaseTest extends BriarTestCase {
@Test @Test
public void testTemporarySecrets() throws Exception { public void testTemporarySecrets() throws Exception {
// Create a contact transport and three consecutive temporary secrets // Create an endpoint and three consecutive temporary secrets
long epoch = 123L, clockDiff = 234L, latency = 345L; long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false; boolean alice = false;
long outgoing1 = 456L, centre1 = 567L; long outgoing1 = 456L, centre1 = 567L;
long outgoing2 = 678L, centre2 = 789L; long outgoing2 = 678L, centre2 = 789L;
long outgoing3 = 890L, centre3 = 901L; long outgoing3 = 890L, centre3 = 901L;
Endpoint ct = new Endpoint(contactId, transportId, Endpoint ep = new Endpoint(contactId, transportId, epoch, clockDiff,
epoch, clockDiff, latency, alice); latency, alice);
Random random = new Random(); Random random = new Random();
byte[] secret1 = new byte[32], bitmap1 = new byte[4]; byte[] secret1 = new byte[32], bitmap1 = new byte[4];
random.nextBytes(secret1); random.nextBytes(secret1);
@@ -1590,9 +1544,11 @@ public class H2DatabaseTest extends BriarTestCase {
// Initially there should be no secrets in the database // Initially there should be no secrets in the database
assertEquals(Collections.emptyList(), db.getSecrets(txn)); assertEquals(Collections.emptyList(), db.getSecrets(txn));
// Add the contact transport and the first two secrets // Add the contact, the transport, the endpoint and the first two
// secrets (periods 0 and 1)
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct); db.addTransport(txn, transportId);
db.addEndpoint(txn, ep);
db.addSecrets(txn, Arrays.asList(s1, s2)); db.addSecrets(txn, Arrays.asList(s1, s2));
// Retrieve the first two secrets // Retrieve the first two secrets
@@ -1667,12 +1623,12 @@ public class H2DatabaseTest extends BriarTestCase {
@Test @Test
public void testIncrementConnectionCounter() throws Exception { public void testIncrementConnectionCounter() throws Exception {
// Create a contact transport and a temporary secret // Create an endpoint and a temporary secret
long epoch = 123L, clockDiff = 234L, latency = 345L; long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false; boolean alice = false;
long period = 456L, outgoing = 567L, centre = 678L; long period = 456L, outgoing = 567L, centre = 678L;
Endpoint ct = new Endpoint(contactId, transportId, Endpoint ep = new Endpoint(contactId, transportId, epoch, clockDiff,
epoch, clockDiff, latency, alice); latency, alice);
Random random = new Random(); Random random = new Random();
byte[] secret = new byte[32], bitmap = new byte[4]; byte[] secret = new byte[32], bitmap = new byte[4];
random.nextBytes(secret); random.nextBytes(secret);
@@ -1683,9 +1639,10 @@ public class H2DatabaseTest extends BriarTestCase {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add the contact transport and the temporary secret // Add the contact, the transport, the endpoint and the temporary secret
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct); db.addTransport(txn, transportId);
db.addEndpoint(txn, ep);
db.addSecrets(txn, Arrays.asList(s)); db.addSecrets(txn, Arrays.asList(s));
// Retrieve the secret // Retrieve the secret
@@ -1722,12 +1679,12 @@ public class H2DatabaseTest extends BriarTestCase {
@Test @Test
public void testSetConnectionWindow() throws Exception { public void testSetConnectionWindow() throws Exception {
// Create a contact transport and a temporary secret // Create an endpoint and a temporary secret
long epoch = 123L, clockDiff = 234L, latency = 345L; long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false; boolean alice = false;
long period = 456L, outgoing = 567L, centre = 678L; long period = 456L, outgoing = 567L, centre = 678L;
Endpoint ct = new Endpoint(contactId, transportId, Endpoint ep = new Endpoint(contactId, transportId, epoch, clockDiff,
epoch, clockDiff, latency, alice); latency, alice);
Random random = new Random(); Random random = new Random();
byte[] secret = new byte[32], bitmap = new byte[4]; byte[] secret = new byte[32], bitmap = new byte[4];
random.nextBytes(secret); random.nextBytes(secret);
@@ -1738,9 +1695,10 @@ public class H2DatabaseTest extends BriarTestCase {
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Add the contact transport and the temporary secret // Add the contact, the transport, the endpoint and the temporary secret
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct); db.addTransport(txn, transportId);
db.addEndpoint(txn, ep);
db.addSecrets(txn, Arrays.asList(s)); db.addSecrets(txn, Arrays.asList(s));
// Retrieve the secret // Retrieve the secret
@@ -1791,45 +1749,47 @@ public class H2DatabaseTest extends BriarTestCase {
@Test @Test
public void testContactTransports() throws Exception { public void testContactTransports() throws Exception {
// Create some contact transports // Create some endpoints
long epoch1 = 123L, clockDiff1 = 234L, latency1 = 345L; long epoch1 = 123L, clockDiff1 = 234L, latency1 = 345L;
long epoch2 = 456L, clockDiff2 = 567L, latency2 = 678L; long epoch2 = 456L, clockDiff2 = 567L, latency2 = 678L;
boolean alice1 = true, alice2 = false; boolean alice1 = true, alice2 = false;
TransportId transportId1 = new TransportId(TestUtils.getRandomId()); TransportId transportId1 = new TransportId(TestUtils.getRandomId());
TransportId transportId2 = new TransportId(TestUtils.getRandomId()); TransportId transportId2 = new TransportId(TestUtils.getRandomId());
Endpoint ct1 = new Endpoint(contactId, transportId1, Endpoint ep1 = new Endpoint(contactId, transportId1, epoch1, clockDiff1,
epoch1, clockDiff1, latency1, alice1); latency1, alice1);
Endpoint ct2 = new Endpoint(contactId, transportId2, Endpoint ep2 = new Endpoint(contactId, transportId2, epoch2, clockDiff2,
epoch2, clockDiff2, latency2, alice2); latency2, alice2);
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
// Initially there should be no contact transports in the database // Initially there should be no endpoints in the database
assertEquals(Collections.emptyList(), db.getEndpoints(txn)); assertEquals(Collections.emptyList(), db.getEndpoints(txn));
// Add a contact and the contact transports // Add the contact, the transports and the endpoints
assertEquals(contactId, db.addContact(txn)); assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct1); db.addTransport(txn, transportId1);
db.addEndpoint(txn, ct2); db.addTransport(txn, transportId2);
db.addEndpoint(txn, ep1);
db.addEndpoint(txn, ep2);
// Retrieve the contact transports // Retrieve the contact transports
Collection<Endpoint> cts = db.getEndpoints(txn); Collection<Endpoint> endpoints = db.getEndpoints(txn);
assertEquals(2, cts.size()); assertEquals(2, endpoints.size());
boolean foundFirst = false, foundSecond = false; boolean foundFirst = false, foundSecond = false;
for(Endpoint ct : cts) { for(Endpoint ep : endpoints) {
assertEquals(contactId, ct.getContactId()); assertEquals(contactId, ep.getContactId());
if(ct.getTransportId().equals(transportId1)) { if(ep.getTransportId().equals(transportId1)) {
assertEquals(epoch1, ct.getEpoch()); assertEquals(epoch1, ep.getEpoch());
assertEquals(clockDiff1, ct.getClockDifference()); assertEquals(clockDiff1, ep.getClockDifference());
assertEquals(latency1, ct.getLatency()); assertEquals(latency1, ep.getLatency());
assertEquals(alice1, ct.getAlice()); assertEquals(alice1, ep.getAlice());
foundFirst = true; foundFirst = true;
} else if(ct.getTransportId().equals(transportId2)) { } else if(ep.getTransportId().equals(transportId2)) {
assertEquals(epoch2, ct.getEpoch()); assertEquals(epoch2, ep.getEpoch());
assertEquals(clockDiff2, ct.getClockDifference()); assertEquals(clockDiff2, ep.getClockDifference());
assertEquals(latency2, ct.getLatency()); assertEquals(latency2, ep.getLatency());
assertEquals(alice2, ct.getAlice()); assertEquals(alice2, ep.getAlice());
foundSecond = true; foundSecond = true;
} else { } else {
fail(); fail();
@@ -1863,8 +1823,8 @@ public class H2DatabaseTest extends BriarTestCase {
} }
private Database<Connection> open(boolean resume) throws Exception { private Database<Connection> open(boolean resume) throws Exception {
Database<Connection> db = new H2Database(new SystemClock(), Database<Connection> db = new H2Database(new TestDatabaseConfig(testDir,
new TestDatabaseConfig(testDir, MAX_SIZE)); MAX_SIZE));
db.open(resume); db.open(resume);
return db; return db;
} }