mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Migrate blog sharing to new sharing client infrastructure
This commit is contained in:
@@ -4,7 +4,6 @@ import net.jodah.concurrentunit.Waiter;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchGroupException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
@@ -17,6 +16,7 @@ import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.BlogSharingManager;
|
||||
import org.briarproject.briar.api.blog.event.BlogInvitationRequestReceivedEvent;
|
||||
import org.briarproject.briar.api.blog.event.BlogInvitationResponseReceivedEvent;
|
||||
import org.briarproject.briar.api.client.ProtocolStateException;
|
||||
import org.briarproject.briar.api.sharing.InvitationMessage;
|
||||
import org.briarproject.briar.test.BriarIntegrationTest;
|
||||
import org.briarproject.briar.test.BriarIntegrationTestComponent;
|
||||
@@ -35,7 +35,6 @@ import static org.briarproject.briar.test.BriarTestUtils.assertGroupCount;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class BlogSharingIntegrationTest
|
||||
extends BriarIntegrationTest<BriarIntegrationTestComponent> {
|
||||
@@ -92,7 +91,7 @@ public class BlogSharingIntegrationTest
|
||||
injectEagerSingletons(c2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = ProtocolStateException.class)
|
||||
public void testPersonalBlogCannotBeSharedWithOwner() throws Exception {
|
||||
listenToEvents(true);
|
||||
|
||||
@@ -109,11 +108,6 @@ public class BlogSharingIntegrationTest
|
||||
blogSharingManager0
|
||||
.sendInvitation(blog1.getId(), contactId1From0, "Hi!",
|
||||
clock.currentTimeMillis());
|
||||
|
||||
// sync invitation
|
||||
sync0To1(1, false);
|
||||
// make sure the invitee ignored the request for their own blog
|
||||
assertFalse(listener1.requestReceived);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -293,17 +287,11 @@ public class BlogSharingIntegrationTest
|
||||
assertFalse(blogSharingManager0.getSharedWith(blog2.getId())
|
||||
.contains(contact1From0));
|
||||
// invitee no longer has blog shared by sharer
|
||||
try {
|
||||
blogSharingManager1.getSharedWith(blog2.getId());
|
||||
fail();
|
||||
} catch (NoSuchGroupException e) {
|
||||
// expected
|
||||
}
|
||||
// blog can be shared again
|
||||
assertEquals(0,
|
||||
blogSharingManager1.getSharedWith(blog2.getId()).size());
|
||||
// blog can be shared again by sharer
|
||||
assertTrue(
|
||||
blogSharingManager0.canBeShared(blog2.getId(), contact1From0));
|
||||
assertTrue(
|
||||
blogSharingManager1.canBeShared(blog2.getId(), contact0From1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
package org.briarproject.briar.sharing;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.client.BdfMessageContext;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_NAME_LENGTH;
|
||||
import static org.briarproject.briar.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH;
|
||||
import static org.briarproject.briar.sharing.MessageType.INVITE;
|
||||
|
||||
public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
|
||||
private final AuthorId authorId = new AuthorId(getRandomId());
|
||||
private final String authorName = TestUtils.getRandomString(42);
|
||||
private final byte[] publicKey =
|
||||
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
private final Author author = new Author(authorId, authorName, publicKey);
|
||||
private final Blog blog = new Blog(group, author);
|
||||
private final BdfList descriptor = BdfList.of(authorName, publicKey);
|
||||
private final String content =
|
||||
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
|
||||
|
||||
@Override
|
||||
SharingValidator getValidator() {
|
||||
return new BlogSharingValidator(messageEncoder, clientHelper,
|
||||
metadataEncoder, clock, blogFactory, authorFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithContent() throws Exception {
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,
|
||||
content));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullContent() throws Exception {
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, null));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullPreviousMsgId() throws Exception {
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), null, descriptor, null));
|
||||
assertExpectedContext(messageContext, null);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullBlogName() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(null, publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonStringBlogName() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(123, publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortBlogName() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of("", publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsMinLengthBlogName() throws Exception {
|
||||
String shortBlogName = TestUtils.getRandomString(1);
|
||||
BdfList validDescriptor = BdfList.of(shortBlogName, publicKey);
|
||||
expectCreateBlog(shortBlogName, publicKey);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, validDescriptor,
|
||||
null));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongBlogName() throws Exception {
|
||||
String invalidBlogName =
|
||||
TestUtils.getRandomString(MAX_BLOG_NAME_LENGTH + 1);
|
||||
BdfList invalidDescriptor = BdfList.of(invalidBlogName, publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullPublicKey() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(authorName, null);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonRawPublicKey() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(authorName, 123);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongPublicKey() throws Exception {
|
||||
byte[] invalidKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH + 1);
|
||||
BdfList invalidDescriptor = BdfList.of(authorName, invalidKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsMinLengthPublicKey() throws Exception {
|
||||
byte[] key = TestUtils.getRandomBytes(1);
|
||||
BdfList validDescriptor = BdfList.of(authorName, key);
|
||||
|
||||
expectCreateBlog(authorName, key);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, validDescriptor,
|
||||
null));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonStringContent() throws Exception {
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,
|
||||
123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsMinLengthContent() throws Exception {
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
expectEncodeMetadata(INVITE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, "1"));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongContent() throws Exception {
|
||||
String invalidContent =
|
||||
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1);
|
||||
expectCreateBlog(authorName, publicKey);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,
|
||||
invalidContent));
|
||||
}
|
||||
|
||||
private void expectCreateBlog(final String name, final byte[] key) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(authorFactory).createAuthor(name, key);
|
||||
will(returnValue(author));
|
||||
oneOf(blogFactory).createBlog(author);
|
||||
will(returnValue(blog));
|
||||
}});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +1,20 @@
|
||||
package org.briarproject.briar.sharing;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
import org.briarproject.bramble.api.client.BdfMessageContext;
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfEntry;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.test.ValidatorTestCase;
|
||||
import org.briarproject.briar.api.forum.Forum;
|
||||
import org.briarproject.briar.api.forum.ForumFactory;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.briar.api.forum.ForumConstants.FORUM_SALT_LENGTH;
|
||||
import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
|
||||
import static org.briarproject.briar.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH;
|
||||
import static org.briarproject.briar.sharing.MessageType.ABORT;
|
||||
import static org.briarproject.briar.sharing.MessageType.ACCEPT;
|
||||
import static org.briarproject.briar.sharing.MessageType.DECLINE;
|
||||
import static org.briarproject.briar.sharing.MessageType.INVITE;
|
||||
import static org.briarproject.briar.sharing.MessageType.LEAVE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ForumSharingValidatorTest extends ValidatorTestCase {
|
||||
public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
|
||||
private final MessageEncoder messageEncoder =
|
||||
context.mock(MessageEncoder.class);
|
||||
private final ForumFactory forumFactory = context.mock(ForumFactory.class);
|
||||
private final ForumSharingValidator v =
|
||||
new ForumSharingValidator(messageEncoder, clientHelper,
|
||||
metadataEncoder, clock, forumFactory);
|
||||
|
||||
private final MessageId previousMsgId = new MessageId(getRandomId());
|
||||
private final String forumName =
|
||||
TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH);
|
||||
private final byte[] salt = TestUtils.getRandomBytes(FORUM_SALT_LENGTH);
|
||||
@@ -47,8 +22,12 @@ public class ForumSharingValidatorTest extends ValidatorTestCase {
|
||||
private final BdfList descriptor = BdfList.of(forumName, salt);
|
||||
private final String content =
|
||||
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
|
||||
private final BdfDictionary meta =
|
||||
BdfDictionary.of(new BdfEntry("meta", "data"));
|
||||
|
||||
@Override
|
||||
SharingValidator getValidator() {
|
||||
return new ForumSharingValidator(messageEncoder, clientHelper,
|
||||
metadataEncoder, clock, forumFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithContent() throws Exception {
|
||||
@@ -78,107 +57,6 @@ public class ForumSharingValidatorTest extends ValidatorTestCase {
|
||||
assertExpectedContext(messageContext, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAccept() throws Exception {
|
||||
expectEncodeMetadata(ACCEPT);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(ACCEPT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDecline() throws Exception {
|
||||
expectEncodeMetadata(DECLINE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(DECLINE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsLeave() throws Exception {
|
||||
expectEncodeMetadata(LEAVE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(LEAVE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAbort() throws Exception {
|
||||
expectEncodeMetadata(ABORT);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullMessageType() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(null, groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonLongMessageType() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of("", groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvalidMessageType() throws Exception {
|
||||
int invalidMessageType = ABORT.getValue() + 1;
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(invalidMessageType, groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullSessionId() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), null, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonRawSessionId() throws Exception {
|
||||
v.validateMessage(message, group, BdfList.of(ABORT.getValue(), 123));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortSessionId() throws Exception {
|
||||
byte[] invalidGroupId = TestUtils.getRandomBytes(UniqueId.LENGTH - 1);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), invalidGroupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongSessionId() throws Exception {
|
||||
byte[] invalidGroupId = TestUtils.getRandomBytes(UniqueId.LENGTH + 1);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), invalidGroupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortBodyForAbort() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongBodyForAbort() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId, 123));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortBodyForInvitation() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongBodyForInvitation() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, null,
|
||||
123));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullForumName() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(null, salt);
|
||||
@@ -293,25 +171,4 @@ public class ForumSharingValidatorTest extends ValidatorTestCase {
|
||||
}});
|
||||
}
|
||||
|
||||
private void expectEncodeMetadata(final MessageType type) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(messageEncoder)
|
||||
.encodeMetadata(type, groupId, timestamp, false, false,
|
||||
false, false);
|
||||
will(returnValue(meta));
|
||||
}});
|
||||
}
|
||||
|
||||
private void assertExpectedContext(BdfMessageContext messageContext,
|
||||
@Nullable MessageId previousMsgId) throws FormatException {
|
||||
Collection<MessageId> dependencies = messageContext.getDependencies();
|
||||
if (previousMsgId == null) {
|
||||
assertTrue(dependencies.isEmpty());
|
||||
} else {
|
||||
assertEquals(1, dependencies.size());
|
||||
assertTrue(dependencies.contains(previousMsgId));
|
||||
}
|
||||
assertEquals(meta, messageContext.getDictionary());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package org.briarproject.briar.sharing;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
import org.briarproject.bramble.api.client.BdfMessageContext;
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfEntry;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.test.ValidatorTestCase;
|
||||
import org.briarproject.briar.api.blog.BlogFactory;
|
||||
import org.briarproject.briar.api.forum.ForumFactory;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.briar.sharing.MessageType.ABORT;
|
||||
import static org.briarproject.briar.sharing.MessageType.ACCEPT;
|
||||
import static org.briarproject.briar.sharing.MessageType.DECLINE;
|
||||
import static org.briarproject.briar.sharing.MessageType.INVITE;
|
||||
import static org.briarproject.briar.sharing.MessageType.LEAVE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
|
||||
protected final MessageEncoder messageEncoder =
|
||||
context.mock(MessageEncoder.class);
|
||||
protected final ForumFactory forumFactory =
|
||||
context.mock(ForumFactory.class);
|
||||
protected final BlogFactory blogFactory = context.mock(BlogFactory.class);
|
||||
protected final SharingValidator v = getValidator();
|
||||
|
||||
protected final MessageId previousMsgId = new MessageId(getRandomId());
|
||||
private final BdfDictionary meta =
|
||||
BdfDictionary.of(new BdfEntry("meta", "data"));
|
||||
|
||||
abstract SharingValidator getValidator();
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortBodyForInvitation() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongBodyForInvitation() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, null,
|
||||
123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAccept() throws Exception {
|
||||
expectEncodeMetadata(ACCEPT);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(ACCEPT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDecline() throws Exception {
|
||||
expectEncodeMetadata(DECLINE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(DECLINE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsLeave() throws Exception {
|
||||
expectEncodeMetadata(LEAVE);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(LEAVE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAbort() throws Exception {
|
||||
expectEncodeMetadata(ABORT);
|
||||
BdfMessageContext messageContext = v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(messageContext, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullMessageType() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(null, groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonLongMessageType() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of("", groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvalidMessageType() throws Exception {
|
||||
int invalidMessageType = ABORT.getValue() + 1;
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(invalidMessageType, groupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullSessionId() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), null, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNonRawSessionId() throws Exception {
|
||||
v.validateMessage(message, group, BdfList.of(ABORT.getValue(), 123));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortSessionId() throws Exception {
|
||||
byte[] invalidGroupId = TestUtils.getRandomBytes(UniqueId.LENGTH - 1);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), invalidGroupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongSessionId() throws Exception {
|
||||
byte[] invalidGroupId = TestUtils.getRandomBytes(UniqueId.LENGTH + 1);
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), invalidGroupId, previousMsgId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortBodyForAbort() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongBodyForAbort() throws Exception {
|
||||
v.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId, 123));
|
||||
}
|
||||
|
||||
protected void expectEncodeMetadata(final MessageType type) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(messageEncoder)
|
||||
.encodeMetadata(type, groupId, timestamp, false, false,
|
||||
false, false);
|
||||
will(returnValue(meta));
|
||||
}});
|
||||
}
|
||||
|
||||
protected void assertExpectedContext(BdfMessageContext messageContext,
|
||||
@Nullable MessageId previousMsgId) throws FormatException {
|
||||
Collection<MessageId> dependencies = messageContext.getDependencies();
|
||||
if (previousMsgId == null) {
|
||||
assertTrue(dependencies.isEmpty());
|
||||
} else {
|
||||
assertEquals(1, dependencies.size());
|
||||
assertTrue(dependencies.contains(previousMsgId));
|
||||
}
|
||||
assertEquals(meta, messageContext.getDictionary());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user