Minimise use of message constructor.

This commit is contained in:
akwizgran
2018-08-24 11:27:53 +01:00
parent d4a4351786
commit 27a169c6e2
18 changed files with 283 additions and 359 deletions

View File

@@ -40,6 +40,7 @@ public class TestUtils {
private static final AtomicInteger nextTestDir =
new AtomicInteger((int) (Math.random() * 1000 * 1000));
private static final Random random = new Random();
private static final long timestamp = System.currentTimeMillis();
public static File getTestDirectory() {
int name = nextTestDir.getAndIncrement();
@@ -101,9 +102,8 @@ public class TestUtils {
String name = getRandomString(nameLength);
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
byte[] privateKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
long created = System.currentTimeMillis();
return new LocalAuthor(id, FORMAT_VERSION, name, publicKey, privateKey,
created);
timestamp);
}
public static Author getAuthor() {
@@ -137,7 +137,6 @@ public class TestUtils {
public static Message getMessage(GroupId groupId, int rawLength) {
MessageId id = new MessageId(getRandomId());
byte[] raw = getRandomBytes(rawLength);
long timestamp = System.currentTimeMillis();
return new Message(id, groupId, timestamp, raw);
}

View File

@@ -18,6 +18,8 @@ import org.junit.Test;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
@@ -28,8 +30,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
new BdfMessageValidator(clientHelper, metadataEncoder, clock) {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body)
throws InvalidMessageException, FormatException {
BdfList body) {
throw new AssertionError();
}
};
@@ -69,7 +70,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
metadataEncoder, clock) {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList b) throws InvalidMessageException, FormatException {
BdfList b) {
assertSame(message, m);
assertSame(group, g);
assertSame(body, b);
@@ -83,11 +84,12 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
@Test(expected = InvalidMessageException.class)
public void testRejectsTooShortMessage() throws Exception {
byte[] invalidRaw = new byte[MESSAGE_HEADER_LENGTH];
byte[] invalidRaw = getRandomBytes(MESSAGE_HEADER_LENGTH);
// Use a mock message so the length of the raw message can be invalid
Message invalidMessage = context.mock(Message.class);
context.checking(new Expectations() {{
//noinspection ResultOfMethodCallIgnored
oneOf(invalidMessage).getTimestamp();
will(returnValue(timestamp));
oneOf(clock).currentTimeMillis();
@@ -101,15 +103,13 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
@Test
public void testAcceptsMinLengthMessage() throws Exception {
byte[] shortRaw = new byte[MESSAGE_HEADER_LENGTH + 1];
Message shortMessage =
new Message(messageId, groupId, timestamp, shortRaw);
Message shortMessage = getMessage(groupId, MESSAGE_HEADER_LENGTH + 1);
context.checking(new Expectations() {{
oneOf(clock).currentTimeMillis();
will(returnValue(timestamp));
oneOf(clientHelper).toList(shortRaw, MESSAGE_HEADER_LENGTH,
shortRaw.length - MESSAGE_HEADER_LENGTH);
oneOf(clientHelper).toList(shortMessage.getRaw(),
MESSAGE_HEADER_LENGTH, 1);
will(returnValue(body));
oneOf(metadataEncoder).encode(dictionary);
will(returnValue(meta));
@@ -120,7 +120,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
metadataEncoder, clock) {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList b) throws InvalidMessageException, FormatException {
BdfList b) {
assertSame(shortMessage, m);
assertSame(group, g);
assertSame(body, b);
@@ -160,7 +160,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
metadataEncoder, clock) {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList b) throws InvalidMessageException, FormatException {
BdfList b) throws FormatException {
throw new FormatException();
}
};

View File

@@ -39,6 +39,7 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
@@ -67,11 +68,10 @@ public class ClientHelperImplTest extends BrambleTestCase {
private final GroupId groupId = new GroupId(getRandomId());
private final BdfDictionary dictionary = new BdfDictionary();
private final long timestamp = 42L;
private final byte[] rawMessage = getRandomBytes(42);
private final MessageId messageId = new MessageId(getRandomId());
private final Message message =
new Message(messageId, groupId, timestamp, rawMessage);
private final Message message = getMessage(groupId);
private final MessageId messageId = message.getId();
private final long timestamp = message.getTimestamp();
private final byte[] rawMessage = message.getRaw();
private final Metadata metadata = new Metadata();
private final BdfList list = BdfList.of("Sign this!", getRandomBytes(42));
private final String label = StringUtils.getRandomString(5);

View File

@@ -64,6 +64,7 @@ import static java.util.Collections.singletonList;
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
import static org.briarproject.bramble.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
@@ -72,6 +73,7 @@ import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getClientId;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.test.TestUtils.getTransportId;
@@ -97,10 +99,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
private final Group group;
private final Author author;
private final LocalAuthor localAuthor;
private final MessageId messageId, messageId1;
private final int size;
private final byte[] raw;
private final Message message;
private final MessageId messageId, messageId1;
private final byte[] raw, raw1;
private final Metadata metadata;
private final TransportId transportId;
private final int maxLatency;
@@ -115,12 +116,12 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
groupId = group.getId();
author = getAuthor();
localAuthor = getLocalAuthor();
messageId = new MessageId(getRandomId());
messageId1 = new MessageId(getRandomId());
long timestamp = System.currentTimeMillis();
size = 1234;
raw = new byte[size];
message = new Message(messageId, groupId, timestamp, raw);
message = getMessage(groupId);
Message message1 = getMessage(groupId);
messageId = message.getId();
messageId1 = message1.getId();
raw = message.getRaw();
raw1 = message1.getRaw();
metadata = new Metadata();
metadata.put("foo", new byte[] {'b', 'a', 'r'});
transportId = getTransportId();
@@ -865,7 +866,6 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
@Test
public void testGenerateBatch() throws Exception {
byte[] raw1 = new byte[size];
Collection<MessageId> ids = Arrays.asList(messageId, messageId1);
Collection<byte[]> messages = Arrays.asList(raw, raw1);
context.checking(new Expectations() {{
@@ -873,7 +873,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
will(returnValue(txn));
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getMessagesToSend(txn, contactId, size * 2);
oneOf(database).getMessagesToSend(txn, contactId,
MAX_MESSAGE_LENGTH * 2);
will(returnValue(ids));
oneOf(database).getRawMessage(txn, messageId);
will(returnValue(raw));
@@ -893,7 +894,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
Transaction transaction = db.startTransaction(false);
try {
assertEquals(messages, db.generateBatch(transaction, contactId,
size * 2, maxLatency));
MAX_MESSAGE_LENGTH * 2, maxLatency));
db.commitTransaction(transaction);
} finally {
db.endTransaction(transaction);
@@ -961,7 +962,6 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
@Test
public void testGenerateRequestedBatch() throws Exception {
byte[] raw1 = new byte[size];
Collection<MessageId> ids = Arrays.asList(messageId, messageId1);
Collection<byte[]> messages = Arrays.asList(raw, raw1);
context.checking(new Expectations() {{
@@ -970,7 +970,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).getRequestedMessagesToSend(txn, contactId,
size * 2);
MAX_MESSAGE_LENGTH * 2);
will(returnValue(ids));
oneOf(database).getRawMessage(txn, messageId);
will(returnValue(raw));
@@ -990,7 +990,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
Transaction transaction = db.startTransaction(false);
try {
assertEquals(messages, db.generateRequestedBatch(transaction,
contactId, size * 2, maxLatency));
contactId, MAX_MESSAGE_LENGTH * 2, maxLatency));
db.commitTransaction(transaction);
} finally {
db.endTransaction(transaction);

View File

@@ -53,7 +53,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.INVALID;
import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING;
@@ -62,7 +62,7 @@ import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getClientId;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
@@ -89,11 +89,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
private final Group group;
private final Author author;
private final LocalAuthor localAuthor;
private final MessageId messageId;
private final long timestamp;
private final int size;
private final byte[] raw;
private final Message message;
private final MessageId messageId;
private final TransportId transportId;
private final ContactId contactId;
private final KeySetId keySetId, keySetId1;
@@ -106,11 +103,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
groupId = group.getId();
author = getAuthor();
localAuthor = getLocalAuthor();
messageId = new MessageId(getRandomId());
timestamp = System.currentTimeMillis();
size = 1234;
raw = getRandomBytes(size);
message = new Message(messageId, groupId, timestamp, raw);
message = getMessage(groupId);
messageId = message.getId();
transportId = getTransportId();
contactId = new ContactId(1);
keySetId = new KeySetId(1);
@@ -150,8 +144,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
assertTrue(db.containsContact(txn, contactId));
assertTrue(db.containsGroup(txn, groupId));
assertTrue(db.containsMessage(txn, messageId));
byte[] raw1 = db.getRawMessage(txn, messageId);
assertArrayEquals(raw, raw1);
assertArrayEquals(message.getRaw(), db.getRawMessage(txn, messageId));
// Delete the records
db.removeMessage(txn, messageId);
@@ -361,11 +354,11 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
// The message is sendable, but too large to send
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
size - 1);
message.getLength() - 1);
assertTrue(ids.isEmpty());
// The message is just the right size to send
ids = db.getMessagesToSend(txn, contactId, size);
ids = db.getMessagesToSend(txn, contactId, message.getLength());
assertEquals(singletonList(messageId), ids);
db.commitTransaction(txn);
@@ -385,8 +378,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
db.addGroupVisibility(txn, contactId, groupId, false);
// Add some messages to ack
MessageId messageId1 = new MessageId(getRandomId());
Message message1 = new Message(messageId1, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
MessageId messageId1 = message1.getId();
db.addMessage(txn, message, DELIVERED, true, contactId);
db.addMessage(txn, message1, DELIVERED, true, contactId);
@@ -449,9 +442,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testGetFreeSpace() throws Exception {
byte[] largeBody = new byte[MAX_MESSAGE_LENGTH];
for (int i = 0; i < largeBody.length; i++) largeBody[i] = (byte) i;
Message message = new Message(messageId, groupId, timestamp, largeBody);
Message message = getMessage(groupId, MAX_MESSAGE_BODY_LENGTH);
Database<Connection> db = open(false);
// Sanity check: there should be enough space on disk for this test
@@ -1105,8 +1096,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testMetadataQueries() throws Exception {
MessageId messageId1 = new MessageId(getRandomId());
Message message1 = new Message(messageId1, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
MessageId messageId1 = message1.getId();
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -1209,8 +1200,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testMetadataQueriesOnlyForDeliveredMessages() throws Exception {
MessageId messageId1 = new MessageId(getRandomId());
Message message1 = new Message(messageId1, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
MessageId messageId1 = message1.getId();
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -1280,14 +1271,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testMessageDependencies() throws Exception {
MessageId messageId1 = new MessageId(getRandomId());
MessageId messageId2 = new MessageId(getRandomId());
MessageId messageId3 = new MessageId(getRandomId());
MessageId messageId4 = new MessageId(getRandomId());
Message message1 = new Message(messageId1, groupId, timestamp, raw);
Message message2 = new Message(messageId2, groupId, timestamp, raw);
Message message3 = new Message(messageId3, groupId, timestamp, raw);
Message message4 = new Message(messageId4, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
Message message2 = getMessage(groupId);
Message message3 = getMessage(groupId);
Message message4 = getMessage(groupId);
MessageId messageId1 = message1.getId();
MessageId messageId2 = message2.getId();
MessageId messageId3 = message3.getId();
MessageId messageId4 = message4.getId();
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -1385,16 +1376,16 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
db.addGroup(txn, group1);
// Add a message to the second group
MessageId messageId1 = new MessageId(getRandomId());
Message message1 = new Message(messageId1, groupId1, timestamp, raw);
Message message1 = getMessage(groupId1);
MessageId messageId1 = message1.getId();
db.addMessage(txn, message1, DELIVERED, true, contactId);
// Create an ID for a missing message
MessageId messageId2 = new MessageId(getRandomId());
// Add another message to the first group
MessageId messageId3 = new MessageId(getRandomId());
Message message3 = new Message(messageId3, groupId, timestamp, raw);
Message message3 = getMessage(groupId);
MessageId messageId3 = message3.getId();
db.addMessage(txn, message3, DELIVERED, true, contactId);
// Add dependencies between the messages
@@ -1428,36 +1419,32 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testGetPendingMessagesForDelivery() throws Exception {
MessageId mId1 = new MessageId(getRandomId());
MessageId mId2 = new MessageId(getRandomId());
MessageId mId3 = new MessageId(getRandomId());
MessageId mId4 = new MessageId(getRandomId());
Message m1 = new Message(mId1, groupId, timestamp, raw);
Message m2 = new Message(mId2, groupId, timestamp, raw);
Message m3 = new Message(mId3, groupId, timestamp, raw);
Message m4 = new Message(mId4, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
Message message2 = getMessage(groupId);
Message message3 = getMessage(groupId);
Message message4 = getMessage(groupId);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a group and some messages with different states
db.addGroup(txn, group);
db.addMessage(txn, m1, UNKNOWN, true, contactId);
db.addMessage(txn, m2, INVALID, true, contactId);
db.addMessage(txn, m3, PENDING, true, contactId);
db.addMessage(txn, m4, DELIVERED, true, contactId);
db.addMessage(txn, message1, UNKNOWN, true, contactId);
db.addMessage(txn, message2, INVALID, true, contactId);
db.addMessage(txn, message3, PENDING, true, contactId);
db.addMessage(txn, message4, DELIVERED, true, contactId);
Collection<MessageId> result;
// Retrieve messages to be validated
result = db.getMessagesToValidate(txn);
assertEquals(1, result.size());
assertTrue(result.contains(mId1));
assertTrue(result.contains(message1.getId()));
// Retrieve pending messages
result = db.getPendingMessages(txn);
assertEquals(1, result.size());
assertTrue(result.contains(mId3));
assertTrue(result.contains(message3.getId()));
db.commitTransaction(txn);
db.close();
@@ -1465,35 +1452,31 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testGetMessagesToShare() throws Exception {
MessageId mId1 = new MessageId(getRandomId());
MessageId mId2 = new MessageId(getRandomId());
MessageId mId3 = new MessageId(getRandomId());
MessageId mId4 = new MessageId(getRandomId());
Message m1 = new Message(mId1, groupId, timestamp, raw);
Message m2 = new Message(mId2, groupId, timestamp, raw);
Message m3 = new Message(mId3, groupId, timestamp, raw);
Message m4 = new Message(mId4, groupId, timestamp, raw);
Message message1 = getMessage(groupId);
Message message2 = getMessage(groupId);
Message message3 = getMessage(groupId);
Message message4 = getMessage(groupId);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a group and some messages
db.addGroup(txn, group);
db.addMessage(txn, m1, DELIVERED, true, contactId);
db.addMessage(txn, m2, DELIVERED, false, contactId);
db.addMessage(txn, m3, DELIVERED, false, contactId);
db.addMessage(txn, m4, DELIVERED, true, contactId);
db.addMessage(txn, message1, DELIVERED, true, contactId);
db.addMessage(txn, message2, DELIVERED, false, contactId);
db.addMessage(txn, message3, DELIVERED, false, contactId);
db.addMessage(txn, message4, DELIVERED, true, contactId);
// Introduce dependencies between the messages
db.addMessageDependency(txn, m1, mId2, DELIVERED);
db.addMessageDependency(txn, m3, mId1, DELIVERED);
db.addMessageDependency(txn, m4, mId3, DELIVERED);
db.addMessageDependency(txn, message1, message2.getId(), DELIVERED);
db.addMessageDependency(txn, message3, message1.getId(), DELIVERED);
db.addMessageDependency(txn, message4, message3.getId(), DELIVERED);
// Retrieve messages to be shared
Collection<MessageId> result = db.getMessagesToShare(txn);
assertEquals(2, result.size());
assertTrue(result.contains(mId2));
assertTrue(result.contains(mId3));
assertTrue(result.contains(message2.getId()));
assertTrue(result.contains(message3.getId()));
db.commitTransaction(txn);
db.close();
@@ -1656,7 +1639,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
assertEquals(singletonList(messageId), ids);
// The raw message should be available
assertArrayEquals(raw, db.getRawMessage(txn, messageId));
assertArrayEquals(message.getRaw(), db.getRawMessage(txn, messageId));
// Delete the message
db.deleteMessage(txn, messageId);

View File

@@ -34,11 +34,10 @@ import static java.util.Collections.singletonList;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -187,8 +186,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
throws Exception {
Transaction txn = new Transaction(null, false);
GroupId contactGroupId = new GroupId(getRandomId());
long timestamp = 123456789;
Message message = getMessage(contactGroupId, timestamp);
Message message = getMessage(contactGroupId);
Metadata meta = new Metadata();
BdfDictionary metaDictionary = BdfDictionary.of(
new BdfEntry("transportId", "foo"),
@@ -229,8 +227,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
throws Exception {
Transaction txn = new Transaction(null, false);
GroupId contactGroupId = new GroupId(getRandomId());
long timestamp = 123456789;
Message message = getMessage(contactGroupId, timestamp);
Message message = getMessage(contactGroupId);
Metadata meta = new Metadata();
// Version 4 is being delivered
BdfDictionary metaDictionary = BdfDictionary.of(
@@ -267,8 +264,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
public void testDeletesObsoleteUpdateWhenDelivered() throws Exception {
Transaction txn = new Transaction(null, false);
GroupId contactGroupId = new GroupId(getRandomId());
long timestamp = 123456789;
Message message = getMessage(contactGroupId, timestamp);
Message message = getMessage(contactGroupId);
Metadata meta = new Metadata();
// Version 3 is being delivered
BdfDictionary metaDictionary = BdfDictionary.of(
@@ -619,12 +615,6 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
true, active);
}
private Message getMessage(GroupId g, long timestamp) {
MessageId messageId = new MessageId(getRandomId());
byte[] raw = getRandomBytes(MAX_MESSAGE_BODY_LENGTH);
return new Message(messageId, g, timestamp, raw);
}
private void expectGetLocalProperties(Transaction txn) throws Exception {
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
// The latest update for transport "foo" should be returned
@@ -664,9 +654,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
private void expectStoreMessage(Transaction txn, GroupId g,
String transportId, BdfDictionary properties, long version,
boolean local, boolean shared) throws Exception {
long timestamp = 123456789;
BdfList body = BdfList.of(transportId, version, properties);
Message message = getMessage(g, timestamp);
Message message = getMessage(g);
long timestamp = message.getTimestamp();
BdfDictionary meta = BdfDictionary.of(
new BdfEntry("transportId", transportId),
new BdfEntry("version", version),

View File

@@ -18,6 +18,7 @@ import java.util.Collections;
import java.util.concurrent.Executor;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
@@ -72,7 +73,7 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
@Test
public void testSomethingToSend() throws Exception {
Ack ack = new Ack(Collections.singletonList(messageId));
byte[] raw = new byte[1234];
byte[] raw = getRandomBytes(1234);
SimplexOutgoingSession session = new SimplexOutgoingSession(db,
dbExecutor, eventBus, contactId, MAX_LATENCY, streamWriter,
recordWriter);

View File

@@ -1,6 +1,5 @@
package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.Metadata;
@@ -21,23 +20,26 @@ import org.briarproject.bramble.api.sync.ValidationManager.State;
import org.briarproject.bramble.api.sync.event.MessageAddedEvent;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.bramble.util.ByteUtils;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.INVALID;
import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING;
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
import static org.briarproject.bramble.test.TestUtils.getClientId;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
public class ValidationManagerImplTest extends BrambleMockTestCase {
@@ -54,34 +56,23 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
private final Executor validationExecutor = new ImmediateExecutor();
private final ClientId clientId = getClientId();
private final int majorVersion = 123;
private final MessageId messageId = new MessageId(getRandomId());
private final MessageId messageId1 = new MessageId(getRandomId());
private final MessageId messageId2 = new MessageId(getRandomId());
private final Group group = getGroup(clientId, majorVersion);
private final GroupId groupId = group.getId();
private final long timestamp = System.currentTimeMillis();
private final byte[] raw = new byte[123];
private final Message message = new Message(messageId, groupId, timestamp,
raw);
private final Message message1 = new Message(messageId1, groupId, timestamp,
raw);
private final Message message2 = new Message(messageId2, groupId, timestamp,
raw);
private final Message message = getMessage(groupId);
private final Message message1 = getMessage(groupId);
private final Message message2 = getMessage(groupId);
private final MessageId messageId = message.getId();
private final MessageId messageId1 = message1.getId();
private final MessageId messageId2 = message2.getId();
private final Metadata metadata = new Metadata();
private final MessageContext validResult = new MessageContext(metadata);
private final ContactId contactId = new ContactId(234);
private final MessageContext validResultWithDependencies =
new MessageContext(metadata, Collections.singletonList(messageId1));
new MessageContext(metadata, singletonList(messageId1));
private ValidationManagerImpl vm;
public ValidationManagerImplTest() {
// Encode the messages
System.arraycopy(groupId.getBytes(), 0, raw, 0, UniqueId.LENGTH);
ByteUtils.writeUint64(timestamp, raw, UniqueId.LENGTH);
}
@Before
public void setUp() {
vm = new ValidationManagerImpl(db, dbExecutor, validationExecutor,
@@ -101,21 +92,21 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn));
oneOf(db).getMessagesToValidate(txn);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn);
// deliverOutstandingMessages()
oneOf(db).startTransaction(true);
will(returnValue(txn1));
oneOf(db).getPendingMessages(txn1);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
// shareOutstandingMessages()
oneOf(db).startTransaction(true);
will(returnValue(txn2));
oneOf(db).getMessagesToShare(txn2);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
}});
@@ -146,8 +137,8 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn1));
oneOf(db).getRawMessage(txn1, messageId);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId, raw);
will(returnValue(message.getRaw()));
oneOf(messageFactory).createMessage(messageId, message.getRaw());
will(returnValue(message));
oneOf(db).getGroup(txn1, groupId);
will(returnValue(group));
@@ -166,15 +157,15 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn2, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn2, messageId);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
// Load the second raw message and group
oneOf(db).startTransaction(true);
will(returnValue(txn3));
oneOf(db).getRawMessage(txn3, messageId1);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId1, raw);
will(returnValue(message1.getRaw()));
oneOf(messageFactory).createMessage(messageId1, message1.getRaw());
will(returnValue(message1));
oneOf(db).getGroup(txn3, groupId);
will(returnValue(group));
@@ -193,21 +184,21 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn4, messageId1);
// Recursively invalidate any dependents
oneOf(db).getMessageDependents(txn4, messageId1);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
// Get pending messages to deliver
oneOf(db).startTransaction(true);
will(returnValue(txn5));
oneOf(db).getPendingMessages(txn5);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn5);
oneOf(db).endTransaction(txn5);
// Get messages to share
oneOf(db).startTransaction(true);
will(returnValue(txn6));
oneOf(db).getMessagesToShare(txn6);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn6);
oneOf(db).endTransaction(txn6);
}});
@@ -228,14 +219,14 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn));
oneOf(db).getMessagesToValidate(txn);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn);
// Get pending messages to deliver
oneOf(db).startTransaction(true);
will(returnValue(txn1));
oneOf(db).getPendingMessages(txn1);
will(returnValue(Collections.singletonList(messageId)));
will(returnValue(singletonList(messageId)));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
// Check whether the message is ready to deliver
@@ -244,11 +235,11 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).getMessageState(txn2, messageId);
will(returnValue(PENDING));
oneOf(db).getMessageDependencies(txn2, messageId);
will(returnValue(Collections.singletonMap(messageId1, DELIVERED)));
will(returnValue(singletonMap(messageId1, DELIVERED)));
// Get the message and its metadata to deliver
oneOf(db).getRawMessage(txn2, messageId);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId, raw);
will(returnValue(message.getRaw()));
oneOf(messageFactory).createMessage(messageId, message.getRaw());
will(returnValue(message));
oneOf(db).getGroup(txn2, groupId);
will(returnValue(group));
@@ -260,7 +251,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn2, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn2, messageId);
will(returnValue(Collections.singletonMap(messageId2, PENDING)));
will(returnValue(singletonMap(messageId2, PENDING)));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
// Check whether the dependent is ready to deliver
@@ -269,11 +260,11 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).getMessageState(txn3, messageId2);
will(returnValue(PENDING));
oneOf(db).getMessageDependencies(txn3, messageId2);
will(returnValue(Collections.singletonMap(messageId1, DELIVERED)));
will(returnValue(singletonMap(messageId1, DELIVERED)));
// Get the dependent and its metadata to deliver
oneOf(db).getRawMessage(txn3, messageId2);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId2, raw);
will(returnValue(message2.getRaw()));
oneOf(messageFactory).createMessage(messageId2, message2.getRaw());
will(returnValue(message2));
oneOf(db).getGroup(txn3, groupId);
will(returnValue(group));
@@ -285,7 +276,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn3, messageId2, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn3, messageId2);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
@@ -293,7 +284,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn4));
oneOf(db).getMessagesToShare(txn4);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
}});
@@ -314,14 +305,14 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn));
oneOf(db).getMessagesToValidate(txn);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn);
// No pending messages to deliver
oneOf(db).startTransaction(true);
will(returnValue(txn1));
oneOf(db).getPendingMessages(txn1);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
@@ -329,7 +320,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn2));
oneOf(db).getMessagesToShare(txn2);
will(returnValue(Collections.singletonList(messageId)));
will(returnValue(singletonList(messageId)));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
// Share message and get dependencies
@@ -337,7 +328,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
will(returnValue(txn3));
oneOf(db).setMessageShared(txn3, messageId);
oneOf(db).getMessageDependencies(txn3, messageId);
will(returnValue(Collections.singletonMap(messageId2, DELIVERED)));
will(returnValue(singletonMap(messageId2, DELIVERED)));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
// Share dependency
@@ -345,7 +336,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
will(returnValue(txn4));
oneOf(db).setMessageShared(txn4, messageId2);
oneOf(db).getMessageDependencies(txn4, messageId2);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
}});
@@ -376,7 +367,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).addMessageDependencies(txn1, message,
validResultWithDependencies.getDependencies());
oneOf(db).getMessageDependencies(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId1, DELIVERED)));
will(returnValue(singletonMap(messageId1, DELIVERED)));
oneOf(db).mergeMessageMetadata(txn1, messageId, metadata);
// Deliver the message
oneOf(hook).incomingMessage(txn1, message, metadata);
@@ -384,7 +375,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn1, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn1, messageId);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
// Share message
oneOf(db).setMessageShared(txn1, messageId);
oneOf(db).commitTransaction(txn1);
@@ -394,7 +385,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
will(returnValue(txn2));
oneOf(db).setMessageShared(txn2, messageId1);
oneOf(db).getMessageDependencies(txn2, messageId1);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
}});
@@ -431,8 +422,8 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn2));
oneOf(db).getRawMessage(txn2, messageId1);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId1, raw);
will(returnValue(message1.getRaw()));
oneOf(messageFactory).createMessage(messageId1, message1.getRaw());
will(returnValue(message1));
oneOf(db).getGroup(txn2, groupId);
will(returnValue(group));
@@ -451,21 +442,21 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn3, messageId1);
// Recursively invalidate dependents
oneOf(db).getMessageDependents(txn3, messageId1);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
// Get pending messages to deliver
oneOf(db).startTransaction(true);
will(returnValue(txn4));
oneOf(db).getPendingMessages(txn4);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
// Get messages to share
oneOf(db).startTransaction(true);
will(returnValue(txn5));
oneOf(db).getMessagesToShare(txn5);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn5);
oneOf(db).endTransaction(txn5);
}});
@@ -495,8 +486,8 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn1));
oneOf(db).getRawMessage(txn1, messageId);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId, raw);
will(returnValue(message.getRaw()));
oneOf(messageFactory).createMessage(messageId, message.getRaw());
will(returnValue(message));
// Load the group - *gasp* it's gone!
oneOf(db).getGroup(txn1, groupId);
@@ -507,8 +498,8 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).startTransaction(true);
will(returnValue(txn2));
oneOf(db).getRawMessage(txn2, messageId1);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId1, raw);
will(returnValue(message1.getRaw()));
oneOf(messageFactory).createMessage(messageId1, message1.getRaw());
will(returnValue(message1));
oneOf(db).getGroup(txn2, groupId);
will(returnValue(group));
@@ -527,21 +518,21 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn3, messageId1);
// Recursively invalidate dependents
oneOf(db).getMessageDependents(txn3, messageId1);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
// Get pending messages to deliver
oneOf(db).startTransaction(true);
will(returnValue(txn4));
oneOf(db).getPendingMessages(txn4);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
// Get messages to share
oneOf(db).startTransaction(true);
will(returnValue(txn5));
oneOf(db).getMessagesToShare(txn5);
will(returnValue(Collections.emptyList()));
will(returnValue(emptyList()));
oneOf(db).commitTransaction(txn5);
oneOf(db).endTransaction(txn5);
}});
@@ -575,7 +566,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn1, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn1, messageId);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
}});
@@ -584,7 +575,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
}
@Test
public void testLocalMessagesAreNotValidatedWhenAdded() throws Exception {
public void testLocalMessagesAreNotValidatedWhenAdded() {
vm.eventOccurred(new MessageAddedEvent(message, null));
}
@@ -611,7 +602,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).addMessageDependencies(txn1, message,
validResultWithDependencies.getDependencies());
oneOf(db).getMessageDependencies(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId1, UNKNOWN)));
will(returnValue(singletonMap(messageId1, UNKNOWN)));
oneOf(db).mergeMessageMetadata(txn1, messageId, metadata);
oneOf(db).setMessageState(txn1, messageId, PENDING);
oneOf(db).commitTransaction(txn1);
@@ -644,7 +635,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).addMessageDependencies(txn1, message,
validResultWithDependencies.getDependencies());
oneOf(db).getMessageDependencies(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId1, DELIVERED)));
will(returnValue(singletonMap(messageId1, DELIVERED)));
oneOf(db).mergeMessageMetadata(txn1, messageId, metadata);
// Deliver the message
oneOf(hook).incomingMessage(txn1, message, metadata);
@@ -652,7 +643,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn1, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn1, messageId);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
}});
@@ -685,7 +676,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
validResultWithDependencies.getDependencies());
// Check for invalid dependencies
oneOf(db).getMessageDependencies(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId1, INVALID)));
will(returnValue(singletonMap(messageId1, INVALID)));
// Invalidate message
oneOf(db).getMessageState(txn1, messageId);
will(returnValue(UNKNOWN));
@@ -694,7 +685,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn1, messageId);
// Recursively invalidate dependents
oneOf(db).getMessageDependents(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId2, UNKNOWN)));
will(returnValue(singletonMap(messageId2, UNKNOWN)));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
// Invalidate dependent in a new transaction
@@ -706,7 +697,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessage(txn2, messageId2);
oneOf(db).deleteMessageMetadata(txn2, messageId2);
oneOf(db).getMessageDependents(txn2, messageId2);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
}});
@@ -763,7 +754,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn2, messageId1);
// Message 1 has one dependent: 3
oneOf(db).getMessageDependents(txn2, messageId1);
will(returnValue(Collections.singletonMap(messageId3, PENDING)));
will(returnValue(singletonMap(messageId3, PENDING)));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
// Invalidate message 2
@@ -776,7 +767,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn3, messageId2);
// Message 2 has one dependent: 3 (same dependent as 1)
oneOf(db).getMessageDependents(txn3, messageId2);
will(returnValue(Collections.singletonMap(messageId3, PENDING)));
will(returnValue(singletonMap(messageId3, PENDING)));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
// Invalidate message 3 (via 1)
@@ -789,7 +780,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn4, messageId3);
// Message 3 has one dependent: 4
oneOf(db).getMessageDependents(txn4, messageId3);
will(returnValue(Collections.singletonMap(messageId4, PENDING)));
will(returnValue(singletonMap(messageId4, PENDING)));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
// Invalidate message 3 (again, via 2)
@@ -809,7 +800,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).deleteMessageMetadata(txn6, messageId4);
// Message 4 has no dependents
oneOf(db).getMessageDependents(txn6, messageId4);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn6);
oneOf(db).endTransaction(txn6);
}});
@@ -819,12 +810,10 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
@Test
public void testPendingDependentsGetDelivered() throws Exception {
MessageId messageId3 = new MessageId(getRandomId());
MessageId messageId4 = new MessageId(getRandomId());
Message message3 = new Message(messageId3, groupId, timestamp,
raw);
Message message4 = new Message(messageId4, groupId, timestamp,
raw);
Message message3 = getMessage(groupId);
Message message4 = getMessage(groupId);
MessageId messageId3 = message3.getId();
MessageId messageId4 = message4.getId();
Map<MessageId, State> twoDependents = new LinkedHashMap<>();
twoDependents.put(messageId1, PENDING);
twoDependents.put(messageId2, PENDING);
@@ -869,11 +858,11 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).getMessageState(txn2, messageId1);
will(returnValue(PENDING));
oneOf(db).getMessageDependencies(txn2, messageId1);
will(returnValue(Collections.singletonMap(messageId, DELIVERED)));
will(returnValue(singletonMap(messageId, DELIVERED)));
// Get message 1 and its metadata
oneOf(db).getRawMessage(txn2, messageId1);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId1, raw);
will(returnValue(message1.getRaw()));
oneOf(messageFactory).createMessage(messageId1, message1.getRaw());
will(returnValue(message1));
oneOf(db).getGroup(txn2, groupId);
will(returnValue(group));
@@ -885,7 +874,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn2, messageId1, DELIVERED);
// Message 1 has one pending dependent: 3
oneOf(db).getMessageDependents(txn2, messageId1);
will(returnValue(Collections.singletonMap(messageId3, PENDING)));
will(returnValue(singletonMap(messageId3, PENDING)));
oneOf(db).commitTransaction(txn2);
oneOf(db).endTransaction(txn2);
// Check whether message 2 is ready to be delivered
@@ -894,11 +883,11 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).getMessageState(txn3, messageId2);
will(returnValue(PENDING));
oneOf(db).getMessageDependencies(txn3, messageId2);
will(returnValue(Collections.singletonMap(messageId, DELIVERED)));
will(returnValue(singletonMap(messageId, DELIVERED)));
// Get message 2 and its metadata
oneOf(db).getRawMessage(txn3, messageId2);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId2, raw);
will(returnValue(message2.getRaw()));
oneOf(messageFactory).createMessage(messageId2, message2.getRaw());
will(returnValue(message2));
oneOf(db).getGroup(txn3, groupId);
will(returnValue(group));
@@ -910,7 +899,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn3, messageId2, DELIVERED);
// Message 2 has one pending dependent: 3 (same dependent as 1)
oneOf(db).getMessageDependents(txn3, messageId2);
will(returnValue(Collections.singletonMap(messageId3, PENDING)));
will(returnValue(singletonMap(messageId3, PENDING)));
oneOf(db).commitTransaction(txn3);
oneOf(db).endTransaction(txn3);
// Check whether message 3 is ready to be delivered (via 1)
@@ -922,8 +911,8 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
will(returnValue(twoDependencies));
// Get message 3 and its metadata
oneOf(db).getRawMessage(txn4, messageId3);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId3, raw);
will(returnValue(message3.getRaw()));
oneOf(messageFactory).createMessage(messageId3, message3.getRaw());
will(returnValue(message3));
oneOf(db).getGroup(txn4, groupId);
will(returnValue(group));
@@ -934,7 +923,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn4, messageId3, DELIVERED);
// Message 3 has one pending dependent: 4
oneOf(db).getMessageDependents(txn4, messageId3);
will(returnValue(Collections.singletonMap(messageId4, PENDING)));
will(returnValue(singletonMap(messageId4, PENDING)));
oneOf(db).commitTransaction(txn4);
oneOf(db).endTransaction(txn4);
// Check whether message 3 is ready to be delivered (again, via 2)
@@ -950,11 +939,11 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).getMessageState(txn6, messageId4);
will(returnValue(PENDING));
oneOf(db).getMessageDependencies(txn6, messageId4);
will(returnValue(Collections.singletonMap(messageId3, DELIVERED)));
will(returnValue(singletonMap(messageId3, DELIVERED)));
// Get message 4 and its metadata
oneOf(db).getRawMessage(txn6, messageId4);
will(returnValue(raw));
oneOf(messageFactory).createMessage(messageId4, raw);
will(returnValue(message4.getRaw()));
oneOf(messageFactory).createMessage(messageId4, message4.getRaw());
will(returnValue(message4));
oneOf(db).getGroup(txn6, groupId);
will(returnValue(group));
@@ -966,7 +955,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn6, messageId4, DELIVERED);
// Message 4 has no pending dependents
oneOf(db).getMessageDependents(txn6, messageId4);
will(returnValue(Collections.emptyMap()));
will(returnValue(emptyMap()));
oneOf(db).commitTransaction(txn6);
oneOf(db).endTransaction(txn6);
}});
@@ -1004,7 +993,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
oneOf(db).setMessageState(txn1, messageId, DELIVERED);
// Get any pending dependents
oneOf(db).getMessageDependents(txn1, messageId);
will(returnValue(Collections.singletonMap(messageId1, PENDING)));
will(returnValue(singletonMap(messageId1, PENDING)));
oneOf(db).commitTransaction(txn1);
oneOf(db).endTransaction(txn1);
// Check whether the pending dependent is ready to be delivered

View File

@@ -32,10 +32,9 @@ import org.junit.Test;
import static org.briarproject.bramble.api.identity.Author.Status.NONE;
import static org.briarproject.bramble.api.identity.Author.Status.OURSELVES;
import static org.briarproject.bramble.api.identity.Author.Status.VERIFIED;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR;
@@ -75,9 +74,9 @@ public class BlogManagerImplTest extends BriarTestCase {
private final LocalAuthor localAuthor1, localAuthor2, rssLocalAuthor;
private final BdfList authorList1, authorList2, rssAuthorList;
private final Blog blog1, blog2, rssBlog;
private final long timestamp, timeReceived;
private final MessageId messageId, rssMessageId;
private final Message message, rssMessage;
private final MessageId messageId, rssMessageId;
private final long timestamp, timeReceived;
private final String comment;
public BlogManagerImplTest() {
@@ -94,14 +93,12 @@ public class BlogManagerImplTest extends BriarTestCase {
blog1 = createBlog(localAuthor1, false);
blog2 = createBlog(localAuthor2, false);
rssBlog = createBlog(rssLocalAuthor, true);
timestamp = System.currentTimeMillis();
message = getMessage(blog1.getId());
rssMessage = getMessage(rssBlog.getId());
messageId = message.getId();
rssMessageId = rssMessage.getId();
timestamp = message.getTimestamp();
timeReceived = timestamp + 1;
messageId = new MessageId(getRandomId());
rssMessageId = new MessageId(getRandomId());
message = new Message(messageId, blog1.getId(), timestamp,
getRandomBytes(MAX_MESSAGE_LENGTH));
rssMessage = new Message(rssMessageId, rssBlog.getId(), timestamp,
getRandomBytes(MAX_MESSAGE_LENGTH));
comment = getRandomString(MAX_BLOG_COMMENT_LENGTH);
}
@@ -372,9 +369,8 @@ public class BlogManagerImplTest extends BriarTestCase {
new BdfEntry(KEY_TIMESTAMP, timestamp),
new BdfEntry(KEY_TIME_RECEIVED, timeReceived)
);
MessageId commentId = new MessageId(getRandomId());
Message commentMsg = new Message(commentId, blog1.getId(),
timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message commentMsg = getMessage(blog1.getId());
MessageId commentId = commentMsg.getId();
BdfDictionary commentMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, COMMENT.getInt()),
new BdfEntry(KEY_COMMENT, comment),
@@ -457,9 +453,8 @@ public class BlogManagerImplTest extends BriarTestCase {
// The post was originally posted to blog 1, then reblogged to
// blog 2 with a comment
BdfList originalPostBody = BdfList.of("originalPostBody");
MessageId wrappedPostId = new MessageId(getRandomId());
Message wrappedPostMsg = new Message(wrappedPostId, blog2.getId(),
timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message wrappedPostMsg = getMessage(blog2.getId());
MessageId wrappedPostId = wrappedPostMsg.getId();
BdfDictionary wrappedPostMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, WRAPPED_POST.getInt()),
new BdfEntry(KEY_RSS_FEED, false),
@@ -468,9 +463,8 @@ public class BlogManagerImplTest extends BriarTestCase {
new BdfEntry(KEY_TIMESTAMP, timestamp),
new BdfEntry(KEY_TIME_RECEIVED, timeReceived)
);
MessageId commentId = new MessageId(getRandomId());
Message commentMsg = new Message(commentId, blog2.getId(),
timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message commentMsg = getMessage(blog2.getId());
MessageId commentId = commentMsg.getId();
BdfDictionary commentMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, COMMENT.getInt()),
new BdfEntry(KEY_COMMENT, comment),
@@ -568,9 +562,8 @@ public class BlogManagerImplTest extends BriarTestCase {
// The post was originally posted to the RSS blog, then reblogged to
// blog 1 with a comment
BdfList originalPostBody = BdfList.of("originalPostBody");
MessageId wrappedPostId = new MessageId(getRandomId());
Message wrappedPostMsg = new Message(wrappedPostId, blog1.getId(),
timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message wrappedPostMsg = getMessage(blog1.getId());
MessageId wrappedPostId = wrappedPostMsg.getId();
BdfDictionary wrappedPostMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, WRAPPED_POST.getInt()),
new BdfEntry(KEY_RSS_FEED, true),
@@ -579,9 +572,8 @@ public class BlogManagerImplTest extends BriarTestCase {
new BdfEntry(KEY_TIMESTAMP, timestamp),
new BdfEntry(KEY_TIME_RECEIVED, timeReceived)
);
MessageId commentId = new MessageId(getRandomId());
Message commentMsg = new Message(commentId, blog1.getId(),
timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message commentMsg = getMessage(blog1.getId());
MessageId commentId = commentMsg.getId();
BdfDictionary commentMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, COMMENT.getInt()),
new BdfEntry(KEY_COMMENT, comment),
@@ -681,9 +673,8 @@ public class BlogManagerImplTest extends BriarTestCase {
MessageId originalCommentId = new MessageId(getRandomId());
BdfList originalCommentBody = BdfList.of("originalCommentBody");
// The post and comment were reblogged to blog 2 with another comment
MessageId rewrappedPostId = new MessageId(getRandomId());
Message rewrappedPostMsg = new Message(rewrappedPostId,
blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message rewrappedPostMsg = getMessage(blog2.getId());
MessageId rewrappedPostId = rewrappedPostMsg.getId();
BdfDictionary rewrappedPostMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, WRAPPED_POST.getInt()),
new BdfEntry(KEY_RSS_FEED, true),
@@ -692,9 +683,8 @@ public class BlogManagerImplTest extends BriarTestCase {
new BdfEntry(KEY_TIMESTAMP, timestamp),
new BdfEntry(KEY_TIME_RECEIVED, timeReceived)
);
MessageId wrappedCommentId = new MessageId(getRandomId());
Message wrappedCommentMsg = new Message(wrappedCommentId,
blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message wrappedCommentMsg = getMessage(blog2.getId());
MessageId wrappedCommentId = wrappedCommentMsg.getId();
BdfDictionary wrappedCommentMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, WRAPPED_COMMENT.getInt()),
new BdfEntry(KEY_COMMENT, comment),
@@ -705,9 +695,8 @@ public class BlogManagerImplTest extends BriarTestCase {
new BdfEntry(KEY_TIME_RECEIVED, timeReceived)
);
String localComment = getRandomString(MAX_BLOG_COMMENT_LENGTH);
MessageId localCommentId = new MessageId(getRandomId());
Message localCommentMsg = new Message(localCommentId,
blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH));
Message localCommentMsg = getMessage(blog2.getId());
MessageId localCommentId = localCommentMsg.getId();
BdfDictionary localCommentMeta = BdfDictionary.of(
new BdfEntry(KEY_TYPE, COMMENT.getInt()),
new BdfEntry(KEY_COMMENT, localComment),

View File

@@ -25,6 +25,7 @@ import java.security.GeneralSecurityException;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
@@ -74,17 +75,12 @@ public class BlogPostValidatorTest extends BriarTestCase {
);
blog = new Blog(group, author, false);
rssBlog = new Blog(group, author, true);
MessageId messageId = new MessageId(getRandomId());
long timestamp = System.currentTimeMillis();
byte[] raw = getRandomBytes(123);
message = new Message(messageId, group.getId(), timestamp, raw);
message = getMessage(group.getId());
MetadataEncoder metadataEncoder = context.mock(MetadataEncoder.class);
Clock clock = new SystemClock();
validator =
new BlogPostValidator(groupFactory, messageFactory, blogFactory,
clientHelper, metadataEncoder, clock);
validator = new BlogPostValidator(groupFactory, messageFactory,
blogFactory, clientHelper, metadataEncoder, clock);
context.assertIsSatisfied();
}
@@ -117,8 +113,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
}
@Test(expected = FormatException.class)
public void testValidateBlogPostWithoutAttachments()
throws IOException, GeneralSecurityException {
public void testValidateBlogPostWithoutAttachments() throws IOException {
BdfList content = BdfList.of(null, null, body);
BdfList m = BdfList.of(POST.getInt(), content, null);
@@ -126,8 +121,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
}
@Test(expected = FormatException.class)
public void testValidateBlogPostWithoutSignature()
throws IOException, GeneralSecurityException {
public void testValidateBlogPostWithoutSignature() throws IOException {
BdfList content = BdfList.of(null, null, body, null);
BdfList m = BdfList.of(POST.getInt(), content, null);

View File

@@ -14,7 +14,6 @@ import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor;
@@ -39,8 +38,7 @@ import okhttp3.Dns;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEEDS;
import static org.briarproject.briar.api.feed.FeedManager.CLIENT_ID;
import static org.briarproject.briar.api.feed.FeedManager.MAJOR_VERSION;
@@ -109,8 +107,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
entry.setUpdatedDate(new Date());
entries.add(entry);
String body = "<p> (" + entry.getUpdatedDate().toString() + ")</p>";
Message msg = new Message(new MessageId(getRandomId()), blogGroupId, 0,
getRandomBytes(42));
Message msg = getMessage(blogGroupId);
BlogPost post = new BlogPost(msg, null, localAuthor);
context.checking(new Expectations() {{
@@ -118,9 +115,8 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
will(returnValue(txn));
oneOf(clock).currentTimeMillis();
will(returnValue(42L));
oneOf(blogPostFactory)
.createBlogPost(feed.getBlogId(), 42L, null, localAuthor,
body);
oneOf(blogPostFactory).createBlogPost(feed.getBlogId(), 42L, null,
localAuthor, body);
will(returnValue(post));
oneOf(blogManager).addLocalPost(txn, post);
oneOf(db).commitTransaction(txn);

View File

@@ -11,8 +11,11 @@ import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations;
import org.junit.Test;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.introduction.IntroductionConstants.MAX_REQUEST_MESSAGE_LENGTH;
@@ -27,9 +30,9 @@ public class MessageEncoderTest extends BrambleMockTestCase {
new MessageEncoderImpl(clientHelper, messageFactory);
private final GroupId groupId = new GroupId(getRandomId());
private final Message message = getMessage(groupId);
private final Message message = getMessage(groupId, MAX_MESSAGE_LENGTH);
private final long timestamp = message.getTimestamp();
private final byte[] body = message.getRaw();
private final byte[] body = getRandomBytes(MAX_MESSAGE_BODY_LENGTH);
private final Author author = getAuthor();
private final BdfList authorList = new BdfList();
private final String text = getRandomString(MAX_REQUEST_MESSAGE_LENGTH);
@@ -43,8 +46,8 @@ public class MessageEncoderTest extends BrambleMockTestCase {
expectCreateMessage(
BdfList.of(REQUEST.getValue(), null, authorList, text));
messageEncoder
.encodeRequestMessage(groupId, timestamp, null, author, text);
messageEncoder.encodeRequestMessage(groupId, timestamp, null,
author, text);
}
private void expectCreateMessage(BdfList bodyList) throws FormatException {

View File

@@ -44,7 +44,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
@Inject
ForumPostFactory forumPostFactory;
public MessageSizeIntegrationTest() throws Exception {
public MessageSizeIntegrationTest() {
MessageSizeIntegrationTestComponent component =
DaggerMessageSizeIntegrationTestComponent.builder().build();
component.inject(this);
@@ -60,9 +60,9 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
PrivateMessage message = privateMessageFactory.createPrivateMessage(
groupId, timestamp, body);
// Check the size of the serialised message
int length = message.getMessage().getRaw().length;
assertTrue(
length > UniqueId.LENGTH + 8 + MAX_PRIVATE_MESSAGE_BODY_LENGTH);
int length = message.getMessage().getLength();
assertTrue(length > UniqueId.LENGTH + 8
+ MAX_PRIVATE_MESSAGE_BODY_LENGTH);
assertTrue(length <= MAX_RECORD_PAYLOAD_BYTES);
}
@@ -83,7 +83,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
ForumPost post = forumPostFactory.createPost(groupId,
timestamp, parent, author, body);
// Check the size of the serialised message
int length = post.getMessage().getRaw().length;
int length = post.getMessage().getLength();
assertTrue(length > UniqueId.LENGTH + 8 + UniqueId.LENGTH + 4
+ MAX_AUTHOR_NAME_LENGTH + MAX_PUBLIC_KEY_LENGTH
+ MAX_FORUM_POST_BODY_LENGTH);

View File

@@ -29,6 +29,7 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATUR
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
@@ -44,94 +45,83 @@ import static org.briarproject.briar.privategroup.invitation.MessageType.JOIN;
import static org.briarproject.briar.privategroup.invitation.MessageType.LEAVE;
import static org.junit.Assert.assertEquals;
public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
protected final DatabaseComponent db =
context.mock(DatabaseComponent.class);
protected final ClientHelper clientHelper =
context.mock(ClientHelper.class);
protected final ClientVersioningManager clientVersioningManager =
final DatabaseComponent db = context.mock(DatabaseComponent.class);
final ClientHelper clientHelper = context.mock(ClientHelper.class);
final ClientVersioningManager clientVersioningManager =
context.mock(ClientVersioningManager.class);
protected final PrivateGroupFactory privateGroupFactory =
final PrivateGroupFactory privateGroupFactory =
context.mock(PrivateGroupFactory.class);
protected final PrivateGroupManager privateGroupManager =
final PrivateGroupManager privateGroupManager =
context.mock(PrivateGroupManager.class);
protected final MessageParser messageParser =
context.mock(MessageParser.class);
protected final GroupMessageFactory groupMessageFactory =
final MessageParser messageParser = context.mock(MessageParser.class);
final GroupMessageFactory groupMessageFactory =
context.mock(GroupMessageFactory.class);
protected final IdentityManager identityManager =
context.mock(IdentityManager.class);
protected final MessageEncoder messageEncoder =
context.mock(MessageEncoder.class);
protected final MessageTracker messageTracker =
context.mock(MessageTracker.class);
protected final Clock clock = context.mock(Clock.class);
final IdentityManager identityManager = context.mock(IdentityManager.class);
final MessageEncoder messageEncoder = context.mock(MessageEncoder.class);
final MessageTracker messageTracker = context.mock(MessageTracker.class);
final Clock clock = context.mock(Clock.class);
protected final Transaction txn = new Transaction(null, false);
protected final GroupId contactGroupId = new GroupId(getRandomId());
protected final Group privateGroupGroup =
getGroup(CLIENT_ID, MAJOR_VERSION);
protected final GroupId privateGroupId = privateGroupGroup.getId();
protected final Author author = getAuthor();
protected final PrivateGroup privateGroup =
new PrivateGroup(privateGroupGroup,
getRandomString(MAX_GROUP_NAME_LENGTH), author,
getRandomBytes(GROUP_SALT_LENGTH));
protected final byte[] signature = getRandomBytes(MAX_SIGNATURE_LENGTH);
protected final MessageId lastLocalMessageId = new MessageId(getRandomId());
protected final MessageId lastRemoteMessageId =
new MessageId(getRandomId());
protected final long localTimestamp = 3L;
protected final long inviteTimestamp = 6L;
protected final long messageTimestamp = inviteTimestamp + 1;
protected final MessageId messageId = new MessageId(getRandomId());
protected final Message message = new Message(messageId, contactGroupId,
messageTimestamp, getRandomBytes(42));
final Transaction txn = new Transaction(null, false);
final GroupId contactGroupId = new GroupId(getRandomId());
final Group privateGroupGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
final GroupId privateGroupId = privateGroupGroup.getId();
final Author author = getAuthor();
final PrivateGroup privateGroup = new PrivateGroup(privateGroupGroup,
getRandomString(MAX_GROUP_NAME_LENGTH), author,
getRandomBytes(GROUP_SALT_LENGTH));
final byte[] signature = getRandomBytes(MAX_SIGNATURE_LENGTH);
final MessageId lastLocalMessageId = new MessageId(getRandomId());
final MessageId lastRemoteMessageId = new MessageId(getRandomId());
final Message message = getMessage(contactGroupId);
final MessageId messageId = message.getId();
final long messageTimestamp = message.getTimestamp();
final long inviteTimestamp = messageTimestamp - 1;
final long localTimestamp = inviteTimestamp - 1;
private final BdfDictionary meta =
BdfDictionary.of(new BdfEntry("me", "ta"));
protected final ContactId contactId = new ContactId(5);
protected final Contact contact = new Contact(contactId, author,
final ContactId contactId = new ContactId(5);
final Contact contact = new Contact(contactId, author,
new AuthorId(getRandomId()), true, true);
protected final InviteMessage inviteMessage =
final InviteMessage inviteMessage =
new InviteMessage(new MessageId(getRandomId()), contactGroupId,
privateGroupId, 0L, privateGroup.getName(),
privateGroup.getCreator(), privateGroup.getSalt(),
getRandomString(MAX_GROUP_INVITATION_MSG_LENGTH),
signature);
protected final JoinMessage joinMessage =
final JoinMessage joinMessage =
new JoinMessage(new MessageId(getRandomId()), contactGroupId,
privateGroupId, 0L, lastRemoteMessageId);
protected final LeaveMessage leaveMessage =
final LeaveMessage leaveMessage =
new LeaveMessage(new MessageId(getRandomId()), contactGroupId,
privateGroupId, 0L, lastRemoteMessageId);
protected final AbortMessage abortMessage =
final AbortMessage abortMessage =
new AbortMessage(messageId, contactGroupId, privateGroupId,
inviteTimestamp + 1);
protected void assertSessionConstantsUnchanged(Session s1, Session s2) {
void assertSessionConstantsUnchanged(Session s1, Session s2) {
assertEquals(s1.getRole(), s2.getRole());
assertEquals(s1.getContactGroupId(), s2.getContactGroupId());
assertEquals(s1.getPrivateGroupId(), s2.getPrivateGroupId());
}
protected void assertSessionRecordedSentMessage(Session s) {
void assertSessionRecordedSentMessage(Session s) {
assertEquals(messageId, s.getLastLocalMessageId());
assertEquals(lastRemoteMessageId, s.getLastRemoteMessageId());
assertEquals(messageTimestamp, s.getLocalTimestamp());
assertEquals(inviteTimestamp, s.getInviteTimestamp());
}
protected void expectGetLocalTimestamp(long time) {
void expectGetLocalTimestamp(long time) {
context.checking(new Expectations() {{
oneOf(clock).currentTimeMillis();
will(returnValue(time));
}});
}
protected void expectSendInviteMessage(String msg)
throws Exception {
void expectSendInviteMessage(String msg) throws Exception {
context.checking(new Expectations() {{
oneOf(messageEncoder)
.encodeInviteMessage(contactGroupId, privateGroupId,
@@ -142,7 +132,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
expectSendMessage(INVITE, true);
}
protected void expectSendJoinMessage(JoinMessage m, boolean visible)
void expectSendJoinMessage(JoinMessage m, boolean visible)
throws Exception {
expectGetLocalTimestamp(messageTimestamp);
context.checking(new Expectations() {{
@@ -154,7 +144,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
expectSendMessage(JOIN, visible);
}
protected void expectSendLeaveMessage(boolean visible) throws Exception {
void expectSendLeaveMessage(boolean visible) throws Exception {
expectGetLocalTimestamp(messageTimestamp);
context.checking(new Expectations() {{
oneOf(messageEncoder)
@@ -165,7 +155,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
expectSendMessage(LEAVE, visible);
}
protected void expectSendAbortMessage() throws Exception {
void expectSendAbortMessage() throws Exception {
expectGetLocalTimestamp(messageTimestamp);
context.checking(new Expectations() {{
oneOf(messageEncoder)
@@ -186,8 +176,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
}});
}
protected void expectSetPrivateGroupVisibility(Visibility v)
throws Exception {
void expectSetPrivateGroupVisibility(Visibility v) throws Exception {
expectGetContactId();
context.checking(new Expectations() {{
oneOf(clientVersioningManager).getClientVisibility(txn, contactId,
@@ -197,7 +186,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
}});
}
protected void expectGetContactId() throws Exception {
void expectGetContactId() throws Exception {
BdfDictionary groupMeta = BdfDictionary
.of(new BdfEntry(GROUP_KEY_CONTACT_ID, contactId.getInt()));
context.checking(new Expectations() {{
@@ -207,8 +196,7 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
}});
}
protected void expectIsSubscribedPrivateGroup()
throws Exception {
void expectIsSubscribedPrivateGroup() throws Exception {
context.checking(new Expectations() {{
oneOf(db).containsGroup(txn, privateGroupId);
will(returnValue(true));
@@ -217,19 +205,17 @@ public abstract class AbstractProtocolEngineTest extends BrambleMockTestCase {
}});
}
protected void expectIsNotSubscribedPrivateGroup()
throws Exception {
void expectIsNotSubscribedPrivateGroup() throws Exception {
context.checking(new Expectations() {{
oneOf(db).containsGroup(txn, privateGroupId);
will(returnValue(false));
}});
}
protected void expectMarkMessageVisibleInUi(MessageId m, boolean visible)
throws Exception {
void expectMarkMessageVisibleInUi(MessageId m) throws Exception {
BdfDictionary d = new BdfDictionary();
context.checking(new Expectations() {{
oneOf(messageEncoder).setVisibleInUi(d, visible);
oneOf(messageEncoder).setVisibleInUi(d, true);
oneOf(clientHelper).mergeMessageMetadata(txn, m, d);
}});
}

View File

@@ -305,7 +305,7 @@ public class CreatorProtocolEngineTest extends AbstractProtocolEngineTest {
lastRemoteMessageId);
expectSendJoinMessage(properJoinMessage, false);
expectMarkMessageVisibleInUi(properJoinMessage.getId(), true);
expectMarkMessageVisibleInUi(properJoinMessage.getId());
context.checking(new Expectations() {{
oneOf(messageTracker)
.trackMessage(txn, contactGroupId, inviteTimestamp + 1,
@@ -394,7 +394,7 @@ public class CreatorProtocolEngineTest extends AbstractProtocolEngineTest {
lastRemoteMessageId);
CreatorSession session = getDefaultSession(INVITED);
expectMarkMessageVisibleInUi(properLeaveMessage.getId(), true);
expectMarkMessageVisibleInUi(properLeaveMessage.getId());
context.checking(new Expectations() {{
oneOf(messageTracker)
.trackMessage(txn, contactGroupId, inviteTimestamp + 1,
@@ -463,7 +463,7 @@ public class CreatorProtocolEngineTest extends AbstractProtocolEngineTest {
}
private void assertSessionAborted(CreatorSession oldSession,
CreatorSession newSession) throws Exception {
CreatorSession newSession) {
assertEquals(ERROR, newSession.getState());
assertSessionRecordedSentMessage(newSession);
assertSessionConstantsUnchanged(oldSession, newSession);

View File

@@ -46,7 +46,6 @@ import javax.annotation.Nullable;
import static junit.framework.TestCase.fail;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage;
@@ -106,9 +105,7 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
private final Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
private final Group privateGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
private final BdfDictionary meta = BdfDictionary.of(new BdfEntry("m", "e"));
private final Message message =
new Message(new MessageId(getRandomId()), contactGroup.getId(),
0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
private final Message message = getMessage(contactGroup.getId());
private final BdfList body = BdfList.of("body");
private final SessionId sessionId =
new SessionId(privateGroup.getId().getBytes());
@@ -725,13 +722,11 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
@Test
public void testGetInvitations() throws Exception {
BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u"));
MessageId messageId2 = new MessageId(TestUtils.getRandomId());
Message message2 = getMessage(contactGroup.getId());
BdfDictionary meta2 = BdfDictionary.of(new BdfEntry("m2", "e"));
Map<MessageId, BdfDictionary> results = new HashMap<>();
results.put(message.getId(), meta);
results.put(messageId2, meta2);
Message message2 = new Message(messageId2, contactGroup.getId(),
0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
results.put(message2.getId(), meta2);
long time1 = 1L, time2 = 2L;
String groupName = getRandomString(MAX_GROUP_NAME_LENGTH);
byte[] salt = getRandomBytes(GROUP_SALT_LENGTH);
@@ -766,7 +761,7 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
salt);
will(returnValue(pg));
// message 2
oneOf(messageParser).getInviteMessage(txn, messageId2);
oneOf(messageParser).getInviteMessage(txn, message2.getId());
will(returnValue(inviteMessage2));
oneOf(privateGroupFactory).createPrivateGroup(groupName, author,
salt);

View File

@@ -360,7 +360,7 @@ public class InviteeProtocolEngineTest extends AbstractProtocolEngineTest {
oneOf(db).getContact(txn, contactId);
will(returnValue(contact));
}});
expectMarkMessageVisibleInUi(properInviteMessage.getId(), true);
expectMarkMessageVisibleInUi(properInviteMessage.getId());
expectMarkMessageAvailableToAnswer(properInviteMessage.getId(), true);
context.checking(new Expectations() {{
oneOf(messageTracker).trackMessage(txn, contactGroupId,
@@ -766,7 +766,7 @@ public class InviteeProtocolEngineTest extends AbstractProtocolEngineTest {
}
private void assertSessionAborted(InviteeSession oldSession,
InviteeSession newSession) throws Exception {
InviteeSession newSession) {
assertEquals(ERROR, newSession.getState());
assertSessionRecordedSentMessage(newSession);
assertSessionConstantsUnchanged(oldSession, newSession);

View File

@@ -36,7 +36,7 @@ import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.briar.api.blog.BlogSharingManager.CLIENT_ID;
import static org.briarproject.briar.api.blog.BlogSharingManager.MAJOR_VERSION;
@@ -198,8 +198,7 @@ public class BlogSharingManagerImplTest extends BrambleMockTestCase {
throws Exception {
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
BdfDictionary sessionDict = new BdfDictionary();
Message message = new Message(new MessageId(getRandomId()),
contactGroup.getId(), 42L, getRandomBytes(1337));
Message message = getMessage(contactGroup.getId());
context.checking(new Expectations() {{
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
MAJOR_VERSION, contact);