Commit 27a169c6 authored by akwizgran's avatar akwizgran

Minimise use of message constructor.

parent d4a43517
......@@ -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);
}
......
......@@ -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();
}
};
......
......@@ -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);
......
......@@ -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);
......
......@@ -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),
......
......@@ -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);
......
......@@ -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),
......
......@@ -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);
......
......@@ -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);
......
......@@ -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 {
......
......@@ -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);
......
......@@ -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);