From a42d9eec1cf5a821f0aacb9a5031e58eb32e0f46 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Fri, 13 May 2022 11:45:42 +0200 Subject: [PATCH 1/4] Include mailbox API version in local and remote mailbox properties This changes the format of the mailbox properties update message, so the major version of the client is bumped. --- .../bramble/api/client/ClientHelper.java | 12 +- .../api/mailbox/MailboxPropertiesUpdate.java | 35 +--- .../MailboxPropertiesUpdateMailbox.java | 51 +++++ .../api/mailbox/MailboxPropertyManager.java | 2 +- .../api/mailbox/MailboxSettingsManager.java | 5 +- .../RemoteMailboxPropertiesUpdateEvent.java | 5 +- .../briarproject/bramble/test/TestUtils.java | 20 +- .../bramble/client/ClientHelperImpl.java | 46 ++++- .../bramble/mailbox/MailboxApi.java | 11 + .../mailbox/MailboxPairingTaskImpl.java | 2 +- .../mailbox/MailboxPropertyManagerImpl.java | 80 +++++--- .../mailbox/MailboxPropertyValidator.java | 14 +- .../mailbox/MailboxSettingsManagerImpl.java | 2 +- .../bramble/client/ClientHelperImplTest.java | 122 ++++++++++-- .../mailbox/MailboxPairingTaskImplTest.java | 10 +- .../MailboxPropertyManagerImplTest.java | 188 ++++++++++++------ .../mailbox/MailboxPropertyValidatorTest.java | 39 +++- 17 files changed, 473 insertions(+), 171 deletions(-) create mode 100644 bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java b/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java index 262d82c9b..84893c56c 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java @@ -21,8 +21,6 @@ import java.security.GeneralSecurityException; import java.util.Collection; import java.util.Map; -import javax.annotation.Nullable; - @NotNullByDefault public interface ClientHelper { @@ -127,15 +125,13 @@ public interface ClientHelper { BdfDictionary properties) throws FormatException; /** - * Parse and validate the property dictionary of a Mailbox property update - * message. + * Parse and validate the elements of a Mailbox property update message. * - * @return the properties for using the Mailbox, or null if there is no - * Mailbox available - * @throws FormatException if the properties are not valid + * @return the parsed update message + * @throws FormatException if the message elements are invalid */ - @Nullable MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( + BdfList clientSupports, BdfList serverSupports, BdfDictionary properties) throws FormatException; /** diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java index fb37448dd..8319d1ec5 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java @@ -2,40 +2,27 @@ package org.briarproject.bramble.api.mailbox; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; +import java.util.List; + import javax.annotation.concurrent.Immutable; @Immutable @NotNullByDefault public class MailboxPropertiesUpdate { - private final String onion; - private final MailboxAuthToken authToken; - private final MailboxFolderId inboxId; - private final MailboxFolderId outboxId; + boolean hasMailbox; + private final List clientSupports; - public MailboxPropertiesUpdate(String onion, - MailboxAuthToken authToken, MailboxFolderId inboxId, - MailboxFolderId outboxId) { - this.onion = onion; - this.authToken = authToken; - this.inboxId = inboxId; - this.outboxId = outboxId; + public MailboxPropertiesUpdate(List clientSupports) { + this.hasMailbox = false; + this.clientSupports = clientSupports; } - public String getOnion() { - return onion; + public List getClientSupports() { + return clientSupports; } - public MailboxAuthToken getAuthToken() { - return authToken; + public boolean hasMailbox() { + return hasMailbox; } - - public MailboxFolderId getInboxId() { - return inboxId; - } - - public MailboxFolderId getOutboxId() { - return outboxId; - } - } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java new file mode 100644 index 000000000..2992c49ab --- /dev/null +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java @@ -0,0 +1,51 @@ +package org.briarproject.bramble.api.mailbox; + +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; + +import java.util.List; + +import javax.annotation.concurrent.Immutable; + +@Immutable +@NotNullByDefault +public class MailboxPropertiesUpdateMailbox extends MailboxPropertiesUpdate { + private final List serverSupports; + private final String onion; + private final MailboxAuthToken authToken; + private final MailboxFolderId inboxId; + private final MailboxFolderId outboxId; + + public MailboxPropertiesUpdateMailbox(List clientSupports, + List serverSupports, String onion, + MailboxAuthToken authToken, MailboxFolderId inboxId, + MailboxFolderId outboxId + ) { + super(clientSupports); + this.hasMailbox = true; + this.serverSupports = serverSupports; + this.onion = onion; + this.authToken = authToken; + this.inboxId = inboxId; + this.outboxId = outboxId; + } + + public String getOnion() { + return onion; + } + + public MailboxAuthToken getAuthToken() { + return authToken; + } + + public MailboxFolderId getInboxId() { + return inboxId; + } + + public MailboxFolderId getOutboxId() { + return outboxId; + } + + public List getServerSupports() { + return serverSupports; + } +} diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java index b0ecf8fdd..6180ba152 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java @@ -20,7 +20,7 @@ public interface MailboxPropertyManager { /** * The current major version of the mailbox property client. */ - int MAJOR_VERSION = 0; + int MAJOR_VERSION = 1; /** * The current minor version of the mailbox property client. diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxSettingsManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxSettingsManager.java index e2a4c7110..3bd50d6c6 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxSettingsManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxSettingsManager.java @@ -7,6 +7,8 @@ import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; +import java.util.List; + import javax.annotation.Nullable; @NotNullByDefault @@ -49,7 +51,8 @@ public interface MailboxSettingsManager { * @param txn A read-write transaction * @param ownOnion Our new mailbox's onion (56 base32 chars) */ - void mailboxPaired(Transaction txn, String ownOnion) + void mailboxPaired(Transaction txn, String ownOnion, + List serverSupports) throws DbException; /** diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java index 3d63acd37..649c5c41e 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java @@ -4,7 +4,6 @@ import org.briarproject.bramble.api.contact.ContactId; import org.briarproject.bramble.api.event.Event; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -16,11 +15,10 @@ import javax.annotation.concurrent.Immutable; public class RemoteMailboxPropertiesUpdateEvent extends Event { private final ContactId contactId; - @Nullable private final MailboxPropertiesUpdate mailboxPropertiesUpdate; public RemoteMailboxPropertiesUpdateEvent(ContactId contactId, - @Nullable MailboxPropertiesUpdate mailboxPropertiesUpdate) { + MailboxPropertiesUpdate mailboxPropertiesUpdate) { this.contactId = contactId; this.mailboxPropertiesUpdate = mailboxPropertiesUpdate; } @@ -29,7 +27,6 @@ public class RemoteMailboxPropertiesUpdateEvent extends Event { return contactId; } - @Nullable public MailboxPropertiesUpdate getMailboxPropertiesUpdate() { return mailboxPropertiesUpdate; } diff --git a/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java b/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java index 0f4a4748d..fa01c7beb 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java @@ -22,6 +22,7 @@ import org.briarproject.bramble.api.identity.Identity; import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.sync.ClientId; @@ -286,10 +287,21 @@ public class TestUtils { if (a == null || b == null) { return a == b; } - return a.getOnion().equals(b.getOnion()) && - a.getAuthToken().equals(b.getAuthToken()) && - a.getInboxId().equals(b.getInboxId()) && - a.getOutboxId().equals(b.getOutboxId()); + if (!a.hasMailbox() && !b.hasMailbox()) { + return a.getClientSupports().equals(b.getClientSupports()); + } else if (a.hasMailbox() && b.hasMailbox()) { + MailboxPropertiesUpdateMailbox am = + (MailboxPropertiesUpdateMailbox) a; + MailboxPropertiesUpdateMailbox bm = + (MailboxPropertiesUpdateMailbox) b; + return am.getClientSupports().equals(bm.getClientSupports()) && + am.getServerSupports().equals(bm.getServerSupports()) && + am.getOnion().equals(bm.getOnion()) && + am.getAuthToken().equals(bm.getAuthToken()) && + am.getInboxId().equals(bm.getInboxId()) && + am.getOutboxId().equals(bm.getOutboxId()); + } + return false; } public static boolean mailboxPropertiesEqual( diff --git a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java index bab9a18e6..39d9079d1 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java @@ -26,6 +26,8 @@ import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.properties.TransportProperties; @@ -39,12 +41,13 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import javax.inject.Inject; @@ -412,11 +415,28 @@ class ClientHelperImpl implements ClientHelper { } @Override - @Nullable public MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( + BdfList clientSupports, BdfList serverSupports, BdfDictionary properties) throws FormatException { + List clientSupportsList = + getMailboxVersionList(clientSupports); + List serverSupportsList = + getMailboxVersionList(serverSupports); + + // We must always learn what Mailbox API version(s) the client supports + if (clientSupports.isEmpty()) { + throw new FormatException(); + } if (properties.isEmpty()) { - return null; + // No mailbox -- cannot claim to support any API versions! + if (!serverSupports.isEmpty()) { + throw new FormatException(); + } + return new MailboxPropertiesUpdate(clientSupportsList); + } + // Mailbox must be accompanied by the Mailbox API version(s) it supports + if (serverSupports.isEmpty()) { + throw new FormatException(); } // Accepting more props than we need, for forward compatibility if (properties.size() < PROP_COUNT) { @@ -435,9 +455,23 @@ class ClientHelperImpl implements ClientHelper { checkLength(inboxId, UniqueId.LENGTH); byte[] outboxId = properties.getRaw(PROP_KEY_OUTBOXID); checkLength(outboxId, UniqueId.LENGTH); - return new MailboxPropertiesUpdate(onion, - new MailboxAuthToken(authToken), new MailboxFolderId(inboxId), - new MailboxFolderId(outboxId)); + return new MailboxPropertiesUpdateMailbox(clientSupportsList, + serverSupportsList, onion, new MailboxAuthToken(authToken), + new MailboxFolderId(inboxId), new MailboxFolderId(outboxId)); + } + + private List getMailboxVersionList(BdfList bdfList) + throws FormatException { + List list = new ArrayList<>(); + for (int i = 0; i < bdfList.size(); i++) { + BdfList element = bdfList.getList(i); + if (element.size() != 2) { + throw new FormatException(); + } + list.add(new MailboxVersion(element.getLong(0).intValue(), + element.getLong(1).intValue())); + } + return list; } @Override diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java index 9596ce2f3..22d00a18f 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java @@ -7,6 +7,8 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFileId; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; +import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import java.io.File; @@ -17,9 +19,18 @@ import java.util.List; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; +import static java.util.Collections.singletonList; + @NotNullByDefault interface MailboxApi { + /** + * Mailbox API versions that we support as a client. This is reported to our + * contacts by {@link MailboxPropertyManager}. + */ + List CLIENT_SUPPORTS = singletonList( + new MailboxVersion(1, 0)); + /** * Sets up the mailbox with the setup token. * diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java index 7dabeaa2c..8236e07e2 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java @@ -127,7 +127,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask { for (Contact c : db.getContacts(txn)) { MailboxPropertiesUpdate remoteProps = mailboxPropertyManager .getRemoteProperties(txn, c.getId()); - if (remoteProps == null) { + if (remoteProps == null || !remoteProps.hasMailbox()) { db.resetUnackedMessagesToSend(txn, c.getId()); } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java index c1db5697d..f2ca77084 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java @@ -19,9 +19,11 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager.MailboxHook; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.sync.Group; @@ -35,6 +37,7 @@ import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.versioning.ClientVersioningManager; import org.briarproject.bramble.api.versioning.ClientVersioningManager.ClientVersioningHook; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -42,6 +45,7 @@ import javax.annotation.Nullable; import javax.inject.Inject; import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE; +import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS; @NotNullByDefault class MailboxPropertyManagerImpl implements MailboxPropertyManager, @@ -100,11 +104,15 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, db.setGroupVisibility(txn, c.getId(), g.getId(), client); // Attach the contact ID to the group clientHelper.setContactId(txn, g.getId(), c.getId()); - // If we are paired, create and send props to the newly added contact MailboxProperties ownProps = mailboxSettingsManager.getOwnMailboxProperties(txn); if (ownProps != null) { - createAndSendProperties(txn, c, ownProps.getOnion()); + // We are paired, create and send props to the newly added contact + createAndSendProperties(txn, c, ownProps.getServerSupports(), + ownProps.getOnion()); + } else { + // Not paired, but we still want to get our clientSupports sent + sendEmptyProperties(txn, c); } } @@ -114,10 +122,11 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } @Override - public void mailboxPaired(Transaction txn, String ownOnion) + public void mailboxPaired(Transaction txn, String ownOnion, + List serverSupports) throws DbException { for (Contact c : db.getContacts(txn)) { - createAndSendProperties(txn, c, ownOnion); + createAndSendProperties(txn, c, serverSupports, ownOnion); } } @@ -186,12 +195,15 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, /** * Creates and sends an update message to the given contact. The message - * holds our own mailbox's onion, and generated unique properties. All of - * which the contact needs to communicate with our Mailbox. + * holds our own mailbox's onion, generated unique properties, and lists of + * supported Mailbox API version(s). All of which the contact needs to + * communicate with our Mailbox. */ - private void createAndSendProperties(Transaction txn, - Contact c, String ownOnion) throws DbException { - MailboxPropertiesUpdate p = new MailboxPropertiesUpdate(ownOnion, + private void createAndSendProperties(Transaction txn, Contact c, + List serverSupports, String ownOnion) + throws DbException { + MailboxPropertiesUpdate p = new MailboxPropertiesUpdateMailbox( + CLIENT_SUPPORTS, serverSupports, ownOnion, new MailboxAuthToken(crypto.generateUniqueId().getBytes()), new MailboxFolderId(crypto.generateUniqueId().getBytes()), new MailboxFolderId(crypto.generateUniqueId().getBytes())); @@ -200,14 +212,17 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } /** - * Sends an empty update message to the given contact. The empty update - * indicates for the receiving contact that we no longer have a Mailbox that - * they can use. + * Sends an update message with empty properties to the given contact. The + * empty update indicates for the receiving contact that we don't have any + * Mailbox that they can use. It still includes the list of Mailbox API + * version(s) that we support as a client. */ private void sendEmptyProperties(Transaction txn, Contact c) throws DbException { Group g = getContactGroup(c); - storeMessageReplaceLatest(txn, g.getId(), null); + MailboxPropertiesUpdate p = + new MailboxPropertiesUpdate(CLIENT_SUPPORTS); + storeMessageReplaceLatest(txn, g.getId(), p); } @Nullable @@ -229,7 +244,7 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } private void storeMessageReplaceLatest(Transaction txn, GroupId g, - @Nullable MailboxPropertiesUpdate p) throws DbException { + MailboxPropertiesUpdate p) throws DbException { try { LatestUpdate latest = findLatest(txn, g, true); long version = latest == null ? 1 : latest.version + 1; @@ -266,23 +281,38 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, return null; } - private BdfList encodeProperties(long version, - @Nullable MailboxPropertiesUpdate p) { + private BdfList encodeProperties(long version, MailboxPropertiesUpdate p) { BdfDictionary dict = new BdfDictionary(); - if (p != null) { - dict.put(PROP_KEY_ONION, p.getOnion()); - dict.put(PROP_KEY_AUTHTOKEN, p.getAuthToken().getBytes()); - dict.put(PROP_KEY_INBOXID, p.getInboxId().getBytes()); - dict.put(PROP_KEY_OUTBOXID, p.getOutboxId().getBytes()); + BdfList serverSupports = new BdfList(); + if (p.hasMailbox()) { + MailboxPropertiesUpdateMailbox pm = + (MailboxPropertiesUpdateMailbox) p; + serverSupports = encodeSupportsList(pm.getServerSupports()); + dict.put(PROP_KEY_ONION, pm.getOnion()); + dict.put(PROP_KEY_AUTHTOKEN, pm.getAuthToken().getBytes()); + dict.put(PROP_KEY_INBOXID, pm.getInboxId().getBytes()); + dict.put(PROP_KEY_OUTBOXID, pm.getOutboxId().getBytes()); } - return BdfList.of(version, dict); + return BdfList.of(version, encodeSupportsList(p.getClientSupports()), + serverSupports, dict); + } + + private BdfList encodeSupportsList(List supportsList) { + BdfList supports = new BdfList(); + for (MailboxVersion version : supportsList) { + supports.add(BdfList.of(version.getMajor(), version.getMinor())); + } + return supports; } - @Nullable private MailboxPropertiesUpdate parseProperties(BdfList body) throws FormatException { - BdfDictionary dict = body.getDictionary(1); - return clientHelper.parseAndValidateMailboxPropertiesUpdate(dict); + BdfList clientSupports = body.getList(1); + BdfList serverSupports = body.getList(2); + BdfDictionary dict = body.getDictionary(3); + return clientHelper.parseAndValidateMailboxPropertiesUpdate( + clientSupports, serverSupports, dict + ); } private Group getContactGroup(Contact c) { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java index 32b581fc0..8d279c8bd 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java @@ -31,14 +31,20 @@ class MailboxPropertyValidator extends BdfMessageValidator { @Override protected BdfMessageContext validateMessage(Message m, Group g, BdfList body) throws InvalidMessageException, FormatException { - // Version, properties - checkSize(body, 2); + // Version, Properties, clientSupports, serverSupports + checkSize(body, 4); // Version long version = body.getLong(0); if (version < 0) throw new FormatException(); + // clientSupports + BdfList clientSupports = body.getList(1); + // serverSupports + BdfList serverSupports = body.getList(2); // Properties - BdfDictionary dictionary = body.getDictionary(1); - clientHelper.parseAndValidateMailboxPropertiesUpdate(dictionary); + BdfDictionary dictionary = body.getDictionary(3); + clientHelper.parseAndValidateMailboxPropertiesUpdate(clientSupports, + serverSupports, dictionary + ); // Return the metadata BdfDictionary meta = new BdfDictionary(); meta.put(MSG_KEY_VERSION, version); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSettingsManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSettingsManagerImpl.java index ae705784e..eee2a49c2 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSettingsManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSettingsManagerImpl.java @@ -91,7 +91,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager { s.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS, ints); settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE); for (MailboxHook hook : hooks) { - hook.mailboxPaired(txn, p.getOnion()); + hook.mailboxPaired(txn, p.getOnion(), p.getServerSupports()); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java index 6214d67c5..b5dd6d06c 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java @@ -24,6 +24,8 @@ import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.MessageFactory; @@ -41,6 +43,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; +import static java.util.Collections.singletonList; 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.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; @@ -58,7 +61,7 @@ import static org.briarproject.bramble.test.TestUtils.mailboxPropertiesUpdateEqu import static org.briarproject.bramble.util.StringUtils.getRandomString; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -95,10 +98,20 @@ public class ClientHelperImplTest extends BrambleMockTestCase { messageFactory, bdfReaderFactory, bdfWriterFactory, metadataParser, metadataEncoder, cryptoComponent, authorFactory); - private final MailboxPropertiesUpdate validMailboxPropsUpdate; + private final MailboxPropertiesUpdateMailbox validMailboxPropsUpdate; + private final BdfList emptyClientSupports; + private final BdfList someClientSupports; + private final BdfList emptyServerSupports; + private final BdfList someServerSupports; public ClientHelperImplTest() { - validMailboxPropsUpdate = new MailboxPropertiesUpdate( + emptyClientSupports = new BdfList(); + someClientSupports = BdfList.of(BdfList.of(1, 0)); + emptyServerSupports = new BdfList(); + someServerSupports = BdfList.of(BdfList.of(1, 0)); + validMailboxPropsUpdate = new MailboxPropertiesUpdateMailbox( + singletonList(new MailboxVersion(1, 0)), + singletonList(new MailboxVersion(1, 0)), "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd", new MailboxAuthToken(getRandomId()), new MailboxFolderId(getRandomId()), @@ -546,23 +559,84 @@ public class ClientHelperImplTest extends BrambleMockTestCase { }}); } + @Test(expected = FormatException.class) + public void testRejectsMailboxPropsWithEmptyClientSupports() + throws Exception { + BdfDictionary emptyPropsDict = new BdfDictionary(); + clientHelper.parseAndValidateMailboxPropertiesUpdate( + emptyClientSupports, emptyServerSupports, emptyPropsDict + ); + } + @Test public void testParseEmptyMailboxPropsUpdate() throws Exception { BdfDictionary emptyPropsDict = new BdfDictionary(); MailboxPropertiesUpdate parsedProps = clientHelper - .parseAndValidateMailboxPropertiesUpdate(emptyPropsDict); - assertNull(parsedProps); + .parseAndValidateMailboxPropertiesUpdate(someClientSupports, + emptyServerSupports, emptyPropsDict + ); + assertFalse(parsedProps.hasMailbox()); + } + + @Test(expected = FormatException.class) + public void testRejectsEmptyMailboxPropsWithSomeServerSupports() + throws Exception { + BdfDictionary emptyPropsDict = new BdfDictionary(); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, emptyPropsDict + ); + } + + @Test(expected = FormatException.class) + public void testRejectsMailboxPropsShortSupports() throws Exception { + clientHelper.parseAndValidateMailboxPropertiesUpdate( + BdfList.of(BdfList.of(1)), emptyServerSupports, + new BdfDictionary() + ); + } + + @Test(expected = FormatException.class) + public void testRejectsMailboxPropsLongSupports() throws Exception { + clientHelper.parseAndValidateMailboxPropertiesUpdate( + BdfList.of(BdfList.of(1, 0, 0)), emptyServerSupports, + new BdfDictionary() + ); + } + + @Test(expected = FormatException.class) + public void testRejectsMailboxPropsNonIntSupports() throws Exception { + clientHelper.parseAndValidateMailboxPropertiesUpdate( + BdfList.of(BdfList.of(1, "0")), emptyServerSupports, + new BdfDictionary() + ); + } + + @Test(expected = FormatException.class) + public void testRejectsMailboxPropsNonListSupports() throws Exception { + clientHelper.parseAndValidateMailboxPropertiesUpdate( + BdfList.of("non-list"), emptyServerSupports, new BdfDictionary() + ); } @Test public void testParseValidMailboxPropsUpdate() throws Exception { MailboxPropertiesUpdate parsedProps = clientHelper .parseAndValidateMailboxPropertiesUpdate( - getValidMailboxPropsUpdateDict()); + someClientSupports, someServerSupports, + getValidMailboxPropsUpdateDict() + ); assertTrue(mailboxPropertiesUpdateEqual(validMailboxPropsUpdate, parsedProps)); } + @Test(expected = FormatException.class) + public void rejectsMailboxPropsWithEmptyServerSupports() throws Exception { + clientHelper.parseAndValidateMailboxPropertiesUpdate( + someClientSupports, emptyServerSupports, + getValidMailboxPropsUpdateDict() + ); + } + @Test(expected = FormatException.class) public void testRejectsMailboxPropsUpdateOnionNotDecodable() throws Exception { @@ -570,7 +644,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase { String badOnion = "!" + propsDict.getString(PROP_KEY_ONION) .substring(1); propsDict.put(PROP_KEY_ONION, badOnion); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + emptyServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -579,7 +655,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); String tooLongOnion = propsDict.getString(PROP_KEY_ONION) + "!"; propsDict.put(PROP_KEY_ONION, tooLongOnion); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + emptyServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -587,7 +665,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase { throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.put(PROP_KEY_INBOXID, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -595,7 +675,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase { throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.put(PROP_KEY_OUTBOXID, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -603,14 +685,18 @@ public class ClientHelperImplTest extends BrambleMockTestCase { throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.put(PROP_KEY_AUTHTOKEN, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) public void testRejectsMailboxPropsUpdateMissingOnion() throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.remove(PROP_KEY_ONION); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -618,14 +704,18 @@ public class ClientHelperImplTest extends BrambleMockTestCase { throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.remove(PROP_KEY_AUTHTOKEN); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) public void testRejectsMailboxPropsUpdateMissingInboxId() throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.remove(PROP_KEY_INBOXID); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } @Test(expected = FormatException.class) @@ -633,7 +723,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase { throws Exception { BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); propsDict.remove(PROP_KEY_OUTBOXID); - clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); + clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + someServerSupports, propsDict + ); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java index 70d6477a7..bd60e24be 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java @@ -9,6 +9,7 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxPairingState; import org.briarproject.bramble.api.mailbox.MailboxPairingTask; import org.briarproject.bramble.api.mailbox.MailboxProperties; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent; @@ -24,10 +25,11 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Collections; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicInteger; +import static java.util.Collections.singletonList; +import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS; import static org.briarproject.bramble.test.TestUtils.getContact; import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomId; @@ -105,16 +107,18 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase { }}); Contact contact1 = getContact(); Transaction txn = new Transaction(null, false); + MailboxPropertiesUpdate emptyProps = new MailboxPropertiesUpdate( + CLIENT_SUPPORTS); context.checking(new DbExpectations() {{ oneOf(db).transaction(with(false), withDbRunnable(txn)); oneOf(mailboxSettingsManager).setOwnMailboxProperties( with(txn), with(matches(ownerProperties))); oneOf(mailboxSettingsManager).recordSuccessfulConnection(txn, time); oneOf(db).getContacts(txn); - will(returnValue(Collections.singletonList(contact1))); + will(returnValue(singletonList(contact1))); oneOf(mailboxPropertyManager).getRemoteProperties(txn, contact1.getId()); - will(returnValue(null)); + will(returnValue(emptyProps)); oneOf(db).resetUnackedMessagesToSend(txn, contact1.getId()); }}); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java index fd2c6ab22..679555d53 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java @@ -15,7 +15,9 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.GroupId; @@ -27,7 +29,6 @@ import org.briarproject.bramble.test.BrambleMockTestCase; import org.jmock.Expectations; import org.junit.Test; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -43,6 +44,7 @@ import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_K import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_OUTBOXID; import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED; import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE; +import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS; import static org.briarproject.bramble.test.TestUtils.getContact; import static org.briarproject.bramble.test.TestUtils.getGroup; import static org.briarproject.bramble.test.TestUtils.getMessage; @@ -72,21 +74,44 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { private final Group localGroup = getGroup(CLIENT_ID, MAJOR_VERSION); private final BdfDictionary propsDict; private final BdfDictionary emptyPropsDict = new BdfDictionary(); - private final MailboxPropertiesUpdate props; + private final BdfList realClientSupports; + private final BdfList someClientSupports; + private final List someClientSupportsList; + private final BdfList emptyServerSupports; + private final BdfList someServerSupports; + private final List someServerSupportsList; + private final MailboxPropertiesUpdateMailbox updateMailbox; + private final MailboxPropertiesUpdate updateNoMailbox; private final MailboxProperties ownProps; public MailboxPropertyManagerImplTest() { + someClientSupports = BdfList.of(BdfList.of(1, 0)); + someClientSupportsList = singletonList(new MailboxVersion(1, 0)); + emptyServerSupports = new BdfList(); + someServerSupports = BdfList.of(BdfList.of(1, 0)); + someServerSupportsList = singletonList(new MailboxVersion(1, 0)); + realClientSupports = new BdfList(); + for (MailboxVersion v : CLIENT_SUPPORTS) { + realClientSupports.add(BdfList.of(v.getMajor(), v.getMinor())); + } ownProps = new MailboxProperties("http://bar.onion", - new MailboxAuthToken(getRandomId()), true, new ArrayList<>()); - props = new MailboxPropertiesUpdate(ownProps.getOnion(), + new MailboxAuthToken(getRandomId()), true, + someServerSupportsList); + updateMailbox = new MailboxPropertiesUpdateMailbox( + singletonList(new MailboxVersion(1, 0)), + singletonList(new MailboxVersion(1, 0)), + ownProps.getOnion(), new MailboxAuthToken(getRandomId()), new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId())); propsDict = new BdfDictionary(); - propsDict.put(PROP_KEY_ONION, props.getOnion()); - propsDict.put(PROP_KEY_AUTHTOKEN, props.getAuthToken().getBytes()); - propsDict.put(PROP_KEY_INBOXID, props.getInboxId().getBytes()); - propsDict.put(PROP_KEY_OUTBOXID, props.getOutboxId().getBytes()); + propsDict.put(PROP_KEY_ONION, updateMailbox.getOnion()); + propsDict.put(PROP_KEY_AUTHTOKEN, + updateMailbox.getAuthToken().getBytes()); + propsDict.put(PROP_KEY_INBOXID, updateMailbox.getInboxId().getBytes()); + propsDict.put(PROP_KEY_OUTBOXID, + updateMailbox.getOutboxId().getBytes()); + updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList); } private MailboxPropertyManagerImpl createInstance() { @@ -105,6 +130,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); + Map messageMetadata = new LinkedHashMap<>(); context.checking(new Expectations() {{ oneOf(db).containsGroup(txn, localGroup.getId()); @@ -125,6 +151,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { contact.getId()); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(null)); + oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, + MAJOR_VERSION, contact); + will(returnValue(contactGroup)); + oneOf(clientHelper).getMessageMetadataAsDictionary(txn, + contactGroup.getId()); + will(returnValue(messageMetadata)); + expectStoreMessage(txn, contactGroup.getId(), 1, realClientSupports, + emptyServerSupports, emptyPropsDict, true); }}); MailboxPropertyManagerImpl t = createInstance(); @@ -159,18 +193,19 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(ownProps)); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getAuthToken())); + will(returnValue(updateMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getInboxId())); + will(returnValue(updateMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getOutboxId())); + will(returnValue(updateMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - expectStoreMessage(txn, contactGroup.getId(), propsDict, 1, true); + expectStoreMessage(txn, contactGroup.getId(), 1, realClientSupports, + someServerSupports, propsDict, true); }}); MailboxPropertyManagerImpl t = createInstance(); @@ -197,6 +232,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); + Map messageMetadata = new LinkedHashMap<>(); context.checking(new Expectations() {{ // Create the group and share it with the contact @@ -213,6 +249,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { contact.getId()); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(null)); + oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, + MAJOR_VERSION, contact); + will(returnValue(contactGroup)); + oneOf(clientHelper).getMessageMetadataAsDictionary(txn, + contactGroup.getId()); + will(returnValue(messageMetadata)); + expectStoreMessage(txn, contactGroup.getId(), 1, realClientSupports, + emptyServerSupports, emptyPropsDict, true); }}); MailboxPropertyManagerImpl t = createInstance(); @@ -243,18 +287,19 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(ownProps)); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getAuthToken())); + will(returnValue(updateMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getInboxId())); + will(returnValue(updateMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getOutboxId())); + will(returnValue(updateMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - expectStoreMessage(txn, contactGroup.getId(), propsDict, 1, true); + expectStoreMessage(txn, contactGroup.getId(), 1, someClientSupports, + someServerSupports, propsDict, true); }}); MailboxPropertyManagerImpl t = createInstance(); @@ -285,7 +330,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { Contact contact = getContact(); GroupId contactGroupId = new GroupId(getRandomId()); Message message = getMessage(contactGroupId); - BdfList body = BdfList.of(1, propsDict); + BdfList body = BdfList.of(1, someClientSupports, someServerSupports, + propsDict); Metadata meta = new Metadata(); BdfDictionary metaDictionary = BdfDictionary.of( new BdfEntry(MSG_KEY_VERSION, 1), @@ -310,8 +356,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageAsList(txn, message.getId()); will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - propsDict); - will(returnValue(props)); + someClientSupports, someServerSupports, propsDict); + will(returnValue(updateMailbox)); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); }}); @@ -328,7 +374,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { Contact contact = getContact(); GroupId contactGroupId = new GroupId(getRandomId()); Message message = getMessage(contactGroupId); - BdfList body = BdfList.of(1, propsDict); + BdfList body = BdfList.of(1, someClientSupports, someServerSupports, + propsDict); Metadata meta = new Metadata(); BdfDictionary metaDictionary = BdfDictionary.of( new BdfEntry(MSG_KEY_VERSION, 2), @@ -361,8 +408,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageAsList(txn, message.getId()); will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - propsDict); - will(returnValue(props)); + someClientSupports, someServerSupports, propsDict); + will(returnValue(updateMailbox)); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); }}); @@ -430,23 +477,24 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).getContacts(txn); will(returnValue(contacts)); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getAuthToken())); + will(returnValue(updateMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getInboxId())); + will(returnValue(updateMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(props.getOutboxId())); + will(returnValue(updateMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - expectStoreMessage(txn, contactGroup.getId(), propsDict, 2, true); + expectStoreMessage(txn, contactGroup.getId(), 2, someClientSupports, + someServerSupports, propsDict, true); oneOf(db).removeMessage(txn, latestId); }}); MailboxPropertyManagerImpl t = createInstance(); - t.mailboxPaired(txn, ownProps.getOnion()); + t.mailboxPaired(txn, ownProps.getOnion(), someServerSupportsList); } @Test @@ -478,8 +526,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - expectStoreMessage(txn, contactGroup.getId(), emptyPropsDict, - 2, true); + expectStoreMessage(txn, contactGroup.getId(), 2, someClientSupports, + emptyServerSupports, emptyPropsDict, true); oneOf(db).removeMessage(txn, latestId); }}); @@ -498,9 +546,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { new BdfEntry(MSG_KEY_LOCAL, false) ); Map messageMetadata = new LinkedHashMap<>(); - MessageId fooUpdateId = new MessageId(getRandomId()); - messageMetadata.put(fooUpdateId, metaDictionary); - BdfList fooUpdate = BdfList.of(1, propsDict); + MessageId messageId = new MessageId(getRandomId()); + messageMetadata.put(messageId, metaDictionary); + BdfList body = BdfList.of(1, someClientSupports, someServerSupports, + propsDict); context.checking(new Expectations() {{ oneOf(db).getContact(txn, contact.getId()); @@ -511,17 +560,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); - will(returnValue(fooUpdate)); + oneOf(clientHelper).getMessageAsList(txn, messageId); + will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - propsDict); - will(returnValue(props)); + someClientSupports, someServerSupports, propsDict); + will(returnValue(updateMailbox)); }}); MailboxPropertyManagerImpl t = createInstance(); MailboxPropertiesUpdate remote = t.getRemoteProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(remote, props)); + assertTrue(mailboxPropertiesUpdateEqual(remote, updateMailbox)); } @Test @@ -549,7 +598,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { } @Test - public void testGetRemotePropertiesReturnsNullBecauseEmptyUpdate() + public void testGetRemotePropertiesNoMailbox() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); @@ -559,9 +608,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { new BdfEntry(MSG_KEY_LOCAL, false) ); Map messageMetadata = new LinkedHashMap<>(); - MessageId fooUpdateId = new MessageId(getRandomId()); - messageMetadata.put(fooUpdateId, metaDictionary); - BdfList fooUpdate = BdfList.of(1, emptyPropsDict); + MessageId messageId = new MessageId(getRandomId()); + messageMetadata.put(messageId, metaDictionary); + BdfList body = BdfList.of(1, someClientSupports, emptyServerSupports, + emptyPropsDict); context.checking(new Expectations() {{ oneOf(db).getContact(txn, contact.getId()); @@ -572,15 +622,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); - will(returnValue(fooUpdate)); + oneOf(clientHelper).getMessageAsList(txn, messageId); + will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - emptyPropsDict); - will(returnValue(null)); + someClientSupports, emptyServerSupports, emptyPropsDict); + will(returnValue(updateNoMailbox)); }}); MailboxPropertyManagerImpl t = createInstance(); - assertNull(t.getRemoteProperties(txn, contact.getId())); + MailboxPropertiesUpdate remote = + t.getRemoteProperties(txn, contact.getId()); + assertTrue(mailboxPropertiesUpdateEqual(remote, updateNoMailbox)); } @Test @@ -594,9 +646,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { new BdfEntry(MSG_KEY_LOCAL, true) ); Map messageMetadata = new LinkedHashMap<>(); - MessageId fooUpdateId = new MessageId(getRandomId()); - messageMetadata.put(fooUpdateId, metaDictionary); - BdfList fooUpdate = BdfList.of(1, propsDict); + MessageId messageId = new MessageId(getRandomId()); + messageMetadata.put(messageId, metaDictionary); + BdfList body = BdfList.of(1, someClientSupports, someServerSupports, + propsDict); context.checking(new Expectations() {{ oneOf(db).getContact(txn, contact.getId()); @@ -607,17 +660,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); - will(returnValue(fooUpdate)); + oneOf(clientHelper).getMessageAsList(txn, messageId); + will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - propsDict); - will(returnValue(props)); + someClientSupports, someServerSupports, propsDict); + will(returnValue(updateMailbox)); }}); MailboxPropertyManagerImpl t = createInstance(); MailboxPropertiesUpdate local = t.getLocalProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(local, props)); + assertTrue(mailboxPropertiesUpdateEqual(local, updateMailbox)); } @Test @@ -645,7 +698,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { } @Test - public void testGetLocalPropertiesReturnsNullBecauseEmptyUpdate() + public void testGetLocalPropertiesNoMailbox() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); @@ -655,9 +708,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { new BdfEntry(MSG_KEY_LOCAL, true) ); Map messageMetadata = new LinkedHashMap<>(); - MessageId fooUpdateId = new MessageId(getRandomId()); - messageMetadata.put(fooUpdateId, metaDictionary); - BdfList fooUpdate = BdfList.of(1, emptyPropsDict); + MessageId messageId = new MessageId(getRandomId()); + messageMetadata.put(messageId, metaDictionary); + BdfList body = BdfList.of(1, someClientSupports, emptyServerSupports, + emptyPropsDict); context.checking(new Expectations() {{ oneOf(db).getContact(txn, contact.getId()); @@ -668,21 +722,25 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); - oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); - will(returnValue(fooUpdate)); + oneOf(clientHelper).getMessageAsList(txn, messageId); + will(returnValue(body)); oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - emptyPropsDict); - will(returnValue(null)); + someClientSupports, emptyServerSupports, emptyPropsDict); + will(returnValue(updateNoMailbox)); }}); MailboxPropertyManagerImpl t = createInstance(); - assertNull(t.getLocalProperties(txn, contact.getId())); + MailboxPropertiesUpdate local = + t.getLocalProperties(txn, contact.getId()); + assertTrue(mailboxPropertiesUpdateEqual(local, updateNoMailbox)); } private void expectStoreMessage(Transaction txn, GroupId g, - BdfDictionary properties, long version, boolean local) + long version, BdfList clientSupports, BdfList serverSupports, + BdfDictionary properties, boolean local) throws Exception { - BdfList body = BdfList.of(version, properties); + BdfList body = BdfList.of(version, clientSupports, serverSupports, + properties); Message message = getMessage(g); long timestamp = message.getTimestamp(); BdfDictionary meta = BdfDictionary.of( diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java index 0ee1747da..eea64ac5e 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java @@ -9,7 +9,9 @@ import org.briarproject.bramble.api.data.MetadataEncoder; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; +import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.system.Clock; @@ -18,7 +20,9 @@ import org.jmock.Expectations; import org.junit.Test; import java.io.IOException; +import java.util.List; +import static java.util.Collections.singletonList; import static org.briarproject.bramble.test.TestUtils.getGroup; import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getRandomId; @@ -29,7 +33,12 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { private final ClientHelper clientHelper = context.mock(ClientHelper.class); private final BdfDictionary bdfDict; - private final MailboxPropertiesUpdate mailboxProps; + private final BdfList emptyServerSupports; + private final BdfList someClientSupports; + private final List someClientSupportsList; + private final BdfList someServerSupports; + private final MailboxPropertiesUpdateMailbox updateMailbox; + private final MailboxPropertiesUpdate updateNoMailbox; private final Group group; private final Message message; private final MailboxPropertyValidator mpv; @@ -38,11 +47,21 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { // Just dummies, clientHelper is mocked so our test is a bit shallow; // not testing // {@link ClientHelper#parseAndValidateMailboxPropertiesUpdate(BdfDictionary)} + emptyServerSupports = new BdfList(); + someClientSupports = BdfList.of(BdfList.of(1, 0)); + someClientSupportsList = singletonList(new MailboxVersion(1, 0)); + someServerSupports = BdfList.of(BdfList.of(1, 0)); bdfDict = BdfDictionary.of(new BdfEntry("foo", "bar")); - mailboxProps = new MailboxPropertiesUpdate("baz", + + updateMailbox = new MailboxPropertiesUpdateMailbox( + singletonList(new MailboxVersion(1, 0)), + singletonList(new MailboxVersion(1, 0)), + "baz", new MailboxAuthToken(getRandomId()), new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId())); + updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList); + group = getGroup(MailboxPropertyManager.CLIENT_ID, MailboxPropertyManager.MAJOR_VERSION); @@ -56,12 +75,13 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { @Test public void testValidateMessageBody() throws IOException { - BdfList body = BdfList.of(4, bdfDict); + BdfList body = + BdfList.of(4, someClientSupports, someServerSupports, bdfDict); context.checking(new Expectations() {{ oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - bdfDict); - will(returnValue(mailboxProps)); + someClientSupports, someServerSupports, bdfDict); + will(returnValue(updateMailbox)); }}); BdfDictionary result = @@ -82,14 +102,15 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { } @Test - public void testEmptyPropertiesReturnsNull() throws IOException { + public void testEmptyProperties() throws IOException { BdfDictionary emptyBdfDict = new BdfDictionary(); - BdfList body = BdfList.of(42, emptyBdfDict); + BdfList body = BdfList.of(42, someClientSupports, emptyServerSupports, + emptyBdfDict); context.checking(new Expectations() {{ oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( - emptyBdfDict); - will(returnValue(null)); + someClientSupports, emptyServerSupports, emptyBdfDict); + will(returnValue(updateNoMailbox)); }}); BdfDictionary result = From 84afc6d9349fea5d55ac631de773e2223f369384 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Fri, 13 May 2022 11:46:41 +0200 Subject: [PATCH 2/4] Let integration tests mind the mailbox prop update when adding contact --- .../TransportKeyAgreementIntegrationTest.java | 7 ++++--- .../IntroductionIntegrationTest.java | 7 ++++--- .../briar/test/BriarIntegrationTest.java | 21 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java b/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java index d16b6052c..0b8adb015 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java @@ -345,11 +345,12 @@ public class TransportKeyAgreementIntegrationTest .canSendOutgoingStreams(aliceId, DUPLEX_TRANSPORT_ID)); } - // Sync initial client versioning updates + // Sync initial client versioning updates and mailbox properties updates syncMessage(alice, bob, bobId, 1, true); syncMessage(bob, alice, aliceId, 1, true); - syncMessage(alice, bob, bobId, 1, true); - sendAcks(bob, alice, aliceId, 1); + syncMessage(alice, bob, bobId, 2, true); + syncMessage(bob, alice, aliceId, 1, true); + sendAcks(alice, bob, bobId, 1); return new Pair<>(aliceId, bobId); } diff --git a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java index 29131fe8b..e022adf92 100644 --- a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java @@ -1051,11 +1051,12 @@ public class IntroductionIntegrationTest true); contact0From1 = contactManager1.getContact(contactId0From1); - // Sync initial client versioning updates and transport properties + // Sync initial client versioning updates, mailbox properties updates, + // and transport properties sync0To1(1, true); sync1To0(1, true); - sync0To1(2, true); - sync1To0(1, true); + sync0To1(3, true); + sync1To0(2, true); // a new introduction should be possible assertTrue(introductionManager0 diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java index 599ebef07..9226fde90 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java @@ -172,16 +172,18 @@ public abstract class BriarIntegrationTest Date: Mon, 16 May 2022 10:05:15 +0200 Subject: [PATCH 3/4] Assert that we have a local update --- .../api/mailbox/MailboxPropertyManager.java | 1 - .../mailbox/MailboxPropertyManagerImpl.java | 9 +++++-- .../MailboxPropertyManagerImplTest.java | 24 ------------------- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java index 6180ba152..e7e1d8d42 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java @@ -57,7 +57,6 @@ public interface MailboxPropertyManager { */ String MSG_KEY_LOCAL = "local"; - @Nullable MailboxPropertiesUpdate getLocalProperties(Transaction txn, ContactId c) throws DbException; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java index f2ca77084..c6eb94ab4 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java @@ -180,10 +180,15 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } @Override - @Nullable public MailboxPropertiesUpdate getLocalProperties(Transaction txn, ContactId c) throws DbException { - return getProperties(txn, db.getContact(txn, c), true); + MailboxPropertiesUpdate local = + getProperties(txn, db.getContact(txn, c), true); + // An update (with or without mailbox) is created when contact is added + if (local == null) { + throw new DbException(); + } + return local; } @Override diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java index 679555d53..63cfc2d1a 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java @@ -673,30 +673,6 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { assertTrue(mailboxPropertiesUpdateEqual(local, updateMailbox)); } - @Test - public void testGetLocalPropertiesReturnsNullBecauseNoUpdate() - throws Exception { - Transaction txn = new Transaction(null, false); - Contact contact = getContact(); - Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); - Map emptyMessageMetadata = - new LinkedHashMap<>(); - - context.checking(new Expectations() {{ - oneOf(db).getContact(txn, contact.getId()); - will(returnValue(contact)); - oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, - MAJOR_VERSION, contact); - will(returnValue(contactGroup)); - oneOf(clientHelper).getMessageMetadataAsDictionary(txn, - contactGroup.getId()); - will(returnValue(emptyMessageMetadata)); - }}); - - MailboxPropertyManagerImpl t = createInstance(); - assertNull(t.getLocalProperties(txn, contact.getId())); - } - @Test public void testGetLocalPropertiesNoMailbox() throws Exception { From 3f7aed78864ec35ef4db5133560723692f215c57 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Mon, 16 May 2022 09:59:38 +0200 Subject: [PATCH 4/4] Rename to Mailbox update --- .../bramble/api/client/ClientHelper.java | 10 +- ...opertiesUpdate.java => MailboxUpdate.java} | 4 +- ...Manager.java => MailboxUpdateManager.java} | 16 +- ...box.java => MailboxUpdateWithMailbox.java} | 4 +- .../RemoteMailboxPropertiesUpdateEvent.java | 33 ---- .../api/mailbox/RemoteMailboxUpdateEvent.java | 33 ++++ .../briarproject/bramble/test/TestUtils.java | 18 +- .../bramble/client/ClientHelperImpl.java | 26 +-- .../bramble/mailbox/MailboxApi.java | 4 +- .../bramble/mailbox/MailboxModule.java | 32 +-- .../MailboxPairingTaskFactoryImpl.java | 10 +- .../mailbox/MailboxPairingTaskImpl.java | 16 +- ...mpl.java => MailboxUpdateManagerImpl.java} | 90 ++++----- ...dator.java => MailboxUpdateValidator.java} | 13 +- .../bramble/client/ClientHelperImplTest.java | 184 ++++++++---------- .../mailbox/MailboxPairingTaskImplTest.java | 17 +- ...java => MailboxUpdateManagerImplTest.java} | 173 ++++++++-------- ...t.java => MailboxUpdateValidatorTest.java} | 40 ++-- .../TransportKeyAgreementIntegrationTest.java | 2 +- .../IntroductionIntegrationTest.java | 4 +- .../briar/test/BriarIntegrationTest.java | 6 +- 21 files changed, 348 insertions(+), 387 deletions(-) rename bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/{MailboxPropertiesUpdate.java => MailboxUpdate.java} (81%) rename bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/{MailboxPropertyManager.java => MailboxUpdateManager.java} (69%) rename bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/{MailboxPropertiesUpdateMailbox.java => MailboxUpdateWithMailbox.java} (87%) delete mode 100644 bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java create mode 100644 bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxUpdateEvent.java rename bramble-core/src/main/java/org/briarproject/bramble/mailbox/{MailboxPropertyManagerImpl.java => MailboxUpdateManagerImpl.java} (80%) rename bramble-core/src/main/java/org/briarproject/bramble/mailbox/{MailboxPropertyValidator.java => MailboxUpdateValidator.java} (80%) rename bramble-core/src/test/java/org/briarproject/bramble/mailbox/{MailboxPropertyManagerImplTest.java => MailboxUpdateManagerImplTest.java} (82%) rename bramble-core/src/test/java/org/briarproject/bramble/mailbox/{MailboxPropertyValidatorTest.java => MailboxUpdateValidatorTest.java} (73%) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java b/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java index 84893c56c..940ee2f13 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/client/ClientHelper.java @@ -9,7 +9,7 @@ import org.briarproject.bramble.api.data.BdfList; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.identity.Author; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.properties.TransportProperties; @@ -125,14 +125,14 @@ public interface ClientHelper { BdfDictionary properties) throws FormatException; /** - * Parse and validate the elements of a Mailbox property update message. + * Parse and validate the elements of a Mailbox update message. * * @return the parsed update message * @throws FormatException if the message elements are invalid */ - MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( - BdfList clientSupports, BdfList serverSupports, - BdfDictionary properties) throws FormatException; + MailboxUpdate parseAndValidateMailboxUpdate(BdfList clientSupports, + BdfList serverSupports, BdfDictionary properties) + throws FormatException; /** * Retrieves the contact ID from the group metadata of the given contact diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdate.java similarity index 81% rename from bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java rename to bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdate.java index 8319d1ec5..8ead367d3 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdate.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdate.java @@ -8,12 +8,12 @@ import javax.annotation.concurrent.Immutable; @Immutable @NotNullByDefault -public class MailboxPropertiesUpdate { +public class MailboxUpdate { boolean hasMailbox; private final List clientSupports; - public MailboxPropertiesUpdate(List clientSupports) { + public MailboxUpdate(List clientSupports) { this.hasMailbox = false; this.clientSupports = clientSupports; } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateManager.java similarity index 69% rename from bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java rename to bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateManager.java index e7e1d8d42..92c0ebc78 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertyManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateManager.java @@ -9,31 +9,31 @@ import org.briarproject.bramble.api.sync.ClientId; import javax.annotation.Nullable; @NotNullByDefault -public interface MailboxPropertyManager { +public interface MailboxUpdateManager { /** - * The unique ID of the mailbox property client. + * The unique ID of the mailbox update (properties) client. */ ClientId CLIENT_ID = new ClientId("org.briarproject.bramble.mailbox.properties"); /** - * The current major version of the mailbox property client. + * The current major version of the mailbox update (properties) client. */ int MAJOR_VERSION = 1; /** - * The current minor version of the mailbox property client. + * The current minor version of the mailbox update (properties) client. */ int MINOR_VERSION = 0; /** - * The number of properties required for a (non-empty) update message. + * The number of properties required for an update message with a mailbox. */ int PROP_COUNT = 4; /** - * The required properties of a non-empty update message. + * The required properties of an update message with a mailbox. */ String PROP_KEY_ONION = "onion"; String PROP_KEY_AUTHTOKEN = "authToken"; @@ -57,10 +57,10 @@ public interface MailboxPropertyManager { */ String MSG_KEY_LOCAL = "local"; - MailboxPropertiesUpdate getLocalProperties(Transaction txn, ContactId c) + MailboxUpdate getLocalUpdate(Transaction txn, ContactId c) throws DbException; @Nullable - MailboxPropertiesUpdate getRemoteProperties(Transaction txn, ContactId c) + MailboxUpdate getRemoteUpdate(Transaction txn, ContactId c) throws DbException; } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateWithMailbox.java similarity index 87% rename from bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java rename to bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateWithMailbox.java index 2992c49ab..e84506dbd 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxPropertiesUpdateMailbox.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/MailboxUpdateWithMailbox.java @@ -8,14 +8,14 @@ import javax.annotation.concurrent.Immutable; @Immutable @NotNullByDefault -public class MailboxPropertiesUpdateMailbox extends MailboxPropertiesUpdate { +public class MailboxUpdateWithMailbox extends MailboxUpdate { private final List serverSupports; private final String onion; private final MailboxAuthToken authToken; private final MailboxFolderId inboxId; private final MailboxFolderId outboxId; - public MailboxPropertiesUpdateMailbox(List clientSupports, + public MailboxUpdateWithMailbox(List clientSupports, List serverSupports, String onion, MailboxAuthToken authToken, MailboxFolderId inboxId, MailboxFolderId outboxId diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java deleted file mode 100644 index 649c5c41e..000000000 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxPropertiesUpdateEvent.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.briarproject.bramble.api.mailbox; - -import org.briarproject.bramble.api.contact.ContactId; -import org.briarproject.bramble.api.event.Event; -import org.briarproject.bramble.api.nullsafety.NotNullByDefault; - -import javax.annotation.concurrent.Immutable; - -/** - * An event that is broadcast when {@link MailboxPropertiesUpdate} are received - * from a contact. - */ -@Immutable -@NotNullByDefault -public class RemoteMailboxPropertiesUpdateEvent extends Event { - - private final ContactId contactId; - private final MailboxPropertiesUpdate mailboxPropertiesUpdate; - - public RemoteMailboxPropertiesUpdateEvent(ContactId contactId, - MailboxPropertiesUpdate mailboxPropertiesUpdate) { - this.contactId = contactId; - this.mailboxPropertiesUpdate = mailboxPropertiesUpdate; - } - - public ContactId getContact() { - return contactId; - } - - public MailboxPropertiesUpdate getMailboxPropertiesUpdate() { - return mailboxPropertiesUpdate; - } -} diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxUpdateEvent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxUpdateEvent.java new file mode 100644 index 000000000..f213eb27b --- /dev/null +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/mailbox/RemoteMailboxUpdateEvent.java @@ -0,0 +1,33 @@ +package org.briarproject.bramble.api.mailbox; + +import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.event.Event; +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; + +import javax.annotation.concurrent.Immutable; + +/** + * An event that is broadcast when {@link MailboxUpdate} are received + * from a contact. + */ +@Immutable +@NotNullByDefault +public class RemoteMailboxUpdateEvent extends Event { + + private final ContactId contactId; + private final MailboxUpdate mailboxUpdate; + + public RemoteMailboxUpdateEvent(ContactId contactId, + MailboxUpdate mailboxUpdate) { + this.contactId = contactId; + this.mailboxUpdate = mailboxUpdate; + } + + public ContactId getContact() { + return contactId; + } + + public MailboxUpdate getMailboxUpdate() { + return mailboxUpdate; + } +} diff --git a/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java b/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java index fa01c7beb..0b9c646a4 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java @@ -21,8 +21,8 @@ import org.briarproject.bramble.api.identity.AuthorId; import org.briarproject.bramble.api.identity.Identity; import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.sync.ClientId; @@ -281,19 +281,16 @@ public class TestUtils { asList(optionalTests.split(",")).contains(testClass.getName()); } - public static boolean mailboxPropertiesUpdateEqual( - @Nullable MailboxPropertiesUpdate a, - @Nullable MailboxPropertiesUpdate b) { + public static boolean mailboxUpdateEqual(@Nullable MailboxUpdate a, + @Nullable MailboxUpdate b) { if (a == null || b == null) { return a == b; } if (!a.hasMailbox() && !b.hasMailbox()) { return a.getClientSupports().equals(b.getClientSupports()); } else if (a.hasMailbox() && b.hasMailbox()) { - MailboxPropertiesUpdateMailbox am = - (MailboxPropertiesUpdateMailbox) a; - MailboxPropertiesUpdateMailbox bm = - (MailboxPropertiesUpdateMailbox) b; + MailboxUpdateWithMailbox am = (MailboxUpdateWithMailbox) a; + MailboxUpdateWithMailbox bm = (MailboxUpdateWithMailbox) b; return am.getClientSupports().equals(bm.getClientSupports()) && am.getServerSupports().equals(bm.getServerSupports()) && am.getOnion().equals(bm.getOnion()) && @@ -304,8 +301,7 @@ public class TestUtils { return false; } - public static boolean mailboxPropertiesEqual( - @Nullable MailboxProperties a, + public static boolean mailboxPropertiesEqual(@Nullable MailboxProperties a, @Nullable MailboxProperties b) { if (a == null || b == null) { return a == b; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java index 39d9079d1..7baf56f56 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java @@ -25,8 +25,8 @@ import org.briarproject.bramble.api.identity.Author; import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.TransportId; @@ -55,12 +55,12 @@ import static org.briarproject.bramble.api.client.ContactGroupConstants.GROUP_KE import static org.briarproject.bramble.api.identity.Author.FORMAT_VERSION; 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.api.mailbox.MailboxPropertyManager.PROP_COUNT; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_AUTHTOKEN; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_INBOXID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_ONION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_OUTBOXID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_ONION_LENGTH; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_COUNT; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_AUTHTOKEN; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_INBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_ONION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_OUTBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_ONION_LENGTH; import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT; import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH; import static org.briarproject.bramble.util.ValidationUtils.checkLength; @@ -415,9 +415,9 @@ class ClientHelperImpl implements ClientHelper { } @Override - public MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( - BdfList clientSupports, BdfList serverSupports, - BdfDictionary properties) throws FormatException { + public MailboxUpdate parseAndValidateMailboxUpdate(BdfList clientSupports, + BdfList serverSupports, BdfDictionary properties) + throws FormatException { List clientSupportsList = getMailboxVersionList(clientSupports); List serverSupportsList = @@ -432,7 +432,7 @@ class ClientHelperImpl implements ClientHelper { if (!serverSupports.isEmpty()) { throw new FormatException(); } - return new MailboxPropertiesUpdate(clientSupportsList); + return new MailboxUpdate(clientSupportsList); } // Mailbox must be accompanied by the Mailbox API version(s) it supports if (serverSupports.isEmpty()) { @@ -455,7 +455,7 @@ class ClientHelperImpl implements ClientHelper { checkLength(inboxId, UniqueId.LENGTH); byte[] outboxId = properties.getRaw(PROP_KEY_OUTBOXID); checkLength(outboxId, UniqueId.LENGTH); - return new MailboxPropertiesUpdateMailbox(clientSupportsList, + return new MailboxUpdateWithMailbox(clientSupportsList, serverSupportsList, onion, new MailboxAuthToken(authToken), new MailboxFolderId(inboxId), new MailboxFolderId(outboxId)); } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java index 22d00a18f..37b6ca042 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java @@ -7,7 +7,7 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFileId; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; @@ -26,7 +26,7 @@ interface MailboxApi { /** * Mailbox API versions that we support as a client. This is reported to our - * contacts by {@link MailboxPropertyManager}. + * contacts by {@link MailboxUpdateManager}. */ List CLIENT_SUPPORTS = singletonList( new MailboxVersion(1, 0)); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxModule.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxModule.java index a89065011..e1ad79555 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxModule.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxModule.java @@ -5,8 +5,8 @@ import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.data.MetadataEncoder; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.mailbox.MailboxManager; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; import org.briarproject.bramble.api.sync.validation.ValidationManager; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.versioning.ClientVersioningManager; @@ -17,18 +17,18 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.CLIENT_ID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MAJOR_VERSION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MINOR_VERSION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.CLIENT_ID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MAJOR_VERSION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MINOR_VERSION; @Module public class MailboxModule { public static class EagerSingletons { @Inject - MailboxPropertyValidator mailboxPropertyValidator; + MailboxUpdateValidator mailboxUpdateValidator; @Inject - MailboxPropertyManager mailboxPropertyManager; + MailboxUpdateManager mailboxUpdateManager; } @Provides @@ -56,10 +56,10 @@ public class MailboxModule { @Provides @Singleton - MailboxPropertyValidator provideMailboxPropertyValidator( + MailboxUpdateValidator provideMailboxUpdateValidator( ValidationManager validationManager, ClientHelper clientHelper, MetadataEncoder metadataEncoder, Clock clock) { - MailboxPropertyValidator validator = new MailboxPropertyValidator( + MailboxUpdateValidator validator = new MailboxUpdateValidator( clientHelper, metadataEncoder, clock); validationManager.registerMessageValidator(CLIENT_ID, MAJOR_VERSION, validator); @@ -68,19 +68,19 @@ public class MailboxModule { @Provides @Singleton - MailboxPropertyManager provideMailboxPropertyManager( + MailboxUpdateManager provideMailboxUpdateManager( LifecycleManager lifecycleManager, ValidationManager validationManager, ContactManager contactManager, ClientVersioningManager clientVersioningManager, MailboxSettingsManager mailboxSettingsManager, - MailboxPropertyManagerImpl mailboxPropertyManager) { - lifecycleManager.registerOpenDatabaseHook(mailboxPropertyManager); + MailboxUpdateManagerImpl mailboxUpdateManager) { + lifecycleManager.registerOpenDatabaseHook(mailboxUpdateManager); validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION, - mailboxPropertyManager); - contactManager.registerContactHook(mailboxPropertyManager); + mailboxUpdateManager); + contactManager.registerContactHook(mailboxUpdateManager); clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION, - MINOR_VERSION, mailboxPropertyManager); - mailboxSettingsManager.registerMailboxHook(mailboxPropertyManager); - return mailboxPropertyManager; + MINOR_VERSION, mailboxUpdateManager); + mailboxSettingsManager.registerMailboxHook(mailboxUpdateManager); + return mailboxUpdateManager; } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskFactoryImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskFactoryImpl.java index 1bab17766..c688fc511 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskFactoryImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskFactoryImpl.java @@ -4,8 +4,8 @@ import org.briarproject.bramble.api.crypto.CryptoComponent; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.event.EventExecutor; import org.briarproject.bramble.api.mailbox.MailboxPairingTask; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.system.Clock; @@ -24,7 +24,7 @@ class MailboxPairingTaskFactoryImpl implements MailboxPairingTaskFactory { private final Clock clock; private final MailboxApi api; private final MailboxSettingsManager mailboxSettingsManager; - private final MailboxPropertyManager mailboxPropertyManager; + private final MailboxUpdateManager mailboxUpdateManager; @Inject MailboxPairingTaskFactoryImpl( @@ -34,20 +34,20 @@ class MailboxPairingTaskFactoryImpl implements MailboxPairingTaskFactory { Clock clock, MailboxApi api, MailboxSettingsManager mailboxSettingsManager, - MailboxPropertyManager mailboxPropertyManager) { + MailboxUpdateManager mailboxUpdateManager) { this.eventExecutor = eventExecutor; this.db = db; this.crypto = crypto; this.clock = clock; this.api = api; this.mailboxSettingsManager = mailboxSettingsManager; - this.mailboxPropertyManager = mailboxPropertyManager; + this.mailboxUpdateManager = mailboxUpdateManager; } @Override public MailboxPairingTask createPairingTask(String qrCodePayload) { return new MailboxPairingTaskImpl(qrCodePayload, eventExecutor, db, crypto, clock, api, mailboxSettingsManager, - mailboxPropertyManager); + mailboxUpdateManager); } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java index 8236e07e2..4d7eea749 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImpl.java @@ -11,9 +11,9 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxPairingState; import org.briarproject.bramble.api.mailbox.MailboxPairingTask; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.mailbox.MailboxApi.ApiException; @@ -51,7 +51,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask { private final Clock clock; private final MailboxApi api; private final MailboxSettingsManager mailboxSettingsManager; - private final MailboxPropertyManager mailboxPropertyManager; + private final MailboxUpdateManager mailboxUpdateManager; private final Object lock = new Object(); @GuardedBy("lock") @@ -68,7 +68,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask { Clock clock, MailboxApi api, MailboxSettingsManager mailboxSettingsManager, - MailboxPropertyManager mailboxPropertyManager) { + MailboxUpdateManager mailboxUpdateManager) { this.payload = payload; this.eventExecutor = eventExecutor; this.db = db; @@ -76,7 +76,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask { this.clock = clock; this.api = api; this.mailboxSettingsManager = mailboxSettingsManager; - this.mailboxPropertyManager = mailboxPropertyManager; + this.mailboxUpdateManager = mailboxUpdateManager; state = new MailboxPairingState.QrCodeReceived(); } @@ -125,9 +125,9 @@ class MailboxPairingTaskImpl implements MailboxPairingTask { // timers for contacts who doesn't have their own mailbox. This way, // data stranded on our old mailbox will be re-uploaded to our new. for (Contact c : db.getContacts(txn)) { - MailboxPropertiesUpdate remoteProps = mailboxPropertyManager - .getRemoteProperties(txn, c.getId()); - if (remoteProps == null || !remoteProps.hasMailbox()) { + MailboxUpdate update = mailboxUpdateManager.getRemoteUpdate( + txn, c.getId()); + if (update == null || !update.hasMailbox()) { db.resetUnackedMessagesToSend(txn, c.getId()); } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImpl.java similarity index 80% rename from bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java rename to bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImpl.java index c6eb94ab4..7cbaf2fb9 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImpl.java @@ -18,13 +18,13 @@ import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager.MailboxHook; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.mailbox.MailboxVersion; -import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent; +import org.briarproject.bramble.api.mailbox.RemoteMailboxUpdateEvent; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Group.Visibility; @@ -48,7 +48,7 @@ import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.D import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS; @NotNullByDefault -class MailboxPropertyManagerImpl implements MailboxPropertyManager, +class MailboxUpdateManagerImpl implements MailboxUpdateManager, OpenDatabaseHook, ContactHook, ClientVersioningHook, IncomingMessageHook, MailboxHook { @@ -63,7 +63,7 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, private final Group localGroup; @Inject - MailboxPropertyManagerImpl(DatabaseComponent db, ClientHelper clientHelper, + MailboxUpdateManagerImpl(DatabaseComponent db, ClientHelper clientHelper, ClientVersioningManager clientVersioningManager, MetadataParser metadataParser, ContactGroupFactory contactGroupFactory, Clock clock, @@ -108,11 +108,11 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, mailboxSettingsManager.getOwnMailboxProperties(txn); if (ownProps != null) { // We are paired, create and send props to the newly added contact - createAndSendProperties(txn, c, ownProps.getServerSupports(), + createAndSendUpdateWithMailbox(txn, c, ownProps.getServerSupports(), ownProps.getOnion()); } else { // Not paired, but we still want to get our clientSupports sent - sendEmptyProperties(txn, c); + sendUpdateNoMailbox(txn, c); } } @@ -126,14 +126,14 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, List serverSupports) throws DbException { for (Contact c : db.getContacts(txn)) { - createAndSendProperties(txn, c, serverSupports, ownOnion); + createAndSendUpdateWithMailbox(txn, c, serverSupports, ownOnion); } } @Override public void mailboxUnpaired(Transaction txn) throws DbException { for (Contact c : db.getContacts(txn)) { - sendEmptyProperties(txn, c); + sendUpdateNoMailbox(txn, c); } } @@ -165,8 +165,8 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } ContactId c = clientHelper.getContactId(txn, m.getGroupId()); BdfList body = clientHelper.getMessageAsList(txn, m.getId()); - MailboxPropertiesUpdate p = parseProperties(body); - txn.attach(new RemoteMailboxPropertiesUpdateEvent(c, p)); + MailboxUpdate u = parseUpdate(body); + txn.attach(new RemoteMailboxUpdateEvent(c, u)); // Reset message retransmission timers for the contact. Avoiding // messages getting stranded: // - on our mailbox, if they now have a mailbox but didn't before @@ -180,10 +180,9 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, } @Override - public MailboxPropertiesUpdate getLocalProperties(Transaction txn, - ContactId c) throws DbException { - MailboxPropertiesUpdate local = - getProperties(txn, db.getContact(txn, c), true); + public MailboxUpdate getLocalUpdate(Transaction txn, ContactId c) + throws DbException { + MailboxUpdate local = getUpdate(txn, db.getContact(txn, c), true); // An update (with or without mailbox) is created when contact is added if (local == null) { throw new DbException(); @@ -193,9 +192,9 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, @Override @Nullable - public MailboxPropertiesUpdate getRemoteProperties(Transaction txn, - ContactId c) throws DbException { - return getProperties(txn, db.getContact(txn, c), false); + public MailboxUpdate getRemoteUpdate(Transaction txn, ContactId c) throws + DbException { + return getUpdate(txn, db.getContact(txn, c), false); } /** @@ -204,16 +203,16 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, * supported Mailbox API version(s). All of which the contact needs to * communicate with our Mailbox. */ - private void createAndSendProperties(Transaction txn, Contact c, + private void createAndSendUpdateWithMailbox(Transaction txn, Contact c, List serverSupports, String ownOnion) throws DbException { - MailboxPropertiesUpdate p = new MailboxPropertiesUpdateMailbox( + MailboxUpdate u = new MailboxUpdateWithMailbox( CLIENT_SUPPORTS, serverSupports, ownOnion, new MailboxAuthToken(crypto.generateUniqueId().getBytes()), new MailboxFolderId(crypto.generateUniqueId().getBytes()), new MailboxFolderId(crypto.generateUniqueId().getBytes())); Group g = getContactGroup(c); - storeMessageReplaceLatest(txn, g.getId(), p); + storeMessageReplaceLatest(txn, g.getId(), u); } /** @@ -222,39 +221,38 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, * Mailbox that they can use. It still includes the list of Mailbox API * version(s) that we support as a client. */ - private void sendEmptyProperties(Transaction txn, Contact c) + private void sendUpdateNoMailbox(Transaction txn, Contact c) throws DbException { Group g = getContactGroup(c); - MailboxPropertiesUpdate p = - new MailboxPropertiesUpdate(CLIENT_SUPPORTS); - storeMessageReplaceLatest(txn, g.getId(), p); + MailboxUpdate u = new MailboxUpdate(CLIENT_SUPPORTS); + storeMessageReplaceLatest(txn, g.getId(), u); } @Nullable - private MailboxPropertiesUpdate getProperties(Transaction txn, - Contact c, boolean local) throws DbException { - MailboxPropertiesUpdate p = null; + private MailboxUpdate getUpdate(Transaction txn, Contact c, boolean local) + throws DbException { + MailboxUpdate u = null; Group g = getContactGroup(c); try { LatestUpdate latest = findLatest(txn, g.getId(), local); if (latest != null) { BdfList body = clientHelper.getMessageAsList(txn, latest.messageId); - p = parseProperties(body); + u = parseUpdate(body); } } catch (FormatException e) { throw new DbException(e); } - return p; + return u; } private void storeMessageReplaceLatest(Transaction txn, GroupId g, - MailboxPropertiesUpdate p) throws DbException { + MailboxUpdate u) throws DbException { try { LatestUpdate latest = findLatest(txn, g, true); long version = latest == null ? 1 : latest.version + 1; Message m = clientHelper.createMessage(g, clock.currentTimeMillis(), - encodeProperties(version, p)); + encodeProperties(version, u)); BdfDictionary meta = new BdfDictionary(); meta.put(MSG_KEY_VERSION, version); meta.put(MSG_KEY_LOCAL, true); @@ -286,19 +284,18 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, return null; } - private BdfList encodeProperties(long version, MailboxPropertiesUpdate p) { + private BdfList encodeProperties(long version, MailboxUpdate u) { BdfDictionary dict = new BdfDictionary(); BdfList serverSupports = new BdfList(); - if (p.hasMailbox()) { - MailboxPropertiesUpdateMailbox pm = - (MailboxPropertiesUpdateMailbox) p; - serverSupports = encodeSupportsList(pm.getServerSupports()); - dict.put(PROP_KEY_ONION, pm.getOnion()); - dict.put(PROP_KEY_AUTHTOKEN, pm.getAuthToken().getBytes()); - dict.put(PROP_KEY_INBOXID, pm.getInboxId().getBytes()); - dict.put(PROP_KEY_OUTBOXID, pm.getOutboxId().getBytes()); + if (u.hasMailbox()) { + MailboxUpdateWithMailbox um = (MailboxUpdateWithMailbox) u; + serverSupports = encodeSupportsList(um.getServerSupports()); + dict.put(PROP_KEY_ONION, um.getOnion()); + dict.put(PROP_KEY_AUTHTOKEN, um.getAuthToken().getBytes()); + dict.put(PROP_KEY_INBOXID, um.getInboxId().getBytes()); + dict.put(PROP_KEY_OUTBOXID, um.getOutboxId().getBytes()); } - return BdfList.of(version, encodeSupportsList(p.getClientSupports()), + return BdfList.of(version, encodeSupportsList(u.getClientSupports()), serverSupports, dict); } @@ -310,14 +307,13 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager, return supports; } - private MailboxPropertiesUpdate parseProperties(BdfList body) + private MailboxUpdate parseUpdate(BdfList body) throws FormatException { BdfList clientSupports = body.getList(1); BdfList serverSupports = body.getList(2); BdfDictionary dict = body.getDictionary(3); - return clientHelper.parseAndValidateMailboxPropertiesUpdate( - clientSupports, serverSupports, dict - ); + return clientHelper.parseAndValidateMailboxUpdate(clientSupports, + serverSupports, dict); } private Group getContactGroup(Contact c) { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateValidator.java similarity index 80% rename from bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java rename to bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateValidator.java index 8d279c8bd..e717f911f 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxPropertyValidator.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxUpdateValidator.java @@ -15,15 +15,15 @@ import org.briarproject.bramble.api.system.Clock; import javax.annotation.concurrent.Immutable; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MSG_KEY_LOCAL; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MSG_KEY_VERSION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MSG_KEY_LOCAL; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MSG_KEY_VERSION; import static org.briarproject.bramble.util.ValidationUtils.checkSize; @Immutable @NotNullByDefault -class MailboxPropertyValidator extends BdfMessageValidator { +class MailboxUpdateValidator extends BdfMessageValidator { - MailboxPropertyValidator(ClientHelper clientHelper, + MailboxUpdateValidator(ClientHelper clientHelper, MetadataEncoder metadataEncoder, Clock clock) { super(clientHelper, metadataEncoder, clock); } @@ -42,9 +42,8 @@ class MailboxPropertyValidator extends BdfMessageValidator { BdfList serverSupports = body.getList(2); // Properties BdfDictionary dictionary = body.getDictionary(3); - clientHelper.parseAndValidateMailboxPropertiesUpdate(clientSupports, - serverSupports, dictionary - ); + clientHelper.parseAndValidateMailboxUpdate(clientSupports, + serverSupports, dictionary); // Return the metadata BdfDictionary meta = new BdfDictionary(); meta.put(MSG_KEY_VERSION, version); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java index b5dd6d06c..9e316b07d 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/client/ClientHelperImplTest.java @@ -23,8 +23,8 @@ import org.briarproject.bramble.api.identity.Author; import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.Message; @@ -47,17 +47,17 @@ import static java.util.Collections.singletonList; 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.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_AUTHTOKEN; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_INBOXID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_ONION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_OUTBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_AUTHTOKEN; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_INBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_ONION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_OUTBOXID; import static org.briarproject.bramble.test.TestUtils.getAuthor; import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomId; import static org.briarproject.bramble.test.TestUtils.getSignaturePrivateKey; import static org.briarproject.bramble.test.TestUtils.getSignaturePublicKey; -import static org.briarproject.bramble.test.TestUtils.mailboxPropertiesUpdateEqual; +import static org.briarproject.bramble.test.TestUtils.mailboxUpdateEqual; import static org.briarproject.bramble.util.StringUtils.getRandomString; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -98,7 +98,7 @@ public class ClientHelperImplTest extends BrambleMockTestCase { messageFactory, bdfReaderFactory, bdfWriterFactory, metadataParser, metadataEncoder, cryptoComponent, authorFactory); - private final MailboxPropertiesUpdateMailbox validMailboxPropsUpdate; + private final MailboxUpdateWithMailbox validMailboxUpdateWithMailbox; private final BdfList emptyClientSupports; private final BdfList someClientSupports; private final BdfList emptyServerSupports; @@ -109,7 +109,7 @@ public class ClientHelperImplTest extends BrambleMockTestCase { someClientSupports = BdfList.of(BdfList.of(1, 0)); emptyServerSupports = new BdfList(); someServerSupports = BdfList.of(BdfList.of(1, 0)); - validMailboxPropsUpdate = new MailboxPropertiesUpdateMailbox( + validMailboxUpdateWithMailbox = new MailboxUpdateWithMailbox( singletonList(new MailboxVersion(1, 0)), singletonList(new MailboxVersion(1, 0)), "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd", @@ -118,14 +118,14 @@ public class ClientHelperImplTest extends BrambleMockTestCase { new MailboxFolderId(getRandomId())); } - private BdfDictionary getValidMailboxPropsUpdateDict() { + private BdfDictionary getValidMailboxUpdateWithMailboxDict() { BdfDictionary dict = new BdfDictionary(); - dict.put(PROP_KEY_ONION, validMailboxPropsUpdate.getOnion()); - dict.put(PROP_KEY_AUTHTOKEN, validMailboxPropsUpdate.getAuthToken() + dict.put(PROP_KEY_ONION, validMailboxUpdateWithMailbox.getOnion()); + dict.put(PROP_KEY_AUTHTOKEN, validMailboxUpdateWithMailbox + .getAuthToken().getBytes()); + dict.put(PROP_KEY_INBOXID, validMailboxUpdateWithMailbox.getInboxId() .getBytes()); - dict.put(PROP_KEY_INBOXID, validMailboxPropsUpdate.getInboxId() - .getBytes()); - dict.put(PROP_KEY_OUTBOXID, validMailboxPropsUpdate.getOutboxId() + dict.put(PROP_KEY_OUTBOXID, validMailboxUpdateWithMailbox.getOutboxId() .getBytes()); return dict; } @@ -560,172 +560,150 @@ public class ClientHelperImplTest extends BrambleMockTestCase { } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsWithEmptyClientSupports() + public void testRejectsMailboxUpdateWithEmptyClientSupports() throws Exception { BdfDictionary emptyPropsDict = new BdfDictionary(); - clientHelper.parseAndValidateMailboxPropertiesUpdate( - emptyClientSupports, emptyServerSupports, emptyPropsDict + clientHelper.parseAndValidateMailboxUpdate(emptyClientSupports, + emptyServerSupports, emptyPropsDict ); } @Test - public void testParseEmptyMailboxPropsUpdate() throws Exception { + public void testParseMailboxUpdateNoMailbox() throws Exception { BdfDictionary emptyPropsDict = new BdfDictionary(); - MailboxPropertiesUpdate parsedProps = clientHelper - .parseAndValidateMailboxPropertiesUpdate(someClientSupports, - emptyServerSupports, emptyPropsDict - ); - assertFalse(parsedProps.hasMailbox()); + MailboxUpdate parsedUpdate = clientHelper.parseAndValidateMailboxUpdate( + someClientSupports, emptyServerSupports, emptyPropsDict); + assertFalse(parsedUpdate.hasMailbox()); } @Test(expected = FormatException.class) - public void testRejectsEmptyMailboxPropsWithSomeServerSupports() + public void testRejectsMailboxUpdateNoMailboxWithSomeServerSupports() throws Exception { BdfDictionary emptyPropsDict = new BdfDictionary(); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, emptyPropsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, emptyPropsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsShortSupports() throws Exception { - clientHelper.parseAndValidateMailboxPropertiesUpdate( - BdfList.of(BdfList.of(1)), emptyServerSupports, - new BdfDictionary() - ); + public void testRejectsMailboxUpdateShortSupports() throws Exception { + clientHelper.parseAndValidateMailboxUpdate(BdfList.of(BdfList.of(1)), + emptyServerSupports, new BdfDictionary()); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsLongSupports() throws Exception { - clientHelper.parseAndValidateMailboxPropertiesUpdate( + public void testRejectsMailboxUpdateLongSupports() throws Exception { + clientHelper.parseAndValidateMailboxUpdate( BdfList.of(BdfList.of(1, 0, 0)), emptyServerSupports, - new BdfDictionary() - ); + new BdfDictionary()); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsNonIntSupports() throws Exception { - clientHelper.parseAndValidateMailboxPropertiesUpdate( + public void testRejectsMailboxUpdateNonIntSupports() throws Exception { + clientHelper.parseAndValidateMailboxUpdate( BdfList.of(BdfList.of(1, "0")), emptyServerSupports, new BdfDictionary() ); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsNonListSupports() throws Exception { - clientHelper.parseAndValidateMailboxPropertiesUpdate( - BdfList.of("non-list"), emptyServerSupports, new BdfDictionary() - ); + public void testRejectsMailboxUpdateNonListSupports() throws Exception { + clientHelper.parseAndValidateMailboxUpdate( + BdfList.of("non-list"), emptyServerSupports, + new BdfDictionary()); } @Test - public void testParseValidMailboxPropsUpdate() throws Exception { - MailboxPropertiesUpdate parsedProps = clientHelper - .parseAndValidateMailboxPropertiesUpdate( - someClientSupports, someServerSupports, - getValidMailboxPropsUpdateDict() - ); - assertTrue(mailboxPropertiesUpdateEqual(validMailboxPropsUpdate, - parsedProps)); + public void testParseValidMailboxUpdateWithMailbox() throws Exception { + MailboxUpdate parsedUpdate = clientHelper.parseAndValidateMailboxUpdate( + someClientSupports, someServerSupports, + getValidMailboxUpdateWithMailboxDict()); + assertTrue( + mailboxUpdateEqual(validMailboxUpdateWithMailbox, + parsedUpdate)); } @Test(expected = FormatException.class) - public void rejectsMailboxPropsWithEmptyServerSupports() throws Exception { - clientHelper.parseAndValidateMailboxPropertiesUpdate( - someClientSupports, emptyServerSupports, - getValidMailboxPropsUpdateDict() - ); + public void rejectsMailboxUpdateWithEmptyServerSupports() throws Exception { + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + emptyServerSupports, getValidMailboxUpdateWithMailboxDict()); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateOnionNotDecodable() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateOnionNotDecodable() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); String badOnion = "!" + propsDict.getString(PROP_KEY_ONION) .substring(1); propsDict.put(PROP_KEY_ONION, badOnion); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - emptyServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + emptyServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateOnionWrongLength() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateOnionWrongLength() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); String tooLongOnion = propsDict.getString(PROP_KEY_ONION) + "!"; propsDict.put(PROP_KEY_ONION, tooLongOnion); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, emptyServerSupports, propsDict ); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateInboxIdWrongLength() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateInboxIdWrongLength() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.put(PROP_KEY_INBOXID, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateOutboxIdWrongLength() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateOutboxIdWrongLength() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.put(PROP_KEY_OUTBOXID, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateAuthTokenWrongLength() + public void testRejectsMailboxUpdateAuthTokenWrongLength() throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.put(PROP_KEY_AUTHTOKEN, getRandomBytes(UniqueId.LENGTH + 1)); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateMissingOnion() throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateMissingOnion() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.remove(PROP_KEY_ONION); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, someServerSupports, propsDict ); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateMissingAuthToken() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateMissingAuthToken() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.remove(PROP_KEY_AUTHTOKEN); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateMissingInboxId() throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateMissingInboxId() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.remove(PROP_KEY_INBOXID); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } @Test(expected = FormatException.class) - public void testRejectsMailboxPropsUpdateMissingOutboxId() - throws Exception { - BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); + public void testRejectsMailboxUpdateMissingOutboxId() throws Exception { + BdfDictionary propsDict = getValidMailboxUpdateWithMailboxDict(); propsDict.remove(PROP_KEY_OUTBOXID); - clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports, - someServerSupports, propsDict - ); + clientHelper.parseAndValidateMailboxUpdate(someClientSupports, + someServerSupports, propsDict); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java index bd60e24be..be7555d90 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPairingTaskImplTest.java @@ -9,9 +9,9 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxPairingState; import org.briarproject.bramble.api.mailbox.MailboxPairingTask; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.test.BrambleMockTestCase; @@ -49,11 +49,11 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase { private final MailboxApi api = context.mock(MailboxApi.class); private final MailboxSettingsManager mailboxSettingsManager = context.mock(MailboxSettingsManager.class); - private final MailboxPropertyManager mailboxPropertyManager = - context.mock(MailboxPropertyManager.class); + private final MailboxUpdateManager mailboxUpdateManager = + context.mock(MailboxUpdateManager.class); private final MailboxPairingTaskFactory factory = new MailboxPairingTaskFactoryImpl(executor, db, crypto, clock, api, - mailboxSettingsManager, mailboxPropertyManager); + mailboxSettingsManager, mailboxUpdateManager); private final String onion = getRandomString(56); private final byte[] onionBytes = getRandomBytes(32); @@ -107,8 +107,7 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase { }}); Contact contact1 = getContact(); Transaction txn = new Transaction(null, false); - MailboxPropertiesUpdate emptyProps = new MailboxPropertiesUpdate( - CLIENT_SUPPORTS); + MailboxUpdate updateNoMailbox = new MailboxUpdate(CLIENT_SUPPORTS); context.checking(new DbExpectations() {{ oneOf(db).transaction(with(false), withDbRunnable(txn)); oneOf(mailboxSettingsManager).setOwnMailboxProperties( @@ -116,9 +115,9 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase { oneOf(mailboxSettingsManager).recordSuccessfulConnection(txn, time); oneOf(db).getContacts(txn); will(returnValue(singletonList(contact1))); - oneOf(mailboxPropertyManager).getRemoteProperties(txn, + oneOf(mailboxUpdateManager).getRemoteUpdate(txn, contact1.getId()); - will(returnValue(emptyProps)); + will(returnValue(updateNoMailbox)); oneOf(db).resetUnackedMessagesToSend(txn, contact1.getId()); }}); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImplTest.java similarity index 82% rename from bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java rename to bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImplTest.java index 63cfc2d1a..0301f4dd0 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateManagerImplTest.java @@ -14,11 +14,11 @@ import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxProperties; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.mailbox.MailboxVersion; -import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent; +import org.briarproject.bramble.api.mailbox.RemoteMailboxUpdateEvent; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.Message; @@ -34,14 +34,14 @@ import java.util.List; import java.util.Map; import static java.util.Collections.singletonList; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.CLIENT_ID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MAJOR_VERSION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MSG_KEY_LOCAL; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.MSG_KEY_VERSION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_AUTHTOKEN; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_INBOXID; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_ONION; -import static org.briarproject.bramble.api.mailbox.MailboxPropertyManager.PROP_KEY_OUTBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.CLIENT_ID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MAJOR_VERSION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MSG_KEY_LOCAL; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.MSG_KEY_VERSION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_AUTHTOKEN; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_INBOXID; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_ONION; +import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_OUTBOXID; import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED; import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE; import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS; @@ -50,13 +50,13 @@ import static org.briarproject.bramble.test.TestUtils.getGroup; import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getRandomId; import static org.briarproject.bramble.test.TestUtils.hasEvent; -import static org.briarproject.bramble.test.TestUtils.mailboxPropertiesUpdateEqual; +import static org.briarproject.bramble.test.TestUtils.mailboxUpdateEqual; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { +public class MailboxUpdateManagerImplTest extends BrambleMockTestCase { private final DatabaseComponent db = context.mock(DatabaseComponent.class); private final ClientHelper clientHelper = context.mock(ClientHelper.class); @@ -80,11 +80,11 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { private final BdfList emptyServerSupports; private final BdfList someServerSupports; private final List someServerSupportsList; - private final MailboxPropertiesUpdateMailbox updateMailbox; - private final MailboxPropertiesUpdate updateNoMailbox; + private final MailboxUpdateWithMailbox updateWithMailbox; + private final MailboxUpdate updateNoMailbox; private final MailboxProperties ownProps; - public MailboxPropertyManagerImplTest() { + public MailboxUpdateManagerImplTest() { someClientSupports = BdfList.of(BdfList.of(1, 0)); someClientSupportsList = singletonList(new MailboxVersion(1, 0)); emptyServerSupports = new BdfList(); @@ -97,7 +97,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { ownProps = new MailboxProperties("http://bar.onion", new MailboxAuthToken(getRandomId()), true, someServerSupportsList); - updateMailbox = new MailboxPropertiesUpdateMailbox( + updateWithMailbox = new MailboxUpdateWithMailbox( singletonList(new MailboxVersion(1, 0)), singletonList(new MailboxVersion(1, 0)), ownProps.getOnion(), @@ -105,22 +105,23 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId())); propsDict = new BdfDictionary(); - propsDict.put(PROP_KEY_ONION, updateMailbox.getOnion()); - propsDict.put(PROP_KEY_AUTHTOKEN, - updateMailbox.getAuthToken().getBytes()); - propsDict.put(PROP_KEY_INBOXID, updateMailbox.getInboxId().getBytes()); - propsDict.put(PROP_KEY_OUTBOXID, - updateMailbox.getOutboxId().getBytes()); - updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList); + propsDict.put(PROP_KEY_ONION, updateWithMailbox.getOnion()); + propsDict.put(PROP_KEY_AUTHTOKEN, updateWithMailbox.getAuthToken() + .getBytes()); + propsDict.put(PROP_KEY_INBOXID, updateWithMailbox.getInboxId() + .getBytes()); + propsDict.put(PROP_KEY_OUTBOXID, updateWithMailbox.getOutboxId() + .getBytes()); + updateNoMailbox = new MailboxUpdate(someClientSupportsList); } - private MailboxPropertyManagerImpl createInstance() { + private MailboxUpdateManagerImpl createInstance() { context.checking(new Expectations() {{ oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID, MAJOR_VERSION); will(returnValue(localGroup)); }}); - return new MailboxPropertyManagerImpl(db, clientHelper, + return new MailboxUpdateManagerImpl(db, clientHelper, clientVersioningManager, metadataParser, contactGroupFactory, clock, mailboxSettingsManager, crypto); } @@ -161,7 +162,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { emptyServerSupports, emptyPropsDict, true); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.onDatabaseOpened(txn); } @@ -193,11 +194,11 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(ownProps)); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getAuthToken())); + will(returnValue(updateWithMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getInboxId())); + will(returnValue(updateWithMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getOutboxId())); + will(returnValue(updateWithMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); @@ -208,7 +209,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { someServerSupports, propsDict, true); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.onDatabaseOpened(txn); } @@ -222,7 +223,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(true)); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.onDatabaseOpened(txn); } @@ -259,7 +260,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { emptyServerSupports, emptyPropsDict, true); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.addingContact(txn, contact); } @@ -287,11 +288,11 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); will(returnValue(ownProps)); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getAuthToken())); + will(returnValue(updateWithMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getInboxId())); + will(returnValue(updateWithMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getOutboxId())); + will(returnValue(updateWithMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); @@ -302,7 +303,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { someServerSupports, propsDict, true); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.addingContact(txn, contact); } @@ -319,7 +320,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).removeGroup(txn, contactGroup); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.removingContact(txn, contact); } @@ -355,16 +356,16 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(contact.getId())); oneOf(clientHelper).getMessageAsList(txn, message.getId()); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, someServerSupports, propsDict); - will(returnValue(updateMailbox)); + will(returnValue(updateWithMailbox)); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); assertEquals(ACCEPT_DO_NOT_SHARE, t.incomingMessage(txn, message, meta)); - assertTrue(hasEvent(txn, RemoteMailboxPropertiesUpdateEvent.class)); + assertTrue(hasEvent(txn, RemoteMailboxUpdateEvent.class)); } @Test @@ -407,16 +408,16 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(contact.getId())); oneOf(clientHelper).getMessageAsList(txn, message.getId()); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, someServerSupports, propsDict); - will(returnValue(updateMailbox)); + will(returnValue(updateWithMailbox)); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); assertEquals(ACCEPT_DO_NOT_SHARE, t.incomingMessage(txn, message, meta)); - assertTrue(hasEvent(txn, RemoteMailboxPropertiesUpdateEvent.class)); + assertTrue(hasEvent(txn, RemoteMailboxUpdateEvent.class)); } @Test @@ -447,14 +448,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).deleteMessageMetadata(txn, message.getId()); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); assertEquals(ACCEPT_DO_NOT_SHARE, t.incomingMessage(txn, message, meta)); - assertFalse(hasEvent(txn, RemoteMailboxPropertiesUpdateEvent.class)); + assertFalse(hasEvent(txn, RemoteMailboxUpdateEvent.class)); } @Test - public void testCreatesAndStoresLocalPropertiesWithNewVersionOnPairing() + public void testCreatesAndStoresLocalUpdateWithNewVersionOnPairing() throws Exception { Contact contact = getContact(); List contacts = singletonList(contact); @@ -477,11 +478,11 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).getContacts(txn); will(returnValue(contacts)); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getAuthToken())); + will(returnValue(updateWithMailbox.getAuthToken())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getInboxId())); + will(returnValue(updateWithMailbox.getInboxId())); oneOf(crypto).generateUniqueId(); - will(returnValue(updateMailbox.getOutboxId())); + will(returnValue(updateWithMailbox.getOutboxId())); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); @@ -493,12 +494,12 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).removeMessage(txn, latestId); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.mailboxPaired(txn, ownProps.getOnion(), someServerSupportsList); } @Test - public void testStoresEmptyLocalPropertiesWithNewVersionOnUnpairing() + public void testStoresLocalUpdateNoMailboxWithNewVersionOnUnpairing() throws Exception { Contact contact = getContact(); List contacts = singletonList(contact); @@ -531,13 +532,12 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { oneOf(db).removeMessage(txn, latestId); }}); - MailboxPropertyManagerImpl t = createInstance(); + MailboxUpdateManagerImpl t = createInstance(); t.mailboxUnpaired(txn); } @Test - public void testGetRemoteProperties() - throws Exception { + public void testGetRemoteUpdate() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); @@ -554,27 +554,26 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { context.checking(new Expectations() {{ oneOf(db).getContact(txn, contact.getId()); will(returnValue(contact)); - oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, - MAJOR_VERSION, contact); + oneOf(contactGroupFactory) + .createContactGroup(CLIENT_ID, MAJOR_VERSION, contact); will(returnValue(contactGroup)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, contactGroup.getId()); will(returnValue(messageMetadata)); oneOf(clientHelper).getMessageAsList(txn, messageId); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, someServerSupports, propsDict); - will(returnValue(updateMailbox)); + will(returnValue(updateWithMailbox)); }}); - MailboxPropertyManagerImpl t = createInstance(); - MailboxPropertiesUpdate remote = - t.getRemoteProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(remote, updateMailbox)); + MailboxUpdateManagerImpl t = createInstance(); + MailboxUpdate remote = t.getRemoteUpdate(txn, contact.getId()); + assertTrue(mailboxUpdateEqual(remote, updateWithMailbox)); } @Test - public void testGetRemotePropertiesReturnsNullBecauseNoUpdate() + public void testGetRemoteUpdateReturnsNullBecauseNoUpdate() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); @@ -593,13 +592,12 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(emptyMessageMetadata)); }}); - MailboxPropertyManagerImpl t = createInstance(); - assertNull(t.getRemoteProperties(txn, contact.getId())); + MailboxUpdateManagerImpl t = createInstance(); + assertNull(t.getRemoteUpdate(txn, contact.getId())); } @Test - public void testGetRemotePropertiesNoMailbox() - throws Exception { + public void testGetRemoteUpdateNoMailbox() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); @@ -624,20 +622,18 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(messageMetadata)); oneOf(clientHelper).getMessageAsList(txn, messageId); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, emptyServerSupports, emptyPropsDict); will(returnValue(updateNoMailbox)); }}); - MailboxPropertyManagerImpl t = createInstance(); - MailboxPropertiesUpdate remote = - t.getRemoteProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(remote, updateNoMailbox)); + MailboxUpdateManagerImpl t = createInstance(); + MailboxUpdate remote = t.getRemoteUpdate(txn, contact.getId()); + assertTrue(mailboxUpdateEqual(remote, updateNoMailbox)); } @Test - public void testGetLocalProperties() - throws Exception { + public void testGetLocalUpdate() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); @@ -662,20 +658,18 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(messageMetadata)); oneOf(clientHelper).getMessageAsList(txn, messageId); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, someServerSupports, propsDict); - will(returnValue(updateMailbox)); + will(returnValue(updateWithMailbox)); }}); - MailboxPropertyManagerImpl t = createInstance(); - MailboxPropertiesUpdate local = - t.getLocalProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(local, updateMailbox)); + MailboxUpdateManagerImpl t = createInstance(); + MailboxUpdate local = t.getLocalUpdate(txn, contact.getId()); + assertTrue(mailboxUpdateEqual(local, updateWithMailbox)); } @Test - public void testGetLocalPropertiesNoMailbox() - throws Exception { + public void testGetLocalUpdateNoMailbox() throws Exception { Transaction txn = new Transaction(null, false); Contact contact = getContact(); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); @@ -700,15 +694,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase { will(returnValue(messageMetadata)); oneOf(clientHelper).getMessageAsList(txn, messageId); will(returnValue(body)); - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, emptyServerSupports, emptyPropsDict); will(returnValue(updateNoMailbox)); }}); - MailboxPropertyManagerImpl t = createInstance(); - MailboxPropertiesUpdate local = - t.getLocalProperties(txn, contact.getId()); - assertTrue(mailboxPropertiesUpdateEqual(local, updateNoMailbox)); + MailboxUpdateManagerImpl t = createInstance(); + MailboxUpdate local = t.getLocalUpdate(txn, contact.getId()); + assertTrue(mailboxUpdateEqual(local, updateNoMailbox)); } private void expectStoreMessage(Transaction txn, GroupId g, diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateValidatorTest.java similarity index 73% rename from bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java rename to bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateValidatorTest.java index eea64ac5e..8c489dccb 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxPropertyValidatorTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxUpdateValidatorTest.java @@ -8,9 +8,9 @@ import org.briarproject.bramble.api.data.BdfList; import org.briarproject.bramble.api.data.MetadataEncoder; import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxFolderId; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; -import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdateMailbox; -import org.briarproject.bramble.api.mailbox.MailboxPropertyManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdate; +import org.briarproject.bramble.api.mailbox.MailboxUpdateManager; +import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox; import org.briarproject.bramble.api.mailbox.MailboxVersion; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Message; @@ -28,7 +28,7 @@ import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getRandomId; import static org.junit.Assert.assertEquals; -public class MailboxPropertyValidatorTest extends BrambleMockTestCase { +public class MailboxUpdateValidatorTest extends BrambleMockTestCase { private final ClientHelper clientHelper = context.mock(ClientHelper.class); @@ -37,39 +37,39 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { private final BdfList someClientSupports; private final List someClientSupportsList; private final BdfList someServerSupports; - private final MailboxPropertiesUpdateMailbox updateMailbox; - private final MailboxPropertiesUpdate updateNoMailbox; + private final MailboxUpdateWithMailbox updateMailbox; + private final MailboxUpdate updateNoMailbox; private final Group group; private final Message message; - private final MailboxPropertyValidator mpv; + private final MailboxUpdateValidator muv; - public MailboxPropertyValidatorTest() { + public MailboxUpdateValidatorTest() { // Just dummies, clientHelper is mocked so our test is a bit shallow; // not testing - // {@link ClientHelper#parseAndValidateMailboxPropertiesUpdate(BdfDictionary)} + // {@link ClientHelper#parseAndValidateMailboxUpdate(BdfList, BdfList, BdfDictionary)} emptyServerSupports = new BdfList(); someClientSupports = BdfList.of(BdfList.of(1, 0)); someClientSupportsList = singletonList(new MailboxVersion(1, 0)); someServerSupports = BdfList.of(BdfList.of(1, 0)); bdfDict = BdfDictionary.of(new BdfEntry("foo", "bar")); - updateMailbox = new MailboxPropertiesUpdateMailbox( + updateMailbox = new MailboxUpdateWithMailbox( singletonList(new MailboxVersion(1, 0)), singletonList(new MailboxVersion(1, 0)), "baz", new MailboxAuthToken(getRandomId()), new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId())); - updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList); + updateNoMailbox = new MailboxUpdate(someClientSupportsList); - group = getGroup(MailboxPropertyManager.CLIENT_ID, - MailboxPropertyManager.MAJOR_VERSION); + group = getGroup(MailboxUpdateManager.CLIENT_ID, + MailboxUpdateManager.MAJOR_VERSION); message = getMessage(group.getId()); MetadataEncoder metadataEncoder = context.mock(MetadataEncoder.class); Clock clock = context.mock(Clock.class); - mpv = new MailboxPropertyValidator(clientHelper, metadataEncoder, + muv = new MailboxUpdateValidator(clientHelper, metadataEncoder, clock); } @@ -79,26 +79,26 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { BdfList.of(4, someClientSupports, someServerSupports, bdfDict); context.checking(new Expectations() {{ - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, someServerSupports, bdfDict); will(returnValue(updateMailbox)); }}); BdfDictionary result = - mpv.validateMessage(message, group, body).getDictionary(); + muv.validateMessage(message, group, body).getDictionary(); assertEquals(4, result.getLong("version").longValue()); } @Test(expected = FormatException.class) public void testValidateWrongVersionValue() throws IOException { BdfList body = BdfList.of(-1, bdfDict); - mpv.validateMessage(message, group, body); + muv.validateMessage(message, group, body); } @Test(expected = FormatException.class) public void testValidateWrongVersionType() throws IOException { BdfList body = BdfList.of(bdfDict, bdfDict); - mpv.validateMessage(message, group, body); + muv.validateMessage(message, group, body); } @Test @@ -108,13 +108,13 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase { emptyBdfDict); context.checking(new Expectations() {{ - oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( + oneOf(clientHelper).parseAndValidateMailboxUpdate( someClientSupports, emptyServerSupports, emptyBdfDict); will(returnValue(updateNoMailbox)); }}); BdfDictionary result = - mpv.validateMessage(message, group, body).getDictionary(); + muv.validateMessage(message, group, body).getDictionary(); assertEquals(42, result.getLong("version").longValue()); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java b/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java index 0b8adb015..21bfb7df4 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementIntegrationTest.java @@ -345,7 +345,7 @@ public class TransportKeyAgreementIntegrationTest .canSendOutgoingStreams(aliceId, DUPLEX_TRANSPORT_ID)); } - // Sync initial client versioning updates and mailbox properties updates + // Sync initial client versioning updates and mailbox updates syncMessage(alice, bob, bobId, 1, true); syncMessage(bob, alice, aliceId, 1, true); syncMessage(alice, bob, bobId, 2, true); diff --git a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java index e022adf92..88e13b57a 100644 --- a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java @@ -1051,8 +1051,8 @@ public class IntroductionIntegrationTest true); contact0From1 = contactManager1.getContact(contactId0From1); - // Sync initial client versioning updates, mailbox properties updates, - // and transport properties + // Sync initial client versioning updates, mailbox updates, and + // transport properties sync0To1(1, true); sync1To0(1, true); sync0To1(3, true); diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java index 9226fde90..2dd294b79 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java @@ -172,7 +172,7 @@ public abstract class BriarIntegrationTest