mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Updated transport constants and renamed some test classes.
This commit is contained in:
@@ -2,9 +2,9 @@ package net.sf.briar.plugins.socket;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
|
||||
@@ -7,8 +7,8 @@ import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import net.sf.briar.api.transport.TransportConstants;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
import net.sf.briar.protocol.ProtocolModule;
|
||||
import net.sf.briar.protocol.duplex.DuplexProtocolModule;
|
||||
import net.sf.briar.protocol.simplex.SimplexProtocolModule;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
import net.sf.briar.transport.TransportModule;
|
||||
|
||||
|
||||
@@ -36,7 +36,6 @@ import net.sf.briar.lifecycle.LifecycleModule;
|
||||
import net.sf.briar.plugins.ImmediateExecutor;
|
||||
import net.sf.briar.protocol.ProtocolModule;
|
||||
import net.sf.briar.protocol.duplex.DuplexProtocolModule;
|
||||
import net.sf.briar.protocol.simplex.SimplexProtocolModule;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
import net.sf.briar.transport.TransportModule;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
@@ -23,7 +24,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testLengthZero() throws Exception {
|
||||
int payloadLength = 0;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
// Calculate the MAC
|
||||
mac.init(macKey);
|
||||
@@ -31,8 +32,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
// There should be no bytes available before EOF
|
||||
assertEquals(-1, r.getInputStream().read());
|
||||
}
|
||||
@@ -41,7 +42,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testLengthOne() throws Exception {
|
||||
int payloadLength = 1;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
// Calculate the MAC
|
||||
mac.init(macKey);
|
||||
@@ -49,8 +50,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
// There should be one byte available before EOF
|
||||
assertEquals(0, r.getInputStream().read());
|
||||
assertEquals(-1, r.getInputStream().read());
|
||||
@@ -60,27 +61,27 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testMaxLength() throws Exception {
|
||||
// First frame: max payload length
|
||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, maxPayloadLength, 0);
|
||||
HeaderEncoder.encodeHeader(frame, 0, MAX_PAYLOAD_LENGTH, 0);
|
||||
mac.init(macKey);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + maxPayloadLength);
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + maxPayloadLength);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
// Second frame: max payload length plus one
|
||||
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, maxPayloadLength + 1, 0);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + maxPayloadLength + 1);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + maxPayloadLength + 1);
|
||||
HeaderEncoder.encodeHeader(frame1, 1, MAX_PAYLOAD_LENGTH + 1, 0);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + 1);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + 1);
|
||||
// Concatenate the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(frame);
|
||||
out.write(frame1);
|
||||
// Read the first frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
byte[] read = new byte[maxPayloadLength];
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
byte[] read = new byte[MAX_PAYLOAD_LENGTH];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
// Try to read the second frame
|
||||
byte[] read1 = new byte[maxPayloadLength + 1];
|
||||
byte[] read1 = new byte[MAX_PAYLOAD_LENGTH + 1];
|
||||
try {
|
||||
TestUtils.readFully(r.getInputStream(), read1);
|
||||
fail();
|
||||
@@ -92,29 +93,29 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
int paddingLength = 10;
|
||||
// First frame: max payload length, including padding
|
||||
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, maxPayloadLength - paddingLength,
|
||||
HeaderEncoder.encodeHeader(frame, 0, MAX_PAYLOAD_LENGTH - paddingLength,
|
||||
paddingLength);
|
||||
mac.init(macKey);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + maxPayloadLength);
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + maxPayloadLength);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
// Second frame: max payload length plus one, including padding
|
||||
byte[] frame1 = new byte[MAX_FRAME_LENGTH + 1];
|
||||
HeaderEncoder.encodeHeader(frame1, 1,
|
||||
maxPayloadLength + 1 - paddingLength, paddingLength);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + maxPayloadLength + 1);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + maxPayloadLength + 1);
|
||||
MAX_PAYLOAD_LENGTH + 1 - paddingLength, paddingLength);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + 1);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + 1);
|
||||
// Concatenate the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(frame);
|
||||
out.write(frame1);
|
||||
// Read the first frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
byte[] read = new byte[maxPayloadLength - paddingLength];
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
byte[] read = new byte[MAX_PAYLOAD_LENGTH - paddingLength];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
// Try to read the second frame
|
||||
byte[] read1 = new byte[maxPayloadLength + 1 - paddingLength];
|
||||
byte[] read1 = new byte[MAX_PAYLOAD_LENGTH + 1 - paddingLength];
|
||||
try {
|
||||
TestUtils.readFully(r.getInputStream(), read1);
|
||||
fail();
|
||||
@@ -125,7 +126,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testNonZeroPadding() throws Exception {
|
||||
int payloadLength = 10, paddingLength = 10;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ paddingLength + macLength];
|
||||
+ paddingLength + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, paddingLength);
|
||||
// Set a byte of the padding to a non-zero value
|
||||
frame[FRAME_HEADER_LENGTH + payloadLength] = 1;
|
||||
@@ -135,8 +136,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength + paddingLength);
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
// The non-zero padding should be rejected
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
@@ -149,7 +150,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// First frame: 123-byte payload
|
||||
int payloadLength = 123;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
mac.init(macKey);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + payloadLength);
|
||||
@@ -157,7 +158,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Second frame: 1234-byte payload
|
||||
int payloadLength1 = 1234;
|
||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + payloadLength1);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + payloadLength1);
|
||||
@@ -167,8 +168,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
out.write(frame1);
|
||||
// Read the frames
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
byte[] read = new byte[payloadLength];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
assertArrayEquals(new byte[payloadLength], read);
|
||||
@@ -181,7 +182,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testCorruptPayload() throws Exception {
|
||||
int payloadLength = 8;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
// Calculate the MAC
|
||||
mac.init(macKey);
|
||||
@@ -191,8 +192,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
frame[12] ^= 1;
|
||||
// Try to read the frame - not a single byte should be read
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
fail();
|
||||
@@ -203,7 +204,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
public void testCorruptMac() throws Exception {
|
||||
int payloadLength = 8;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
// Calculate the MAC
|
||||
mac.init(macKey);
|
||||
@@ -213,8 +214,8 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
frame[17] ^= 1;
|
||||
// Try to read the frame - not a single byte should be read
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
|
||||
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
fail();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
@@ -20,8 +21,8 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testFlushWithoutWriteProducesNothing() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
||||
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||
w.getOutputStream().flush();
|
||||
w.getOutputStream().flush();
|
||||
w.getOutputStream().flush();
|
||||
@@ -32,7 +33,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
public void testSingleByteFrame() throws Exception {
|
||||
int payloadLength = 1;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
// Calculate the MAC
|
||||
mac.init(macKey);
|
||||
@@ -40,8 +41,8 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
||||
// Check that the ConnectionWriter gets the same results
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
||||
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||
w.getOutputStream().write(0);
|
||||
w.getOutputStream().flush();
|
||||
assertArrayEquals(frame, out.toByteArray());
|
||||
@@ -50,11 +51,11 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteByteToMaxLengthWritesFrame() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
||||
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||
OutputStream out1 = w.getOutputStream();
|
||||
// The first maxPayloadLength - 1 bytes should be buffered
|
||||
for(int i = 0; i < maxPayloadLength - 1; i++) out1.write(0);
|
||||
for(int i = 0; i < MAX_PAYLOAD_LENGTH - 1; i++) out1.write(0);
|
||||
assertEquals(0, out.size());
|
||||
// The next byte should trigger the writing of a frame
|
||||
out1.write(0);
|
||||
@@ -64,14 +65,14 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
@Test
|
||||
public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
||||
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||
OutputStream out1 = w.getOutputStream();
|
||||
// The first maxPayloadLength - 1 bytes should be buffered
|
||||
out1.write(new byte[maxPayloadLength - 1]);
|
||||
out1.write(new byte[MAX_PAYLOAD_LENGTH - 1]);
|
||||
assertEquals(0, out.size());
|
||||
// The next maxPayloadLength + 1 bytes should trigger two frames
|
||||
out1.write(new byte[maxPayloadLength + 1]);
|
||||
out1.write(new byte[MAX_PAYLOAD_LENGTH + 1]);
|
||||
assertEquals(MAX_FRAME_LENGTH * 2, out.size());
|
||||
}
|
||||
|
||||
@@ -80,7 +81,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
// First frame: 123-byte payload
|
||||
int payloadLength = 123;
|
||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0);
|
||||
mac.init(macKey);
|
||||
mac.update(frame, 0, FRAME_HEADER_LENGTH + payloadLength);
|
||||
@@ -88,7 +89,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
// Second frame: 1234-byte payload
|
||||
int payloadLength1 = 1234;
|
||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
||||
+ macLength];
|
||||
+ MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0);
|
||||
mac.update(frame1, 0, FRAME_HEADER_LENGTH + 1234);
|
||||
mac.doFinal(frame1, FRAME_HEADER_LENGTH + 1234);
|
||||
@@ -99,8 +100,8 @@ public class ConnectionWriterImplTest extends TransportTest {
|
||||
byte[] expected = out.toByteArray();
|
||||
// Check that the ConnectionWriter gets the same results
|
||||
out.reset();
|
||||
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
|
||||
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
|
||||
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||
w.getOutputStream().write(new byte[123]);
|
||||
w.getOutputStream().flush();
|
||||
w.getOutputStream().write(new byte[1234]);
|
||||
|
||||
@@ -91,8 +91,8 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
assertArrayEquals(tag, recoveredTag);
|
||||
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
||||
macKey);
|
||||
InputStream in1 = reader.getInputStream();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
@@ -22,8 +23,6 @@ import com.google.inject.Injector;
|
||||
|
||||
public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
@@ -61,7 +60,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, MAC_LENGTH, false);
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
@@ -101,7 +100,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, MAC_LENGTH, true);
|
||||
tagCipher, frameCipher, tagKey, frameKey, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
@@ -23,8 +24,6 @@ import com.google.inject.Injector;
|
||||
|
||||
public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
@@ -60,7 +59,7 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, MAC_LENGTH, false);
|
||||
tagKey, frameKey, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
@@ -98,7 +97,7 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, MAC_LENGTH, true);
|
||||
tagKey, frameKey, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
import java.io.EOFException;
|
||||
@@ -9,19 +10,16 @@ import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.api.FormatException;
|
||||
|
||||
/** A connection decrypter that performs no decryption. */
|
||||
class NullConnectionDecrypter implements IncomingEncryptionLayer {
|
||||
/** An encryption layer that performs no encryption. */
|
||||
class NullIncomingEncryptionLayer implements IncomingEncryptionLayer {
|
||||
|
||||
private final InputStream in;
|
||||
private final int macLength;
|
||||
|
||||
NullConnectionDecrypter(InputStream in, int macLength) {
|
||||
NullIncomingEncryptionLayer(InputStream in) {
|
||||
this.in = in;
|
||||
this.macLength = macLength;
|
||||
}
|
||||
|
||||
public int readFrame(byte[] b) throws IOException {
|
||||
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||
// Read the header to determine the frame length
|
||||
int offset = 0, length = FRAME_HEADER_LENGTH;
|
||||
while(offset < length) {
|
||||
@@ -35,7 +33,7 @@ class NullConnectionDecrypter implements IncomingEncryptionLayer {
|
||||
// Parse the header
|
||||
int payload = HeaderEncoder.getPayloadLength(b);
|
||||
int padding = HeaderEncoder.getPaddingLength(b);
|
||||
length = FRAME_HEADER_LENGTH + payload + padding + macLength;
|
||||
length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
// Read the remainder of the frame
|
||||
while(offset < length) {
|
||||
@@ -3,19 +3,19 @@ package net.sf.briar.transport;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/** A ConnectionEncrypter that performs no encryption. */
|
||||
class NullConnectionEncrypter implements OutgoingEncryptionLayer {
|
||||
/** An encryption layer that performs no encryption. */
|
||||
class NullOutgoingEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
|
||||
private final OutputStream out;
|
||||
|
||||
private long capacity;
|
||||
|
||||
NullConnectionEncrypter(OutputStream out) {
|
||||
NullOutgoingEncryptionLayer(OutputStream out) {
|
||||
this.out = out;
|
||||
capacity = Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
NullConnectionEncrypter(OutputStream out, long capacity) {
|
||||
NullOutgoingEncryptionLayer(OutputStream out, long capacity) {
|
||||
this.out = out;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
@@ -15,9 +16,11 @@ import com.google.inject.Injector;
|
||||
|
||||
public abstract class TransportTest extends BriarTestCase {
|
||||
|
||||
static final int MAX_PAYLOAD_LENGTH
|
||||
= MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - MAC_LENGTH;
|
||||
|
||||
protected final Mac mac;
|
||||
protected final ErasableKey macKey;
|
||||
protected final int macLength, maxPayloadLength;
|
||||
|
||||
public TransportTest() throws Exception {
|
||||
super();
|
||||
@@ -25,7 +28,5 @@ public abstract class TransportTest extends BriarTestCase {
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
mac = crypto.getMac();
|
||||
macKey = crypto.generateTestKey();
|
||||
macLength = mac.getMacLength();
|
||||
maxPayloadLength = MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - macLength;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user