From 52eb261a1132b405847b18348b59c61326578a61 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 11:49:49 +0000 Subject: [PATCH 1/6] Unit tests for PrivateMessageValidator. --- .../PrivateMessageValidatorTest.java | 108 +++++++++++++++++- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java index 28668520f..f73d02f96 100644 --- a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java @@ -1,14 +1,116 @@ package org.briarproject.messaging; import org.briarproject.BriarTestCase; +import org.briarproject.TestUtils; +import org.briarproject.api.FormatException; +import org.briarproject.api.clients.BdfMessageContext; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.data.BdfDictionary; +import org.briarproject.api.data.BdfList; +import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.sync.ClientId; +import org.briarproject.api.sync.Group; +import org.briarproject.api.sync.GroupId; +import org.briarproject.api.sync.Message; +import org.briarproject.api.sync.MessageId; +import org.briarproject.api.system.Clock; +import org.jmock.Mockery; +import org.junit.After; import org.junit.Test; -import static org.junit.Assert.fail; +import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; +import static org.briarproject.clients.BdfConstants.MSG_KEY_READ; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; public class PrivateMessageValidatorTest extends BriarTestCase { + private final Mockery context = new Mockery(); + private final ClientHelper clientHelper = context.mock(ClientHelper.class); + private final MetadataEncoder metadataEncoder = + context.mock(MetadataEncoder.class); + private final Clock clock = context.mock(Clock.class); + + private final MessageId messageId = new MessageId(TestUtils.getRandomId()); + private final GroupId groupId = new GroupId(TestUtils.getRandomId()); + private final long timestamp = 1234567890 * 1000L; + private final byte[] raw = TestUtils.getRandomBytes(123); + private final Message message = + new Message(messageId, groupId, timestamp, raw); + private final ClientId clientId = + new ClientId(TestUtils.getRandomString(123)); + private final byte[] descriptor = TestUtils.getRandomBytes(123); + private final Group group = new Group(groupId, clientId, descriptor); + + @After + public void checkExpectations() { + context.assertIsSatisfied(); + } + + @Test(expected = FormatException.class) + public void testRejectsEmptyBody() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, new BdfList()); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongBody() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of("", "")); + } + + @Test(expected = FormatException.class) + public void testRejectsNullContent() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of((String) null)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonStringContent() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of(123)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongContent() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + String content = + TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH + 1); + v.validateMessage(message, group, BdfList.of(content)); + } + @Test - public void testUnitTestsExist() { - fail(); // FIXME: Write tests + public void testAcceptsMaxLengthContent() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + String content = + TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH); + BdfMessageContext context = + v.validateMessage(message, group, BdfList.of(content)); + assertExpectedContext(context); + } + + @Test + public void testAcceptsEmptyContent() throws Exception { + PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext context = + v.validateMessage(message, group, BdfList.of("")); + assertExpectedContext(context); + } + + private void assertExpectedContext(BdfMessageContext context) + throws FormatException { + assertEquals(0, context.getDependencies().size()); + BdfDictionary meta = context.getDictionary(); + assertEquals(3, meta.size()); + assertEquals(Long.valueOf(timestamp), meta.getLong("timestamp")); + assertFalse(meta.getBoolean("local")); + assertFalse(meta.getBoolean(MSG_KEY_READ)); } } From 11fcad89c6ef53fc802f273cccd40ea8630b85d4 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 14:32:22 +0000 Subject: [PATCH 2/6] Unit tests for BdfMessageValidator. --- .../org/briarproject/BriarMockTestCase.java | 14 ++ .../org/briarproject/ValidatorTestCase.java | 23 +++ .../clients/BdfMessageValidatorTest.java | 189 ++++++++++++++++++ .../PrivateMessageValidatorTest.java | 52 ++--- 4 files changed, 240 insertions(+), 38 deletions(-) create mode 100644 briar-tests/src/org/briarproject/BriarMockTestCase.java create mode 100644 briar-tests/src/org/briarproject/ValidatorTestCase.java create mode 100644 briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java diff --git a/briar-tests/src/org/briarproject/BriarMockTestCase.java b/briar-tests/src/org/briarproject/BriarMockTestCase.java new file mode 100644 index 000000000..45b662013 --- /dev/null +++ b/briar-tests/src/org/briarproject/BriarMockTestCase.java @@ -0,0 +1,14 @@ +package org.briarproject; + +import org.jmock.Mockery; +import org.junit.After; + +public abstract class BriarMockTestCase extends BriarTestCase { + + protected final Mockery context = new Mockery(); + + @After + public void checkExpectations() { + context.assertIsSatisfied(); + } +} diff --git a/briar-tests/src/org/briarproject/ValidatorTestCase.java b/briar-tests/src/org/briarproject/ValidatorTestCase.java new file mode 100644 index 000000000..a340994ed --- /dev/null +++ b/briar-tests/src/org/briarproject/ValidatorTestCase.java @@ -0,0 +1,23 @@ +package org.briarproject; + +import org.briarproject.api.sync.ClientId; +import org.briarproject.api.sync.Group; +import org.briarproject.api.sync.GroupId; +import org.briarproject.api.sync.Message; +import org.briarproject.api.sync.MessageId; + +public abstract class ValidatorTestCase extends BriarMockTestCase { + + protected final MessageId messageId = + new MessageId(TestUtils.getRandomId()); + protected final GroupId groupId = new GroupId(TestUtils.getRandomId()); + protected final long timestamp = 1234567890 * 1000L; + protected final byte[] raw = TestUtils.getRandomBytes(123); + protected final Message message = + new Message(messageId, groupId, timestamp, raw); + protected final ClientId clientId = + new ClientId(TestUtils.getRandomString(123)); + protected final byte[] descriptor = TestUtils.getRandomBytes(123); + protected final Group group = new Group(groupId, clientId, descriptor); + +} diff --git a/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java new file mode 100644 index 000000000..8c567e295 --- /dev/null +++ b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java @@ -0,0 +1,189 @@ +package org.briarproject.clients; + +import org.briarproject.ValidatorTestCase; +import org.briarproject.api.FormatException; +import org.briarproject.api.clients.BdfMessageContext; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.data.BdfDictionary; +import org.briarproject.api.data.BdfList; +import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.db.Metadata; +import org.briarproject.api.sync.Group; +import org.briarproject.api.sync.InvalidMessageException; +import org.briarproject.api.sync.Message; +import org.briarproject.api.sync.MessageContext; +import org.briarproject.api.system.Clock; +import org.jmock.Expectations; +import org.jmock.lib.legacy.ClassImposteriser; +import org.junit.Test; + +import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; +import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +public class BdfMessageValidatorTest extends ValidatorTestCase { + + private final ClientHelper clientHelper = context.mock(ClientHelper.class); + private final MetadataEncoder metadataEncoder = + context.mock(MetadataEncoder.class); + private final Clock clock = context.mock(Clock.class); + + private final BdfList body = BdfList.of(123, 456); + private final BdfDictionary dictionary = new BdfDictionary(); + private final Metadata meta = new Metadata(); + + public BdfMessageValidatorTest() { + context.setImposteriser(ClassImposteriser.INSTANCE); + } + + @Test(expected = InvalidMessageException.class) + public void testRejectsFarFutureTimestamp() throws Exception { + context.checking(new Expectations() {{ + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp - MAX_CLOCK_DIFFERENCE - 1)); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + fail(); + return null; + } + }; + v.validateMessage(message, group); + } + + @Test + public void testAcceptsMaxTimestamp() throws Exception { + context.checking(new Expectations() {{ + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp - MAX_CLOCK_DIFFERENCE)); + oneOf(clientHelper).toList(raw, MESSAGE_HEADER_LENGTH, + raw.length - MESSAGE_HEADER_LENGTH); + will(returnValue(body)); + oneOf(metadataEncoder).encode(dictionary); + will(returnValue(meta)); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + assertSame(message, m); + assertSame(group, g); + assertSame(body, b); + return new BdfMessageContext(dictionary); + } + }; + MessageContext messageContext = v.validateMessage(message, group); + assertEquals(0, messageContext.getDependencies().size()); + assertSame(meta, messageContext.getMetadata()); + } + + @Test(expected = InvalidMessageException.class) + public void testRejectsTooShortMessage() throws Exception { + final byte[] invalidRaw = new byte[MESSAGE_HEADER_LENGTH]; + // Use a mock message so the length of the raw message can be invalid + final Message invalidMessage = context.mock(Message.class); + + context.checking(new Expectations() {{ + oneOf(invalidMessage).getTimestamp(); + will(returnValue(timestamp)); + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp)); + oneOf(invalidMessage).getRaw(); + will(returnValue(invalidRaw)); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + fail(); + return null; + } + }; + v.validateMessage(invalidMessage, group); + } + + @Test + public void testAcceptsMinLengthMessage() throws Exception { + final byte[] shortRaw = new byte[MESSAGE_HEADER_LENGTH + 1]; + final Message shortMessage = + new Message(messageId, groupId, timestamp, shortRaw); + + context.checking(new Expectations() {{ + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp)); + oneOf(clientHelper).toList(shortRaw, MESSAGE_HEADER_LENGTH, + shortRaw.length - MESSAGE_HEADER_LENGTH); + will(returnValue(body)); + oneOf(metadataEncoder).encode(dictionary); + will(returnValue(meta)); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + assertSame(shortMessage, m); + assertSame(group, g); + assertSame(body, b); + return new BdfMessageContext(dictionary); + } + }; + MessageContext messageContext = v.validateMessage(shortMessage, group); + assertEquals(0, messageContext.getDependencies().size()); + assertSame(meta, messageContext.getMetadata()); + } + + @Test(expected = InvalidMessageException.class) + public void testRejectsInvalidBdfList() throws Exception { + context.checking(new Expectations() {{ + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp)); + oneOf(clientHelper).toList(raw, MESSAGE_HEADER_LENGTH, + raw.length - MESSAGE_HEADER_LENGTH); + will(throwException(new FormatException())); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + fail(); + return null; + } + }; + v.validateMessage(message, group); + } + + @Test(expected = InvalidMessageException.class) + public void testRethrowsFormatExceptionFromSubclass() throws Exception { + context.checking(new Expectations() {{ + oneOf(clock).currentTimeMillis(); + will(returnValue(timestamp)); + oneOf(clientHelper).toList(raw, MESSAGE_HEADER_LENGTH, + raw.length - MESSAGE_HEADER_LENGTH); + will(returnValue(body)); + }}); + + BdfMessageValidator v = new BdfMessageValidator(clientHelper, + metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList b) throws InvalidMessageException, FormatException { + throw new FormatException(); + } + }; + v.validateMessage(message, group); + } +} diff --git a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java index f73d02f96..d9b62b5a3 100644 --- a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java @@ -1,21 +1,14 @@ package org.briarproject.messaging; -import org.briarproject.BriarTestCase; import org.briarproject.TestUtils; +import org.briarproject.ValidatorTestCase; import org.briarproject.api.FormatException; import org.briarproject.api.clients.BdfMessageContext; import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; import org.briarproject.api.data.MetadataEncoder; -import org.briarproject.api.sync.ClientId; -import org.briarproject.api.sync.Group; -import org.briarproject.api.sync.GroupId; -import org.briarproject.api.sync.Message; -import org.briarproject.api.sync.MessageId; import org.briarproject.api.system.Clock; -import org.jmock.Mockery; -import org.junit.After; import org.junit.Test; import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; @@ -23,32 +16,15 @@ import static org.briarproject.clients.BdfConstants.MSG_KEY_READ; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -public class PrivateMessageValidatorTest extends BriarTestCase { +public class PrivateMessageValidatorTest extends ValidatorTestCase { - private final Mockery context = new Mockery(); private final ClientHelper clientHelper = context.mock(ClientHelper.class); private final MetadataEncoder metadataEncoder = context.mock(MetadataEncoder.class); private final Clock clock = context.mock(Clock.class); - private final MessageId messageId = new MessageId(TestUtils.getRandomId()); - private final GroupId groupId = new GroupId(TestUtils.getRandomId()); - private final long timestamp = 1234567890 * 1000L; - private final byte[] raw = TestUtils.getRandomBytes(123); - private final Message message = - new Message(messageId, groupId, timestamp, raw); - private final ClientId clientId = - new ClientId(TestUtils.getRandomString(123)); - private final byte[] descriptor = TestUtils.getRandomBytes(123); - private final Group group = new Group(groupId, clientId, descriptor); - - @After - public void checkExpectations() { - context.assertIsSatisfied(); - } - @Test(expected = FormatException.class) - public void testRejectsEmptyBody() throws Exception { + public void testRejectsTooShortBody() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, metadataEncoder, clock); v.validateMessage(message, group, new BdfList()); @@ -58,7 +34,7 @@ public class PrivateMessageValidatorTest extends BriarTestCase { public void testRejectsTooLongBody() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, metadataEncoder, clock); - v.validateMessage(message, group, BdfList.of("", "")); + v.validateMessage(message, group, BdfList.of(1, 2)); } @Test(expected = FormatException.class) @@ -79,9 +55,9 @@ public class PrivateMessageValidatorTest extends BriarTestCase { public void testRejectsTooLongContent() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, metadataEncoder, clock); - String content = + String invalidContent = TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH + 1); - v.validateMessage(message, group, BdfList.of(content)); + v.validateMessage(message, group, BdfList.of(invalidContent)); } @Test @@ -90,27 +66,27 @@ public class PrivateMessageValidatorTest extends BriarTestCase { metadataEncoder, clock); String content = TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH); - BdfMessageContext context = + BdfMessageContext messageContext = v.validateMessage(message, group, BdfList.of(content)); - assertExpectedContext(context); + assertExpectedContext(messageContext); } @Test - public void testAcceptsEmptyContent() throws Exception { + public void testAcceptsMinLengthContent() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, metadataEncoder, clock); - BdfMessageContext context = + BdfMessageContext messageContext = v.validateMessage(message, group, BdfList.of("")); - assertExpectedContext(context); + assertExpectedContext(messageContext); } - private void assertExpectedContext(BdfMessageContext context) + private void assertExpectedContext(BdfMessageContext messageContext) throws FormatException { - assertEquals(0, context.getDependencies().size()); - BdfDictionary meta = context.getDictionary(); + BdfDictionary meta = messageContext.getDictionary(); assertEquals(3, meta.size()); assertEquals(Long.valueOf(timestamp), meta.getLong("timestamp")); assertFalse(meta.getBoolean("local")); assertFalse(meta.getBoolean(MSG_KEY_READ)); + assertEquals(0, messageContext.getDependencies().size()); } } From bd3bba6e8a7ae8977cbdc4102a4c85f254a59cce Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 14:32:45 +0000 Subject: [PATCH 3/6] Unit tests for ForumPostValidator. --- .../forum/ForumPostValidatorTest.java | 395 +++++++++++++++++- 1 file changed, 390 insertions(+), 5 deletions(-) diff --git a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java index 089cce54d..a6707b526 100644 --- a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java +++ b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java @@ -1,14 +1,399 @@ package org.briarproject.forum; -import org.briarproject.BriarTestCase; +import org.briarproject.TestUtils; +import org.briarproject.ValidatorTestCase; +import org.briarproject.api.FormatException; +import org.briarproject.api.UniqueId; +import org.briarproject.api.clients.BdfMessageContext; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.data.BdfDictionary; +import org.briarproject.api.data.BdfList; +import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.identity.Author; +import org.briarproject.api.identity.AuthorFactory; +import org.briarproject.api.identity.AuthorId; +import org.briarproject.api.sync.InvalidMessageException; +import org.briarproject.api.sync.MessageId; +import org.briarproject.api.system.Clock; +import org.jmock.Expectations; import org.junit.Test; -import static org.junit.Assert.fail; +import java.security.GeneralSecurityException; +import java.util.Collection; -public class ForumPostValidatorTest extends BriarTestCase { +import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH; +import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; +import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; +import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class ForumPostValidatorTest extends ValidatorTestCase { + + private final AuthorFactory authorFactory = + context.mock(AuthorFactory.class); + private final ClientHelper clientHelper = context.mock(ClientHelper.class); + private final MetadataEncoder metadataEncoder = + context.mock(MetadataEncoder.class); + private final Clock clock = context.mock(Clock.class); + + private final MessageId parentId = new MessageId(TestUtils.getRandomId()); + private final String authorName = + TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH); + private final byte[] authorPublicKey = + TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH); + private final BdfList authorList = BdfList.of(authorName, authorPublicKey); + private final String content = + TestUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH); + private final byte[] signature = + TestUtils.getRandomBytes(MAX_SIGNATURE_LENGTH); + private final AuthorId authorId = new AuthorId(TestUtils.getRandomId()); + private final Author author = + new Author(authorId, authorName, authorPublicKey); + private final BdfList signedWithParent = BdfList.of(groupId, timestamp, + parentId.getBytes(), authorList, content); + private final BdfList signedWithoutParent = BdfList.of(groupId, timestamp, + null, authorList, content); + + @Test(expected = FormatException.class) + public void testRejectsTooShortBody() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of(1, 2, 3)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongBody() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of(1, 2, 3, 4, 5)); + } @Test - public void testUnitTestsExist() { - fail(); // FIXME: Write tests + public void testAcceptsNullParentId() throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + oneOf(clientHelper).verifySignature(signature, authorPublicKey, + signedWithoutParent); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(null, authorList, content, signature)); + assertExpectedContext(messageContext, false, authorName); + } + + @Test(expected = FormatException.class) + public void testRejectsNonRawParentId() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(123, authorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortParentId() throws Exception { + byte[] invalidParentId = new byte[UniqueId.LENGTH - 1]; + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(invalidParentId, authorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongParentId() throws Exception { + byte[] invalidParentId = new byte[UniqueId.LENGTH + 1]; + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(invalidParentId, authorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullAuthorList() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, null, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonListAuthorList() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, 123, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortAuthorList() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, new BdfList(), content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongAuthorList() throws Exception { + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, BdfList.of(1, 2, 3), content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullAuthorName() throws Exception { + BdfList invalidAuthorList = BdfList.of(null, authorPublicKey); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonStringAuthorName() throws Exception { + BdfList invalidAuthorList = BdfList.of(123, authorPublicKey); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortAuthorName() throws Exception { + BdfList invalidAuthorList = BdfList.of("", authorPublicKey); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test + public void testAcceptsMinLengthAuthorName() throws Exception { + final String shortAuthorName = "a"; + BdfList shortNameAuthorList = + BdfList.of(shortAuthorName, authorPublicKey); + final Author shortNameAuthor = + new Author(authorId, shortAuthorName, authorPublicKey); + final BdfList signedWithShortNameAuthor = BdfList.of(groupId, timestamp, + parentId.getBytes(), shortNameAuthorList, content); + + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(shortAuthorName, authorPublicKey); + will(returnValue(shortNameAuthor)); + oneOf(clientHelper).verifySignature(signature, authorPublicKey, + signedWithShortNameAuthor); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(parentId, shortNameAuthorList, content, signature)); + assertExpectedContext(messageContext, true, shortAuthorName); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongAuthorName() throws Exception { + String invalidAuthorName = + TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH + 1); + BdfList invalidAuthorList = + BdfList.of(invalidAuthorName, authorPublicKey); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullAuthorPublicKey() throws Exception { + BdfList invalidAuthorList = BdfList.of(authorName, null); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonRawAuthorPublicKey() throws Exception { + BdfList invalidAuthorList = BdfList.of(authorName, 123); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongAuthorPublicKey() throws Exception { + byte[] invalidAuthorPublicKey = + TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH + 1); + BdfList invalidAuthorList = + BdfList.of(authorName, invalidAuthorPublicKey); + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, invalidAuthorList, content, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullContent() throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, null, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonStringContent() throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, 123, signature)); + } + + @Test + public void testAcceptsMinLengthContent() throws Exception { + String shortContent = ""; + final BdfList signedWithShortContent = BdfList.of(groupId, timestamp, + parentId.getBytes(), authorList, shortContent); + + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + oneOf(clientHelper).verifySignature(signature, authorPublicKey, + signedWithShortContent); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(parentId, authorList, shortContent, signature)); + assertExpectedContext(messageContext, true, authorName); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongContent() throws Exception { + String invalidContent = + TestUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH + 1); + + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, invalidContent, signature)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullSignature() throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, null)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonRawSignature() throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, 123)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongSignature() throws Exception { + byte[] invalidSignature = + TestUtils.getRandomBytes(MAX_SIGNATURE_LENGTH + 1); + + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, invalidSignature)); + } + + @Test(expected = FormatException.class) + public void testRejectsIfVerifyingSignatureThrowsFormatException() + throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + oneOf(clientHelper).verifySignature(signature, authorPublicKey, + signedWithParent); + will(throwException(new FormatException())); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, signature)); + } + + @Test(expected = InvalidMessageException.class) + public void testRejectsIfVerifyingSignatureThrowsGeneralSecurityException() + throws Exception { + context.checking(new Expectations() {{ + oneOf(authorFactory).createAuthor(authorName, authorPublicKey); + will(returnValue(author)); + oneOf(clientHelper).verifySignature(signature, authorPublicKey, + signedWithParent); + will(throwException(new GeneralSecurityException())); + }}); + + ForumPostValidator v = new ForumPostValidator(authorFactory, + clientHelper, metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, signature)); + } + + private void assertExpectedContext(BdfMessageContext messageContext, + boolean hasParent, String authorName) throws FormatException { + BdfDictionary meta = messageContext.getDictionary(); + Collection dependencies = messageContext.getDependencies(); + if (hasParent) { + assertEquals(4, meta.size()); + assertArrayEquals(parentId.getBytes(), meta.getRaw("parent")); + assertEquals(1, dependencies.size()); + assertEquals(parentId, dependencies.iterator().next()); + } else { + assertEquals(3, meta.size()); + assertEquals(0, dependencies.size()); + } + assertEquals(Long.valueOf(timestamp), meta.getLong("timestamp")); + assertFalse(meta.getBoolean("read")); + BdfDictionary authorMeta = meta.getDictionary("author"); + assertEquals(3, authorMeta.size()); + assertArrayEquals(authorId.getBytes(), authorMeta.getRaw("id")); + assertEquals(authorName, authorMeta.getString("name")); + assertArrayEquals(authorPublicKey, authorMeta.getRaw("publicKey")); } } From 8c3b598ab2f6e774061038dfe7355812bfb63d6a Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 15:35:27 +0000 Subject: [PATCH 4/6] Unit tests for ForumSharingValidator. --- .../forum/ForumPostValidatorTest.java | 14 +- .../forum/ForumSharingValidatorTest.java | 14 - .../PrivateMessageValidatorTest.java | 4 +- .../sharing/ForumSharingValidatorTest.java | 351 ++++++++++++++++++ 4 files changed, 361 insertions(+), 22 deletions(-) delete mode 100644 briar-tests/src/org/briarproject/forum/ForumSharingValidatorTest.java create mode 100644 briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java diff --git a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java index a6707b526..512b6f3d1 100644 --- a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java +++ b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java @@ -60,14 +60,16 @@ public class ForumPostValidatorTest extends ValidatorTestCase { public void testRejectsTooShortBody() throws Exception { ForumPostValidator v = new ForumPostValidator(authorFactory, clientHelper, metadataEncoder, clock); - v.validateMessage(message, group, BdfList.of(1, 2, 3)); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content)); } @Test(expected = FormatException.class) public void testRejectsTooLongBody() throws Exception { ForumPostValidator v = new ForumPostValidator(authorFactory, clientHelper, metadataEncoder, clock); - v.validateMessage(message, group, BdfList.of(1, 2, 3, 4, 5)); + v.validateMessage(message, group, + BdfList.of(parentId, authorList, content, signature, 123)); } @Test @@ -96,7 +98,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase { @Test(expected = FormatException.class) public void testRejectsTooShortParentId() throws Exception { - byte[] invalidParentId = new byte[UniqueId.LENGTH - 1]; + byte[] invalidParentId = TestUtils.getRandomBytes(UniqueId.LENGTH - 1); ForumPostValidator v = new ForumPostValidator(authorFactory, clientHelper, metadataEncoder, clock); v.validateMessage(message, group, @@ -105,7 +107,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase { @Test(expected = FormatException.class) public void testRejectsTooLongParentId() throws Exception { - byte[] invalidParentId = new byte[UniqueId.LENGTH + 1]; + byte[] invalidParentId = TestUtils.getRandomBytes(UniqueId.LENGTH + 1); ForumPostValidator v = new ForumPostValidator(authorFactory, clientHelper, metadataEncoder, clock); v.validateMessage(message, group, @@ -173,7 +175,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase { @Test public void testAcceptsMinLengthAuthorName() throws Exception { - final String shortAuthorName = "a"; + final String shortAuthorName = TestUtils.getRandomString(1); BdfList shortNameAuthorList = BdfList.of(shortAuthorName, authorPublicKey); final Author shortNameAuthor = @@ -388,7 +390,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase { assertEquals(3, meta.size()); assertEquals(0, dependencies.size()); } - assertEquals(Long.valueOf(timestamp), meta.getLong("timestamp")); + assertEquals(timestamp, meta.getLong("timestamp").longValue()); assertFalse(meta.getBoolean("read")); BdfDictionary authorMeta = meta.getDictionary("author"); assertEquals(3, authorMeta.size()); diff --git a/briar-tests/src/org/briarproject/forum/ForumSharingValidatorTest.java b/briar-tests/src/org/briarproject/forum/ForumSharingValidatorTest.java deleted file mode 100644 index 0b87d04c1..000000000 --- a/briar-tests/src/org/briarproject/forum/ForumSharingValidatorTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.briarproject.forum; - -import org.briarproject.BriarTestCase; -import org.junit.Test; - -import static org.junit.Assert.fail; - -public class ForumSharingValidatorTest extends BriarTestCase { - - @Test - public void testUnitTestsExist() { - fail(); // FIXME: Write tests - } -} diff --git a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java index d9b62b5a3..fd99b31bb 100644 --- a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java @@ -34,7 +34,7 @@ public class PrivateMessageValidatorTest extends ValidatorTestCase { public void testRejectsTooLongBody() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, metadataEncoder, clock); - v.validateMessage(message, group, BdfList.of(1, 2)); + v.validateMessage(message, group, BdfList.of("", 123)); } @Test(expected = FormatException.class) @@ -84,7 +84,7 @@ public class PrivateMessageValidatorTest extends ValidatorTestCase { throws FormatException { BdfDictionary meta = messageContext.getDictionary(); assertEquals(3, meta.size()); - assertEquals(Long.valueOf(timestamp), meta.getLong("timestamp")); + assertEquals(timestamp, meta.getLong("timestamp").longValue()); assertFalse(meta.getBoolean("local")); assertFalse(meta.getBoolean(MSG_KEY_READ)); assertEquals(0, messageContext.getDependencies().size()); diff --git a/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java b/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java new file mode 100644 index 000000000..099fa2632 --- /dev/null +++ b/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java @@ -0,0 +1,351 @@ +package org.briarproject.sharing; + +import org.briarproject.TestUtils; +import org.briarproject.ValidatorTestCase; +import org.briarproject.api.FormatException; +import org.briarproject.api.UniqueId; +import org.briarproject.api.clients.BdfMessageContext; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.clients.SessionId; +import org.briarproject.api.data.BdfDictionary; +import org.briarproject.api.data.BdfList; +import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.system.Clock; +import org.junit.Test; + +import javax.annotation.Nullable; + +import static org.briarproject.api.forum.ForumConstants.FORUM_NAME; +import static org.briarproject.api.forum.ForumConstants.FORUM_SALT; +import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH; +import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH; +import static org.briarproject.api.sharing.SharingConstants.INVITATION_MSG; +import static org.briarproject.api.sharing.SharingConstants.LOCAL; +import static org.briarproject.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH; +import static org.briarproject.api.sharing.SharingConstants.SESSION_ID; +import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ABORT; +import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ACCEPT; +import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_DECLINE; +import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_INVITATION; +import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_LEAVE; +import static org.briarproject.api.sharing.SharingConstants.TIME; +import static org.briarproject.api.sharing.SharingConstants.TYPE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class ForumSharingValidatorTest extends ValidatorTestCase { + + private final ClientHelper clientHelper = context.mock(ClientHelper.class); + private final MetadataEncoder metadataEncoder = + context.mock(MetadataEncoder.class); + private final Clock clock = context.mock(Clock.class); + + private final SessionId sessionId = new SessionId(TestUtils.getRandomId()); + private final String forumName = + TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH); + private final byte[] salt = TestUtils.getRandomBytes(FORUM_SALT_LENGTH); + private final String content = + TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH); + + @Test + public void testAcceptsInvitationWithContent() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, content)); + assertExpectedContextForInvitation(messageContext, forumName, content); + } + + @Test + public void testAcceptsInvitationWithoutContent() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt)); + assertExpectedContextForInvitation(messageContext, forumName, null); + } + + @Test + public void testAcceptsAccept() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ACCEPT, sessionId)); + assertExpectedContext(messageContext, SHARE_MSG_TYPE_ACCEPT); + } + + @Test + public void testAcceptsDecline() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_DECLINE, sessionId)); + assertExpectedContext(messageContext, SHARE_MSG_TYPE_DECLINE); + } + + @Test + public void testAcceptsLeave() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_LEAVE, sessionId)); + assertExpectedContext(messageContext, SHARE_MSG_TYPE_LEAVE); + } + + @Test + public void testAcceptsAbort() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, sessionId)); + assertExpectedContext(messageContext, SHARE_MSG_TYPE_ABORT); + } + + @Test(expected = FormatException.class) + public void testRejectsNullMessageType() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of(null, sessionId)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonLongMessageType() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of("", sessionId)); + } + + @Test(expected = FormatException.class) + public void testRejectsInvalidMessageType() throws Exception { + int invalidMessageType = SHARE_MSG_TYPE_ABORT + 1; + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(invalidMessageType, sessionId)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullSessionId() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, null)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonRawSessionId() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, 123)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortSessionId() throws Exception { + byte[] invalidSessionId = TestUtils.getRandomBytes(UniqueId.LENGTH - 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, invalidSessionId)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongSessionId() throws Exception { + byte[] invalidSessionId = TestUtils.getRandomBytes(UniqueId.LENGTH + 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, invalidSessionId)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortBodyForAbort() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, BdfList.of(SHARE_MSG_TYPE_ABORT)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongBodyForAbort() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_ABORT, sessionId, 123)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortBodyForInvitation() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongBodyForInvitation() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, content, 123)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullForumName() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, null, + salt, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonStringForumName() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, 123, + salt, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortForumName() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, "", + salt, content)); + } + + @Test + public void testAcceptsMinLengthForumName() throws Exception { + String shortForumName = TestUtils.getRandomString(1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, shortForumName, + salt, content)); + assertExpectedContextForInvitation(messageContext, shortForumName, + content); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongForumName() throws Exception { + String invalidForumName = + TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH + 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, + invalidForumName, salt, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullSalt() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + null, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonRawSalt() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + 123, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooShortSalt() throws Exception { + byte[] invalidSalt = TestUtils.getRandomBytes(FORUM_SALT_LENGTH - 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + invalidSalt, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongSalt() throws Exception { + byte[] invalidSalt = TestUtils.getRandomBytes(FORUM_SALT_LENGTH + 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + invalidSalt, content)); + } + + @Test(expected = FormatException.class) + public void testRejectsNullContent() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, null)); + } + + @Test(expected = FormatException.class) + public void testRejectsNonStringContent() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, 123)); + } + + @Test + public void testAcceptsMinLengthContent() throws Exception { + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + BdfMessageContext messageContext = v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, "")); + assertExpectedContextForInvitation(messageContext, forumName, ""); + } + + @Test(expected = FormatException.class) + public void testRejectsTooLongContent() throws Exception { + String invalidContent = + TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1); + ForumSharingValidator v = new ForumSharingValidator(clientHelper, + metadataEncoder, clock); + v.validateMessage(message, group, + BdfList.of(SHARE_MSG_TYPE_INVITATION, sessionId, forumName, + salt, invalidContent)); + } + + private void assertExpectedContextForInvitation( + BdfMessageContext messageContext, String forumName, + @Nullable String content) throws FormatException { + BdfDictionary meta = messageContext.getDictionary(); + if (content == null) { + assertEquals(6, meta.size()); + } else { + assertEquals(7, meta.size()); + assertEquals(content, meta.getString(INVITATION_MSG)); + } + assertEquals(forumName, meta.getString(FORUM_NAME)); + assertEquals(salt, meta.getRaw(FORUM_SALT)); + assertEquals(SHARE_MSG_TYPE_INVITATION, meta.getLong(TYPE).intValue()); + assertEquals(sessionId.getBytes(), meta.getRaw(SESSION_ID)); + assertFalse(meta.getBoolean(LOCAL)); + assertEquals(timestamp, meta.getLong(TIME).longValue()); + assertEquals(0, messageContext.getDependencies().size()); + } + + private void assertExpectedContext(BdfMessageContext messageContext, + int type) throws FormatException { + BdfDictionary meta = messageContext.getDictionary(); + assertEquals(4, meta.size()); + assertEquals(type, meta.getLong(TYPE).intValue()); + assertEquals(sessionId.getBytes(), meta.getRaw(SESSION_ID)); + assertFalse(meta.getBoolean(LOCAL)); + assertEquals(timestamp, meta.getLong(TIME).longValue()); + assertEquals(0, messageContext.getDependencies().size()); + } +} From eaf17c054fdfa71b3f3bf97cad03d35a32b9c535 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 16:39:30 +0000 Subject: [PATCH 5/6] Moved common fields to superclass. --- briar-tests/src/org/briarproject/ValidatorTestCase.java | 9 +++++++++ .../briarproject/clients/BdfMessageValidatorTest.java | 8 -------- .../org/briarproject/forum/ForumPostValidatorTest.java | 7 ------- .../messaging/PrivateMessageValidatorTest.java | 8 -------- .../briarproject/sharing/ForumSharingValidatorTest.java | 8 -------- 5 files changed, 9 insertions(+), 31 deletions(-) diff --git a/briar-tests/src/org/briarproject/ValidatorTestCase.java b/briar-tests/src/org/briarproject/ValidatorTestCase.java index a340994ed..4e5a3b5e6 100644 --- a/briar-tests/src/org/briarproject/ValidatorTestCase.java +++ b/briar-tests/src/org/briarproject/ValidatorTestCase.java @@ -1,13 +1,22 @@ package org.briarproject; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.Group; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; +import org.briarproject.api.system.Clock; public abstract class ValidatorTestCase extends BriarMockTestCase { + protected final ClientHelper clientHelper = + context.mock(ClientHelper.class); + protected final MetadataEncoder metadataEncoder = + context.mock(MetadataEncoder.class); + protected final Clock clock = context.mock(Clock.class); + protected final MessageId messageId = new MessageId(TestUtils.getRandomId()); protected final GroupId groupId = new GroupId(TestUtils.getRandomId()); diff --git a/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java index 8c567e295..65d856dc0 100644 --- a/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java @@ -3,16 +3,13 @@ package org.briarproject.clients; import org.briarproject.ValidatorTestCase; import org.briarproject.api.FormatException; import org.briarproject.api.clients.BdfMessageContext; -import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; -import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.db.Metadata; import org.briarproject.api.sync.Group; import org.briarproject.api.sync.InvalidMessageException; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageContext; -import org.briarproject.api.system.Clock; import org.jmock.Expectations; import org.jmock.lib.legacy.ClassImposteriser; import org.junit.Test; @@ -25,11 +22,6 @@ import static org.junit.Assert.fail; public class BdfMessageValidatorTest extends ValidatorTestCase { - private final ClientHelper clientHelper = context.mock(ClientHelper.class); - private final MetadataEncoder metadataEncoder = - context.mock(MetadataEncoder.class); - private final Clock clock = context.mock(Clock.class); - private final BdfList body = BdfList.of(123, 456); private final BdfDictionary dictionary = new BdfDictionary(); private final Metadata meta = new Metadata(); diff --git a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java index 512b6f3d1..17e5c1266 100644 --- a/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java +++ b/briar-tests/src/org/briarproject/forum/ForumPostValidatorTest.java @@ -5,16 +5,13 @@ import org.briarproject.ValidatorTestCase; import org.briarproject.api.FormatException; import org.briarproject.api.UniqueId; import org.briarproject.api.clients.BdfMessageContext; -import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; -import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.sync.InvalidMessageException; import org.briarproject.api.sync.MessageId; -import org.briarproject.api.system.Clock; import org.jmock.Expectations; import org.junit.Test; @@ -33,10 +30,6 @@ public class ForumPostValidatorTest extends ValidatorTestCase { private final AuthorFactory authorFactory = context.mock(AuthorFactory.class); - private final ClientHelper clientHelper = context.mock(ClientHelper.class); - private final MetadataEncoder metadataEncoder = - context.mock(MetadataEncoder.class); - private final Clock clock = context.mock(Clock.class); private final MessageId parentId = new MessageId(TestUtils.getRandomId()); private final String authorName = diff --git a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java index fd99b31bb..3d5fad97a 100644 --- a/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/messaging/PrivateMessageValidatorTest.java @@ -4,11 +4,8 @@ import org.briarproject.TestUtils; import org.briarproject.ValidatorTestCase; import org.briarproject.api.FormatException; import org.briarproject.api.clients.BdfMessageContext; -import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; -import org.briarproject.api.data.MetadataEncoder; -import org.briarproject.api.system.Clock; import org.junit.Test; import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; @@ -18,11 +15,6 @@ import static org.junit.Assert.assertFalse; public class PrivateMessageValidatorTest extends ValidatorTestCase { - private final ClientHelper clientHelper = context.mock(ClientHelper.class); - private final MetadataEncoder metadataEncoder = - context.mock(MetadataEncoder.class); - private final Clock clock = context.mock(Clock.class); - @Test(expected = FormatException.class) public void testRejectsTooShortBody() throws Exception { PrivateMessageValidator v = new PrivateMessageValidator(clientHelper, diff --git a/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java b/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java index 099fa2632..7d3c6e22b 100644 --- a/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java +++ b/briar-tests/src/org/briarproject/sharing/ForumSharingValidatorTest.java @@ -5,12 +5,9 @@ import org.briarproject.ValidatorTestCase; import org.briarproject.api.FormatException; import org.briarproject.api.UniqueId; import org.briarproject.api.clients.BdfMessageContext; -import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.clients.SessionId; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; -import org.briarproject.api.data.MetadataEncoder; -import org.briarproject.api.system.Clock; import org.junit.Test; import javax.annotation.Nullable; @@ -35,11 +32,6 @@ import static org.junit.Assert.assertFalse; public class ForumSharingValidatorTest extends ValidatorTestCase { - private final ClientHelper clientHelper = context.mock(ClientHelper.class); - private final MetadataEncoder metadataEncoder = - context.mock(MetadataEncoder.class); - private final Clock clock = context.mock(Clock.class); - private final SessionId sessionId = new SessionId(TestUtils.getRandomId()); private final String forumName = TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH); From e2bbe7429bbe9cf0c42d4d7f492f592f837c646d Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 16 Nov 2016 17:00:53 +0000 Subject: [PATCH 6/6] Moved some boilerplate into a field. --- .../clients/BdfMessageValidatorTest.java | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java index 65d856dc0..1714b261b 100644 --- a/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java +++ b/briar-tests/src/org/briarproject/clients/BdfMessageValidatorTest.java @@ -22,6 +22,17 @@ import static org.junit.Assert.fail; public class BdfMessageValidatorTest extends ValidatorTestCase { + private final BdfMessageValidator subclassNotCalled = + new BdfMessageValidator(clientHelper, metadataEncoder, clock) { + @Override + protected BdfMessageContext validateMessage(Message m, Group g, + BdfList body) + throws InvalidMessageException, FormatException { + fail(); + return null; + } + }; + private final BdfList body = BdfList.of(123, 456); private final BdfDictionary dictionary = new BdfDictionary(); private final Metadata meta = new Metadata(); @@ -37,16 +48,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase { will(returnValue(timestamp - MAX_CLOCK_DIFFERENCE - 1)); }}); - BdfMessageValidator v = new BdfMessageValidator(clientHelper, - metadataEncoder, clock) { - @Override - protected BdfMessageContext validateMessage(Message m, Group g, - BdfList b) throws InvalidMessageException, FormatException { - fail(); - return null; - } - }; - v.validateMessage(message, group); + subclassNotCalled.validateMessage(message, group); } @Test @@ -92,16 +94,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase { will(returnValue(invalidRaw)); }}); - BdfMessageValidator v = new BdfMessageValidator(clientHelper, - metadataEncoder, clock) { - @Override - protected BdfMessageContext validateMessage(Message m, Group g, - BdfList b) throws InvalidMessageException, FormatException { - fail(); - return null; - } - }; - v.validateMessage(invalidMessage, group); + subclassNotCalled.validateMessage(invalidMessage, group); } @Test @@ -146,16 +139,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase { will(throwException(new FormatException())); }}); - BdfMessageValidator v = new BdfMessageValidator(clientHelper, - metadataEncoder, clock) { - @Override - protected BdfMessageContext validateMessage(Message m, Group g, - BdfList b) throws InvalidMessageException, FormatException { - fail(); - return null; - } - }; - v.validateMessage(message, group); + subclassNotCalled.validateMessage(message, group); } @Test(expected = InvalidMessageException.class)