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

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