Added the ability to skip serialised objects.

This commit is contained in:
akwizgran
2013-11-19 21:28:53 +00:00
parent 6764ade475
commit 2e472c1d16
5 changed files with 380 additions and 334 deletions

View File

@@ -1,67 +1,79 @@
package net.sf.briar.api.serial; package net.sf.briar.api.serial;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map;
public interface Reader { public interface Reader {
boolean eof() throws IOException; boolean eof() throws IOException;
void close() throws IOException; void close() throws IOException;
void setMaxStringLength(int length);
void resetMaxStringLength();
void setMaxBytesLength(int length);
void resetMaxBytesLength();
void addConsumer(Consumer c); void addConsumer(Consumer c);
void removeConsumer(Consumer c); void removeConsumer(Consumer c);
boolean hasBoolean() throws IOException; boolean hasBoolean() throws IOException;
boolean readBoolean() throws IOException; boolean readBoolean() throws IOException;
void skipBoolean() throws IOException;
boolean hasUint7() throws IOException; boolean hasUint7() throws IOException;
byte readUint7() throws IOException; byte readUint7() throws IOException;
void skipUint7() throws IOException;
boolean hasInt8() throws IOException; boolean hasInt8() throws IOException;
byte readInt8() throws IOException; byte readInt8() throws IOException;
void skipInt8() throws IOException;
boolean hasInt16() throws IOException; boolean hasInt16() throws IOException;
short readInt16() throws IOException; short readInt16() throws IOException;
void skipInt16() throws IOException;
boolean hasInt32() throws IOException; boolean hasInt32() throws IOException;
int readInt32() throws IOException; int readInt32() throws IOException;
void skipInt32() throws IOException;
boolean hasInt64() throws IOException; boolean hasInt64() throws IOException;
long readInt64() throws IOException; long readInt64() throws IOException;
void skipInt64() throws IOException;
boolean hasIntAny() throws IOException; boolean hasIntAny() throws IOException;
long readIntAny() throws IOException; long readIntAny() throws IOException;
void skipIntAny() throws IOException;
boolean hasFloat32() throws IOException; boolean hasFloat32() throws IOException;
float readFloat32() throws IOException; float readFloat32() throws IOException;
void skipFloat32() throws IOException;
boolean hasFloat64() throws IOException; boolean hasFloat64() throws IOException;
double readFloat64() throws IOException; double readFloat64() throws IOException;
void skipFloat64() throws IOException;
boolean hasString() throws IOException; boolean hasString() throws IOException;
String readString(int maxLength) throws IOException; String readString(int maxLength) throws IOException;
void skipString(int maxLength) throws IOException;
boolean hasBytes() throws IOException; boolean hasBytes() throws IOException;
byte[] readBytes(int maxLength) throws IOException; byte[] readBytes(int maxLength) throws IOException;
void skipBytes(int maxLength) throws IOException;
boolean hasList() throws IOException; boolean hasList() throws IOException;
<E> List<E> readList(Class<E> e) throws IOException;
void readListStart() throws IOException; void readListStart() throws IOException;
boolean hasListEnd() throws IOException; boolean hasListEnd() throws IOException;
void readListEnd() throws IOException; void readListEnd() throws IOException;
void skipList() throws IOException;
boolean hasMap() throws IOException; boolean hasMap() throws IOException;
<K, V> Map<K, V> readMap(Class<K> k, Class<V> v) throws IOException;
void readMapStart() throws IOException; void readMapStart() throws IOException;
boolean hasMapEnd() throws IOException; boolean hasMapEnd() throws IOException;
void readMapEnd() throws IOException; void readMapEnd() throws IOException;
void skipMap() throws IOException;
boolean hasStruct() throws IOException;
boolean hasStruct(int id) throws IOException; boolean hasStruct(int id) throws IOException;
void readStructStart(int id) throws IOException; void readStructStart(int id) throws IOException;
boolean hasStructEnd() throws IOException; boolean hasStructEnd() throws IOException;
void readStructEnd() throws IOException; void readStructEnd() throws IOException;
void skipStruct() throws IOException;
boolean hasNull() throws IOException; boolean hasNull() throws IOException;
void readNull() throws IOException; void readNull() throws IOException;
void skipNull() throws IOException;
} }

View File

@@ -244,11 +244,16 @@ abstract class Connector extends Thread {
byte[] b = r.readBytes(UniqueId.LENGTH); byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException(); if(b.length != UniqueId.LENGTH) throw new FormatException();
TransportId id = new TransportId(b); TransportId id = new TransportId(b);
r.setMaxStringLength(MAX_PROPERTY_LENGTH); Map<String, String> p = new HashMap<String, String>();
Map<String, String> p = r.readMap(String.class, String.class); r.readMapStart();
r.resetMaxStringLength(); for(int i = 0; !r.hasMapEnd(); i++) {
if(p.size() > MAX_PROPERTIES_PER_TRANSPORT) if(i == MAX_PROPERTIES_PER_TRANSPORT)
throw new FormatException(); throw new FormatException();
String key = r.readString(MAX_PROPERTY_LENGTH);
String value = r.readString(MAX_PROPERTY_LENGTH);
p.put(key, value);
}
r.readMapEnd();
remoteProps.put(id, new TransportProperties(p)); remoteProps.put(id, new TransportProperties(p));
} }
r.readListEnd(); r.readListEnd();

View File

@@ -19,10 +19,10 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.Bytes;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
@@ -75,22 +75,21 @@ class PacketReaderImpl implements PacketReader {
r.addConsumer(counting); r.addConsumer(counting);
// Read the start of the struct // Read the start of the struct
r.readStructStart(ACK); r.readStructStart(ACK);
// Read the message IDs as byte arrays // Read the message IDs
r.setMaxBytesLength(UniqueId.LENGTH); List<MessageId> acked = new ArrayList<MessageId>();
List<Bytes> raw = r.readList(Bytes.class); r.readListStart();
while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH)
throw new FormatException();
acked.add(new MessageId(b));
}
if(acked.isEmpty()) throw new FormatException();
r.readListEnd();
// Read the end of the struct // Read the end of the struct
r.readStructEnd(); r.readStructEnd();
// Reset the reader // Reset the reader
r.resetMaxBytesLength();
r.removeConsumer(counting); r.removeConsumer(counting);
if(raw.isEmpty()) throw new FormatException();
// Convert the byte arrays to message IDs
List<MessageId> acked = new ArrayList<MessageId>();
for(Bytes b : raw) {
if(b.getBytes().length != UniqueId.LENGTH)
throw new FormatException();
acked.add(new MessageId(b.getBytes()));
}
// Build and return the ack // Build and return the ack
return new Ack(Collections.unmodifiableList(acked)); return new Ack(Collections.unmodifiableList(acked));
} }
@@ -113,22 +112,21 @@ class PacketReaderImpl implements PacketReader {
r.addConsumer(counting); r.addConsumer(counting);
// Read the start of the struct // Read the start of the struct
r.readStructStart(OFFER); r.readStructStart(OFFER);
// Read the message IDs as byte arrays // Read the message IDs
r.setMaxBytesLength(UniqueId.LENGTH); List<MessageId> messages = new ArrayList<MessageId>();
List<Bytes> raw = r.readList(Bytes.class); r.readListStart();
while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH)
throw new FormatException();
messages.add(new MessageId(b));
}
if(messages.isEmpty()) throw new FormatException();
r.readListEnd();
// Read the end of the struct // Read the end of the struct
r.readStructEnd(); r.readStructEnd();
// Reset the reader // Reset the reader
r.resetMaxBytesLength();
r.removeConsumer(counting); r.removeConsumer(counting);
if(raw.isEmpty()) throw new FormatException();
// Convert the byte arrays to message IDs
List<MessageId> messages = new ArrayList<MessageId>();
for(Bytes b : raw) {
if(b.getBytes().length != UniqueId.LENGTH)
throw new FormatException();
messages.add(new MessageId(b.getBytes()));
}
// Build and return the offer // Build and return the offer
return new Offer(Collections.unmodifiableList(messages)); return new Offer(Collections.unmodifiableList(messages));
} }
@@ -239,10 +237,16 @@ class PacketReaderImpl implements PacketReader {
if(b.length < UniqueId.LENGTH) throw new FormatException(); if(b.length < UniqueId.LENGTH) throw new FormatException();
TransportId id = new TransportId(b); TransportId id = new TransportId(b);
// Read the transport properties // Read the transport properties
r.setMaxStringLength(MAX_PROPERTY_LENGTH); Map<String, String> p = new HashMap<String, String>();
Map<String, String> m = r.readMap(String.class, String.class); r.readMapStart();
r.resetMaxStringLength(); for(int i = 0; !r.hasMapEnd(); i++) {
if(m.size() > MAX_PROPERTIES_PER_TRANSPORT) throw new FormatException(); if(i == MAX_PROPERTIES_PER_TRANSPORT)
throw new FormatException();
String key = r.readString(MAX_PROPERTY_LENGTH);
String value = r.readString(MAX_PROPERTY_LENGTH);
p.put(key, value);
}
r.readMapEnd();
// Read the version number // Read the version number
long version = r.readIntAny(); long version = r.readIntAny();
if(version < 0) throw new FormatException(); if(version < 0) throw new FormatException();
@@ -251,6 +255,6 @@ class PacketReaderImpl implements PacketReader {
// Reset the reader // Reset the reader
r.removeConsumer(counting); r.removeConsumer(counting);
// Build and return the transport update // Build and return the transport update
return new TransportUpdate(id, new TransportProperties(m), version); return new TransportUpdate(id, new TransportProperties(p), version);
} }
} }

View File

@@ -4,12 +4,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.briar.api.Bytes;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.serial.Consumer; import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.Reader; import net.sf.briar.api.serial.Reader;
@@ -25,8 +20,6 @@ class ReaderImpl implements Reader {
private boolean hasLookahead = false, eof = false; private boolean hasLookahead = false, eof = false;
private byte next, nextStructId; private byte next, nextStructId;
private byte[] buf = new byte[8]; private byte[] buf = new byte[8];
private int maxStringLength = Integer.MAX_VALUE;
private int maxBytesLength = Integer.MAX_VALUE;
ReaderImpl(InputStream in) { ReaderImpl(InputStream in) {
this.in = in; this.in = in;
@@ -70,22 +63,6 @@ class ReaderImpl implements Reader {
in.close(); in.close();
} }
public void setMaxStringLength(int length) {
maxStringLength = length;
}
public void resetMaxStringLength() {
maxStringLength = Integer.MAX_VALUE;
}
public void setMaxBytesLength(int length) {
maxBytesLength = length;
}
public void resetMaxBytesLength() {
maxBytesLength = Integer.MAX_VALUE;
}
public void addConsumer(Consumer c) { public void addConsumer(Consumer c) {
consumers.add(c); consumers.add(c);
} }
@@ -106,6 +83,11 @@ class ReaderImpl implements Reader {
return next == Tag.TRUE; return next == Tag.TRUE;
} }
public void skipBoolean() throws IOException {
if(!hasBoolean()) throw new FormatException();
hasLookahead = false;
}
public boolean hasUint7() throws IOException { public boolean hasUint7() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -118,6 +100,11 @@ class ReaderImpl implements Reader {
return next; return next;
} }
public void skipUint7() throws IOException {
if(!hasUint7()) throw new FormatException();
hasLookahead = false;
}
public boolean hasInt8() throws IOException { public boolean hasInt8() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -132,7 +119,19 @@ class ReaderImpl implements Reader {
eof = true; eof = true;
throw new FormatException(); throw new FormatException();
} }
return (byte) i; byte b = (byte) i;
// Feed the hungry mouths
for(Consumer c : consumers) c.write(b);
return b;
}
public void skipInt8() throws IOException {
if(!hasInt8()) throw new FormatException();
if(in.read() == -1) {
eof = true;
throw new FormatException();
}
hasLookahead = false;
} }
public boolean hasInt16() throws IOException { public boolean hasInt16() throws IOException {
@@ -148,24 +147,6 @@ class ReaderImpl implements Reader {
return (short) (((buf[0] & 0xFF) << 8) | (buf[1] & 0xFF)); return (short) (((buf[0] & 0xFF) << 8) | (buf[1] & 0xFF));
} }
public boolean hasInt32() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == Tag.INT32;
}
public int readInt32() throws IOException {
if(!hasInt32()) throw new FormatException();
consumeLookahead();
return readInt32Bits();
}
private int readInt32Bits() throws IOException {
readIntoBuffer(4);
return ((buf[0] & 0xFF) << 24) | ((buf[1] & 0xFF) << 16) |
((buf[2] & 0xFF) << 8) | (buf[3] & 0xFF);
}
private void readIntoBuffer(int length) throws IOException { private void readIntoBuffer(int length) throws IOException {
if(buf.length < length) buf = new byte[length]; if(buf.length < length) buf = new byte[length];
readIntoBuffer(buf, length); readIntoBuffer(buf, length);
@@ -186,6 +167,47 @@ class ReaderImpl implements Reader {
for(Consumer c : consumers) c.write(b, 0, length); for(Consumer c : consumers) c.write(b, 0, length);
} }
public void skipInt16() throws IOException {
if(!hasInt16()) throw new FormatException();
hasLookahead = false;
skip(2);
}
private void skip(int length) throws IOException {
while(length > 0) {
int read = in.read(buf, 0, Math.min(length, buf.length));
if(read == -1) {
eof = true;
throw new FormatException();
}
length -= read;
}
}
public boolean hasInt32() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == Tag.INT32;
}
public int readInt32() throws IOException {
if(!hasInt32()) throw new FormatException();
consumeLookahead();
return readInt32Bits();
}
private int readInt32Bits() throws IOException {
readIntoBuffer(4);
return ((buf[0] & 0xFF) << 24) | ((buf[1] & 0xFF) << 16) |
((buf[2] & 0xFF) << 8) | (buf[3] & 0xFF);
}
public void skipInt32() throws IOException {
if(!hasInt32()) throw new FormatException();
hasLookahead = false;
skip(4);
}
public boolean hasInt64() throws IOException { public boolean hasInt64() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -206,6 +228,12 @@ class ReaderImpl implements Reader {
((buf[6] & 0xFFL) << 8) | (buf[7] & 0xFFL); ((buf[6] & 0xFFL) << 8) | (buf[7] & 0xFFL);
} }
public void skipInt64() throws IOException {
if(!hasInt64()) throw new FormatException();
hasLookahead = false;
skip(8);
}
public boolean hasIntAny() throws IOException { public boolean hasIntAny() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -223,6 +251,16 @@ class ReaderImpl implements Reader {
throw new IllegalStateException(); throw new IllegalStateException();
} }
public void skipIntAny() throws IOException {
if(!hasIntAny()) throw new FormatException();
if(next >= 0) skipUint7();
else if(next == Tag.INT8) skipInt8();
else if(next == Tag.INT16) skipInt16();
else if(next == Tag.INT32) skipInt32();
else if(next == Tag.INT64) skipInt64();
else throw new IllegalStateException();
}
public boolean hasFloat32() throws IOException { public boolean hasFloat32() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -235,6 +273,12 @@ class ReaderImpl implements Reader {
return Float.intBitsToFloat(readInt32Bits()); return Float.intBitsToFloat(readInt32Bits());
} }
public void skipFloat32() throws IOException {
if(!hasFloat32()) throw new FormatException();
hasLookahead = false;
skip(4);
}
public boolean hasFloat64() throws IOException { public boolean hasFloat64() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -247,6 +291,12 @@ class ReaderImpl implements Reader {
return Double.longBitsToDouble(readInt64Bits()); return Double.longBitsToDouble(readInt64Bits());
} }
public void skipFloat64() throws IOException {
if(!hasFloat64()) throw new FormatException();
hasLookahead = false;
skip(8);
}
public boolean hasString() throws IOException { public boolean hasString() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -282,6 +332,15 @@ class ReaderImpl implements Reader {
|| next == Tag.INT32; || next == Tag.INT32;
} }
public void skipString(int maxLength) throws IOException {
if(!hasString()) throw new FormatException();
hasLookahead = false;
int length = readLength();
if(length > maxLength) throw new FormatException();
hasLookahead = false;
skip(length);
}
public boolean hasBytes() throws IOException { public boolean hasBytes() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -299,79 +358,21 @@ class ReaderImpl implements Reader {
return b; return b;
} }
public void skipBytes(int maxLength) throws IOException {
if(!hasBytes()) throw new FormatException();
hasLookahead = false;
int length = readLength();
if(length > maxLength) throw new FormatException();
hasLookahead = false;
skip(length);
}
public boolean hasList() throws IOException { public boolean hasList() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
return next == Tag.LIST; return next == Tag.LIST;
} }
public <E> List<E> readList(Class<E> e) throws IOException {
if(!hasList()) throw new FormatException();
consumeLookahead();
List<E> list = new ArrayList<E>();
while(!hasEnd()) list.add(readObject(e));
readEnd();
return Collections.unmodifiableList(list);
}
private boolean hasEnd() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == Tag.END;
}
private void readEnd() throws IOException {
if(!hasEnd()) throw new FormatException();
consumeLookahead();
}
private Object readObject() throws IOException {
if(hasBoolean()) return Boolean.valueOf(readBoolean());
if(hasUint7()) return Byte.valueOf(readUint7());
if(hasInt8()) return Byte.valueOf(readInt8());
if(hasInt16()) return Short.valueOf(readInt16());
if(hasInt32()) return Integer.valueOf(readInt32());
if(hasInt64()) return Long.valueOf(readInt64());
if(hasFloat32()) return Float.valueOf(readFloat32());
if(hasFloat64()) return Double.valueOf(readFloat64());
if(hasString()) return readString(maxStringLength);
if(hasBytes()) return new Bytes(readBytes(maxBytesLength));
if(hasList()) return readList(Object.class);
if(hasMap()) return readMap(Object.class, Object.class);
if(hasNull()) {
readNull();
return null;
}
throw new FormatException();
}
private <T> T readObject(Class<T> t) throws IOException {
try {
Object o = readObject();
// If this is a small integer type and we're expecting a larger
// integer type, promote before casting
if(o instanceof Byte) {
if(Short.class.isAssignableFrom(t))
return t.cast(Short.valueOf((Byte) o));
if(Integer.class.isAssignableFrom(t))
return t.cast(Integer.valueOf((Byte) o));
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Byte) o));
} else if(o instanceof Short) {
if(Integer.class.isAssignableFrom(t))
return t.cast(Integer.valueOf((Short) o));
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Short) o));
} else if(o instanceof Integer) {
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Integer) o));
}
return t.cast(o);
} catch(ClassCastException e) {
throw new FormatException();
}
}
public void readListStart() throws IOException { public void readListStart() throws IOException {
if(!hasList()) throw new FormatException(); if(!hasList()) throw new FormatException();
consumeLookahead(); consumeLookahead();
@@ -381,28 +382,48 @@ class ReaderImpl implements Reader {
return hasEnd(); return hasEnd();
} }
private boolean hasEnd() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == Tag.END;
}
public void readListEnd() throws IOException { public void readListEnd() throws IOException {
readEnd(); readEnd();
} }
private void readEnd() throws IOException {
if(!hasEnd()) throw new FormatException();
consumeLookahead();
}
public void skipList() throws IOException {
if(!hasList()) throw new FormatException();
hasLookahead = false;
while(!hasListEnd()) skipObject();
hasLookahead = false;
}
private void skipObject() throws IOException {
if(hasBoolean()) skipBoolean();
else if(hasIntAny()) skipIntAny();
else if(hasFloat32()) skipFloat32();
else if(hasFloat64()) skipFloat64();
else if(hasString()) skipString(Integer.MAX_VALUE);
else if(hasBytes()) skipBytes(Integer.MAX_VALUE);
else if(hasList()) skipList();
else if(hasMap()) skipMap();
else if(hasStruct()) skipStruct();
else if(hasNull()) skipNull();
else throw new FormatException();
}
public boolean hasMap() throws IOException { public boolean hasMap() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
return next == Tag.MAP; return next == Tag.MAP;
} }
public <K, V> Map<K, V> readMap(Class<K> k, Class<V> v) throws IOException {
if(!hasMap()) throw new FormatException();
consumeLookahead();
Map<K, V> m = new HashMap<K, V>();
while(!hasEnd()) {
if(m.put(readObject(k), readObject(v)) != null)
throw new FormatException(); // Duplicate key
}
readEnd();
return Collections.unmodifiableMap(m);
}
public void readMapStart() throws IOException { public void readMapStart() throws IOException {
if(!hasMap()) throw new FormatException(); if(!hasMap()) throw new FormatException();
consumeLookahead(); consumeLookahead();
@@ -416,11 +437,27 @@ class ReaderImpl implements Reader {
readEnd(); readEnd();
} }
public void skipMap() throws IOException {
if(!hasMap()) throw new FormatException();
hasLookahead = false;
while(!hasMapEnd()) {
skipObject();
skipObject();
}
hasLookahead = false;
}
public boolean hasStruct() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == Tag.STRUCT;
}
public boolean hasStruct(int id) throws IOException { public boolean hasStruct(int id) throws IOException {
if(id < 0 || id > 255) throw new IllegalArgumentException(); if(id < 0 || id > 255) throw new IllegalArgumentException();
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
return (nextStructId & 0xFF) == id; return next == Tag.STRUCT && (nextStructId & 0xFF) == id;
} }
public void readStructStart(int id) throws IOException { public void readStructStart(int id) throws IOException {
@@ -436,6 +473,13 @@ class ReaderImpl implements Reader {
readEnd(); readEnd();
} }
public void skipStruct() throws IOException {
if(!hasStruct()) throw new FormatException();
hasLookahead = false;
while(!hasStructEnd()) skipObject();
hasLookahead = false;
}
public boolean hasNull() throws IOException { public boolean hasNull() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
@@ -446,4 +490,9 @@ class ReaderImpl implements Reader {
if(!hasNull()) throw new FormatException(); if(!hasNull()) throw new FormatException();
consumeLookahead(); consumeLookahead();
} }
public void skipNull() throws IOException {
if(!hasNull()) throw new FormatException();
hasLookahead = false;
}
} }

View File

@@ -3,13 +3,8 @@ package net.sf.briar.serial;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.Bytes;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.util.StringUtils; import net.sf.briar.util.StringUtils;
@@ -22,12 +17,20 @@ public class ReaderImplTest extends BriarTestCase {
@Test @Test
public void testReadBoolean() throws Exception { public void testReadBoolean() throws Exception {
setContents("FFFE"); setContents("FF" + "FE");
assertFalse(r.readBoolean()); assertFalse(r.readBoolean());
assertTrue(r.readBoolean()); assertTrue(r.readBoolean());
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipBoolean() throws Exception {
setContents("FF" + "FE");
r.skipBoolean();
r.skipBoolean();
assertTrue(r.eof());
}
@Test @Test
public void testReadInt8() throws Exception { public void testReadInt8() throws Exception {
setContents("FD00" + "FDFF" + "FD7F" + "FD80"); setContents("FD00" + "FDFF" + "FD7F" + "FD80");
@@ -38,6 +41,13 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipInt8() throws Exception {
setContents("FD00");
r.skipInt8();
assertTrue(r.eof());
}
@Test @Test
public void testReadInt16() throws Exception { public void testReadInt16() throws Exception {
setContents("FC0000" + "FCFFFF" + "FC7FFF" + "FC8000"); setContents("FC0000" + "FCFFFF" + "FC7FFF" + "FC8000");
@@ -48,6 +58,13 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipInt16() throws Exception {
setContents("FC0000");
r.skipInt16();
assertTrue(r.eof());
}
@Test @Test
public void testReadInt32() throws Exception { public void testReadInt32() throws Exception {
setContents("FB00000000" + "FBFFFFFFFF" + "FB7FFFFFFF" + "FB80000000"); setContents("FB00000000" + "FBFFFFFFFF" + "FB7FFFFFFF" + "FB80000000");
@@ -58,6 +75,13 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipInt32() throws Exception {
setContents("FB00000000");
r.skipInt32();
assertTrue(r.eof());
}
@Test @Test
public void testReadInt64() throws Exception { public void testReadInt64() throws Exception {
setContents("FA0000000000000000" + "FAFFFFFFFFFFFFFFFF" setContents("FA0000000000000000" + "FAFFFFFFFFFFFFFFFF"
@@ -69,6 +93,13 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipInt64() throws Exception {
setContents("FA0000000000000000");
r.skipInt64();
assertTrue(r.eof());
}
@Test @Test
public void testReadIntAny() throws Exception { public void testReadIntAny() throws Exception {
setContents("00" + "7F" + "FD80" + "FDFF" + "FC0080" + "FC7FFF" setContents("00" + "7F" + "FD80" + "FDFF" + "FC0080" + "FC7FFF"
@@ -85,6 +116,18 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipIntAny() throws Exception {
setContents("00" + "FD00" + "FC0000" + "FB00000000"
+ "FA0000000000000000");
r.skipIntAny();
r.skipIntAny();
r.skipIntAny();
r.skipIntAny();
r.skipIntAny();
assertTrue(r.eof());
}
@Test @Test
public void testReadFloat32() throws Exception { public void testReadFloat32() throws Exception {
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html // http://babbage.cs.qc.edu/IEEE-754/Decimal.html
@@ -102,6 +145,13 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipFloat32() throws Exception {
setContents("F900000000");
r.skipFloat32();
assertTrue(r.eof());
}
@Test @Test
public void testReadFloat64() throws Exception { public void testReadFloat64() throws Exception {
setContents("F80000000000000000" + "F83FF0000000000000" setContents("F80000000000000000" + "F83FF0000000000000"
@@ -119,11 +169,18 @@ public class ReaderImplTest extends BriarTestCase {
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipFloat64() throws Exception {
setContents("F80000000000000000");
r.skipFloat64();
assertTrue(r.eof());
}
@Test @Test
public void testReadString() throws Exception { public void testReadString() throws Exception {
setContents("F703666F6F" + "F700"); setContents("F703666F6F" + "F700");
assertEquals("foo", r.readString(1000)); assertEquals("foo", r.readString(Integer.MAX_VALUE));
assertEquals("", r.readString(1000)); assertEquals("", r.readString(Integer.MAX_VALUE));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@@ -137,11 +194,29 @@ public class ReaderImplTest extends BriarTestCase {
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }
@Test
public void testSkipString() throws Exception {
setContents("F703666F6F" + "F700");
r.skipString(Integer.MAX_VALUE);
r.skipString(Integer.MAX_VALUE);
assertTrue(r.eof());
}
@Test
public void testSkipStringMaxLength() throws Exception {
setContents("F703666F6F" + "F703666F6F");
r.skipString(3);
try {
r.skipString(2);
fail();
} catch(FormatException expected) {}
}
@Test @Test
public void testReadBytes() throws Exception { public void testReadBytes() throws Exception {
setContents("F603010203" + "F600"); setContents("F603010203" + "F600");
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(1000)); assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(Integer.MAX_VALUE));
assertArrayEquals(new byte[] {}, r.readBytes(1000)); assertArrayEquals(new byte[] {}, r.readBytes(Integer.MAX_VALUE));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@@ -155,87 +230,27 @@ public class ReaderImplTest extends BriarTestCase {
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }
@Test
public void testSkipBytes() throws Exception {
setContents("F603010203" + "F600");
r.skipBytes(Integer.MAX_VALUE);
r.skipBytes(Integer.MAX_VALUE);
assertTrue(r.eof());
}
@Test
public void testSkipBytesMaxLength() throws Exception {
setContents("F603010203" + "F603010203");
r.skipBytes(3);
try {
r.skipBytes(2);
fail();
} catch(FormatException expected) {}
}
@Test @Test
public void testReadList() throws Exception { public void testReadList() throws Exception {
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2"); setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
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 testReadListTypeSafe() throws Exception {
setContents("F5" + "01" + "02" + "03" + "F2");
List<Byte> l = r.readList(Byte.class);
assertNotNull(l);
assertEquals(3, l.size());
assertEquals(Byte.valueOf((byte) 1), l.get(0));
assertEquals(Byte.valueOf((byte) 2), l.get(1));
assertEquals(Byte.valueOf((byte) 3), l.get(2));
assertTrue(r.eof());
}
@Test
public void testReadListTypeSafeThrowsFormatException() throws Exception {
setContents("F5" + "01" + "F703666F6F" + "03" + "F2");
// Trying to read a mixed list as a list of bytes should throw a
// FormatException
try {
r.readList(Byte.class);
fail();
} catch(FormatException expected) {}
}
@Test
public void testReadMap() throws Exception {
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "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 testReadMapTypeSafe() throws Exception {
setContents("F4" + "F703666F6F" + "7B" + "F700" + "F1" + "F2");
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
assertEquals(Byte.valueOf((byte) 123), m.get("foo"));
assertTrue(m.containsKey(""));
assertNull(m.get(""));
assertTrue(r.eof());
}
@Test
public void testMapKeysMustBeUnique() throws Exception {
setContents("F4" + "F703666F6F" + "01" + "F703626172" + "02" + "F2"
+ "F4" + "F703666F6F" + "01" + "F703666F6F" + "02" + "F2");
// The first map has unique keys
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
assertEquals(2, m.size());
assertEquals(Byte.valueOf((byte) 1), m.get("foo"));
assertEquals(Byte.valueOf((byte) 2), m.get("bar"));
// The second map has a duplicate key
try {
r.readMap(String.class, Byte.class);
fail();
} catch(FormatException expected) {}
}
@Test
public void testReadDelimitedListElements() throws Exception {
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
assertTrue(r.hasList());
r.readListStart(); r.readListStart();
assertFalse(r.hasListEnd()); assertFalse(r.hasListEnd());
assertEquals((byte) 1, r.readIntAny()); assertEquals((byte) 1, r.readIntAny());
@@ -249,21 +264,15 @@ public class ReaderImplTest extends BriarTestCase {
} }
@Test @Test
public void testReadDelimitedListTypeSafe() throws Exception { public void testSkipList() throws Exception {
setContents("F5" + "01" + "02" + "03" + "F2"); setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
List<Byte> l = r.readList(Byte.class); r.skipList();
assertNotNull(l);
assertEquals(3, l.size());
assertEquals(Byte.valueOf((byte) 1), l.get(0));
assertEquals(Byte.valueOf((byte) 2), l.get(1));
assertEquals(Byte.valueOf((byte) 3), l.get(2));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadDelimitedMapEntries() throws Exception { public void testReadMap() throws Exception {
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2"); setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2");
assertTrue(r.hasMap());
r.readMapStart(); r.readMapStart();
assertFalse(r.hasMapEnd()); assertFalse(r.hasMapEnd());
assertEquals("foo", r.readString(1000)); assertEquals("foo", r.readString(1000));
@@ -280,87 +289,54 @@ public class ReaderImplTest extends BriarTestCase {
} }
@Test @Test
public void testReadDelimitedMapTypeSafe() throws Exception { public void testSkipMap() throws Exception {
setContents("F4" + "F703666F6F" + "7B" + "F700" + "F1" + "F2"); setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2");
Map<String, Byte> m = r.readMap(String.class, Byte.class); r.skipMap();
assertNotNull(m);
assertEquals(2, m.size());
assertEquals(Byte.valueOf((byte) 123), m.get("foo"));
assertTrue(m.containsKey(""));
assertNull(m.get(""));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
@SuppressWarnings("unchecked")
public void testReadNestedMapsAndLists() throws Exception {
setContents("F4" + "F4" + "F703666F6F" + "7B" + "F2"
+ "F5" + "01" + "F2" + "F2");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(1, m.size());
Entry<Object, Object> e = m.entrySet().iterator().next();
Map<Object, Object> m1 = (Map<Object, Object>) e.getKey();
assertNotNull(m1);
assertEquals(1, m1.size());
assertEquals((byte) 123, m1.get("foo"));
List<Object> l = (List<Object>) e.getValue();
assertNotNull(l);
assertEquals(1, l.size());
assertEquals((byte) 1, l.get(0));
assertTrue(r.eof());
}
@Test
public void testMaxStringLengthAppliesInsideMap() throws Exception {
setContents("F4" + "F703666F6F" + "F603010203" + "F2");
r.setMaxStringLength(3);
Map<String, Bytes> m = r.readMap(String.class, Bytes.class);
assertTrue(r.eof());
String key = "foo";
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("F4" + "F703666F6F" + "F603010203" + "F2");
r.setMaxStringLength(2);
try {
r.readMap(String.class, Bytes.class);
fail();
} catch(FormatException expected) {}
}
@Test
public void testMaxBytesLengthAppliesInsideMap() throws Exception {
setContents("F4" + "F703666F6F" + "F603010203" + "F2");
r.setMaxBytesLength(3);
Map<String, Bytes> m = r.readMap(String.class, Bytes.class);
assertTrue(r.eof());
String key = "foo";
Bytes value = new Bytes(new byte[] {1, 2, 3});
assertEquals(Collections.singletonMap(key, value), m);
// The max bytes length should be applied inside the map
setContents("F4" + "F703666F6F" + "F603010203" + "F2");
r.setMaxBytesLength(2);
try {
r.readMap(String.class, Bytes.class);
fail();
} catch(FormatException expected) {}
}
@Test @Test
public void testReadStruct() throws Exception { public void testReadStruct() throws Exception {
// Two structs with IDs 0 and 255, each containing 1 as a uint7 // Two empty structs with IDs 0 and 255
setContents("F300" + "01" + "F2" + "F3FF" + "01" + "F2"); setContents("F300" + "F2" + "F3FF" + "F2");
r.readStructStart(0); r.readStructStart(0);
assertEquals(1, r.readUint7());
r.readStructEnd(); r.readStructEnd();
r.readStructStart(255); r.readStructStart(255);
assertEquals(1, r.readUint7());
r.readStructEnd(); r.readStructEnd();
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test
public void testSkipStruct() throws Exception {
// Two empty structs with IDs 0 and 255
setContents("F300" + "F2" + "F3FF" + "F2");
r.skipStruct();
r.skipStruct();
assertTrue(r.eof());
}
@Test
public void testSkipNestedStructMapAndList() throws Exception {
// A struct containing a map containing two empty lists
setContents("F300" + "F4" + "F5" + "F2" + "F5" + "F2" + "F2" + "F2");
r.skipStruct();
assertTrue(r.eof());
}
@Test
public void testReadNull() throws Exception {
setContents("F1");
r.readNull();
assertTrue(r.eof());
}
@Test
public void testSkipNull() throws Exception {
setContents("F1");
r.skipNull();
assertTrue(r.eof());
}
@Test @Test
public void testReadEmptyInput() throws Exception { public void testReadEmptyInput() throws Exception {
setContents(""); setContents("");