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

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