mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Merge branch 'test-sync-at-higher-level' into 'master'
Test sync at a higher level See merge request briar/briar!1116
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class TestCryptoExecutorModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@CryptoExecutor
|
||||
Executor provideCryptoExecutor() {
|
||||
return new ImmediateExecutor();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class TestDuplexTransportConnection
|
||||
implements DuplexTransportConnection {
|
||||
|
||||
private final TransportConnectionReader reader;
|
||||
private final TransportConnectionWriter writer;
|
||||
|
||||
public TestDuplexTransportConnection(InputStream in, OutputStream out) {
|
||||
reader = new TestTransportConnectionReader(in);
|
||||
writer = new TestTransportConnectionWriter(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a pair of TestDuplexTransportConnections that are
|
||||
* connected to each other.
|
||||
*/
|
||||
public static TestDuplexTransportConnection[] createPair()
|
||||
throws IOException {
|
||||
PipedInputStream aliceIn = new PipedInputStream();
|
||||
PipedInputStream bobIn = new PipedInputStream();
|
||||
PipedOutputStream aliceOut = new PipedOutputStream(bobIn);
|
||||
PipedOutputStream bobOut = new PipedOutputStream(aliceIn);
|
||||
TestDuplexTransportConnection alice =
|
||||
new TestDuplexTransportConnection(aliceIn, aliceOut);
|
||||
TestDuplexTransportConnection bob =
|
||||
new TestDuplexTransportConnection(bobIn, bobOut);
|
||||
return new TestDuplexTransportConnection[] {alice, bob};
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
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.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
@@ -15,22 +16,22 @@ import javax.annotation.Nullable;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
|
||||
@Module
|
||||
public class TestPluginConfigModule {
|
||||
|
||||
public static final TransportId TRANSPORT_ID = getTransportId();
|
||||
public static final int MAX_LATENCY = 2 * 60 * 1000; // 2 minutes
|
||||
public static final TransportId SIMPLEX_TRANSPORT_ID = getTransportId();
|
||||
public static final TransportId DUPLEX_TRANSPORT_ID = getTransportId();
|
||||
public static final int MAX_LATENCY = 30_000; // 30 seconds
|
||||
|
||||
@NotNullByDefault
|
||||
private final SimplexPluginFactory simplex = new SimplexPluginFactory() {
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return TRANSPORT_ID;
|
||||
return SIMPLEX_TRANSPORT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,6 +46,26 @@ public class TestPluginConfigModule {
|
||||
}
|
||||
};
|
||||
|
||||
@NotNullByDefault
|
||||
private final DuplexPluginFactory duplex = new DuplexPluginFactory() {
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return DUPLEX_TRANSPORT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLatency() {
|
||||
return MAX_LATENCY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@Provides
|
||||
PluginConfig providePluginConfig() {
|
||||
@NotNullByDefault
|
||||
@@ -52,7 +73,7 @@ public class TestPluginConfigModule {
|
||||
|
||||
@Override
|
||||
public Collection<DuplexPluginFactory> getDuplexFactories() {
|
||||
return emptyList();
|
||||
return singletonList(duplex);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class TestTransportConnectionReader
|
||||
implements TransportConnectionReader {
|
||||
|
||||
private final InputStream in;
|
||||
|
||||
public TestTransportConnectionReader(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class TestTransportConnectionWriter
|
||||
implements TransportConnectionWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final CountDownLatch disposed = new CountDownLatch(1);
|
||||
|
||||
public TestTransportConnectionWriter(OutputStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
public CountDownLatch getDisposedLatch() {
|
||||
return disposed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLatency() {
|
||||
return 30_000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxIdleTime() {
|
||||
return 60_000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() {
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
disposed.countDown();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfEntry;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
@@ -37,12 +38,10 @@ import org.briarproject.briar.test.BriarIntegrationTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.SIMPLEX_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAgreementPublicKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportProperties;
|
||||
@@ -1009,13 +1008,12 @@ public class IntroductionIntegrationTest
|
||||
// 0 and 1 remove and re-add each other
|
||||
contactManager0.removeContact(contactId1From0);
|
||||
contactManager1.removeContact(contactId0From1);
|
||||
contactId1From0 = contactManager0
|
||||
.addContact(author1, author0.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
SecretKey rootKey0_1 = getSecretKey();
|
||||
contactId1From0 = contactManager0.addContact(author1, author0.getId(),
|
||||
rootKey0_1, clock.currentTimeMillis(), true, true, true);
|
||||
contact1From0 = contactManager0.getContact(contactId1From0);
|
||||
contactId0From1 = contactManager1
|
||||
.addContact(author0, author1.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId0From1 = contactManager1.addContact(author0, author1.getId(),
|
||||
rootKey0_1, clock.currentTimeMillis(), false, true, true);
|
||||
contact0From1 = contactManager1.getContact(contactId0From1);
|
||||
|
||||
// Sync initial client versioning updates and transport properties
|
||||
@@ -1044,8 +1042,7 @@ public class IntroductionIntegrationTest
|
||||
assertTrue(listener1.requestReceived);
|
||||
}
|
||||
|
||||
private void testModifiedResponse(StateVisitor visitor)
|
||||
throws Exception {
|
||||
private void testModifiedResponse(StateVisitor visitor) throws Exception {
|
||||
addListeners(true, true);
|
||||
|
||||
// make introduction
|
||||
@@ -1157,20 +1154,22 @@ public class IntroductionIntegrationTest
|
||||
);
|
||||
}
|
||||
|
||||
private void addTransportProperties()
|
||||
throws DbException, IOException, TimeoutException {
|
||||
private void addTransportProperties() throws Exception {
|
||||
TransportPropertyManager tpm0 = c0.getTransportPropertyManager();
|
||||
TransportPropertyManager tpm1 = c1.getTransportPropertyManager();
|
||||
TransportPropertyManager tpm2 = c2.getTransportPropertyManager();
|
||||
|
||||
tpm0.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2));
|
||||
tpm0.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
|
||||
getTransportProperties(2));
|
||||
sync0To1(1, true);
|
||||
sync0To2(1, true);
|
||||
|
||||
tpm1.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2));
|
||||
tpm1.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
|
||||
getTransportProperties(2));
|
||||
sync1To0(1, true);
|
||||
|
||||
tpm2.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2));
|
||||
tpm2.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
|
||||
getTransportProperties(2));
|
||||
sync2To0(1, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,15 +11,10 @@ import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||
import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent;
|
||||
import org.briarproject.bramble.api.transport.KeyManager;
|
||||
import org.briarproject.bramble.api.transport.StreamContext;
|
||||
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionReader;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionWriter;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
@@ -32,20 +27,15 @@ import org.junit.Test;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.SIMPLEX_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
@@ -55,6 +45,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
private final File testDir = getTestDirectory();
|
||||
private final File aliceDir = new File(testDir, "alice");
|
||||
private final File bobDir = new File(testDir, "bob");
|
||||
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
private final long timestamp = System.currentTimeMillis();
|
||||
|
||||
@@ -90,11 +81,11 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
// Alice sends a private message to Bob
|
||||
sendMessage(alice, bobId);
|
||||
// Sync Alice's client versions and transport properties
|
||||
read(bob, aliceId, write(alice, bobId), 2);
|
||||
read(bob, write(alice, bobId), 2);
|
||||
// Sync Bob's client versions and transport properties
|
||||
read(alice, bobId, write(bob, aliceId), 2);
|
||||
read(alice, write(bob, aliceId), 2);
|
||||
// Sync the private message
|
||||
read(bob, aliceId, write(alice, bobId), 1);
|
||||
read(bob, write(alice, bobId), 1);
|
||||
// Bob should have received the private message
|
||||
assertTrue(listener.messageAdded);
|
||||
}
|
||||
@@ -127,32 +118,17 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
}
|
||||
|
||||
private void read(SimplexMessagingIntegrationTestComponent device,
|
||||
ContactId contactId, byte[] stream, int deliveries)
|
||||
throws Exception {
|
||||
byte[] stream, int deliveries) throws Exception {
|
||||
// Listen for message deliveries
|
||||
MessageDeliveryListener listener =
|
||||
new MessageDeliveryListener(deliveries);
|
||||
device.getEventBus().addListener(listener);
|
||||
// Read and recognise the tag
|
||||
// Read the incoming stream
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(stream);
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
int read = in.read(tag);
|
||||
assertEquals(tag.length, read);
|
||||
KeyManager keyManager = device.getKeyManager();
|
||||
StreamContext ctx = keyManager.getStreamContext(TRANSPORT_ID, tag);
|
||||
assertNotNull(ctx);
|
||||
// Create a stream reader
|
||||
StreamReaderFactory streamReaderFactory =
|
||||
device.getStreamReaderFactory();
|
||||
InputStream streamReader = streamReaderFactory.createStreamReader(
|
||||
in, ctx);
|
||||
// Create an incoming sync session
|
||||
SyncSessionFactory syncSessionFactory = device.getSyncSessionFactory();
|
||||
SyncSession session = syncSessionFactory.createIncomingSession(
|
||||
contactId, streamReader);
|
||||
// Read whatever needs to be read
|
||||
session.run();
|
||||
streamReader.close();
|
||||
TestTransportConnectionReader reader =
|
||||
new TestTransportConnectionReader(in);
|
||||
device.getConnectionManager().manageIncomingConnection(
|
||||
SIMPLEX_TRANSPORT_ID, reader);
|
||||
// Wait for the messages to be delivered
|
||||
assertTrue(listener.delivered.await(TIMEOUT_MS, MILLISECONDS));
|
||||
// Clean up the listener
|
||||
@@ -161,24 +137,14 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
|
||||
private byte[] write(SimplexMessagingIntegrationTestComponent device,
|
||||
ContactId contactId) throws Exception {
|
||||
// Write the outgoing stream
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
// Get a stream context
|
||||
KeyManager keyManager = device.getKeyManager();
|
||||
StreamContext ctx = keyManager.getStreamContext(contactId,
|
||||
TRANSPORT_ID);
|
||||
assertNotNull(ctx);
|
||||
// Create a stream writer
|
||||
StreamWriterFactory streamWriterFactory =
|
||||
device.getStreamWriterFactory();
|
||||
StreamWriter streamWriter =
|
||||
streamWriterFactory.createStreamWriter(out, ctx);
|
||||
// Create an outgoing sync session
|
||||
SyncSessionFactory syncSessionFactory = device.getSyncSessionFactory();
|
||||
SyncSession session = syncSessionFactory.createSimplexOutgoingSession(
|
||||
contactId, MAX_LATENCY, streamWriter);
|
||||
// Write whatever needs to be written
|
||||
session.run();
|
||||
streamWriter.sendEndOfStream();
|
||||
TestTransportConnectionWriter writer =
|
||||
new TestTransportConnectionWriter(out);
|
||||
device.getConnectionManager().manageOutgoingConnection(contactId,
|
||||
SIMPLEX_TRANSPORT_ID, writer);
|
||||
// Wait for the writer to be disposed
|
||||
writer.getDisposedLatch().await(TIMEOUT_MS, MILLISECONDS);
|
||||
// Return the contents of the stream
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -6,10 +6,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||
import org.briarproject.bramble.api.transport.KeyManager;
|
||||
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
@@ -44,15 +41,9 @@ interface SimplexMessagingIntegrationTestComponent
|
||||
|
||||
MessagingManager getMessagingManager();
|
||||
|
||||
KeyManager getKeyManager();
|
||||
|
||||
PrivateMessageFactory getPrivateMessageFactory();
|
||||
|
||||
EventBus getEventBus();
|
||||
|
||||
StreamWriterFactory getStreamWriterFactory();
|
||||
|
||||
StreamReaderFactory getStreamReaderFactory();
|
||||
|
||||
SyncSessionFactory getSyncSessionFactory();
|
||||
ConnectionManager getConnectionManager();
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.data.BdfStringUtils;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
@@ -23,11 +24,10 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||
import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionReader;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionWriter;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.briar.api.blog.BlogFactory;
|
||||
import org.briarproject.briar.api.blog.BlogPostFactory;
|
||||
@@ -43,10 +43,8 @@ import org.junit.Before;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -56,11 +54,12 @@ import javax.inject.Inject;
|
||||
import static java.util.concurrent.Executors.newSingleThreadExecutor;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.INVALID;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.SIMPLEX_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -73,9 +72,12 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
extends BriarTestCase {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BriarIntegrationTest.class.getName());
|
||||
getLogger(BriarIntegrationTest.class.getName());
|
||||
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
protected final static int TIMEOUT = 15000;
|
||||
|
||||
@Nullable
|
||||
protected ContactId contactId1From2, contactId2From1;
|
||||
protected ContactId contactId0From1, contactId0From2, contactId1From0,
|
||||
@@ -91,7 +93,9 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
|
||||
private LifecycleManager lifecycleManager0, lifecycleManager1,
|
||||
lifecycleManager2;
|
||||
private SyncSessionFactory sync0, sync1, sync2;
|
||||
private SecretKey rootKey0_1 = getSecretKey();
|
||||
private SecretKey rootKey0_2 = getSecretKey();
|
||||
private SecretKey rootKey1_2 = getSecretKey();
|
||||
|
||||
@Inject
|
||||
protected Clock clock;
|
||||
@@ -120,7 +124,6 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
private volatile Waiter validationWaiter;
|
||||
private volatile Waiter deliveryWaiter;
|
||||
|
||||
protected final static int TIMEOUT = 15000;
|
||||
protected C c0, c1, c2;
|
||||
|
||||
private final Semaphore messageSemaphore = new Semaphore(0);
|
||||
@@ -151,9 +154,6 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
db0 = c0.getDatabaseComponent();
|
||||
db1 = c1.getDatabaseComponent();
|
||||
db2 = c2.getDatabaseComponent();
|
||||
sync0 = c0.getSyncSessionFactory();
|
||||
sync1 = c1.getSyncSessionFactory();
|
||||
sync2 = c2.getSyncSessionFactory();
|
||||
|
||||
// initialize waiters fresh for each test
|
||||
validationWaiter = new Waiter();
|
||||
@@ -251,21 +251,17 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
}
|
||||
|
||||
protected void addDefaultContacts() throws Exception {
|
||||
contactId1From0 = contactManager0
|
||||
.addContact(author1, author0.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId1From0 = contactManager0.addContact(author1, author0.getId(),
|
||||
rootKey0_1, clock.currentTimeMillis(), true, true, true);
|
||||
contact1From0 = contactManager0.getContact(contactId1From0);
|
||||
contactId0From1 = contactManager1
|
||||
.addContact(author0, author1.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId0From1 = contactManager1.addContact(author0, author1.getId(),
|
||||
rootKey0_1, clock.currentTimeMillis(), false, true, true);
|
||||
contact0From1 = contactManager1.getContact(contactId0From1);
|
||||
contactId2From0 = contactManager0
|
||||
.addContact(author2, author0.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId2From0 = contactManager0.addContact(author2, author0.getId(),
|
||||
rootKey0_2, clock.currentTimeMillis(), true, true, true);
|
||||
contact2From0 = contactManager0.getContact(contactId2From0);
|
||||
contactId0From2 = contactManager2
|
||||
.addContact(author0, author2.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId0From2 = contactManager2.addContact(author0, author2.getId(),
|
||||
rootKey0_2, clock.currentTimeMillis(), false, true, true);
|
||||
contact0From2 = contactManager2.getContact(contactId0From2);
|
||||
|
||||
// Sync initial client versioning updates
|
||||
@@ -283,12 +279,10 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
|
||||
protected void addContacts1And2(boolean haveTransportProperties)
|
||||
throws Exception {
|
||||
contactId2From1 = contactManager1
|
||||
.addContact(author2, author1.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId1From2 = contactManager2
|
||||
.addContact(author1, author2.getId(), getSecretKey(),
|
||||
clock.currentTimeMillis(), true, true, true);
|
||||
contactId2From1 = contactManager1.addContact(author2, author1.getId(),
|
||||
rootKey1_2, clock.currentTimeMillis(), true, true, true);
|
||||
contactId1From2 = contactManager2.addContact(author1, author2.getId(),
|
||||
rootKey1_2, clock.currentTimeMillis(), false, true, true);
|
||||
|
||||
// Sync initial client versioning updates
|
||||
sync1To2(1, true);
|
||||
@@ -322,69 +316,55 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
lifecycleManager2.waitForShutdown();
|
||||
}
|
||||
|
||||
protected void sync0To1(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
syncMessage(sync0, contactId0From1, sync1, contactId1From0, num, valid);
|
||||
protected void sync0To1(int num, boolean valid) throws Exception {
|
||||
syncMessage(c0, c1, contactId1From0, num, valid);
|
||||
}
|
||||
|
||||
protected void sync0To2(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
syncMessage(sync0, contactId0From2, sync2, contactId2From0, num, valid);
|
||||
protected void sync0To2(int num, boolean valid) throws Exception {
|
||||
syncMessage(c0, c2, contactId2From0, num, valid);
|
||||
}
|
||||
|
||||
protected void sync1To0(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
syncMessage(sync1, contactId1From0, sync0, contactId0From1, num, valid);
|
||||
protected void sync1To0(int num, boolean valid) throws Exception {
|
||||
syncMessage(c1, c0, contactId0From1, num, valid);
|
||||
}
|
||||
|
||||
protected void sync2To0(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
syncMessage(sync2, contactId2From0, sync0, contactId0From2, num, valid);
|
||||
protected void sync2To0(int num, boolean valid) throws Exception {
|
||||
syncMessage(c2, c0, contactId0From2, num, valid);
|
||||
}
|
||||
|
||||
protected void sync2To1(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
assertNotNull(contactId2From1);
|
||||
protected void sync2To1(int num, boolean valid) throws Exception {
|
||||
assertNotNull(contactId1From2);
|
||||
syncMessage(sync2, contactId2From1, sync1, contactId1From2, num, valid);
|
||||
syncMessage(c2, c1, contactId1From2, num, valid);
|
||||
}
|
||||
|
||||
protected void sync1To2(int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
protected void sync1To2(int num, boolean valid) throws Exception {
|
||||
assertNotNull(contactId2From1);
|
||||
assertNotNull(contactId1From2);
|
||||
syncMessage(sync1, contactId1From2, sync2, contactId2From1, num, valid);
|
||||
syncMessage(c1, c2, contactId2From1, num, valid);
|
||||
}
|
||||
|
||||
private void syncMessage(SyncSessionFactory fromSync, ContactId fromId,
|
||||
SyncSessionFactory toSync, ContactId toId, int num, boolean valid)
|
||||
throws IOException, TimeoutException {
|
||||
private void syncMessage(BriarIntegrationTestComponent fromComponent,
|
||||
BriarIntegrationTestComponent toComponent, ContactId toId, int num,
|
||||
boolean valid) throws Exception {
|
||||
|
||||
// Debug output
|
||||
String from = "0";
|
||||
if (fromSync == sync1) from = "1";
|
||||
else if (fromSync == sync2) from = "2";
|
||||
String to = "0";
|
||||
if (toSync == sync1) to = "1";
|
||||
else if (toSync == sync2) to = "2";
|
||||
String from =
|
||||
fromComponent.getIdentityManager().getLocalAuthor().getName();
|
||||
String to = toComponent.getIdentityManager().getLocalAuthor().getName();
|
||||
LOG.info("TEST: Sending " + num + " message(s) from " + from + " to " +
|
||||
to);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamWriter streamWriter = new TestStreamWriter(out);
|
||||
// Create an outgoing sync session
|
||||
SyncSession sessionFrom = fromSync.createSimplexOutgoingSession(toId,
|
||||
MAX_LATENCY, streamWriter);
|
||||
// Write whatever needs to be written
|
||||
sessionFrom.run();
|
||||
out.close();
|
||||
TestTransportConnectionWriter writer =
|
||||
new TestTransportConnectionWriter(out);
|
||||
fromComponent.getConnectionManager().manageOutgoingConnection(toId,
|
||||
SIMPLEX_TRANSPORT_ID, writer);
|
||||
writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Create an incoming sync session
|
||||
SyncSession sessionTo = toSync.createIncomingSession(fromId, in);
|
||||
// Read whatever needs to be read
|
||||
sessionTo.run();
|
||||
in.close();
|
||||
TestTransportConnectionReader reader =
|
||||
new TestTransportConnectionReader(in);
|
||||
toComponent.getConnectionManager().manageIncomingConnection(
|
||||
SIMPLEX_TRANSPORT_ID, reader);
|
||||
|
||||
if (valid) {
|
||||
deliveryWaiter.await(TIMEOUT, num);
|
||||
|
||||
@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||
import org.briarproject.briar.api.blog.BlogFactory;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
@@ -87,8 +87,6 @@ public interface BriarIntegrationTestComponent
|
||||
|
||||
ContactManager getContactManager();
|
||||
|
||||
SyncSessionFactory getSyncSessionFactory();
|
||||
|
||||
DatabaseComponent getDatabaseComponent();
|
||||
|
||||
BlogManager getBlogManager();
|
||||
@@ -112,4 +110,6 @@ public interface BriarIntegrationTestComponent
|
||||
AuthorFactory getAuthorFactory();
|
||||
|
||||
BlogFactory getBlogFactory();
|
||||
|
||||
ConnectionManager getConnectionManager();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user