mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Update blog and forum sharing clients to include self-destruct timers.
This commit is contained in:
@@ -8,10 +8,16 @@ import org.briarproject.briar.api.blog.Blog;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.briarproject.briar.api.sharing.SharingConstants.MAX_INVITATION_TEXT_LENGTH;
|
||||
import static org.briarproject.briar.sharing.MessageType.INVITE;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
|
||||
@@ -31,7 +37,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithText() throws Exception {
|
||||
expectCreateBlog();
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -40,7 +46,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullText() throws Exception {
|
||||
expectCreateBlog();
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, null));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -49,16 +55,64 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullPreviousMsgId() throws Exception {
|
||||
expectCreateBlog();
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), null, descriptor, text));
|
||||
assertExpectedContext(context, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithMinAutoDeleteTimer() throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(MIN_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithMaxAutoDeleteTimer() throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(MAX_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(null);
|
||||
}
|
||||
|
||||
private void testAcceptsInvitationWithAutoDeleteTimer(@Nullable Long timer)
|
||||
throws Exception {
|
||||
expectCreateBlog();
|
||||
expectEncodeMetadata(INVITE,
|
||||
timer == null ? NO_AUTO_DELETE_TIMER : timer);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text,
|
||||
timer));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvitationWithTooBigAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsInvitationWithAutoDeleteTimer(MAX_AUTO_DELETE_TIMER_MS + 1);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvitationWithTooSmallAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsInvitationWithAutoDeleteTimer(MIN_AUTO_DELETE_TIMER_MS - 1);
|
||||
}
|
||||
|
||||
private void testRejectsInvitationWithAutoDeleteTimer(Long timer)
|
||||
throws Exception {
|
||||
expectCreateBlog();
|
||||
validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text,
|
||||
timer));
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationForRssBlog() throws Exception {
|
||||
expectCreateRssBlog();
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfList rssDescriptor = BdfList.of(authorList, true);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, rssDescriptor,
|
||||
@@ -93,7 +147,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
|
||||
public void testAcceptsMinLengthText() throws Exception {
|
||||
String shortText = getRandomString(1);
|
||||
expectCreateBlog();
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,
|
||||
shortText));
|
||||
|
||||
@@ -7,12 +7,18 @@ import org.briarproject.briar.api.forum.Forum;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
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_TEXT_LENGTH;
|
||||
import static org.briarproject.briar.sharing.MessageType.INVITE;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
|
||||
@@ -31,7 +37,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithText() throws Exception {
|
||||
expectCreateForum(forumName);
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -40,7 +46,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullText() throws Exception {
|
||||
expectCreateForum(forumName);
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, null));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -49,12 +55,60 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullPreviousMsgId() throws Exception {
|
||||
expectCreateForum(forumName);
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), null, descriptor, null));
|
||||
assertExpectedContext(context, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithMinAutoDeleteTimer() throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(MIN_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithMaxAutoDeleteTimer() throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(MAX_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsInvitationWithNullAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testAcceptsInvitationWithAutoDeleteTimer(null);
|
||||
}
|
||||
|
||||
private void testAcceptsInvitationWithAutoDeleteTimer(@Nullable Long timer)
|
||||
throws Exception {
|
||||
expectCreateForum(forumName);
|
||||
expectEncodeMetadata(INVITE,
|
||||
timer == null ? NO_AUTO_DELETE_TIMER : timer);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text,
|
||||
timer));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvitationWithTooBigAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsInvitationWithAutoDeleteTimer(MAX_AUTO_DELETE_TIMER_MS + 1);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsInvitationWithTooSmallAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsInvitationWithAutoDeleteTimer(MIN_AUTO_DELETE_TIMER_MS - 1);
|
||||
}
|
||||
|
||||
private void testRejectsInvitationWithAutoDeleteTimer(long timer)
|
||||
throws FormatException {
|
||||
expectCreateForum(forumName);
|
||||
validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, text,
|
||||
timer));
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsNullForumName() throws Exception {
|
||||
BdfList invalidDescriptor = BdfList.of(null, salt);
|
||||
@@ -84,7 +138,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
String shortForumName = getRandomString(1);
|
||||
BdfList validDescriptor = BdfList.of(shortForumName, salt);
|
||||
expectCreateForum(shortForumName);
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, validDescriptor,
|
||||
null));
|
||||
@@ -144,7 +198,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
|
||||
@Test
|
||||
public void testAcceptsMinLengthText() throws Exception {
|
||||
expectCreateForum(forumName);
|
||||
expectEncodeMetadata(INVITE);
|
||||
expectEncodeMetadata(INVITE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(INVITE.getValue(), previousMsgId, descriptor, "1"));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
|
||||
@@ -16,6 +16,11 @@ import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MAX_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
|
||||
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.briar.sharing.MessageType.ABORT;
|
||||
@@ -24,7 +29,7 @@ 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;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
|
||||
@@ -54,23 +59,84 @@ public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
|
||||
@Test
|
||||
public void testAcceptsAccept() throws Exception {
|
||||
expectEncodeMetadata(ACCEPT);
|
||||
expectEncodeMetadata(ACCEPT, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(ACCEPT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAcceptWithMinAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(ACCEPT,
|
||||
MIN_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAcceptWithMaxAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(ACCEPT,
|
||||
MAX_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsAcceptWithNullAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(ACCEPT, null);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAcceptWithTooBigAutoDeleteTimer() throws Exception {
|
||||
testRejectsResponseWithAutoDeleteTimer(ACCEPT,
|
||||
MAX_AUTO_DELETE_TIMER_MS + 1);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAcceptWithTooSmallAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsResponseWithAutoDeleteTimer(ACCEPT,
|
||||
MIN_AUTO_DELETE_TIMER_MS - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDecline() throws Exception {
|
||||
expectEncodeMetadata(DECLINE);
|
||||
expectEncodeMetadata(DECLINE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(DECLINE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDeclineWithMinAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(DECLINE,
|
||||
MIN_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDeclineWithMaxAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(DECLINE,
|
||||
MAX_AUTO_DELETE_TIMER_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsDeclineWithNullAutoDeleteTimer() throws Exception {
|
||||
testAcceptsResponseWithAutoDeleteTimer(DECLINE, null);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsDeclineWithTooBigAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsResponseWithAutoDeleteTimer(DECLINE,
|
||||
MAX_AUTO_DELETE_TIMER_MS + 1);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsDeclineWithTooSmallAutoDeleteTimer()
|
||||
throws Exception {
|
||||
testRejectsResponseWithAutoDeleteTimer(DECLINE,
|
||||
MIN_AUTO_DELETE_TIMER_MS - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsLeave() throws Exception {
|
||||
expectEncodeMetadata(LEAVE);
|
||||
expectEncodeMetadata(LEAVE, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(LEAVE.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -78,7 +144,7 @@ public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
|
||||
@Test
|
||||
public void testAcceptsAbort() throws Exception {
|
||||
expectEncodeMetadata(ABORT);
|
||||
expectEncodeMetadata(ABORT, NO_AUTO_DELETE_TIMER);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
@@ -141,10 +207,10 @@ public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
BdfList.of(ABORT.getValue(), groupId, previousMsgId, 123));
|
||||
}
|
||||
|
||||
void expectEncodeMetadata(MessageType type) {
|
||||
void expectEncodeMetadata(MessageType type, long autoDeleteTimer) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(messageEncoder).encodeMetadata(type, groupId, timestamp,
|
||||
false, false, false, false, false);
|
||||
false, false, false, false, false, autoDeleteTimer);
|
||||
will(returnValue(meta));
|
||||
}});
|
||||
}
|
||||
@@ -153,12 +219,26 @@ public abstract class SharingValidatorTest extends ValidatorTestCase {
|
||||
@Nullable MessageId previousMsgId) {
|
||||
Collection<MessageId> dependencies = messageContext.getDependencies();
|
||||
if (previousMsgId == null) {
|
||||
assertTrue(dependencies.isEmpty());
|
||||
assertEquals(emptyList(), dependencies);
|
||||
} else {
|
||||
assertEquals(1, dependencies.size());
|
||||
assertTrue(dependencies.contains(previousMsgId));
|
||||
assertEquals(singletonList(previousMsgId), dependencies);
|
||||
}
|
||||
assertEquals(meta, messageContext.getDictionary());
|
||||
}
|
||||
|
||||
private void testAcceptsResponseWithAutoDeleteTimer(MessageType type,
|
||||
@Nullable Long timer) throws Exception {
|
||||
expectEncodeMetadata(type,
|
||||
timer == null ? NO_AUTO_DELETE_TIMER : timer);
|
||||
BdfMessageContext context = validator.validateMessage(message, group,
|
||||
BdfList.of(type.getValue(), groupId, previousMsgId, timer));
|
||||
assertExpectedContext(context, previousMsgId);
|
||||
}
|
||||
|
||||
private void testRejectsResponseWithAutoDeleteTimer(MessageType type,
|
||||
long timer) throws FormatException {
|
||||
validator.validateMessage(message, group,
|
||||
BdfList.of(type.getValue(), groupId, previousMsgId, timer));
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user