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

@@ -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
);
}
}

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.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());
}});

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.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<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;
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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> 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(

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.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<MailboxVersion> 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 =