From 82eea6bb77f5e95e23403715b9c20d25e1f52bad Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Wed, 19 Oct 2016 18:09:53 +0100
Subject: [PATCH] Throw FormatException if BdfList index is out of bounds.

---
 .../org/briarproject/api/data/BdfList.java    |  25 ++
 .../org/briarproject/data/BdfListTest.java    | 307 ++++++++++++++++++
 2 files changed, 332 insertions(+)

diff --git a/briar-api/src/org/briarproject/api/data/BdfList.java b/briar-api/src/org/briarproject/api/data/BdfList.java
index 20a68c9477..59732f4a05 100644
--- a/briar-api/src/org/briarproject/api/data/BdfList.java
+++ b/briar-api/src/org/briarproject/api/data/BdfList.java
@@ -29,13 +29,19 @@ public class BdfList extends Vector<Object> {
 		super(items);
 	}
 
+	private boolean isInRange(int index) {
+		return index >= 0 && index < size();
+	}
+
 	public Boolean getBoolean(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof Boolean) return (Boolean) o;
 		throw new FormatException();
 	}
 
 	public Boolean getOptionalBoolean(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof Boolean) return (Boolean) o;
@@ -43,12 +49,14 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Boolean getBoolean(int index, Boolean defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof Boolean) return (Boolean) o;
 		return defaultValue;
 	}
 
 	public Long getLong(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof Long) return (Long) o;
 		if (o instanceof Integer) return ((Integer) o).longValue();
@@ -58,6 +66,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Long getOptionalLong(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof Long) return (Long) o;
@@ -68,6 +77,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Long getLong(int index, Long defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof Long) return (Long) o;
 		if (o instanceof Integer) return ((Integer) o).longValue();
@@ -77,6 +87,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Double getDouble(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof Double) return (Double) o;
 		if (o instanceof Float) return ((Float) o).doubleValue();
@@ -84,6 +95,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Double getOptionalDouble(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof Double) return (Double) o;
@@ -92,6 +104,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public Double getDouble(int index, Double defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof Double) return (Double) o;
 		if (o instanceof Float) return ((Float) o).doubleValue();
@@ -99,12 +112,14 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public String getString(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof String) return (String) o;
 		throw new FormatException();
 	}
 
 	public String getOptionalString(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof String) return (String) o;
@@ -112,12 +127,14 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public String getString(int index, String defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof String) return (String) o;
 		return defaultValue;
 	}
 
 	public byte[] getRaw(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof byte[]) return (byte[]) o;
 		if (o instanceof Bytes) return ((Bytes) o).getBytes();
@@ -125,6 +142,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public byte[] getOptionalRaw(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof byte[]) return (byte[]) o;
@@ -133,6 +151,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public byte[] getRaw(int index, byte[] defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof byte[]) return (byte[]) o;
 		if (o instanceof Bytes) return ((Bytes) o).getBytes();
@@ -140,12 +159,14 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public BdfList getList(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof BdfList) return (BdfList) o;
 		throw new FormatException();
 	}
 
 	public BdfList getOptionalList(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof BdfList) return (BdfList) o;
@@ -153,12 +174,14 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public BdfList getList(int index, BdfList defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof BdfList) return (BdfList) o;
 		return defaultValue;
 	}
 
 	public BdfDictionary getDictionary(int index) throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o instanceof BdfDictionary) return (BdfDictionary) o;
 		throw new FormatException();
@@ -166,6 +189,7 @@ public class BdfList extends Vector<Object> {
 
 	public BdfDictionary getOptionalDictionary(int index)
 			throws FormatException {
+		if (!isInRange(index)) throw new FormatException();
 		Object o = get(index);
 		if (o == null || o == NULL_VALUE) return null;
 		if (o instanceof BdfDictionary) return (BdfDictionary) o;
@@ -173,6 +197,7 @@ public class BdfList extends Vector<Object> {
 	}
 
 	public BdfDictionary getDictionary(int index, BdfDictionary defaultValue) {
+		if (!isInRange(index)) return defaultValue;
 		Object o = get(index);
 		if (o instanceof BdfDictionary) return (BdfDictionary) o;
 		return defaultValue;
diff --git a/briar-tests/src/org/briarproject/data/BdfListTest.java b/briar-tests/src/org/briarproject/data/BdfListTest.java
index 7bb54b3d84..1cced78ffd 100644
--- a/briar-tests/src/org/briarproject/data/BdfListTest.java
+++ b/briar-tests/src/org/briarproject/data/BdfListTest.java
@@ -2,6 +2,9 @@ package org.briarproject.data;
 
 import org.briarproject.BriarTestCase;
 import org.briarproject.api.Bytes;
+import org.briarproject.api.FormatException;
+import org.briarproject.api.data.BdfDictionary;
+import org.briarproject.api.data.BdfEntry;
 import org.briarproject.api.data.BdfList;
 import org.junit.Test;
 
@@ -62,4 +65,308 @@ public class BdfListTest extends BriarTestCase {
 		assertEquals(123, second.length);
 		assertArrayEquals(new byte[123], second);
 	}
+
+	@Test
+	@SuppressWarnings("ConstantConditions")
+	public void testIndexOutOfBoundsReturnsDefaultValue() throws Exception {
+		BdfList list = BdfList.of(1, 2, 3);
+		boolean defaultBoolean = true;
+		assertEquals(defaultBoolean, list.getBoolean(-1, defaultBoolean));
+		assertEquals(defaultBoolean, list.getBoolean(3, defaultBoolean));
+		Long defaultLong = 123L;
+		assertEquals(defaultLong, list.getLong(-1, defaultLong));
+		assertEquals(defaultLong, list.getLong(3, defaultLong));
+		Double defaultDouble = 1.23;
+		assertEquals(defaultDouble, list.getDouble(-1, defaultDouble));
+		assertEquals(defaultDouble, list.getDouble(3, defaultDouble));
+		String defaultString = "123";
+		assertEquals(defaultString, list.getString(-1, defaultString));
+		assertEquals(defaultString, list.getString(3, defaultString));
+		byte[] defaultBytes = new byte[] {1, 2, 3};
+		assertArrayEquals(defaultBytes, list.getRaw(-1, defaultBytes));
+		assertArrayEquals(defaultBytes, list.getRaw(3, defaultBytes));
+		BdfList defaultList = BdfList.of(1, 2, 3);
+		assertEquals(defaultList, list.getList(-1, defaultList));
+		assertEquals(defaultList, list.getList(3, defaultList));
+		BdfDictionary defaultDict = BdfDictionary.of(
+				new BdfEntry("1", 1),
+				new BdfEntry("2", 2),
+				new BdfEntry("3", 3)
+		);
+		assertEquals(defaultDict, list.getDictionary(-1, defaultDict));
+		assertEquals(defaultDict, list.getDictionary(3, defaultDict));
+	}
+
+	@Test
+	@SuppressWarnings("ConstantConditions")
+	public void testWrongTypeReturnsDefaultValue() throws Exception {
+		BdfList list = BdfList.of(1, 2, 3, true);
+		boolean defaultBoolean = true;
+		assertEquals(defaultBoolean, list.getBoolean(0, defaultBoolean));
+		Long defaultLong = 123L;
+		assertEquals(defaultLong, list.getLong(3, defaultLong));
+		Double defaultDouble = 1.23;
+		assertEquals(defaultDouble, list.getDouble(0, defaultDouble));
+		String defaultString = "123";
+		assertEquals(defaultString, list.getString(0, defaultString));
+		byte[] defaultBytes = new byte[] {1, 2, 3};
+		assertArrayEquals(defaultBytes, list.getRaw(0, defaultBytes));
+		BdfList defaultList = BdfList.of(1, 2, 3);
+		assertEquals(defaultList, list.getList(0, defaultList));
+		BdfDictionary defaultDict = BdfDictionary.of(
+				new BdfEntry("1", 1),
+				new BdfEntry("2", 2),
+				new BdfEntry("3", 3)
+		);
+		assertEquals(defaultDict, list.getDictionary(0, defaultDict));
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForBooleanThrowsFormatException()
+			throws Exception {
+		new BdfList().getBoolean(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalBooleanThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalBoolean(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForLongThrowsFormatException()
+			throws Exception {
+		new BdfList().getLong(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalLongThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalLong(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForDoubleThrowsFormatException()
+			throws Exception {
+		new BdfList().getDouble(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalDoubleThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalDouble(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForStringThrowsFormatException()
+			throws Exception {
+		new BdfList().getString(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalStringThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalString(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForRawThrowsFormatException()
+			throws Exception {
+		new BdfList().getRaw(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalRawThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalRaw(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForListThrowsFormatException()
+			throws Exception {
+		new BdfList().getList(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalListThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalList(-1);
+	}
+
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForDictionaryThrowsFormatException()
+			throws Exception {
+		new BdfList().getDictionary(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testNegativeIndexForOptionalDictionaryThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalDictionary(-1);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForBooleanThrowsFormatException()
+			throws Exception {
+		new BdfList().getBoolean(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalBooleanThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalBoolean(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForLongThrowsFormatException()
+			throws Exception {
+		new BdfList().getLong(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalLongThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalLong(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForDoubleThrowsFormatException()
+			throws Exception {
+		new BdfList().getDouble(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalDoubleThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalDouble(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForStringThrowsFormatException()
+			throws Exception {
+		new BdfList().getString(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalStringThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalString(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForRawThrowsFormatException()
+			throws Exception {
+		new BdfList().getRaw(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalRawThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalRaw(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForListThrowsFormatException()
+			throws Exception {
+		new BdfList().getList(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalListThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalList(0);
+	}
+
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForDictionaryThrowsFormatException()
+			throws Exception {
+		new BdfList().getDictionary(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testTooLargeIndexForOptionalDictionaryThrowsFormatException()
+			throws Exception {
+		new BdfList().getOptionalDictionary(0);
+	}
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForBooleanThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getBoolean(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalBooleanThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalBoolean(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForLongThrowsFormatException() throws Exception {
+		BdfList.of(1.23).getLong(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalLongThrowsFormatException()
+			throws Exception {
+		BdfList.of(1.23).getOptionalLong(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForDoubleThrowsFormatException() throws Exception {
+		BdfList.of(123).getDouble(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalDoubleThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalDouble(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForStringThrowsFormatException() throws Exception {
+		BdfList.of(123).getString(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalStringThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalString(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForRawThrowsFormatException() throws Exception {
+		BdfList.of(123).getRaw(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalRawThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalRaw(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForListThrowsFormatException() throws Exception {
+		BdfList.of(123).getList(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalListThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalList(0);
+	}
+
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForDictionaryThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getDictionary(0);
+	}
+
+	@Test(expected = FormatException.class)
+	public void testWrongTypeForOptionalDictionaryThrowsFormatException()
+			throws Exception {
+		BdfList.of(123).getOptionalDictionary(0);
+	}
 }
-- 
GitLab