diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfDictionary.java b/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfDictionary.java index c17c19bca..11d6f8a08 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfDictionary.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfDictionary.java @@ -11,7 +11,7 @@ import javax.annotation.Nullable; import javax.annotation.concurrent.NotThreadSafe; @NotThreadSafe -public class BdfDictionary extends TreeMap { +public final class BdfDictionary extends TreeMap { public static final Object NULL_VALUE = new Object(); @@ -39,9 +39,9 @@ public class BdfDictionary extends TreeMap { } public Boolean getBoolean(String key) throws FormatException { - Object o = get(key); - if (o instanceof Boolean) return (Boolean) o; - throw new FormatException(); + Boolean value = getOptionalBoolean(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -52,19 +52,16 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public Boolean getBoolean(String key, Boolean defaultValue) { - Object o = get(key); - if (o instanceof Boolean) return (Boolean) o; - return defaultValue; + public Boolean getBoolean(String key, Boolean defaultValue) + throws FormatException { + Boolean value = getOptionalBoolean(key); + return value == null ? defaultValue : value; } public Long getLong(String key) throws FormatException { - Object o = get(key); - if (o instanceof Long) return (Long) o; - if (o instanceof Integer) return ((Integer) o).longValue(); - if (o instanceof Short) return ((Short) o).longValue(); - if (o instanceof Byte) return ((Byte) o).longValue(); - throw new FormatException(); + Long value = getOptionalLong(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -78,20 +75,37 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public Long getLong(String key, Long defaultValue) { - Object o = get(key); - if (o instanceof Long) return (Long) o; - if (o instanceof Integer) return ((Integer) o).longValue(); - if (o instanceof Short) return ((Short) o).longValue(); - if (o instanceof Byte) return ((Byte) o).longValue(); - return defaultValue; + public Long getLong(String key, Long defaultValue) throws FormatException { + Long value = getOptionalLong(key); + return value == null ? defaultValue : value; + } + + public Integer getInt(String key) throws FormatException { + Integer value = getOptionalInt(key); + if (value == null) throw new FormatException(); + return value; + } + + @Nullable + public Integer getOptionalInt(String key) throws FormatException { + Long value = getOptionalLong(key); + if (value == null) return null; + if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { + throw new FormatException(); + } + return value.intValue(); + } + + public Integer getInt(String key, Integer defaultValue) + throws FormatException { + Integer value = getOptionalInt(key); + return value == null ? defaultValue : value; } public Double getDouble(String key) throws FormatException { - Object o = get(key); - if (o instanceof Double) return (Double) o; - if (o instanceof Float) return ((Float) o).doubleValue(); - throw new FormatException(); + Double value = getOptionalDouble(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -103,17 +117,16 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public Double getDouble(String key, Double defaultValue) { - Object o = get(key); - if (o instanceof Double) return (Double) o; - if (o instanceof Float) return ((Float) o).doubleValue(); - return defaultValue; + public Double getDouble(String key, Double defaultValue) + throws FormatException { + Double value = getOptionalDouble(key); + return value == null ? defaultValue : value; } public String getString(String key) throws FormatException { - Object o = get(key); - if (o instanceof String) return (String) o; - throw new FormatException(); + String value = getOptionalString(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -124,17 +137,16 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public String getString(String key, String defaultValue) { - Object o = get(key); - if (o instanceof String) return (String) o; - return defaultValue; + public String getString(String key, String defaultValue) + throws FormatException { + String value = getOptionalString(key); + return value == null ? defaultValue : value; } public byte[] getRaw(String key) throws FormatException { - Object o = get(key); - if (o instanceof byte[]) return (byte[]) o; - if (o instanceof Bytes) return ((Bytes) o).getBytes(); - throw new FormatException(); + byte[] value = getOptionalRaw(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -146,17 +158,16 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public byte[] getRaw(String key, byte[] defaultValue) { - Object o = get(key); - if (o instanceof byte[]) return (byte[]) o; - if (o instanceof Bytes) return ((Bytes) o).getBytes(); - return defaultValue; + public byte[] getRaw(String key, byte[] defaultValue) + throws FormatException { + byte[] value = getOptionalRaw(key); + return value == null ? defaultValue : value; } public BdfList getList(String key) throws FormatException { - Object o = get(key); - if (o instanceof BdfList) return (BdfList) o; - throw new FormatException(); + BdfList value = getOptionalList(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -167,16 +178,16 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public BdfList getList(String key, BdfList defaultValue) { - Object o = get(key); - if (o instanceof BdfList) return (BdfList) o; - return defaultValue; + public BdfList getList(String key, BdfList defaultValue) + throws FormatException { + BdfList value = getOptionalList(key); + return value == null ? defaultValue : value; } public BdfDictionary getDictionary(String key) throws FormatException { - Object o = get(key); - if (o instanceof BdfDictionary) return (BdfDictionary) o; - throw new FormatException(); + BdfDictionary value = getOptionalDictionary(key); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -188,9 +199,9 @@ public class BdfDictionary extends TreeMap { throw new FormatException(); } - public BdfDictionary getDictionary(String key, BdfDictionary defaultValue) { - Object o = get(key); - if (o instanceof BdfDictionary) return (BdfDictionary) o; - return defaultValue; + public BdfDictionary getDictionary(String key, BdfDictionary defaultValue) + throws FormatException { + BdfDictionary value = getOptionalDictionary(key); + return value == null ? defaultValue : value; } } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfList.java b/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfList.java index 21ec22728..6ba6747cc 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfList.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/data/BdfList.java @@ -13,7 +13,7 @@ import javax.annotation.concurrent.NotThreadSafe; import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE; @NotThreadSafe -public class BdfList extends ArrayList { +public final class BdfList extends ArrayList { /** * Factory method for constructing lists inline. @@ -33,15 +33,15 @@ public class BdfList extends ArrayList { super(items); } + @SuppressWarnings("BooleanMethodIsAlwaysInverted") 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(); + Boolean value = getOptionalBoolean(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -53,21 +53,16 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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 Boolean getBoolean(int index, Boolean defaultValue) + throws FormatException { + Boolean value = getOptionalBoolean(index); + return value == null ? defaultValue : value; } 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(); - if (o instanceof Short) return ((Short) o).longValue(); - if (o instanceof Byte) return ((Byte) o).longValue(); - throw new FormatException(); + Long value = getOptionalLong(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -82,22 +77,37 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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(); - if (o instanceof Short) return ((Short) o).longValue(); - if (o instanceof Byte) return ((Byte) o).longValue(); - return defaultValue; + public Long getLong(int index, Long defaultValue) throws FormatException { + Long value = getOptionalLong(index); + return value == null ? defaultValue : value; + } + + public Integer getInt(int index) throws FormatException { + Integer value = getOptionalInt(index); + if (value == null) throw new FormatException(); + return value; + } + + @Nullable + public Integer getOptionalInt(int index) throws FormatException { + Long value = getOptionalLong(index); + if (value == null) return null; + if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { + throw new FormatException(); + } + return value.intValue(); + } + + public Integer getInt(int index, Integer defaultValue) + throws FormatException { + Integer value = getOptionalInt(index); + return value == null ? defaultValue : value; } 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(); - throw new FormatException(); + Double value = getOptionalDouble(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -110,19 +120,16 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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(); - return defaultValue; + public Double getDouble(int index, Double defaultValue) + throws FormatException { + Double value = getOptionalDouble(index); + return value == null ? defaultValue : value; } 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(); + String value = getOptionalString(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -134,19 +141,16 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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 String getString(int index, String defaultValue) + throws FormatException { + String value = getOptionalString(index); + return value == null ? defaultValue : value; } 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(); - throw new FormatException(); + byte[] value = getOptionalRaw(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -159,19 +163,16 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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(); - return defaultValue; + public byte[] getRaw(int index, byte[] defaultValue) + throws FormatException { + byte[] value = getOptionalRaw(index); + return value == null ? defaultValue : value; } 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(); + BdfList value = getOptionalList(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -183,18 +184,16 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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 BdfList getList(int index, BdfList defaultValue) + throws FormatException { + BdfList value = getOptionalList(index); + return value == null ? defaultValue : value; } 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(); + BdfDictionary value = getOptionalDictionary(index); + if (value == null) throw new FormatException(); + return value; } @Nullable @@ -207,10 +206,9 @@ public class BdfList extends ArrayList { throw new FormatException(); } - 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; + public BdfDictionary getDictionary(int index, BdfDictionary defaultValue) + throws FormatException { + BdfDictionary value = getOptionalDictionary(index); + return value == null ? defaultValue : value; } } diff --git a/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfDictionaryTest.java b/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfDictionaryTest.java index 17bb65116..274834cb3 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfDictionaryTest.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfDictionaryTest.java @@ -1,6 +1,7 @@ package org.briarproject.bramble.api.data; import org.briarproject.bramble.api.Bytes; +import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.test.BrambleTestCase; import org.junit.Test; @@ -8,9 +9,12 @@ import java.util.Collections; import java.util.Iterator; import java.util.Map.Entry; +import static java.lang.Boolean.TRUE; +import static java.util.Collections.singletonMap; import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class BdfDictionaryTest extends BrambleTestCase { @@ -19,20 +23,20 @@ public class BdfDictionaryTest extends BrambleTestCase { public void testConstructors() { assertEquals(Collections.emptyMap(), new BdfDictionary()); - assertEquals(Collections.singletonMap("foo", NULL_VALUE), - new BdfDictionary(Collections.singletonMap("foo", NULL_VALUE))); + assertEquals(singletonMap("foo", NULL_VALUE), + new BdfDictionary(singletonMap("foo", NULL_VALUE))); } @Test public void testFactoryMethod() { assertEquals(Collections.emptyMap(), BdfDictionary.of()); - assertEquals(Collections.singletonMap("foo", NULL_VALUE), + assertEquals(singletonMap("foo", NULL_VALUE), BdfDictionary.of(new BdfEntry("foo", NULL_VALUE))); } @Test - public void testIntegerPromotion() throws Exception { + public void testLongPromotion() throws Exception { BdfDictionary d = new BdfDictionary(); d.put("foo", (byte) 1); d.put("bar", (short) 2); @@ -44,6 +48,33 @@ public class BdfDictionaryTest extends BrambleTestCase { assertEquals(Long.valueOf(4), d.getLong("bam")); } + @Test + public void testIntPromotionAndDemotion() throws Exception { + BdfDictionary d = new BdfDictionary(); + d.put("foo", (byte) 1); + d.put("bar", (short) 2); + d.put("baz", 3); + d.put("bam", 4L); + assertEquals(Integer.valueOf(1), d.getInt("foo")); + assertEquals(Integer.valueOf(2), d.getInt("bar")); + assertEquals(Integer.valueOf(3), d.getInt("baz")); + assertEquals(Integer.valueOf(4), d.getInt("bam")); + } + + @Test(expected = FormatException.class) + public void testIntUnderflow() throws Exception { + BdfDictionary d = + BdfDictionary.of(new BdfEntry("foo", Integer.MIN_VALUE - 1L)); + d.getInt("foo"); + } + + @Test(expected = FormatException.class) + public void testIntOverflow() throws Exception { + BdfDictionary d = + BdfDictionary.of(new BdfEntry("foo", Integer.MAX_VALUE + 1L)); + d.getInt("foo"); + } + @Test public void testFloatPromotion() throws Exception { BdfDictionary d = new BdfDictionary(); @@ -67,7 +98,7 @@ public class BdfDictionaryTest extends BrambleTestCase { } @Test - public void testKeySetIteratorIsOrderedByKeys() throws Exception { + public void testKeySetIteratorIsOrderedByKeys() { BdfDictionary d = new BdfDictionary(); d.put("a", 1); d.put("d", 4); @@ -86,7 +117,7 @@ public class BdfDictionaryTest extends BrambleTestCase { } @Test - public void testValuesIteratorIsOrderedByKeys() throws Exception { + public void testValuesIteratorIsOrderedByKeys() { BdfDictionary d = new BdfDictionary(); d.put("a", 1); d.put("d", 4); @@ -105,7 +136,7 @@ public class BdfDictionaryTest extends BrambleTestCase { } @Test - public void testEntrySetIteratorIsOrderedByKeys() throws Exception { + public void testEntrySetIteratorIsOrderedByKeys() { BdfDictionary d = new BdfDictionary(); d.put("a", 1); d.put("d", 4); @@ -130,4 +161,284 @@ public class BdfDictionaryTest extends BrambleTestCase { assertEquals("d", e.getKey()); assertEquals(4, e.getValue()); } + + @Test(expected = FormatException.class) + public void testMissingValueForBooleanThrowsFormatException() + throws Exception { + new BdfDictionary().getBoolean("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForLongThrowsFormatException() + throws Exception { + new BdfDictionary().getLong("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForIntThrowsFormatException() throws Exception { + new BdfDictionary().getInt("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForDoubleThrowsFormatException() + throws Exception { + new BdfDictionary().getDouble("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForStringThrowsFormatException() + throws Exception { + new BdfDictionary().getString("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForRawThrowsFormatException() throws Exception { + new BdfDictionary().getRaw("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForListThrowsFormatException() + throws Exception { + new BdfDictionary().getList("foo"); + } + + @Test(expected = FormatException.class) + public void testMissingValueForDictionaryThrowsFormatException() + throws Exception { + new BdfDictionary().getDictionary("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForBooleanThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getBoolean("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForLongThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getLong("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForIntThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getInt("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForDoubleThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getDouble("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForStringThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getString("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForRawThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getRaw("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForListThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getList("foo"); + } + + @Test(expected = FormatException.class) + public void testNullValueForDictionaryThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)).getDictionary("foo"); + } + + @Test + public void testOptionalMethodsReturnNullForMissingValue() + throws Exception { + testOptionalMethodsReturnNull(new BdfDictionary()); + } + + @Test + public void testOptionalMethodsReturnNullForNullValue() throws Exception { + BdfDictionary d = BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)); + testOptionalMethodsReturnNull(d); + } + + private void testOptionalMethodsReturnNull(BdfDictionary d) + throws Exception { + assertNull(d.getOptionalBoolean("foo")); + assertNull(d.getOptionalLong("foo")); + assertNull(d.getOptionalInt("foo")); + assertNull(d.getOptionalDouble("foo")); + assertNull(d.getOptionalString("foo")); + assertNull(d.getOptionalRaw("foo")); + assertNull(d.getOptionalList("foo")); + assertNull(d.getOptionalDictionary("foo")); + } + + @Test + public void testDefaultMethodsReturnDefaultForMissingValue() + throws Exception { + testDefaultMethodsReturnDefault(new BdfDictionary()); + } + + @Test + public void testDefaultMethodsReturnDefaultForNullValue() throws Exception { + BdfDictionary d = BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)); + testDefaultMethodsReturnDefault(d); + } + + private void testDefaultMethodsReturnDefault(BdfDictionary d) + throws Exception { + assertEquals(TRUE, d.getBoolean("foo", TRUE)); + assertEquals(Long.valueOf(123L), d.getLong("foo", 123L)); + assertEquals(Integer.valueOf(123), d.getInt("foo", 123)); + assertEquals(Double.valueOf(123D), d.getDouble("foo", 123D)); + assertEquals("123", d.getString("foo", "123")); + byte[] defaultRaw = {1, 2, 3}; + assertArrayEquals(defaultRaw, d.getRaw("foo", defaultRaw)); + BdfList defaultList = BdfList.of(1, 2, 3); + assertEquals(defaultList, d.getList("foo", defaultList)); + BdfDictionary defaultDict = BdfDictionary.of(new BdfEntry("123", 123)); + assertEquals(defaultDict, d.getDictionary("foo", defaultDict)); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForBooleanThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getBoolean("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalBooleanThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalBoolean("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultBooleanThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getBoolean("foo", true); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForLongThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getLong("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalLongThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getOptionalLong("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultLongThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getLong("foo", 1L); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForIntThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getInt("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalIntThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getOptionalInt("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultIntThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 1.23)).getInt("foo", 1); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDoubleThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getDouble("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalDoubleThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalDouble("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultDoubleThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getDouble("foo", 1D); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForStringThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getString("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalStringThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalString("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultStringThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getString("foo", ""); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForRawThrowsFormatException() throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getRaw("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalRawThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalRaw("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultRawThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getRaw("foo", new byte[0]); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForListThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getList("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalListThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalList("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultListThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getList("foo", + new BdfList()); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDictionaryThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getDictionary("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalDictionaryThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getOptionalDictionary("foo"); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultDictionaryThrowsFormatException() + throws Exception { + BdfDictionary.of(new BdfEntry("foo", 123)).getDictionary("foo", + new BdfDictionary()); + } } diff --git a/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfListTest.java b/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfListTest.java index 55fbf39db..7ee0ba799 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfListTest.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/api/data/BdfListTest.java @@ -5,31 +5,31 @@ import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.test.BrambleTestCase; import org.junit.Test; -import java.util.Arrays; -import java.util.Collections; - +import static java.lang.Boolean.TRUE; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; public class BdfListTest extends BrambleTestCase { @Test public void testConstructors() { - assertEquals(Collections.emptyList(), new BdfList()); - assertEquals(Arrays.asList(1, 2, NULL_VALUE), - new BdfList(Arrays.asList(1, 2, NULL_VALUE))); + assertEquals(emptyList(), new BdfList()); + assertEquals(asList(1, 2, NULL_VALUE), + new BdfList(asList(1, 2, NULL_VALUE))); } @Test public void testFactoryMethod() { - assertEquals(Collections.emptyList(), BdfList.of()); - assertEquals(Arrays.asList(1, 2, NULL_VALUE), - BdfList.of(1, 2, NULL_VALUE)); + assertEquals(emptyList(), BdfList.of()); + assertEquals(asList(1, 2, NULL_VALUE), BdfList.of(1, 2, NULL_VALUE)); } @Test - public void testIntegerPromotion() throws Exception { + public void testLongPromotion() throws Exception { BdfList list = new BdfList(); list.add((byte) 1); list.add((short) 2); @@ -41,6 +41,31 @@ public class BdfListTest extends BrambleTestCase { assertEquals(Long.valueOf(4), list.getLong(3)); } + @Test + public void testIntPromotionAndDemotion() throws Exception { + BdfList list = new BdfList(); + list.add((byte) 1); + list.add((short) 2); + list.add(3); + list.add(4L); + assertEquals(Integer.valueOf(1), list.getInt(0)); + assertEquals(Integer.valueOf(2), list.getInt(1)); + assertEquals(Integer.valueOf(3), list.getInt(2)); + assertEquals(Integer.valueOf(4), list.getInt(3)); + } + + @Test(expected = FormatException.class) + public void testIntUnderflow() throws Exception { + BdfList list = BdfList.of(Integer.MIN_VALUE - 1L); + list.getInt(0); + } + + @Test(expected = FormatException.class) + public void testIntOverflow() throws Exception { + BdfList list = BdfList.of(Integer.MAX_VALUE + 1L); + list.getInt(0); + } + @Test public void testFloatPromotion() throws Exception { BdfList list = new BdfList(); @@ -63,61 +88,6 @@ public class BdfListTest extends BrambleTestCase { 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 { @@ -130,6 +100,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalBoolean(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultBooleanThrowsFormatException() + throws Exception { + new BdfList().getBoolean(-1, true); + } + @Test(expected = FormatException.class) public void testNegativeIndexForLongThrowsFormatException() throws Exception { @@ -142,6 +118,30 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalLong(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultLongThrowsFormatException() + throws Exception { + new BdfList().getLong(-1, 1L); + } + + @Test(expected = FormatException.class) + public void testNegativeIndexForIntThrowsFormatException() + throws Exception { + new BdfList().getInt(-1); + } + + @Test(expected = FormatException.class) + public void testNegativeIndexForOptionalIntThrowsFormatException() + throws Exception { + new BdfList().getOptionalInt(-1); + } + + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultIntThrowsFormatException() + throws Exception { + new BdfList().getInt(-1, 1); + } + @Test(expected = FormatException.class) public void testNegativeIndexForDoubleThrowsFormatException() throws Exception { @@ -154,6 +154,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalDouble(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultDoubleThrowsFormatException() + throws Exception { + new BdfList().getDouble(-1, 1D); + } + @Test(expected = FormatException.class) public void testNegativeIndexForStringThrowsFormatException() throws Exception { @@ -166,6 +172,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalString(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultStringThrowsFormatException() + throws Exception { + new BdfList().getString(-1, ""); + } + @Test(expected = FormatException.class) public void testNegativeIndexForRawThrowsFormatException() throws Exception { @@ -178,6 +190,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalRaw(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultRawThrowsFormatException() + throws Exception { + new BdfList().getRaw(-1, new byte[0]); + } + @Test(expected = FormatException.class) public void testNegativeIndexForListThrowsFormatException() throws Exception { @@ -190,6 +208,11 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalList(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultListThrowsFormatException() + throws Exception { + new BdfList().getList(-1, new BdfList()); + } @Test(expected = FormatException.class) public void testNegativeIndexForDictionaryThrowsFormatException() @@ -203,6 +226,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalDictionary(-1); } + @Test(expected = FormatException.class) + public void testNegativeIndexForDefaultDictionaryThrowsFormatException() + throws Exception { + new BdfList().getDictionary(-1, new BdfDictionary()); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForBooleanThrowsFormatException() throws Exception { @@ -215,6 +244,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalBoolean(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultBooleanThrowsFormatException() + throws Exception { + new BdfList().getBoolean(0, true); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForLongThrowsFormatException() throws Exception { @@ -227,6 +262,30 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalLong(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultLongThrowsFormatException() + throws Exception { + new BdfList().getLong(0, 1L); + } + + @Test(expected = FormatException.class) + public void testTooLargeIndexForIntThrowsFormatException() + throws Exception { + new BdfList().getInt(0); + } + + @Test(expected = FormatException.class) + public void testTooLargeIndexForOptionalIntThrowsFormatException() + throws Exception { + new BdfList().getOptionalInt(0); + } + + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultIntThrowsFormatException() + throws Exception { + new BdfList().getInt(0, 1); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForDoubleThrowsFormatException() throws Exception { @@ -239,6 +298,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalDouble(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultDoubleThrowsFormatException() + throws Exception { + new BdfList().getDouble(0, 1D); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForStringThrowsFormatException() throws Exception { @@ -251,6 +316,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalString(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultStringThrowsFormatException() + throws Exception { + new BdfList().getString(0, ""); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForRawThrowsFormatException() throws Exception { @@ -263,6 +334,12 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalRaw(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultRawThrowsFormatException() + throws Exception { + new BdfList().getRaw(0, new byte[0]); + } + @Test(expected = FormatException.class) public void testTooLargeIndexForListThrowsFormatException() throws Exception { @@ -275,6 +352,11 @@ public class BdfListTest extends BrambleTestCase { new BdfList().getOptionalList(0); } + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultListThrowsFormatException() + throws Exception { + new BdfList().getList(0, new BdfList()); + } @Test(expected = FormatException.class) public void testTooLargeIndexForDictionaryThrowsFormatException() @@ -287,6 +369,13 @@ public class BdfListTest extends BrambleTestCase { throws Exception { new BdfList().getOptionalDictionary(0); } + + @Test(expected = FormatException.class) + public void testTooLargeIndexForDefaultDictionaryThrowsFormatException() + throws Exception { + new BdfList().getDictionary(0, new BdfDictionary()); + } + @Test(expected = FormatException.class) public void testWrongTypeForBooleanThrowsFormatException() throws Exception { @@ -299,6 +388,12 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(123).getOptionalBoolean(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultBooleanThrowsFormatException() + throws Exception { + BdfList.of(123).getBoolean(0, true); + } + @Test(expected = FormatException.class) public void testWrongTypeForLongThrowsFormatException() throws Exception { BdfList.of(1.23).getLong(0); @@ -310,6 +405,29 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(1.23).getOptionalLong(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultLongThrowsFormatException() + throws Exception { + BdfList.of(1.23).getLong(0, 1L); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForIntThrowsFormatException() throws Exception { + BdfList.of(1.23).getInt(0); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForOptionalIntThrowsFormatException() + throws Exception { + BdfList.of(1.23).getOptionalInt(0); + } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultIntThrowsFormatException() + throws Exception { + BdfList.of(1.23).getInt(0, 1); + } + @Test(expected = FormatException.class) public void testWrongTypeForDoubleThrowsFormatException() throws Exception { BdfList.of(123).getDouble(0); @@ -321,6 +439,12 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(123).getOptionalDouble(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultDoubleThrowsFormatException() + throws Exception { + BdfList.of(123).getDouble(0, 1D); + } + @Test(expected = FormatException.class) public void testWrongTypeForStringThrowsFormatException() throws Exception { BdfList.of(123).getString(0); @@ -332,6 +456,12 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(123).getOptionalString(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultStringThrowsFormatException() + throws Exception { + BdfList.of(123).getString(0, ""); + } + @Test(expected = FormatException.class) public void testWrongTypeForRawThrowsFormatException() throws Exception { BdfList.of(123).getRaw(0); @@ -343,6 +473,12 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(123).getOptionalRaw(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultRawThrowsFormatException() + throws Exception { + BdfList.of(123).getRaw(0, new byte[0]); + } + @Test(expected = FormatException.class) public void testWrongTypeForListThrowsFormatException() throws Exception { BdfList.of(123).getList(0); @@ -354,6 +490,11 @@ public class BdfListTest extends BrambleTestCase { BdfList.of(123).getOptionalList(0); } + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultListThrowsFormatException() + throws Exception { + BdfList.of(123).getList(0, new BdfList()); + } @Test(expected = FormatException.class) public void testWrongTypeForDictionaryThrowsFormatException() @@ -366,4 +507,81 @@ public class BdfListTest extends BrambleTestCase { throws Exception { BdfList.of(123).getOptionalDictionary(0); } + + @Test(expected = FormatException.class) + public void testWrongTypeForDefaultDictionaryThrowsFormatException() + throws Exception { + BdfList.of(123).getDictionary(0, new BdfDictionary()); + } + + @Test(expected = FormatException.class) + public void testNullValueForBooleanThrowsFormatException() + throws Exception { + BdfList.of(NULL_VALUE).getBoolean(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForLongThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getLong(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForIntThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getInt(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForDoubleThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getDouble(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForStringThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getString(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForRawThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getRaw(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForListThrowsFormatException() throws Exception { + BdfList.of(NULL_VALUE).getList(0); + } + + @Test(expected = FormatException.class) + public void testNullValueForDictionaryThrowsFormatException() + throws Exception { + BdfList.of(NULL_VALUE).getDictionary(0); + } + + @Test + public void testOptionalMethodsReturnNullForNullValue() throws Exception { + BdfList list = BdfList.of(NULL_VALUE); + assertNull(list.getOptionalBoolean(0)); + assertNull(list.getOptionalLong(0)); + assertNull(list.getOptionalInt(0)); + assertNull(list.getOptionalDouble(0)); + assertNull(list.getOptionalString(0)); + assertNull(list.getOptionalRaw(0)); + assertNull(list.getOptionalList(0)); + assertNull(list.getOptionalDictionary(0)); + } + + @Test + public void testDefaultMethodsReturnDefaultForNullValue() throws Exception { + BdfList list = BdfList.of(NULL_VALUE); + assertEquals(TRUE, list.getBoolean(0, TRUE)); + assertEquals(Long.valueOf(123L), list.getLong(0, 123L)); + assertEquals(Integer.valueOf(123), list.getInt(0, 123)); + assertEquals(Double.valueOf(123D), list.getDouble(0, 123D)); + assertEquals("123", list.getString(0, "123")); + byte[] defaultRaw = {1, 2, 3}; + assertArrayEquals(defaultRaw, list.getRaw(0, defaultRaw)); + BdfList defaultList = BdfList.of(1, 2, 3); + assertEquals(defaultList, list.getList(0, defaultList)); + BdfDictionary defaultDict = BdfDictionary.of(new BdfEntry("123", 123)); + assertEquals(defaultDict, list.getDictionary(0, defaultDict)); + } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java index 30d7d641b..34635142d 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java @@ -378,7 +378,7 @@ class ClientHelperImpl implements ClientHelper { public Author parseAndValidateAuthor(BdfList author) throws FormatException { checkSize(author, 3); - int formatVersion = author.getLong(0).intValue(); + int formatVersion = author.getInt(0); if (formatVersion != FORMAT_VERSION) throw new FormatException(); String name = author.getString(1); checkLength(name, 1, MAX_AUTHOR_NAME_LENGTH); @@ -489,8 +489,7 @@ class ClientHelperImpl implements ClientHelper { if (element.size() != 2) { throw new FormatException(); } - list.add(new MailboxVersion(element.getLong(0).intValue(), - element.getLong(1).intValue())); + list.add(new MailboxVersion(element.getInt(0), element.getInt(1))); } // Sort the list of versions for easier comparison sort(list); @@ -503,7 +502,7 @@ class ClientHelperImpl implements ClientHelper { try { BdfDictionary meta = getGroupMetadataAsDictionary(txn, contactGroupId); - return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue()); + return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID)); } catch (FormatException e) { throw new DbException(e); // Invalid group metadata } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java index 1354900a7..a4250c7a8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java @@ -419,7 +419,7 @@ class LanTcpPlugin extends TcpPlugin { private InetSocketAddress parseSocketAddress(BdfList descriptor) throws FormatException { byte[] address = descriptor.getRaw(1); - int port = descriptor.getLong(2).intValue(); + int port = descriptor.getInt(2); if (port < 1 || port > MAX_16_BIT_UNSIGNED) throw new FormatException(); try { InetAddress addr = InetAddress.getByAddress(address); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/SessionParserImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/SessionParserImpl.java index a0d18f380..160a134a8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/SessionParserImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/SessionParserImpl.java @@ -32,8 +32,7 @@ class SessionParserImpl implements SessionParser { @Override public Session parseSession(BdfDictionary meta) throws FormatException { - State state = - State.fromValue(meta.getLong(SESSION_KEY_STATE).intValue()); + State state = State.fromValue(meta.getInt(SESSION_KEY_STATE)); MessageId lastLocalMessageId = null; byte[] lastLocalMessageIdBytes = @@ -56,9 +55,9 @@ class SessionParserImpl implements SessionParser { Long localTimestamp = meta.getOptionalLong(SESSION_KEY_LOCAL_TIMESTAMP); KeySetId keySetId = null; - Long keySetIdLong = meta.getOptionalLong(SESSION_KEY_KEY_SET_ID); - if (keySetIdLong != null) { - keySetId = new KeySetId(keySetIdLong.intValue()); + Integer keySetIdInt = meta.getOptionalInt(SESSION_KEY_KEY_SET_ID); + if (keySetIdInt != null) { + keySetId = new KeySetId(keySetIdInt); } return new Session(state, lastLocalMessageId, localKeyPair, diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementManagerImpl.java index 82a17af1e..d092709ae 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementManagerImpl.java @@ -177,8 +177,8 @@ class TransportKeyAgreementManagerImpl extends BdfIncomingMessageHook protected DeliveryAction incomingMessage(Transaction txn, Message m, BdfList body, BdfDictionary meta) throws DbException, FormatException { - MessageType type = MessageType.fromValue( - meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue()); + MessageType type = + MessageType.fromValue(meta.getInt(MSG_KEY_MESSAGE_TYPE)); TransportId t = new TransportId(meta.getString(MSG_KEY_TRANSPORT_ID)); if (LOG.isLoggable(INFO)) { LOG.info("Received " + type + " message for " + t); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementValidator.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementValidator.java index ca43a94c4..9133d3e8b 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementValidator.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/agreement/TransportKeyAgreementValidator.java @@ -42,7 +42,7 @@ class TransportKeyAgreementValidator extends BdfMessageValidator { @Override protected BdfMessageContext validateMessage(Message m, Group g, BdfList body) throws FormatException { - MessageType type = MessageType.fromValue(body.getLong(0).intValue()); + MessageType type = MessageType.fromValue(body.getInt(0)); if (type == KEY) return validateKeyMessage(m.getTimestamp(), body); else if (type == ACTIVATE) return validateActivateMessage(body); else throw new AssertionError(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java index 232b71f1b..1dc50702e 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java @@ -301,8 +301,8 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, for (int i = 0; i < size; i++) { BdfList cv = body.getList(i); ClientId clientId = new ClientId(cv.getString(0)); - int majorVersion = cv.getLong(1).intValue(); - int minorVersion = cv.getLong(2).intValue(); + int majorVersion = cv.getInt(1); + int minorVersion = cv.getInt(2); parsed.add(new ClientVersion(clientId, majorVersion, minorVersion)); } return parsed; @@ -408,8 +408,8 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, throws FormatException { // Client ID, major version, minor version, active ClientId clientId = new ClientId(clientState.getString(0)); - int majorVersion = clientState.getLong(1).intValue(); - int minorVersion = clientState.getLong(2).intValue(); + int majorVersion = clientState.getInt(1); + int minorVersion = clientState.getInt(2); boolean active = clientState.getBoolean(3); return new ClientState(clientId, majorVersion, minorVersion, active); } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningValidator.java b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningValidator.java index b6fbb0c5d..2171a3d75 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningValidator.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningValidator.java @@ -43,9 +43,9 @@ class ClientVersioningValidator extends BdfMessageValidator { checkSize(clientState, 4); String clientId = clientState.getString(0); checkLength(clientId, 1, MAX_CLIENT_ID_LENGTH); - int majorVersion = clientState.getLong(1).intValue(); + int majorVersion = clientState.getInt(1); if (majorVersion < 0) throw new FormatException(); - int minorVersion = clientState.getLong(2).intValue(); + int minorVersion = clientState.getInt(2); if (minorVersion < 0) throw new FormatException(); clientState.getBoolean(3); } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java index 6f6881599..6a890e33c 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java @@ -223,7 +223,7 @@ public class LanTcpPluginTest extends BrambleTestCase { assertTrue(addr instanceof Inet4Address); assertFalse(addr.isLoopbackAddress()); assertTrue(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()); - int port = descriptor.getLong(2).intValue(); + int port = descriptor.getInt(2); assertTrue(port > 0 && port < 65536); // The plugin should be listening on the port InetSocketAddress socketAddr = new InetSocketAddress(addr, port); diff --git a/briar-core/src/main/java/org/briarproject/briar/attachment/AttachmentReaderImpl.java b/briar-core/src/main/java/org/briarproject/briar/attachment/AttachmentReaderImpl.java index 69d5f963f..fb981a6b4 100644 --- a/briar-core/src/main/java/org/briarproject/briar/attachment/AttachmentReaderImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/attachment/AttachmentReaderImpl.java @@ -56,7 +56,7 @@ public class AttachmentReaderImpl implements AttachmentReader { String contentType = meta.getString(MSG_KEY_CONTENT_TYPE); if (!contentType.equals(h.getContentType())) throw new NoSuchMessageException(); - int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue(); + int offset = meta.getInt(MSG_KEY_DESCRIPTOR_LENGTH); InputStream stream = new ByteArrayInputStream(body, offset, body.length - offset); return new Attachment(h, stream); diff --git a/briar-core/src/main/java/org/briarproject/briar/avatar/AvatarManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/avatar/AvatarManagerImpl.java index 2c720367c..4e8875eec 100644 --- a/briar-core/src/main/java/org/briarproject/briar/avatar/AvatarManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/avatar/AvatarManagerImpl.java @@ -250,7 +250,7 @@ class AvatarManagerImpl implements AvatarManager, OpenDatabaseHook, ContactHook, try { BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(txn, g); - return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue()); + return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID)); } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java index 2c55ff4fd..77786f026 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogManagerImpl.java @@ -487,7 +487,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager, } private String getPostText(BdfList message) throws FormatException { - MessageType type = MessageType.valueOf(message.getLong(0).intValue()); + MessageType type = MessageType.valueOf(message.getInt(0)); if (type == POST) { // Type, text, signature return message.getString(1); @@ -621,7 +621,6 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager, } private MessageType getMessageType(BdfDictionary d) throws FormatException { - Long longType = d.getLong(KEY_TYPE); - return MessageType.valueOf(longType.intValue()); + return MessageType.valueOf(d.getInt(KEY_TYPE)); } } diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostFactoryImpl.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostFactoryImpl.java index 4bef7fe85..64ae88196 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostFactoryImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostFactoryImpl.java @@ -167,6 +167,6 @@ class BlogPostFactoryImpl implements BlogPostFactory { } private MessageType getType(BdfList body) throws FormatException { - return MessageType.valueOf(body.getLong(0).intValue()); + return MessageType.valueOf(body.getInt(0)); } } diff --git a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java index d4c135281..b17953892 100644 --- a/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/blog/BlogPostValidator.java @@ -72,7 +72,7 @@ class BlogPostValidator extends BdfMessageValidator { BdfMessageContext c; - int type = body.getLong(0).intValue(); + int type = body.getInt(0); body.remove(0); switch (MessageType.valueOf(type)) { case POST: diff --git a/briar-core/src/main/java/org/briarproject/briar/client/MessageTrackerImpl.java b/briar-core/src/main/java/org/briarproject/briar/client/MessageTrackerImpl.java index 1da615926..43a2ef207 100644 --- a/briar-core/src/main/java/org/briarproject/briar/client/MessageTrackerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/client/MessageTrackerImpl.java @@ -114,8 +114,8 @@ class MessageTrackerImpl implements MessageTracker { try { BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g); return new GroupCount( - d.getLong(GROUP_KEY_MSG_COUNT, 0L).intValue(), - d.getLong(GROUP_KEY_UNREAD_COUNT, 0L).intValue(), + d.getInt(GROUP_KEY_MSG_COUNT, 0), + d.getInt(GROUP_KEY_UNREAD_COUNT, 0), d.getLong(GROUP_KEY_LATEST_MSG, 0L) ); } catch (FormatException e) { diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java index 02fa86274..a13c045a7 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionValidator.java @@ -47,7 +47,7 @@ class IntroductionValidator extends BdfMessageValidator { @Override protected BdfMessageContext validateMessage(Message m, Group g, BdfList body) throws FormatException { - MessageType type = MessageType.fromValue(body.getLong(0).intValue()); + MessageType type = MessageType.fromValue(body.getInt(0)); switch (type) { case REQUEST: diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/MessageParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/introduction/MessageParserImpl.java index 5fd50c59c..5d1ae3c28 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/MessageParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/MessageParserImpl.java @@ -59,8 +59,8 @@ class MessageParserImpl implements MessageParser { @Override public MessageMetadata parseMetadata(BdfDictionary d) throws FormatException { - MessageType type = MessageType - .fromValue(d.getLong(MSG_KEY_MESSAGE_TYPE).intValue()); + MessageType type = + MessageType.fromValue(d.getInt(MSG_KEY_MESSAGE_TYPE)); byte[] sessionIdBytes = d.getOptionalRaw(MSG_KEY_SESSION_ID); SessionId sessionId = sessionIdBytes == null ? null : new SessionId(sessionIdBytes); diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/SessionParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/introduction/SessionParserImpl.java index 203ebec57..ccf2da794 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/SessionParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/SessionParserImpl.java @@ -72,7 +72,7 @@ class SessionParserImpl implements SessionParser { @Override public Role getRole(BdfDictionary d) throws FormatException { - return Role.fromValue(d.getLong(SESSION_KEY_ROLE).intValue()); + return Role.fromValue(d.getInt(SESSION_KEY_ROLE)); } @Override @@ -97,7 +97,7 @@ class SessionParserImpl implements SessionParser { MessageId lastRemoteMessageId = getMessageId(d, SESSION_KEY_LAST_REMOTE_MESSAGE_ID); long localTimestamp = d.getLong(SESSION_KEY_LOCAL_TIMESTAMP); - GroupId groupId = getGroupId(d, SESSION_KEY_GROUP_ID); + GroupId groupId = getGroupId(d); Author author = getAuthor(d, SESSION_KEY_AUTHOR); return new Introducee(sessionId, groupId, author, localTimestamp, lastLocalMessageId, lastRemoteMessageId); @@ -126,12 +126,10 @@ class SessionParserImpl implements SessionParser { MessageId lastLocalMessageId = getMessageId(d, SESSION_KEY_LAST_LOCAL_MESSAGE_ID); long localTimestamp = d.getLong(SESSION_KEY_LOCAL_TIMESTAMP); - PublicKey ephemeralPublicKey = - getEphemeralPublicKey(d, SESSION_KEY_EPHEMERAL_PUBLIC_KEY); + PublicKey ephemeralPublicKey = getEphemeralPublicKey(d); BdfDictionary tpDict = d.getOptionalDictionary(SESSION_KEY_TRANSPORT_PROPERTIES); - PrivateKey ephemeralPrivateKey = - getEphemeralPrivateKey(d, SESSION_KEY_EPHEMERAL_PRIVATE_KEY); + PrivateKey ephemeralPrivateKey = getEphemeralPrivateKey(d); Map transportProperties = tpDict == null ? null : clientHelper .parseAndValidateTransportPropertiesMap(tpDict); @@ -147,8 +145,7 @@ class SessionParserImpl implements SessionParser { Author remoteAuthor = getAuthor(d, SESSION_KEY_REMOTE_AUTHOR); MessageId lastRemoteMessageId = getMessageId(d, SESSION_KEY_LAST_REMOTE_MESSAGE_ID); - PublicKey ephemeralPublicKey = - getEphemeralPublicKey(d, SESSION_KEY_EPHEMERAL_PUBLIC_KEY); + PublicKey ephemeralPublicKey = getEphemeralPublicKey(d); BdfDictionary tpDict = d.getOptionalDictionary(SESSION_KEY_TRANSPORT_PROPERTIES); Map transportProperties = @@ -162,7 +159,7 @@ class SessionParserImpl implements SessionParser { } private int getState(BdfDictionary d) throws FormatException { - return d.getLong(SESSION_KEY_STATE).intValue(); + return d.getInt(SESSION_KEY_STATE); } private SessionId getSessionId(BdfDictionary d) throws FormatException { @@ -177,9 +174,8 @@ class SessionParserImpl implements SessionParser { return b == null ? null : new MessageId(b); } - private GroupId getGroupId(BdfDictionary d, String key) - throws FormatException { - return new GroupId(d.getRaw(key)); + private GroupId getGroupId(BdfDictionary d) throws FormatException { + return new GroupId(d.getRaw(SESSION_KEY_GROUP_ID)); } private Author getAuthor(BdfDictionary d, String key) @@ -193,23 +189,22 @@ class SessionParserImpl implements SessionParser { if (d == null) return null; Map map = new HashMap<>(d.size()); for (String key : d.keySet()) { - map.put(new TransportId(key), - new KeySetId(d.getLong(key).intValue())); + map.put(new TransportId(key), new KeySetId(d.getInt(key))); } return map; } @Nullable - private PublicKey getEphemeralPublicKey(BdfDictionary d, String key) - throws FormatException { - byte[] keyBytes = d.getOptionalRaw(key); + private PublicKey getEphemeralPublicKey(BdfDictionary d) + throws FormatException { + byte[] keyBytes = d.getOptionalRaw(SESSION_KEY_EPHEMERAL_PUBLIC_KEY); return keyBytes == null ? null : new AgreementPublicKey(keyBytes); } @Nullable - private PrivateKey getEphemeralPrivateKey(BdfDictionary d, String key) - throws FormatException { - byte[] keyBytes = d.getOptionalRaw(key); + private PrivateKey getEphemeralPrivateKey(BdfDictionary d) + throws FormatException { + byte[] keyBytes = d.getOptionalRaw(SESSION_KEY_EPHEMERAL_PRIVATE_KEY); return keyBytes == null ? null : new AgreementPrivateKey(keyBytes); } } diff --git a/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java index af0460fd9..1d0a594fe 100644 --- a/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java @@ -371,7 +371,7 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook, try { BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(txn, g); - return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue()); + return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID)); } catch (FormatException e) { throw new DbException(e); } @@ -381,7 +381,7 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook, public ContactId getContactId(GroupId g) throws DbException { try { BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(g); - return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue()); + return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID)); } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java b/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java index 161ea062f..83e8b3036 100644 --- a/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/messaging/PrivateMessageValidator.java @@ -84,7 +84,7 @@ class PrivateMessageValidator implements MessageValidator { context = validateLegacyPrivateMessage(m, list); } else { // Private message or attachment - int messageType = list.getLong(0).intValue(); + int messageType = list.getInt(0); if (messageType == PRIVATE_MESSAGE) { if (!reader.eof()) throw new FormatException(); context = validatePrivateMessage(m, list); diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/GroupMessageValidator.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/GroupMessageValidator.java index 02abed607..0ca99a1f2 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/GroupMessageValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/GroupMessageValidator.java @@ -63,7 +63,7 @@ class GroupMessageValidator extends BdfMessageValidator { checkSize(body, 4, 6); // Message type (int) - int type = body.getLong(0).intValue(); + int type = body.getInt(0); // Member (author) BdfList memberList = body.getList(1); diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java index 3a5ca662a..ff4825c0c 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java @@ -530,8 +530,7 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook BdfList body, BdfDictionary meta) throws DbException, FormatException { - MessageType type = - MessageType.valueOf(meta.getLong(KEY_TYPE).intValue()); + MessageType type = MessageType.valueOf(meta.getInt(KEY_TYPE)); switch (type) { case JOIN: handleJoinMessage(txn, m, meta); @@ -576,8 +575,8 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook .getMessageMetadataAsDictionary(txn, parentId); if (timestamp <= parentMeta.getLong(KEY_TIMESTAMP)) throw new FormatException(); - MessageType parentType = MessageType - .valueOf(parentMeta.getLong(KEY_TYPE).intValue()); + MessageType parentType = + MessageType.valueOf(parentMeta.getInt(KEY_TYPE)); if (parentType != POST) throw new FormatException(); } @@ -592,8 +591,8 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook if (!getAuthor(meta).equals(getAuthor(previousMeta))) throw new FormatException(); // previous message must be a POST or JOIN - MessageType previousType = MessageType - .valueOf(previousMeta.getLong(KEY_TYPE).intValue()); + MessageType previousType = + MessageType.valueOf(previousMeta.getInt(KEY_TYPE)); if (previousType != JOIN && previousType != POST) throw new FormatException(); // track message and broadcast event @@ -641,8 +640,7 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook private Visibility getVisibility(BdfDictionary meta) throws FormatException { - return Visibility - .valueOf(meta.getLong(GROUP_KEY_VISIBILITY).intValue()); + return Visibility.valueOf(meta.getInt(GROUP_KEY_VISIBILITY)); } } diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationManagerImpl.java index cd78802a5..26742b104 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationManagerImpl.java @@ -712,9 +712,9 @@ class GroupInvitationManagerImpl extends ConversationClientImpl // get all sessions and their states Map sessions = new HashMap<>(); for (BdfDictionary d : metadata.values()) { - if (!sessionParser.isSession(d)) continue; Session session; try { + if (!sessionParser.isSession(d)) continue; session = sessionParser.parseSession(g, d); } catch (FormatException e) { throw new DbException(e); @@ -776,13 +776,12 @@ class GroupInvitationManagerImpl extends ConversationClientImpl // assign protocol messages to their sessions for (Entry entry : metadata.entrySet()) { - // skip all sessions, we are only interested in messages - BdfDictionary d = entry.getValue(); - if (sessionParser.isSession(d)) continue; - // parse message metadata and skip messages not visible in UI MessageMetadata m; try { + // skip all sessions, we are only interested in messages + BdfDictionary d = entry.getValue(); + if (sessionParser.isSession(d)) continue; m = messageParser.parseMetadata(d); } catch (FormatException e) { throw new DbException(e); diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java index f04d38fa5..616bfad25 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/GroupInvitationValidator.java @@ -56,7 +56,7 @@ class GroupInvitationValidator extends BdfMessageValidator { @Override protected BdfMessageContext validateMessage(Message m, Group g, BdfList body) throws FormatException { - MessageType type = MessageType.fromValue(body.getLong(0).intValue()); + MessageType type = MessageType.fromValue(body.getInt(0)); switch (type) { case INVITE: return validateInviteMessage(m, body); diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/MessageParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/MessageParserImpl.java index 244ac6853..33725f426 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/MessageParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/MessageParserImpl.java @@ -71,8 +71,8 @@ class MessageParserImpl implements MessageParser { @Override public MessageMetadata parseMetadata(BdfDictionary meta) throws FormatException { - MessageType type = MessageType.fromValue( - meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue()); + MessageType type = + MessageType.fromValue(meta.getInt(MSG_KEY_MESSAGE_TYPE)); GroupId privateGroupId = new GroupId(meta.getRaw(MSG_KEY_PRIVATE_GROUP_ID)); long timestamp = meta.getLong(MSG_KEY_TIMESTAMP); diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParser.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParser.java index 976c5e3df..f59153888 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParser.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParser.java @@ -15,7 +15,7 @@ interface SessionParser { Role getRole(BdfDictionary d) throws FormatException; - boolean isSession(BdfDictionary d); + boolean isSession(BdfDictionary d) throws FormatException; Session parseSession(GroupId contactGroupId, BdfDictionary d) throws FormatException; diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParserImpl.java index a21c80273..4d316ee2e 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/invitation/SessionParserImpl.java @@ -45,11 +45,11 @@ class SessionParserImpl implements SessionParser { @Override public Role getRole(BdfDictionary d) throws FormatException { - return Role.fromValue(d.getLong(SESSION_KEY_ROLE).intValue()); + return Role.fromValue(d.getInt(SESSION_KEY_ROLE)); } @Override - public boolean isSession(BdfDictionary d) { + public boolean isSession(BdfDictionary d) throws FormatException { return d.getBoolean(SESSION_KEY_IS_SESSION, false); } @@ -101,7 +101,7 @@ class SessionParserImpl implements SessionParser { } private int getState(BdfDictionary d) throws FormatException { - return d.getLong(SESSION_KEY_STATE).intValue(); + return d.getInt(SESSION_KEY_STATE); } private GroupId getPrivateGroupId(BdfDictionary d) throws FormatException { diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/MessageParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/sharing/MessageParserImpl.java index dd8768bcc..124aa4d84 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/MessageParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/MessageParserImpl.java @@ -64,8 +64,8 @@ abstract class MessageParserImpl @Override public MessageMetadata parseMetadata(BdfDictionary meta) throws FormatException { - MessageType type = MessageType - .fromValue(meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue()); + MessageType type = + MessageType.fromValue(meta.getInt(MSG_KEY_MESSAGE_TYPE)); GroupId shareableId = new GroupId(meta.getRaw(MSG_KEY_SHAREABLE_ID)); long timestamp = meta.getLong(MSG_KEY_TIMESTAMP); boolean local = meta.getBoolean(MSG_KEY_LOCAL); diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParser.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParser.java index 7eb84706b..3235a73dc 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParser.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParser.java @@ -13,7 +13,7 @@ interface SessionParser { BdfDictionary getAllSessionsQuery(); - boolean isSession(BdfDictionary d); + boolean isSession(BdfDictionary d) throws FormatException; Session parseSession(GroupId contactGroupId, BdfDictionary d) throws FormatException; diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParserImpl.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParserImpl.java index a29b25e0c..027b65850 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParserImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SessionParserImpl.java @@ -40,7 +40,7 @@ class SessionParserImpl implements SessionParser { } @Override - public boolean isSession(BdfDictionary d) { + public boolean isSession(BdfDictionary d) throws FormatException { return d.getBoolean(SESSION_KEY_IS_SESSION, false); } @@ -54,7 +54,7 @@ class SessionParserImpl implements SessionParser { } private int getState(BdfDictionary d) throws FormatException { - return d.getLong(SESSION_KEY_STATE).intValue(); + return d.getInt(SESSION_KEY_STATE); } private GroupId getShareableId(BdfDictionary d) throws FormatException { diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java index b34b9965e..9b74a2b08 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java @@ -604,9 +604,9 @@ abstract class SharingManagerImpl // get all sessions and their states Map sessions = new HashMap<>(); for (BdfDictionary d : metadata.values()) { - if (!sessionParser.isSession(d)) continue; Session session; try { + if (!sessionParser.isSession(d)) continue; session = sessionParser.parseSession(contactGroup, d); } catch (FormatException e) { throw new DbException(e); @@ -668,13 +668,12 @@ abstract class SharingManagerImpl // assign protocol messages to their sessions for (Entry entry : metadata.entrySet()) { - // skip all sessions, we are only interested in messages - BdfDictionary d = entry.getValue(); - if (sessionParser.isSession(d)) continue; - // parse message metadata and skip messages not visible in UI MessageMetadata m; try { + // skip all sessions, we are only interested in messages + BdfDictionary d = entry.getValue(); + if (sessionParser.isSession(d)) continue; m = messageParser.parseMetadata(d); } catch (FormatException e) { throw new DbException(e); diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java index e03af82d7..cf68fe62d 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingValidator.java @@ -40,7 +40,7 @@ abstract class SharingValidator extends BdfMessageValidator { @Override protected BdfMessageContext validateMessage(Message m, Group g, BdfList body) throws FormatException { - MessageType type = MessageType.fromValue(body.getLong(0).intValue()); + MessageType type = MessageType.fromValue(body.getInt(0)); switch (type) { case INVITE: return validateInviteMessage(m, body); diff --git a/briar-core/src/test/java/org/briarproject/briar/privategroup/GroupMessageValidatorTest.java b/briar-core/src/test/java/org/briarproject/briar/privategroup/GroupMessageValidatorTest.java index b8ed89b16..3932d1c99 100644 --- a/briar-core/src/test/java/org/briarproject/briar/privategroup/GroupMessageValidatorTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/privategroup/GroupMessageValidatorTest.java @@ -651,7 +651,7 @@ public class GroupMessageValidatorTest extends ValidatorTestCase { MessageType type, BdfList member, Collection dependencies) throws FormatException { BdfDictionary d = c.getDictionary(); - assertEquals(type.getInt(), d.getLong(KEY_TYPE).intValue()); + assertEquals(type.getInt(), d.getInt(KEY_TYPE).intValue()); assertEquals(message.getTimestamp(), d.getLong(KEY_TIMESTAMP).longValue()); assertFalse(d.getBoolean(KEY_READ));