mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Merge branch 'metadata-encoder-parser-tests' into 'master'
Add tests for metadata encoder and parser as well as UTF-8 tests for BDF reader and writer This also includes a commit to fix issues found in the metadata encoder and parser. See merge request !58
This commit is contained in:
@@ -68,7 +68,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
|
|
||||||
private void encodeInteger(ByteArrayOutputStream out, byte i) {
|
private void encodeInteger(ByteArrayOutputStream out, byte i) {
|
||||||
out.write(INT_8);
|
out.write(INT_8);
|
||||||
out.write(i);
|
encodeInt8(out, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeInteger(ByteArrayOutputStream out, short i) {
|
private void encodeInteger(ByteArrayOutputStream out, short i) {
|
||||||
@@ -76,8 +76,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
encodeInteger(out, (byte) i);
|
encodeInteger(out, (byte) i);
|
||||||
} else {
|
} else {
|
||||||
out.write(INT_16);
|
out.write(INT_16);
|
||||||
out.write((byte) (i >> 8));
|
encodeInt16(out, i);
|
||||||
out.write((byte) ((i << 8) >> 8));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,10 +85,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
encodeInteger(out, (short) i);
|
encodeInteger(out, (short) i);
|
||||||
} else {
|
} else {
|
||||||
out.write(INT_32);
|
out.write(INT_32);
|
||||||
out.write((byte) (i >> 24));
|
encodeInt32(out, i);
|
||||||
out.write((byte) ((i << 8) >> 24));
|
|
||||||
out.write((byte) ((i << 16) >> 24));
|
|
||||||
out.write((byte) ((i << 24) >> 24));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,24 +94,27 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
encodeInteger(out, (int) i);
|
encodeInteger(out, (int) i);
|
||||||
} else {
|
} else {
|
||||||
out.write(INT_64);
|
out.write(INT_64);
|
||||||
out.write((byte) (i >> 56));
|
encodeInt64(out, i);
|
||||||
out.write((byte) ((i << 8) >> 56));
|
|
||||||
out.write((byte) ((i << 16) >> 56));
|
|
||||||
out.write((byte) ((i << 24) >> 56));
|
|
||||||
out.write((byte) ((i << 32) >> 56));
|
|
||||||
out.write((byte) ((i << 40) >> 56));
|
|
||||||
out.write((byte) ((i << 48) >> 56));
|
|
||||||
out.write((byte) ((i << 56) >> 56));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeFloat(ByteArrayOutputStream out, float f) {
|
private void encodeInt8(ByteArrayOutputStream out, byte i) {
|
||||||
encodeFloat(out, (double) f);
|
out.write(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeFloat(ByteArrayOutputStream out, double d) {
|
private void encodeInt16(ByteArrayOutputStream out, short i) {
|
||||||
long i = Double.doubleToLongBits(d);
|
out.write((byte) (i >> 8));
|
||||||
out.write(FLOAT_64);
|
out.write((byte) ((i << 8) >> 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void encodeInt32(ByteArrayOutputStream out, int i) {
|
||||||
|
out.write((byte) (i >> 24));
|
||||||
|
out.write((byte) ((i << 8) >> 24));
|
||||||
|
out.write((byte) ((i << 16) >> 24));
|
||||||
|
out.write((byte) ((i << 24) >> 24));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void encodeInt64(ByteArrayOutputStream out, long i) {
|
||||||
out.write((byte) (i >> 56));
|
out.write((byte) (i >> 56));
|
||||||
out.write((byte) ((i << 8) >> 56));
|
out.write((byte) ((i << 8) >> 56));
|
||||||
out.write((byte) ((i << 16) >> 56));
|
out.write((byte) ((i << 16) >> 56));
|
||||||
@@ -126,17 +125,26 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
out.write((byte) ((i << 56) >> 56));
|
out.write((byte) ((i << 56) >> 56));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void encodeFloat(ByteArrayOutputStream out, float f) {
|
||||||
|
encodeFloat(out, (double) f);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void encodeFloat(ByteArrayOutputStream out, double d) {
|
||||||
|
out.write(FLOAT_64);
|
||||||
|
encodeInt64(out, Double.doubleToLongBits(d));
|
||||||
|
}
|
||||||
|
|
||||||
private void encodeString(ByteArrayOutputStream out, String s) {
|
private void encodeString(ByteArrayOutputStream out, String s) {
|
||||||
byte[] b = StringUtils.toUtf8(s);
|
byte[] b = StringUtils.toUtf8(s);
|
||||||
if (b.length <= Byte.MAX_VALUE) {
|
if (b.length <= Byte.MAX_VALUE) {
|
||||||
out.write(STRING_8);
|
out.write(STRING_8);
|
||||||
encodeInteger(out, (byte) b.length);
|
encodeInt8(out, (byte) b.length);
|
||||||
} else if (b.length <= Short.MAX_VALUE) {
|
} else if (b.length <= Short.MAX_VALUE) {
|
||||||
out.write(STRING_16);
|
out.write(STRING_16);
|
||||||
encodeInteger(out, (short) b.length);
|
encodeInt16(out, (short) b.length);
|
||||||
} else {
|
} else {
|
||||||
out.write(STRING_32);
|
out.write(STRING_32);
|
||||||
encodeInteger(out, b.length);
|
encodeInt32(out, b.length);
|
||||||
}
|
}
|
||||||
out.write(b, 0, b.length);
|
out.write(b, 0, b.length);
|
||||||
}
|
}
|
||||||
@@ -144,13 +152,13 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
private void encodeRaw(ByteArrayOutputStream out, byte[] b) {
|
private void encodeRaw(ByteArrayOutputStream out, byte[] b) {
|
||||||
if (b.length <= Byte.MAX_VALUE) {
|
if (b.length <= Byte.MAX_VALUE) {
|
||||||
out.write(RAW_8);
|
out.write(RAW_8);
|
||||||
encodeInteger(out, (byte) b.length);
|
encodeInt8(out, (byte) b.length);
|
||||||
} else if (b.length <= Short.MAX_VALUE) {
|
} else if (b.length <= Short.MAX_VALUE) {
|
||||||
out.write(RAW_16);
|
out.write(RAW_16);
|
||||||
encodeInteger(out, (short) b.length);
|
encodeInt16(out, (short) b.length);
|
||||||
} else {
|
} else {
|
||||||
out.write(RAW_32);
|
out.write(RAW_32);
|
||||||
encodeInteger(out, b.length);
|
encodeInt32(out, b.length);
|
||||||
}
|
}
|
||||||
out.write(b, 0, b.length);
|
out.write(b, 0, b.length);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,16 +143,18 @@ class MetadataParserImpl implements MetadataParser {
|
|||||||
|
|
||||||
private BdfList parseList(ByteArrayInputStream in) throws FormatException {
|
private BdfList parseList(ByteArrayInputStream in) throws FormatException {
|
||||||
BdfList list = new BdfList();
|
BdfList list = new BdfList();
|
||||||
while (peek(in) != END) list.add(parseObject(in));
|
for (int b = peek(in); b != -1 && b != (END & 0xFF); b = peek(in))
|
||||||
if (in.read() != END) throw new FormatException();
|
list.add(parseObject(in));
|
||||||
|
if (in.read() != (END & 0xFF)) throw new FormatException();
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BdfDictionary parseDictionary(ByteArrayInputStream in)
|
private BdfDictionary parseDictionary(ByteArrayInputStream in)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
BdfDictionary dict = new BdfDictionary();
|
BdfDictionary dict = new BdfDictionary();
|
||||||
while (peek(in) != END) dict.put(parseString(in), parseObject(in));
|
for (int b = peek(in); b != -1 && b != (END & 0xFF); b = peek(in))
|
||||||
if (in.read() != END) throw new FormatException();
|
dict.put(parseString(in), parseObject(in));
|
||||||
|
if (in.read() != (END & 0xFF)) throw new FormatException();
|
||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -258,6 +258,19 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadUtf8String() throws Exception {
|
||||||
|
String str = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \uFDD0\uFDD1\uFDD2\uFDD3\uFDD1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
||||||
|
String strHex = StringUtils.toHexString(str.getBytes("UTF-8"));
|
||||||
|
// STRING_8 tag, "foo", the empty string, and the test string
|
||||||
|
setContents("41" + "03" + "666F6F" + "41" + "00" +
|
||||||
|
"41" + "35" + strHex);
|
||||||
|
assertEquals("foo", r.readString(Integer.MAX_VALUE));
|
||||||
|
assertEquals("", r.readString(Integer.MAX_VALUE));
|
||||||
|
assertEquals(str, r.readString(Integer.MAX_VALUE));
|
||||||
|
assertTrue(r.eof());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadRaw8() throws Exception {
|
public void testReadRaw8() throws Exception {
|
||||||
byte[] longest = new byte[Byte.MAX_VALUE];
|
byte[] longest = new byte[Byte.MAX_VALUE];
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ import org.junit.Test;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
public class BdfWriterImplTest extends BriarTestCase {
|
public class BdfWriterImplTest extends BriarTestCase {
|
||||||
|
|
||||||
@@ -113,6 +112,15 @@ public class BdfWriterImplTest extends BriarTestCase {
|
|||||||
checkContents("44" + "00008000" + shortHex);
|
checkContents("44" + "00008000" + shortHex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriteUtf8String() throws IOException {
|
||||||
|
String str = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \uFDD0\uFDD1\uFDD2\uFDD3\uFDD1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
||||||
|
String strHex = StringUtils.toHexString(str.getBytes("UTF-8"));
|
||||||
|
w.writeString(str);
|
||||||
|
// STRING_8 tag, length 53, UTF-8 bytes
|
||||||
|
checkContents("41" + "35" + strHex);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteBytes8() throws IOException {
|
public void testWriteBytes8() throws IOException {
|
||||||
byte[] longest = new byte[Byte.MAX_VALUE];
|
byte[] longest = new byte[Byte.MAX_VALUE];
|
||||||
@@ -225,7 +233,7 @@ public class BdfWriterImplTest extends BriarTestCase {
|
|||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
byte[] expected = StringUtils.fromHexString(hex);
|
byte[] expected = StringUtils.fromHexString(hex);
|
||||||
assertTrue(StringUtils.toHexString(out.toByteArray()),
|
assertArrayEquals(StringUtils.toHexString(out.toByteArray()),
|
||||||
Arrays.equals(expected, out.toByteArray()));
|
expected, out.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package org.briarproject.data;
|
|
||||||
|
|
||||||
import org.briarproject.BriarTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
public class MetadataEncoderImplTest extends BriarTestCase {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnitTestsExist() {
|
|
||||||
fail(); // FIXME: Write tests
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
package org.briarproject.data;
|
||||||
|
|
||||||
|
import org.briarproject.BriarTestCase;
|
||||||
|
import org.briarproject.api.FormatException;
|
||||||
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
|
import org.briarproject.api.db.Metadata;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class MetadataEncoderParserImplTest extends BriarTestCase {
|
||||||
|
|
||||||
|
MetadataEncoderImpl e = new MetadataEncoderImpl();
|
||||||
|
MetadataParserImpl p = new MetadataParserImpl();
|
||||||
|
BdfDictionary d = new BdfDictionary();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBoolean() throws FormatException {
|
||||||
|
d.put("test", true);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(true, p.parse(metadata).getBoolean("test", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInteger() throws FormatException {
|
||||||
|
d.put("test", 1337);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(1337L, (long) p.parse(metadata).getInteger("test", 0L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLong() throws FormatException {
|
||||||
|
d.put("test", Long.MAX_VALUE);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(Long.MAX_VALUE,
|
||||||
|
(long) p.parse(metadata).getInteger("test", 0L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDouble() throws FormatException {
|
||||||
|
d.put("test", Double.MAX_VALUE);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(Double.MAX_VALUE,
|
||||||
|
p.parse(metadata).getFloat("test", 0.0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFloat() throws FormatException {
|
||||||
|
d.put("test", Float.MIN_NORMAL);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(Float.MIN_NORMAL,
|
||||||
|
p.parse(metadata).getFloat("test", 0.0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testString() throws FormatException {
|
||||||
|
d.put("test", "abc");
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals("abc", p.parse(metadata).getString("test", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUtf8String() throws FormatException {
|
||||||
|
d.put("test", "abcdefghilkmnopqrst <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \uFDD0\uFDD1\uFDD2\uFDD3");
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals("abcdefghilkmnopqrst <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \uFDD0\uFDD1\uFDD2\uFDD3",
|
||||||
|
p.parse(metadata).getString("test", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRaw() throws FormatException {
|
||||||
|
byte[] b = "\uFDD0\uFDD1\uFDD2\uFDD3".getBytes();
|
||||||
|
d.put("test", b);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertArrayEquals(b, p.parse(metadata).getRaw("test", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testList() throws FormatException {
|
||||||
|
List<Long> l = new ArrayList<Long>(4);
|
||||||
|
l.add(42L);
|
||||||
|
l.add(1337L);
|
||||||
|
l.add(Long.MIN_VALUE);
|
||||||
|
l.add(Long.MAX_VALUE);
|
||||||
|
|
||||||
|
d.put("test", l);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertArrayEquals(l.toArray(),
|
||||||
|
p.parse(metadata).getList("test", null).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDictionary() throws FormatException {
|
||||||
|
Map<String, Boolean> m = new HashMap<String, Boolean>();
|
||||||
|
m.put("1", true);
|
||||||
|
m.put("2", false);
|
||||||
|
|
||||||
|
d.put("test", m);
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals(true, p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getBoolean("1", false));
|
||||||
|
|
||||||
|
assertEquals(false, p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getBoolean("2", true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testComplexDictionary() throws FormatException {
|
||||||
|
Map<String, List> m = new HashMap<String, List>();
|
||||||
|
List<String> one = new ArrayList<String>(3);
|
||||||
|
one.add("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
one.add("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
one.add("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
m.put("One", one);
|
||||||
|
List<String> two = new ArrayList<String>(2);
|
||||||
|
two.add("\u0080");
|
||||||
|
two.add("\uD800\uDC00");
|
||||||
|
m.put("Two", two);
|
||||||
|
d.put("test", m);
|
||||||
|
|
||||||
|
Map<String, Boolean> m2 = new HashMap<String, Boolean>();
|
||||||
|
m2.put("should be true", true);
|
||||||
|
d.put("another test", m2);
|
||||||
|
|
||||||
|
Metadata metadata = e.encode(d);
|
||||||
|
|
||||||
|
assertEquals("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getList("One", null).get(0));
|
||||||
|
assertEquals("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getList("One", null).get(1));
|
||||||
|
assertEquals("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getList("One", null).get(2));
|
||||||
|
assertEquals("\u0080", p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getList("Two", null).get(0));
|
||||||
|
assertEquals("\uD800\uDC00", p.parse(metadata).getDictionary("test", null)
|
||||||
|
.getList("Two", null).get(1));
|
||||||
|
|
||||||
|
assertEquals(true, p.parse(metadata).getDictionary("another test", null)
|
||||||
|
.getBoolean("should be true", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package org.briarproject.data;
|
|
||||||
|
|
||||||
import org.briarproject.BriarTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
public class MetadataParserImplTest extends BriarTestCase {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnitTestsExist() {
|
|
||||||
fail(); // FIXME: Write tests
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user