Code clarity, more unit tests for ByteUtils.

Addresses comments for !48.
This commit is contained in:
akwizgran
2016-01-12 11:10:22 +00:00
parent 99f8d21eea
commit 3c6ead0603
11 changed files with 242 additions and 106 deletions

View File

@@ -16,6 +16,7 @@ import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LEN
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_IV_LENGTH;
import static org.briarproject.util.ByteUtils.INT_16_BYTES;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
@@ -119,7 +120,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// The payload length plus padding length is invalid
int payloadLength = MAX_PAYLOAD_LENGTH - 1, paddingLength = 2;
ByteUtils.writeUint16(payloadLength, frameHeader, 0);
ByteUtils.writeUint16(paddingLength, frameHeader, 2);
ByteUtils.writeUint16(paddingLength, frameHeader, INT_16_BYTES);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);

View File

@@ -11,6 +11,7 @@ import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LEN
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.STREAM_HEADER_LENGTH;
import static org.briarproject.util.ByteUtils.INT_16_BYTES;
class TestStreamDecrypter implements StreamDecrypter {
@@ -35,7 +36,7 @@ class TestStreamDecrypter implements StreamDecrypter {
}
finalFrame = (frame[0] & 0x80) == 0x80;
int payloadLength = ByteUtils.readUint16(frame, 0) & 0x7FFF;
int paddingLength = ByteUtils.readUint16(frame, 2);
int paddingLength = ByteUtils.readUint16(frame, INT_16_BYTES);
int frameLength = FRAME_HEADER_LENGTH + payloadLength + paddingLength
+ MAC_LENGTH;
while (offset < frameLength) {

View File

@@ -9,6 +9,7 @@ import java.io.OutputStream;
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_LENGTH;
import static org.briarproject.util.ByteUtils.INT_16_BYTES;
class TestStreamEncrypter implements StreamEncrypter {
@@ -27,7 +28,7 @@ class TestStreamEncrypter implements StreamEncrypter {
if (writeTagAndHeader) writeTagAndHeader();
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
ByteUtils.writeUint16(payloadLength, frameHeader, 0);
ByteUtils.writeUint16(paddingLength, frameHeader, 2);
ByteUtils.writeUint16(paddingLength, frameHeader, INT_16_BYTES);
if (finalFrame) frameHeader[0] |= 0x80;
out.write(frameHeader);
out.write(payload, 0, payloadLength);

View File

@@ -3,7 +3,10 @@ package org.briarproject.util;
import org.briarproject.BriarTestCase;
import org.junit.Test;
import static org.briarproject.util.ByteUtils.MAX_16_BIT_UNSIGNED;
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class ByteUtilsTest extends BriarTestCase {
@@ -17,6 +20,22 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals(65535, ByteUtils.readUint16(b, 1));
}
@Test
public void testReadUint16ValidatesArguments() {
try {
ByteUtils.readUint16(new byte[1], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.readUint16(new byte[2], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
}
@Test
public void testReadUint32() {
byte[] b = StringUtils.fromHexString("0000000000");
@@ -27,6 +46,21 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals(4294967295L, ByteUtils.readUint32(b, 1));
}
@Test
public void testReadUint32ValidatesArguments() {
try {
ByteUtils.readUint32(new byte[3], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.readUint32(new byte[4], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
}
@Test
public void testWriteUint16() {
@@ -37,10 +71,38 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals("00000100", StringUtils.toHexString(b));
ByteUtils.writeUint16(Short.MAX_VALUE, b, 1);
assertEquals("007FFF00", StringUtils.toHexString(b));
ByteUtils.writeUint16(ByteUtils.MAX_16_BIT_UNSIGNED, b, 1);
ByteUtils.writeUint16(MAX_16_BIT_UNSIGNED, b, 1);
assertEquals("00FFFF00", StringUtils.toHexString(b));
}
@Test
public void testWriteUint16ValidatesArguments() {
try {
ByteUtils.writeUint16(0, new byte[1], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint16(0, new byte[2], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint16(-1, new byte[2], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint16(MAX_16_BIT_UNSIGNED + 1, new byte[2], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
}
@Test
public void testWriteUint32() {
byte[] b = new byte[6];
@@ -50,10 +112,38 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals("000000000100", StringUtils.toHexString(b));
ByteUtils.writeUint32(Integer.MAX_VALUE, b, 1);
assertEquals("007FFFFFFF00", StringUtils.toHexString(b));
ByteUtils.writeUint32(ByteUtils.MAX_32_BIT_UNSIGNED, b, 1);
ByteUtils.writeUint32(MAX_32_BIT_UNSIGNED, b, 1);
assertEquals("00FFFFFFFF00", StringUtils.toHexString(b));
}
@Test
public void testWriteUint32ValidatesArguments() {
try {
ByteUtils.writeUint32(0, new byte[3], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint32(0, new byte[4], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint32(-1, new byte[4], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint32(MAX_32_BIT_UNSIGNED + 1, new byte[4], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
}
@Test
public void testWriteUint64() {
byte[] b = new byte[10];
@@ -65,6 +155,28 @@ public class ByteUtilsTest extends BriarTestCase {
assertEquals("007FFFFFFFFFFFFFFF00", StringUtils.toHexString(b));
}
@Test
public void testWriteUint64ValidatesArguments() {
try {
ByteUtils.writeUint64(0, new byte[7], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint64(0, new byte[8], 1);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
try {
ByteUtils.writeUint64(-1, new byte[8], 0);
fail();
} catch (IllegalArgumentException expected) {
// Expected
}
}
@Test
public void testReadUint() {
byte[] b = new byte[1];