mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Renamed "user-defined types" as "structs" in the serialisation format.
This commit is contained in:
@@ -51,7 +51,7 @@ public class AckReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.ACK, Ack.class);
|
||||
reader.readStruct(Types.ACK, Ack.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -73,7 +73,7 @@ public class AckReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
assertEquals(ack, reader.readUserDefined(Types.ACK, Ack.class));
|
||||
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -93,25 +93,25 @@ public class AckReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
assertEquals(ack, reader.readUserDefined(Types.ACK, Ack.class));
|
||||
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
private byte[] createAck(boolean tooBig) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeStructId(Types.ACK);
|
||||
w.writeListStart();
|
||||
byte[] b = new byte[UniqueId.LENGTH];
|
||||
Random random = new Random();
|
||||
while(out.size() + BatchId.LENGTH + 3
|
||||
< ProtocolConstants.MAX_PACKET_LENGTH) {
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeStructId(Types.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
}
|
||||
if(tooBig) {
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeStructId(Types.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public class AckReaderTest extends TestCase {
|
||||
private byte[] createEmptyAck() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeStructId(Types.ACK);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
|
||||
@@ -61,7 +61,7 @@ public class BatchReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.BATCH, Batch.class);
|
||||
reader.readStruct(Types.BATCH, Batch.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -85,7 +85,7 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -136,17 +136,17 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
private byte[] createBatch(int size) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeStructId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
// We're using a fake message reader, so it's OK to use a fake message
|
||||
w.writeUserDefinedId(Types.MESSAGE);
|
||||
w.writeStructId(Types.MESSAGE);
|
||||
w.writeBytes(new byte[size - 10]);
|
||||
w.writeListEnd();
|
||||
byte[] b = out.toByteArray();
|
||||
@@ -157,7 +157,7 @@ public class BatchReaderTest extends TestCase {
|
||||
private byte[] createEmptyBatch() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeStructId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
@@ -166,7 +166,7 @@ public class BatchReaderTest extends TestCase {
|
||||
private class TestMessageReader implements ObjectReader<Message> {
|
||||
|
||||
public Message readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.MESSAGE);
|
||||
r.readStructId(Types.MESSAGE);
|
||||
r.readBytes();
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class RequestReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.REQUEST, Request.class);
|
||||
reader.readStruct(Types.REQUEST, Request.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -69,7 +69,7 @@ public class RequestReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
|
||||
assertEquals(request, reader.readUserDefined(Types.REQUEST,
|
||||
assertEquals(request, reader.readStruct(Types.REQUEST,
|
||||
Request.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -99,7 +99,7 @@ public class RequestReaderTest extends TestCase {
|
||||
RequestReader requestReader =
|
||||
new RequestReader(new RequestFactoryImpl());
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
Request r = reader.readUserDefined(Types.REQUEST, Request.class);
|
||||
Request r = reader.readStruct(Types.REQUEST, Request.class);
|
||||
BitSet decoded = r.getBitmap();
|
||||
// Check that the decoded BitSet matches the original - we can't
|
||||
// use equals() because of padding, but the first i bits should
|
||||
@@ -115,7 +115,7 @@ public class RequestReaderTest extends TestCase {
|
||||
private byte[] createRequest(boolean tooBig) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeStructId(Types.REQUEST);
|
||||
// Allow one byte for the REQUEST tag, one byte for the BYTES tag,
|
||||
// and five bytes for the length as an int32
|
||||
int size = ProtocolConstants.MAX_PACKET_LENGTH - 7;
|
||||
@@ -128,7 +128,7 @@ public class RequestReaderTest extends TestCase {
|
||||
private byte[] createRequest(byte[] bitmap) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeStructId(Types.REQUEST);
|
||||
w.writeBytes(bitmap);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -371,41 +371,41 @@ public class ReaderImplTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUserDefined() throws Exception {
|
||||
public void testReadStruct() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(255);
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
// Test both tag formats, short and long
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertEquals("foo", r.readUserDefined(0, Foo.class).s);
|
||||
assertTrue(r.hasUserDefined(255));
|
||||
assertEquals("foo", r.readUserDefined(255, Bar.class).s);
|
||||
// Test both ID formats, short and long
|
||||
assertTrue(r.hasStruct(0));
|
||||
assertEquals("foo", r.readStruct(0, Foo.class).s);
|
||||
assertTrue(r.hasStruct(255));
|
||||
assertEquals("foo", r.readStruct(255, Bar.class).s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUserDefinedWithConsumer() throws Exception {
|
||||
public void testReadStructWithConsumer() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(255);
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
@@ -421,23 +421,23 @@ public class ReaderImplTest extends TestCase {
|
||||
out.write(b, off, len);
|
||||
}
|
||||
});
|
||||
// Test both tag formats, short and long
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertEquals("foo", r.readUserDefined(0, Foo.class).s);
|
||||
assertTrue(r.hasUserDefined(255));
|
||||
assertEquals("foo", r.readUserDefined(255, Bar.class).s);
|
||||
// Test both ID formats, short and long
|
||||
assertTrue(r.hasStruct(0));
|
||||
assertEquals("foo", r.readStruct(0, Foo.class).s);
|
||||
assertTrue(r.hasStruct(255));
|
||||
assertEquals("foo", r.readStruct(255, Bar.class).s);
|
||||
// Check that everything was passed to the consumer
|
||||
assertEquals("C0" + "83666F6F" + "EF" + "FF" + "83666F6F",
|
||||
StringUtils.toHexString(out.toByteArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnknownTagThrowsFormatException() throws Exception {
|
||||
public void testUnknownStructIdThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
// No object reader has been added for tag 0
|
||||
assertTrue(r.hasStruct(0));
|
||||
// No object reader has been added for struct ID 0
|
||||
try {
|
||||
r.readUserDefined(0, Foo.class);
|
||||
r.readStruct(0, Foo.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
@@ -445,17 +445,17 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testWrongClassThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
// Add an object reader for tag 0, class Foo
|
||||
// Add an object reader for struct ID 0, class Foo
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertTrue(r.hasStruct(0));
|
||||
// Trying to read the object as class Bar should throw a FormatException
|
||||
try {
|
||||
r.readUserDefined(0, Bar.class);
|
||||
r.readStruct(0, Bar.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
@@ -463,10 +463,10 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testReadListUsingObjectReader() throws Exception {
|
||||
setContents("A" + "1" + "C0" + "83666F6F");
|
||||
// Add an object reader for a user-defined type
|
||||
// Add an object reader for a struct
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
@@ -479,16 +479,16 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testReadMapUsingObjectReader() throws Exception {
|
||||
setContents("B" + "1" + "C0" + "83666F6F" + "C1" + "83626172");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(1, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(1);
|
||||
r.readStructId(1);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -265,19 +265,19 @@ public class WriterImplTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteShortUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedId(0);
|
||||
w.writeUserDefinedId(31);
|
||||
// SHORT_USER tag (3 bits), 0 (5 bits), SHORT_USER tag (3 bits),
|
||||
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 testWriteUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedId(32);
|
||||
w.writeUserDefinedId(255);
|
||||
// USER tag, 32 as uint8, USER tag, 255 as uint8
|
||||
public void testWriteStructId() throws IOException {
|
||||
w.writeStructId(32);
|
||||
w.writeStructId(255);
|
||||
// STRUCT tag, 32 as uint8, STRUCT tag, 255 as uint8
|
||||
checkContents("EF" + "20" + "EF" + "FF");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user