Fixed existing unit tests for DatabaseComponentImpl, and two bugs.

This commit is contained in:
akwizgran
2013-01-30 14:39:13 +00:00
parent fe153b0815
commit e379f11698
6 changed files with 178 additions and 126 deletions

View File

@@ -514,7 +514,7 @@ DatabaseCleaner.Callback {
subscriptionLock.readLock().unlock(); subscriptionLock.readLock().unlock();
} }
} finally { } finally {
messageLock.readLock().lock(); messageLock.readLock().unlock();
} }
if(messages.isEmpty()) return null; if(messages.isEmpty()) return null;
// Record the message as sent // Record the message as sent
@@ -1589,7 +1589,8 @@ DatabaseCleaner.Callback {
} finally { } finally {
contactLock.writeLock().unlock(); contactLock.writeLock().unlock();
} }
callListeners(new LocalSubscriptionsUpdatedEvent(affected)); if(!affected.isEmpty())
callListeners(new LocalSubscriptionsUpdatedEvent(affected));
} }
public void subscribe(Group g) throws DbException { public void subscribe(Group g) throws DbException {

View File

@@ -347,8 +347,8 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
this.transportId = transportId; this.transportId = transportId;
} }
private EndpointKey(Endpoint ct) { private EndpointKey(Endpoint ep) {
this(ct.getContactId(), ct.getTransportId()); this(ep.getContactId(), ep.getTransportId());
} }
@Override @Override

View File

@@ -83,6 +83,7 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
oneOf(database).getSendability(txn, messageId); oneOf(database).getSendability(txn, messageId);
will(returnValue(0)); will(returnValue(0));
oneOf(database).removeMessage(txn, messageId); oneOf(database).removeMessage(txn, messageId);
oneOf(database).incrementRetentionVersions(txn);
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
oneOf(database).getFreeSpace(); oneOf(database).getFreeSpace();
will(returnValue(MIN_FREE_SPACE)); will(returnValue(MIN_FREE_SPACE));
@@ -114,6 +115,7 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
oneOf(database).getGroupMessageParent(txn, messageId); oneOf(database).getGroupMessageParent(txn, messageId);
will(returnValue(null)); will(returnValue(null));
oneOf(database).removeMessage(txn, messageId); oneOf(database).removeMessage(txn, messageId);
oneOf(database).incrementRetentionVersions(txn);
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
oneOf(database).getFreeSpace(); oneOf(database).getFreeSpace();
will(returnValue(MIN_FREE_SPACE)); will(returnValue(MIN_FREE_SPACE));

View File

@@ -1,5 +1,6 @@
package net.sf.briar.db; package net.sf.briar.db;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collection; import java.util.Collection;
@@ -39,6 +40,8 @@ import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
import org.junit.Test; import org.junit.Test;
// FIXME: Replace allowing() with oneOf() to tighten up tests
public abstract class DatabaseComponentTest extends BriarTestCase { public abstract class DatabaseComponentTest extends BriarTestCase {
protected final Object txn = new Object(); protected final Object txn = new Object();
@@ -53,8 +56,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
private final Message message, privateMessage; private final Message message, privateMessage;
private final Group group; private final Group group;
private final TransportId transportId; private final TransportId transportId;
private final Collection<Transport> transports; private final TransportProperties transportProperties;
private final Endpoint contactTransport; private final Endpoint endpoint;
private final TemporarySecret temporarySecret; private final TemporarySecret temporarySecret;
public DatabaseComponentTest() { public DatabaseComponentTest() {
@@ -74,12 +77,9 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
timestamp, raw); timestamp, raw);
group = new Group(groupId, "The really exciting group", null); group = new Group(groupId, "The really exciting group", null);
transportId = new TransportId(TestUtils.getRandomId()); transportId = new TransportId(TestUtils.getRandomId());
TransportProperties properties = new TransportProperties( transportProperties = new TransportProperties(
Collections.singletonMap("foo", "bar")); Collections.singletonMap("foo", "bar"));
Transport transport = new Transport(transportId, properties); endpoint = new Endpoint(contactId, transportId, 123L, 234L, 345L, true);
transports = Collections.singletonList(transport);
contactTransport = new Endpoint(contactId, transportId, 123L,
234L, 345L, true);
temporarySecret = new TemporarySecret(contactId, transportId, 1L, 2L, temporarySecret = new TemporarySecret(contactId, transportId, 1L, 2L,
3L, false, 4L, new byte[32], 5L, 6L, new byte[4]); 3L, false, 4L, new byte[32], 5L, 6L, new byte[4]);
} }
@@ -127,7 +127,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// getContacts() // getContacts()
oneOf(database).getContacts(txn); oneOf(database).getContacts(txn);
will(returnValue(Collections.singletonList(contactId))); will(returnValue(Collections.singletonList(contactId)));
// getTransportProperties(transportId) // getRemoteProperties(transportId)
oneOf(database).getRemoteProperties(txn, transportId); oneOf(database).getRemoteProperties(txn, transportId);
will(returnValue(Collections.emptyMap())); will(returnValue(Collections.emptyMap()));
// subscribe(group) // subscribe(group)
@@ -138,6 +138,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).containsSubscription(txn, groupId); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
// getMessageHeaders(groupId) // getMessageHeaders(groupId)
oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true));
oneOf(database).getMessageHeaders(txn, groupId); oneOf(database).getMessageHeaders(txn, groupId);
will(returnValue(Collections.emptyList())); will(returnValue(Collections.emptyList()));
// getSubscriptions() // getSubscriptions()
@@ -149,9 +151,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).getVisibility(txn, groupId); oneOf(database).getVisibility(txn, groupId);
will(returnValue(Collections.emptyList())); will(returnValue(Collections.emptyList()));
oneOf(database).removeSubscription(txn, groupId); oneOf(database).removeSubscription(txn, groupId);
// unsubscribe(groupId) again oneOf(listener).eventOccurred(with(any(
oneOf(database).containsSubscription(txn, groupId); LocalSubscriptionsUpdatedEvent.class)));
will(returnValue(false));
// removeContact(contactId) // removeContact(contactId)
oneOf(database).containsContact(txn, contactId); oneOf(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
@@ -178,9 +179,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
db.subscribe(group); // Second time - not called db.subscribe(group); // Second time - not called
assertEquals(Collections.emptyList(), db.getMessageHeaders(groupId)); assertEquals(Collections.emptyList(), db.getMessageHeaders(groupId));
assertEquals(Collections.singletonList(groupId), db.getSubscriptions()); assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
db.unsubscribe(groupId); // First time - listeners called db.unsubscribe(groupId); // Listeners called
db.unsubscribe(groupId); // Second time - not called db.removeContact(contactId); // Listeners called
db.removeContact(contactId);
db.removeListener(listener); db.removeListener(listener);
db.close(); db.close();
@@ -307,7 +307,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(false)); will(returnValue(false));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
}}); }});
@@ -330,7 +330,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
will(returnValue(false)); will(returnValue(false));
@@ -355,7 +355,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
will(returnValue(true)); will(returnValue(true));
@@ -390,7 +390,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
will(returnValue(true)); will(returnValue(true));
@@ -475,25 +475,19 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class); final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class); final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final Ack ack = context.mock(Ack.class);
final Offer offer = context.mock(Offer.class);
final SubscriptionUpdate subscriptionUpdate =
context.mock(SubscriptionUpdate.class);
final TransportUpdate transportUpdate =
context.mock(TransportUpdate.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Check whether the contact is in the DB (which it's not) // Check whether the contact is in the DB (which it's not)
exactly(16).of(database).startTransaction(); exactly(20).of(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
exactly(16).of(database).containsContact(txn, contactId); exactly(20).of(database).containsContact(txn, contactId);
will(returnValue(false)); will(returnValue(false));
exactly(16).of(database).abortTransaction(txn); exactly(20).of(database).abortTransaction(txn);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
try { try {
db.addEndpoint(contactTransport); db.addEndpoint(endpoint);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -513,8 +507,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.generateBatch(contactId, 123, db.generateBatch(contactId, 123, Arrays.asList(messageId));
Collections.<MessageId>emptyList());
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -523,13 +516,33 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try {
db.generateRetentionAck(contactId);
fail();
} catch(NoSuchContactException expected) {}
try {
db.generateRetentionUpdate(contactId);
fail();
} catch(NoSuchContactException expected) {}
try {
db.generateSubscriptionAck(contactId);
fail();
} catch(NoSuchContactException expected) {}
try { try {
db.generateSubscriptionUpdate(contactId); db.generateSubscriptionUpdate(contactId);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.generateTransportUpdate(contactId); db.generateTransportAcks(contactId);
fail();
} catch(NoSuchContactException expected) {}
try {
db.generateTransportUpdates(contactId);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -539,7 +552,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.receiveAck(contactId, ack); Ack a = new Ack(Arrays.asList(messageId));
db.receiveAck(contactId, a);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -549,17 +563,22 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.receiveOffer(contactId, offer); Offer o = new Offer(Arrays.asList(messageId));
db.receiveOffer(contactId, o);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.receiveSubscriptionUpdate(contactId, subscriptionUpdate); SubscriptionUpdate u = new SubscriptionUpdate(
Collections.<Group>emptyList(), 1L);
db.receiveSubscriptionUpdate(contactId, u);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.receiveTransportUpdate(contactId, transportUpdate); TransportUpdate u = new TransportUpdate(transportId,
transportProperties, 1L);
db.receiveTransportUpdate(contactId, u);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -573,11 +592,13 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
// FIXME: Test more methods
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test @Test
public void testVariousMethodsThrowExceptionIfContactTransportIsMissing() public void testVariousMethodsThrowExceptionIfTransportIsMissing()
throws Exception { throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -585,16 +606,24 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class); final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class); final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Check whether the contact transport is in the DB (which it's not) // addContact()
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).addContact(txn);
will(returnValue(contactId));
oneOf(database).commitTransaction(txn);
// Check whether the transport is in the DB (which it's not)
exactly(2).of(database).startTransaction(); exactly(2).of(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
exactly(2).of(database).containsEndpoint(txn, contactId, exactly(2).of(database).containsContact(txn, contactId);
transportId); will(returnValue(true));
exactly(2).of(database).containsTransport(txn, transportId);
will(returnValue(false)); will(returnValue(false));
exactly(2).of(database).abortTransaction(txn); exactly(2).of(database).abortTransaction(txn);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
assertEquals(contactId, db.addContact());
try { try {
db.incrementConnectionCounter(contactId, transportId, 0L); db.incrementConnectionCounter(contactId, transportId, 0L);
@@ -606,6 +635,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
fail(); fail();
} catch(NoSuchTransportException expected) {} } catch(NoSuchTransportException expected) {}
// FIXME: Test more methods
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -678,8 +709,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
public void testGenerateBatchFromRequest() throws Exception { public void testGenerateBatchFromRequest() throws Exception {
final MessageId messageId2 = new MessageId(TestUtils.getRandomId()); final MessageId messageId2 = new MessageId(TestUtils.getRandomId());
final byte[] raw1 = new byte[size]; final byte[] raw1 = new byte[size];
final Collection<MessageId> requested = Arrays.asList(messageId, final Collection<MessageId> requested = new ArrayList<MessageId>(
messageId1, messageId2); Arrays.asList(messageId, messageId1, messageId2));
final Collection<byte[]> messages = Arrays.asList(raw1); final Collection<byte[]> messages = Arrays.asList(raw1);
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -741,6 +772,30 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test
public void testGenerateSubscriptionUpdateNoUpdateDue() throws Exception {
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{
allowing(database).startTransaction();
will(returnValue(txn));
allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getSubscriptionUpdate(txn, contactId);
will(returnValue(null));
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
assertNull(db.generateSubscriptionUpdate(contactId));
context.assertIsSatisfied();
}
@Test @Test
public void testGenerateSubscriptionUpdate() throws Exception { public void testGenerateSubscriptionUpdate() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
@@ -754,26 +809,21 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Get the visible holes and subscriptions oneOf(database).getSubscriptionUpdate(txn, contactId);
oneOf(database).getVisibleHoles(txn, contactId); will(returnValue(new SubscriptionUpdate(Arrays.asList(group), 1L)));
will(returnValue(Collections.emptyMap())); // FIXME
oneOf(database).getVisibleSubscriptions(txn, contactId);
will(returnValue(Collections.singletonMap(group, 0L))); // FIXME
// Get the expiry time
oneOf(database).getExpiryTime(txn);
will(returnValue(0L));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
SubscriptionUpdate s = db.generateSubscriptionUpdate(contactId); SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId);
// FIXME: Check that the update contains the expected data assertEquals(Arrays.asList(group), u.getGroups());
assertEquals(1L, u.getVersion());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test @Test
public void testTransportUpdateNotSentUnlessDue() throws Exception { public void testGenerateTransportUpdatesNoUpdatesDue() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
@@ -785,17 +835,19 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getTransportUpdates(txn, contactId);
will(returnValue(null));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
assertNull(db.generateTransportUpdate(contactId)); assertNull(db.generateTransportUpdates(contactId));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test @Test
public void testGenerateTransportUpdate() throws Exception { public void testGenerateTransportUpdates() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
@@ -807,15 +859,21 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Get the local transport properties oneOf(database).getTransportUpdates(txn, contactId);
oneOf(database).getTransports(txn); will(returnValue(Arrays.asList(new TransportUpdate(transportId,
will(returnValue(transports)); // FIXME transportProperties, 1L))));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
TransportUpdate t = db.generateTransportUpdate(contactId); Collection<TransportUpdate> updates = db.generateTransportUpdates(
// FIXME: Check that the update contains the expected data contactId);
assertNotNull(updates);
assertEquals(1, updates.size());
TransportUpdate u = updates.iterator().next();
assertEquals(transportId, u.getId());
assertEquals(transportProperties, u.getProperties());
assertEquals(1L, u.getVersion());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -915,8 +973,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Only store messages belonging to visible, subscribed groups // Only store messages belonging to visible, subscribed groups
oneOf(database).containsVisibleSubscription(txn, groupId, oneOf(database).containsVisibleSubscription(txn, contactId,
contactId, timestamp); groupId);
will(returnValue(false)); will(returnValue(false));
// The message is not stored but it must still be acked // The message is not stored but it must still be acked
oneOf(database).addMessageToAck(txn, contactId, messageId); oneOf(database).addMessageToAck(txn, contactId, messageId);
@@ -944,8 +1002,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Only store messages belonging to visible, subscribed groups // Only store messages belonging to visible, subscribed groups
oneOf(database).containsVisibleSubscription(txn, groupId, oneOf(database).containsVisibleSubscription(txn, contactId,
contactId, timestamp); groupId);
will(returnValue(true)); will(returnValue(true));
// The message is stored, but it's a duplicate // The message is stored, but it's a duplicate
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
@@ -976,8 +1034,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Only store messages belonging to visible, subscribed groups // Only store messages belonging to visible, subscribed groups
oneOf(database).containsVisibleSubscription(txn, groupId, oneOf(database).containsVisibleSubscription(txn, contactId,
contactId, timestamp); groupId);
will(returnValue(true)); will(returnValue(true));
// The message is stored, and it's not a duplicate // The message is stored, and it's not a duplicate
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
@@ -1018,8 +1076,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Only store messages belonging to visible, subscribed groups // Only store messages belonging to visible, subscribed groups
oneOf(database).containsVisibleSubscription(txn, groupId, oneOf(database).containsVisibleSubscription(txn, contactId,
contactId, timestamp); groupId);
will(returnValue(true)); will(returnValue(true));
// The message is stored, and it's not a duplicate // The message is stored, and it's not a duplicate
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
@@ -1051,8 +1109,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
public void testReceiveOffer() throws Exception { public void testReceiveOffer() throws Exception {
final MessageId messageId1 = new MessageId(TestUtils.getRandomId()); final MessageId messageId1 = new MessageId(TestUtils.getRandomId());
final MessageId messageId2 = new MessageId(TestUtils.getRandomId()); final MessageId messageId2 = new MessageId(TestUtils.getRandomId());
final Collection<MessageId> offered = Arrays.asList(messageId,
messageId1, messageId2);
final BitSet expectedRequest = new BitSet(3); final BitSet expectedRequest = new BitSet(3);
expectedRequest.set(0); expectedRequest.set(0);
expectedRequest.set(2); expectedRequest.set(2);
@@ -1061,7 +1117,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class); final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class); final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final Offer offer = context.mock(Offer.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
allowing(database).startTransaction(); allowing(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
@@ -1069,8 +1124,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Get the offered messages // Get the offered messages
oneOf(offer).getMessageIds();
will(returnValue(offered));
oneOf(database).setStatusSeenIfVisible(txn, contactId, messageId); oneOf(database).setStatusSeenIfVisible(txn, contactId, messageId);
will(returnValue(false)); // Not visible - request message # 0 will(returnValue(false)); // Not visible - request message # 0
oneOf(database).setStatusSeenIfVisible(txn, contactId, messageId1); oneOf(database).setStatusSeenIfVisible(txn, contactId, messageId1);
@@ -1081,7 +1134,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
Request r = db.receiveOffer(contactId, offer); Offer o = new Offer(Arrays.asList(messageId, messageId1, messageId2));
Request r = db.receiveOffer(contactId, o);
assertEquals(expectedRequest, r.getBitmap()); assertEquals(expectedRequest, r.getBitmap());
assertEquals(3, r.getLength()); assertEquals(3, r.getLength());
@@ -1090,38 +1144,25 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
@Test @Test
public void testReceiveSubscriptionUpdate() throws Exception { public void testReceiveSubscriptionUpdate() throws Exception {
final GroupId start = new GroupId(TestUtils.getRandomId());
final GroupId end = new GroupId(TestUtils.getRandomId());
final long expiry = 1234L;
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class); final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class); final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final SubscriptionUpdate subscriptionUpdate =
context.mock(SubscriptionUpdate.class); // FIXME: Don't mock
context.checking(new Expectations() {{ context.checking(new Expectations() {{
allowing(database).startTransaction(); allowing(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Get the contents of the update oneOf(database).setSubscriptions(txn, contactId,
oneOf(subscriptionUpdate).getHoles(); Arrays.asList(group), 1L);
will(returnValue(Collections.singletonMap(start, end))); // FIXME
oneOf(subscriptionUpdate).getGroupIds();
will(returnValue(Collections.singletonMap(group, 0L))); // FIXME
oneOf(subscriptionUpdate).getExpiryTime();
will(returnValue(expiry));
// Store the contents of the update
oneOf(database).removeSubscriptions(txn, contactId, start, end);
oneOf(database).addSubscription(txn, contactId, group, 0L);
oneOf(database).setExpiryTime(txn, contactId, expiry);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
db.receiveSubscriptionUpdate(contactId, subscriptionUpdate); SubscriptionUpdate u = new SubscriptionUpdate(Arrays.asList(group), 1L);
db.receiveSubscriptionUpdate(contactId, u);
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -1133,23 +1174,21 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class); final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class); final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final TransportUpdate transportUpdate =
context.mock(TransportUpdate.class); // FIXME: Don't mock
context.checking(new Expectations() {{ context.checking(new Expectations() {{
allowing(database).startTransaction(); allowing(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
// Get the contents of the update oneOf(database).setRemoteProperties(txn, contactId, transportId,
oneOf(transportUpdate).getTransports(); transportProperties, 1L);
will(returnValue(transports));
oneOf(database).setTransports(txn, contactId, transports);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
db.receiveTransportUpdate(contactId, transportUpdate); TransportUpdate u = new TransportUpdate(transportId,
transportProperties, 1L);
db.receiveTransportUpdate(contactId, u);
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -1166,7 +1205,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
will(returnValue(true)); will(returnValue(true));
@@ -1234,7 +1273,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addLocalGroupMessage(message) // addLocalGroupMessage(message)
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId, timestamp); oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addGroupMessage(txn, message); oneOf(database).addGroupMessage(txn, message);
will(returnValue(false)); will(returnValue(false));
@@ -1280,7 +1319,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} }
@Test @Test
public void testTransportPropertiesChangedCallsListeners() public void testChangingLocalTransportPropertiesCallsListeners()
throws Exception { throws Exception {
final TransportProperties properties = final TransportProperties properties =
new TransportProperties(Collections.singletonMap("bar", "baz")); new TransportProperties(Collections.singletonMap("bar", "baz"));
@@ -1292,6 +1331,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsTransport(txn, transportId);
will(returnValue(true));
oneOf(database).getLocalProperties(txn, transportId); oneOf(database).getLocalProperties(txn, transportId);
will(returnValue(new TransportProperties())); will(returnValue(new TransportProperties()));
oneOf(database).mergeLocalProperties(txn, transportId, properties); oneOf(database).mergeLocalProperties(txn, transportId, properties);
@@ -1306,7 +1347,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} }
@Test @Test
public void testTransportPropertiesUnchangedDoesNotCallListeners() public void testNotChangingLocalTransportPropertiesDoesNotCallListeners()
throws Exception { throws Exception {
final TransportProperties properties = final TransportProperties properties =
new TransportProperties(Collections.singletonMap("bar", "baz")); new TransportProperties(Collections.singletonMap("bar", "baz"));
@@ -1319,6 +1360,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsTransport(txn, transportId);
will(returnValue(true));
oneOf(database).getLocalProperties(txn, transportId); oneOf(database).getLocalProperties(txn, transportId);
will(returnValue(properties)); will(returnValue(properties));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);
@@ -1357,7 +1400,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} }
@Test @Test
public void testVisibilityChangedCallsListeners() throws Exception { public void testChangingVisibilityCallsListeners() throws Exception {
final ContactId contactId1 = new ContactId(123); final ContactId contactId1 = new ContactId(123);
final Collection<ContactId> both = Arrays.asList(contactId, contactId1); final Collection<ContactId> both = Arrays.asList(contactId, contactId1);
Mockery context = new Mockery(); Mockery context = new Mockery();
@@ -1369,6 +1412,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true));
oneOf(database).getVisibility(txn, groupId); oneOf(database).getVisibility(txn, groupId);
will(returnValue(both)); will(returnValue(both));
oneOf(database).getContacts(txn); oneOf(database).getContacts(txn);
@@ -1388,7 +1433,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} }
@Test @Test
public void testVisibilityUnchangedDoesNotCallListeners() throws Exception { public void testNotChangingVisibilityDoesNotCallListeners()
throws Exception {
final ContactId contactId1 = new ContactId(234); final ContactId contactId1 = new ContactId(234);
final Collection<ContactId> both = Arrays.asList(contactId, contactId1); final Collection<ContactId> both = Arrays.asList(contactId, contactId1);
Mockery context = new Mockery(); Mockery context = new Mockery();
@@ -1400,6 +1446,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsSubscription(txn, groupId);
will(returnValue(true));
oneOf(database).getVisibility(txn, groupId); oneOf(database).getVisibility(txn, groupId);
will(returnValue(both)); will(returnValue(both));
oneOf(database).getContacts(txn); oneOf(database).getContacts(txn);
@@ -1426,8 +1474,9 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// addSecrets() // addSecrets()
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsEndpoint(txn, contactId, oneOf(database).containsContact(txn, contactId);
transportId); will(returnValue(true));
oneOf(database).containsTransport(txn, transportId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).addSecrets(txn, oneOf(database).addSecrets(txn,
Collections.singletonList(temporarySecret)); Collections.singletonList(temporarySecret));

View File

@@ -7,6 +7,7 @@ import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGT
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH; import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@@ -108,7 +109,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
connRegistry, connFactory, protoFactory, ctx, transport); connRegistry, connFactory, protoFactory, ctx, transport);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// No transports to send // No transports to send
oneOf(db).generateTransportUpdate(contactId); oneOf(db).generateTransportUpdates(contactId);
will(returnValue(null)); will(returnValue(null));
// No subscriptions to send // No subscriptions to send
oneOf(db).generateSubscriptionUpdate(contactId); oneOf(db).generateSubscriptionUpdate(contactId);
@@ -138,20 +139,17 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
secret, 0L, true); secret, 0L, true);
OutgoingSimplexConnection connection = new OutgoingSimplexConnection(db, OutgoingSimplexConnection connection = new OutgoingSimplexConnection(db,
connRegistry, connFactory, protoFactory, ctx, transport); connRegistry, connFactory, protoFactory, ctx, transport);
final Ack ack = context.mock(Ack.class);
final byte[] raw = new byte[1234]; final byte[] raw = new byte[1234];
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// No transports to send // No transports to send
oneOf(db).generateTransportUpdate(contactId); oneOf(db).generateTransportUpdates(contactId);
will(returnValue(null)); will(returnValue(null));
// No subscriptions to send // No subscriptions to send
oneOf(db).generateSubscriptionUpdate(contactId); oneOf(db).generateSubscriptionUpdate(contactId);
will(returnValue(null)); will(returnValue(null));
// One ack to send // One ack to send
oneOf(db).generateAck(with(contactId), with(any(int.class))); oneOf(db).generateAck(with(contactId), with(any(int.class)));
will(returnValue(ack)); will(returnValue(new Ack(Arrays.asList(messageId))));
oneOf(ack).getMessageIds();
will(returnValue(Collections.singletonList(messageId)));
// No more acks // No more acks
oneOf(db).generateAck(with(contactId), with(any(int.class))); oneOf(db).generateAck(with(contactId), with(any(int.class)));
will(returnValue(null)); will(returnValue(null));

View File

@@ -11,6 +11,7 @@ import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule; import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.crypto.KeyManager; import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.event.DatabaseEvent; import net.sf.briar.api.db.event.DatabaseEvent;
@@ -106,10 +107,11 @@ public class SimplexProtocolIntegrationTest extends BriarTestCase {
km.start(); km.start();
// Add Bob as a contact // Add Bob as a contact
ContactId contactId = db.addContact(); ContactId contactId = db.addContact();
Endpoint ct = new Endpoint(contactId, transportId, Endpoint ep = new Endpoint(contactId, transportId, epoch,
epoch, CLOCK_DIFFERENCE, LATENCY, true); CLOCK_DIFFERENCE, LATENCY, true);
db.addEndpoint(ct); db.addTransport(transportId);
km.endpointAdded(ct, initialSecret.clone()); db.addEndpoint(ep);
km.endpointAdded(ep, initialSecret.clone());
// Send Bob a message // Send Bob a message
String subject = "Hello"; String subject = "Hello";
byte[] body = "Hi Bob!".getBytes("UTF-8"); byte[] body = "Hi Bob!".getBytes("UTF-8");
@@ -150,17 +152,17 @@ public class SimplexProtocolIntegrationTest extends BriarTestCase {
km.start(); km.start();
// Add Alice as a contact // Add Alice as a contact
ContactId contactId = db.addContact(); ContactId contactId = db.addContact();
Endpoint ct = new Endpoint(contactId, transportId, Endpoint ep = new Endpoint(contactId, transportId, epoch,
epoch, CLOCK_DIFFERENCE, LATENCY, false); CLOCK_DIFFERENCE, LATENCY, false);
db.addEndpoint(ct); db.addTransport(transportId);
km.endpointAdded(ct, initialSecret.clone()); db.addEndpoint(ep);
km.endpointAdded(ep, initialSecret.clone());
// Set up a database listener // Set up a database listener
MessageListener listener = new MessageListener(); MessageListener listener = new MessageListener();
db.addListener(listener); db.addListener(listener);
// Fake a transport update from Alice // Fake a transport update from Alice
Transport t = new Transport(transportId); TransportUpdate transportUpdate = new TransportUpdate(transportId,
TransportUpdate transportUpdate = new TransportUpdate(t, new TransportProperties(), 1L);
transportVersion);
db.receiveTransportUpdate(contactId, transportUpdate); db.receiveTransportUpdate(contactId, transportUpdate);
// Create a connection recogniser and recognise the connection // Create a connection recogniser and recognise the connection
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);