mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Move handshake keys from LocalAuthor to Account.
This commit is contained in:
@@ -3,6 +3,7 @@ package org.briarproject.bramble.account;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.Account;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
@@ -24,7 +25,7 @@ import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||
@@ -47,7 +48,8 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
private final String encryptedKeyHex = toHexString(encryptedKey);
|
||||
private final byte[] newEncryptedKey = getRandomBytes(123);
|
||||
private final String newEncryptedKeyHex = toHexString(newEncryptedKey);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor();
|
||||
private final Account account = getAccount();
|
||||
private final LocalAuthor localAuthor = account.getLocalAuthor();
|
||||
private final String authorName = localAuthor.getName();
|
||||
private final String password = getRandomString(10);
|
||||
private final String newPassword = getRandomString(10);
|
||||
@@ -251,9 +253,9 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testCreateAccountStoresDbKey() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(identityManager).createLocalAuthor(authorName);
|
||||
will(returnValue(localAuthor));
|
||||
oneOf(identityManager).registerLocalAuthor(localAuthor);
|
||||
oneOf(identityManager).createAccount(authorName);
|
||||
will(returnValue(account));
|
||||
oneOf(identityManager).registerAccount(account);
|
||||
oneOf(crypto).generateSecretKey();
|
||||
will(returnValue(key));
|
||||
oneOf(crypto).encryptWithPassword(key.getBytes(), password);
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.briarproject.bramble.api.db.NoSuchPendingContactException;
|
||||
import org.briarproject.bramble.api.db.NoSuchTransportException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.Account;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.identity.event.LocalAuthorAddedEvent;
|
||||
@@ -74,11 +75,11 @@ import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERE
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.UNKNOWN;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
|
||||
import static org.briarproject.bramble.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContact;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
@@ -106,6 +107,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
private final GroupId groupId;
|
||||
private final Group group;
|
||||
private final Author author;
|
||||
private final Account account;
|
||||
private final LocalAuthor localAuthor;
|
||||
private final String alias;
|
||||
private final Message message, message1;
|
||||
@@ -124,7 +126,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
group = getGroup(clientId, majorVersion);
|
||||
groupId = group.getId();
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
account = getAccount();
|
||||
localAuthor = account.getLocalAuthor();
|
||||
message = getMessage(groupId);
|
||||
message1 = getMessage(groupId);
|
||||
messageId = message.getId();
|
||||
@@ -159,15 +162,15 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
// startTransaction()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// registerLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
// addAccount()
|
||||
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).addLocalAuthor(txn, localAuthor);
|
||||
oneOf(database).addAccount(txn, account);
|
||||
oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class)));
|
||||
// addContact()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
oneOf(database).containsAccount(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
localAuthor.getId());
|
||||
@@ -203,10 +206,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(true));
|
||||
oneOf(database).removeContact(txn, contactId);
|
||||
oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class)));
|
||||
// removeLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
// removeAccount()
|
||||
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).removeLocalAuthor(txn, localAuthor.getId());
|
||||
oneOf(database).removeAccount(txn, localAuthor.getId());
|
||||
oneOf(eventBus).broadcast(with(any(LocalAuthorRemovedEvent.class)));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
@@ -218,7 +221,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
assertFalse(db.open(key, null));
|
||||
db.transaction(false, transaction -> {
|
||||
db.addLocalAuthor(transaction, localAuthor);
|
||||
db.addAccount(transaction, account);
|
||||
assertEquals(contactId, db.addContact(transaction, author,
|
||||
localAuthor.getId(), true));
|
||||
assertEquals(singletonList(contact),
|
||||
@@ -229,7 +232,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
db.getGroups(transaction, clientId, majorVersion));
|
||||
db.removeGroup(transaction, group);
|
||||
db.removeContact(transaction, contactId);
|
||||
db.removeLocalAuthor(transaction, localAuthor.getId());
|
||||
db.removeAccount(transaction, localAuthor.getId());
|
||||
});
|
||||
db.close();
|
||||
}
|
||||
@@ -434,14 +437,13 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVariousMethodsThrowExceptionIfLocalAuthorIsMissing()
|
||||
public void testVariousMethodsThrowExceptionIfAccountIsMissing()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the pseudonym is in the DB (which it's not)
|
||||
// Check whether the account is in the DB (which it's not)
|
||||
exactly(4).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(4).of(database).containsLocalAuthor(txn,
|
||||
localAuthor.getId());
|
||||
exactly(4).of(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
exactly(4).of(database).abortTransaction(txn);
|
||||
}});
|
||||
@@ -459,7 +461,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.getLocalAuthor(transaction, localAuthor.getId()));
|
||||
db.getAccount(transaction, localAuthor.getId()));
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
@@ -467,7 +469,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.removeLocalAuthor(transaction, localAuthor.getId()));
|
||||
db.removeAccount(transaction, localAuthor.getId()));
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
@@ -1416,10 +1418,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
// Contact is a local identity
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
oneOf(database).containsAccount(txn, author.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).abortTransaction(txn);
|
||||
}});
|
||||
@@ -1442,9 +1444,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
oneOf(database).containsAccount(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
// Contact already exists for this local identity
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.identity.Account;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
@@ -35,9 +36,9 @@ import static java.util.logging.Level.OFF;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
@@ -161,11 +162,11 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsLocalAuthor() throws Exception {
|
||||
String name = "containsLocalAuthor(T, AuthorId)";
|
||||
public void testContainsAccount() throws Exception {
|
||||
String name = "containsAccount(T, AuthorId)";
|
||||
benchmark(name, db -> {
|
||||
Connection txn = db.startTransaction();
|
||||
db.containsLocalAuthor(txn, localAuthor.getId());
|
||||
db.containsAccount(txn, localAuthor.getId());
|
||||
db.commitTransaction(txn);
|
||||
});
|
||||
}
|
||||
@@ -295,21 +296,21 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLocalAuthor() throws Exception {
|
||||
String name = "getLocalAuthor(T, AuthorId)";
|
||||
public void testGetAccount() throws Exception {
|
||||
String name = "getAccount(T, AuthorId)";
|
||||
benchmark(name, db -> {
|
||||
Connection txn = db.startTransaction();
|
||||
db.getLocalAuthor(txn, localAuthor.getId());
|
||||
db.getAccount(txn, localAuthor.getId());
|
||||
db.commitTransaction(txn);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLocalAuthors() throws Exception {
|
||||
String name = "getLocalAuthors(T)";
|
||||
public void testGetAccounts() throws Exception {
|
||||
String name = "getAccounts(T)";
|
||||
benchmark(name, db -> {
|
||||
Connection txn = db.startTransaction();
|
||||
db.getLocalAuthors(txn);
|
||||
db.getAccounts(txn);
|
||||
db.commitTransaction(txn);
|
||||
});
|
||||
}
|
||||
@@ -531,7 +532,8 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
void populateDatabase(Database<Connection> db) throws DbException {
|
||||
localAuthor = getLocalAuthor();
|
||||
Account account = getAccount();
|
||||
localAuthor = account.getLocalAuthor();
|
||||
clientIds = new ArrayList<>();
|
||||
contacts = new ArrayList<>();
|
||||
groups = new ArrayList<>();
|
||||
@@ -543,7 +545,7 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
for (int i = 0; i < CLIENTS; i++) clientIds.add(getClientId());
|
||||
|
||||
Connection txn = db.startTransaction();
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
for (int i = 0; i < CONTACTS; i++) {
|
||||
ContactId c = db.addContact(txn, getAuthor(), localAuthor.getId(),
|
||||
random.nextBoolean());
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.identity.Account;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
@@ -71,10 +72,10 @@ import static org.briarproject.bramble.db.DatabaseConstants.DB_SETTINGS_NAMESPAC
|
||||
import static org.briarproject.bramble.db.DatabaseConstants.LAST_COMPACTED_KEY;
|
||||
import static org.briarproject.bramble.db.DatabaseConstants.MAX_COMPACTION_INTERVAL_MS;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getPendingContact;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
@@ -105,6 +106,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
private final int majorVersion;
|
||||
private final Group group;
|
||||
private final Author author;
|
||||
private final Account account;
|
||||
private final LocalAuthor localAuthor;
|
||||
private final Message message;
|
||||
private final MessageId messageId;
|
||||
@@ -121,7 +123,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
group = getGroup(clientId, majorVersion);
|
||||
groupId = group.getId();
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
account = getAccount();
|
||||
localAuthor = account.getLocalAuthor();
|
||||
message = getMessage(groupId);
|
||||
messageId = message.getId();
|
||||
transportId = getTransportId();
|
||||
@@ -147,7 +150,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
assertFalse(db.containsContact(txn, contactId));
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
assertTrue(db.containsContact(txn, contactId));
|
||||
@@ -210,7 +213,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -241,7 +244,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared but unvalidated message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -286,7 +289,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, an invisible group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -337,7 +340,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and an unshared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -368,7 +371,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -395,7 +398,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact and a visible group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -436,7 +439,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -568,7 +571,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact and a shared group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -588,7 +591,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
|
||||
@@ -606,7 +609,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, an invisible group and a message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -625,7 +628,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact and a group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -677,7 +680,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||
|
||||
// Add the contact, the transport and the transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -778,7 +781,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||
|
||||
// Add the contact, the transport and the handshake keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -931,7 +934,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the contact, transport and transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -975,7 +978,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the contact, transport and handshake keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -1022,7 +1025,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the contact, transport and transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -1069,7 +1072,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the contact, transport and handshake keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
@@ -1111,8 +1114,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a local author - no contacts should be associated
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
// Add an account for a local author - no contacts should be associated
|
||||
db.addAccount(txn, account);
|
||||
|
||||
// Add a contact associated with the local author
|
||||
assertEquals(contactId,
|
||||
@@ -1138,8 +1141,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a local author - no contacts should be associated
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
// Add an account for a local author - no contacts should be associated
|
||||
db.addAccount(txn, account);
|
||||
Collection<ContactId> contacts =
|
||||
db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(emptyList(), contacts);
|
||||
@@ -1150,8 +1153,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
contacts = db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(singletonList(contactId), contacts);
|
||||
|
||||
// Remove the local author - the contact should be removed
|
||||
db.removeLocalAuthor(txn, localAuthor.getId());
|
||||
// Remove the account - the contact should be removed
|
||||
db.removeAccount(txn, localAuthor.getId());
|
||||
contacts = db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(emptyList(), contacts);
|
||||
assertFalse(db.containsContact(txn, contactId));
|
||||
@@ -1166,7 +1169,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact - initially there should be no offered messages
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
assertEquals(0, db.countOfferedMessages(txn, contactId));
|
||||
@@ -1750,7 +1753,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -1852,14 +1855,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testDifferentLocalAuthorsCanHaveTheSameContact()
|
||||
throws Exception {
|
||||
LocalAuthor localAuthor1 = getLocalAuthor();
|
||||
Account account1 = getAccount();
|
||||
LocalAuthor localAuthor1 = account1.getLocalAuthor();
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add two local authors
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addLocalAuthor(txn, localAuthor1);
|
||||
// Add accounts for two local authors
|
||||
db.addAccount(txn, account);
|
||||
db.addAccount(txn, account1);
|
||||
|
||||
// Add the same contact for each local author
|
||||
ContactId contactId =
|
||||
@@ -1883,7 +1887,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -1937,7 +1941,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
|
||||
@@ -1994,7 +1998,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a group and a message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -2078,7 +2082,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -2123,7 +2127,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addAccount(txn, account);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
db.addGroup(txn, group);
|
||||
@@ -2241,20 +2245,21 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testSetHandshakeKeyPair() throws Exception {
|
||||
assertNull(localAuthor.getHandshakePublicKey());
|
||||
assertNull(localAuthor.getHandshakePrivateKey());
|
||||
Account withoutKeys =
|
||||
new Account(localAuthor, null, null, account.getTimeCreated());
|
||||
assertFalse(withoutKeys.hasHandshakeKeyPair());
|
||||
byte[] publicKey = getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
||||
byte[] privateKey = getRandomBytes(123);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
LocalAuthor retrieved = db.getLocalAuthor(txn, localAuthor.getId());
|
||||
assertNull(retrieved.getHandshakePublicKey());
|
||||
assertNull(retrieved.getHandshakePrivateKey());
|
||||
db.addAccount(txn, withoutKeys);
|
||||
Account retrieved = db.getAccount(txn, localAuthor.getId());
|
||||
assertFalse(retrieved.hasHandshakeKeyPair());
|
||||
db.setHandshakeKeyPair(txn, localAuthor.getId(), publicKey, privateKey);
|
||||
retrieved = db.getLocalAuthor(txn, localAuthor.getId());
|
||||
retrieved = db.getAccount(txn, localAuthor.getId());
|
||||
assertTrue(retrieved.hasHandshakeKeyPair());
|
||||
assertArrayEquals(publicKey, retrieved.getHandshakePublicKey());
|
||||
assertArrayEquals(privateKey, retrieved.getHandshakePrivateKey());
|
||||
|
||||
|
||||
@@ -7,8 +7,10 @@ import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Account;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
import org.jmock.Expectations;
|
||||
@@ -16,8 +18,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
@@ -26,57 +27,58 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final AuthorFactory authorFactory =
|
||||
context.mock(AuthorFactory.class);
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final PublicKey handshakePublicKey = context.mock(PublicKey.class);
|
||||
private final PrivateKey handshakePrivateKey =
|
||||
context.mock(PrivateKey.class);
|
||||
|
||||
private final Transaction txn = new Transaction(null, false);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor(true);
|
||||
private final LocalAuthor localAuthorWithoutHandshakeKeys =
|
||||
new LocalAuthor(localAuthor.getId(), localAuthor.getFormatVersion(),
|
||||
localAuthor.getName(), localAuthor.getPublicKey(),
|
||||
localAuthor.getPrivateKey(), localAuthor.getTimeCreated());
|
||||
private final Account accountWithKeys = getAccount();
|
||||
private final LocalAuthor localAuthor = accountWithKeys.getLocalAuthor();
|
||||
private final Account accountWithoutKeys = new Account(localAuthor,
|
||||
null, null, accountWithKeys.getTimeCreated());
|
||||
private final KeyPair handshakeKeyPair =
|
||||
new KeyPair(handshakePublicKey, handshakePrivateKey);
|
||||
private final byte[] handshakePublicKeyBytes =
|
||||
localAuthor.getHandshakePublicKey();
|
||||
accountWithKeys.getHandshakePublicKey();
|
||||
private final byte[] handshakePrivateKeyBytes =
|
||||
localAuthor.getHandshakePrivateKey();
|
||||
accountWithKeys.getHandshakePrivateKey();
|
||||
|
||||
private IdentityManagerImpl identityManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
identityManager = new IdentityManagerImpl(db, crypto, authorFactory);
|
||||
identityManager =
|
||||
new IdentityManagerImpl(db, crypto, authorFactory, clock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenDatabaseHookLocalAuthorRegistered() throws Exception {
|
||||
public void testOpenDatabaseHookAccountRegistered() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).addLocalAuthor(txn, localAuthor);
|
||||
oneOf(db).addAccount(txn, accountWithKeys);
|
||||
}});
|
||||
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
identityManager.registerAccount(accountWithKeys);
|
||||
identityManager.onDatabaseOpened(txn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenDatabaseHookNoLocalAuthorRegisteredHandshakeKeys()
|
||||
public void testOpenDatabaseHookNoAccountRegisteredHandshakeKeys()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).getLocalAuthors(txn);
|
||||
will(returnValue(singletonList(localAuthor)));
|
||||
oneOf(db).getAccounts(txn);
|
||||
will(returnValue(singletonList(accountWithKeys)));
|
||||
}});
|
||||
|
||||
identityManager.onDatabaseOpened(txn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenDatabaseHookNoLocalAuthorRegisteredNoHandshakeKeys()
|
||||
public void testOpenDatabaseHookNoAccountRegisteredNoHandshakeKeys()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).getLocalAuthors(txn);
|
||||
will(returnValue(singletonList(localAuthorWithoutHandshakeKeys)));
|
||||
oneOf(db).getAccounts(txn);
|
||||
will(returnValue(singletonList(accountWithoutKeys)));
|
||||
oneOf(crypto).generateAgreementKeyPair();
|
||||
will(returnValue(handshakeKeyPair));
|
||||
oneOf(handshakePublicKey).getEncoded();
|
||||
@@ -88,27 +90,21 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
}});
|
||||
|
||||
identityManager.onDatabaseOpened(txn);
|
||||
|
||||
LocalAuthor cached = identityManager.getLocalAuthor();
|
||||
assertArrayEquals(handshakePublicKeyBytes,
|
||||
cached.getHandshakePublicKey());
|
||||
assertArrayEquals(handshakePrivateKeyBytes,
|
||||
cached.getHandshakePrivateKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLocalAuthor() throws Exception {
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getLocalAuthors(txn);
|
||||
will(returnValue(singletonList(localAuthor)));
|
||||
oneOf(db).getAccounts(txn);
|
||||
will(returnValue(singletonList(accountWithKeys)));
|
||||
}});
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCachedLocalAuthor() throws DbException {
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
identityManager.registerAccount(accountWithKeys);
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user