Factored out StreamReader/Writer from messaging layer.

This commit is contained in:
akwizgran
2014-11-05 19:22:01 +00:00
parent dfa4860200
commit 26d93b83b4
10 changed files with 106 additions and 225 deletions

View File

@@ -2,6 +2,7 @@ package org.briarproject.messaging;
import static org.briarproject.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
@@ -35,7 +36,9 @@ import org.briarproject.api.messaging.PacketReaderFactory;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.api.transport.Endpoint;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReader;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriter;
import org.briarproject.api.transport.StreamWriterFactory;
import org.briarproject.api.transport.TagRecogniser;
import org.briarproject.crypto.CryptoModule;
@@ -137,22 +140,22 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
Message message = messageFactory.createAnonymousMessage(null, group,
contentType, timestamp, body);
db.addLocalMessage(message);
// Create an outgoing messaging session
// Create a stream writer
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamWriterFactory streamWriterFactory =
alice.getInstance(StreamWriterFactory.class);
PacketWriterFactory packetWriterFactory =
alice.getInstance(PacketWriterFactory.class);
TestTransportConnectionWriter transport =
new TestTransportConnectionWriter(out);
StreamContext ctx = km.getStreamContext(contactId, transportId);
assertNotNull(ctx);
StreamWriter streamWriter = streamWriterFactory.createStreamWriter(out,
MAX_FRAME_LENGTH, ctx);
// Create an outgoing messaging session
PacketWriterFactory packetWriterFactory =
alice.getInstance(PacketWriterFactory.class);
MessagingSession session = new SinglePassOutgoingSession(db,
new ImmediateExecutor(), streamWriterFactory,
packetWriterFactory, ctx, transport);
new ImmediateExecutor(), packetWriterFactory, contactId,
Long.MAX_VALUE, streamWriter.getOutputStream());
// Write whatever needs to be written
session.run();
transport.dispose(false);
// Clean up
km.stop();
db.close();
@@ -198,24 +201,24 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
assertEquals(tag.length, read);
StreamContext ctx = rec.recogniseTag(transportId, tag);
assertNotNull(ctx);
// Create a stream reader
StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class);
StreamReader streamReader = streamReaderFactory.createStreamReader(in,
MAX_FRAME_LENGTH, ctx);
// Create an incoming messaging session
MessageVerifier messageVerifier =
bob.getInstance(MessageVerifier.class);
StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class);
PacketReaderFactory packetReaderFactory =
bob.getInstance(PacketReaderFactory.class);
TestTransportConnectionReader transport =
new TestTransportConnectionReader(in);
MessagingSession session = new IncomingSession(db,
new ImmediateExecutor(), new ImmediateExecutor(),
messageVerifier, streamReaderFactory, packetReaderFactory,
ctx, transport);
messageVerifier, packetReaderFactory, contactId,
streamReader.getInputStream());
// No messages should have been added yet
assertFalse(listener.messageAdded);
// Read whatever needs to be read
session.run();
transport.dispose(false, true);
// The private message from Alice should have been added
assertTrue(listener.messageAdded);
// Clean up

View File

@@ -1,9 +1,5 @@
package org.briarproject.messaging;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Random;
@@ -15,7 +11,6 @@ import org.briarproject.TestLifecycleModule;
import org.briarproject.TestSystemModule;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.UniqueId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DatabaseExecutor;
@@ -23,8 +18,6 @@ import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.MessagingSession;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamWriterFactory;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.event.EventModule;
import org.briarproject.serial.SerialModule;
@@ -45,11 +38,9 @@ public class SinglePassOutgoingSessionTest extends BriarTestCase {
private final Mockery context;
private final DatabaseComponent db;
private final Executor dbExecutor;
private final StreamWriterFactory streamWriterFactory;
private final PacketWriterFactory packetWriterFactory;
private final ContactId contactId;
private final MessageId messageId;
private final TransportId transportId;
private final byte[] secret;
public SinglePassOutgoingSessionTest() {
@@ -68,11 +59,9 @@ public class SinglePassOutgoingSessionTest extends BriarTestCase {
new TestLifecycleModule(), new TestSystemModule(),
new CryptoModule(), new EventModule(), new MessagingModule(),
new SerialModule(), new TransportModule());
streamWriterFactory = i.getInstance(StreamWriterFactory.class);
packetWriterFactory = i.getInstance(PacketWriterFactory.class);
contactId = new ContactId(234);
messageId = new MessageId(TestUtils.getRandomId());
transportId = new TransportId("id");
secret = new byte[32];
new Random().nextBytes(secret);
}
@@ -80,12 +69,8 @@ public class SinglePassOutgoingSessionTest extends BriarTestCase {
@Test
public void testNothingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestTransportConnectionWriter writer =
new TestTransportConnectionWriter(out);
StreamContext ctx = new StreamContext(contactId, transportId,
secret, 0, true);
MessagingSession session = new SinglePassOutgoingSession(db, dbExecutor,
streamWriterFactory, packetWriterFactory, ctx, writer);
packetWriterFactory, contactId, Long.MAX_VALUE, out);
context.checking(new Expectations() {{
// No transport acks to send
oneOf(db).generateTransportAcks(contactId);
@@ -117,21 +102,16 @@ public class SinglePassOutgoingSessionTest extends BriarTestCase {
will(returnValue(null));
}});
session.run();
// Only the tag and an empty final frame should have been written
assertEquals(TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH, out.size());
// Nothing should have been written
assertEquals(0, out.size());
context.assertIsSatisfied();
}
@Test
public void testSomethingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestTransportConnectionWriter writer =
new TestTransportConnectionWriter(out);
StreamContext ctx = new StreamContext(contactId, transportId,
secret, 0, true);
MessagingSession session = new SinglePassOutgoingSession(db, dbExecutor,
streamWriterFactory, packetWriterFactory,
ctx, writer);
packetWriterFactory, contactId, Long.MAX_VALUE, out);
final byte[] raw = new byte[1234];
context.checking(new Expectations() {{
// No transport acks to send
@@ -172,8 +152,7 @@ public class SinglePassOutgoingSessionTest extends BriarTestCase {
}});
session.run();
// Something should have been written
int overhead = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH;
assertTrue(out.size() > overhead + UniqueId.LENGTH + raw.length);
assertTrue(out.size() > UniqueId.LENGTH + raw.length);
context.assertIsSatisfied();
}
}

View File

@@ -1,35 +0,0 @@
package org.briarproject.messaging;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.InputStream;
import org.briarproject.api.plugins.TransportConnectionReader;
class TestTransportConnectionReader implements TransportConnectionReader {
private final InputStream in;
private boolean disposed = false;
TestTransportConnectionReader(InputStream in) {
this.in = in;
}
public int getMaxFrameLength() {
return MAX_FRAME_LENGTH;
}
public long getMaxLatency() {
return Long.MAX_VALUE;
}
public InputStream getInputStream() {
return in;
}
public void dispose(boolean exception, boolean recognised) {
assert !disposed;
disposed = true;
}
}

View File

@@ -1,40 +0,0 @@
package org.briarproject.messaging;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.briarproject.api.plugins.TransportConnectionWriter;
class TestTransportConnectionWriter implements TransportConnectionWriter {
private final ByteArrayOutputStream out;
private boolean disposed = false;
TestTransportConnectionWriter(ByteArrayOutputStream out) {
this.out = out;
}
public long getCapacity() {
return Long.MAX_VALUE;
}
public int getMaxFrameLength() {
return MAX_FRAME_LENGTH;
}
public long getMaxLatency() {
return Long.MAX_VALUE;
}
public OutputStream getOutputStream() {
return out;
}
public void dispose(boolean exception) {
assert !disposed;
disposed = true;
}
}