From 8c3b598ab2f6e774061038dfe7355812bfb63d6a Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Wed, 16 Nov 2016 15:35:27 +0000
Subject: [PATCH] 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 a6707b526c..512b6f3d16 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 0b87d04c19..0000000000
--- 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 d9b62b5a33..fd99b31bb4 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 0000000000..099fa26327
--- /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());
+	}
+}
-- 
GitLab