From 43c8cfa248ec3be74669a8e0bb7abd50a3720b04 Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Thu, 28 Feb 2013 13:08:35 +0000 Subject: [PATCH] Explanatory names for MessageFactory methods. --- .../briar/api/messaging/MessageFactory.java | 28 +++++++++-------- .../briar/messaging/MessageFactoryImpl.java | 31 ++++++++++++------- .../net/sf/briar/ProtocolIntegrationTest.java | 12 +++---- .../net/sf/briar/messaging/ConstantsTest.java | 5 +-- .../SimplexMessagingIntegrationTest.java | 3 +- 5 files changed, 46 insertions(+), 33 deletions(-) diff --git a/briar-api/src/net/sf/briar/api/messaging/MessageFactory.java b/briar-api/src/net/sf/briar/api/messaging/MessageFactory.java index ee16823153..25dee5e10c 100644 --- a/briar-api/src/net/sf/briar/api/messaging/MessageFactory.java +++ b/briar-api/src/net/sf/briar/api/messaging/MessageFactory.java @@ -7,25 +7,27 @@ import java.security.PrivateKey; public interface MessageFactory { /** Creates a private message. */ - Message createMessage(MessageId parent, String subject, byte[] body) - throws IOException, GeneralSecurityException; + Message createPrivateMessage(MessageId parent, String subject, byte[] body) + throws IOException, GeneralSecurityException; /** Creates an anonymous message to an unrestricted group. */ - Message createMessage(MessageId parent, Group group, String subject, - byte[] body) throws IOException, GeneralSecurityException; - - /** Creates an anonymous message to a restricted group. */ - Message createMessage(MessageId parent, Group group, PrivateKey groupKey, + Message createAnonymousGroupMessage(MessageId parent, Group group, String subject, byte[] body) throws IOException, GeneralSecurityException; + /** Creates an anonymous message to a restricted group. */ + Message createAnonymousGroupMessage(MessageId parent, Group group, + PrivateKey groupKey, String subject, byte[] body) + throws IOException, GeneralSecurityException; + /** Creates a pseudonymous message to an unrestricted group. */ - Message createMessage(MessageId parent, Group group, Author author, - PrivateKey authorKey, String subject, byte[] body) - throws IOException, GeneralSecurityException; + Message createPseudonymousMessage(MessageId parent, Group group, + Author author, PrivateKey authorKey, String subject, byte[] body) + throws IOException, GeneralSecurityException; /** Creates a pseudonymous message to a restricted group. */ - Message createMessage(MessageId parent, Group group, PrivateKey groupKey, - Author author, PrivateKey authorKey, String subject, byte[] body) - throws IOException, GeneralSecurityException; + Message createPseudonymousMessage(MessageId parent, Group group, + PrivateKey groupKey, Author author, PrivateKey authorKey, + String subject, byte[] body) throws IOException, + GeneralSecurityException; } diff --git a/briar-core/src/net/sf/briar/messaging/MessageFactoryImpl.java b/briar-core/src/net/sf/briar/messaging/MessageFactoryImpl.java index 16f0411243..43d5f0f4b9 100644 --- a/briar-core/src/net/sf/briar/messaging/MessageFactoryImpl.java +++ b/briar-core/src/net/sf/briar/messaging/MessageFactoryImpl.java @@ -52,35 +52,44 @@ class MessageFactoryImpl implements MessageFactory { this.clock = clock; } - public Message createMessage(MessageId parent, String subject, byte[] body) - throws IOException, GeneralSecurityException { + public Message createPrivateMessage(MessageId parent, String subject, + byte[] body) throws IOException, GeneralSecurityException { return createMessage(parent, null, null, null, null, subject, body); } - public Message createMessage(MessageId parent, Group group, String subject, - byte[] body) throws IOException, GeneralSecurityException { + public Message createAnonymousGroupMessage(MessageId parent, Group group, + String subject, byte[] body) throws IOException, + GeneralSecurityException { return createMessage(parent, group, null, null, null, subject, body); } - public Message createMessage(MessageId parent, Group group, + public Message createAnonymousGroupMessage(MessageId parent, Group group, PrivateKey groupKey, String subject, byte[] body) - throws IOException, GeneralSecurityException { + throws IOException, GeneralSecurityException { return createMessage(parent, group, groupKey, null, null, subject, body); } - public Message createMessage(MessageId parent, Group group, Author author, - PrivateKey authorKey, String subject, byte[] body) - throws IOException, GeneralSecurityException { + public Message createPseudonymousMessage(MessageId parent, Group group, + Author author, PrivateKey authorKey, String subject, byte[] body) + throws IOException, GeneralSecurityException { return createMessage(parent, group, null, author, authorKey, subject, body); } - public Message createMessage(MessageId parent, Group group, + public Message createPseudonymousMessage(MessageId parent, Group group, PrivateKey groupKey, Author author, PrivateKey authorKey, String subject, byte[] body) throws IOException, GeneralSecurityException { + return createMessage(parent, group, groupKey, author, authorKey, + subject, body); + } + private Message createMessage(MessageId parent, Group group, + PrivateKey groupKey, Author author, PrivateKey authorKey, + String subject, byte[] body) throws IOException, + GeneralSecurityException { + // Validate the arguments if((author == null) != (authorKey == null)) throw new IllegalArgumentException(); if((group == null || group.getPublicKey() == null) @@ -90,7 +99,7 @@ class MessageFactoryImpl implements MessageFactory { throw new IllegalArgumentException(); if(body.length > MAX_BODY_LENGTH) throw new IllegalArgumentException(); - + // Serialise the message to a buffer ByteArrayOutputStream out = new ByteArrayOutputStream(); Writer w = writerFactory.createWriter(out); // Initialise the consumers diff --git a/briar-tests/src/net/sf/briar/ProtocolIntegrationTest.java b/briar-tests/src/net/sf/briar/ProtocolIntegrationTest.java index fe15f9d39a..18aff79214 100644 --- a/briar-tests/src/net/sf/briar/ProtocolIntegrationTest.java +++ b/briar-tests/src/net/sf/briar/ProtocolIntegrationTest.java @@ -106,15 +106,15 @@ public class ProtocolIntegrationTest extends BriarTestCase { authorKeyPair.getPublic().getEncoded()); // Create two messages to each group: one anonymous, one pseudonymous MessageFactory messageFactory = i.getInstance(MessageFactory.class); - message = messageFactory.createMessage(null, group, subject, - messageBody.getBytes("UTF-8")); - message1 = messageFactory.createMessage(null, group1, + message = messageFactory.createAnonymousGroupMessage(null, group, + subject, messageBody.getBytes("UTF-8")); + message1 = messageFactory.createAnonymousGroupMessage(null, group1, groupKeyPair.getPrivate(), subject, messageBody.getBytes("UTF-8")); - message2 = messageFactory.createMessage(null, group, author, - authorKeyPair.getPrivate(), subject, + message2 = messageFactory.createPseudonymousMessage(null, group, + author, authorKeyPair.getPrivate(), subject, messageBody.getBytes("UTF-8")); - message3 = messageFactory.createMessage(null, group1, + message3 = messageFactory.createPseudonymousMessage(null, group1, groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(), subject, messageBody.getBytes("UTF-8")); messageIds = Arrays.asList(message.getId(), message1.getId(), diff --git a/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java b/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java index ca6a804605..f715b6f2c9 100644 --- a/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java +++ b/briar-tests/src/net/sf/briar/messaging/ConstantsTest.java @@ -109,6 +109,7 @@ public class ConstantsTest extends BriarTestCase { @Test public void testMessageFitsIntoPacket() throws Exception { + MessageId parent = new MessageId(TestUtils.getRandomId()); // Create a maximum-length group String groupName = createRandomString(MAX_GROUP_NAME_LENGTH); byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH]; @@ -124,8 +125,8 @@ public class ConstantsTest extends BriarTestCase { crypto.generateSignatureKeyPair().getPrivate(); String subject = createRandomString(MAX_SUBJECT_LENGTH); byte[] body = new byte[MAX_BODY_LENGTH]; - Message message = messageFactory.createMessage(null, group, - groupPrivate, author, authorPrivate, subject, body); + Message message = messageFactory.createPseudonymousMessage(parent, + group, groupPrivate, author, authorPrivate, subject, body); // Check the size of the serialised message int length = message.getSerialised().length; assertTrue(length > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH diff --git a/briar-tests/src/net/sf/briar/messaging/simplex/SimplexMessagingIntegrationTest.java b/briar-tests/src/net/sf/briar/messaging/simplex/SimplexMessagingIntegrationTest.java index 4ee3256b5a..1257c0eccd 100644 --- a/briar-tests/src/net/sf/briar/messaging/simplex/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/net/sf/briar/messaging/simplex/SimplexMessagingIntegrationTest.java @@ -115,7 +115,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { String subject = "Hello"; byte[] body = "Hi Bob!".getBytes("UTF-8"); MessageFactory messageFactory = alice.getInstance(MessageFactory.class); - Message message = messageFactory.createMessage(null, subject, body); + Message message = messageFactory.createPrivateMessage(null, subject, + body); db.addLocalPrivateMessage(message, contactId); // Create an outgoing simplex connection ByteArrayOutputStream out = new ByteArrayOutputStream(); -- GitLab