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:
Torsten Grote
2019-06-04 10:37:31 +00:00
10 changed files with 252 additions and 181 deletions

View File

@@ -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();
}
}

View File

@@ -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};
}
}

View File

@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.PluginCallback; import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.plugin.PluginConfig; import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.TransportId; 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.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin; import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory; import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
@@ -15,22 +16,22 @@ import javax.annotation.Nullable;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static org.briarproject.bramble.test.TestUtils.getTransportId; import static org.briarproject.bramble.test.TestUtils.getTransportId;
@Module @Module
public class TestPluginConfigModule { public class TestPluginConfigModule {
public static final TransportId TRANSPORT_ID = getTransportId(); public static final TransportId SIMPLEX_TRANSPORT_ID = getTransportId();
public static final int MAX_LATENCY = 2 * 60 * 1000; // 2 minutes public static final TransportId DUPLEX_TRANSPORT_ID = getTransportId();
public static final int MAX_LATENCY = 30_000; // 30 seconds
@NotNullByDefault @NotNullByDefault
private final SimplexPluginFactory simplex = new SimplexPluginFactory() { private final SimplexPluginFactory simplex = new SimplexPluginFactory() {
@Override @Override
public TransportId getId() { public TransportId getId() {
return TRANSPORT_ID; return SIMPLEX_TRANSPORT_ID;
} }
@Override @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 @Provides
PluginConfig providePluginConfig() { PluginConfig providePluginConfig() {
@NotNullByDefault @NotNullByDefault
@@ -52,7 +73,7 @@ public class TestPluginConfigModule {
@Override @Override
public Collection<DuplexPluginFactory> getDuplexFactories() { public Collection<DuplexPluginFactory> getDuplexFactories() {
return emptyList(); return singletonList(duplex);
} }
@Override @Override

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.Contact; 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.contact.event.ContactAddedEvent; 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.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry; import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.data.BdfList; import org.briarproject.bramble.api.data.BdfList;
@@ -37,12 +38,10 @@ import org.briarproject.briar.test.BriarIntegrationTest;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Map; 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.getAgreementPublicKey;
import static org.briarproject.bramble.test.TestUtils.getSecretKey; import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.test.TestUtils.getTransportProperties; import static org.briarproject.bramble.test.TestUtils.getTransportProperties;
@@ -1009,13 +1008,12 @@ public class IntroductionIntegrationTest
// 0 and 1 remove and re-add each other // 0 and 1 remove and re-add each other
contactManager0.removeContact(contactId1From0); contactManager0.removeContact(contactId1From0);
contactManager1.removeContact(contactId0From1); contactManager1.removeContact(contactId0From1);
contactId1From0 = contactManager0 SecretKey rootKey0_1 = getSecretKey();
.addContact(author1, author0.getId(), getSecretKey(), contactId1From0 = contactManager0.addContact(author1, author0.getId(),
clock.currentTimeMillis(), true, true, true); rootKey0_1, clock.currentTimeMillis(), true, true, true);
contact1From0 = contactManager0.getContact(contactId1From0); contact1From0 = contactManager0.getContact(contactId1From0);
contactId0From1 = contactManager1 contactId0From1 = contactManager1.addContact(author0, author1.getId(),
.addContact(author0, author1.getId(), getSecretKey(), rootKey0_1, clock.currentTimeMillis(), false, true, true);
clock.currentTimeMillis(), true, true, true);
contact0From1 = contactManager1.getContact(contactId0From1); contact0From1 = contactManager1.getContact(contactId0From1);
// Sync initial client versioning updates and transport properties // Sync initial client versioning updates and transport properties
@@ -1044,8 +1042,7 @@ public class IntroductionIntegrationTest
assertTrue(listener1.requestReceived); assertTrue(listener1.requestReceived);
} }
private void testModifiedResponse(StateVisitor visitor) private void testModifiedResponse(StateVisitor visitor) throws Exception {
throws Exception {
addListeners(true, true); addListeners(true, true);
// make introduction // make introduction
@@ -1157,20 +1154,22 @@ public class IntroductionIntegrationTest
); );
} }
private void addTransportProperties() private void addTransportProperties() throws Exception {
throws DbException, IOException, TimeoutException {
TransportPropertyManager tpm0 = c0.getTransportPropertyManager(); TransportPropertyManager tpm0 = c0.getTransportPropertyManager();
TransportPropertyManager tpm1 = c1.getTransportPropertyManager(); TransportPropertyManager tpm1 = c1.getTransportPropertyManager();
TransportPropertyManager tpm2 = c2.getTransportPropertyManager(); TransportPropertyManager tpm2 = c2.getTransportPropertyManager();
tpm0.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2)); tpm0.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
getTransportProperties(2));
sync0To1(1, true); sync0To1(1, true);
sync0To2(1, true); sync0To2(1, true);
tpm1.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2)); tpm1.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
getTransportProperties(2));
sync1To0(1, true); sync1To0(1, true);
tpm2.mergeLocalProperties(TRANSPORT_ID, getTransportProperties(2)); tpm2.mergeLocalProperties(SIMPLEX_TRANSPORT_ID,
getTransportProperties(2));
sync2To0(1, true); sync2To0(1, true);
} }

View File

@@ -11,15 +11,10 @@ import org.briarproject.bramble.api.identity.IdentityManager;
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;
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.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.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.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessage; import org.briarproject.briar.api.messaging.PrivateMessage;
import org.briarproject.briar.api.messaging.PrivateMessageFactory; import org.briarproject.briar.api.messaging.PrivateMessageFactory;
@@ -32,20 +27,15 @@ import org.junit.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
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.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.bramble.test.TestPluginConfigModule.SIMPLEX_TRANSPORT_ID;
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory; import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SimplexMessagingIntegrationTest extends BriarTestCase { public class SimplexMessagingIntegrationTest extends BriarTestCase {
@@ -55,6 +45,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private final File testDir = getTestDirectory(); private final File testDir = getTestDirectory();
private final File aliceDir = new File(testDir, "alice"); private final File aliceDir = new File(testDir, "alice");
private final File bobDir = new File(testDir, "bob"); private final File bobDir = new File(testDir, "bob");
private final SecretKey rootKey = getSecretKey(); private final SecretKey rootKey = getSecretKey();
private final long timestamp = System.currentTimeMillis(); private final long timestamp = System.currentTimeMillis();
@@ -90,11 +81,11 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Alice sends a private message to Bob // Alice sends a private message to Bob
sendMessage(alice, bobId); sendMessage(alice, bobId);
// Sync Alice's client versions and transport properties // 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 // 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 // Sync the private message
read(bob, aliceId, write(alice, bobId), 1); read(bob, write(alice, bobId), 1);
// Bob should have received the private message // Bob should have received the private message
assertTrue(listener.messageAdded); assertTrue(listener.messageAdded);
} }
@@ -127,32 +118,17 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
} }
private void read(SimplexMessagingIntegrationTestComponent device, private void read(SimplexMessagingIntegrationTestComponent device,
ContactId contactId, byte[] stream, int deliveries) byte[] stream, int deliveries) throws Exception {
throws Exception {
// Listen for message deliveries // Listen for message deliveries
MessageDeliveryListener listener = MessageDeliveryListener listener =
new MessageDeliveryListener(deliveries); new MessageDeliveryListener(deliveries);
device.getEventBus().addListener(listener); device.getEventBus().addListener(listener);
// Read and recognise the tag // Read the incoming stream
ByteArrayInputStream in = new ByteArrayInputStream(stream); ByteArrayInputStream in = new ByteArrayInputStream(stream);
byte[] tag = new byte[TAG_LENGTH]; TestTransportConnectionReader reader =
int read = in.read(tag); new TestTransportConnectionReader(in);
assertEquals(tag.length, read); device.getConnectionManager().manageIncomingConnection(
KeyManager keyManager = device.getKeyManager(); SIMPLEX_TRANSPORT_ID, reader);
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();
// Wait for the messages to be delivered // Wait for the messages to be delivered
assertTrue(listener.delivered.await(TIMEOUT_MS, MILLISECONDS)); assertTrue(listener.delivered.await(TIMEOUT_MS, MILLISECONDS));
// Clean up the listener // Clean up the listener
@@ -161,24 +137,14 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private byte[] write(SimplexMessagingIntegrationTestComponent device, private byte[] write(SimplexMessagingIntegrationTestComponent device,
ContactId contactId) throws Exception { ContactId contactId) throws Exception {
// Write the outgoing stream
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
// Get a stream context TestTransportConnectionWriter writer =
KeyManager keyManager = device.getKeyManager(); new TestTransportConnectionWriter(out);
StreamContext ctx = keyManager.getStreamContext(contactId, device.getConnectionManager().manageOutgoingConnection(contactId,
TRANSPORT_ID); SIMPLEX_TRANSPORT_ID, writer);
assertNotNull(ctx); // Wait for the writer to be disposed
// Create a stream writer writer.getDisposedLatch().await(TIMEOUT_MS, MILLISECONDS);
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();
// Return the contents of the stream // Return the contents of the stream
return out.toByteArray(); return out.toByteArray();
} }

View File

@@ -6,10 +6,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.sync.SyncSessionFactory; import org.briarproject.bramble.api.plugin.ConnectionManager;
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.test.BrambleCoreIntegrationTestModule; import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
import org.briarproject.briar.api.messaging.MessagingManager; import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessageFactory; import org.briarproject.briar.api.messaging.PrivateMessageFactory;
@@ -44,15 +41,9 @@ interface SimplexMessagingIntegrationTestComponent
MessagingManager getMessagingManager(); MessagingManager getMessagingManager();
KeyManager getKeyManager();
PrivateMessageFactory getPrivateMessageFactory(); PrivateMessageFactory getPrivateMessageFactory();
EventBus getEventBus(); EventBus getEventBus();
StreamWriterFactory getStreamWriterFactory(); ConnectionManager getConnectionManager();
StreamReaderFactory getStreamReaderFactory();
SyncSessionFactory getSyncSessionFactory();
} }

View File

@@ -9,6 +9,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.contact.ContactManager; import org.briarproject.bramble.api.contact.ContactManager;
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.data.BdfList; import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.data.BdfStringUtils; import org.briarproject.bramble.api.data.BdfStringUtils;
import org.briarproject.bramble.api.db.DatabaseComponent; 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.nullsafety.ParametersNotNullByDefault;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId; 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.sync.event.MessageStateChangedEvent;
import org.briarproject.bramble.api.system.Clock; 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.bramble.test.TestUtils;
import org.briarproject.briar.api.blog.BlogFactory; import org.briarproject.briar.api.blog.BlogFactory;
import org.briarproject.briar.api.blog.BlogPostFactory; import org.briarproject.briar.api.blog.BlogPostFactory;
@@ -43,10 +43,8 @@ import org.junit.Before;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger; 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.Executors.newSingleThreadExecutor;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static junit.framework.Assert.assertNotNull; 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.DELIVERED;
import static org.briarproject.bramble.api.sync.validation.MessageState.INVALID; 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.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.test.TestUtils.getSecretKey;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -73,9 +72,12 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
extends BriarTestCase { extends BriarTestCase {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(BriarIntegrationTest.class.getName()); getLogger(BriarIntegrationTest.class.getName());
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
protected final static int TIMEOUT = 15000;
@Nullable @Nullable
protected ContactId contactId1From2, contactId2From1; protected ContactId contactId1From2, contactId2From1;
protected ContactId contactId0From1, contactId0From2, contactId1From0, protected ContactId contactId0From1, contactId0From2, contactId1From0,
@@ -91,7 +93,9 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
private LifecycleManager lifecycleManager0, lifecycleManager1, private LifecycleManager lifecycleManager0, lifecycleManager1,
lifecycleManager2; lifecycleManager2;
private SyncSessionFactory sync0, sync1, sync2; private SecretKey rootKey0_1 = getSecretKey();
private SecretKey rootKey0_2 = getSecretKey();
private SecretKey rootKey1_2 = getSecretKey();
@Inject @Inject
protected Clock clock; protected Clock clock;
@@ -120,7 +124,6 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
private volatile Waiter validationWaiter; private volatile Waiter validationWaiter;
private volatile Waiter deliveryWaiter; private volatile Waiter deliveryWaiter;
protected final static int TIMEOUT = 15000;
protected C c0, c1, c2; protected C c0, c1, c2;
private final Semaphore messageSemaphore = new Semaphore(0); private final Semaphore messageSemaphore = new Semaphore(0);
@@ -151,9 +154,6 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
db0 = c0.getDatabaseComponent(); db0 = c0.getDatabaseComponent();
db1 = c1.getDatabaseComponent(); db1 = c1.getDatabaseComponent();
db2 = c2.getDatabaseComponent(); db2 = c2.getDatabaseComponent();
sync0 = c0.getSyncSessionFactory();
sync1 = c1.getSyncSessionFactory();
sync2 = c2.getSyncSessionFactory();
// initialize waiters fresh for each test // initialize waiters fresh for each test
validationWaiter = new Waiter(); validationWaiter = new Waiter();
@@ -251,21 +251,17 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
} }
protected void addDefaultContacts() throws Exception { protected void addDefaultContacts() throws Exception {
contactId1From0 = contactManager0 contactId1From0 = contactManager0.addContact(author1, author0.getId(),
.addContact(author1, author0.getId(), getSecretKey(), rootKey0_1, clock.currentTimeMillis(), true, true, true);
clock.currentTimeMillis(), true, true, true);
contact1From0 = contactManager0.getContact(contactId1From0); contact1From0 = contactManager0.getContact(contactId1From0);
contactId0From1 = contactManager1 contactId0From1 = contactManager1.addContact(author0, author1.getId(),
.addContact(author0, author1.getId(), getSecretKey(), rootKey0_1, clock.currentTimeMillis(), false, true, true);
clock.currentTimeMillis(), true, true, true);
contact0From1 = contactManager1.getContact(contactId0From1); contact0From1 = contactManager1.getContact(contactId0From1);
contactId2From0 = contactManager0 contactId2From0 = contactManager0.addContact(author2, author0.getId(),
.addContact(author2, author0.getId(), getSecretKey(), rootKey0_2, clock.currentTimeMillis(), true, true, true);
clock.currentTimeMillis(), true, true, true);
contact2From0 = contactManager0.getContact(contactId2From0); contact2From0 = contactManager0.getContact(contactId2From0);
contactId0From2 = contactManager2 contactId0From2 = contactManager2.addContact(author0, author2.getId(),
.addContact(author0, author2.getId(), getSecretKey(), rootKey0_2, clock.currentTimeMillis(), false, true, true);
clock.currentTimeMillis(), true, true, true);
contact0From2 = contactManager2.getContact(contactId0From2); contact0From2 = contactManager2.getContact(contactId0From2);
// Sync initial client versioning updates // Sync initial client versioning updates
@@ -283,12 +279,10 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
protected void addContacts1And2(boolean haveTransportProperties) protected void addContacts1And2(boolean haveTransportProperties)
throws Exception { throws Exception {
contactId2From1 = contactManager1 contactId2From1 = contactManager1.addContact(author2, author1.getId(),
.addContact(author2, author1.getId(), getSecretKey(), rootKey1_2, clock.currentTimeMillis(), true, true, true);
clock.currentTimeMillis(), true, true, true); contactId1From2 = contactManager2.addContact(author1, author2.getId(),
contactId1From2 = contactManager2 rootKey1_2, clock.currentTimeMillis(), false, true, true);
.addContact(author1, author2.getId(), getSecretKey(),
clock.currentTimeMillis(), true, true, true);
// Sync initial client versioning updates // Sync initial client versioning updates
sync1To2(1, true); sync1To2(1, true);
@@ -322,69 +316,55 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
lifecycleManager2.waitForShutdown(); lifecycleManager2.waitForShutdown();
} }
protected void sync0To1(int num, boolean valid) protected void sync0To1(int num, boolean valid) throws Exception {
throws IOException, TimeoutException { syncMessage(c0, c1, contactId1From0, num, valid);
syncMessage(sync0, contactId0From1, sync1, contactId1From0, num, valid);
} }
protected void sync0To2(int num, boolean valid) protected void sync0To2(int num, boolean valid) throws Exception {
throws IOException, TimeoutException { syncMessage(c0, c2, contactId2From0, num, valid);
syncMessage(sync0, contactId0From2, sync2, contactId2From0, num, valid);
} }
protected void sync1To0(int num, boolean valid) protected void sync1To0(int num, boolean valid) throws Exception {
throws IOException, TimeoutException { syncMessage(c1, c0, contactId0From1, num, valid);
syncMessage(sync1, contactId1From0, sync0, contactId0From1, num, valid);
} }
protected void sync2To0(int num, boolean valid) protected void sync2To0(int num, boolean valid) throws Exception {
throws IOException, TimeoutException { syncMessage(c2, c0, contactId0From2, num, valid);
syncMessage(sync2, contactId2From0, sync0, contactId0From2, num, valid);
} }
protected void sync2To1(int num, boolean valid) protected void sync2To1(int num, boolean valid) throws Exception {
throws IOException, TimeoutException {
assertNotNull(contactId2From1);
assertNotNull(contactId1From2); assertNotNull(contactId1From2);
syncMessage(sync2, contactId2From1, sync1, contactId1From2, num, valid); syncMessage(c2, c1, contactId1From2, num, valid);
} }
protected void sync1To2(int num, boolean valid) protected void sync1To2(int num, boolean valid) throws Exception {
throws IOException, TimeoutException {
assertNotNull(contactId2From1); assertNotNull(contactId2From1);
assertNotNull(contactId1From2); syncMessage(c1, c2, contactId2From1, num, valid);
syncMessage(sync1, contactId1From2, sync2, contactId2From1, num, valid);
} }
private void syncMessage(SyncSessionFactory fromSync, ContactId fromId, private void syncMessage(BriarIntegrationTestComponent fromComponent,
SyncSessionFactory toSync, ContactId toId, int num, boolean valid) BriarIntegrationTestComponent toComponent, ContactId toId, int num,
throws IOException, TimeoutException { boolean valid) throws Exception {
// Debug output // Debug output
String from = "0"; String from =
if (fromSync == sync1) from = "1"; fromComponent.getIdentityManager().getLocalAuthor().getName();
else if (fromSync == sync2) from = "2"; String to = toComponent.getIdentityManager().getLocalAuthor().getName();
String to = "0";
if (toSync == sync1) to = "1";
else if (toSync == sync2) to = "2";
LOG.info("TEST: Sending " + num + " message(s) from " + from + " to " + LOG.info("TEST: Sending " + num + " message(s) from " + from + " to " +
to); to);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamWriter streamWriter = new TestStreamWriter(out); TestTransportConnectionWriter writer =
// Create an outgoing sync session new TestTransportConnectionWriter(out);
SyncSession sessionFrom = fromSync.createSimplexOutgoingSession(toId, fromComponent.getConnectionManager().manageOutgoingConnection(toId,
MAX_LATENCY, streamWriter); SIMPLEX_TRANSPORT_ID, writer);
// Write whatever needs to be written writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);
sessionFrom.run();
out.close();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Create an incoming sync session TestTransportConnectionReader reader =
SyncSession sessionTo = toSync.createIncomingSession(fromId, in); new TestTransportConnectionReader(in);
// Read whatever needs to be read toComponent.getConnectionManager().manageIncomingConnection(
sessionTo.run(); SIMPLEX_TRANSPORT_ID, reader);
in.close();
if (valid) { if (valid) {
deliveryWaiter.await(TIMEOUT, num); deliveryWaiter.await(TIMEOUT, num);

View File

@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.event.EventBus;
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.lifecycle.LifecycleManager; 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.properties.TransportPropertyManager;
import org.briarproject.bramble.api.sync.SyncSessionFactory;
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule; import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
import org.briarproject.briar.api.blog.BlogFactory; import org.briarproject.briar.api.blog.BlogFactory;
import org.briarproject.briar.api.blog.BlogManager; import org.briarproject.briar.api.blog.BlogManager;
@@ -87,8 +87,6 @@ public interface BriarIntegrationTestComponent
ContactManager getContactManager(); ContactManager getContactManager();
SyncSessionFactory getSyncSessionFactory();
DatabaseComponent getDatabaseComponent(); DatabaseComponent getDatabaseComponent();
BlogManager getBlogManager(); BlogManager getBlogManager();
@@ -112,4 +110,6 @@ public interface BriarIntegrationTestComponent
AuthorFactory getAuthorFactory(); AuthorFactory getAuthorFactory();
BlogFactory getBlogFactory(); BlogFactory getBlogFactory();
ConnectionManager getConnectionManager();
} }