Pass mailbox properties to hook when pairing.

This commit is contained in:
akwizgran
2022-07-19 18:02:35 +01:00
parent d20457f338
commit 04ed3a652a
5 changed files with 43 additions and 14 deletions

View File

@@ -49,10 +49,8 @@ public interface MailboxSettingsManager {
* Called when Briar is paired with a mailbox
*
* @param txn A read-write transaction
* @param ownOnion Our new mailbox's onion (56 base32 chars)
*/
void mailboxPaired(Transaction txn, String ownOnion,
List<MailboxVersion> serverSupports)
void mailboxPaired(Transaction txn, MailboxProperties p)
throws DbException;
/**

View File

@@ -80,7 +80,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
encodeServerSupports(serverSupports, s);
settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE);
for (MailboxHook hook : hooks) {
hook.mailboxPaired(txn, p.getOnion(), p.getServerSupports());
hook.mailboxPaired(txn, p);
}
}
@@ -89,6 +89,10 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
Settings s = new Settings();
s.put(SETTINGS_KEY_ONION, "");
s.put(SETTINGS_KEY_TOKEN, "");
s.put(SETTINGS_KEY_ATTEMPTS, "");
s.put(SETTINGS_KEY_LAST_ATTEMPT, "");
s.put(SETTINGS_KEY_LAST_SUCCESS, "");
s.put(SETTINGS_KEY_SERVER_SUPPORTS, "");
settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE);
for (MailboxHook hook : hooks) {
hook.mailboxUnpaired(txn);

View File

@@ -159,10 +159,11 @@ class MailboxUpdateManagerImpl implements MailboxUpdateManager,
}
@Override
public void mailboxPaired(Transaction txn, String ownOnion,
List<MailboxVersion> serverSupports) throws DbException {
public void mailboxPaired(Transaction txn, MailboxProperties p)
throws DbException {
for (Contact c : db.getContacts(txn)) {
createAndSendUpdateWithMailbox(txn, c, serverSupports, ownOnion);
createAndSendUpdateWithMailbox(txn, c, p.getServerSupports(),
p.getOnion());
}
}

View File

@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager.MailboxHook;
import org.briarproject.bramble.api.mailbox.MailboxStatus;
import org.briarproject.bramble.api.mailbox.MailboxVersion;
import org.briarproject.bramble.api.mailbox.event.OwnMailboxConnectionStatusEvent;
@@ -39,6 +40,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
private final SettingsManager settingsManager =
context.mock(SettingsManager.class);
private final MailboxHook hook = context.mock(MailboxHook.class);
private final MailboxSettingsManager manager =
new MailboxSettingsManagerImpl(settingsManager);
@@ -47,6 +49,8 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
private final MailboxAuthToken token = new MailboxAuthToken(getRandomId());
private final List<MailboxVersion> serverSupports =
asList(new MailboxVersion(1, 0), new MailboxVersion(1, 1));
private final MailboxProperties properties = new MailboxProperties(onion,
token, serverSupports);
private final int[] serverSupportsInts = {1, 0, 1, 1};
private final ContactId contactId1 = new ContactId(random.nextInt());
private final ContactId contactId2 = new ContactId(random.nextInt());
@@ -98,17 +102,40 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
expectedSettings.put(SETTINGS_KEY_TOKEN, token.toString());
expectedSettings.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS,
serverSupportsInts);
MailboxProperties properties = new MailboxProperties(onion, token,
serverSupports);
manager.registerMailboxHook(hook);
context.checking(new Expectations() {{
oneOf(settingsManager).mergeSettings(txn, expectedSettings,
SETTINGS_NAMESPACE);
oneOf(hook).mailboxPaired(txn, properties);
}});
manager.setOwnMailboxProperties(txn, properties);
}
@Test
public void testRemovesProperties() throws Exception {
Transaction txn = new Transaction(null, false);
Settings expectedSettings = new Settings();
expectedSettings.put(SETTINGS_KEY_ONION, "");
expectedSettings.put(SETTINGS_KEY_TOKEN, "");
expectedSettings.put(SETTINGS_KEY_ATTEMPTS, "");
expectedSettings.put(SETTINGS_KEY_LAST_ATTEMPT, "");
expectedSettings.put(SETTINGS_KEY_LAST_SUCCESS, "");
expectedSettings.put(SETTINGS_KEY_SERVER_SUPPORTS, "");
manager.registerMailboxHook(hook);
context.checking(new Expectations() {{
oneOf(settingsManager).mergeSettings(txn, expectedSettings,
SETTINGS_NAMESPACE);
oneOf(hook).mailboxUnpaired(txn);
}});
manager.removeOwnMailboxProperties(txn);
}
@Test
public void testReturnsDefaultStatusIfSettingsAreEmpty() throws Exception {
Transaction txn = new Transaction(null, true);
@@ -182,7 +209,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
}});
manager.recordSuccessfulConnection(txn, now, versions);
hasEvent(txn, OwnMailboxConnectionStatusEvent.class);
assertTrue(hasEvent(txn, OwnMailboxConnectionStatusEvent.class));
}
@Test

View File

@@ -78,7 +78,6 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
private final BdfList someClientSupports;
private final List<MailboxVersion> newerClientSupportsList;
private final BdfList newerClientSupports;
private final List<MailboxVersion> someServerSupportsList;
private final BdfList someServerSupports;
private final BdfList emptyServerSupports = new BdfList();
private final MailboxProperties updateProps;
@@ -100,8 +99,8 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
newerClientSupportsList.get(0).getMajor(),
newerClientSupportsList.get(0).getMinor()));
someServerSupportsList = singletonList(new MailboxVersion(
rnd.nextInt(), rnd.nextInt()));
List<MailboxVersion> someServerSupportsList =
singletonList(new MailboxVersion(rnd.nextInt(), rnd.nextInt()));
someServerSupports = BdfList.of(BdfList.of(
someServerSupportsList.get(0).getMajor(),
someServerSupportsList.get(0).getMinor()));
@@ -679,7 +678,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
}});
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
t.mailboxPaired(txn, ownProps.getOnion(), someServerSupportsList);
t.mailboxPaired(txn, ownProps);
}
@Test