Use the same maximum frame length for all transports.

This commit is contained in:
akwizgran
2015-01-05 16:24:44 +00:00
parent 358166bc12
commit d3bf2d59a1
60 changed files with 194 additions and 321 deletions

View File

@@ -69,7 +69,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private final SecureRandom secureRandom;
private final Clock clock;
private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, pollingInterval;
private final int maxLatency, pollingInterval;
private volatile boolean running = false;
private volatile boolean wasDisabled = false;
@@ -81,7 +81,7 @@ class DroidtoothPlugin implements DuplexPlugin {
DroidtoothPlugin(Executor ioExecutor, AndroidExecutor androidExecutor,
Context appContext, SecureRandom secureRandom, Clock clock,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency,
DuplexPluginCallback callback, int maxLatency,
int pollingInterval) {
this.ioExecutor = ioExecutor;
this.androidExecutor = androidExecutor;
@@ -89,7 +89,6 @@ class DroidtoothPlugin implements DuplexPlugin {
this.secureRandom = secureRandom;
this.clock = clock;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
@@ -98,10 +97,6 @@ class DroidtoothPlugin implements DuplexPlugin {
return ID;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -15,7 +15,6 @@ import android.content.Context;
public class DroidtoothPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -41,7 +40,6 @@ public class DroidtoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new DroidtoothPlugin(ioExecutor, androidExecutor, appContext,
secureRandom, clock, callback, MAX_FRAME_LENGTH, MAX_LATENCY,
POLLING_INTERVAL);
secureRandom, clock, callback, MAX_LATENCY, POLLING_INTERVAL);
}
}

View File

@@ -39,10 +39,6 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
@@ -60,10 +56,6 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -26,10 +26,9 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
private volatile BroadcastReceiver networkStateReceiver = null;
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency,
DuplexPluginCallback callback, int maxLatency,
int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime,
pollingInterval);
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
this.appContext = appContext;
}

View File

@@ -11,7 +11,6 @@ import android.content.Context;
public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -30,6 +29,6 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new AndroidLanTcpPlugin(ioExecutor, appContext, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -75,8 +75,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
private final Context appContext;
private final LocationUtils locationUtils;
private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, maxIdleTime, pollingInterval;
private final int socketTimeout;
private final int maxLatency, maxIdleTime, pollingInterval, socketTimeout;
private final File torDirectory, torFile, geoIpFile, configFile, doneFile;
private final File cookieFile, hostnameFile;
private final AtomicBoolean circuitBuilt;
@@ -90,13 +89,11 @@ class TorPlugin implements DuplexPlugin, EventHandler {
TorPlugin(Executor ioExecutor, Context appContext,
LocationUtils locationUtils, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime,
int pollingInterval) {
int maxLatency, int maxIdleTime, int pollingInterval) {
this.ioExecutor = ioExecutor;
this.appContext = appContext;
this.locationUtils = locationUtils;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval;
@@ -117,10 +114,6 @@ class TorPlugin implements DuplexPlugin, EventHandler {
return ID;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -17,7 +17,6 @@ public class TorPluginFactory implements DuplexPluginFactory {
private static final Logger LOG =
Logger.getLogger(TorPluginFactory.class.getName());
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -44,6 +43,6 @@ public class TorPluginFactory implements DuplexPluginFactory {
return null;
}
return new TorPlugin(ioExecutor,appContext, locationUtils, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -38,10 +38,6 @@ class TorTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
@@ -59,10 +55,6 @@ class TorTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -2,7 +2,7 @@ package org.briarproject.api.crypto;
import java.security.GeneralSecurityException;
/** An authenticated cipher that support additional authenticated data. */
/** An authenticated cipher that supports additional authenticated data. */
public interface AuthenticatedCipher {
/**

View File

@@ -7,12 +7,11 @@ import org.briarproject.api.transport.StreamContext;
public interface StreamDecrypterFactory {
/** Creates a {@link StreamDecrypter} for decrypting a transport stream. */
StreamDecrypter createStreamDecrypter(InputStream in, int maxFrameLength,
StreamContext ctx);
StreamDecrypter createStreamDecrypter(InputStream in, StreamContext ctx);
/**
* Creates a {@link StreamDecrypter} for decrypting an invitation stream.
*/
StreamDecrypter createInvitationStreamDecrypter(InputStream in,
int maxFrameLength, byte[] secret, boolean alice);
byte[] secret, boolean alice);
}

View File

@@ -7,12 +7,11 @@ import org.briarproject.api.transport.StreamContext;
public interface StreamEncrypterFactory {
/** Creates a {@link StreamEncrypter} for encrypting a transport stream. */
StreamEncrypter createStreamEncrypter(OutputStream out,
int maxFrameLength, StreamContext ctx);
StreamEncrypter createStreamEncrypter(OutputStream out, StreamContext ctx);
/**
* Creates a {@link StreamEncrypter} for encrypting an invitation stream.
*/
StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice);
byte[] secret, boolean alice);
}

View File

@@ -11,9 +11,6 @@ public interface Plugin {
/** Returns the plugin's transport identifier. */
TransportId getId();
/** Returns the transport's maximum frame length in bytes. */
int getMaxFrameLength();
/** Returns the transport's maximum latency in milliseconds. */
int getMaxLatency();

View File

@@ -9,9 +9,6 @@ import java.io.InputStream;
*/
public interface TransportConnectionReader {
/** Returns the maximum frame length of the transport in bytes. */
int getMaxFrameLength();
/** Returns the maximum latency of the transport in milliseconds. */
long getMaxLatency();

View File

@@ -9,9 +9,6 @@ import java.io.OutputStream;
*/
public interface TransportConnectionWriter {
/** Returns the maximum frame length of the transport in bytes. */
int getMaxFrameLength();
/** Returns the maximum latency of the transport in milliseconds. */
int getMaxLatency();

View File

@@ -8,13 +8,12 @@ public interface StreamReaderFactory {
* Creates an {@link java.io.InputStream InputStream} for reading from a
* transport stream.
*/
InputStream createStreamReader(InputStream in, int maxFrameLength,
StreamContext ctx);
InputStream createStreamReader(InputStream in, StreamContext ctx);
/**
* Creates an {@link java.io.InputStream InputStream} for reading from an
* invitation stream.
*/
InputStream createInvitationStreamReader(InputStream in,
int maxFrameLength, byte[] secret, boolean alice);
byte[] secret, boolean alice);
}

View File

@@ -8,13 +8,12 @@ public interface StreamWriterFactory {
* Creates an {@link java.io.OutputStream OutputStream} for writing to a
* transport stream
*/
OutputStream createStreamWriter(OutputStream out, int maxFrameLength,
StreamContext ctx);
OutputStream createStreamWriter(OutputStream out, StreamContext ctx);
/**
* Creates an {@link java.io.OutputStream OutputStream} for writing to an
* invitation stream.
*/
OutputStream createInvitationStreamWriter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice);
byte[] secret, boolean alice);
}

View File

@@ -6,7 +6,7 @@ public interface TransportConstants {
int TAG_LENGTH = 16;
/** The maximum length of a frame in bytes, including the header and MAC. */
int MAX_FRAME_LENGTH = 32768; // 2^15, 32 KiB
int MAX_FRAME_LENGTH = 1024;
/** The length of the initalisation vector (IV) in bytes. */
int IV_LENGTH = 12;

View File

@@ -20,23 +20,21 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
}
public StreamDecrypter createStreamDecrypter(InputStream in,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = !ctx.getAlice();
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
public StreamDecrypter createInvitationStreamDecrypter(InputStream in,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.EOFException;
import java.io.IOException;
@@ -21,21 +22,19 @@ class StreamDecrypterImpl implements StreamDecrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean finalFrame;
StreamDecrypterImpl(InputStream in, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength) {
SecretKey frameKey) {
this.in = in;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
finalFrame = false;
}
@@ -44,9 +43,9 @@ class StreamDecrypterImpl implements StreamDecrypter {
if(finalFrame) return -1;
// Read the frame
int ciphertextLength = 0;
while(ciphertextLength < frameLength) {
while(ciphertextLength < MAX_FRAME_LENGTH) {
int read = in.read(ciphertext, ciphertextLength,
frameLength - ciphertextLength);
MAX_FRAME_LENGTH - ciphertextLength);
if(read == -1) break; // We'll check the length later
ciphertextLength += read;
}
@@ -65,7 +64,7 @@ class StreamDecrypterImpl implements StreamDecrypter {
}
// Decode and validate the header
finalFrame = FrameEncoder.isFinalFrame(plaintext);
if(!finalFrame && ciphertextLength < frameLength)
if(!finalFrame && ciphertextLength < MAX_FRAME_LENGTH)
throw new FormatException();
int payloadLength = FrameEncoder.getPayloadLength(plaintext);
if(payloadLength > plaintextLength - HEADER_LENGTH)

View File

@@ -22,7 +22,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
}
public StreamEncrypter createStreamEncrypter(OutputStream out,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = ctx.getAlice();
@@ -34,15 +34,15 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, tag);
tag);
}
public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, null);
null);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import java.io.IOException;
@@ -20,22 +21,20 @@ class StreamEncrypterImpl implements StreamEncrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] tag, iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean writeTag;
StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength, byte[] tag) {
SecretKey frameKey, byte[] tag) {
this.out = out;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
this.tag = tag;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
writeTag = (tag != null);
}
@@ -54,8 +53,8 @@ class StreamEncrypterImpl implements StreamEncrypter {
plaintextLength = HEADER_LENGTH + payloadLength;
ciphertextLength = plaintextLength + MAC_LENGTH;
} else {
plaintextLength = frameLength - MAC_LENGTH;
ciphertextLength = frameLength;
plaintextLength = MAX_FRAME_LENGTH - MAC_LENGTH;
ciphertextLength = MAX_FRAME_LENGTH;
}
// Encode the header
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength);

View File

@@ -128,16 +128,15 @@ class AliceConnector extends Connector {
// Confirmation succeeded - upgrade to a secure connection
if(LOG.isLoggable(INFO))
LOG.info(pluginName + " confirmation succeeded");
int maxFrameLength = conn.getReader().getMaxFrameLength();
// Create the readers
InputStream streamReader =
streamReaderFactory.createInvitationStreamReader(in,
maxFrameLength, secret, false); // Bob's stream
secret, false); // Bob's stream
r = readerFactory.createReader(streamReader);
// Create the writers
OutputStream streamWriter =
streamWriterFactory.createInvitationStreamWriter(out,
maxFrameLength, secret, true); // Alice's stream
secret, true); // Alice's stream
w = writerFactory.createWriter(streamWriter);
// Derive the invitation nonces
byte[][] nonces = crypto.deriveInvitationNonces(secret);

View File

@@ -128,16 +128,15 @@ class BobConnector extends Connector {
// Confirmation succeeded - upgrade to a secure connection
if(LOG.isLoggable(INFO))
LOG.info(pluginName + " confirmation succeeded");
int maxFrameLength = conn.getReader().getMaxFrameLength();
// Create the readers
InputStream streamReader =
streamReaderFactory.createInvitationStreamReader(in,
maxFrameLength, secret, true); // Alice's stream
secret, true); // Alice's stream
r = readerFactory.createReader(streamReader);
// Create the writers
OutputStream streamWriter =
streamWriterFactory.createInvitationStreamWriter(out,
maxFrameLength, secret, false); // Bob's stream
secret, false); // Bob's stream
w = writerFactory.createWriter(streamWriter);
// Derive the nonces
byte[][] nonces = crypto.deriveInvitationNonces(secret);

View File

@@ -95,7 +95,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createIncomingSession(StreamContext ctx,
TransportConnectionReader r) throws IOException {
InputStream streamReader = streamReaderFactory.createStreamReader(
r.getInputStream(), r.getMaxFrameLength(), ctx);
r.getInputStream(), ctx);
return messagingSessionFactory.createIncomingSession(
ctx.getContactId(), ctx.getTransportId(), streamReader);
}
@@ -103,7 +103,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createSimplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
w.getOutputStream(), ctx);
return messagingSessionFactory.createSimplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
streamWriter);
@@ -112,7 +112,7 @@ class ConnectionManagerImpl implements ConnectionManager {
private MessagingSession createDuplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
w.getOutputStream(), ctx);
return messagingSessionFactory.createDuplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
w.getMaxIdleTime(), streamWriter);

View File

@@ -28,7 +28,7 @@ public abstract class FilePlugin implements SimplexPlugin {
protected final Executor ioExecutor;
protected final FileUtils fileUtils;
protected final SimplexPluginCallback callback;
protected final int maxFrameLength, maxLatency;
protected final int maxLatency;
protected volatile boolean running = false;
@@ -38,19 +38,13 @@ public abstract class FilePlugin implements SimplexPlugin {
protected abstract void readerFinished(File f);
protected FilePlugin(Executor ioExecutor, FileUtils fileUtils,
SimplexPluginCallback callback, int maxFrameLength,
int maxLatency) {
SimplexPluginCallback callback, int maxLatency) {
this.ioExecutor = ioExecutor;
this.fileUtils = fileUtils;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -24,10 +24,6 @@ class FileTransportReader implements TransportConnectionReader {
this.plugin = plugin;
}
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -27,10 +27,6 @@ class FileTransportWriter implements TransportConnectionWriter {
this.plugin = plugin;
}
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -17,10 +17,8 @@ class LanTcpPlugin extends TcpPlugin {
static final TransportId ID = new TransportId("lan");
LanTcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime,
int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime,
pollingInterval);
int maxLatency, int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
}
public TransportId getId() {

View File

@@ -9,7 +9,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
public class LanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -25,7 +24,7 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new LanTcpPlugin(ioExecutor, callback, MAX_FRAME_LENGTH,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
return new LanTcpPlugin(ioExecutor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -37,8 +37,7 @@ abstract class TcpPlugin implements DuplexPlugin {
protected final Executor ioExecutor;
protected final DuplexPluginCallback callback;
protected final int maxFrameLength, maxLatency, maxIdleTime;
protected final int pollingInterval, socketTimeout;
protected final int maxLatency, maxIdleTime, pollingInterval, socketTimeout;
protected volatile boolean running = false;
protected volatile ServerSocket socket = null;
@@ -53,11 +52,9 @@ abstract class TcpPlugin implements DuplexPlugin {
protected abstract boolean isConnectable(InetSocketAddress remote);
protected TcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxFrameLength, int maxLatency, int maxIdleTime,
int pollingInterval) {
int maxLatency, int maxIdleTime, int pollingInterval) {
this.ioExecutor = ioExecutor;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval;
@@ -66,10 +63,6 @@ abstract class TcpPlugin implements DuplexPlugin {
else socketTimeout = maxIdleTime * 2;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -38,10 +38,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
@@ -59,10 +55,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -21,10 +21,9 @@ class WanTcpPlugin extends TcpPlugin {
private volatile MappingResult mappingResult;
WanTcpPlugin(Executor ioExecutor, PortMapper portMapper,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency,
int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxFrameLength, maxLatency, maxIdleTime,
pollingInterval);
DuplexPluginCallback callback, int maxLatency, int maxIdleTime,
int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
this.portMapper = portMapper;
}

View File

@@ -10,7 +10,6 @@ import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
public class WanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 5 * 60 * 1000; // 5 minutes
@@ -30,7 +29,6 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new WanTcpPlugin(ioExecutor, new PortMapperImpl(shutdownManager),
callback, MAX_FRAME_LENGTH, MAX_LATENCY, MAX_IDLE_TIME,
POLLING_INTERVAL);
callback, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
}
}

View File

@@ -4,7 +4,6 @@ import java.io.InputStream;
import javax.inject.Inject;
import org.briarproject.api.crypto.StreamDecrypter;
import org.briarproject.api.crypto.StreamDecrypterFactory;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
@@ -18,18 +17,15 @@ class StreamReaderFactoryImpl implements StreamReaderFactory {
this.streamDecrypterFactory = streamDecrypterFactory;
}
public InputStream createStreamReader(InputStream in, int maxFrameLength,
StreamContext ctx) {
StreamDecrypter s = streamDecrypterFactory.createStreamDecrypter(in,
maxFrameLength, ctx);
return new StreamReaderImpl(s, maxFrameLength);
public InputStream createStreamReader(InputStream in, StreamContext ctx) {
return new StreamReaderImpl(
streamDecrypterFactory.createStreamDecrypter(in, ctx));
}
public InputStream createInvitationStreamReader(InputStream in,
int maxFrameLength, byte[] secret, boolean alice) {
StreamDecrypter s =
byte[] secret, boolean alice) {
return new StreamReaderImpl(
streamDecrypterFactory.createInvitationStreamDecrypter(in,
maxFrameLength, secret, alice);
return new StreamReaderImpl(s, maxFrameLength);
secret, alice));
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.InputStream;
@@ -15,9 +16,9 @@ class StreamReaderImpl extends InputStream {
private int offset = 0, length = 0;
StreamReaderImpl(StreamDecrypter decrypter, int frameLength) {
StreamReaderImpl(StreamDecrypter decrypter) {
this.decrypter = decrypter;
payload = new byte[frameLength - HEADER_LENGTH - MAC_LENGTH];
payload = new byte[MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH];
}
@Override

View File

@@ -4,7 +4,6 @@ import java.io.OutputStream;
import javax.inject.Inject;
import org.briarproject.api.crypto.StreamEncrypter;
import org.briarproject.api.crypto.StreamEncrypterFactory;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamWriterFactory;
@@ -18,18 +17,16 @@ class StreamWriterFactoryImpl implements StreamWriterFactory {
this.streamEncrypterFactory = streamEncrypterFactory;
}
public OutputStream createStreamWriter(OutputStream out, int maxFrameLength,
public OutputStream createStreamWriter(OutputStream out,
StreamContext ctx) {
StreamEncrypter s = streamEncrypterFactory.createStreamEncrypter(out,
maxFrameLength, ctx);
return new StreamWriterImpl(s, maxFrameLength);
return new StreamWriterImpl(
streamEncrypterFactory.createStreamEncrypter(out, ctx));
}
public OutputStream createInvitationStreamWriter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) {
StreamEncrypter s =
byte[] secret, boolean alice) {
return new StreamWriterImpl(
streamEncrypterFactory.createInvitationStreamEncrypter(out,
maxFrameLength, secret, alice);
return new StreamWriterImpl(s, maxFrameLength);
secret, alice));
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.OutputStream;
@@ -22,9 +23,9 @@ class StreamWriterImpl extends OutputStream {
private int length = 0;
StreamWriterImpl(StreamEncrypter encrypter, int maxFrameLength) {
StreamWriterImpl(StreamEncrypter encrypter) {
this.encrypter = encrypter;
payload = new byte[maxFrameLength - HEADER_LENGTH - MAC_LENGTH];
payload = new byte[MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH];
}
@Override

View File

@@ -46,7 +46,7 @@ class BluetoothPlugin implements DuplexPlugin {
private final Clock clock;
private final SecureRandom secureRandom;
private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency, pollingInterval;
private final int maxLatency, pollingInterval;
private final Semaphore discoverySemaphore = new Semaphore(1);
private volatile boolean running = false;
@@ -54,13 +54,12 @@ class BluetoothPlugin implements DuplexPlugin {
private volatile LocalDevice localDevice = null;
BluetoothPlugin(Executor ioExecutor, Clock clock, SecureRandom secureRandom,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency,
DuplexPluginCallback callback, int maxLatency,
int pollingInterval) {
this.ioExecutor = ioExecutor;
this.clock = clock;
this.secureRandom = secureRandom;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
@@ -69,10 +68,6 @@ class BluetoothPlugin implements DuplexPlugin {
return ID;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}

View File

@@ -12,7 +12,6 @@ import org.briarproject.system.SystemClock;
public class BluetoothPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
@@ -33,6 +32,6 @@ public class BluetoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new BluetoothPlugin(ioExecutor, clock, secureRandom, callback,
MAX_FRAME_LENGTH, MAX_LATENCY, POLLING_INTERVAL);
MAX_LATENCY, POLLING_INTERVAL);
}
}

View File

@@ -39,10 +39,6 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
@@ -60,10 +56,6 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public int getMaxLatency() {
return plugin.getMaxLatency();
}

View File

@@ -29,8 +29,8 @@ implements RemovableDriveMonitor.Callback {
RemovableDrivePlugin(Executor ioExecutor, FileUtils fileUtils,
SimplexPluginCallback callback, RemovableDriveFinder finder,
RemovableDriveMonitor monitor, int maxFrameLength, int maxLatency) {
super(ioExecutor, fileUtils, callback, maxFrameLength, maxLatency);
RemovableDriveMonitor monitor, int maxLatency) {
super(ioExecutor, fileUtils, callback, maxLatency);
this.finder = finder;
this.monitor = monitor;
}

View File

@@ -1,7 +1,5 @@
package org.briarproject.plugins.file;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.util.concurrent.Executor;
import org.briarproject.api.TransportId;
@@ -52,6 +50,6 @@ public class RemovableDrivePluginFactory implements SimplexPluginFactory {
return null;
}
return new RemovableDrivePlugin(ioExecutor, fileUtils, callback,
finder, monitor, MAX_FRAME_LENGTH, MAX_LATENCY);
finder, monitor, MAX_LATENCY);
}
}

View File

@@ -32,17 +32,16 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private final ModemFactory modemFactory;
private final SerialPortList serialPortList;
private final DuplexPluginCallback callback;
private final int maxFrameLength, maxLatency;
private final int maxLatency;
private volatile boolean running = false;
private volatile Modem modem = null;
ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList,
DuplexPluginCallback callback, int maxFrameLength, int maxLatency) {
DuplexPluginCallback callback, int maxLatency) {
this.modemFactory = modemFactory;
this.serialPortList = serialPortList;
this.callback = callback;
this.maxFrameLength = maxFrameLength;
this.maxLatency = maxLatency;
}
@@ -50,10 +49,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return ID;
}
public int getMaxFrameLength() {
return maxFrameLength;
}
public int getMaxLatency() {
return maxLatency;
}
@@ -199,10 +194,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return maxFrameLength;
}
public long getMaxLatency() {
return maxLatency;
}
@@ -219,10 +210,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private class Writer implements TransportConnectionWriter {
public int getMaxFrameLength() {
return getMaxFrameLength();
}
public int getMaxLatency() {
return getMaxLatency();
}

View File

@@ -11,7 +11,6 @@ import org.briarproject.util.StringUtils;
public class ModemPluginFactory implements DuplexPluginFactory {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private final ModemFactory modemFactory;
@@ -32,6 +31,6 @@ public class ModemPluginFactory implements DuplexPluginFactory {
String enabled = callback.getConfig().get("enabled");
if(StringUtils.isNullOrEmpty(enabled)) return null;
return new ModemPlugin(modemFactory, serialPortList, callback,
MAX_FRAME_LENGTH, MAX_LATENCY);
MAX_LATENCY);
}
}

View File

@@ -1,6 +1,5 @@
package org.briarproject;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -118,8 +117,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamContext ctx = new StreamContext(contactId, transportId, secret,
0, true);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out,
MAX_FRAME_LENGTH, ctx);
OutputStream streamWriter =
streamWriterFactory.createStreamWriter(out, ctx);
PacketWriter packetWriter = packetWriterFactory.createPacketWriter(
streamWriter);
@@ -150,8 +149,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
// FIXME: Check that the expected tag was received
StreamContext ctx = new StreamContext(contactId, transportId, secret,
0, false);
InputStream streamReader = streamReaderFactory.createStreamReader(in,
MAX_FRAME_LENGTH, ctx);
InputStream streamReader =
streamReaderFactory.createStreamReader(in, ctx);
PacketReader packetReader = packetReaderFactory.createPacketReader(
streamReader);

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.ByteArrayInputStream;
@@ -14,6 +15,7 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.AuthenticatedCipher;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.util.ByteUtils;
import org.junit.Test;
import com.google.inject.Guice;
@@ -23,9 +25,8 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher;
@@ -42,16 +43,16 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testReadValidFrames() throws Exception {
// Generate two valid frames
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, false, false);
// Concatenate the frames
byte[] valid = new byte[FRAME_LENGTH * 2];
System.arraycopy(frame, 0, valid, 0, FRAME_LENGTH);
System.arraycopy(frame1, 0, valid, FRAME_LENGTH, FRAME_LENGTH);
byte[] valid = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, valid, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, valid, MAX_FRAME_LENGTH, MAX_FRAME_LENGTH);
// Read the frames
ByteArrayInputStream in = new ByteArrayInputStream(valid);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(123, i.readFrame(payload));
assertEquals(123, i.readFrame(payload));
@@ -60,14 +61,14 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testTruncatedFrameThrowsException() throws Exception {
// Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Chop off the last byte
byte[] truncated = new byte[FRAME_LENGTH - 1];
System.arraycopy(frame, 0, truncated, 0, FRAME_LENGTH - 1);
byte[] truncated = new byte[MAX_FRAME_LENGTH - 1];
System.arraycopy(frame, 0, truncated, 0, MAX_FRAME_LENGTH - 1);
// Try to read the frame, which should fail due to truncation
ByteArrayInputStream in = new ByteArrayInputStream(truncated);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -77,13 +78,13 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testModifiedFrameThrowsException() throws Exception {
// Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Modify a randomly chosen byte of the frame
frame[(int) (Math.random() * FRAME_LENGTH)] ^= 1;
frame[(int) (Math.random() * MAX_FRAME_LENGTH)] ^= 1;
// Try to read the frame, which should fail due to modification
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -93,11 +94,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testShortNonFinalFrameThrowsException() throws Exception {
// Generate a short non-final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, false,
false);
// Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -107,11 +109,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testShortFinalFrameDoesNotThrowException() throws Exception {
// Generate a short final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, true, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, true, false);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
int length = i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
assertEquals(123, length);
}
@@ -119,12 +121,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testInvalidPayloadLengthThrowsException() throws Exception {
// Generate a frame with an invalid payload length
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH + 1,
false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
ByteUtils.writeUint16(MAX_PAYLOAD_LENGTH + 1, frame, 0);
// Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -134,11 +136,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testNonZeroPaddingThrowsException() throws Exception {
// Generate a frame with bad padding
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, true);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, true);
// Try to read the frame, which should fail due to bad padding
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -148,17 +150,18 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testCannotReadBeyondFinalFrame() throws Exception {
// Generate a valid final frame and another valid final frame after it
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH, true,
false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, true, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, MAX_PAYLOAD_LENGTH,
true, false);
byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, true, false);
// Concatenate the frames
byte[] extraFrame = new byte[FRAME_LENGTH * 2];
System.arraycopy(frame, 0, extraFrame, 0, FRAME_LENGTH);
System.arraycopy(frame1, 0, extraFrame, FRAME_LENGTH, FRAME_LENGTH);
byte[] extraFrame = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, extraFrame, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, extraFrame, MAX_FRAME_LENGTH,
MAX_FRAME_LENGTH);
// Read the final frame, which should first read the tag
ByteArrayInputStream in = new ByteArrayInputStream(extraFrame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(MAX_PAYLOAD_LENGTH, i.readFrame(payload));
// The frame after the final frame should not be read

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayOutputStream;
@@ -24,8 +25,6 @@ public class StreamEncrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher;
@@ -40,8 +39,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
public void testEncryptionWithoutTag() throws Exception {
int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH];
byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0);
@@ -51,12 +50,12 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual ciphertext matches what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, frameKey, FRAME_LENGTH, null);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameKey, null);
o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray();
assertEquals(FRAME_LENGTH, actual.length);
for(int i = 0; i < FRAME_LENGTH; i++)
assertEquals(MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[i]);
}
@@ -66,8 +65,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag);
int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH];
byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0);
@@ -77,13 +76,13 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual tag and ciphertext match what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, frameKey, FRAME_LENGTH, tag);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameKey, tag);
o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH, actual.length);
assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < TAG_LENGTH; i++) assertEquals(tag[i], actual[i]);
for(int i = 0; i < FRAME_LENGTH; i++)
for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[TAG_LENGTH + i]);
}
@@ -93,10 +92,10 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Initiator's constructor
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, crypto.generateSecretKey(), FRAME_LENGTH, tag);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
crypto.generateSecretKey(), tag);
// Write an empty final frame without having written any other frames
o.writeFrame(new byte[FRAME_LENGTH - MAC_LENGTH], 0, true);
o.writeFrame(new byte[MAX_FRAME_LENGTH - MAC_LENGTH], 0, true);
// The tag and the empty frame should be written to the output stream
assertEquals(TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH, out.size());
}

View File

@@ -3,7 +3,6 @@ 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_CLOCK_DIFFERENCE;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
@@ -143,8 +142,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamWriterFactory streamWriterFactory =
alice.getInstance(StreamWriterFactory.class);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out,
MAX_FRAME_LENGTH, ctx);
OutputStream streamWriter =
streamWriterFactory.createStreamWriter(out, ctx);
// Create an outgoing messaging session
EventBus eventBus = alice.getInstance(EventBus.class);
PacketWriterFactory packetWriterFactory =
@@ -205,8 +204,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Create a stream reader
StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class);
InputStream streamReader = streamReaderFactory.createStreamReader(in,
MAX_FRAME_LENGTH, ctx);
InputStream streamReader =
streamReaderFactory.createStreamReader(in, ctx);
// Create an incoming messaging session
EventBus eventBus = bob.getInstance(EventBus.class);
MessageVerifier messageVerifier =

View File

@@ -23,12 +23,12 @@ public class BluetoothClientTest extends DuplexClientTest {
p.put("address", serverAddress);
p.put("uuid", BluetoothTest.EMPTY_UUID);
Map<ContactId, TransportProperties> remote =
Collections.singletonMap(contactId, p);
Collections.singletonMap(contactId, p);
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote);
plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0, 0);
new SecureRandom(), callback, 0, 0);
}
public static void main(String[] args) throws Exception {

View File

@@ -23,7 +23,7 @@ public class BluetoothServerTest extends DuplexServerTest {
callback = new ServerCallback(new TransportConfig(), local,
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0, 0);
new SecureRandom(), callback, 0, 0);
}
public static void main(String[] args) throws Exception {

View File

@@ -1,6 +1,5 @@
package org.briarproject.plugins.file;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MIN_STREAM_LENGTH;
import java.io.File;
@@ -58,7 +57,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -93,7 +92,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -130,7 +129,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -169,7 +168,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -208,7 +207,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
assertNotNull(plugin.createWriter(contactId));
@@ -251,7 +250,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
TransportConnectionWriter writer = plugin.createWriter(contactId);
@@ -290,7 +289,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
plugin.start();
plugin.driveInserted(testDir);
@@ -310,7 +309,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, 0);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
@@ -339,7 +338,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), fileUtils, callback, finder, monitor,
MAX_FRAME_LENGTH, 0);
0);
plugin.start();
File f = new File(testDir, "abcdefgh.dat");

View File

@@ -24,7 +24,7 @@ public class ModemPluginTest extends BriarTestCase {
final SerialPortList serialPortList =
context.mock(SerialPortList.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, null, 0, 0);
serialPortList, null, 0);
final Modem modem = context.mock(Modem.class);
context.checking(new Expectations() {{
oneOf(serialPortList).getPortNames();
@@ -58,7 +58,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0);
serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);
@@ -99,7 +99,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0);
serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);
@@ -140,7 +140,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(modemFactory,
serialPortList, callback, 0, 0);
serialPortList, callback, 0);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);

View File

@@ -15,7 +15,6 @@ import org.briarproject.plugins.DuplexClientTest;
// is running on another machine
public class LanTcpClientTest extends DuplexClientTest {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000;
@@ -31,8 +30,8 @@ public class LanTcpClientTest extends DuplexClientTest {
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote);
plugin = new LanTcpPlugin(executor, callback, MAX_FRAME_LENGTH,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
public static void main(String[] args) throws Exception {

View File

@@ -32,7 +32,7 @@ public class LanTcpPluginTest extends BriarTestCase {
@Test
public void testAddressesAreOnSameLan() {
LanTcpPlugin plugin = new LanTcpPlugin(null, null, 0, 0, 0, 0);
LanTcpPlugin plugin = new LanTcpPlugin(null, null, 0, 0, 0);
// Local and remote in 10.0.0.0/8 should return true
assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
makeAddress(10, 255, 255, 255)));
@@ -81,7 +81,7 @@ public class LanTcpPluginTest extends BriarTestCase {
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0, 0);
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -113,7 +113,7 @@ public class LanTcpPluginTest extends BriarTestCase {
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0, 0);
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS));

View File

@@ -13,7 +13,6 @@ import org.briarproject.plugins.DuplexServerTest;
// is running on another machine
public class LanTcpServerTest extends DuplexServerTest {
private static final int MAX_FRAME_LENGTH = 1024;
private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000;
@@ -22,8 +21,8 @@ public class LanTcpServerTest extends DuplexServerTest {
callback = new ServerCallback(new TransportConfig(),
new TransportProperties(),
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new LanTcpPlugin(executor, callback, MAX_FRAME_LENGTH,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
public static void main(String[] args) throws Exception {

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamDecrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamReaderImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test
public void testEmptyFramesAreSkipped() throws Exception {
@@ -29,7 +29,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
assertEquals(0, r.read()); // Skip the first empty frame, read a byte
assertEquals(0, r.read()); // Read another byte
assertEquals(-1, r.read()); // Skip the second empty frame, reach EOF
@@ -52,7 +52,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Skip the first empty frame, read the two payload bytes
assertEquals(2, r.read(buf));
@@ -74,7 +74,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH / 2];
// Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf));
@@ -96,7 +96,7 @@ public class StreamReaderImplTest extends BriarTestCase {
oneOf(decrypter).readFrame(with(any(byte[].class)));
will(returnValue(-1)); // No more frames
}});
StreamReaderImpl r = new StreamReaderImpl(decrypter, FRAME_LENGTH);
StreamReaderImpl r = new StreamReaderImpl(decrypter);
byte[] buf = new byte[MAX_PAYLOAD_LENGTH];
// Read the first half of the payload
assertEquals(MAX_PAYLOAD_LENGTH / 2, r.read(buf, MAX_PAYLOAD_LENGTH / 2,

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.StreamEncrypter;
@@ -11,9 +12,8 @@ import org.junit.Test;
public class StreamWriterImplTest extends BriarTestCase {
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
@Test
public void testCloseWithoutWritingWritesFinalFrame() throws Exception {
@@ -26,7 +26,7 @@ public class StreamWriterImplTest extends BriarTestCase {
// Flush the stream
oneOf(encrypter).flush();
}});
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
w.close();
context.assertIsSatisfied();
}
@@ -36,7 +36,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a non-final frame with an empty payload
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(0),
@@ -63,7 +63,7 @@ public class StreamWriterImplTest extends BriarTestCase {
throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a non-final frame with one payload byte
oneOf(encrypter).writeFrame(with(any(byte[].class)), with(1),
@@ -90,7 +90,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testSingleByteWritesWriteFullFrame() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write a full non-final frame
oneOf(encrypter).writeFrame(with(any(byte[].class)),
@@ -116,7 +116,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testMultiByteWritesWriteFullFrames() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),
@@ -147,7 +147,7 @@ public class StreamWriterImplTest extends BriarTestCase {
public void testLargeMultiByteWriteWritesFullFrames() throws Exception {
Mockery context = new Mockery();
final StreamEncrypter encrypter = context.mock(StreamEncrypter.class);
StreamWriterImpl w = new StreamWriterImpl(encrypter, FRAME_LENGTH);
StreamWriterImpl w = new StreamWriterImpl(encrypter);
context.checking(new Expectations() {{
// Write two full non-final frames
exactly(2).of(encrypter).writeFrame(with(any(byte[].class)),

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import java.io.EOFException;
import java.io.IOException;
@@ -16,9 +17,9 @@ class TestStreamDecrypter implements StreamDecrypter {
private final InputStream in;
private final byte[] frame;
TestStreamDecrypter(InputStream in, int frameLength) {
TestStreamDecrypter(InputStream in) {
this.in = in;
frame = new byte[frameLength];
frame = new byte[MAX_FRAME_LENGTH];
}
public int readFrame(byte[] payload) throws IOException {

View File

@@ -2,6 +2,7 @@ package org.briarproject.transport;
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.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.OutputStream;
@@ -16,10 +17,10 @@ class TestStreamEncrypter implements StreamEncrypter {
private boolean writeTag = true;
TestStreamEncrypter(OutputStream out, int frameLength, byte[] tag) {
TestStreamEncrypter(OutputStream out, byte[] tag) {
this.out = out;
this.tag = tag;
frame = new byte[frameLength];
frame = new byte[MAX_FRAME_LENGTH];
}
public void writeFrame(byte[] payload, int payloadLength,

View File

@@ -1,5 +1,6 @@
package org.briarproject.transport;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -17,8 +18,6 @@ import org.junit.Test;
public class TransportIntegrationTest extends BriarTestCase {
private final int FRAME_LENGTH = 2048;
private final Random random;
public TransportIntegrationTest() {
@@ -40,31 +39,28 @@ public class TransportIntegrationTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH];
random.nextBytes(tag);
// Generate two frames with random payloads
byte[] payload1 = new byte[1234];
byte[] payload1 = new byte[123];
random.nextBytes(payload1);
byte[] payload2 = new byte[321];
random.nextBytes(payload2);
// Write the tag and the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypter encrypter = new TestStreamEncrypter(out, FRAME_LENGTH,
tag);
OutputStream streamWriter = new StreamWriterImpl(encrypter,
FRAME_LENGTH);
StreamEncrypter encrypter = new TestStreamEncrypter(out, tag);
OutputStream streamWriter = new StreamWriterImpl(encrypter);
streamWriter.write(payload1);
streamWriter.flush();
streamWriter.write(payload2);
streamWriter.flush();
byte[] output = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH * 2, output.length);
assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH * 2, output.length);
// Read the tag back
ByteArrayInputStream in = new ByteArrayInputStream(output);
byte[] recoveredTag = new byte[tag.length];
read(in, recoveredTag);
assertArrayEquals(tag, recoveredTag);
// Read the frames back
StreamDecrypter decrypter = new TestStreamDecrypter(in, FRAME_LENGTH);
InputStream streamReader = new StreamReaderImpl(decrypter,
FRAME_LENGTH);
StreamDecrypter decrypter = new TestStreamDecrypter(in);
InputStream streamReader = new StreamReaderImpl(decrypter);
byte[] recoveredPayload1 = new byte[payload1.length];
read(streamReader, recoveredPayload1);
assertArrayEquals(payload1, recoveredPayload1);