Simplified serialisation format: removed compact encodings.

This commit is contained in:
akwizgran
2013-10-11 15:16:16 +01:00
parent 38c1b4eb97
commit e0d313a28c
7 changed files with 70 additions and 246 deletions

View File

@@ -214,10 +214,10 @@ public class PacketReaderImplTest extends BriarTestCase {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(REQUEST);
// Allow one byte for the REQUEST tag, one byte for the padding length
// as a uint7, one byte for the BYTES tag, and five bytes for the
// length of the byte array as an int32
int size = MAX_PACKET_LENGTH - 8;
// Allow one byte for the STRUCT tag, one byte for the REQUEST tag,
// one byte for the padding length as a uint7, one byte for the BYTES
// tag, and five bytes for the length of the byte array as an int32
int size = MAX_PACKET_LENGTH - 9;
if(tooBig) size++;
assertTrue(size > Short.MAX_VALUE);
w.writeUint7((byte) 0);

View File

@@ -60,9 +60,10 @@ public class PacketWriterImplTest extends BriarTestCase {
b.set(12);
b.set(15);
w.writeRequest(new Request(b, 16));
// Short user tag 5, 0 as uint7, short bytes with length 2, 0xD959
// STRUCT tag, struct ID 5, 0 as uint7, BYTES tag, length 2 as uint7,
// 0xD959
byte[] output = out.toByteArray();
assertEquals("C5" + "00" + "92" + "D959",
assertEquals("F1" + "05" + "00" + "F6" + "02" + "D959",
StringUtils.toHexString(output));
}
@@ -83,9 +84,10 @@ public class PacketWriterImplTest extends BriarTestCase {
b.set(11);
b.set(12);
w.writeRequest(new Request(b, 13));
// Short user tag 5, 3 as uint7, short bytes with length 2, 0x59D8
// STRUCT tag, struct ID 5, 3 as uint7, BYTES tag, length 2 as uint7,
// 0xD959
byte[] output = out.toByteArray();
assertEquals("C5" + "03" + "92" + "59D8",
assertEquals("F1" + "05" + "03" + "F6" + "02" + "59D8",
StringUtils.toHexString(output));
}
}

View File

@@ -121,17 +121,15 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadString() throws Exception {
setContents("F703666F6F" + "83666F6F" + "F700" + "80");
setContents("F703666F6F" + "F700");
assertEquals("foo", r.readString());
assertEquals("foo", r.readString());
assertEquals("", r.readString());
assertEquals("", r.readString());
assertTrue(r.eof());
}
@Test
public void testReadStringMaxLength() throws Exception {
setContents("83666F6F" + "83666F6F");
setContents("F703666F6F" + "F703666F6F");
assertEquals("foo", r.readString(3));
try {
r.readString(2);
@@ -141,17 +139,15 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadBytes() throws Exception {
setContents("F603010203" + "93010203" + "F600" + "90");
setContents("F603010203" + "F600");
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes());
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes());
assertArrayEquals(new byte[] {}, r.readBytes());
assertArrayEquals(new byte[] {}, r.readBytes());
assertTrue(r.eof());
}
@Test
public void testReadBytesMaxLength() throws Exception {
setContents("93010203" + "93010203");
setContents("F603010203" + "F603010203");
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(3));
try {
r.readBytes(2);
@@ -159,21 +155,9 @@ public class ReaderImplTest extends BriarTestCase {
} catch(FormatException expected) {}
}
@Test
public void testReadShortList() throws Exception {
setContents("A" + "3" + "01" + "83666F6F" + "FC0080");
List<Object> l = r.readList(Object.class);
assertNotNull(l);
assertEquals(3, l.size());
assertEquals((byte) 1, l.get(0));
assertEquals("foo", l.get(1));
assertEquals((short) 128, l.get(2));
assertTrue(r.eof());
}
@Test
public void testReadList() throws Exception {
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F3");
List<Object> l = r.readList(Object.class);
assertNotNull(l);
assertEquals(3, l.size());
@@ -185,7 +169,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadListTypeSafe() throws Exception {
setContents("A" + "3" + "01" + "02" + "03");
setContents("F5" + "01" + "02" + "03" + "F3");
List<Byte> l = r.readList(Byte.class);
assertNotNull(l);
assertEquals(3, l.size());
@@ -197,7 +181,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadListTypeSafeThrowsFormatException() throws Exception {
setContents("A" + "3" + "01" + "83666F6F" + "03");
setContents("F5" + "01" + "F703666F6F" + "03" + "F3");
// Trying to read a mixed list as a list of bytes should throw a
// FormatException
try {
@@ -206,22 +190,9 @@ public class ReaderImplTest extends BriarTestCase {
} catch(FormatException expected) {}
}
@Test
public void testReadShortMap() throws Exception {
setContents("B" + "2" + "83666F6F" + "7B" + "90" + "F2");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Bytes b = new Bytes(new byte[] {});
assertTrue(m.containsKey(b));
assertNull(m.get(b));
assertTrue(r.eof());
}
@Test
public void testReadMap() throws Exception {
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F2" + "F3");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -234,7 +205,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadMapTypeSafe() throws Exception {
setContents("B" + "2" + "83666F6F" + "7B" + "80" + "F2");
setContents("F4" + "F703666F6F" + "7B" + "F700" + "F2" + "F3");
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -246,8 +217,8 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testMapKeysMustBeUnique() throws Exception {
setContents("B" + "2" + "83666F6F" + "01" + "83626172" + "02"
+ "B" + "2" + "83666F6F" + "01" + "83666F6F" + "02");
setContents("F4" + "F703666F6F" + "01" + "F703626172" + "02" + "F3"
+ "F4" + "F703666F6F" + "01" + "F703666F6F" + "02" + "F3");
// The first map has unique keys
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
@@ -261,21 +232,9 @@ public class ReaderImplTest extends BriarTestCase {
} catch(FormatException expected) {}
}
@Test
public void testReadDelimitedList() throws Exception {
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
List<Object> l = r.readList(Object.class);
assertNotNull(l);
assertEquals(3, l.size());
assertEquals((byte) 1, l.get(0));
assertEquals("foo", l.get(1));
assertEquals((short) 128, l.get(2));
assertTrue(r.eof());
}
@Test
public void testReadDelimitedListElements() throws Exception {
setContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F3");
assertTrue(r.hasListStart());
r.readListStart();
assertFalse(r.hasListEnd());
@@ -301,22 +260,9 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof());
}
@Test
public void testReadDelimitedMap() throws Exception {
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Bytes b = new Bytes(new byte[] {});
assertTrue(m.containsKey(b));
assertNull(m.get(b));
assertTrue(r.eof());
}
@Test
public void testReadDelimitedMapEntries() throws Exception {
setContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F2" + "F3");
assertTrue(r.hasMapStart());
r.readMapStart();
assertFalse(r.hasMapEnd());
@@ -335,7 +281,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testReadDelimitedMapTypeSafe() throws Exception {
setContents("F4" + "83666F6F" + "7B" + "80" + "F2" + "F3");
setContents("F4" + "F703666F6F" + "7B" + "F700" + "F2" + "F3");
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
@@ -348,8 +294,8 @@ public class ReaderImplTest extends BriarTestCase {
@Test
@SuppressWarnings("unchecked")
public void testReadNestedMapsAndLists() throws Exception {
setContents("B" + "1" + "B" + "1" + "83666F6F" + "7B"
+ "A" + "1" + "01");
setContents("F4" + "F4" + "F703666F6F" + "7B" + "F3"
+ "F5" + "01" + "F3" + "F3");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(1, m.size());
@@ -368,7 +314,7 @@ public class ReaderImplTest extends BriarTestCase {
@Test
public void testMaxLengthAppliesInsideMap() throws Exception {
setContents("B" + "1" + "83666F6F" + "93010203");
setContents("F4" + "F703666F6F" + "F603010203" + "F3");
r.setMaxStringLength(3);
r.setMaxBytesLength(3);
Map<String, Bytes> m = r.readMap(String.class, Bytes.class);
@@ -376,14 +322,14 @@ public class ReaderImplTest extends BriarTestCase {
Bytes value = new Bytes(new byte[] {1, 2, 3});
assertEquals(Collections.singletonMap(key, value), m);
// The max string length should be applied inside the map
setContents("B" + "1" + "83666F6F" + "93010203");
setContents("F4" + "F703666F6F" + "F603010203" + "F3");
r.setMaxStringLength(2);
try {
r.readMap(String.class, Bytes.class);
fail();
} catch(FormatException expected) {}
// The max bytes length should be applied inside the map
setContents("B" + "1" + "83666F6F" + "93010203");
setContents("F4" + "F703666F6F" + "F603010203" + "F3");
r.setMaxBytesLength(2);
try {
r.readMap(String.class, Bytes.class);

View File

@@ -133,13 +133,6 @@ public class WriterImplTest extends BriarTestCase {
+ "F8" + "7FF0000000000000" + "F8" + "7FF8000000000000");
}
@Test
public void testWriteShortString() throws IOException {
w.writeString("foo bar baz bam");
// SHORT_STRING tag, length 15, UTF-8 bytes
checkContents("8" + "F" + "666F6F206261722062617A2062616D");
}
@Test
public void testWriteString() throws IOException {
w.writeString("foo bar baz bam ");
@@ -147,15 +140,6 @@ public class WriterImplTest extends BriarTestCase {
checkContents("F7" + "10" + "666F6F206261722062617A2062616D20");
}
@Test
public void testWriteShortBytes() throws IOException {
w.writeBytes(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
});
// SHORT_BYTES tag, length 15, bytes
checkContents("9" + "F" + "000102030405060708090A0B0C0D0E");
}
@Test
public void testWriteBytes() throws IOException {
w.writeBytes(new byte[] {
@@ -165,15 +149,6 @@ public class WriterImplTest extends BriarTestCase {
checkContents("F6" + "10" + "000102030405060708090A0B0C0D0E0F");
}
@Test
public void testWriteShortList() throws IOException {
List<Object> l = new ArrayList<Object>();
for(int i = 0; i < 15; i++) l.add(i);
w.writeList(l);
// SHORT_LIST tag, length, elements as uint7
checkContents("A" + "F" + "000102030405060708090A0B0C0D0E");
}
@Test
public void testWriteList() throws IOException {
List<Object> l = new ArrayList<Object>();
@@ -190,20 +165,8 @@ public class WriterImplTest extends BriarTestCase {
l.add(null);
l.add(2);
w.writeList(l);
// SHORT_LIST tag, length, 1 as uint7, null, 2 as uint7
checkContents("A" + "3" + "01" + "F2" + "02");
}
@Test
public void testWriteShortMap() throws IOException {
// Use LinkedHashMap to get predictable iteration order
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
for(int i = 0; i < 15; i++) m.put(i, i + 1);
w.writeMap(m);
// SHORT_MAP tag, size, entries as uint7
checkContents("B" + "F" + "0001" + "0102" + "0203" + "0304" + "0405"
+ "0506" + "0607" + "0708" + "0809" + "090A" + "0A0B" + "0B0C"
+ "0C0D" + "0D0E" + "0E0F");
// LIST tag, 1 as uint7, null, 2 as uint7, END tag
checkContents("F5" + "01" + "F2" + "02" + "F3");
}
@Test
@@ -222,25 +185,24 @@ public class WriterImplTest extends BriarTestCase {
public void testWriteDelimitedList() throws IOException {
w.writeListStart();
w.writeIntAny((byte) 1); // Written as uint7
w.writeString("foo"); // Written as short string
w.writeIntAny(128L); // Written as an int16
w.writeString("foo"); // Written as string
w.writeIntAny(128L); // Written as int16
w.writeListEnd();
// LIST tag, 1 as uint7, "foo" as short string, 128 as int16,
// END tag
checkContents("F5" + "01" + "83666F6F" + "FC0080" + "F3");
// LIST tag, 1 as uint7, "foo" as string, 128 as int16, END tag
checkContents("F5" + "01" + "F703666F6F" + "FC0080" + "F3");
}
@Test
public void testWriteDelimitedMap() throws IOException {
w.writeMapStart();
w.writeString("foo"); // Written as short string
w.writeIntAny(123); // Written as a uint7
w.writeBytes(new byte[] {}); // Written as short bytes
w.writeString("foo"); // Written as string
w.writeIntAny(123); // Written as uint7
w.writeBytes(new byte[0]); // Written as bytes
w.writeNull();
w.writeMapEnd();
// MAP tag, "foo" as short string, 123 as uint7,
// byte[] {} as short bytes, NULL tag, END tag
checkContents("F4" + "83666F6F" + "7B" + "90" + "F2" + "F3");
// MAP tag, "foo" as string, 123 as uint7, byte[0] as bytes,
// NULL tag, END tag
checkContents("F4" + "F703666F6F" + "7B" + "F600" + "F2" + "F3");
}
@Test
@@ -252,10 +214,10 @@ public class WriterImplTest extends BriarTestCase {
Map<Object, Object> m1 = new LinkedHashMap<Object, Object>();
m1.put(m, l);
w.writeMap(m1);
// SHORT_MAP tag, length 1, SHORT_MAP tag, length 1,
// "foo" as short string, 123 as uint7, SHORT_LIST tag, length 1,
// 1 as uint7
checkContents("B" + "1" + "B" + "1" + "83666F6F" + "7B" + "A1" + "01");
// MAP tag, MAP tag, "foo" as string, 123 as uint7, END tag,
// LIST tag, 1 as uint7, END tag, END tag
checkContents("F4" + "F4" + "F703666F6F" + "7B" + "F3"
+ "F5" + "01" + "F3" + "F3");
}
@Test
@@ -264,15 +226,6 @@ public class WriterImplTest extends BriarTestCase {
checkContents("F2");
}
@Test
public void testWriteShortStructId() throws IOException {
w.writeStructId(0);
w.writeStructId(31);
// SHORT_STRUCT tag (3 bits), 0 (5 bits), SHORT_STRUCT tag (3 bits),
// 31 (5 bits)
checkContents("C0" + "DF");
}
@Test
public void testWriteStructId() throws IOException {
w.writeStructId(32);