mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Move local author creation into IdentityManager.
This commit is contained in:
@@ -2,15 +2,21 @@ package org.briarproject.bramble.identity;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -27,24 +33,48 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final IdentityManager identityManager;
|
||||
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final AuthorFactory authorFactory =
|
||||
context.mock(AuthorFactory.class);
|
||||
private final PublicKey publicKey = context.mock(PublicKey.class);
|
||||
private final PrivateKey privateKey = context.mock(PrivateKey.class);
|
||||
|
||||
private final Transaction txn = new Transaction(null, false);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor();
|
||||
private final Collection<LocalAuthor> localAuthors =
|
||||
Collections.singletonList(localAuthor);
|
||||
private final String authorName = localAuthor.getName();
|
||||
private final KeyPair keyPair = new KeyPair(publicKey, privateKey);
|
||||
private final byte[] publicKeyBytes = localAuthor.getPublicKey();
|
||||
private final byte[] privateKeyBytes = localAuthor.getPrivateKey();
|
||||
private IdentityManager identityManager;
|
||||
|
||||
public IdentityManagerImplTest() {
|
||||
identityManager = new IdentityManagerImpl(db);
|
||||
@Before
|
||||
public void setUp() {
|
||||
identityManager = new IdentityManagerImpl(db, crypto, authorFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterLocalAuthor() throws DbException {
|
||||
expectRegisterLocalAuthor();
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
public void testCreateLocalAuthor() {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(crypto).generateSignatureKeyPair();
|
||||
will(returnValue(keyPair));
|
||||
oneOf(publicKey).getEncoded();
|
||||
will(returnValue(publicKeyBytes));
|
||||
oneOf(privateKey).getEncoded();
|
||||
will(returnValue(privateKeyBytes));
|
||||
oneOf(authorFactory).createLocalAuthor(authorName,
|
||||
publicKeyBytes, privateKeyBytes);
|
||||
will(returnValue(localAuthor));
|
||||
}});
|
||||
|
||||
assertEquals(localAuthor,
|
||||
identityManager.createLocalAuthor(authorName));
|
||||
}
|
||||
|
||||
private void expectRegisterLocalAuthor() throws DbException {
|
||||
@Test
|
||||
public void testRegisterAndStoreLocalAuthor() throws DbException {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).startTransaction(false);
|
||||
will(returnValue(txn));
|
||||
@@ -52,6 +82,10 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(db).commitTransaction(txn);
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
identityManager.storeLocalAuthor();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,7 +103,6 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetCachedLocalAuthor() throws DbException {
|
||||
expectRegisterLocalAuthor();
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
}
|
||||
|
||||
@@ -46,16 +46,6 @@ public class TestDatabaseConfig implements DatabaseConfig {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocalAuthorName(String nickname) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalAuthorName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return maxSize;
|
||||
|
||||
@@ -11,7 +11,6 @@ import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
@@ -40,7 +39,7 @@ public class TestLifecycleModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StartResult startServices(@Nullable String nickname) {
|
||||
public StartResult startServices() {
|
||||
return StartResult.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -49,15 +48,15 @@ public class TestLifecycleModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForDatabase() throws InterruptedException {
|
||||
public void waitForDatabase() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForStartup() throws InterruptedException {
|
||||
public void waitForStartup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForShutdown() throws InterruptedException {
|
||||
public void waitForShutdown() {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user