mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Test stream reading and writing at a higher level.
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
disposed.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,7 +42,7 @@ import java.util.Collection;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeoutException;
|
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;
|
||||||
@@ -1163,14 +1163,14 @@ public class IntroductionIntegrationTest
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user