Separate the sync layer from its clients. #112

This commit is contained in:
akwizgran
2015-12-21 14:36:24 +00:00
parent f5f572139a
commit 5355951466
117 changed files with 3160 additions and 3465 deletions

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.BriarTestCase;
import org.briarproject.TestMessage;
import org.briarproject.TestUtils;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportId;
@@ -10,8 +9,10 @@ import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.NoSuchLocalAuthorException;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.db.NoSuchTransportException;
import org.briarproject.api.event.ContactAddedEvent;
@@ -25,6 +26,7 @@ import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.MessageRequestedEvent;
import org.briarproject.api.event.MessageToAckEvent;
import org.briarproject.api.event.MessageToRequestEvent;
import org.briarproject.api.event.MessageValidatedEvent;
import org.briarproject.api.event.MessagesAckedEvent;
import org.briarproject.api.event.MessagesSentEvent;
import org.briarproject.api.event.SubscriptionAddedEvent;
@@ -34,6 +36,7 @@ import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.lifecycle.ShutdownManager;
import org.briarproject.api.sync.Ack;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
@@ -56,7 +59,7 @@ import java.util.Collection;
import java.util.Collections;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.sync.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -66,28 +69,31 @@ import static org.junit.Assert.fail;
public class DatabaseComponentImplTest extends BriarTestCase {
protected final Object txn = new Object();
protected final GroupId groupId;
protected final Group group;
protected final AuthorId authorId;
protected final Author author;
protected final AuthorId localAuthorId;
protected final LocalAuthor localAuthor;
protected final MessageId messageId, messageId1;
protected final String contentType;
protected final long timestamp;
protected final int size;
protected final byte[] raw;
protected final Message message, message1;
protected final TransportId transportId;
protected final TransportProperties transportProperties;
protected final int maxLatency;
protected final ContactId contactId;
protected final Contact contact;
private final Object txn = new Object();
private final ClientId clientId;
private final GroupId groupId;
private final Group group;
private final AuthorId authorId;
private final Author author;
private final AuthorId localAuthorId;
private final LocalAuthor localAuthor;
private final MessageId messageId, messageId1;
private final int size;
private final byte[] raw;
private final Message message;
private final Metadata metadata;
private final TransportId transportId;
private final TransportProperties transportProperties;
private final int maxLatency;
private final ContactId contactId;
private final Contact contact;
public DatabaseComponentImplTest() {
clientId = new ClientId(TestUtils.getRandomId());
groupId = new GroupId(TestUtils.getRandomId());
group = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH]);
ClientId clientId = new ClientId(TestUtils.getRandomId());
byte[] descriptor = new byte[MAX_GROUP_DESCRIPTOR_LENGTH];
group = new Group(groupId, clientId, descriptor);
authorId = new AuthorId(TestUtils.getRandomId());
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
localAuthorId = new AuthorId(TestUtils.getRandomId());
@@ -95,14 +101,12 @@ public class DatabaseComponentImplTest extends BriarTestCase {
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
messageId = new MessageId(TestUtils.getRandomId());
messageId1 = new MessageId(TestUtils.getRandomId());
contentType = "text/plain";
timestamp = System.currentTimeMillis();
long timestamp = System.currentTimeMillis();
size = 1234;
raw = new byte[size];
message = new TestMessage(messageId, null, group, author, contentType,
timestamp, raw);
message1 = new TestMessage(messageId1, messageId, group, null,
contentType, timestamp, raw);
message = new Message(messageId, groupId, timestamp, raw);
metadata = new Metadata();
metadata.put("foo", new byte[] {'b', 'a', 'r'});
transportId = new TransportId("id");
transportProperties = new TransportProperties(Collections.singletonMap(
"bar", "baz"));
@@ -125,9 +129,9 @@ public class DatabaseComponentImplTest extends BriarTestCase {
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final EventBus eventBus = context.mock(EventBus.class);
context.checking(new Expectations() {{
exactly(11).of(database).startTransaction();
exactly(10).of(database).startTransaction();
will(returnValue(txn));
exactly(11).of(database).commitTransaction(txn);
exactly(10).of(database).commitTransaction(txn);
// open()
oneOf(database).open();
will(returnValue(false));
@@ -161,11 +165,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
// addGroup() again
oneOf(database).containsGroup(txn, groupId);
will(returnValue(true));
// getMessageHeaders()
oneOf(database).containsGroup(txn, groupId);
will(returnValue(true));
oneOf(database).getMessageHeaders(txn, groupId);
will(returnValue(Collections.emptyList()));
// getGroups()
oneOf(database).getGroups(txn);
will(returnValue(Collections.singletonList(group)));
@@ -182,8 +181,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
// removeContact()
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getInboxGroupId(txn, contactId);
will(returnValue(null));
oneOf(database).removeContact(txn, contactId);
oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class)));
// removeLocalAuthor()
@@ -208,7 +205,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
db.getRemoteProperties(transportId));
db.addGroup(group); // First time - listeners called
db.addGroup(group); // Second time - not called
assertEquals(Collections.emptyList(), db.getMessageHeaders(groupId));
assertEquals(Collections.singletonList(group), db.getGroups());
db.removeGroup(group);
db.removeContact(contactId);
@@ -237,7 +233,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
db.addLocalMessage(message);
db.addLocalMessage(message, clientId, metadata);
context.assertIsSatisfied();
}
@@ -262,7 +258,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
db.addLocalMessage(message);
db.addLocalMessage(message, clientId, metadata);
context.assertIsSatisfied();
}
@@ -282,7 +278,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
oneOf(database).containsGroup(txn, groupId);
will(returnValue(true));
oneOf(database).addMessage(txn, message, true);
oneOf(database).setReadFlag(txn, messageId, true);
oneOf(database).mergeMessageMetadata(txn, messageId, metadata);
oneOf(database).getVisibility(txn, groupId);
will(returnValue(Collections.singletonList(contactId)));
oneOf(database).getContactIds(txn);
@@ -291,13 +287,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
will(returnValue(false));
oneOf(database).addStatus(txn, contactId, messageId, false, false);
oneOf(database).commitTransaction(txn);
// The message was added, so the listener should be called
// The message was added, so the listeners should be called
oneOf(eventBus).broadcast(with(any(MessageAddedEvent.class)));
oneOf(eventBus).broadcast(with(any(MessageValidatedEvent.class)));
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
db.addLocalMessage(message);
db.addLocalMessage(message, clientId, metadata);
context.assertIsSatisfied();
}
@@ -385,7 +382,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
}
try {
db.getInboxGroupId(contactId);
db.getMessageStatus(contactId, groupId);
fail();
} catch (NoSuchContactException expected) {
// Expected
}
try {
db.getMessageStatus(contactId, messageId);
fail();
} catch (NoSuchContactException expected) {
// Expected
@@ -469,13 +473,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
// Expected
}
try {
db.setInboxGroup(contactId, group);
fail();
} catch (NoSuchContactException expected) {
// Expected
}
context.assertIsSatisfied();
}
@@ -540,6 +537,9 @@ public class DatabaseComponentImplTest extends BriarTestCase {
exactly(5).of(database).containsGroup(txn, groupId);
will(returnValue(false));
exactly(5).of(database).abortTransaction(txn);
// This is needed for getMessageStatus() to proceed
exactly(1).of(database).containsContact(txn, contactId);
will(returnValue(true));
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
@@ -552,7 +552,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
}
try {
db.getMessageHeaders(groupId);
db.getMessageStatus(contactId, groupId);
fail();
} catch (NoSuchSubscriptionException expected) {
// Expected
@@ -582,6 +582,59 @@ public class DatabaseComponentImplTest extends BriarTestCase {
context.assertIsSatisfied();
}
@Test
public void testVariousMethodsThrowExceptionIfMessageIsMissing()
throws Exception {
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final EventBus eventBus = context.mock(EventBus.class);
context.checking(new Expectations() {{
// Check whether the message is in the DB (which it's not)
exactly(4).of(database).startTransaction();
will(returnValue(txn));
exactly(4).of(database).containsMessage(txn, messageId);
will(returnValue(false));
exactly(4).of(database).abortTransaction(txn);
// This is needed for getMessageStatus() to proceed
exactly(1).of(database).containsContact(txn, contactId);
will(returnValue(true));
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
try {
db.getRawMessage(messageId);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
try {
db.getMessageMetadata(messageId);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
try {
db.getMessageStatus(contactId, messageId);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
try {
db.mergeMessageMetadata(messageId, metadata);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
context.assertIsSatisfied();
}
@Test
public void testVariousMethodsThrowExceptionIfTransportIsMissing()
throws Exception {