Explanatory names for MessageFactory methods.

This commit is contained in:
akwizgran
2013-02-28 13:08:35 +00:00
parent bfd4ee5e9f
commit 43c8cfa248
5 changed files with 46 additions and 33 deletions

View File

@@ -7,25 +7,27 @@ import java.security.PrivateKey;
public interface MessageFactory { public interface MessageFactory {
/** Creates a private message. */ /** Creates a private message. */
Message createMessage(MessageId parent, String subject, byte[] body) Message createPrivateMessage(MessageId parent, String subject, byte[] body)
throws IOException, GeneralSecurityException; throws IOException, GeneralSecurityException;
/** Creates an anonymous message to an unrestricted group. */ /** Creates an anonymous message to an unrestricted group. */
Message createMessage(MessageId parent, Group group, String subject, Message createAnonymousGroupMessage(MessageId parent, Group group,
byte[] body) throws IOException, GeneralSecurityException;
/** Creates an anonymous message to a restricted group. */
Message createMessage(MessageId parent, Group group, PrivateKey groupKey,
String subject, byte[] body) throws IOException, String subject, byte[] body) throws IOException,
GeneralSecurityException; 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. */ /** Creates a pseudonymous message to an unrestricted group. */
Message createMessage(MessageId parent, Group group, Author author, Message createPseudonymousMessage(MessageId parent, Group group,
PrivateKey authorKey, String subject, byte[] body) Author author, PrivateKey authorKey, String subject, byte[] body)
throws IOException, GeneralSecurityException; throws IOException, GeneralSecurityException;
/** Creates a pseudonymous message to a restricted group. */ /** Creates a pseudonymous message to a restricted group. */
Message createMessage(MessageId parent, Group group, PrivateKey groupKey, Message createPseudonymousMessage(MessageId parent, Group group,
Author author, PrivateKey authorKey, String subject, byte[] body) PrivateKey groupKey, Author author, PrivateKey authorKey,
throws IOException, GeneralSecurityException; String subject, byte[] body) throws IOException,
GeneralSecurityException;
} }

View File

@@ -52,35 +52,44 @@ class MessageFactoryImpl implements MessageFactory {
this.clock = clock; this.clock = clock;
} }
public Message createMessage(MessageId parent, String subject, byte[] body) public Message createPrivateMessage(MessageId parent, String subject,
throws IOException, GeneralSecurityException { byte[] body) throws IOException, GeneralSecurityException {
return createMessage(parent, null, null, null, null, subject, body); return createMessage(parent, null, null, null, null, subject, body);
} }
public Message createMessage(MessageId parent, Group group, String subject, public Message createAnonymousGroupMessage(MessageId parent, Group group,
byte[] body) throws IOException, GeneralSecurityException { String subject, byte[] body) throws IOException,
GeneralSecurityException {
return createMessage(parent, group, null, null, null, subject, body); 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) PrivateKey groupKey, String subject, byte[] body)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
return createMessage(parent, group, groupKey, null, null, subject, return createMessage(parent, group, groupKey, null, null, subject,
body); body);
} }
public Message createMessage(MessageId parent, Group group, Author author, public Message createPseudonymousMessage(MessageId parent, Group group,
PrivateKey authorKey, String subject, byte[] body) Author author, PrivateKey authorKey, String subject, byte[] body)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
return createMessage(parent, group, null, author, authorKey, subject, return createMessage(parent, group, null, author, authorKey, subject,
body); body);
} }
public Message createMessage(MessageId parent, Group group, public Message createPseudonymousMessage(MessageId parent, Group group,
PrivateKey groupKey, Author author, PrivateKey authorKey, PrivateKey groupKey, Author author, PrivateKey authorKey,
String subject, byte[] body) throws IOException, String subject, byte[] body) throws IOException,
GeneralSecurityException { 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)) if((author == null) != (authorKey == null))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if((group == null || group.getPublicKey() == null) if((group == null || group.getPublicKey() == null)
@@ -90,7 +99,7 @@ class MessageFactoryImpl implements MessageFactory {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(body.length > MAX_BODY_LENGTH) if(body.length > MAX_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
// Serialise the message to a buffer
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out); Writer w = writerFactory.createWriter(out);
// Initialise the consumers // Initialise the consumers

View File

@@ -106,15 +106,15 @@ public class ProtocolIntegrationTest extends BriarTestCase {
authorKeyPair.getPublic().getEncoded()); authorKeyPair.getPublic().getEncoded());
// Create two messages to each group: one anonymous, one pseudonymous // Create two messages to each group: one anonymous, one pseudonymous
MessageFactory messageFactory = i.getInstance(MessageFactory.class); MessageFactory messageFactory = i.getInstance(MessageFactory.class);
message = messageFactory.createMessage(null, group, subject, message = messageFactory.createAnonymousGroupMessage(null, group,
messageBody.getBytes("UTF-8")); subject, messageBody.getBytes("UTF-8"));
message1 = messageFactory.createMessage(null, group1, message1 = messageFactory.createAnonymousGroupMessage(null, group1,
groupKeyPair.getPrivate(), subject, groupKeyPair.getPrivate(), subject,
messageBody.getBytes("UTF-8")); messageBody.getBytes("UTF-8"));
message2 = messageFactory.createMessage(null, group, author, message2 = messageFactory.createPseudonymousMessage(null, group,
authorKeyPair.getPrivate(), subject, author, authorKeyPair.getPrivate(), subject,
messageBody.getBytes("UTF-8")); messageBody.getBytes("UTF-8"));
message3 = messageFactory.createMessage(null, group1, message3 = messageFactory.createPseudonymousMessage(null, group1,
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(), groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
subject, messageBody.getBytes("UTF-8")); subject, messageBody.getBytes("UTF-8"));
messageIds = Arrays.asList(message.getId(), message1.getId(), messageIds = Arrays.asList(message.getId(), message1.getId(),

View File

@@ -109,6 +109,7 @@ public class ConstantsTest extends BriarTestCase {
@Test @Test
public void testMessageFitsIntoPacket() throws Exception { public void testMessageFitsIntoPacket() throws Exception {
MessageId parent = new MessageId(TestUtils.getRandomId());
// Create a maximum-length group // Create a maximum-length group
String groupName = createRandomString(MAX_GROUP_NAME_LENGTH); String groupName = createRandomString(MAX_GROUP_NAME_LENGTH);
byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH]; byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
@@ -124,8 +125,8 @@ public class ConstantsTest extends BriarTestCase {
crypto.generateSignatureKeyPair().getPrivate(); crypto.generateSignatureKeyPair().getPrivate();
String subject = createRandomString(MAX_SUBJECT_LENGTH); String subject = createRandomString(MAX_SUBJECT_LENGTH);
byte[] body = new byte[MAX_BODY_LENGTH]; byte[] body = new byte[MAX_BODY_LENGTH];
Message message = messageFactory.createMessage(null, group, Message message = messageFactory.createPseudonymousMessage(parent,
groupPrivate, author, authorPrivate, subject, body); group, groupPrivate, author, authorPrivate, subject, body);
// Check the size of the serialised message // Check the size of the serialised message
int length = message.getSerialised().length; int length = message.getSerialised().length;
assertTrue(length > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH assertTrue(length > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH

View File

@@ -115,7 +115,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
String subject = "Hello"; String subject = "Hello";
byte[] body = "Hi Bob!".getBytes("UTF-8"); byte[] body = "Hi Bob!".getBytes("UTF-8");
MessageFactory messageFactory = alice.getInstance(MessageFactory.class); MessageFactory messageFactory = alice.getInstance(MessageFactory.class);
Message message = messageFactory.createMessage(null, subject, body); Message message = messageFactory.createPrivateMessage(null, subject,
body);
db.addLocalPrivateMessage(message, contactId); db.addLocalPrivateMessage(message, contactId);
// Create an outgoing simplex connection // Create an outgoing simplex connection
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();