mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Move handshake keys from LocalAuthor to Account.
This commit is contained in:
@@ -5,9 +5,9 @@ import org.briarproject.bramble.api.contact.ContactId;
|
|||||||
import org.briarproject.bramble.api.contact.PendingContact;
|
import org.briarproject.bramble.api.contact.PendingContact;
|
||||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
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.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
@@ -101,6 +101,11 @@ public interface DatabaseComponent {
|
|||||||
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
|
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
|
||||||
NullableDbCallable<R, E> task) throws DbException, E;
|
NullableDbCallable<R, E> task) throws DbException, E;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores an account.
|
||||||
|
*/
|
||||||
|
void addAccount(Transaction txn, Account a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a contact associated with the given local and remote pseudonyms,
|
* Stores a contact associated with the given local and remote pseudonyms,
|
||||||
* and returns an ID for the contact.
|
* and returns an ID for the contact.
|
||||||
@@ -127,11 +132,6 @@ public interface DatabaseComponent {
|
|||||||
HandshakeKeySetId addHandshakeKeys(Transaction txn, PendingContactId p,
|
HandshakeKeySetId addHandshakeKeys(Transaction txn, PendingContactId p,
|
||||||
HandshakeKeys k) throws DbException;
|
HandshakeKeys k) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores a local pseudonym.
|
|
||||||
*/
|
|
||||||
void addLocalAuthor(Transaction txn, LocalAuthor a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a local message.
|
* Stores a local message.
|
||||||
*/
|
*/
|
||||||
@@ -157,6 +157,13 @@ public interface DatabaseComponent {
|
|||||||
TransportKeySetId addTransportKeys(Transaction txn, ContactId c,
|
TransportKeySetId addTransportKeys(Transaction txn, ContactId c,
|
||||||
TransportKeys k) throws DbException;
|
TransportKeys k) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the database contains an account for the given pseudonym.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
boolean containsAccount(Transaction txn, AuthorId local) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the database contains the given contact for the given
|
* Returns true if the database contains the given contact for the given
|
||||||
* local pseudonym.
|
* local pseudonym.
|
||||||
@@ -173,14 +180,6 @@ public interface DatabaseComponent {
|
|||||||
*/
|
*/
|
||||||
boolean containsGroup(Transaction txn, GroupId g) throws DbException;
|
boolean containsGroup(Transaction txn, GroupId g) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the database contains the given local author.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
boolean containsLocalAuthor(Transaction txn, AuthorId local)
|
|
||||||
throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the database contains the given pending contact.
|
* Returns true if the database contains the given pending contact.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -247,6 +246,20 @@ public interface DatabaseComponent {
|
|||||||
Collection<Message> generateRequestedBatch(Transaction txn, ContactId c,
|
Collection<Message> generateRequestedBatch(Transaction txn, ContactId c,
|
||||||
int maxLength, int maxLatency) throws DbException;
|
int maxLength, int maxLatency) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the account for the local pseudonym with the given ID.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Account getAccount(Transaction txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the accounts for all local pseudonyms.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Collection<Account> getAccounts(Transaction txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the contact with the given ID.
|
* Returns the contact with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -316,20 +329,6 @@ public interface DatabaseComponent {
|
|||||||
Collection<HandshakeKeySet> getHandshakeKeys(Transaction txn, TransportId t)
|
Collection<HandshakeKeySet> getHandshakeKeys(Transaction txn, TransportId t)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the local pseudonym with the given ID.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
LocalAuthor getLocalAuthor(Transaction txn, AuthorId a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all local pseudonyms.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
Collection<LocalAuthor> getLocalAuthors(Transaction txn) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the message with the given ID.
|
* Returns the message with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -542,6 +541,11 @@ public interface DatabaseComponent {
|
|||||||
void receiveRequest(Transaction txn, ContactId c, Request r)
|
void receiveRequest(Transaction txn, ContactId c, Request r)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an account (and all associated state) from the database.
|
||||||
|
*/
|
||||||
|
void removeAccount(Transaction txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a contact (and all associated state) from the database.
|
* Removes a contact (and all associated state) from the database.
|
||||||
*/
|
*/
|
||||||
@@ -558,11 +562,6 @@ public interface DatabaseComponent {
|
|||||||
void removeHandshakeKeys(Transaction txn, TransportId t,
|
void removeHandshakeKeys(Transaction txn, TransportId t,
|
||||||
HandshakeKeySetId k) throws DbException;
|
HandshakeKeySetId k) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes a local pseudonym (and all associated state) from the database.
|
|
||||||
*/
|
|
||||||
void removeLocalAuthor(Transaction txn, AuthorId a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a message (and all associated state) from the database.
|
* Removes a message (and all associated state) from the database.
|
||||||
*/
|
*/
|
||||||
@@ -620,7 +619,7 @@ public interface DatabaseComponent {
|
|||||||
Collection<MessageId> dependencies) throws DbException;
|
Collection<MessageId> dependencies) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the handshake key pair for the local pseudonym with the given ID.
|
* Sets the handshake key pair for the account with the given ID.
|
||||||
*/
|
*/
|
||||||
void setHandshakeKeyPair(Transaction txn, AuthorId local, byte[] publicKey,
|
void setHandshakeKeyPair(Transaction txn, AuthorId local, byte[] publicKey,
|
||||||
byte[] privateKey) throws DbException;
|
byte[] privateKey) throws DbException;
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package org.briarproject.bramble.api.identity;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
import static org.briarproject.bramble.api.crypto.CryptoConstants.MAX_AGREEMENT_PUBLIC_KEY_BYTES;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private final LocalAuthor localAuthor;
|
||||||
|
@Nullable
|
||||||
|
private final byte[] handshakePublicKey, handshakePrivateKey;
|
||||||
|
private final long created;
|
||||||
|
|
||||||
|
public Account(LocalAuthor localAuthor,
|
||||||
|
@Nullable byte[] handshakePublicKey,
|
||||||
|
@Nullable byte[] handshakePrivateKey, long created) {
|
||||||
|
if (handshakePublicKey != null) {
|
||||||
|
int keyLength = handshakePublicKey.length;
|
||||||
|
if (keyLength == 0 || keyLength > MAX_AGREEMENT_PUBLIC_KEY_BYTES)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
this.localAuthor = localAuthor;
|
||||||
|
this.handshakePublicKey = handshakePublicKey;
|
||||||
|
this.handshakePrivateKey = handshakePrivateKey;
|
||||||
|
this.created = created;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ID of the user's pseudonym.
|
||||||
|
*/
|
||||||
|
public AuthorId getId() {
|
||||||
|
return localAuthor.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the user's pseudonym.
|
||||||
|
*/
|
||||||
|
public LocalAuthor getLocalAuthor() {
|
||||||
|
return localAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the account has a handshake key pair.
|
||||||
|
*/
|
||||||
|
public boolean hasHandshakeKeyPair() {
|
||||||
|
return handshakePublicKey != null && handshakePrivateKey != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the public key used for handshaking, or null if no key exists.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public byte[] getHandshakePublicKey() {
|
||||||
|
return handshakePublicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the private key used for handshaking, or null if no key exists.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public byte[] getHandshakePrivateKey() {
|
||||||
|
return handshakePrivateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the time the account was created, in milliseconds since the
|
||||||
|
* Unix epoch.
|
||||||
|
*/
|
||||||
|
public long getTimeCreated() {
|
||||||
|
return created;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return localAuthor.getId().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o instanceof Account) {
|
||||||
|
Account a = (Account) o;
|
||||||
|
return created == a.created &&
|
||||||
|
localAuthor.equals(a.localAuthor) &&
|
||||||
|
Arrays.equals(handshakePublicKey, a.handshakePublicKey) &&
|
||||||
|
Arrays.equals(handshakePrivateKey, a.handshakePrivateKey);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,9 +19,6 @@ public interface AuthorFactory {
|
|||||||
/**
|
/**
|
||||||
* Creates a local author with the current format version and the given
|
* Creates a local author with the current format version and the given
|
||||||
* name.
|
* name.
|
||||||
*
|
|
||||||
* @param handshakeKeys true if the local author should include handshake
|
|
||||||
* keys.
|
|
||||||
*/
|
*/
|
||||||
LocalAuthor createLocalAuthor(String name, boolean handshakeKeys);
|
LocalAuthor createLocalAuthor(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,12 +17,19 @@ public interface IdentityManager {
|
|||||||
LocalAuthor createLocalAuthor(String name);
|
LocalAuthor createLocalAuthor(String name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the given local identity with the manager. This method should
|
* Creates an account with the given name. The account includes a handshake
|
||||||
* be called before {@link LifecycleManager#startServices(SecretKey)}. The
|
* key pair.
|
||||||
* identity is stored when {@link LifecycleManager#startServices(SecretKey)}
|
|
||||||
* is called.
|
|
||||||
*/
|
*/
|
||||||
void registerLocalAuthor(LocalAuthor a);
|
@CryptoExecutor
|
||||||
|
Account createAccount(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the given account with the manager. This method should be
|
||||||
|
* called before {@link LifecycleManager#startServices(SecretKey)}. The
|
||||||
|
* account is stored when {@link LifecycleManager#startServices(SecretKey)}
|
||||||
|
* is called. The account must include a handshake key pair.
|
||||||
|
*/
|
||||||
|
void registerAccount(Account a);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cached local identity or loads it from the database.
|
* Returns the cached local identity or loads it from the database.
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ package org.briarproject.bramble.api.identity;
|
|||||||
|
|
||||||
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;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pseudonym for the local user.
|
* A pseudonym for the local user.
|
||||||
*/
|
*/
|
||||||
@@ -15,31 +12,11 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_K
|
|||||||
public class LocalAuthor extends Author {
|
public class LocalAuthor extends Author {
|
||||||
|
|
||||||
private final byte[] privateKey;
|
private final byte[] privateKey;
|
||||||
@Nullable
|
|
||||||
private final byte[] handshakePublicKey, handshakePrivateKey;
|
|
||||||
private final long created;
|
|
||||||
|
|
||||||
public LocalAuthor(AuthorId id, int formatVersion, String name,
|
public LocalAuthor(AuthorId id, int formatVersion, String name,
|
||||||
byte[] publicKey, byte[] privateKey, long created) {
|
byte[] publicKey, byte[] privateKey) {
|
||||||
super(id, formatVersion, name, publicKey);
|
super(id, formatVersion, name, publicKey);
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey;
|
||||||
this.created = created;
|
|
||||||
handshakePublicKey = null;
|
|
||||||
handshakePrivateKey = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LocalAuthor(AuthorId id, int formatVersion, String name,
|
|
||||||
byte[] publicKey, byte[] privateKey, byte[] handshakePublicKey,
|
|
||||||
byte[] handshakePrivateKey, long created) {
|
|
||||||
super(id, formatVersion, name, publicKey);
|
|
||||||
if (handshakePublicKey.length == 0 ||
|
|
||||||
handshakePublicKey.length > MAX_PUBLIC_KEY_LENGTH) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.privateKey = privateKey;
|
|
||||||
this.handshakePublicKey = handshakePublicKey;
|
|
||||||
this.handshakePrivateKey = handshakePrivateKey;
|
|
||||||
this.created = created;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,28 +25,4 @@ public class LocalAuthor extends Author {
|
|||||||
public byte[] getPrivateKey() {
|
public byte[] getPrivateKey() {
|
||||||
return privateKey;
|
return privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the public key used for handshaking, or null if no key exists.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public byte[] getHandshakePublicKey() {
|
|
||||||
return handshakePublicKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the private key used for handshaking, or null if no key exists.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public byte[] getHandshakePrivateKey() {
|
|
||||||
return handshakePrivateKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the time the pseudonym was created, in milliseconds since the
|
|
||||||
* Unix epoch.
|
|
||||||
*/
|
|
||||||
public long getTimeCreated() {
|
|
||||||
return created;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.ContactId;
|
|||||||
import org.briarproject.bramble.api.contact.PendingContact;
|
import org.briarproject.bramble.api.contact.PendingContact;
|
||||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
@@ -100,28 +101,20 @@ public class TestUtils {
|
|||||||
return new SecretKey(getRandomBytes(SecretKey.LENGTH));
|
return new SecretKey(getRandomBytes(SecretKey.LENGTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LocalAuthor getLocalAuthor() {
|
public static Account getAccount() {
|
||||||
return getLocalAuthor(false);
|
LocalAuthor localAuthor = getLocalAuthor();
|
||||||
|
byte[] handshakePub = getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
||||||
|
byte[] handshakePriv = getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
||||||
|
return new Account(localAuthor, handshakePub, handshakePriv, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LocalAuthor getLocalAuthor(boolean withHandshakeKeys) {
|
public static LocalAuthor getLocalAuthor() {
|
||||||
AuthorId id = new AuthorId(getRandomId());
|
AuthorId id = new AuthorId(getRandomId());
|
||||||
int nameLength = 1 + random.nextInt(MAX_AUTHOR_NAME_LENGTH);
|
int nameLength = 1 + random.nextInt(MAX_AUTHOR_NAME_LENGTH);
|
||||||
String name = getRandomString(nameLength);
|
String name = getRandomString(nameLength);
|
||||||
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||||
byte[] privateKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
byte[] privateKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||||
if (withHandshakeKeys) {
|
return new LocalAuthor(id, FORMAT_VERSION, name, publicKey, privateKey);
|
||||||
byte[] handshakePublicKey =
|
|
||||||
getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
|
||||||
byte[] handshakePrivateKey =
|
|
||||||
getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
|
||||||
return new LocalAuthor(id, FORMAT_VERSION, name, publicKey,
|
|
||||||
privateKey, handshakePublicKey, handshakePrivateKey,
|
|
||||||
timestamp);
|
|
||||||
} else {
|
|
||||||
return new LocalAuthor(id, FORMAT_VERSION, name, publicKey,
|
|
||||||
privateKey, timestamp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Author getAuthor() {
|
public static Author getAuthor() {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import org.briarproject.bramble.api.account.AccountManager;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
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.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.util.IoUtils;
|
import org.briarproject.bramble.util.IoUtils;
|
||||||
@@ -161,8 +161,8 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
synchronized (stateChangeLock) {
|
synchronized (stateChangeLock) {
|
||||||
if (hasDatabaseKey())
|
if (hasDatabaseKey())
|
||||||
throw new AssertionError("Already have a database key");
|
throw new AssertionError("Already have a database key");
|
||||||
LocalAuthor localAuthor = identityManager.createLocalAuthor(name);
|
Account account = identityManager.createAccount(name);
|
||||||
identityManager.registerLocalAuthor(localAuthor);
|
identityManager.registerAccount(account);
|
||||||
SecretKey key = crypto.generateSecretKey();
|
SecretKey key = crypto.generateSecretKey();
|
||||||
if (!encryptAndStoreDatabaseKey(key, password)) return false;
|
if (!encryptAndStoreDatabaseKey(key, password)) return false;
|
||||||
databaseKey = key;
|
databaseKey = key;
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ import org.briarproject.bramble.api.db.DbException;
|
|||||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
import org.briarproject.bramble.api.db.Metadata;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
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.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
@@ -86,6 +86,11 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void commitTransaction(T txn) throws DbException;
|
void commitTransaction(T txn) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores an account.
|
||||||
|
*/
|
||||||
|
void addAccount(T txn, Account a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a contact associated with the given local and remote pseudonyms,
|
* Stores a contact associated with the given local and remote pseudonyms,
|
||||||
* and returns an ID for the contact.
|
* and returns an ID for the contact.
|
||||||
@@ -119,11 +124,6 @@ interface Database<T> {
|
|||||||
HandshakeKeySetId addHandshakeKeys(T txn, PendingContactId p,
|
HandshakeKeySetId addHandshakeKeys(T txn, PendingContactId p,
|
||||||
HandshakeKeys k) throws DbException;
|
HandshakeKeys k) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores a local pseudonym.
|
|
||||||
*/
|
|
||||||
void addLocalAuthor(T txn, LocalAuthor a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a message.
|
* Stores a message.
|
||||||
*
|
*
|
||||||
@@ -163,6 +163,13 @@ interface Database<T> {
|
|||||||
TransportKeySetId addTransportKeys(T txn, ContactId c, TransportKeys k)
|
TransportKeySetId addTransportKeys(T txn, ContactId c, TransportKeys k)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the database contains an account for the given pseudonym.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
boolean containsAccount(T txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the database contains the given contact for the given
|
* Returns true if the database contains the given contact for the given
|
||||||
* local pseudonym.
|
* local pseudonym.
|
||||||
@@ -186,13 +193,6 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
boolean containsGroup(T txn, GroupId g) throws DbException;
|
boolean containsGroup(T txn, GroupId g) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the database contains the given local pseudonym.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
boolean containsLocalAuthor(T txn, AuthorId a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the database contains the given message.
|
* Returns true if the database contains the given message.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -245,6 +245,20 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void deleteMessageMetadata(T txn, MessageId m) throws DbException;
|
void deleteMessageMetadata(T txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the account for local pseudonym with the given ID.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Account getAccount(T txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the accounts for all local pseudonyms.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Collection<Account> getAccounts(T txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the contact with the given ID.
|
* Returns the contact with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -322,20 +336,6 @@ interface Database<T> {
|
|||||||
Collection<HandshakeKeySet> getHandshakeKeys(T txn, TransportId t)
|
Collection<HandshakeKeySet> getHandshakeKeys(T txn, TransportId t)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the local pseudonym with the given ID.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
LocalAuthor getLocalAuthor(T txn, AuthorId a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all local pseudonyms.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
Collection<LocalAuthor> getLocalAuthors(T txn) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the message with the given ID.
|
* Returns the message with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -605,6 +605,11 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void raiseSeenFlag(T txn, ContactId c, MessageId m) throws DbException;
|
void raiseSeenFlag(T txn, ContactId c, MessageId m) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an account (and all associated state) from the database.
|
||||||
|
*/
|
||||||
|
void removeAccount(T txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a contact from the database.
|
* Removes a contact from the database.
|
||||||
*/
|
*/
|
||||||
@@ -628,11 +633,6 @@ interface Database<T> {
|
|||||||
void removeHandshakeKeys(T txn, TransportId t, HandshakeKeySetId k)
|
void removeHandshakeKeys(T txn, TransportId t, HandshakeKeySetId k)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes a local pseudonym (and all associated state) from the database.
|
|
||||||
*/
|
|
||||||
void removeLocalAuthor(T txn, AuthorId a) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a message (and all associated state) from the database.
|
* Removes a message (and all associated state) from the database.
|
||||||
*/
|
*/
|
||||||
@@ -686,7 +686,7 @@ interface Database<T> {
|
|||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the handshake key pair for the local pseudonym with the given ID.
|
* Sets the handshake key pair for the account with the given ID.
|
||||||
*/
|
*/
|
||||||
void setHandshakeKeyPair(T txn, AuthorId local, byte[] publicKey,
|
void setHandshakeKeyPair(T txn, AuthorId local, byte[] publicKey,
|
||||||
byte[] privateKey) throws DbException;
|
byte[] privateKey) throws DbException;
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ import org.briarproject.bramble.api.db.TaskAction;
|
|||||||
import org.briarproject.bramble.api.db.Transaction;
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
import org.briarproject.bramble.api.event.EventExecutor;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
import org.briarproject.bramble.api.identity.event.LocalAuthorAddedEvent;
|
import org.briarproject.bramble.api.identity.event.LocalAuthorAddedEvent;
|
||||||
import org.briarproject.bramble.api.identity.event.LocalAuthorRemovedEvent;
|
import org.briarproject.bramble.api.identity.event.LocalAuthorRemovedEvent;
|
||||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||||
@@ -231,15 +231,26 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return txnClass.cast(transaction.unbox());
|
return txnClass.cast(transaction.unbox());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addAccount(Transaction transaction, Account a)
|
||||||
|
throws DbException {
|
||||||
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsAccount(txn, a.getId())) {
|
||||||
|
db.addAccount(txn, a);
|
||||||
|
transaction.attach(new LocalAuthorAddedEvent(a.getId()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ContactId addContact(Transaction transaction, Author remote,
|
public ContactId addContact(Transaction transaction, Author remote,
|
||||||
AuthorId local, boolean verified)
|
AuthorId local, boolean verified)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsLocalAuthor(txn, local))
|
if (!db.containsAccount(txn, local))
|
||||||
throw new NoSuchLocalAuthorException();
|
throw new NoSuchLocalAuthorException();
|
||||||
if (db.containsLocalAuthor(txn, remote.getId()))
|
if (db.containsAccount(txn, remote.getId()))
|
||||||
throw new ContactExistsException();
|
throw new ContactExistsException();
|
||||||
if (db.containsContact(txn, remote.getId(), local))
|
if (db.containsContact(txn, remote.getId(), local))
|
||||||
throw new ContactExistsException();
|
throw new ContactExistsException();
|
||||||
@@ -282,17 +293,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.addHandshakeKeys(txn, p, k);
|
return db.addHandshakeKeys(txn, p, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addLocalAuthor(Transaction transaction, LocalAuthor a)
|
|
||||||
throws DbException {
|
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
if (!db.containsLocalAuthor(txn, a.getId())) {
|
|
||||||
db.addLocalAuthor(txn, a);
|
|
||||||
transaction.attach(new LocalAuthorAddedEvent(a.getId()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addLocalMessage(Transaction transaction, Message m,
|
public void addLocalMessage(Transaction transaction, Message m,
|
||||||
Metadata meta, boolean shared) throws DbException {
|
Metadata meta, boolean shared) throws DbException {
|
||||||
@@ -341,11 +341,18 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.addTransportKeys(txn, c, k);
|
return db.addTransportKeys(txn, c, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAccount(Transaction transaction, AuthorId local)
|
||||||
|
throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
return db.containsAccount(txn, local);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsContact(Transaction transaction, AuthorId remote,
|
public boolean containsContact(Transaction transaction, AuthorId remote,
|
||||||
AuthorId local) throws DbException {
|
AuthorId local) throws DbException {
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsLocalAuthor(txn, local))
|
if (!db.containsAccount(txn, local))
|
||||||
throw new NoSuchLocalAuthorException();
|
throw new NoSuchLocalAuthorException();
|
||||||
return db.containsContact(txn, remote, local);
|
return db.containsContact(txn, remote, local);
|
||||||
}
|
}
|
||||||
@@ -357,13 +364,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.containsGroup(txn, g);
|
return db.containsGroup(txn, g);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsLocalAuthor(Transaction transaction, AuthorId local)
|
|
||||||
throws DbException {
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
return db.containsLocalAuthor(txn, local);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsPendingContact(Transaction transaction,
|
public boolean containsPendingContact(Transaction transaction,
|
||||||
PendingContactId p) throws DbException {
|
PendingContactId p) throws DbException {
|
||||||
@@ -478,6 +478,22 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Account getAccount(Transaction transaction, AuthorId a)
|
||||||
|
throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsAccount(txn, a))
|
||||||
|
throw new NoSuchLocalAuthorException();
|
||||||
|
return db.getAccount(txn, a);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Account> getAccounts(Transaction transaction)
|
||||||
|
throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
return db.getAccounts(txn);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Contact getContact(Transaction transaction, ContactId c)
|
public Contact getContact(Transaction transaction, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -505,7 +521,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
public Collection<ContactId> getContacts(Transaction transaction,
|
public Collection<ContactId> getContacts(Transaction transaction,
|
||||||
AuthorId a) throws DbException {
|
AuthorId a) throws DbException {
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsLocalAuthor(txn, a))
|
if (!db.containsAccount(txn, a))
|
||||||
throw new NoSuchLocalAuthorException();
|
throw new NoSuchLocalAuthorException();
|
||||||
return db.getContacts(txn, a);
|
return db.getContacts(txn, a);
|
||||||
}
|
}
|
||||||
@@ -553,22 +569,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.getHandshakeKeys(txn, t);
|
return db.getHandshakeKeys(txn, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a)
|
|
||||||
throws DbException {
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
if (!db.containsLocalAuthor(txn, a))
|
|
||||||
throw new NoSuchLocalAuthorException();
|
|
||||||
return db.getLocalAuthor(txn, a);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<LocalAuthor> getLocalAuthors(Transaction transaction)
|
|
||||||
throws DbException {
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
return db.getLocalAuthors(txn);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getMessage(Transaction transaction, MessageId m)
|
public Message getMessage(Transaction transaction, MessageId m)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -868,6 +868,17 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
if (requested) transaction.attach(new MessageRequestedEvent(c));
|
if (requested) transaction.attach(new MessageRequestedEvent(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAccount(Transaction transaction, AuthorId a)
|
||||||
|
throws DbException {
|
||||||
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsAccount(txn, a))
|
||||||
|
throw new NoSuchLocalAuthorException();
|
||||||
|
db.removeAccount(txn, a);
|
||||||
|
transaction.attach(new LocalAuthorRemovedEvent(a));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeContact(Transaction transaction, ContactId c)
|
public void removeContact(Transaction transaction, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -904,17 +915,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
db.removeHandshakeKeys(txn, t, k);
|
db.removeHandshakeKeys(txn, t, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeLocalAuthor(Transaction transaction, AuthorId a)
|
|
||||||
throws DbException {
|
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
if (!db.containsLocalAuthor(txn, a))
|
|
||||||
throw new NoSuchLocalAuthorException();
|
|
||||||
db.removeLocalAuthor(txn, a);
|
|
||||||
transaction.attach(new LocalAuthorRemovedEvent(a));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeMessage(Transaction transaction, MessageId m)
|
public void removeMessage(Transaction transaction, MessageId m)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -1040,7 +1040,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
byte[] publicKey, byte[] privateKey) throws DbException {
|
byte[] publicKey, byte[] privateKey) throws DbException {
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsLocalAuthor(txn, local))
|
if (!db.containsAccount(txn, local))
|
||||||
throw new NoSuchLocalAuthorException();
|
throw new NoSuchLocalAuthorException();
|
||||||
db.setHandshakeKeyPair(txn, local, publicKey, privateKey);
|
db.setHandshakeKeyPair(txn, local, publicKey, privateKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.briarproject.bramble.api.db.DbException;
|
|||||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
import org.briarproject.bramble.api.db.Metadata;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
@@ -661,6 +662,35 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
if (interrupted) Thread.currentThread().interrupt();
|
if (interrupted) Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addAccount(Connection txn, Account a) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
try {
|
||||||
|
String sql = "INSERT INTO localAuthors"
|
||||||
|
+ " (authorId, formatVersion, name, publicKey, privateKey,"
|
||||||
|
+ " handshakePublicKey, handshakePrivateKey, created)"
|
||||||
|
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
LocalAuthor local = a.getLocalAuthor();
|
||||||
|
ps.setBytes(1, local.getId().getBytes());
|
||||||
|
ps.setInt(2, local.getFormatVersion());
|
||||||
|
ps.setString(3, local.getName());
|
||||||
|
ps.setBytes(4, local.getPublicKey());
|
||||||
|
ps.setBytes(5, local.getPrivateKey());
|
||||||
|
if (a.getHandshakePublicKey() == null) ps.setNull(6, BINARY);
|
||||||
|
else ps.setBytes(6, a.getHandshakePublicKey());
|
||||||
|
if (a.getHandshakePrivateKey() == null) ps.setNull(7, BINARY);
|
||||||
|
else ps.setBytes(7, a.getHandshakePrivateKey());
|
||||||
|
ps.setLong(8, a.getTimeCreated());
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if (affected != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ContactId addContact(Connection txn, Author remote, AuthorId local,
|
public ContactId addContact(Connection txn, Author remote, AuthorId local,
|
||||||
boolean verified) throws DbException {
|
boolean verified) throws DbException {
|
||||||
@@ -873,35 +903,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addLocalAuthor(Connection txn, LocalAuthor a)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
String sql = "INSERT INTO localAuthors"
|
|
||||||
+ " (authorId, formatVersion, name, publicKey, privateKey,"
|
|
||||||
+ " handshakePublicKey, handshakePrivateKey, created)"
|
|
||||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, a.getId().getBytes());
|
|
||||||
ps.setInt(2, a.getFormatVersion());
|
|
||||||
ps.setString(3, a.getName());
|
|
||||||
ps.setBytes(4, a.getPublicKey());
|
|
||||||
ps.setBytes(5, a.getPrivateKey());
|
|
||||||
if (a.getHandshakePublicKey() == null) ps.setNull(6, BINARY);
|
|
||||||
else ps.setBytes(6, a.getHandshakePublicKey());
|
|
||||||
if (a.getHandshakePrivateKey() == null) ps.setNull(7, BINARY);
|
|
||||||
else ps.setBytes(7, a.getHandshakePrivateKey());
|
|
||||||
ps.setLong(8, a.getTimeCreated());
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if (affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addMessage(Connection txn, Message m, MessageState state,
|
public void addMessage(Connection txn, Message m, MessageState state,
|
||||||
boolean messageShared, @Nullable ContactId sender)
|
boolean messageShared, @Nullable ContactId sender)
|
||||||
@@ -1179,6 +1180,28 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAccount(Connection txn, AuthorId a)
|
||||||
|
throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT NULL FROM localAuthors WHERE authorId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, a.getBytes());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
boolean found = rs.next();
|
||||||
|
if (rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return found;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsContact(Connection txn, AuthorId remote,
|
public boolean containsContact(Connection txn, AuthorId remote,
|
||||||
AuthorId local) throws DbException {
|
AuthorId local) throws DbException {
|
||||||
@@ -1247,28 +1270,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsLocalAuthor(Connection txn, AuthorId a)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT NULL FROM localAuthors WHERE authorId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, a.getBytes());
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
boolean found = rs.next();
|
|
||||||
if (rs.next()) throw new DbStateException();
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return found;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(rs, LOG, WARNING);
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsMessage(Connection txn, MessageId m)
|
public boolean containsMessage(Connection txn, MessageId m)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -1426,6 +1427,76 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Account getAccount(Connection txn, AuthorId a) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT formatVersion, name, publicKey, privateKey,"
|
||||||
|
+ " handshakePublicKey, handshakePrivateKey, created"
|
||||||
|
+ " FROM localAuthors"
|
||||||
|
+ " WHERE authorId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, a.getBytes());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
if (!rs.next()) throw new DbStateException();
|
||||||
|
int formatVersion = rs.getInt(1);
|
||||||
|
String name = rs.getString(2);
|
||||||
|
byte[] publicKey = rs.getBytes(3);
|
||||||
|
byte[] privateKey = rs.getBytes(4);
|
||||||
|
byte[] handshakePublicKey = rs.getBytes(5);
|
||||||
|
byte[] handshakePrivateKey = rs.getBytes(6);
|
||||||
|
long created = rs.getLong(7);
|
||||||
|
if (rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
LocalAuthor local = new LocalAuthor(a, formatVersion, name,
|
||||||
|
publicKey, privateKey);
|
||||||
|
return new Account(local, handshakePublicKey, handshakePrivateKey,
|
||||||
|
created);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Account> getAccounts(Connection txn) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT authorId, formatVersion, name, publicKey,"
|
||||||
|
+ " privateKey, handshakePublicKey, handshakePrivateKey,"
|
||||||
|
+ " created"
|
||||||
|
+ " FROM localAuthors";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
List<Account> accounts = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
AuthorId authorId = new AuthorId(rs.getBytes(1));
|
||||||
|
int formatVersion = rs.getInt(2);
|
||||||
|
String name = rs.getString(3);
|
||||||
|
byte[] publicKey = rs.getBytes(4);
|
||||||
|
byte[] privateKey = rs.getBytes(5);
|
||||||
|
byte[] handshakePublicKey = rs.getBytes(6);
|
||||||
|
byte[] handshakePrivateKey = rs.getBytes(7);
|
||||||
|
long created = rs.getLong(8);
|
||||||
|
LocalAuthor local = new LocalAuthor(authorId, formatVersion,
|
||||||
|
name, publicKey, privateKey);
|
||||||
|
accounts.add(new Account(local, handshakePublicKey,
|
||||||
|
handshakePrivateKey, created));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return accounts;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Contact getContact(Connection txn, ContactId c) throws DbException {
|
public Contact getContact(Connection txn, ContactId c) throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
@@ -1660,49 +1731,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public LocalAuthor getLocalAuthor(Connection txn, AuthorId a)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT formatVersion, name, publicKey, privateKey,"
|
|
||||||
+ " handshakePublicKey, handshakePrivateKey, created"
|
|
||||||
+ " FROM localAuthors"
|
|
||||||
+ " WHERE authorId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, a.getBytes());
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
if (!rs.next()) throw new DbStateException();
|
|
||||||
int formatVersion = rs.getInt(1);
|
|
||||||
String name = rs.getString(2);
|
|
||||||
byte[] publicKey = rs.getBytes(3);
|
|
||||||
byte[] privateKey = rs.getBytes(4);
|
|
||||||
byte[] handshakePublicKey = rs.getBytes(5);
|
|
||||||
byte[] handshakePrivateKey = rs.getBytes(6);
|
|
||||||
long created = rs.getLong(7);
|
|
||||||
if (rs.next()) throw new DbStateException();
|
|
||||||
LocalAuthor localAuthor;
|
|
||||||
if (handshakePublicKey == null) {
|
|
||||||
if (handshakePrivateKey != null) throw new DbStateException();
|
|
||||||
localAuthor = new LocalAuthor(a, formatVersion, name,
|
|
||||||
publicKey, privateKey, created);
|
|
||||||
} else {
|
|
||||||
if (handshakePrivateKey == null) throw new DbStateException();
|
|
||||||
localAuthor = new LocalAuthor(a, formatVersion, name,
|
|
||||||
publicKey, privateKey, handshakePublicKey,
|
|
||||||
handshakePrivateKey, created);
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return localAuthor;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(rs, LOG, WARNING);
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<HandshakeKeySet> getHandshakeKeys(Connection txn,
|
public Collection<HandshakeKeySet> getHandshakeKeys(Connection txn,
|
||||||
TransportId t) throws DbException {
|
TransportId t) throws DbException {
|
||||||
@@ -1783,42 +1811,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<LocalAuthor> getLocalAuthors(Connection txn)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT authorId, formatVersion, name, publicKey,"
|
|
||||||
+ " privateKey, handshakePublicKey, handshakePrivateKey,"
|
|
||||||
+ " created"
|
|
||||||
+ " FROM localAuthors";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
List<LocalAuthor> authors = new ArrayList<>();
|
|
||||||
while (rs.next()) {
|
|
||||||
AuthorId authorId = new AuthorId(rs.getBytes(1));
|
|
||||||
int formatVersion = rs.getInt(2);
|
|
||||||
String name = rs.getString(3);
|
|
||||||
byte[] publicKey = rs.getBytes(4);
|
|
||||||
byte[] privateKey = rs.getBytes(5);
|
|
||||||
byte[] handshakePublicKey = rs.getBytes(6);
|
|
||||||
byte[] handshakePrivateKey = rs.getBytes(7);
|
|
||||||
long created = rs.getLong(8);
|
|
||||||
authors.add(new LocalAuthor(authorId, formatVersion, name,
|
|
||||||
publicKey, privateKey, handshakePublicKey,
|
|
||||||
handshakePrivateKey, created));
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return authors;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(rs, LOG, WARNING);
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getMessage(Connection txn, MessageId m) throws DbException {
|
public Message getMessage(Connection txn, MessageId m) throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
@@ -2887,6 +2879,22 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAccount(Connection txn, AuthorId a) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
try {
|
||||||
|
String sql = "DELETE FROM localAuthors WHERE authorId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, a.getBytes());
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if (affected != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeContact(Connection txn, ContactId c)
|
public void removeContact(Connection txn, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -2969,23 +2977,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeLocalAuthor(Connection txn, AuthorId a)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
String sql = "DELETE FROM localAuthors WHERE authorId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, a.getBytes());
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if (affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeMessage(Connection txn, MessageId m) throws DbException {
|
public void removeMessage(Connection txn, MessageId m) throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import org.briarproject.bramble.api.identity.AuthorFactory;
|
|||||||
import org.briarproject.bramble.api.identity.AuthorId;
|
import org.briarproject.bramble.api.identity.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
|
||||||
import org.briarproject.bramble.util.ByteUtils;
|
import org.briarproject.bramble.util.ByteUtils;
|
||||||
import org.briarproject.bramble.util.StringUtils;
|
import org.briarproject.bramble.util.StringUtils;
|
||||||
|
|
||||||
@@ -23,12 +22,10 @@ import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
|||||||
class AuthorFactoryImpl implements AuthorFactory {
|
class AuthorFactoryImpl implements AuthorFactory {
|
||||||
|
|
||||||
private final CryptoComponent crypto;
|
private final CryptoComponent crypto;
|
||||||
private final Clock clock;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AuthorFactoryImpl(CryptoComponent crypto, Clock clock) {
|
AuthorFactoryImpl(CryptoComponent crypto) {
|
||||||
this.crypto = crypto;
|
this.crypto = crypto;
|
||||||
this.clock = clock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -44,21 +41,12 @@ class AuthorFactoryImpl implements AuthorFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalAuthor createLocalAuthor(String name, boolean handshakeKeys) {
|
public LocalAuthor createLocalAuthor(String name) {
|
||||||
KeyPair signatureKeyPair = crypto.generateSignatureKeyPair();
|
KeyPair signatureKeyPair = crypto.generateSignatureKeyPair();
|
||||||
byte[] sigPub = signatureKeyPair.getPublic().getEncoded();
|
byte[] publicKey = signatureKeyPair.getPublic().getEncoded();
|
||||||
byte[] sigPriv = signatureKeyPair.getPrivate().getEncoded();
|
byte[] privateKey = signatureKeyPair.getPrivate().getEncoded();
|
||||||
AuthorId id = getId(FORMAT_VERSION, name, sigPub);
|
AuthorId id = getId(FORMAT_VERSION, name, publicKey);
|
||||||
if (handshakeKeys) {
|
return new LocalAuthor(id, FORMAT_VERSION, name, publicKey, privateKey);
|
||||||
KeyPair handshakeKeyPair = crypto.generateAgreementKeyPair();
|
|
||||||
byte[] handPub = handshakeKeyPair.getPublic().getEncoded();
|
|
||||||
byte[] handPriv = handshakeKeyPair.getPrivate().getEncoded();
|
|
||||||
return new LocalAuthor(id, FORMAT_VERSION, name, sigPub, sigPriv,
|
|
||||||
handPub, handPriv, clock.currentTimeMillis());
|
|
||||||
} else {
|
|
||||||
return new LocalAuthor(id, FORMAT_VERSION, name, sigPub, sigPriv,
|
|
||||||
clock.currentTimeMillis());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private AuthorId getId(int formatVersion, String name, byte[] publicKey) {
|
private AuthorId getId(int formatVersion, String name, byte[] publicKey) {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import org.briarproject.bramble.api.crypto.KeyPair;
|
|||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
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.AuthorFactory;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -32,87 +34,94 @@ class IdentityManagerImpl implements IdentityManager, OpenDatabaseHook {
|
|||||||
private final DatabaseComponent db;
|
private final DatabaseComponent db;
|
||||||
private final CryptoComponent crypto;
|
private final CryptoComponent crypto;
|
||||||
private final AuthorFactory authorFactory;
|
private final AuthorFactory authorFactory;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
// The local author is immutable so we can cache it
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile LocalAuthor cachedAuthor;
|
private volatile Account cachedAccount;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
IdentityManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
IdentityManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||||
AuthorFactory authorFactory) {
|
AuthorFactory authorFactory, Clock clock) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.crypto = crypto;
|
this.crypto = crypto;
|
||||||
this.authorFactory = authorFactory;
|
this.authorFactory = authorFactory;
|
||||||
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalAuthor createLocalAuthor(String name) {
|
public LocalAuthor createLocalAuthor(String name) {
|
||||||
long start = now();
|
long start = now();
|
||||||
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name, true);
|
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name);
|
||||||
logDuration(LOG, "Creating local author", start);
|
logDuration(LOG, "Creating local author", start);
|
||||||
return localAuthor;
|
return localAuthor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerLocalAuthor(LocalAuthor a) {
|
public Account createAccount(String name) {
|
||||||
cachedAuthor = a;
|
long start = now();
|
||||||
LOG.info("Local author registered");
|
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name);
|
||||||
|
KeyPair handshakeKeyPair = crypto.generateAgreementKeyPair();
|
||||||
|
byte[] handshakePub = handshakeKeyPair.getPublic().getEncoded();
|
||||||
|
byte[] handshakePriv = handshakeKeyPair.getPrivate().getEncoded();
|
||||||
|
logDuration(LOG, "Creating account", start);
|
||||||
|
return new Account(localAuthor, handshakePub, handshakePriv,
|
||||||
|
clock.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerAccount(Account a) {
|
||||||
|
if (!a.hasHandshakeKeyPair()) throw new IllegalArgumentException();
|
||||||
|
cachedAccount = a;
|
||||||
|
LOG.info("Account registered");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDatabaseOpened(Transaction txn) throws DbException {
|
public void onDatabaseOpened(Transaction txn) throws DbException {
|
||||||
LocalAuthor cached = cachedAuthor;
|
Account cached = cachedAccount;
|
||||||
if (cached == null) {
|
if (cached == null) {
|
||||||
LocalAuthor loaded = loadLocalAuthor(txn);
|
cached = loadAccount(txn);
|
||||||
if (loaded.getHandshakePublicKey() == null) {
|
if (cached.hasHandshakeKeyPair()) {
|
||||||
KeyPair handshakeKeyPair = crypto.generateAgreementKeyPair();
|
cachedAccount = cached;
|
||||||
byte[] handshakePublicKey =
|
LOG.info("Account loaded");
|
||||||
handshakeKeyPair.getPublic().getEncoded();
|
|
||||||
byte[] handshakePrivateKey =
|
|
||||||
handshakeKeyPair.getPrivate().getEncoded();
|
|
||||||
db.setHandshakeKeyPair(txn, loaded.getId(),
|
|
||||||
handshakePublicKey, handshakePrivateKey);
|
|
||||||
cachedAuthor = new LocalAuthor(loaded.getId(),
|
|
||||||
loaded.getFormatVersion(), loaded.getName(),
|
|
||||||
loaded.getPublicKey(), loaded.getPrivateKey(),
|
|
||||||
handshakePublicKey, handshakePrivateKey,
|
|
||||||
loaded.getTimeCreated());
|
|
||||||
LOG.info("Handshake key pair added");
|
|
||||||
} else {
|
} else {
|
||||||
cachedAuthor = loaded;
|
KeyPair handshakeKeyPair = crypto.generateAgreementKeyPair();
|
||||||
LOG.info("Local author loaded");
|
byte[] handshakePub = handshakeKeyPair.getPublic().getEncoded();
|
||||||
|
byte[] handshakePriv =
|
||||||
|
handshakeKeyPair.getPrivate().getEncoded();
|
||||||
|
db.setHandshakeKeyPair(txn, cached.getId(), handshakePub,
|
||||||
|
handshakePriv);
|
||||||
|
LOG.info("Handshake key pair stored");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
db.addLocalAuthor(txn, cached);
|
db.addAccount(txn, cached);
|
||||||
LOG.info("Local author stored");
|
LOG.info("Account stored");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalAuthor getLocalAuthor() throws DbException {
|
public LocalAuthor getLocalAuthor() throws DbException {
|
||||||
LocalAuthor cached = cachedAuthor;
|
Account cached = cachedAccount;
|
||||||
if (cached == null) {
|
if (cached == null) {
|
||||||
cachedAuthor = cached =
|
cachedAccount = cached =
|
||||||
db.transactionWithResult(true, this::loadLocalAuthor);
|
db.transactionWithResult(true, this::loadAccount);
|
||||||
LOG.info("Local author loaded");
|
LOG.info("Account loaded");
|
||||||
}
|
}
|
||||||
return cached;
|
return cached.getLocalAuthor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalAuthor getLocalAuthor(Transaction txn) throws DbException {
|
public LocalAuthor getLocalAuthor(Transaction txn) throws DbException {
|
||||||
LocalAuthor cached = cachedAuthor;
|
Account cached = cachedAccount;
|
||||||
if (cached == null) {
|
if (cached == null) {
|
||||||
cachedAuthor = cached = loadLocalAuthor(txn);
|
cachedAccount = cached = loadAccount(txn);
|
||||||
LOG.info("Local author loaded");
|
LOG.info("Account loaded");
|
||||||
}
|
}
|
||||||
return cached;
|
return cached.getLocalAuthor();
|
||||||
}
|
}
|
||||||
|
|
||||||
private LocalAuthor loadLocalAuthor(Transaction txn) throws DbException {
|
private Account loadAccount(Transaction txn) throws DbException {
|
||||||
Collection<LocalAuthor> authors = db.getLocalAuthors(txn);
|
Collection<Account> accounts = db.getAccounts(txn);
|
||||||
if (authors.size() != 1) throw new IllegalStateException();
|
if (accounts.size() != 1) throw new DbException();
|
||||||
return authors.iterator().next();
|
return accounts.iterator().next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.briarproject.bramble.account;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
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.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
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.assertNull;
|
||||||
import static junit.framework.Assert.assertTrue;
|
import static junit.framework.Assert.assertTrue;
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
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.getRandomBytes;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
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 String encryptedKeyHex = toHexString(encryptedKey);
|
||||||
private final byte[] newEncryptedKey = getRandomBytes(123);
|
private final byte[] newEncryptedKey = getRandomBytes(123);
|
||||||
private final String newEncryptedKeyHex = toHexString(newEncryptedKey);
|
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 authorName = localAuthor.getName();
|
||||||
private final String password = getRandomString(10);
|
private final String password = getRandomString(10);
|
||||||
private final String newPassword = getRandomString(10);
|
private final String newPassword = getRandomString(10);
|
||||||
@@ -251,9 +253,9 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testCreateAccountStoresDbKey() throws Exception {
|
public void testCreateAccountStoresDbKey() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(identityManager).createLocalAuthor(authorName);
|
oneOf(identityManager).createAccount(authorName);
|
||||||
will(returnValue(localAuthor));
|
will(returnValue(account));
|
||||||
oneOf(identityManager).registerLocalAuthor(localAuthor);
|
oneOf(identityManager).registerAccount(account);
|
||||||
oneOf(crypto).generateSecretKey();
|
oneOf(crypto).generateSecretKey();
|
||||||
will(returnValue(key));
|
will(returnValue(key));
|
||||||
oneOf(crypto).encryptWithPassword(key.getBytes(), password);
|
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.db.NoSuchTransportException;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
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.Author;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.identity.event.LocalAuthorAddedEvent;
|
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.sync.validation.MessageState.UNKNOWN;
|
||||||
import static org.briarproject.bramble.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
|
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.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.getAuthor;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
||||||
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.getLocalAuthor;
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||||
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;
|
||||||
@@ -106,6 +107,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
private final GroupId groupId;
|
private final GroupId groupId;
|
||||||
private final Group group;
|
private final Group group;
|
||||||
private final Author author;
|
private final Author author;
|
||||||
|
private final Account account;
|
||||||
private final LocalAuthor localAuthor;
|
private final LocalAuthor localAuthor;
|
||||||
private final String alias;
|
private final String alias;
|
||||||
private final Message message, message1;
|
private final Message message, message1;
|
||||||
@@ -124,7 +126,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
group = getGroup(clientId, majorVersion);
|
group = getGroup(clientId, majorVersion);
|
||||||
groupId = group.getId();
|
groupId = group.getId();
|
||||||
author = getAuthor();
|
author = getAuthor();
|
||||||
localAuthor = getLocalAuthor();
|
account = getAccount();
|
||||||
|
localAuthor = account.getLocalAuthor();
|
||||||
message = getMessage(groupId);
|
message = getMessage(groupId);
|
||||||
message1 = getMessage(groupId);
|
message1 = getMessage(groupId);
|
||||||
messageId = message.getId();
|
messageId = message.getId();
|
||||||
@@ -159,15 +162,15 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
// startTransaction()
|
// startTransaction()
|
||||||
oneOf(database).startTransaction();
|
oneOf(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
// registerLocalAuthor()
|
// addAccount()
|
||||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
oneOf(database).addLocalAuthor(txn, localAuthor);
|
oneOf(database).addAccount(txn, account);
|
||||||
oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class)));
|
oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class)));
|
||||||
// addContact()
|
// addContact()
|
||||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
oneOf(database).containsAccount(txn, author.getId());
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
oneOf(database).containsContact(txn, author.getId(),
|
oneOf(database).containsContact(txn, author.getId(),
|
||||||
localAuthor.getId());
|
localAuthor.getId());
|
||||||
@@ -203,10 +206,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).removeContact(txn, contactId);
|
oneOf(database).removeContact(txn, contactId);
|
||||||
oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class)));
|
oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class)));
|
||||||
// removeLocalAuthor()
|
// removeAccount()
|
||||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).removeLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).removeAccount(txn, localAuthor.getId());
|
||||||
oneOf(eventBus).broadcast(with(any(LocalAuthorRemovedEvent.class)));
|
oneOf(eventBus).broadcast(with(any(LocalAuthorRemovedEvent.class)));
|
||||||
// endTransaction()
|
// endTransaction()
|
||||||
oneOf(database).commitTransaction(txn);
|
oneOf(database).commitTransaction(txn);
|
||||||
@@ -218,7 +221,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
assertFalse(db.open(key, null));
|
assertFalse(db.open(key, null));
|
||||||
db.transaction(false, transaction -> {
|
db.transaction(false, transaction -> {
|
||||||
db.addLocalAuthor(transaction, localAuthor);
|
db.addAccount(transaction, account);
|
||||||
assertEquals(contactId, db.addContact(transaction, author,
|
assertEquals(contactId, db.addContact(transaction, author,
|
||||||
localAuthor.getId(), true));
|
localAuthor.getId(), true));
|
||||||
assertEquals(singletonList(contact),
|
assertEquals(singletonList(contact),
|
||||||
@@ -229,7 +232,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
db.getGroups(transaction, clientId, majorVersion));
|
db.getGroups(transaction, clientId, majorVersion));
|
||||||
db.removeGroup(transaction, group);
|
db.removeGroup(transaction, group);
|
||||||
db.removeContact(transaction, contactId);
|
db.removeContact(transaction, contactId);
|
||||||
db.removeLocalAuthor(transaction, localAuthor.getId());
|
db.removeAccount(transaction, localAuthor.getId());
|
||||||
});
|
});
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
@@ -434,14 +437,13 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVariousMethodsThrowExceptionIfLocalAuthorIsMissing()
|
public void testVariousMethodsThrowExceptionIfAccountIsMissing()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
context.checking(new Expectations() {{
|
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();
|
exactly(4).of(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
exactly(4).of(database).containsLocalAuthor(txn,
|
exactly(4).of(database).containsAccount(txn, localAuthor.getId());
|
||||||
localAuthor.getId());
|
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
exactly(4).of(database).abortTransaction(txn);
|
exactly(4).of(database).abortTransaction(txn);
|
||||||
}});
|
}});
|
||||||
@@ -459,7 +461,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
db.transaction(false, transaction ->
|
db.transaction(false, transaction ->
|
||||||
db.getLocalAuthor(transaction, localAuthor.getId()));
|
db.getAccount(transaction, localAuthor.getId()));
|
||||||
fail();
|
fail();
|
||||||
} catch (NoSuchLocalAuthorException expected) {
|
} catch (NoSuchLocalAuthorException expected) {
|
||||||
// Expected
|
// Expected
|
||||||
@@ -467,7 +469,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
db.transaction(false, transaction ->
|
db.transaction(false, transaction ->
|
||||||
db.removeLocalAuthor(transaction, localAuthor.getId()));
|
db.removeAccount(transaction, localAuthor.getId()));
|
||||||
fail();
|
fail();
|
||||||
} catch (NoSuchLocalAuthorException expected) {
|
} catch (NoSuchLocalAuthorException expected) {
|
||||||
// Expected
|
// Expected
|
||||||
@@ -1416,10 +1418,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(database).startTransaction();
|
oneOf(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
// Contact is a local identity
|
// Contact is a local identity
|
||||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
oneOf(database).containsAccount(txn, author.getId());
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).abortTransaction(txn);
|
oneOf(database).abortTransaction(txn);
|
||||||
}});
|
}});
|
||||||
@@ -1442,9 +1444,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(database).startTransaction();
|
oneOf(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
oneOf(database).containsAccount(txn, localAuthor.getId());
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
oneOf(database).containsAccount(txn, author.getId());
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
// Contact already exists for this local identity
|
// Contact already exists for this local identity
|
||||||
oneOf(database).containsContact(txn, author.getId(),
|
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.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
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.AuthorId;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.sync.ClientId;
|
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.SyncConstants.MAX_MESSAGE_IDS;
|
||||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
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.deleteTestDirectory;
|
||||||
|
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
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.getMessage;
|
||||||
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;
|
||||||
@@ -161,11 +162,11 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testContainsLocalAuthor() throws Exception {
|
public void testContainsAccount() throws Exception {
|
||||||
String name = "containsLocalAuthor(T, AuthorId)";
|
String name = "containsAccount(T, AuthorId)";
|
||||||
benchmark(name, db -> {
|
benchmark(name, db -> {
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
db.containsLocalAuthor(txn, localAuthor.getId());
|
db.containsAccount(txn, localAuthor.getId());
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -295,21 +296,21 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLocalAuthor() throws Exception {
|
public void testGetAccount() throws Exception {
|
||||||
String name = "getLocalAuthor(T, AuthorId)";
|
String name = "getAccount(T, AuthorId)";
|
||||||
benchmark(name, db -> {
|
benchmark(name, db -> {
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
db.getLocalAuthor(txn, localAuthor.getId());
|
db.getAccount(txn, localAuthor.getId());
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLocalAuthors() throws Exception {
|
public void testGetAccounts() throws Exception {
|
||||||
String name = "getLocalAuthors(T)";
|
String name = "getAccounts(T)";
|
||||||
benchmark(name, db -> {
|
benchmark(name, db -> {
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
db.getLocalAuthors(txn);
|
db.getAccounts(txn);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -531,7 +532,8 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void populateDatabase(Database<Connection> db) throws DbException {
|
void populateDatabase(Database<Connection> db) throws DbException {
|
||||||
localAuthor = getLocalAuthor();
|
Account account = getAccount();
|
||||||
|
localAuthor = account.getLocalAuthor();
|
||||||
clientIds = new ArrayList<>();
|
clientIds = new ArrayList<>();
|
||||||
contacts = new ArrayList<>();
|
contacts = new ArrayList<>();
|
||||||
groups = 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());
|
for (int i = 0; i < CLIENTS; i++) clientIds.add(getClientId());
|
||||||
|
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
for (int i = 0; i < CONTACTS; i++) {
|
for (int i = 0; i < CONTACTS; i++) {
|
||||||
ContactId c = db.addContact(txn, getAuthor(), localAuthor.getId(),
|
ContactId c = db.addContact(txn, getAuthor(), localAuthor.getId(),
|
||||||
random.nextBoolean());
|
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.DbException;
|
||||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
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.Author;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
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.LAST_COMPACTED_KEY;
|
||||||
import static org.briarproject.bramble.db.DatabaseConstants.MAX_COMPACTION_INTERVAL_MS;
|
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.deleteTestDirectory;
|
||||||
|
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
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.getMessage;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getPendingContact;
|
import static org.briarproject.bramble.test.TestUtils.getPendingContact;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||||
@@ -105,6 +106,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
private final int majorVersion;
|
private final int majorVersion;
|
||||||
private final Group group;
|
private final Group group;
|
||||||
private final Author author;
|
private final Author author;
|
||||||
|
private final Account account;
|
||||||
private final LocalAuthor localAuthor;
|
private final LocalAuthor localAuthor;
|
||||||
private final Message message;
|
private final Message message;
|
||||||
private final MessageId messageId;
|
private final MessageId messageId;
|
||||||
@@ -121,7 +123,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
group = getGroup(clientId, majorVersion);
|
group = getGroup(clientId, majorVersion);
|
||||||
groupId = group.getId();
|
groupId = group.getId();
|
||||||
author = getAuthor();
|
author = getAuthor();
|
||||||
localAuthor = getLocalAuthor();
|
account = getAccount();
|
||||||
|
localAuthor = account.getLocalAuthor();
|
||||||
message = getMessage(groupId);
|
message = getMessage(groupId);
|
||||||
messageId = message.getId();
|
messageId = message.getId();
|
||||||
transportId = getTransportId();
|
transportId = getTransportId();
|
||||||
@@ -147,7 +150,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertFalse(db.containsContact(txn, contactId));
|
assertFalse(db.containsContact(txn, contactId));
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
assertTrue(db.containsContact(txn, contactId));
|
assertTrue(db.containsContact(txn, contactId));
|
||||||
@@ -210,7 +213,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -241,7 +244,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared but unvalidated message
|
// Add a contact, a shared group and a shared but unvalidated message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -286,7 +289,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, an invisible group and a shared message
|
// Add a contact, an invisible group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -337,7 +340,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and an unshared message
|
// Add a contact, a shared group and an unshared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -368,7 +371,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -395,7 +398,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact and a visible group
|
// Add a contact and a visible group
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -436,7 +439,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -568,7 +571,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact and a shared group
|
// Add a contact and a shared group
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -588,7 +591,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact
|
// Add a contact
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
|
|
||||||
@@ -606,7 +609,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, an invisible group and a message
|
// Add a contact, an invisible group and a message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -625,7 +628,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact and a group
|
// Add a contact and a group
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -677,7 +680,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||||
|
|
||||||
// Add the contact, the transport and the transport keys
|
// Add the contact, the transport and the transport keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -778,7 +781,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
assertEquals(emptyList(), db.getHandshakeKeys(txn, transportId));
|
||||||
|
|
||||||
// Add the contact, the transport and the handshake keys
|
// Add the contact, the transport and the handshake keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -931,7 +934,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add the contact, transport and transport keys
|
// Add the contact, transport and transport keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -975,7 +978,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add the contact, transport and handshake keys
|
// Add the contact, transport and handshake keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -1022,7 +1025,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add the contact, transport and transport keys
|
// Add the contact, transport and transport keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -1069,7 +1072,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add the contact, transport and handshake keys
|
// Add the contact, transport and handshake keys
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addTransport(txn, transportId, 123);
|
db.addTransport(txn, transportId, 123);
|
||||||
@@ -1111,8 +1114,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a local author - no contacts should be associated
|
// Add an account for a local author - no contacts should be associated
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
|
|
||||||
// Add a contact associated with the local author
|
// Add a contact associated with the local author
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
@@ -1138,8 +1141,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a local author - no contacts should be associated
|
// Add an account for a local author - no contacts should be associated
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
Collection<ContactId> contacts =
|
Collection<ContactId> contacts =
|
||||||
db.getContacts(txn, localAuthor.getId());
|
db.getContacts(txn, localAuthor.getId());
|
||||||
assertEquals(emptyList(), contacts);
|
assertEquals(emptyList(), contacts);
|
||||||
@@ -1150,8 +1153,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
contacts = db.getContacts(txn, localAuthor.getId());
|
contacts = db.getContacts(txn, localAuthor.getId());
|
||||||
assertEquals(singletonList(contactId), contacts);
|
assertEquals(singletonList(contactId), contacts);
|
||||||
|
|
||||||
// Remove the local author - the contact should be removed
|
// Remove the account - the contact should be removed
|
||||||
db.removeLocalAuthor(txn, localAuthor.getId());
|
db.removeAccount(txn, localAuthor.getId());
|
||||||
contacts = db.getContacts(txn, localAuthor.getId());
|
contacts = db.getContacts(txn, localAuthor.getId());
|
||||||
assertEquals(emptyList(), contacts);
|
assertEquals(emptyList(), contacts);
|
||||||
assertFalse(db.containsContact(txn, contactId));
|
assertFalse(db.containsContact(txn, contactId));
|
||||||
@@ -1166,7 +1169,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact - initially there should be no offered messages
|
// Add a contact - initially there should be no offered messages
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
assertEquals(0, db.countOfferedMessages(txn, contactId));
|
assertEquals(0, db.countOfferedMessages(txn, contactId));
|
||||||
@@ -1750,7 +1753,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -1852,14 +1855,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testDifferentLocalAuthorsCanHaveTheSameContact()
|
public void testDifferentLocalAuthorsCanHaveTheSameContact()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
LocalAuthor localAuthor1 = getLocalAuthor();
|
Account account1 = getAccount();
|
||||||
|
LocalAuthor localAuthor1 = account1.getLocalAuthor();
|
||||||
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add two local authors
|
// Add accounts for two local authors
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
db.addLocalAuthor(txn, localAuthor1);
|
db.addAccount(txn, account1);
|
||||||
|
|
||||||
// Add the same contact for each local author
|
// Add the same contact for each local author
|
||||||
ContactId contactId =
|
ContactId contactId =
|
||||||
@@ -1883,7 +1887,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -1937,7 +1941,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact
|
// Add a contact
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
|
|
||||||
@@ -1994,7 +1998,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a group and a message
|
// Add a contact, a group and a message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -2078,7 +2082,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -2123,7 +2127,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, account);
|
||||||
assertEquals(contactId,
|
assertEquals(contactId,
|
||||||
db.addContact(txn, author, localAuthor.getId(), true));
|
db.addContact(txn, author, localAuthor.getId(), true));
|
||||||
db.addGroup(txn, group);
|
db.addGroup(txn, group);
|
||||||
@@ -2241,20 +2245,21 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetHandshakeKeyPair() throws Exception {
|
public void testSetHandshakeKeyPair() throws Exception {
|
||||||
assertNull(localAuthor.getHandshakePublicKey());
|
Account withoutKeys =
|
||||||
assertNull(localAuthor.getHandshakePrivateKey());
|
new Account(localAuthor, null, null, account.getTimeCreated());
|
||||||
|
assertFalse(withoutKeys.hasHandshakeKeyPair());
|
||||||
byte[] publicKey = getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
byte[] publicKey = getRandomBytes(MAX_AGREEMENT_PUBLIC_KEY_BYTES);
|
||||||
byte[] privateKey = getRandomBytes(123);
|
byte[] privateKey = getRandomBytes(123);
|
||||||
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
db.addLocalAuthor(txn, localAuthor);
|
db.addAccount(txn, withoutKeys);
|
||||||
LocalAuthor retrieved = db.getLocalAuthor(txn, localAuthor.getId());
|
Account retrieved = db.getAccount(txn, localAuthor.getId());
|
||||||
assertNull(retrieved.getHandshakePublicKey());
|
assertFalse(retrieved.hasHandshakeKeyPair());
|
||||||
assertNull(retrieved.getHandshakePrivateKey());
|
|
||||||
db.setHandshakeKeyPair(txn, localAuthor.getId(), publicKey, privateKey);
|
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(publicKey, retrieved.getHandshakePublicKey());
|
||||||
assertArrayEquals(privateKey, retrieved.getHandshakePrivateKey());
|
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.DatabaseComponent;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
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.AuthorFactory;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
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.BrambleMockTestCase;
|
||||||
import org.briarproject.bramble.test.DbExpectations;
|
import org.briarproject.bramble.test.DbExpectations;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
@@ -16,8 +18,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
import static org.briarproject.bramble.test.TestUtils.getAccount;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||||
@@ -26,57 +27,58 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
|||||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||||
private final AuthorFactory authorFactory =
|
private final AuthorFactory authorFactory =
|
||||||
context.mock(AuthorFactory.class);
|
context.mock(AuthorFactory.class);
|
||||||
|
private final Clock clock = context.mock(Clock.class);
|
||||||
private final PublicKey handshakePublicKey = context.mock(PublicKey.class);
|
private final PublicKey handshakePublicKey = context.mock(PublicKey.class);
|
||||||
private final PrivateKey handshakePrivateKey =
|
private final PrivateKey handshakePrivateKey =
|
||||||
context.mock(PrivateKey.class);
|
context.mock(PrivateKey.class);
|
||||||
|
|
||||||
private final Transaction txn = new Transaction(null, false);
|
private final Transaction txn = new Transaction(null, false);
|
||||||
private final LocalAuthor localAuthor = getLocalAuthor(true);
|
private final Account accountWithKeys = getAccount();
|
||||||
private final LocalAuthor localAuthorWithoutHandshakeKeys =
|
private final LocalAuthor localAuthor = accountWithKeys.getLocalAuthor();
|
||||||
new LocalAuthor(localAuthor.getId(), localAuthor.getFormatVersion(),
|
private final Account accountWithoutKeys = new Account(localAuthor,
|
||||||
localAuthor.getName(), localAuthor.getPublicKey(),
|
null, null, accountWithKeys.getTimeCreated());
|
||||||
localAuthor.getPrivateKey(), localAuthor.getTimeCreated());
|
|
||||||
private final KeyPair handshakeKeyPair =
|
private final KeyPair handshakeKeyPair =
|
||||||
new KeyPair(handshakePublicKey, handshakePrivateKey);
|
new KeyPair(handshakePublicKey, handshakePrivateKey);
|
||||||
private final byte[] handshakePublicKeyBytes =
|
private final byte[] handshakePublicKeyBytes =
|
||||||
localAuthor.getHandshakePublicKey();
|
accountWithKeys.getHandshakePublicKey();
|
||||||
private final byte[] handshakePrivateKeyBytes =
|
private final byte[] handshakePrivateKeyBytes =
|
||||||
localAuthor.getHandshakePrivateKey();
|
accountWithKeys.getHandshakePrivateKey();
|
||||||
|
|
||||||
private IdentityManagerImpl identityManager;
|
private IdentityManagerImpl identityManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
identityManager = new IdentityManagerImpl(db, crypto, authorFactory);
|
identityManager =
|
||||||
|
new IdentityManagerImpl(db, crypto, authorFactory, clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpenDatabaseHookLocalAuthorRegistered() throws Exception {
|
public void testOpenDatabaseHookAccountRegistered() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(db).addLocalAuthor(txn, localAuthor);
|
oneOf(db).addAccount(txn, accountWithKeys);
|
||||||
}});
|
}});
|
||||||
|
|
||||||
identityManager.registerLocalAuthor(localAuthor);
|
identityManager.registerAccount(accountWithKeys);
|
||||||
identityManager.onDatabaseOpened(txn);
|
identityManager.onDatabaseOpened(txn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpenDatabaseHookNoLocalAuthorRegisteredHandshakeKeys()
|
public void testOpenDatabaseHookNoAccountRegisteredHandshakeKeys()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(db).getLocalAuthors(txn);
|
oneOf(db).getAccounts(txn);
|
||||||
will(returnValue(singletonList(localAuthor)));
|
will(returnValue(singletonList(accountWithKeys)));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
identityManager.onDatabaseOpened(txn);
|
identityManager.onDatabaseOpened(txn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpenDatabaseHookNoLocalAuthorRegisteredNoHandshakeKeys()
|
public void testOpenDatabaseHookNoAccountRegisteredNoHandshakeKeys()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(db).getLocalAuthors(txn);
|
oneOf(db).getAccounts(txn);
|
||||||
will(returnValue(singletonList(localAuthorWithoutHandshakeKeys)));
|
will(returnValue(singletonList(accountWithoutKeys)));
|
||||||
oneOf(crypto).generateAgreementKeyPair();
|
oneOf(crypto).generateAgreementKeyPair();
|
||||||
will(returnValue(handshakeKeyPair));
|
will(returnValue(handshakeKeyPair));
|
||||||
oneOf(handshakePublicKey).getEncoded();
|
oneOf(handshakePublicKey).getEncoded();
|
||||||
@@ -88,27 +90,21 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
|||||||
}});
|
}});
|
||||||
|
|
||||||
identityManager.onDatabaseOpened(txn);
|
identityManager.onDatabaseOpened(txn);
|
||||||
|
|
||||||
LocalAuthor cached = identityManager.getLocalAuthor();
|
|
||||||
assertArrayEquals(handshakePublicKeyBytes,
|
|
||||||
cached.getHandshakePublicKey());
|
|
||||||
assertArrayEquals(handshakePrivateKeyBytes,
|
|
||||||
cached.getHandshakePrivateKey());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLocalAuthor() throws Exception {
|
public void testGetLocalAuthor() throws Exception {
|
||||||
context.checking(new DbExpectations() {{
|
context.checking(new DbExpectations() {{
|
||||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||||
oneOf(db).getLocalAuthors(txn);
|
oneOf(db).getAccounts(txn);
|
||||||
will(returnValue(singletonList(localAuthor)));
|
will(returnValue(singletonList(accountWithKeys)));
|
||||||
}});
|
}});
|
||||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetCachedLocalAuthor() throws DbException {
|
public void testGetCachedLocalAuthor() throws DbException {
|
||||||
identityManager.registerLocalAuthor(localAuthor);
|
identityManager.registerAccount(accountWithKeys);
|
||||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class FeedFactoryImpl implements FeedFactory {
|
|||||||
if (title == null) title = "RSS";
|
if (title == null) title = "RSS";
|
||||||
else title = StringUtils.truncateUtf8(title, MAX_AUTHOR_NAME_LENGTH);
|
else title = StringUtils.truncateUtf8(title, MAX_AUTHOR_NAME_LENGTH);
|
||||||
|
|
||||||
LocalAuthor localAuthor = authorFactory.createLocalAuthor(title, false);
|
LocalAuthor localAuthor = authorFactory.createLocalAuthor(title);
|
||||||
Blog blog = blogFactory.createFeedBlog(localAuthor);
|
Blog blog = blogFactory.createFeedBlog(localAuthor);
|
||||||
long added = clock.currentTimeMillis();
|
long added = clock.currentTimeMillis();
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ class FeedFactoryImpl implements FeedFactory {
|
|||||||
Author author = clientHelper.parseAndValidateAuthor(authorList);
|
Author author = clientHelper.parseAndValidateAuthor(authorList);
|
||||||
LocalAuthor localAuthor = new LocalAuthor(author.getId(),
|
LocalAuthor localAuthor = new LocalAuthor(author.getId(),
|
||||||
author.getFormatVersion(), author.getName(),
|
author.getFormatVersion(), author.getName(),
|
||||||
author.getPublicKey(), privateKey, 0);
|
author.getPublicKey(), privateKey);
|
||||||
Blog blog = blogFactory.createFeedBlog(localAuthor);
|
Blog blog = blogFactory.createFeedBlog(localAuthor);
|
||||||
|
|
||||||
String desc = d.getOptionalString(KEY_FEED_DESC);
|
String desc = d.getOptionalString(KEY_FEED_DESC);
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
@Override
|
@Override
|
||||||
public Contact addContact(String name) throws DbException {
|
public Contact addContact(String name) throws DbException {
|
||||||
LocalAuthor localAuthor = identityManager.getLocalAuthor();
|
LocalAuthor localAuthor = identityManager.getLocalAuthor();
|
||||||
LocalAuthor remote = authorFactory.createLocalAuthor(name, false);
|
LocalAuthor remote = authorFactory.createLocalAuthor(name);
|
||||||
return addContact(localAuthor.getId(), remote);
|
return addContact(localAuthor.getId(), remote);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,7 +202,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private LocalAuthor getRandomAuthor() {
|
private LocalAuthor getRandomAuthor() {
|
||||||
return authorFactory.createLocalAuthor(getRandomAuthorName(), false);
|
return authorFactory.createLocalAuthor(getRandomAuthorName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private SecretKey getSecretKey() {
|
private SecretKey getSecretKey() {
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ public class BlogManagerIntegrationTest
|
|||||||
|
|
||||||
author0 = identityManager0.getLocalAuthor();
|
author0 = identityManager0.getLocalAuthor();
|
||||||
author1 = identityManager1.getLocalAuthor();
|
author1 = identityManager1.getLocalAuthor();
|
||||||
rssAuthor = c0.getAuthorFactory().createLocalAuthor(
|
String rssTitle = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||||
getRandomString(MAX_AUTHOR_NAME_LENGTH), false);
|
rssAuthor = c0.getAuthorFactory().createLocalAuthor(rssTitle);
|
||||||
|
|
||||||
blogManager0 = c0.getBlogManager();
|
blogManager0 = c0.getBlogManager();
|
||||||
blogManager1 = c1.getBlogManager();
|
blogManager1 = c1.getBlogManager();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.briarproject.briar.feed;
|
package org.briarproject.briar.feed;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
import org.briarproject.bramble.contact.ContactModule;
|
import org.briarproject.bramble.contact.ContactModule;
|
||||||
import org.briarproject.bramble.crypto.CryptoExecutorModule;
|
import org.briarproject.bramble.crypto.CryptoExecutorModule;
|
||||||
@@ -51,8 +51,8 @@ public class FeedManagerIntegrationTest extends BriarTestCase {
|
|||||||
injectEagerSingletons(component);
|
injectEagerSingletons(component);
|
||||||
|
|
||||||
IdentityManager identityManager = component.getIdentityManager();
|
IdentityManager identityManager = component.getIdentityManager();
|
||||||
LocalAuthor localAuthor = identityManager.createLocalAuthor("feedTest");
|
Account account = identityManager.createAccount("feedTest");
|
||||||
identityManager.registerLocalAuthor(localAuthor);
|
identityManager.registerAccount(account);
|
||||||
|
|
||||||
lifecycleManager = component.getLifecycleManager();
|
lifecycleManager = component.getLifecycleManager();
|
||||||
lifecycleManager.startServices(getSecretKey());
|
lifecycleManager.startServices(getSecretKey());
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
|
|||||||
public void testForumPostFitsIntoRecord() throws Exception {
|
public void testForumPostFitsIntoRecord() throws Exception {
|
||||||
// Create a maximum-length author
|
// Create a maximum-length author
|
||||||
String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||||
LocalAuthor author = authorFactory.createLocalAuthor(authorName, false);
|
LocalAuthor author = authorFactory.createLocalAuthor(authorName);
|
||||||
// Create a maximum-length forum post
|
// Create a maximum-length forum post
|
||||||
GroupId groupId = new GroupId(getRandomId());
|
GroupId groupId = new GroupId(getRandomId());
|
||||||
long timestamp = Long.MAX_VALUE;
|
long timestamp = Long.MAX_VALUE;
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
|||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
|
||||||
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 org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
@@ -75,13 +75,14 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testWriteAndRead() throws Exception {
|
public void testWriteAndRead() throws Exception {
|
||||||
// Create the identities
|
// Create the identities
|
||||||
LocalAuthor aliceAuthor =
|
Account aliceAccount =
|
||||||
alice.getIdentityManager().createLocalAuthor("Alice");
|
alice.getIdentityManager().createAccount("Alice");
|
||||||
LocalAuthor bobAuthor =
|
Account bobAccount = bob.getIdentityManager().createAccount("Bob");
|
||||||
bob.getIdentityManager().createLocalAuthor("Bob");
|
|
||||||
// Set up the devices and get the contact IDs
|
// Set up the devices and get the contact IDs
|
||||||
ContactId bobId = setUp(alice, aliceAuthor, bobAuthor, true);
|
ContactId bobId = setUp(alice, aliceAccount,
|
||||||
ContactId aliceId = setUp(bob, bobAuthor, aliceAuthor, false);
|
bobAccount.getLocalAuthor(), true);
|
||||||
|
ContactId aliceId = setUp(bob, bobAccount,
|
||||||
|
aliceAccount.getLocalAuthor(), false);
|
||||||
// Add a private message listener
|
// Add a private message listener
|
||||||
PrivateMessageListener listener = new PrivateMessageListener();
|
PrivateMessageListener listener = new PrivateMessageListener();
|
||||||
bob.getEventBus().addListener(listener);
|
bob.getEventBus().addListener(listener);
|
||||||
@@ -100,10 +101,10 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ContactId setUp(SimplexMessagingIntegrationTestComponent device,
|
private ContactId setUp(SimplexMessagingIntegrationTestComponent device,
|
||||||
LocalAuthor local, Author remote, boolean alice) throws Exception {
|
Account local, Author remote, boolean alice) throws Exception {
|
||||||
// Add an identity for the user
|
// Add an identity for the user
|
||||||
IdentityManager identityManager = device.getIdentityManager();
|
IdentityManager identityManager = device.getIdentityManager();
|
||||||
identityManager.registerLocalAuthor(local);
|
identityManager.registerAccount(local);
|
||||||
// Start the lifecycle manager
|
// Start the lifecycle manager
|
||||||
LifecycleManager lifecycleManager = device.getLifecycleManager();
|
LifecycleManager lifecycleManager = device.getLifecycleManager();
|
||||||
lifecycleManager.startServices(getSecretKey());
|
lifecycleManager.startServices(getSecretKey());
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
|
import org.briarproject.bramble.api.identity.Account;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
@@ -274,12 +275,15 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createAndRegisterIdentities() {
|
private void createAndRegisterIdentities() {
|
||||||
author0 = identityManager0.createLocalAuthor(AUTHOR0);
|
Account account0 = identityManager0.createAccount(AUTHOR0);
|
||||||
identityManager0.registerLocalAuthor(author0);
|
identityManager0.registerAccount(account0);
|
||||||
author1 = identityManager1.createLocalAuthor(AUTHOR1);
|
author0 = account0.getLocalAuthor();
|
||||||
identityManager1.registerLocalAuthor(author1);
|
Account account1 = identityManager0.createAccount(AUTHOR1);
|
||||||
author2 = identityManager2.createLocalAuthor(AUTHOR2);
|
identityManager1.registerAccount(account1);
|
||||||
identityManager2.registerLocalAuthor(author2);
|
author1 = account1.getLocalAuthor();
|
||||||
|
Account account2 = identityManager0.createAccount(AUTHOR2);
|
||||||
|
identityManager2.registerAccount(account2);
|
||||||
|
author2 = account2.getLocalAuthor();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addDefaultContacts() throws Exception {
|
protected void addDefaultContacts() throws Exception {
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import org.briarproject.briar.api.client.MessageTracker;
|
|||||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||||
|
|
||||||
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.test.TestUtils.getRandomBytes;
|
|
||||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@@ -34,13 +32,12 @@ public class BriarTestUtils {
|
|||||||
|
|
||||||
public static Author getRealAuthor(AuthorFactory authorFactory) {
|
public static Author getRealAuthor(AuthorFactory authorFactory) {
|
||||||
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||||
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
return authorFactory.createLocalAuthor(name);
|
||||||
return authorFactory.createAuthor(name, publicKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LocalAuthor getRealLocalAuthor(AuthorFactory authorFactory) {
|
public static LocalAuthor getRealLocalAuthor(AuthorFactory authorFactory) {
|
||||||
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||||
return authorFactory.createLocalAuthor(name, false);
|
return authorFactory.createLocalAuthor(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user