mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Use a separate linked list for each contact, containing visible groups.
This commit is contained in:
@@ -22,6 +22,7 @@ import net.sf.briar.api.db.event.ContactRemovedEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.MessagesAddedEvent;
|
||||
import net.sf.briar.api.db.event.RatingChangedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.TransportAddedEvent;
|
||||
import net.sf.briar.api.lifecycle.ShutdownManager;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
@@ -679,14 +680,10 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
|
||||
public void testGenerateBatch() throws Exception {
|
||||
final MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
final byte[] raw1 = new byte[size];
|
||||
final Collection<MessageId> sendable = Arrays.asList(new MessageId[] {
|
||||
messageId,
|
||||
messageId1
|
||||
});
|
||||
final Collection<byte[]> messages = Arrays.asList(new byte[][] {
|
||||
raw,
|
||||
raw1
|
||||
});
|
||||
final Collection<MessageId> sendable =
|
||||
Arrays.asList(new MessageId[] {messageId, messageId1});
|
||||
final Collection<byte[]> messages =
|
||||
Arrays.asList(new byte[][] {raw, raw1});
|
||||
Mockery context = new Mockery();
|
||||
@SuppressWarnings("unchecked")
|
||||
final Database<Object> database = context.mock(Database.class);
|
||||
@@ -733,9 +730,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
|
||||
requested.add(messageId);
|
||||
requested.add(messageId1);
|
||||
requested.add(messageId2);
|
||||
final Collection<byte[]> msgs = Arrays.asList(new byte[][] {
|
||||
raw1
|
||||
});
|
||||
final Collection<byte[]> msgs = Arrays.asList(new byte[][] {raw1});
|
||||
Mockery context = new Mockery();
|
||||
@SuppressWarnings("unchecked")
|
||||
final Database<Object> database = context.mock(Database.class);
|
||||
@@ -1541,4 +1536,70 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVisibilityChangedCallsListeners() throws Exception {
|
||||
final ContactId contactId1 = new ContactId(234);
|
||||
final Collection<ContactId> both =
|
||||
Arrays.asList(new ContactId[] {contactId, contactId1});
|
||||
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);
|
||||
final PacketFactory packetFactory = context.mock(PacketFactory.class);
|
||||
final DatabaseListener listener = context.mock(DatabaseListener.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).getVisibility(txn, groupId);
|
||||
will(returnValue(both));
|
||||
oneOf(database).getContacts(txn);
|
||||
will(returnValue(both));
|
||||
oneOf(database).removeVisibility(txn, contactId1, groupId);
|
||||
oneOf(database).setSubscriptionsModified(with(txn),
|
||||
with(Collections.singletonList(contactId1)),
|
||||
with(any(long.class)));
|
||||
oneOf(database).commitTransaction(txn);
|
||||
oneOf(listener).eventOccurred(with(any(
|
||||
SubscriptionsUpdatedEvent.class)));
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, cleaner,
|
||||
shutdown, packetFactory);
|
||||
|
||||
db.addListener(listener);
|
||||
db.setVisibility(groupId, Collections.singletonList(contactId));
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVisibilityUnchangedDoesNotCallListeners() throws Exception {
|
||||
final ContactId contactId1 = new ContactId(234);
|
||||
final Collection<ContactId> both =
|
||||
Arrays.asList(new ContactId[] {contactId, contactId1});
|
||||
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);
|
||||
final PacketFactory packetFactory = context.mock(PacketFactory.class);
|
||||
final DatabaseListener listener = context.mock(DatabaseListener.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).getVisibility(txn, groupId);
|
||||
will(returnValue(both));
|
||||
oneOf(database).getContacts(txn);
|
||||
will(returnValue(both));
|
||||
oneOf(database).commitTransaction(txn);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, cleaner,
|
||||
shutdown, packetFactory);
|
||||
|
||||
db.addListener(listener);
|
||||
db.setVisibility(groupId, both);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||
@@ -393,7 +393,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
@@ -435,7 +435,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||
@@ -474,7 +474,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||
@@ -509,7 +509,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
@@ -553,7 +553,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
assertFalse(it.hasNext());
|
||||
|
||||
// Making the subscription visible should make the message sendable
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
assertTrue(db.hasSendableMessages(txn, contactId));
|
||||
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
||||
assertTrue(it.hasNext());
|
||||
@@ -674,7 +674,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
@@ -713,7 +713,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
db.setSendability(txn, messageId, 1);
|
||||
@@ -1274,7 +1274,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// the message is older than the contact's subscription
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
Map<Group, Long> subs = Collections.singletonMap(group, timestamp + 1);
|
||||
db.setSubscriptions(txn, contactId, subs, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
@@ -1298,7 +1298,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
|
||||
@@ -1323,7 +1323,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact and subscribe to a group
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
|
||||
// The message is not in the database
|
||||
@@ -1398,7 +1398,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
|
||||
@@ -1420,7 +1420,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// Add a contact, subscribe to a group and store a message
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
db.addSubscription(txn, group);
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
db.setSubscriptions(txn, contactId, subscriptions, 1);
|
||||
db.addGroupMessage(txn, message);
|
||||
|
||||
@@ -1444,11 +1444,11 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
// The group should not be visible to the contact
|
||||
assertEquals(Collections.emptyList(), db.getVisibility(txn, groupId));
|
||||
// Make the group visible to the contact
|
||||
db.setVisibility(txn, groupId, Collections.singletonList(contactId));
|
||||
db.addVisibility(txn, contactId, groupId);
|
||||
assertEquals(Collections.singletonList(contactId),
|
||||
db.getVisibility(txn, groupId));
|
||||
// Make the group invisible again
|
||||
db.setVisibility(txn, groupId, Collections.<ContactId>emptyList());
|
||||
db.removeVisibility(txn, contactId, groupId);
|
||||
assertEquals(Collections.emptyList(), db.getVisibility(txn, groupId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
@@ -1895,12 +1895,20 @@ public class H2DatabaseTest extends BriarTestCase {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the groups to the database
|
||||
// Subscribe to the groups and add a contact
|
||||
for(Group g : groups) db.addSubscription(txn, g);
|
||||
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
|
||||
|
||||
// Remove the groups in a different order
|
||||
// Make the groups visible to the contact
|
||||
Collections.shuffle(groups);
|
||||
for(Group g : groups) db.removeSubscription(txn, g.getId());
|
||||
for(Group g : groups) db.addVisibility(txn, contactId, g.getId());
|
||||
|
||||
// Make the groups invisible to the contact and remove them
|
||||
Collections.shuffle(groups);
|
||||
for(Group g : groups) {
|
||||
db.removeVisibility(txn, contactId, g.getId());
|
||||
db.removeSubscription(txn, g.getId());
|
||||
}
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
|
||||
Reference in New Issue
Block a user