Test that a max-length attachment fits into a record.

This commit is contained in:
akwizgran
2021-03-15 13:17:07 +00:00
parent 43740777d4
commit a9e83491d3

View File

@@ -1,10 +1,14 @@
package org.briarproject.briar.messaging; package org.briarproject.briar.messaging;
import org.briarproject.bramble.api.UniqueId; import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.crypto.CryptoComponent; import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.attachment.AttachmentHeader; import org.briarproject.briar.api.attachment.AttachmentHeader;
import org.briarproject.briar.api.forum.ForumPost; import org.briarproject.briar.api.forum.ForumPost;
@@ -14,6 +18,8 @@ import org.briarproject.briar.api.messaging.PrivateMessageFactory;
import org.briarproject.briar.test.BriarTestCase; import org.briarproject.briar.test.BriarTestCase;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -22,12 +28,16 @@ import javax.inject.Inject;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES; import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId; import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
import static org.briarproject.bramble.util.StringUtils.getRandomString; import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.attachment.MediaConstants.MAX_CONTENT_TYPE_BYTES; import static org.briarproject.briar.api.attachment.MediaConstants.MAX_CONTENT_TYPE_BYTES;
import static org.briarproject.briar.api.attachment.MediaConstants.MAX_IMAGE_SIZE;
import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_POST_TEXT_LENGTH; import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_POST_TEXT_LENGTH;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE; import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH; import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH;
import static org.briarproject.briar.messaging.MessageTypes.ATTACHMENT;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class MessageSizeIntegrationTest extends BriarTestCase { public class MessageSizeIntegrationTest extends BriarTestCase {
@@ -40,6 +50,10 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
PrivateMessageFactory privateMessageFactory; PrivateMessageFactory privateMessageFactory;
@Inject @Inject
ForumPostFactory forumPostFactory; ForumPostFactory forumPostFactory;
@Inject
ClientHelper clientHelper;
@Inject
MessageFactory messageFactory;
public MessageSizeIntegrationTest() { public MessageSizeIntegrationTest() {
MessageSizeIntegrationTestComponent component = MessageSizeIntegrationTestComponent component =
@@ -87,6 +101,32 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
assertTrue(length <= MAX_RECORD_PAYLOAD_BYTES); assertTrue(length <= MAX_RECORD_PAYLOAD_BYTES);
} }
@Test
public void testAttachmentFitsIntoRecord() throws Exception {
// Create a maximum-length attachment
String contentType = getRandomString(MAX_CONTENT_TYPE_BYTES);
byte[] data = getRandomBytes(MAX_IMAGE_SIZE);
ByteArrayInputStream dataIn = new ByteArrayInputStream(data);
ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
byte[] descriptor =
clientHelper.toByteArray(BdfList.of(ATTACHMENT, contentType));
bodyOut.write(descriptor);
copyAndClose(dataIn, bodyOut);
byte[] body = bodyOut.toByteArray();
GroupId groupId = new GroupId(getRandomId());
long timestamp = Long.MAX_VALUE;
Message message =
messageFactory.createMessage(groupId, timestamp, body);
// Check the size of the serialised message
int length = message.getRawLength();
assertTrue(length > UniqueId.LENGTH + 8
+ 1 + MAX_CONTENT_TYPE_BYTES + MAX_IMAGE_SIZE);
assertTrue(length <= MAX_RECORD_PAYLOAD_BYTES);
}
@Test @Test
public void testForumPostFitsIntoRecord() throws Exception { public void testForumPostFitsIntoRecord() throws Exception {
// Create a maximum-length author // Create a maximum-length author