diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogFactoryImpl.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogFactoryImpl.java index c7d3a2001ab1a99cb8790ba700c8f3f59ef33f7e..6410d69bcd03fbb2ce42704777a8d983894508bd 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogFactoryImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogFactoryImpl.java @@ -63,7 +63,7 @@ class BlogFactoryImpl implements BlogFactory { @Override public Blog parseBlog(Group group) throws FormatException { byte[] descriptor = group.getDescriptor(); - // Author Name, Public Key + // Author name, public key, RSS feed BdfList blog = clientHelper.toList(descriptor); String name = blog.getString(0); if (name.length() > MAX_AUTHOR_NAME_LENGTH) @@ -72,8 +72,7 @@ class BlogFactoryImpl implements BlogFactory { if (publicKey.length > MAX_PUBLIC_KEY_LENGTH) throw new IllegalArgumentException(); - Author author = - authorFactory.createAuthor(name, publicKey); + Author author = authorFactory.createAuthor(name, publicKey); boolean rssFeed = blog.getBoolean(2); return new Blog(group, author, rssFeed); } diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java index 39ccb5241087c915669c032226f3e63d7de9c770..b27a3bf116b71a22ed1bb6b020b30cf10bf288e9 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java @@ -373,6 +373,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager, // Re-wrap wrapped post without adding another wrapping layer wMessage = blogPostFactory.rewrapWrappedPost(groupId, body); meta.put(KEY_TYPE, WRAPPED_POST.getInt()); + meta.put(KEY_RSS_FEED, pOriginalHeader.isRssFeed()); } else if (type == WRAPPED_COMMENT) { BlogCommentHeader wComment = (BlogCommentHeader) pOriginalHeader; MessageId wrappedId = diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java index 5e74400c52ade433c2a175789d3e50648be6fbe7..eb3283c79c4644963fa9e0548ae7e2d35cd3b745 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java @@ -201,6 +201,7 @@ class BlogPostValidator extends BdfMessageValidator { // Get and Validate the Wrapped Message Group wGroup = groupFactory .createGroup(BlogManagerImpl.CLIENT_ID, descriptor); + Blog wBlog = blogFactory.parseBlog(wGroup); BdfList wBodyList = BdfList.of(POST.getInt(), content, signature); byte[] wBody = clientHelper.toByteArray(wBodyList); Message wMessage = @@ -213,6 +214,7 @@ class BlogPostValidator extends BdfMessageValidator { meta.put(KEY_ORIGINAL_MSG_ID, wMessage.getId()); meta.put(KEY_TIMESTAMP, wTimestamp); meta.put(KEY_AUTHOR, c.getDictionary().getDictionary(KEY_AUTHOR)); + meta.put(KEY_RSS_FEED, wBlog.isRssFeed()); return new BdfMessageContext(meta); } diff --git a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerImplTest.java b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerImplTest.java index 4ff1bb75c4826d35c093fc7a8815f9acaa32975f..1ff64048113a618cc259f3727475da38a9dfe58b 100644 --- a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerImplTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerImplTest.java @@ -44,6 +44,7 @@ import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR_ID; import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR_NAME; import static org.briarproject.briar.api.blog.BlogConstants.KEY_PUBLIC_KEY; import static org.briarproject.briar.api.blog.BlogConstants.KEY_READ; +import static org.briarproject.briar.api.blog.BlogConstants.KEY_RSS_FEED; import static org.briarproject.briar.api.blog.BlogConstants.KEY_TIMESTAMP; import static org.briarproject.briar.api.blog.BlogConstants.KEY_TIME_RECEIVED; import static org.briarproject.briar.api.blog.BlogConstants.KEY_TYPE; @@ -198,12 +199,17 @@ public class BlogManagerImplTest extends BriarTestCase { new BdfEntry(KEY_TYPE, POST.getInt()), new BdfEntry(KEY_TIMESTAMP, message.getTimestamp()), new BdfEntry(KEY_AUTHOR, authorMeta), - new BdfEntry(KEY_READ, true) + new BdfEntry(KEY_READ, true), + new BdfEntry(KEY_RSS_FEED, false) ); context.checking(new Expectations() {{ oneOf(db).startTransaction(false); will(returnValue(txn)); + oneOf(db).getGroup(txn, blog1.getId()); + will(returnValue(blog1.getGroup())); + oneOf(blogFactory).parseBlog(blog1.getGroup()); + will(returnValue(blog1)); oneOf(clientHelper) .addLocalMessage(txn, message, meta, true); oneOf(identityManager) diff --git a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java index 165c1fab52d764f43b4cd2cd8858b0ac082c2400..289f3c09915e4259d4941d1d92280b312b0d203b 100644 --- a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java @@ -2,6 +2,7 @@ package org.briarproject.briar.blog; import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.identity.Author; +import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.test.TestDatabaseModule; import org.briarproject.briar.api.blog.Blog; @@ -22,6 +23,9 @@ import java.util.Iterator; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; +import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; +import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; +import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomString; import static org.briarproject.briar.api.blog.MessageType.COMMENT; import static org.briarproject.briar.api.blog.MessageType.POST; @@ -35,6 +39,7 @@ public class BlogManagerIntegrationTest private BlogManager blogManager0, blogManager1; private Blog blog0, blog1, rssBlog; + private LocalAuthor rssAuthor; @Rule public ExpectedException thrown = ExpectedException.none(); @@ -46,6 +51,10 @@ public class BlogManagerIntegrationTest author0 = identityManager0.getLocalAuthor(); author1 = identityManager1.getLocalAuthor(); + rssAuthor = c0.getAuthorFactory().createLocalAuthor( + getRandomString(MAX_AUTHOR_NAME_LENGTH), + getRandomBytes(MAX_PUBLIC_KEY_LENGTH), + getRandomBytes(123)); blogManager0 = c0.getBlogManager(); blogManager1 = c1.getBlogManager(); @@ -53,7 +62,7 @@ public class BlogManagerIntegrationTest blog0 = blogFactory.createBlog(author0); blog1 = blogFactory.createBlog(author1); - rssBlog = blogFactory.createFeedBlog(author0); + rssBlog = blogFactory.createFeedBlog(rssAuthor); Transaction txn = db0.startTransaction(false); blogManager0.addBlog(txn, rssBlog); db0.commitTransaction(txn); @@ -82,11 +91,12 @@ public class BlogManagerIntegrationTest @Test public void testPersonalBlogInitialisation() throws Exception { Collection<Blog> blogs0 = blogManager0.getBlogs(); - assertEquals(3, blogs0.size()); + assertEquals(4, blogs0.size()); Iterator<Blog> i0 = blogs0.iterator(); assertEquals(author0, i0.next().getAuthor()); assertEquals(author1, i0.next().getAuthor()); assertEquals(author2, i0.next().getAuthor()); + assertEquals(rssAuthor, i0.next().getAuthor()); Collection<Blog> blogs1 = blogManager1.getBlogs(); assertEquals(2, blogs1.size()); @@ -103,11 +113,14 @@ public class BlogManagerIntegrationTest assertEquals(blog0, blogManager1.getBlog(blog0.getId())); assertEquals(blog1, blogManager0.getBlog(blog1.getId())); assertEquals(blog1, blogManager1.getBlog(blog1.getId())); + assertEquals(rssBlog, blogManager0.getBlog(rssBlog.getId())); assertEquals(1, blogManager0.getBlogs(author0).size()); assertEquals(1, blogManager1.getBlogs(author0).size()); assertEquals(1, blogManager0.getBlogs(author1).size()); assertEquals(1, blogManager1.getBlogs(author1).size()); + assertEquals(1, blogManager0.getBlogs(rssAuthor).size()); + assertEquals(0, blogManager1.getBlogs(rssAuthor).size()); } @Test @@ -287,9 +300,8 @@ public class BlogManagerIntegrationTest assertEquals(POST, headers1.iterator().next().getType()); // 1 reblogs that blog post - blogManager1 - .addLocalComment(author1, blog1.getId(), null, - headers1.iterator().next()); + blogManager1.addLocalComment(author1, blog1.getId(), null, + headers1.iterator().next()); // sync comment over sync1To0(2, true); @@ -312,8 +324,7 @@ public class BlogManagerIntegrationTest sync0To1(3, true); // check that comment arrived - headers1 = - blogManager1.getPostHeaders(blog0.getId()); + headers1 = blogManager1.getPostHeaders(blog0.getId()); assertEquals(2, headers1.size()); // get header of comment @@ -453,7 +464,7 @@ public class BlogManagerIntegrationTest // make sure it got saved as an RSS feed post headers = blogManager0.getPostHeaders(blog0.getId()); assertEquals(2, headers.size()); - for (BlogPostHeader h: headers) { + for (BlogPostHeader h : headers) { assertTrue(h instanceof BlogCommentHeader); assertEquals(COMMENT, h.getType()); assertTrue(((BlogCommentHeader) h).getRootPost().isRssFeed()); diff --git a/briar-core/src/test/java/org/briarproject/briar/blog/BlogPostValidatorTest.java b/briar-core/src/test/java/org/briarproject/briar/blog/BlogPostValidatorTest.java index d3731d19de9c9fcea112144ab37589e782081793..f3eb8a803d77cd894e0e459358269ffc01bfef85 100644 --- a/briar-core/src/test/java/org/briarproject/briar/blog/BlogPostValidatorTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/blog/BlogPostValidatorTest.java @@ -38,6 +38,7 @@ import static org.briarproject.briar.api.blog.BlogConstants.KEY_ORIGINAL_PARENT_ import static org.briarproject.briar.api.blog.BlogConstants.KEY_PARENT_MSG_ID; import static org.briarproject.briar.api.blog.BlogConstants.KEY_PUBLIC_KEY; import static org.briarproject.briar.api.blog.BlogConstants.KEY_READ; +import static org.briarproject.briar.api.blog.BlogConstants.KEY_RSS_FEED; import static org.briarproject.briar.api.blog.BlogPostFactory.SIGNING_LABEL_COMMENT; import static org.briarproject.briar.api.blog.BlogPostFactory.SIGNING_LABEL_POST; import static org.briarproject.briar.api.blog.MessageType.COMMENT; @@ -50,7 +51,7 @@ import static org.junit.Assert.assertFalse; public class BlogPostValidatorTest extends BriarTestCase { private final Mockery context = new Mockery(); - private final Blog blog; + private final Blog blog, rssBlog; private final BdfDictionary authorDict; private final ClientId clientId; private final byte[] descriptor; @@ -80,6 +81,7 @@ public class BlogPostValidatorTest extends BriarTestCase { new BdfEntry(KEY_PUBLIC_KEY, author.getPublicKey()) ); blog = new Blog(group, author, false); + rssBlog = new Blog(group, author, true); MessageId messageId = new MessageId(TestUtils.getRandomId()); long timestamp = System.currentTimeMillis(); @@ -97,17 +99,28 @@ public class BlogPostValidatorTest extends BriarTestCase { @Test public void testValidateProperBlogPost() throws IOException, GeneralSecurityException { + testValidateProperBlogPost(blog, false); + } + + @Test + public void testValidateProperRssBlogPost() + throws IOException, GeneralSecurityException { + testValidateProperBlogPost(rssBlog, true); + } + + private void testValidateProperBlogPost(Blog b, boolean rssFeed) + throws IOException, GeneralSecurityException { final byte[] sigBytes = TestUtils.getRandomBytes(42); BdfList m = BdfList.of(POST.getInt(), body, sigBytes); - BdfList signed = - BdfList.of(blog.getId(), message.getTimestamp(), body); - expectCrypto(SIGNING_LABEL_POST, signed, sigBytes); + BdfList signed = BdfList.of(b.getId(), message.getTimestamp(), body); + expectCrypto(b, SIGNING_LABEL_POST, signed, sigBytes); final BdfDictionary result = validator.validateMessage(message, group, m).getDictionary(); assertEquals(authorDict, result.getDictionary(KEY_AUTHOR)); assertFalse(result.getBoolean(KEY_READ)); + assertEquals(rssFeed, result.getBoolean(KEY_RSS_FEED)); context.assertIsSatisfied(); } @@ -137,14 +150,12 @@ public class BlogPostValidatorTest extends BriarTestCase { MessageId pOriginalId = new MessageId(TestUtils.getRandomId()); MessageId currentId = new MessageId(TestUtils.getRandomId()); final byte[] sigBytes = TestUtils.getRandomBytes(42); - BdfList m = - BdfList.of(COMMENT.getInt(), comment, pOriginalId, currentId, - sigBytes); - - BdfList signed = - BdfList.of(blog.getId(), message.getTimestamp(), comment, - pOriginalId, currentId); - expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes); + BdfList m = BdfList.of(COMMENT.getInt(), comment, pOriginalId, + currentId, sigBytes); + + BdfList signed = BdfList.of(blog.getId(), message.getTimestamp(), + comment, pOriginalId, currentId); + expectCrypto(blog, SIGNING_LABEL_COMMENT, signed, sigBytes); final BdfDictionary result = validator.validateMessage(message, group, m).getDictionary(); @@ -164,14 +175,12 @@ public class BlogPostValidatorTest extends BriarTestCase { MessageId originalId = new MessageId(TestUtils.getRandomId()); MessageId currentId = new MessageId(TestUtils.getRandomId()); final byte[] sigBytes = TestUtils.getRandomBytes(42); - BdfList m = - BdfList.of(COMMENT.getInt(), null, originalId, currentId, - sigBytes); - - BdfList signed = - BdfList.of(blog.getId(), message.getTimestamp(), null, - originalId, currentId); - expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes); + BdfList m = BdfList.of(COMMENT.getInt(), null, originalId, currentId, + sigBytes); + + BdfList signed = BdfList.of(blog.getId(), message.getTimestamp(), null, + originalId, currentId); + expectCrypto(blog, SIGNING_LABEL_COMMENT, signed, sigBytes); final BdfDictionary result = validator.validateMessage(message, group, m).getDictionary(); @@ -182,22 +191,33 @@ public class BlogPostValidatorTest extends BriarTestCase { @Test public void testValidateProperWrappedPost() throws IOException, GeneralSecurityException { + testValidateProperWrappedPost(blog, false); + } + + @Test + public void testValidateProperWrappedRssPost() + throws IOException, GeneralSecurityException { + testValidateProperWrappedPost(rssBlog, true); + } + + private void testValidateProperWrappedPost(final Blog b, boolean rssFeed) + throws IOException, GeneralSecurityException { // group descriptor, timestamp, content, signature final byte[] sigBytes = TestUtils.getRandomBytes(42); - BdfList m = - BdfList.of(WRAPPED_POST.getInt(), descriptor, - message.getTimestamp(), body, sigBytes); + BdfList m = BdfList.of(WRAPPED_POST.getInt(), descriptor, + message.getTimestamp(), body, sigBytes); - BdfList signed = - BdfList.of(blog.getId(), message.getTimestamp(), body); - expectCrypto(SIGNING_LABEL_POST, signed, sigBytes); + BdfList signed = BdfList.of(b.getId(), message.getTimestamp(), body); + expectCrypto(b, SIGNING_LABEL_POST, signed, sigBytes); final BdfList originalList = BdfList.of(POST.getInt(), body, sigBytes); final byte[] originalBody = TestUtils.getRandomBytes(42); context.checking(new Expectations() {{ oneOf(groupFactory).createGroup(clientId, descriptor); - will(returnValue(blog.getGroup())); + will(returnValue(b.getGroup())); + oneOf(blogFactory).parseBlog(b.getGroup()); + will(returnValue(b)); oneOf(clientHelper).toByteArray(originalList); will(returnValue(originalBody)); oneOf(messageFactory) @@ -210,6 +230,7 @@ public class BlogPostValidatorTest extends BriarTestCase { validator.validateMessage(message, group, m).getDictionary(); assertEquals(authorDict, result.getDictionary(KEY_AUTHOR)); + assertEquals(rssFeed, result.getBoolean(KEY_RSS_FEED)); context.assertIsSatisfied(); } @@ -229,7 +250,7 @@ public class BlogPostValidatorTest extends BriarTestCase { BdfList signed = BdfList.of(blog.getId(), message.getTimestamp(), comment, originalId, oldId); - expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes); + expectCrypto(blog, SIGNING_LABEL_COMMENT, signed, sigBytes); final BdfList originalList = BdfList.of(COMMENT.getInt(), comment, originalId, oldId, sigBytes); @@ -257,11 +278,12 @@ public class BlogPostValidatorTest extends BriarTestCase { context.assertIsSatisfied(); } - private void expectCrypto(final String label, final BdfList signed, - final byte[] sig) throws IOException, GeneralSecurityException { + private void expectCrypto(final Blog b, final String label, + final BdfList signed, final byte[] sig) + throws IOException, GeneralSecurityException { context.checking(new Expectations() {{ oneOf(blogFactory).parseBlog(group); - will(returnValue(blog)); + will(returnValue(b)); oneOf(clientHelper) .verifySignature(label, sig, author.getPublicKey(), signed); }}); diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTestComponent.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTestComponent.java index c4219c9e486928c05d515f163d11f54f71baab0e..c114930bf8c330cb8bfd5ea58fcfe107b13ce773 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTestComponent.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTestComponent.java @@ -4,6 +4,7 @@ import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.event.EventBus; +import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.properties.TransportPropertyManager; @@ -136,4 +137,5 @@ public interface BriarIntegrationTestComponent { TransportPropertyManager getTransportPropertyManager(); + AuthorFactory getAuthorFactory(); }