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.
This commit is contained in:
Daniel Lublin
2022-05-13 11:45:42 +02:00
parent 5d5d8d206c
commit a42d9eec1c
17 changed files with 473 additions and 171 deletions

View File

@@ -21,8 +21,6 @@ import java.security.GeneralSecurityException;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable;
@NotNullByDefault @NotNullByDefault
public interface ClientHelper { public interface ClientHelper {
@@ -127,15 +125,13 @@ public interface ClientHelper {
BdfDictionary properties) throws FormatException; BdfDictionary properties) throws FormatException;
/** /**
* Parse and validate the property dictionary of a Mailbox property update * Parse and validate the elements of a Mailbox property update message.
* message.
* *
* @return the properties for using the Mailbox, or null if there is no * @return the parsed update message
* Mailbox available * @throws FormatException if the message elements are invalid
* @throws FormatException if the properties are not valid
*/ */
@Nullable
MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate(
BdfList clientSupports, BdfList serverSupports,
BdfDictionary properties) throws FormatException; BdfDictionary properties) throws FormatException;
/** /**

View File

@@ -2,40 +2,27 @@ package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.List;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
public class MailboxPropertiesUpdate { public class MailboxPropertiesUpdate {
private final String onion; boolean hasMailbox;
private final MailboxAuthToken authToken; private final List<MailboxVersion> clientSupports;
private final MailboxFolderId inboxId;
private final MailboxFolderId outboxId;
public MailboxPropertiesUpdate(String onion, public MailboxPropertiesUpdate(List<MailboxVersion> clientSupports) {
MailboxAuthToken authToken, MailboxFolderId inboxId, this.hasMailbox = false;
MailboxFolderId outboxId) { this.clientSupports = clientSupports;
this.onion = onion;
this.authToken = authToken;
this.inboxId = inboxId;
this.outboxId = outboxId;
} }
public String getOnion() { public List<MailboxVersion> getClientSupports() {
return onion; return clientSupports;
} }
public MailboxAuthToken getAuthToken() { public boolean hasMailbox() {
return authToken; return hasMailbox;
} }
public MailboxFolderId getInboxId() {
return inboxId;
}
public MailboxFolderId getOutboxId() {
return outboxId;
}
} }

View File

@@ -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<MailboxVersion> serverSupports;
private final String onion;
private final MailboxAuthToken authToken;
private final MailboxFolderId inboxId;
private final MailboxFolderId outboxId;
public MailboxPropertiesUpdateMailbox(List<MailboxVersion> clientSupports,
List<MailboxVersion> 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<MailboxVersion> getServerSupports() {
return serverSupports;
}
}

View File

@@ -20,7 +20,7 @@ public interface MailboxPropertyManager {
/** /**
* The current major version of the mailbox property client. * 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. * The current minor version of the mailbox property client.

View File

@@ -7,6 +7,8 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@NotNullByDefault @NotNullByDefault
@@ -49,7 +51,8 @@ public interface MailboxSettingsManager {
* @param txn A read-write transaction * @param txn A read-write transaction
* @param ownOnion Our new mailbox's onion (56 base32 chars) * @param ownOnion Our new mailbox's onion (56 base32 chars)
*/ */
void mailboxPaired(Transaction txn, String ownOnion) void mailboxPaired(Transaction txn, String ownOnion,
List<MailboxVersion> serverSupports)
throws DbException; throws DbException;
/** /**

View File

@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.event.Event; import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
@@ -16,11 +15,10 @@ import javax.annotation.concurrent.Immutable;
public class RemoteMailboxPropertiesUpdateEvent extends Event { public class RemoteMailboxPropertiesUpdateEvent extends Event {
private final ContactId contactId; private final ContactId contactId;
@Nullable
private final MailboxPropertiesUpdate mailboxPropertiesUpdate; private final MailboxPropertiesUpdate mailboxPropertiesUpdate;
public RemoteMailboxPropertiesUpdateEvent(ContactId contactId, public RemoteMailboxPropertiesUpdateEvent(ContactId contactId,
@Nullable MailboxPropertiesUpdate mailboxPropertiesUpdate) { MailboxPropertiesUpdate mailboxPropertiesUpdate) {
this.contactId = contactId; this.contactId = contactId;
this.mailboxPropertiesUpdate = mailboxPropertiesUpdate; this.mailboxPropertiesUpdate = mailboxPropertiesUpdate;
} }
@@ -29,7 +27,6 @@ public class RemoteMailboxPropertiesUpdateEvent extends Event {
return contactId; return contactId;
} }
@Nullable
public MailboxPropertiesUpdate getMailboxPropertiesUpdate() { public MailboxPropertiesUpdate getMailboxPropertiesUpdate() {
return mailboxPropertiesUpdate; return mailboxPropertiesUpdate;
} }

View File

@@ -22,6 +22,7 @@ import org.briarproject.bramble.api.identity.Identity;
import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.plugin.TransportId;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.sync.ClientId; import org.briarproject.bramble.api.sync.ClientId;
@@ -286,10 +287,21 @@ public class TestUtils {
if (a == null || b == null) { if (a == null || b == null) {
return a == b; return a == b;
} }
return a.getOnion().equals(b.getOnion()) && if (!a.hasMailbox() && !b.hasMailbox()) {
a.getAuthToken().equals(b.getAuthToken()) && return a.getClientSupports().equals(b.getClientSupports());
a.getInboxId().equals(b.getInboxId()) && } else if (a.hasMailbox() && b.hasMailbox()) {
a.getOutboxId().equals(b.getOutboxId()); 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( public static boolean mailboxPropertiesEqual(

View File

@@ -26,6 +26,8 @@ import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
@@ -39,12 +41,13 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import javax.inject.Inject; import javax.inject.Inject;
@@ -412,11 +415,28 @@ class ClientHelperImpl implements ClientHelper {
} }
@Override @Override
@Nullable
public MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate( public MailboxPropertiesUpdate parseAndValidateMailboxPropertiesUpdate(
BdfList clientSupports, BdfList serverSupports,
BdfDictionary properties) throws FormatException { BdfDictionary properties) throws FormatException {
List<MailboxVersion> clientSupportsList =
getMailboxVersionList(clientSupports);
List<MailboxVersion> serverSupportsList =
getMailboxVersionList(serverSupports);
// We must always learn what Mailbox API version(s) the client supports
if (clientSupports.isEmpty()) {
throw new FormatException();
}
if (properties.isEmpty()) { 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 // Accepting more props than we need, for forward compatibility
if (properties.size() < PROP_COUNT) { if (properties.size() < PROP_COUNT) {
@@ -435,9 +455,23 @@ class ClientHelperImpl implements ClientHelper {
checkLength(inboxId, UniqueId.LENGTH); checkLength(inboxId, UniqueId.LENGTH);
byte[] outboxId = properties.getRaw(PROP_KEY_OUTBOXID); byte[] outboxId = properties.getRaw(PROP_KEY_OUTBOXID);
checkLength(outboxId, UniqueId.LENGTH); checkLength(outboxId, UniqueId.LENGTH);
return new MailboxPropertiesUpdate(onion, return new MailboxPropertiesUpdateMailbox(clientSupportsList,
new MailboxAuthToken(authToken), new MailboxFolderId(inboxId), serverSupportsList, onion, new MailboxAuthToken(authToken),
new MailboxFolderId(outboxId)); new MailboxFolderId(inboxId), new MailboxFolderId(outboxId));
}
private List<MailboxVersion> getMailboxVersionList(BdfList bdfList)
throws FormatException {
List<MailboxVersion> 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 @Override

View File

@@ -7,6 +7,8 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFileId; import org.briarproject.bramble.api.mailbox.MailboxFileId;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxProperties; 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 org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.File; import java.io.File;
@@ -17,9 +19,18 @@ import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static java.util.Collections.singletonList;
@NotNullByDefault @NotNullByDefault
interface MailboxApi { interface MailboxApi {
/**
* Mailbox API versions that we support as a client. This is reported to our
* contacts by {@link MailboxPropertyManager}.
*/
List<MailboxVersion> CLIENT_SUPPORTS = singletonList(
new MailboxVersion(1, 0));
/** /**
* Sets up the mailbox with the setup token. * Sets up the mailbox with the setup token.
* *

View File

@@ -127,7 +127,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask {
for (Contact c : db.getContacts(txn)) { for (Contact c : db.getContacts(txn)) {
MailboxPropertiesUpdate remoteProps = mailboxPropertyManager MailboxPropertiesUpdate remoteProps = mailboxPropertyManager
.getRemoteProperties(txn, c.getId()); .getRemoteProperties(txn, c.getId());
if (remoteProps == null) { if (remoteProps == null || !remoteProps.hasMailbox()) {
db.resetUnackedMessagesToSend(txn, c.getId()); db.resetUnackedMessagesToSend(txn, c.getId());
} }
} }

View File

@@ -19,9 +19,11 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.MailboxPropertyManager;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager.MailboxHook; 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.mailbox.RemoteMailboxPropertiesUpdateEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Group; 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;
import org.briarproject.bramble.api.versioning.ClientVersioningManager.ClientVersioningHook; import org.briarproject.bramble.api.versioning.ClientVersioningManager.ClientVersioningHook;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@@ -42,6 +45,7 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE; import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE;
import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS;
@NotNullByDefault @NotNullByDefault
class MailboxPropertyManagerImpl implements MailboxPropertyManager, class MailboxPropertyManagerImpl implements MailboxPropertyManager,
@@ -100,11 +104,15 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager,
db.setGroupVisibility(txn, c.getId(), g.getId(), client); db.setGroupVisibility(txn, c.getId(), g.getId(), client);
// Attach the contact ID to the group // Attach the contact ID to the group
clientHelper.setContactId(txn, g.getId(), c.getId()); clientHelper.setContactId(txn, g.getId(), c.getId());
// If we are paired, create and send props to the newly added contact
MailboxProperties ownProps = MailboxProperties ownProps =
mailboxSettingsManager.getOwnMailboxProperties(txn); mailboxSettingsManager.getOwnMailboxProperties(txn);
if (ownProps != null) { 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 @Override
public void mailboxPaired(Transaction txn, String ownOnion) public void mailboxPaired(Transaction txn, String ownOnion,
List<MailboxVersion> serverSupports)
throws DbException { throws DbException {
for (Contact c : db.getContacts(txn)) { 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 * Creates and sends an update message to the given contact. The message
* holds our own mailbox's onion, and generated unique properties. All of * holds our own mailbox's onion, generated unique properties, and lists of
* which the contact needs to communicate with our Mailbox. * supported Mailbox API version(s). All of which the contact needs to
* communicate with our Mailbox.
*/ */
private void createAndSendProperties(Transaction txn, private void createAndSendProperties(Transaction txn, Contact c,
Contact c, String ownOnion) throws DbException { List<MailboxVersion> serverSupports, String ownOnion)
MailboxPropertiesUpdate p = new MailboxPropertiesUpdate(ownOnion, throws DbException {
MailboxPropertiesUpdate p = new MailboxPropertiesUpdateMailbox(
CLIENT_SUPPORTS, serverSupports, ownOnion,
new MailboxAuthToken(crypto.generateUniqueId().getBytes()), new MailboxAuthToken(crypto.generateUniqueId().getBytes()),
new MailboxFolderId(crypto.generateUniqueId().getBytes()), new MailboxFolderId(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 * Sends an update message with empty properties to the given contact. The
* indicates for the receiving contact that we no longer have a Mailbox that * empty update indicates for the receiving contact that we don't have any
* they can use. * 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 sendEmptyProperties(Transaction txn, Contact c)
throws DbException { throws DbException {
Group g = getContactGroup(c); Group g = getContactGroup(c);
storeMessageReplaceLatest(txn, g.getId(), null); MailboxPropertiesUpdate p =
new MailboxPropertiesUpdate(CLIENT_SUPPORTS);
storeMessageReplaceLatest(txn, g.getId(), p);
} }
@Nullable @Nullable
@@ -229,7 +244,7 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager,
} }
private void storeMessageReplaceLatest(Transaction txn, GroupId g, private void storeMessageReplaceLatest(Transaction txn, GroupId g,
@Nullable MailboxPropertiesUpdate p) throws DbException { MailboxPropertiesUpdate p) throws DbException {
try { try {
LatestUpdate latest = findLatest(txn, g, true); LatestUpdate latest = findLatest(txn, g, true);
long version = latest == null ? 1 : latest.version + 1; long version = latest == null ? 1 : latest.version + 1;
@@ -266,23 +281,38 @@ class MailboxPropertyManagerImpl implements MailboxPropertyManager,
return null; return null;
} }
private BdfList encodeProperties(long version, private BdfList encodeProperties(long version, MailboxPropertiesUpdate p) {
@Nullable MailboxPropertiesUpdate p) {
BdfDictionary dict = new BdfDictionary(); BdfDictionary dict = new BdfDictionary();
if (p != null) { BdfList serverSupports = new BdfList();
dict.put(PROP_KEY_ONION, p.getOnion()); if (p.hasMailbox()) {
dict.put(PROP_KEY_AUTHTOKEN, p.getAuthToken().getBytes()); MailboxPropertiesUpdateMailbox pm =
dict.put(PROP_KEY_INBOXID, p.getInboxId().getBytes()); (MailboxPropertiesUpdateMailbox) p;
dict.put(PROP_KEY_OUTBOXID, p.getOutboxId().getBytes()); 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<MailboxVersion> 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) private MailboxPropertiesUpdate parseProperties(BdfList body)
throws FormatException { throws FormatException {
BdfDictionary dict = body.getDictionary(1); BdfList clientSupports = body.getList(1);
return clientHelper.parseAndValidateMailboxPropertiesUpdate(dict); BdfList serverSupports = body.getList(2);
BdfDictionary dict = body.getDictionary(3);
return clientHelper.parseAndValidateMailboxPropertiesUpdate(
clientSupports, serverSupports, dict
);
} }
private Group getContactGroup(Contact c) { private Group getContactGroup(Contact c) {

View File

@@ -31,14 +31,20 @@ class MailboxPropertyValidator extends BdfMessageValidator {
@Override @Override
protected BdfMessageContext validateMessage(Message m, Group g, protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws InvalidMessageException, FormatException { BdfList body) throws InvalidMessageException, FormatException {
// Version, properties // Version, Properties, clientSupports, serverSupports
checkSize(body, 2); checkSize(body, 4);
// Version // Version
long version = body.getLong(0); long version = body.getLong(0);
if (version < 0) throw new FormatException(); if (version < 0) throw new FormatException();
// clientSupports
BdfList clientSupports = body.getList(1);
// serverSupports
BdfList serverSupports = body.getList(2);
// Properties // Properties
BdfDictionary dictionary = body.getDictionary(1); BdfDictionary dictionary = body.getDictionary(3);
clientHelper.parseAndValidateMailboxPropertiesUpdate(dictionary); clientHelper.parseAndValidateMailboxPropertiesUpdate(clientSupports,
serverSupports, dictionary
);
// Return the metadata // Return the metadata
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put(MSG_KEY_VERSION, version); meta.put(MSG_KEY_VERSION, version);

View File

@@ -91,7 +91,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
s.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS, ints); s.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS, ints);
settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE); settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE);
for (MailboxHook hook : hooks) { for (MailboxHook hook : hooks) {
hook.mailboxPaired(txn, p.getOnion()); hook.mailboxPaired(txn, p.getOnion(), p.getServerSupports());
} }
} }

View File

@@ -24,6 +24,8 @@ import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.GroupId;
import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
@@ -41,6 +43,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; 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_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_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.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.briarproject.bramble.util.StringUtils.getRandomString;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; 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.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -95,10 +98,20 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
messageFactory, bdfReaderFactory, bdfWriterFactory, metadataParser, messageFactory, bdfReaderFactory, bdfWriterFactory, metadataParser,
metadataEncoder, cryptoComponent, authorFactory); 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() { 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", "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd",
new MailboxAuthToken(getRandomId()), new MailboxAuthToken(getRandomId()),
new MailboxFolderId(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 @Test
public void testParseEmptyMailboxPropsUpdate() throws Exception { public void testParseEmptyMailboxPropsUpdate() throws Exception {
BdfDictionary emptyPropsDict = new BdfDictionary(); BdfDictionary emptyPropsDict = new BdfDictionary();
MailboxPropertiesUpdate parsedProps = clientHelper MailboxPropertiesUpdate parsedProps = clientHelper
.parseAndValidateMailboxPropertiesUpdate(emptyPropsDict); .parseAndValidateMailboxPropertiesUpdate(someClientSupports,
assertNull(parsedProps); 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 @Test
public void testParseValidMailboxPropsUpdate() throws Exception { public void testParseValidMailboxPropsUpdate() throws Exception {
MailboxPropertiesUpdate parsedProps = clientHelper MailboxPropertiesUpdate parsedProps = clientHelper
.parseAndValidateMailboxPropertiesUpdate( .parseAndValidateMailboxPropertiesUpdate(
getValidMailboxPropsUpdateDict()); someClientSupports, someServerSupports,
getValidMailboxPropsUpdateDict()
);
assertTrue(mailboxPropertiesUpdateEqual(validMailboxPropsUpdate, assertTrue(mailboxPropertiesUpdateEqual(validMailboxPropsUpdate,
parsedProps)); parsedProps));
} }
@Test(expected = FormatException.class)
public void rejectsMailboxPropsWithEmptyServerSupports() throws Exception {
clientHelper.parseAndValidateMailboxPropertiesUpdate(
someClientSupports, emptyServerSupports,
getValidMailboxPropsUpdateDict()
);
}
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testRejectsMailboxPropsUpdateOnionNotDecodable() public void testRejectsMailboxPropsUpdateOnionNotDecodable()
throws Exception { throws Exception {
@@ -570,7 +644,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
String badOnion = "!" + propsDict.getString(PROP_KEY_ONION) String badOnion = "!" + propsDict.getString(PROP_KEY_ONION)
.substring(1); .substring(1);
propsDict.put(PROP_KEY_ONION, badOnion); propsDict.put(PROP_KEY_ONION, badOnion);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
emptyServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -579,7 +655,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
String tooLongOnion = propsDict.getString(PROP_KEY_ONION) + "!"; String tooLongOnion = propsDict.getString(PROP_KEY_ONION) + "!";
propsDict.put(PROP_KEY_ONION, tooLongOnion); propsDict.put(PROP_KEY_ONION, tooLongOnion);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
emptyServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -587,7 +665,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.put(PROP_KEY_INBOXID, getRandomBytes(UniqueId.LENGTH + 1)); propsDict.put(PROP_KEY_INBOXID, getRandomBytes(UniqueId.LENGTH + 1));
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -595,7 +675,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.put(PROP_KEY_OUTBOXID, getRandomBytes(UniqueId.LENGTH + 1)); propsDict.put(PROP_KEY_OUTBOXID, getRandomBytes(UniqueId.LENGTH + 1));
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -603,14 +685,18 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.put(PROP_KEY_AUTHTOKEN, getRandomBytes(UniqueId.LENGTH + 1)); propsDict.put(PROP_KEY_AUTHTOKEN, getRandomBytes(UniqueId.LENGTH + 1));
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testRejectsMailboxPropsUpdateMissingOnion() throws Exception { public void testRejectsMailboxPropsUpdateMissingOnion() throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.remove(PROP_KEY_ONION); propsDict.remove(PROP_KEY_ONION);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -618,14 +704,18 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.remove(PROP_KEY_AUTHTOKEN); propsDict.remove(PROP_KEY_AUTHTOKEN);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testRejectsMailboxPropsUpdateMissingInboxId() throws Exception { public void testRejectsMailboxPropsUpdateMissingInboxId() throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.remove(PROP_KEY_INBOXID); propsDict.remove(PROP_KEY_INBOXID);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
@@ -633,7 +723,9 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
BdfDictionary propsDict = getValidMailboxPropsUpdateDict(); BdfDictionary propsDict = getValidMailboxPropsUpdateDict();
propsDict.remove(PROP_KEY_OUTBOXID); propsDict.remove(PROP_KEY_OUTBOXID);
clientHelper.parseAndValidateMailboxPropertiesUpdate(propsDict); clientHelper.parseAndValidateMailboxPropertiesUpdate(someClientSupports,
someServerSupports, propsDict
);
} }
} }

View File

@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxPairingState; import org.briarproject.bramble.api.mailbox.MailboxPairingState;
import org.briarproject.bramble.api.mailbox.MailboxPairingTask; import org.briarproject.bramble.api.mailbox.MailboxPairingTask;
import org.briarproject.bramble.api.mailbox.MailboxProperties; 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.MailboxPropertyManager;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager; import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent; import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent;
@@ -24,10 +25,11 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger; 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.getContact;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId; import static org.briarproject.bramble.test.TestUtils.getRandomId;
@@ -105,16 +107,18 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
}}); }});
Contact contact1 = getContact(); Contact contact1 = getContact();
Transaction txn = new Transaction(null, false); Transaction txn = new Transaction(null, false);
MailboxPropertiesUpdate emptyProps = new MailboxPropertiesUpdate(
CLIENT_SUPPORTS);
context.checking(new DbExpectations() {{ context.checking(new DbExpectations() {{
oneOf(db).transaction(with(false), withDbRunnable(txn)); oneOf(db).transaction(with(false), withDbRunnable(txn));
oneOf(mailboxSettingsManager).setOwnMailboxProperties( oneOf(mailboxSettingsManager).setOwnMailboxProperties(
with(txn), with(matches(ownerProperties))); with(txn), with(matches(ownerProperties)));
oneOf(mailboxSettingsManager).recordSuccessfulConnection(txn, time); oneOf(mailboxSettingsManager).recordSuccessfulConnection(txn, time);
oneOf(db).getContacts(txn); oneOf(db).getContacts(txn);
will(returnValue(Collections.singletonList(contact1))); will(returnValue(singletonList(contact1)));
oneOf(mailboxPropertyManager).getRemoteProperties(txn, oneOf(mailboxPropertyManager).getRemoteProperties(txn,
contact1.getId()); contact1.getId());
will(returnValue(null)); will(returnValue(emptyProps));
oneOf(db).resetUnackedMessagesToSend(txn, contact1.getId()); oneOf(db).resetUnackedMessagesToSend(txn, contact1.getId());
}}); }});

View File

@@ -15,7 +15,9 @@ import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.MailboxSettingsManager;
import org.briarproject.bramble.api.mailbox.MailboxVersion;
import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent; import org.briarproject.bramble.api.mailbox.RemoteMailboxPropertiesUpdateEvent;
import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.GroupId;
@@ -27,7 +29,6 @@ import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.mailbox.MailboxPropertyManager.PROP_KEY_OUTBOXID;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED; 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.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.getContact;
import static org.briarproject.bramble.test.TestUtils.getGroup; import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage; 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 Group localGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
private final BdfDictionary propsDict; private final BdfDictionary propsDict;
private final BdfDictionary emptyPropsDict = new BdfDictionary(); private final BdfDictionary emptyPropsDict = new BdfDictionary();
private final MailboxPropertiesUpdate props; private final BdfList realClientSupports;
private final BdfList someClientSupports;
private final List<MailboxVersion> someClientSupportsList;
private final BdfList emptyServerSupports;
private final BdfList someServerSupports;
private final List<MailboxVersion> someServerSupportsList;
private final MailboxPropertiesUpdateMailbox updateMailbox;
private final MailboxPropertiesUpdate updateNoMailbox;
private final MailboxProperties ownProps; private final MailboxProperties ownProps;
public MailboxPropertyManagerImplTest() { 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", ownProps = new MailboxProperties("http://bar.onion",
new MailboxAuthToken(getRandomId()), true, new ArrayList<>()); new MailboxAuthToken(getRandomId()), true,
props = new MailboxPropertiesUpdate(ownProps.getOnion(), someServerSupportsList);
updateMailbox = new MailboxPropertiesUpdateMailbox(
singletonList(new MailboxVersion(1, 0)),
singletonList(new MailboxVersion(1, 0)),
ownProps.getOnion(),
new MailboxAuthToken(getRandomId()), new MailboxAuthToken(getRandomId()),
new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId()),
new MailboxFolderId(getRandomId())); new MailboxFolderId(getRandomId()));
propsDict = new BdfDictionary(); propsDict = new BdfDictionary();
propsDict.put(PROP_KEY_ONION, props.getOnion()); propsDict.put(PROP_KEY_ONION, updateMailbox.getOnion());
propsDict.put(PROP_KEY_AUTHTOKEN, props.getAuthToken().getBytes()); propsDict.put(PROP_KEY_AUTHTOKEN,
propsDict.put(PROP_KEY_INBOXID, props.getInboxId().getBytes()); updateMailbox.getAuthToken().getBytes());
propsDict.put(PROP_KEY_OUTBOXID, props.getOutboxId().getBytes()); propsDict.put(PROP_KEY_INBOXID, updateMailbox.getInboxId().getBytes());
propsDict.put(PROP_KEY_OUTBOXID,
updateMailbox.getOutboxId().getBytes());
updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList);
} }
private MailboxPropertyManagerImpl createInstance() { private MailboxPropertyManagerImpl createInstance() {
@@ -105,6 +130,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
Transaction txn = new Transaction(null, false); Transaction txn = new Transaction(null, false);
Contact contact = getContact(); Contact contact = getContact();
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).containsGroup(txn, localGroup.getId()); oneOf(db).containsGroup(txn, localGroup.getId());
@@ -125,6 +151,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
contact.getId()); contact.getId());
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
will(returnValue(null)); 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(); MailboxPropertyManagerImpl t = createInstance();
@@ -159,18 +193,19 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
will(returnValue(ownProps)); will(returnValue(ownProps));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getAuthToken())); will(returnValue(updateMailbox.getAuthToken()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getInboxId())); will(returnValue(updateMailbox.getInboxId()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getOutboxId())); will(returnValue(updateMailbox.getOutboxId()));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
MAJOR_VERSION, contact); MAJOR_VERSION, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
expectStoreMessage(txn, contactGroup.getId(), propsDict, 1, true); expectStoreMessage(txn, contactGroup.getId(), 1, realClientSupports,
someServerSupports, propsDict, true);
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
@@ -197,6 +232,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
Transaction txn = new Transaction(null, false); Transaction txn = new Transaction(null, false);
Contact contact = getContact(); Contact contact = getContact();
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION); Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Create the group and share it with the contact // Create the group and share it with the contact
@@ -213,6 +249,14 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
contact.getId()); contact.getId());
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
will(returnValue(null)); 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(); MailboxPropertyManagerImpl t = createInstance();
@@ -243,18 +287,19 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn); oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
will(returnValue(ownProps)); will(returnValue(ownProps));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getAuthToken())); will(returnValue(updateMailbox.getAuthToken()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getInboxId())); will(returnValue(updateMailbox.getInboxId()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getOutboxId())); will(returnValue(updateMailbox.getOutboxId()));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
MAJOR_VERSION, contact); MAJOR_VERSION, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
expectStoreMessage(txn, contactGroup.getId(), propsDict, 1, true); expectStoreMessage(txn, contactGroup.getId(), 1, someClientSupports,
someServerSupports, propsDict, true);
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
@@ -285,7 +330,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
Contact contact = getContact(); Contact contact = getContact();
GroupId contactGroupId = new GroupId(getRandomId()); GroupId contactGroupId = new GroupId(getRandomId());
Message message = getMessage(contactGroupId); Message message = getMessage(contactGroupId);
BdfList body = BdfList.of(1, propsDict); BdfList body = BdfList.of(1, someClientSupports, someServerSupports,
propsDict);
Metadata meta = new Metadata(); Metadata meta = new Metadata();
BdfDictionary metaDictionary = BdfDictionary.of( BdfDictionary metaDictionary = BdfDictionary.of(
new BdfEntry(MSG_KEY_VERSION, 1), new BdfEntry(MSG_KEY_VERSION, 1),
@@ -310,8 +356,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageAsList(txn, message.getId()); oneOf(clientHelper).getMessageAsList(txn, message.getId());
will(returnValue(body)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
propsDict); someClientSupports, someServerSupports, propsDict);
will(returnValue(props)); will(returnValue(updateMailbox));
oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId());
}}); }});
@@ -328,7 +374,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
Contact contact = getContact(); Contact contact = getContact();
GroupId contactGroupId = new GroupId(getRandomId()); GroupId contactGroupId = new GroupId(getRandomId());
Message message = getMessage(contactGroupId); Message message = getMessage(contactGroupId);
BdfList body = BdfList.of(1, propsDict); BdfList body = BdfList.of(1, someClientSupports, someServerSupports,
propsDict);
Metadata meta = new Metadata(); Metadata meta = new Metadata();
BdfDictionary metaDictionary = BdfDictionary.of( BdfDictionary metaDictionary = BdfDictionary.of(
new BdfEntry(MSG_KEY_VERSION, 2), new BdfEntry(MSG_KEY_VERSION, 2),
@@ -361,8 +408,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageAsList(txn, message.getId()); oneOf(clientHelper).getMessageAsList(txn, message.getId());
will(returnValue(body)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
propsDict); someClientSupports, someServerSupports, propsDict);
will(returnValue(props)); will(returnValue(updateMailbox));
oneOf(db).resetUnackedMessagesToSend(txn, contact.getId()); oneOf(db).resetUnackedMessagesToSend(txn, contact.getId());
}}); }});
@@ -430,23 +477,24 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(db).getContacts(txn); oneOf(db).getContacts(txn);
will(returnValue(contacts)); will(returnValue(contacts));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getAuthToken())); will(returnValue(updateMailbox.getAuthToken()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getInboxId())); will(returnValue(updateMailbox.getInboxId()));
oneOf(crypto).generateUniqueId(); oneOf(crypto).generateUniqueId();
will(returnValue(props.getOutboxId())); will(returnValue(updateMailbox.getOutboxId()));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
MAJOR_VERSION, contact); MAJOR_VERSION, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
expectStoreMessage(txn, contactGroup.getId(), propsDict, 2, true); expectStoreMessage(txn, contactGroup.getId(), 2, someClientSupports,
someServerSupports, propsDict, true);
oneOf(db).removeMessage(txn, latestId); oneOf(db).removeMessage(txn, latestId);
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
t.mailboxPaired(txn, ownProps.getOnion()); t.mailboxPaired(txn, ownProps.getOnion(), someServerSupportsList);
} }
@Test @Test
@@ -478,8 +526,8 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
expectStoreMessage(txn, contactGroup.getId(), emptyPropsDict, expectStoreMessage(txn, contactGroup.getId(), 2, someClientSupports,
2, true); emptyServerSupports, emptyPropsDict, true);
oneOf(db).removeMessage(txn, latestId); oneOf(db).removeMessage(txn, latestId);
}}); }});
@@ -498,9 +546,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
new BdfEntry(MSG_KEY_LOCAL, false) new BdfEntry(MSG_KEY_LOCAL, false)
); );
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>(); Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
MessageId fooUpdateId = new MessageId(getRandomId()); MessageId messageId = new MessageId(getRandomId());
messageMetadata.put(fooUpdateId, metaDictionary); messageMetadata.put(messageId, metaDictionary);
BdfList fooUpdate = BdfList.of(1, propsDict); BdfList body = BdfList.of(1, someClientSupports, someServerSupports,
propsDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).getContact(txn, contact.getId()); oneOf(db).getContact(txn, contact.getId());
@@ -511,17 +560,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); oneOf(clientHelper).getMessageAsList(txn, messageId);
will(returnValue(fooUpdate)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
propsDict); someClientSupports, someServerSupports, propsDict);
will(returnValue(props)); will(returnValue(updateMailbox));
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
MailboxPropertiesUpdate remote = MailboxPropertiesUpdate remote =
t.getRemoteProperties(txn, contact.getId()); t.getRemoteProperties(txn, contact.getId());
assertTrue(mailboxPropertiesUpdateEqual(remote, props)); assertTrue(mailboxPropertiesUpdateEqual(remote, updateMailbox));
} }
@Test @Test
@@ -549,7 +598,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
} }
@Test @Test
public void testGetRemotePropertiesReturnsNullBecauseEmptyUpdate() public void testGetRemotePropertiesNoMailbox()
throws Exception { throws Exception {
Transaction txn = new Transaction(null, false); Transaction txn = new Transaction(null, false);
Contact contact = getContact(); Contact contact = getContact();
@@ -559,9 +608,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
new BdfEntry(MSG_KEY_LOCAL, false) new BdfEntry(MSG_KEY_LOCAL, false)
); );
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>(); Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
MessageId fooUpdateId = new MessageId(getRandomId()); MessageId messageId = new MessageId(getRandomId());
messageMetadata.put(fooUpdateId, metaDictionary); messageMetadata.put(messageId, metaDictionary);
BdfList fooUpdate = BdfList.of(1, emptyPropsDict); BdfList body = BdfList.of(1, someClientSupports, emptyServerSupports,
emptyPropsDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).getContact(txn, contact.getId()); oneOf(db).getContact(txn, contact.getId());
@@ -572,15 +622,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); oneOf(clientHelper).getMessageAsList(txn, messageId);
will(returnValue(fooUpdate)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
emptyPropsDict); someClientSupports, emptyServerSupports, emptyPropsDict);
will(returnValue(null)); will(returnValue(updateNoMailbox));
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
assertNull(t.getRemoteProperties(txn, contact.getId())); MailboxPropertiesUpdate remote =
t.getRemoteProperties(txn, contact.getId());
assertTrue(mailboxPropertiesUpdateEqual(remote, updateNoMailbox));
} }
@Test @Test
@@ -594,9 +646,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
new BdfEntry(MSG_KEY_LOCAL, true) new BdfEntry(MSG_KEY_LOCAL, true)
); );
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>(); Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
MessageId fooUpdateId = new MessageId(getRandomId()); MessageId messageId = new MessageId(getRandomId());
messageMetadata.put(fooUpdateId, metaDictionary); messageMetadata.put(messageId, metaDictionary);
BdfList fooUpdate = BdfList.of(1, propsDict); BdfList body = BdfList.of(1, someClientSupports, someServerSupports,
propsDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).getContact(txn, contact.getId()); oneOf(db).getContact(txn, contact.getId());
@@ -607,17 +660,17 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); oneOf(clientHelper).getMessageAsList(txn, messageId);
will(returnValue(fooUpdate)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
propsDict); someClientSupports, someServerSupports, propsDict);
will(returnValue(props)); will(returnValue(updateMailbox));
}}); }});
MailboxPropertyManagerImpl t = createInstance(); MailboxPropertyManagerImpl t = createInstance();
MailboxPropertiesUpdate local = MailboxPropertiesUpdate local =
t.getLocalProperties(txn, contact.getId()); t.getLocalProperties(txn, contact.getId());
assertTrue(mailboxPropertiesUpdateEqual(local, props)); assertTrue(mailboxPropertiesUpdateEqual(local, updateMailbox));
} }
@Test @Test
@@ -645,7 +698,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
} }
@Test @Test
public void testGetLocalPropertiesReturnsNullBecauseEmptyUpdate() public void testGetLocalPropertiesNoMailbox()
throws Exception { throws Exception {
Transaction txn = new Transaction(null, false); Transaction txn = new Transaction(null, false);
Contact contact = getContact(); Contact contact = getContact();
@@ -655,9 +708,10 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
new BdfEntry(MSG_KEY_LOCAL, true) new BdfEntry(MSG_KEY_LOCAL, true)
); );
Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>(); Map<MessageId, BdfDictionary> messageMetadata = new LinkedHashMap<>();
MessageId fooUpdateId = new MessageId(getRandomId()); MessageId messageId = new MessageId(getRandomId());
messageMetadata.put(fooUpdateId, metaDictionary); messageMetadata.put(messageId, metaDictionary);
BdfList fooUpdate = BdfList.of(1, emptyPropsDict); BdfList body = BdfList.of(1, someClientSupports, emptyServerSupports,
emptyPropsDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).getContact(txn, contact.getId()); oneOf(db).getContact(txn, contact.getId());
@@ -668,21 +722,25 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
contactGroup.getId()); contactGroup.getId());
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId); oneOf(clientHelper).getMessageAsList(txn, messageId);
will(returnValue(fooUpdate)); will(returnValue(body));
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
emptyPropsDict); someClientSupports, emptyServerSupports, emptyPropsDict);
will(returnValue(null)); will(returnValue(updateNoMailbox));
}}); }});
MailboxPropertyManagerImpl t = createInstance(); 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, 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 { throws Exception {
BdfList body = BdfList.of(version, properties); BdfList body = BdfList.of(version, clientSupports, serverSupports,
properties);
Message message = getMessage(g); Message message = getMessage(g);
long timestamp = message.getTimestamp(); long timestamp = message.getTimestamp();
BdfDictionary meta = BdfDictionary.of( BdfDictionary meta = BdfDictionary.of(

View File

@@ -9,7 +9,9 @@ import org.briarproject.bramble.api.data.MetadataEncoder;
import org.briarproject.bramble.api.mailbox.MailboxAuthToken; import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxFolderId; import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate; 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.MailboxPropertyManager;
import org.briarproject.bramble.api.mailbox.MailboxVersion;
import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
@@ -18,7 +20,9 @@ import org.jmock.Expectations;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; 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.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomId; 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 ClientHelper clientHelper = context.mock(ClientHelper.class);
private final BdfDictionary bdfDict; private final BdfDictionary bdfDict;
private final MailboxPropertiesUpdate mailboxProps; private final BdfList emptyServerSupports;
private final BdfList someClientSupports;
private final List<MailboxVersion> someClientSupportsList;
private final BdfList someServerSupports;
private final MailboxPropertiesUpdateMailbox updateMailbox;
private final MailboxPropertiesUpdate updateNoMailbox;
private final Group group; private final Group group;
private final Message message; private final Message message;
private final MailboxPropertyValidator mpv; 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; // Just dummies, clientHelper is mocked so our test is a bit shallow;
// not testing // not testing
// {@link ClientHelper#parseAndValidateMailboxPropertiesUpdate(BdfDictionary)} // {@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")); 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 MailboxAuthToken(getRandomId()),
new MailboxFolderId(getRandomId()), new MailboxFolderId(getRandomId()),
new MailboxFolderId(getRandomId())); new MailboxFolderId(getRandomId()));
updateNoMailbox = new MailboxPropertiesUpdate(someClientSupportsList);
group = getGroup(MailboxPropertyManager.CLIENT_ID, group = getGroup(MailboxPropertyManager.CLIENT_ID,
MailboxPropertyManager.MAJOR_VERSION); MailboxPropertyManager.MAJOR_VERSION);
@@ -56,12 +75,13 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase {
@Test @Test
public void testValidateMessageBody() throws IOException { public void testValidateMessageBody() throws IOException {
BdfList body = BdfList.of(4, bdfDict); BdfList body =
BdfList.of(4, someClientSupports, someServerSupports, bdfDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
bdfDict); someClientSupports, someServerSupports, bdfDict);
will(returnValue(mailboxProps)); will(returnValue(updateMailbox));
}}); }});
BdfDictionary result = BdfDictionary result =
@@ -82,14 +102,15 @@ public class MailboxPropertyValidatorTest extends BrambleMockTestCase {
} }
@Test @Test
public void testEmptyPropertiesReturnsNull() throws IOException { public void testEmptyProperties() throws IOException {
BdfDictionary emptyBdfDict = new BdfDictionary(); BdfDictionary emptyBdfDict = new BdfDictionary();
BdfList body = BdfList.of(42, emptyBdfDict); BdfList body = BdfList.of(42, someClientSupports, emptyServerSupports,
emptyBdfDict);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate( oneOf(clientHelper).parseAndValidateMailboxPropertiesUpdate(
emptyBdfDict); someClientSupports, emptyServerSupports, emptyBdfDict);
will(returnValue(null)); will(returnValue(updateNoMailbox));
}}); }});
BdfDictionary result = BdfDictionary result =