mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Use @Test annotation to test for exceptions being thrown
Please note that this commit only uses the @Test annotation where exceptions are thrown at the end of the test, because otherwise the test would not be executed completely. Examples for this are in DatabaseComponentImplTest where many exceptions are thrown in close succession or in ConnectionRegistryImplTest where an exception is thrown in the middle of the test.
This commit is contained in:
@@ -82,7 +82,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
|||||||
assertEquals(-1, s.readFrame(buffer));
|
assertEquals(-1, s.readFrame(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IOException.class)
|
||||||
public void testTruncatedFrameThrowsException() throws Exception {
|
public void testTruncatedFrameThrowsException() throws Exception {
|
||||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||||
int payloadLength = 123, paddingLength = 234;
|
int payloadLength = 123, paddingLength = 234;
|
||||||
@@ -105,15 +105,10 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
|||||||
|
|
||||||
// Try to read the truncated frame
|
// Try to read the truncated frame
|
||||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||||
try {
|
s.readFrame(buffer);
|
||||||
s.readFrame(buffer);
|
|
||||||
fail();
|
|
||||||
} catch (IOException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IOException.class)
|
||||||
public void testInvalidPayloadAndPaddingLengthThrowsException()
|
public void testInvalidPayloadAndPaddingLengthThrowsException()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||||
@@ -139,15 +134,10 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
|||||||
|
|
||||||
// Try to read the invalid frame
|
// Try to read the invalid frame
|
||||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||||
try {
|
s.readFrame(buffer);
|
||||||
s.readFrame(buffer);
|
|
||||||
fail();
|
|
||||||
} catch (IOException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IOException.class)
|
||||||
public void testNonZeroPaddingThrowsException() throws Exception {
|
public void testNonZeroPaddingThrowsException() throws Exception {
|
||||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||||
int payloadLength = 123, paddingLength = 234;
|
int payloadLength = 123, paddingLength = 234;
|
||||||
@@ -174,12 +164,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
|||||||
|
|
||||||
// Try to read the invalid frame
|
// Try to read the invalid frame
|
||||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||||
try {
|
s.readFrame(buffer);
|
||||||
s.readFrame(buffer);
|
|
||||||
fail();
|
|
||||||
} catch (IOException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -165,18 +165,13 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadString8ChecksMaxLength() throws Exception {
|
public void testReadString8ChecksMaxLength() throws Exception {
|
||||||
// "foo" twice
|
// "foo" twice
|
||||||
setContents("41" + "03" + "666F6F" + "41" + "03" + "666F6F");
|
setContents("41" + "03" + "666F6F" + "41" + "03" + "666F6F");
|
||||||
assertEquals("foo", r.readString(3));
|
assertEquals("foo", r.readString(3));
|
||||||
assertTrue(r.hasString());
|
assertTrue(r.hasString());
|
||||||
try {
|
r.readString(2);
|
||||||
r.readString(2);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -205,7 +200,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadString16ChecksMaxLength() throws Exception {
|
public void testReadString16ChecksMaxLength() throws Exception {
|
||||||
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
|
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
|
||||||
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
|
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
|
||||||
@@ -213,12 +208,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
setContents("42" + "0080" + shortHex + "42" + "0080" + shortHex);
|
setContents("42" + "0080" + shortHex + "42" + "0080" + shortHex);
|
||||||
assertEquals(shortest, r.readString(Byte.MAX_VALUE + 1));
|
assertEquals(shortest, r.readString(Byte.MAX_VALUE + 1));
|
||||||
assertTrue(r.hasString());
|
assertTrue(r.hasString());
|
||||||
try {
|
r.readString(Byte.MAX_VALUE);
|
||||||
r.readString(Byte.MAX_VALUE);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -244,7 +234,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadString32ChecksMaxLength() throws Exception {
|
public void testReadString32ChecksMaxLength() throws Exception {
|
||||||
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
|
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
|
||||||
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
|
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
|
||||||
@@ -253,12 +243,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
"44" + "00008000" + shortHex);
|
"44" + "00008000" + shortHex);
|
||||||
assertEquals(shortest, r.readString(Short.MAX_VALUE + 1));
|
assertEquals(shortest, r.readString(Short.MAX_VALUE + 1));
|
||||||
assertTrue(r.hasString());
|
assertTrue(r.hasString());
|
||||||
try {
|
r.readString(Short.MAX_VALUE);
|
||||||
r.readString(Short.MAX_VALUE);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -286,18 +271,13 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadRaw8ChecksMaxLength() throws Exception {
|
public void testReadRaw8ChecksMaxLength() throws Exception {
|
||||||
// {1, 2, 3} twice
|
// {1, 2, 3} twice
|
||||||
setContents("51" + "03" + "010203" + "51" + "03" + "010203");
|
setContents("51" + "03" + "010203" + "51" + "03" + "010203");
|
||||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(3));
|
assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(3));
|
||||||
assertTrue(r.hasRaw());
|
assertTrue(r.hasRaw());
|
||||||
try {
|
r.readRaw(2);
|
||||||
r.readRaw(2);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -326,7 +306,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadRaw16ChecksMaxLength() throws Exception {
|
public void testReadRaw16ChecksMaxLength() throws Exception {
|
||||||
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
|
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
|
||||||
String shortHex = StringUtils.toHexString(shortest);
|
String shortHex = StringUtils.toHexString(shortest);
|
||||||
@@ -334,12 +314,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex);
|
setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex);
|
||||||
assertArrayEquals(shortest, r.readRaw(Byte.MAX_VALUE + 1));
|
assertArrayEquals(shortest, r.readRaw(Byte.MAX_VALUE + 1));
|
||||||
assertTrue(r.hasRaw());
|
assertTrue(r.hasRaw());
|
||||||
try {
|
r.readRaw(Byte.MAX_VALUE);
|
||||||
r.readRaw(Byte.MAX_VALUE);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -365,7 +340,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testReadRaw32ChecksMaxLength() throws Exception {
|
public void testReadRaw32ChecksMaxLength() throws Exception {
|
||||||
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
||||||
String shortHex = StringUtils.toHexString(shortest);
|
String shortHex = StringUtils.toHexString(shortest);
|
||||||
@@ -374,12 +349,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
"54" + "00008000" + shortHex);
|
"54" + "00008000" + shortHex);
|
||||||
assertArrayEquals(shortest, r.readRaw(Short.MAX_VALUE + 1));
|
assertArrayEquals(shortest, r.readRaw(Short.MAX_VALUE + 1));
|
||||||
assertTrue(r.hasRaw());
|
assertTrue(r.hasRaw());
|
||||||
try {
|
r.readRaw(Short.MAX_VALUE);
|
||||||
r.readRaw(Short.MAX_VALUE);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class ConsumersTest extends BriarTestCase {
|
|||||||
assertArrayEquals(dig, dig1);
|
assertArrayEquals(dig, dig1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testCountingConsumer() throws Exception {
|
public void testCountingConsumer() throws Exception {
|
||||||
byte[] data = new byte[1234];
|
byte[] data = new byte[1234];
|
||||||
CountingConsumer cc = new CountingConsumer(data.length);
|
CountingConsumer cc = new CountingConsumer(data.length);
|
||||||
@@ -39,12 +39,7 @@ public class ConsumersTest extends BriarTestCase {
|
|||||||
cc.write(data, 1, data.length - 2);
|
cc.write(data, 1, data.length - 2);
|
||||||
cc.write(data[data.length - 1]);
|
cc.write(data[data.length - 1]);
|
||||||
assertEquals(data.length, cc.getCount());
|
assertEquals(data.length, cc.getCount());
|
||||||
try {
|
cc.write((byte) 0);
|
||||||
cc.write((byte) 0);
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -39,18 +39,13 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
bdfWriterFactory = i.getInstance(BdfWriterFactory.class);
|
bdfWriterFactory = i.getInstance(BdfWriterFactory.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testFormatExceptionIfAckIsTooLarge() throws Exception {
|
public void testFormatExceptionIfAckIsTooLarge() throws Exception {
|
||||||
byte[] b = createAck(true);
|
byte[] b = createAck(true);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readAck();
|
||||||
reader.readAck();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -62,32 +57,22 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
reader.readAck();
|
reader.readAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testEmptyAck() throws Exception {
|
public void testEmptyAck() throws Exception {
|
||||||
byte[] b = createEmptyAck();
|
byte[] b = createEmptyAck();
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readAck();
|
||||||
reader.readAck();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testFormatExceptionIfOfferIsTooLarge() throws Exception {
|
public void testFormatExceptionIfOfferIsTooLarge() throws Exception {
|
||||||
byte[] b = createOffer(true);
|
byte[] b = createOffer(true);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readOffer();
|
||||||
reader.readOffer();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -101,32 +86,22 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
reader.readOffer();
|
reader.readOffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testEmptyOffer() throws Exception {
|
public void testEmptyOffer() throws Exception {
|
||||||
byte[] b = createEmptyOffer();
|
byte[] b = createEmptyOffer();
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readOffer();
|
||||||
reader.readOffer();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testFormatExceptionIfRequestIsTooLarge() throws Exception {
|
public void testFormatExceptionIfRequestIsTooLarge() throws Exception {
|
||||||
byte[] b = createRequest(true);
|
byte[] b = createRequest(true);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readRequest();
|
||||||
reader.readRequest();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -138,18 +113,13 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
reader.readRequest();
|
reader.readRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = FormatException.class)
|
||||||
public void testEmptyRequest() throws Exception {
|
public void testEmptyRequest() throws Exception {
|
||||||
byte[] b = createEmptyRequest();
|
byte[] b = createEmptyRequest();
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||||
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
PacketReaderImpl reader = new PacketReaderImpl(bdfReaderFactory, null,
|
||||||
null, in);
|
null, in);
|
||||||
try {
|
reader.readRequest();
|
||||||
reader.readRequest();
|
|
||||||
fail();
|
|
||||||
} catch (FormatException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] createAck(boolean tooBig) throws Exception {
|
private byte[] createAck(boolean tooBig) throws Exception {
|
||||||
|
|||||||
@@ -20,20 +20,14 @@ public class ByteUtilsTest extends BriarTestCase {
|
|||||||
assertEquals(65535, ByteUtils.readUint16(b, 1));
|
assertEquals(65535, ByteUtils.readUint16(b, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testReadUint16ValidatesArguments() {
|
public void testReadUint16ValidatesArguments1() {
|
||||||
try {
|
ByteUtils.readUint16(new byte[1], 0);
|
||||||
ByteUtils.readUint16(new byte[1], 0);
|
}
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException expected) {
|
@Test(expected = IllegalArgumentException.class)
|
||||||
// Expected
|
public void testReadUint16ValidatesArguments2() {
|
||||||
}
|
ByteUtils.readUint16(new byte[2], 1);
|
||||||
try {
|
|
||||||
ByteUtils.readUint16(new byte[2], 1);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -46,20 +40,14 @@ public class ByteUtilsTest extends BriarTestCase {
|
|||||||
assertEquals(4294967295L, ByteUtils.readUint32(b, 1));
|
assertEquals(4294967295L, ByteUtils.readUint32(b, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testReadUint32ValidatesArguments() {
|
public void testReadUint32ValidatesArguments1() {
|
||||||
try {
|
ByteUtils.readUint32(new byte[3], 0);
|
||||||
ByteUtils.readUint32(new byte[3], 0);
|
}
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException expected) {
|
@Test(expected = IllegalArgumentException.class)
|
||||||
// Expected
|
public void testReadUint32ValidatesArguments2() {
|
||||||
}
|
ByteUtils.readUint32(new byte[4], 1);
|
||||||
try {
|
|
||||||
ByteUtils.readUint32(new byte[4], 1);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException expected) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user