mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Changed maximum packet and message sizes in preparation for new
transport format.
This commit is contained in:
@@ -9,6 +9,7 @@ import java.util.Random;
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
@@ -103,7 +104,8 @@ public class AckReaderTest extends TestCase {
|
||||
w.writeListStart();
|
||||
byte[] b = new byte[UniqueId.LENGTH];
|
||||
Random random = new Random();
|
||||
while(out.size() < Ack.MAX_SIZE - BatchId.SERIALISED_LENGTH) {
|
||||
while(out.size() + BatchId.LENGTH + 3
|
||||
< ProtocolConstants.MAX_PACKET_LENGTH) {
|
||||
w.writeUserDefinedTag(Tags.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
@@ -114,7 +116,7 @@ public class AckReaderTest extends TestCase {
|
||||
w.writeBytes(b);
|
||||
}
|
||||
w.writeListEnd();
|
||||
assertEquals(tooBig, out.size() > Ack.MAX_SIZE);
|
||||
assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
@@ -54,7 +55,7 @@ public class BatchReaderTest extends TestCase {
|
||||
BatchReader batchReader = new BatchReader(crypto, messageReader,
|
||||
batchFactory);
|
||||
|
||||
byte[] b = createBatch(Batch.MAX_SIZE + 1);
|
||||
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH + 1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Tags.BATCH, batchReader);
|
||||
@@ -79,7 +80,7 @@ public class BatchReaderTest extends TestCase {
|
||||
will(returnValue(batch));
|
||||
}});
|
||||
|
||||
byte[] b = createBatch(Batch.MAX_SIZE);
|
||||
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Tags.BATCH, batchReader);
|
||||
@@ -90,7 +91,7 @@ public class BatchReaderTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testBatchId() throws Exception {
|
||||
byte[] b = createBatch(Batch.MAX_SIZE);
|
||||
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Calculate the expected batch ID
|
||||
MessageDigest messageDigest = crypto.getMessageDigest();
|
||||
messageDigest.reset();
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.BitSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
@@ -127,9 +128,10 @@ public class RequestReaderTest extends TestCase {
|
||||
// UniqueID.LENGTH bytes for the offer ID, one byte for the BYTES tag,
|
||||
// and five bytes for the length as an int32
|
||||
int overhead = UniqueId.LENGTH + 10;
|
||||
if(tooBig) w.writeBytes(new byte[Request.MAX_SIZE - overhead + 1]);
|
||||
else w.writeBytes(new byte[Request.MAX_SIZE - overhead]);
|
||||
assertEquals(tooBig, out.size() > Request.MAX_SIZE);
|
||||
int size = ProtocolConstants.MAX_PACKET_LENGTH - overhead;
|
||||
if(tooBig) size++;
|
||||
w.writeBytes(new byte[size]);
|
||||
assertEquals(tooBig, out.size() > ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -53,7 +55,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
}});
|
||||
final ConnectionRecogniserImpl c =
|
||||
new ConnectionRecogniserImpl(transportId, crypto, db);
|
||||
assertNull(c.acceptConnection(new byte[Constants.TAG_BYTES]));
|
||||
assertNull(c.acceptConnection(new byte[TAG_LENGTH]));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -33,25 +35,25 @@ public class PacketDecrypterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testSingleBytePackets() throws Exception {
|
||||
byte[] ciphertext = new byte[(Constants.TAG_BYTES + 1) * 2];
|
||||
byte[] ciphertext = new byte[(TAG_LENGTH + 1) * 2];
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(ciphertext);
|
||||
byte[] firstTag = new byte[Constants.TAG_BYTES];
|
||||
assertEquals(Constants.TAG_BYTES, in.read(firstTag));
|
||||
byte[] firstTag = new byte[TAG_LENGTH];
|
||||
assertEquals(TAG_LENGTH, in.read(firstTag));
|
||||
PacketDecrypter p = new PacketDecrypterImpl(firstTag, in, tagCipher,
|
||||
packetCipher, tagKey, packetKey);
|
||||
byte[] decryptedTag = p.readTag();
|
||||
assertEquals(Constants.TAG_BYTES, decryptedTag.length);
|
||||
assertEquals(TAG_LENGTH, decryptedTag.length);
|
||||
assertTrue(p.getInputStream().read() > -1);
|
||||
byte[] decryptedTag1 = p.readTag();
|
||||
assertEquals(Constants.TAG_BYTES, decryptedTag1.length);
|
||||
assertEquals(TAG_LENGTH, decryptedTag1.length);
|
||||
assertTrue(p.getInputStream().read() > -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryption() throws Exception {
|
||||
byte[] tag = new byte[Constants.TAG_BYTES];
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
byte[] packet = new byte[123];
|
||||
byte[] tag1 = new byte[Constants.TAG_BYTES];
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
byte[] packet1 = new byte[234];
|
||||
// Calculate the first expected decrypted tag
|
||||
tagCipher.init(Cipher.DECRYPT_MODE, tagKey);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -36,15 +38,15 @@ public class PacketEncrypterImplTest extends TestCase {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
PacketEncrypter p = new PacketEncrypterImpl(out, tagCipher,
|
||||
packetCipher, tagKey, packetKey);
|
||||
p.writeTag(new byte[Constants.TAG_BYTES]);
|
||||
p.writeTag(new byte[TAG_LENGTH]);
|
||||
p.getOutputStream().write((byte) 0);
|
||||
p.finishPacket();
|
||||
assertEquals(Constants.TAG_BYTES + 1, out.toByteArray().length);
|
||||
assertEquals(TAG_LENGTH + 1, out.toByteArray().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryption() throws Exception {
|
||||
byte[] tag = new byte[Constants.TAG_BYTES];
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
byte[] packet = new byte[123];
|
||||
// Calculate the expected encrypted tag
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
@@ -63,14 +65,14 @@ public class PacketEncrypterImplTest extends TestCase {
|
||||
p.getOutputStream().write(packet);
|
||||
p.finishPacket();
|
||||
byte[] ciphertext = out.toByteArray();
|
||||
assertEquals(Constants.TAG_BYTES + packet.length, ciphertext.length);
|
||||
assertEquals(TAG_LENGTH + packet.length, ciphertext.length);
|
||||
// Check the tag
|
||||
byte[] actualTag = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(ciphertext, 0, actualTag, 0, Constants.TAG_BYTES);
|
||||
byte[] actualTag = new byte[TAG_LENGTH];
|
||||
System.arraycopy(ciphertext, 0, actualTag, 0, TAG_LENGTH);
|
||||
assertTrue(Arrays.equals(expectedTag, actualTag));
|
||||
// Check the packet
|
||||
byte[] actualPacket = new byte[packet.length];
|
||||
System.arraycopy(ciphertext, Constants.TAG_BYTES, actualPacket, 0,
|
||||
System.arraycopy(ciphertext, TAG_LENGTH, actualPacket, 0,
|
||||
actualPacket.length);
|
||||
assertTrue(Arrays.equals(expectedPacket, actualPacket));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
@@ -68,8 +70,8 @@ public class PacketReadWriteTest extends TestCase {
|
||||
writer.finishPacket();
|
||||
// Read the packets back
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
byte[] firstTag = new byte[Constants.TAG_BYTES];
|
||||
assertEquals(Constants.TAG_BYTES, in.read(firstTag));
|
||||
byte[] firstTag = new byte[TAG_LENGTH];
|
||||
assertEquals(TAG_LENGTH, in.read(firstTag));
|
||||
PacketDecrypter decrypter = new PacketDecrypterImpl(firstTag, in,
|
||||
tagCipher, packetCipher, tagKey, packetKey);
|
||||
PacketReader reader = new PacketReaderImpl(decrypter, mac, transportId,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
@@ -35,7 +37,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testFirstReadTriggersTag() throws Exception {
|
||||
// TAG_BYTES for the tag, 1 byte for the packet
|
||||
byte[] b = new byte[Constants.TAG_BYTES + 1];
|
||||
byte[] b = new byte[TAG_LENGTH + 1];
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
PacketDecrypter d = new NullPacketDecrypter(in);
|
||||
PacketReader p = new PacketReaderImpl(d, mac, 0, 0L);
|
||||
@@ -47,7 +49,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testFinishPacketAfterReadTriggersMac() throws Exception {
|
||||
// TAG_BYTES for the tag, 1 byte for the packet
|
||||
byte[] b = new byte[Constants.TAG_BYTES + 1];
|
||||
byte[] b = new byte[TAG_LENGTH + 1];
|
||||
// Calculate the MAC and append it to the packet
|
||||
mac.update(b);
|
||||
byte[] macBytes = mac.doFinal();
|
||||
@@ -66,14 +68,14 @@ public class PacketReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testModifyingPacketInvalidatesMac() throws Exception {
|
||||
// TAG_BYTES for the tag, 1 byte for the packet
|
||||
byte[] b = new byte[Constants.TAG_BYTES + 1];
|
||||
byte[] b = new byte[TAG_LENGTH + 1];
|
||||
// Calculate the MAC and append it to the packet
|
||||
mac.update(b);
|
||||
byte[] macBytes = mac.doFinal();
|
||||
byte[] b1 = Arrays.copyOf(b, b.length + macBytes.length);
|
||||
System.arraycopy(macBytes, 0, b1, b.length, macBytes.length);
|
||||
// Modify the packet
|
||||
b1[Constants.TAG_BYTES] = (byte) 1;
|
||||
b1[TAG_LENGTH] = (byte) 1;
|
||||
// Check that the PacketReader reads and fails to verify the MAC
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b1);
|
||||
PacketDecrypter d = new NullPacketDecrypter(in);
|
||||
@@ -88,7 +90,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testExtraCallsToFinishPacketDoNothing() throws Exception {
|
||||
// TAG_BYTES for the tag, 1 byte for the packet
|
||||
byte[] b = new byte[Constants.TAG_BYTES + 1];
|
||||
byte[] b = new byte[TAG_LENGTH + 1];
|
||||
// Calculate the MAC and append it to the packet
|
||||
mac.update(b);
|
||||
byte[] macBytes = mac.doFinal();
|
||||
@@ -121,7 +123,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
+ "00000000" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, tag.length);
|
||||
assertEquals(TAG_LENGTH, tag.length);
|
||||
byte[] tag1 = StringUtils.fromHexString(
|
||||
"0000" // 16 bits reserved
|
||||
+ "F00D" // 16 bits for the transport ID
|
||||
@@ -129,7 +131,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
+ "00000001" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, tag1.length);
|
||||
assertEquals(TAG_LENGTH, tag1.length);
|
||||
// Calculate the MAC on the first packet and append it to the packet
|
||||
mac.update(tag);
|
||||
mac.update((byte) 0);
|
||||
@@ -172,7 +174,7 @@ public class PacketReaderImplTest extends TestCase {
|
||||
}
|
||||
|
||||
public byte[] readTag() throws IOException {
|
||||
byte[] tag = new byte[Constants.TAG_BYTES];
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
int offset = 0;
|
||||
while(offset < tag.length) {
|
||||
int read = in.read(tag, offset, tag.length - offset);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -37,14 +39,14 @@ public class PacketWriterImplTest extends TestCase {
|
||||
PacketWriter p = new PacketWriterImpl(e, mac, 0, 0L);
|
||||
p.getOutputStream().write(0);
|
||||
// There should be TAG_BYTES bytes for the tag, 1 byte for the packet
|
||||
assertTrue(Arrays.equals(new byte[Constants.TAG_BYTES + 1],
|
||||
assertTrue(Arrays.equals(new byte[TAG_LENGTH + 1],
|
||||
out.toByteArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinishPacketAfterWriteTriggersMac() throws Exception {
|
||||
// Calculate what the MAC should be
|
||||
mac.update(new byte[Constants.TAG_BYTES + 1]);
|
||||
mac.update(new byte[TAG_LENGTH + 1]);
|
||||
byte[] expectedMac = mac.doFinal();
|
||||
// Check that the PacketWriter calculates and writes the correct MAC
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -53,10 +55,10 @@ public class PacketWriterImplTest extends TestCase {
|
||||
p.getOutputStream().write(0);
|
||||
p.finishPacket();
|
||||
byte[] written = out.toByteArray();
|
||||
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length,
|
||||
assertEquals(TAG_LENGTH + 1 + expectedMac.length,
|
||||
written.length);
|
||||
byte[] actualMac = new byte[expectedMac.length];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0,
|
||||
System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
|
||||
actualMac.length);
|
||||
assertTrue(Arrays.equals(expectedMac, actualMac));
|
||||
}
|
||||
@@ -64,7 +66,7 @@ public class PacketWriterImplTest extends TestCase {
|
||||
@Test
|
||||
public void testExtraCallsToFinishPacketDoNothing() throws Exception {
|
||||
// Calculate what the MAC should be
|
||||
mac.update(new byte[Constants.TAG_BYTES + 1]);
|
||||
mac.update(new byte[TAG_LENGTH + 1]);
|
||||
byte[] expectedMac = mac.doFinal();
|
||||
// Check that the PacketWriter calculates and writes the correct MAC
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -81,10 +83,10 @@ public class PacketWriterImplTest extends TestCase {
|
||||
p.finishPacket();
|
||||
p.finishPacket();
|
||||
byte[] written = out.toByteArray();
|
||||
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length,
|
||||
assertEquals(TAG_LENGTH + 1 + expectedMac.length,
|
||||
written.length);
|
||||
byte[] actualMac = new byte[expectedMac.length];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0,
|
||||
System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
|
||||
actualMac.length);
|
||||
assertTrue(Arrays.equals(expectedMac, actualMac));
|
||||
}
|
||||
@@ -98,7 +100,7 @@ public class PacketWriterImplTest extends TestCase {
|
||||
+ "00000000" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, expectedTag.length);
|
||||
assertEquals(TAG_LENGTH, expectedTag.length);
|
||||
byte[] expectedTag1 = StringUtils.fromHexString(
|
||||
"0000" // 16 bits reserved
|
||||
+ "F00D" // 16 bits for the transport ID
|
||||
@@ -106,7 +108,7 @@ public class PacketWriterImplTest extends TestCase {
|
||||
+ "00000001" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, expectedTag1.length);
|
||||
assertEquals(TAG_LENGTH, expectedTag1.length);
|
||||
// Calculate what the MAC on the first packet should be
|
||||
mac.update(expectedTag);
|
||||
mac.update((byte) 0);
|
||||
@@ -126,27 +128,27 @@ public class PacketWriterImplTest extends TestCase {
|
||||
p.getOutputStream().write(0);
|
||||
p.finishPacket();
|
||||
byte[] written = out.toByteArray();
|
||||
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length
|
||||
+ Constants.TAG_BYTES + 1 + expectedMac1.length,
|
||||
assertEquals(TAG_LENGTH + 1 + expectedMac.length
|
||||
+ TAG_LENGTH + 1 + expectedMac1.length,
|
||||
written.length);
|
||||
// Check the first packet's tag
|
||||
byte[] actualTag = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(written, 0, actualTag, 0, Constants.TAG_BYTES);
|
||||
byte[] actualTag = new byte[TAG_LENGTH];
|
||||
System.arraycopy(written, 0, actualTag, 0, TAG_LENGTH);
|
||||
assertTrue(Arrays.equals(expectedTag, actualTag));
|
||||
// Check the first packet's MAC
|
||||
byte[] actualMac = new byte[expectedMac.length];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0,
|
||||
System.arraycopy(written, TAG_LENGTH + 1, actualMac, 0,
|
||||
actualMac.length);
|
||||
assertTrue(Arrays.equals(expectedMac, actualMac));
|
||||
// Check the second packet's tag
|
||||
byte[] actualTag1 = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length,
|
||||
actualTag1, 0, Constants.TAG_BYTES);
|
||||
byte[] actualTag1 = new byte[TAG_LENGTH];
|
||||
System.arraycopy(written, TAG_LENGTH + 1 + expectedMac.length,
|
||||
actualTag1, 0, TAG_LENGTH);
|
||||
assertTrue(Arrays.equals(expectedTag1, actualTag1));
|
||||
// Check the second packet's MAC
|
||||
byte[] actualMac1 = new byte[expectedMac1.length];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length
|
||||
+ Constants.TAG_BYTES + 1, actualMac1, 0, actualMac1.length);
|
||||
System.arraycopy(written, TAG_LENGTH + 1 + expectedMac.length
|
||||
+ TAG_LENGTH + 1, actualMac1, 0, actualMac1.length);
|
||||
assertTrue(Arrays.equals(expectedMac1, actualMac1));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user