mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
clean up mailbox integration tests
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.api.identity.Identity;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxPairingState;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxPairingTask;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.mailbox.lib.AbstractMailbox;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.briarproject.bramble.api.mailbox.MailboxAuthToken.fromString;
|
||||
import static org.briarproject.bramble.mailbox.MailboxTestUtils.getQrCodePayload;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
abstract class AbstractMailboxIntegrationTest extends BrambleTestCase {
|
||||
|
||||
static final String URL_BASE = "http://127.0.0.1:8000";
|
||||
|
||||
private final File testDir = getTestDirectory();
|
||||
final File dir1 = new File(testDir, "alice");
|
||||
final File dir2 = new File(testDir, "bob");
|
||||
|
||||
MailboxIntegrationTestComponent c1, c2;
|
||||
|
||||
MailboxIntegrationTestComponent startTestComponent(
|
||||
File databaseDir, String name) throws Exception {
|
||||
MailboxIntegrationTestComponent component =
|
||||
DaggerMailboxIntegrationTestComponent
|
||||
.builder()
|
||||
.testDatabaseConfigModule(
|
||||
new TestDatabaseConfigModule(databaseDir))
|
||||
.build();
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(component);
|
||||
setUp(component, name);
|
||||
return component;
|
||||
}
|
||||
|
||||
private void setUp(MailboxIntegrationTestComponent device,
|
||||
String name) throws Exception {
|
||||
// Add an identity for the user
|
||||
IdentityManager identityManager = device.getIdentityManager();
|
||||
Identity identity = identityManager.createIdentity(name);
|
||||
identityManager.registerIdentity(identity);
|
||||
// Start the lifecycle manager
|
||||
LifecycleManager lifecycleManager = device.getLifecycleManager();
|
||||
lifecycleManager.startServices(getSecretKey());
|
||||
lifecycleManager.waitForStartup();
|
||||
}
|
||||
|
||||
MailboxProperties pair(MailboxIntegrationTestComponent c,
|
||||
AbstractMailbox mailbox) throws Exception {
|
||||
MailboxAuthToken setupToken = fromString(mailbox.getSetupToken());
|
||||
|
||||
MailboxPairingTask pairingTask = c.getMailboxManager()
|
||||
.startPairingTask(getQrCodePayload(setupToken.getBytes()));
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
pairingTask.addObserver((state) -> {
|
||||
if (state instanceof MailboxPairingState.Paired) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
if (!latch.await(10, SECONDS)) {
|
||||
fail("Timeout reached when waiting for pairing.");
|
||||
}
|
||||
return c.getDatabaseComponent()
|
||||
.transactionWithNullableResult(true, txn ->
|
||||
c.getMailboxSettingsManager()
|
||||
.getOwnMailboxProperties(txn)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import java.util.List;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.URL_BASE;
|
||||
import static org.briarproject.bramble.mailbox.AbstractMailboxIntegrationTest.URL_BASE;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.createMailboxApi;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
|
||||
@@ -2,15 +2,11 @@ package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdateManager;
|
||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.TestUrlConverterModule;
|
||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||
import org.briarproject.bramble.test.BrambleIntegrationTestComponent;
|
||||
@@ -38,19 +34,11 @@ interface MailboxIntegrationTestComponent extends
|
||||
|
||||
MailboxManager getMailboxManager();
|
||||
|
||||
MailboxUpdateManager getMailboxUpdateManager();
|
||||
|
||||
MailboxSettingsManager getMailboxSettingsManager();
|
||||
|
||||
LifecycleManager getLifecycleManager();
|
||||
|
||||
ContactManager getContactManager();
|
||||
|
||||
Clock getClock();
|
||||
|
||||
TransportPropertyManager getTransportPropertyManager();
|
||||
|
||||
AuthorFactory getAuthorFactory();
|
||||
|
||||
CryptoComponent getCrypto();
|
||||
}
|
||||
|
||||
@@ -1,89 +1,16 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import static java.lang.System.currentTimeMillis;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.mailbox.MailboxTestUtils.createHttpClientProvider;
|
||||
import static org.briarproject.bramble.mailbox.TestUrlConverterModule.urlConverter;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
class MailboxIntegrationTestUtils {
|
||||
|
||||
static final String URL_BASE = "http://127.0.0.1:8000";
|
||||
|
||||
static String getQrCodePayload(MailboxAuthToken setupToken) {
|
||||
byte[] bytes = getQrCodeBytes(setupToken);
|
||||
Charset charset = Charset.forName("ISO-8859-1");
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
|
||||
private static byte[] getQrCodeBytes(MailboxAuthToken setupToken) {
|
||||
byte[] hiddenServiceBytes = getHiddenServiceBytes();
|
||||
byte[] setupTokenBytes = setupToken.getBytes();
|
||||
return ByteBuffer.allocate(65).put((byte) 32)
|
||||
.put(hiddenServiceBytes).put(setupTokenBytes).array();
|
||||
}
|
||||
|
||||
private static byte[] getHiddenServiceBytes() {
|
||||
byte[] data = new byte[32];
|
||||
Arrays.fill(data, (byte) 'a');
|
||||
return data;
|
||||
}
|
||||
|
||||
private static WeakSingletonProvider<OkHttpClient> createHttpClientProvider() {
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.socketFactory(SocketFactory.getDefault())
|
||||
.connectTimeout(60_000, MILLISECONDS)
|
||||
.build();
|
||||
return new WeakSingletonProvider<OkHttpClient>() {
|
||||
@Override
|
||||
@Nonnull
|
||||
public OkHttpClient createInstance() {
|
||||
return client;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static MailboxApi createMailboxApi() {
|
||||
return new MailboxApiImpl(createHttpClientProvider(),
|
||||
new TestUrlConverter());
|
||||
}
|
||||
|
||||
static MailboxIntegrationTestComponent createTestComponent(
|
||||
File databaseDir) {
|
||||
MailboxIntegrationTestComponent component =
|
||||
DaggerMailboxIntegrationTestComponent
|
||||
.builder()
|
||||
.testDatabaseConfigModule(
|
||||
new TestDatabaseConfigModule(databaseDir))
|
||||
.build();
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(component);
|
||||
return component;
|
||||
}
|
||||
|
||||
@Module
|
||||
static class TestUrlConverterModule {
|
||||
|
||||
@Provides
|
||||
UrlConverter provideUrlConverter() {
|
||||
return new TestUrlConverter();
|
||||
}
|
||||
return new MailboxApiImpl(createHttpClientProvider(), urlConverter);
|
||||
}
|
||||
|
||||
interface Check {
|
||||
@@ -126,6 +53,7 @@ class MailboxIntegrationTestUtils {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
//noinspection BusyWait
|
||||
Thread.sleep(step);
|
||||
} catch (InterruptedException ignore) {
|
||||
// continue
|
||||
|
||||
@@ -6,14 +6,8 @@ import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.Identity;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxPairingState.Paired;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxPairingTask;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.mailbox.lib.TestMailbox;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -21,23 +15,17 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.briarproject.bramble.api.mailbox.MailboxAuthToken.fromString;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.createMailboxApi;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.createTestComponent;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.getQrCodePayload;
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.retryUntilSuccessOrTimeout;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class OwnMailboxContactListWorkerIntegrationTest
|
||||
extends BrambleTestCase {
|
||||
extends AbstractMailboxIntegrationTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder mailboxDataDirectory = new TemporaryFolder();
|
||||
@@ -48,11 +36,7 @@ public class OwnMailboxContactListWorkerIntegrationTest
|
||||
|
||||
private MailboxProperties ownerProperties;
|
||||
|
||||
private final File testDir = getTestDirectory();
|
||||
private final File aliceDir = new File(testDir, "alice");
|
||||
|
||||
private MailboxIntegrationTestComponent component;
|
||||
private Identity identity;
|
||||
private LocalAuthor localAuthor1;
|
||||
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
private final long timestamp = System.currentTimeMillis();
|
||||
@@ -62,27 +46,10 @@ public class OwnMailboxContactListWorkerIntegrationTest
|
||||
mailbox = new TestMailbox(mailboxDataDirectory.getRoot());
|
||||
mailbox.startLifecycle();
|
||||
|
||||
MailboxAuthToken setupToken = fromString(mailbox.getSetupToken());
|
||||
c1 = startTestComponent(dir1, "Alice");
|
||||
localAuthor1 = c1.getIdentityManager().getLocalAuthor();
|
||||
|
||||
component = createTestComponent(aliceDir);
|
||||
identity = setUp(component, "Alice");
|
||||
|
||||
MailboxPairingTask pairingTask = component.getMailboxManager()
|
||||
.startPairingTask(getQrCodePayload(setupToken));
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
pairingTask.addObserver((state) -> {
|
||||
if (state instanceof Paired) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
latch.await();
|
||||
|
||||
ownerProperties = component.getDatabaseComponent()
|
||||
.transactionWithNullableResult(false, txn ->
|
||||
component.getMailboxSettingsManager()
|
||||
.getOwnMailboxProperties(txn)
|
||||
);
|
||||
ownerProperties = pair(c1, mailbox);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -90,28 +57,15 @@ public class OwnMailboxContactListWorkerIntegrationTest
|
||||
mailbox.stopLifecycle(true);
|
||||
}
|
||||
|
||||
private Identity setUp(MailboxIntegrationTestComponent device, String name)
|
||||
throws Exception {
|
||||
// Add an identity for the user
|
||||
IdentityManager identityManager = device.getIdentityManager();
|
||||
Identity identity = identityManager.createIdentity(name);
|
||||
identityManager.registerIdentity(identity);
|
||||
// Start the lifecycle manager
|
||||
LifecycleManager lifecycleManager = device.getLifecycleManager();
|
||||
lifecycleManager.startServices(getSecretKey());
|
||||
lifecycleManager.waitForStartup();
|
||||
// Check the initial conditions
|
||||
ContactManager contactManager = device.getContactManager();
|
||||
assertEquals(0, contactManager.getPendingContacts().size());
|
||||
assertEquals(0, contactManager.getContacts().size());
|
||||
return identity;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUploadContacts() throws Exception {
|
||||
// Check the initial conditions
|
||||
ContactManager contactManager = c1.getContactManager();
|
||||
assertEquals(0, contactManager.getPendingContacts().size());
|
||||
assertEquals(0, contactManager.getContacts().size());
|
||||
|
||||
int numContactsToAdd = 5;
|
||||
List<ContactId> expectedContacts =
|
||||
createContacts(component, identity, numContactsToAdd);
|
||||
List<ContactId> expectedContacts = createContacts(c1, numContactsToAdd);
|
||||
|
||||
// Check for number of contacts on mailbox via API every 100ms
|
||||
retryUntilSuccessOrTimeout(1000, 100, () -> {
|
||||
@@ -125,15 +79,16 @@ public class OwnMailboxContactListWorkerIntegrationTest
|
||||
}
|
||||
|
||||
private List<ContactId> createContacts(
|
||||
MailboxIntegrationTestComponent component, Identity local,
|
||||
int numContacts) throws DbException {
|
||||
MailboxIntegrationTestComponent component, int numContacts)
|
||||
throws DbException {
|
||||
List<ContactId> contactIds = new ArrayList<>();
|
||||
ContactManager contactManager = component.getContactManager();
|
||||
AuthorFactory authorFactory = component.getAuthorFactory();
|
||||
for (int i = 0; i < numContacts; i++) {
|
||||
Author remote = authorFactory.createLocalAuthor("Bob " + i);
|
||||
contactIds.add(contactManager.addContact(remote, local.getId(),
|
||||
rootKey, timestamp, true, true, true));
|
||||
ContactId c = contactManager.addContact(remote,
|
||||
localAuthor1.getId(), rootKey, timestamp, true, true, true);
|
||||
contactIds.add(c);
|
||||
}
|
||||
return contactIds;
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import static org.briarproject.bramble.mailbox.MailboxIntegrationTestUtils.URL_BASE;
|
||||
|
||||
@NotNullByDefault
|
||||
class TestUrlConverter implements UrlConverter {
|
||||
|
||||
@Override
|
||||
public String convertOnionToBaseUrl(String onion) {
|
||||
return URL_BASE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static org.briarproject.bramble.mailbox.AbstractMailboxIntegrationTest.URL_BASE;
|
||||
|
||||
@Module
|
||||
@NotNullByDefault
|
||||
class TestUrlConverterModule {
|
||||
|
||||
static UrlConverter urlConverter = onion -> URL_BASE;
|
||||
|
||||
@Provides
|
||||
UrlConverter provideUrlConverter() {
|
||||
return urlConverter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
|
||||
@NotNullByDefault
|
||||
public class FakeTorPlugin implements DuplexPlugin {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(FakeTorPlugin.class.getName());
|
||||
|
||||
private State state = INACTIVE;
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return TorConstants.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxLatency() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxIdleTime() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
LOG.info("Starting plugin");
|
||||
state = ACTIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
LOG.info("Stopping plugin");
|
||||
state = DISABLED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReasonsDisabled() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldPoll() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPollingInterval() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(
|
||||
Collection<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsKeyAgreement() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public KeyAgreementListener createKeyAgreementListener(
|
||||
byte[] localCommitment) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DuplexTransportConnection createKeyAgreementConnection(
|
||||
byte[] remoteCommitment, BdfList descriptor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsRendezvous() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
|
||||
boolean alice, ConnectionHandler incoming) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
|
||||
@Module
|
||||
public class FakeTorPluginConfigModule {
|
||||
|
||||
public static final TransportId SIMPLEX_TRANSPORT_ID = getTransportId();
|
||||
private static final int MAX_LATENCY = 30_000; // 30 seconds
|
||||
|
||||
@NotNullByDefault
|
||||
private final SimplexPluginFactory simplex = new SimplexPluginFactory() {
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return SIMPLEX_TRANSPORT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxLatency() {
|
||||
return MAX_LATENCY;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public SimplexPlugin createPlugin(PluginCallback callback) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@Provides
|
||||
PluginConfig providePluginConfig(FakeTorPluginFactory tor) {
|
||||
@NotNullByDefault
|
||||
PluginConfig pluginConfig = new PluginConfig() {
|
||||
|
||||
@Override
|
||||
public Collection<DuplexPluginFactory> getDuplexFactories() {
|
||||
return singletonList(tor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SimplexPluginFactory> getSimplexFactories() {
|
||||
return singletonList(simplex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldPoll() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<TransportId, List<TransportId>> getTransportPreferences() {
|
||||
return emptyMap();
|
||||
}
|
||||
|
||||
};
|
||||
return pluginConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@NotNullByDefault
|
||||
public class FakeTorPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
|
||||
|
||||
@Inject
|
||||
FakeTorPluginFactory() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return TorConstants.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxLatency() {
|
||||
return MAX_LATENCY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
return new FakeTorPlugin();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user