mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Renamed raw data type.
This commit is contained in:
@@ -30,9 +30,9 @@ public interface Reader {
|
||||
String readString(int maxLength) throws IOException;
|
||||
void skipString() throws IOException;
|
||||
|
||||
boolean hasBytes() throws IOException;
|
||||
byte[] readBytes(int maxLength) throws IOException;
|
||||
void skipBytes() throws IOException;
|
||||
boolean hasRaw() throws IOException;
|
||||
byte[] readRaw(int maxLength) throws IOException;
|
||||
void skipRaw() throws IOException;
|
||||
|
||||
boolean hasList() throws IOException;
|
||||
void readListStart() throws IOException;
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface Writer {
|
||||
void writeInteger(long l) throws IOException;
|
||||
void writeFloat(double d) throws IOException;
|
||||
void writeString(String s) throws IOException;
|
||||
void writeBytes(byte[] b) throws IOException;
|
||||
void writeRaw(byte[] b) throws IOException;
|
||||
|
||||
void writeList(Collection<?> c) throws IOException;
|
||||
void writeListStart() throws IOException;
|
||||
|
||||
@@ -90,7 +90,7 @@ class ReaderImpl implements Reader {
|
||||
else if(hasInteger()) skipInteger();
|
||||
else if(hasFloat()) skipFloat();
|
||||
else if(hasString()) skipString();
|
||||
else if(hasBytes()) skipBytes();
|
||||
else if(hasRaw()) skipRaw();
|
||||
else if(hasList()) skipList();
|
||||
else if(hasMap()) skipMap();
|
||||
else if(hasNull()) skipNull();
|
||||
@@ -262,16 +262,16 @@ class ReaderImpl implements Reader {
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
public boolean hasBytes() throws IOException {
|
||||
public boolean hasRaw() throws IOException {
|
||||
if(!hasLookahead) readLookahead();
|
||||
if(eof) return false;
|
||||
return next == RAW_8 || next == RAW_16 || next == RAW_32;
|
||||
}
|
||||
|
||||
public byte[] readBytes(int maxLength) throws IOException {
|
||||
if(!hasBytes()) throw new FormatException();
|
||||
public byte[] readRaw(int maxLength) throws IOException {
|
||||
if(!hasRaw()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
int length = readBytesLength(true);
|
||||
int length = readRawLength(true);
|
||||
if(length < 0 || length > maxLength) throw new FormatException();
|
||||
if(length == 0) return EMPTY_BUFFER;
|
||||
byte[] b = new byte[length];
|
||||
@@ -279,16 +279,16 @@ class ReaderImpl implements Reader {
|
||||
return b;
|
||||
}
|
||||
|
||||
private int readBytesLength(boolean consume) throws IOException {
|
||||
private int readRawLength(boolean consume) throws IOException {
|
||||
if(next == RAW_8) return readInt8(consume);
|
||||
if(next == RAW_16) return readInt16(consume);
|
||||
if(next == RAW_32) return readInt32(consume);
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public void skipBytes() throws IOException {
|
||||
if(!hasBytes()) throw new FormatException();
|
||||
int length = readBytesLength(false);
|
||||
public void skipRaw() throws IOException {
|
||||
if(!hasRaw()) throw new FormatException();
|
||||
int length = readRawLength(false);
|
||||
if(length < 0) throw new FormatException();
|
||||
skip(length);
|
||||
hasLookahead = false;
|
||||
|
||||
@@ -124,7 +124,7 @@ class WriterImpl implements Writer {
|
||||
write(b);
|
||||
}
|
||||
|
||||
public void writeBytes(byte[] b) throws IOException {
|
||||
public void writeRaw(byte[] b) throws IOException {
|
||||
if(b.length <= Byte.MAX_VALUE) {
|
||||
write(RAW_8);
|
||||
write((byte) b.length);
|
||||
@@ -153,8 +153,8 @@ class WriterImpl implements Writer {
|
||||
else if(o instanceof Float) writeFloat((Float) o);
|
||||
else if(o instanceof Double) writeFloat((Double) o);
|
||||
else if(o instanceof String) writeString((String) o);
|
||||
else if(o instanceof byte[]) writeBytes((byte[]) o);
|
||||
else if(o instanceof Bytes) writeBytes(((Bytes) o).getBytes());
|
||||
else if(o instanceof byte[]) writeRaw((byte[]) o);
|
||||
else if(o instanceof Bytes) writeRaw(((Bytes) o).getBytes());
|
||||
else if(o instanceof List<?>) writeList((List<?>) o);
|
||||
else if(o instanceof Map<?, ?>) writeMap((Map<?, ?>) o);
|
||||
else if(o == null) writeNull();
|
||||
|
||||
@@ -124,14 +124,14 @@ abstract class Connector extends Thread {
|
||||
}
|
||||
|
||||
protected void sendPublicKeyHash(Writer w) throws IOException {
|
||||
w.writeBytes(messageDigest.digest(keyPair.getPublic().getEncoded()));
|
||||
w.writeRaw(messageDigest.digest(keyPair.getPublic().getEncoded()));
|
||||
w.flush();
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent hash");
|
||||
}
|
||||
|
||||
protected byte[] receivePublicKeyHash(Reader r) throws IOException {
|
||||
int hashLength = messageDigest.getDigestLength();
|
||||
byte[] b = r.readBytes(hashLength);
|
||||
byte[] b = r.readRaw(hashLength);
|
||||
if(b.length < hashLength) throw new FormatException();
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received hash");
|
||||
return b;
|
||||
@@ -139,14 +139,14 @@ abstract class Connector extends Thread {
|
||||
|
||||
protected void sendPublicKey(Writer w) throws IOException {
|
||||
byte[] key = keyPair.getPublic().getEncoded();
|
||||
w.writeBytes(key);
|
||||
w.writeRaw(key);
|
||||
w.flush();
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent key");
|
||||
}
|
||||
|
||||
protected byte[] receivePublicKey(Reader r) throws GeneralSecurityException,
|
||||
IOException {
|
||||
byte[] b = r.readBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
byte[] b = r.readRaw(MAX_PUBLIC_KEY_LENGTH);
|
||||
keyParser.parsePublicKey(b);
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received key");
|
||||
return b;
|
||||
@@ -192,8 +192,8 @@ abstract class Connector extends Thread {
|
||||
byte[] sig = signature.sign();
|
||||
// Write the name, public key and signature
|
||||
w.writeString(localAuthor.getName());
|
||||
w.writeBytes(localAuthor.getPublicKey());
|
||||
w.writeBytes(sig);
|
||||
w.writeRaw(localAuthor.getPublicKey());
|
||||
w.writeRaw(sig);
|
||||
w.flush();
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent pseudonym");
|
||||
}
|
||||
@@ -202,8 +202,8 @@ abstract class Connector extends Thread {
|
||||
throws GeneralSecurityException, IOException {
|
||||
// Read the name, public key and signature
|
||||
String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
|
||||
byte[] publicKey = r.readBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
byte[] sig = r.readBytes(MAX_SIGNATURE_LENGTH);
|
||||
byte[] publicKey = r.readRaw(MAX_PUBLIC_KEY_LENGTH);
|
||||
byte[] sig = r.readRaw(MAX_SIGNATURE_LENGTH);
|
||||
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received pseudonym");
|
||||
// Verify the signature
|
||||
Signature signature = crypto.getSignature();
|
||||
|
||||
@@ -45,7 +45,7 @@ class AuthorFactoryImpl implements AuthorFactory {
|
||||
try {
|
||||
w.writeListStart();
|
||||
w.writeString(name);
|
||||
w.writeBytes(publicKey);
|
||||
w.writeRaw(publicKey);
|
||||
w.writeListEnd();
|
||||
} catch(IOException e) {
|
||||
// Shouldn't happen with ByteArrayOutputStream
|
||||
|
||||
@@ -29,7 +29,7 @@ class AuthorReader implements ObjectReader<Author> {
|
||||
r.readListStart();
|
||||
String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
|
||||
if(name.length() == 0) throw new FormatException();
|
||||
byte[] publicKey = r.readBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
byte[] publicKey = r.readRaw(MAX_PUBLIC_KEY_LENGTH);
|
||||
r.readListEnd();
|
||||
// Reset the reader
|
||||
r.removeConsumer(digesting);
|
||||
|
||||
@@ -38,7 +38,7 @@ class GroupFactoryImpl implements GroupFactory {
|
||||
try {
|
||||
w.writeListStart();
|
||||
w.writeString(name);
|
||||
w.writeBytes(salt);
|
||||
w.writeRaw(salt);
|
||||
w.writeListEnd();
|
||||
} catch(IOException e) {
|
||||
// Shouldn't happen with ByteArrayOutputStream
|
||||
|
||||
@@ -28,7 +28,7 @@ class GroupReader implements ObjectReader<Group> {
|
||||
r.readListStart();
|
||||
String name = r.readString(MAX_GROUP_NAME_LENGTH);
|
||||
if(name.length() == 0) throw new FormatException();
|
||||
byte[] salt = r.readBytes(GROUP_SALT_LENGTH);
|
||||
byte[] salt = r.readRaw(GROUP_SALT_LENGTH);
|
||||
if(salt.length != GROUP_SALT_LENGTH) throw new FormatException();
|
||||
r.readListEnd();
|
||||
r.removeConsumer(digesting);
|
||||
|
||||
@@ -84,7 +84,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
// Write the message
|
||||
w.writeListStart();
|
||||
if(parent == null) w.writeNull();
|
||||
else w.writeBytes(parent.getBytes());
|
||||
else w.writeRaw(parent.getBytes());
|
||||
writeGroup(w, group);
|
||||
if(author == null) w.writeNull();
|
||||
else writeAuthor(w, author);
|
||||
@@ -92,8 +92,8 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
w.writeInteger(timestamp);
|
||||
byte[] salt = new byte[MESSAGE_SALT_LENGTH];
|
||||
random.nextBytes(salt);
|
||||
w.writeBytes(salt);
|
||||
w.writeBytes(body);
|
||||
w.writeRaw(salt);
|
||||
w.writeRaw(body);
|
||||
int bodyStart = (int) counting.getCount() - body.length;
|
||||
// Sign the message with the author's private key, if there is one
|
||||
if(privateKey == null) {
|
||||
@@ -103,7 +103,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
byte[] sig = signature.sign();
|
||||
if(sig.length > MAX_SIGNATURE_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
w.writeBytes(sig);
|
||||
w.writeRaw(sig);
|
||||
}
|
||||
w.writeListEnd();
|
||||
// Hash the message, including the signature, to get the message ID
|
||||
@@ -116,14 +116,14 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
private void writeGroup(Writer w, Group g) throws IOException {
|
||||
w.writeListStart();
|
||||
w.writeString(g.getName());
|
||||
w.writeBytes(g.getSalt());
|
||||
w.writeRaw(g.getSalt());
|
||||
w.writeListEnd();
|
||||
}
|
||||
|
||||
private void writeAuthor(Writer w, Author a) throws IOException {
|
||||
w.writeListStart();
|
||||
w.writeString(a.getName());
|
||||
w.writeBytes(a.getPublicKey());
|
||||
w.writeRaw(a.getPublicKey());
|
||||
w.writeListEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
if(r.hasNull()) {
|
||||
r.readNull();
|
||||
} else {
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
byte[] b = r.readRaw(UniqueId.LENGTH);
|
||||
if(b.length < UniqueId.LENGTH) throw new FormatException();
|
||||
parent = new MessageId(b);
|
||||
}
|
||||
@@ -56,10 +56,10 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
long timestamp = r.readInteger();
|
||||
if(timestamp < 0) throw new FormatException();
|
||||
// Read the salt
|
||||
byte[] salt = r.readBytes(MESSAGE_SALT_LENGTH);
|
||||
byte[] salt = r.readRaw(MESSAGE_SALT_LENGTH);
|
||||
if(salt.length < MESSAGE_SALT_LENGTH) throw new FormatException();
|
||||
// Read the message body
|
||||
byte[] body = r.readBytes(MAX_BODY_LENGTH);
|
||||
byte[] body = r.readRaw(MAX_BODY_LENGTH);
|
||||
// Record the offset of the body within the message
|
||||
int bodyStart = (int) counting.getCount() - body.length;
|
||||
// Record the length of the data covered by the author's signature
|
||||
@@ -67,7 +67,7 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
// Read the author's signature, if there is one
|
||||
byte[] signature = null;
|
||||
if(author == null) r.readNull();
|
||||
else signature = r.readBytes(MAX_SIGNATURE_LENGTH);
|
||||
else signature = r.readRaw(MAX_SIGNATURE_LENGTH);
|
||||
// Read the end of the message
|
||||
r.readListEnd();
|
||||
// Reset the reader
|
||||
|
||||
@@ -122,7 +122,7 @@ class PacketReaderImpl implements PacketReader {
|
||||
List<MessageId> acked = new ArrayList<MessageId>();
|
||||
r.readListStart();
|
||||
while(!r.hasListEnd()) {
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
byte[] b = r.readRaw(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH)
|
||||
throw new FormatException();
|
||||
acked.add(new MessageId(b));
|
||||
@@ -168,7 +168,7 @@ class PacketReaderImpl implements PacketReader {
|
||||
List<MessageId> offered = new ArrayList<MessageId>();
|
||||
r.readListStart();
|
||||
while(!r.hasListEnd()) {
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
byte[] b = r.readRaw(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH)
|
||||
throw new FormatException();
|
||||
offered.add(new MessageId(b));
|
||||
@@ -198,7 +198,7 @@ class PacketReaderImpl implements PacketReader {
|
||||
r.readListStart();
|
||||
List<MessageId> requested = new ArrayList<MessageId>();
|
||||
while(!r.hasListEnd()) {
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
byte[] b = r.readRaw(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH)
|
||||
throw new FormatException();
|
||||
requested.add(new MessageId(b));
|
||||
|
||||
@@ -85,7 +85,7 @@ class PacketWriterImpl implements PacketWriter {
|
||||
Writer w = writerFactory.createWriter(payload);
|
||||
w.writeListStart();
|
||||
w.writeListStart();
|
||||
for(MessageId m : a.getMessageIds()) w.writeBytes(m.getBytes());
|
||||
for(MessageId m : a.getMessageIds()) w.writeRaw(m.getBytes());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
writePacket(ACK);
|
||||
@@ -103,7 +103,7 @@ class PacketWriterImpl implements PacketWriter {
|
||||
Writer w = writerFactory.createWriter(payload);
|
||||
w.writeListStart();
|
||||
w.writeListStart();
|
||||
for(MessageId m : o.getMessageIds()) w.writeBytes(m.getBytes());
|
||||
for(MessageId m : o.getMessageIds()) w.writeRaw(m.getBytes());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
writePacket(OFFER);
|
||||
@@ -114,7 +114,7 @@ class PacketWriterImpl implements PacketWriter {
|
||||
Writer w = writerFactory.createWriter(payload);
|
||||
w.writeListStart();
|
||||
w.writeListStart();
|
||||
for(MessageId m : r.getMessageIds()) w.writeBytes(m.getBytes());
|
||||
for(MessageId m : r.getMessageIds()) w.writeRaw(m.getBytes());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
writePacket(REQUEST);
|
||||
@@ -157,7 +157,7 @@ class PacketWriterImpl implements PacketWriter {
|
||||
for(Group g : u.getGroups()) {
|
||||
w.writeListStart();
|
||||
w.writeString(g.getName());
|
||||
w.writeBytes(g.getSalt());
|
||||
w.writeRaw(g.getSalt());
|
||||
w.writeListEnd();
|
||||
}
|
||||
w.writeListEnd();
|
||||
|
||||
@@ -330,138 +330,138 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes8() throws Exception {
|
||||
public void testReadRaw8() throws Exception {
|
||||
byte[] longest = new byte[Byte.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
// {1, 2, 3}, {}, and 127 zero bytes
|
||||
setContents("51" + "03" + "010203" + "51" + "00" +
|
||||
"51" + "7F" + longHex);
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(new byte[0], r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(Integer.MAX_VALUE));
|
||||
assertArrayEquals(new byte[0], r.readRaw(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest, r.readRaw(Integer.MAX_VALUE));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes8ChecksMaxLength() throws Exception {
|
||||
public void testReadRaw8ChecksMaxLength() throws Exception {
|
||||
// {1, 2, 3} twice
|
||||
setContents("51" + "03" + "010203" + "51" + "03" + "010203");
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(3));
|
||||
assertTrue(r.hasBytes());
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(3));
|
||||
assertTrue(r.hasRaw());
|
||||
try {
|
||||
r.readBytes(2);
|
||||
r.readRaw(2);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipBytes8() throws Exception {
|
||||
public void testSkipRaw8() throws Exception {
|
||||
byte[] longest = new byte[Byte.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
// {1, 2, 3}, {}, and 127 zero bytes
|
||||
setContents("51" + "03" + "010203" + "51" + "00" +
|
||||
"51" + "7F" + longHex);
|
||||
r.skipBytes();
|
||||
r.skipBytes();
|
||||
r.skipBytes();
|
||||
r.skipRaw();
|
||||
r.skipRaw();
|
||||
r.skipRaw();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes16() throws Exception {
|
||||
public void testReadRaw16() throws Exception {
|
||||
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
byte[] longest = new byte[Short.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
// 128 zero bytes and 2^15 - 1 zero bytes
|
||||
setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
|
||||
assertArrayEquals(shortest, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(shortest, r.readRaw(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest, r.readRaw(Integer.MAX_VALUE));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes16ChecksMaxLength() throws Exception {
|
||||
public void testReadRaw16ChecksMaxLength() throws Exception {
|
||||
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
// 128 zero bytes, twice
|
||||
setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex);
|
||||
assertArrayEquals(shortest, r.readBytes(Byte.MAX_VALUE + 1));
|
||||
assertTrue(r.hasBytes());
|
||||
assertArrayEquals(shortest, r.readRaw(Byte.MAX_VALUE + 1));
|
||||
assertTrue(r.hasRaw());
|
||||
try {
|
||||
r.readBytes(Byte.MAX_VALUE);
|
||||
r.readRaw(Byte.MAX_VALUE);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipBytes16() throws Exception {
|
||||
public void testSkipRaw16() throws Exception {
|
||||
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
byte[] longest = new byte[Short.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
// 128 zero bytes and 2^15 - 1 zero bytes
|
||||
setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
|
||||
r.skipBytes();
|
||||
r.skipBytes();
|
||||
r.skipRaw();
|
||||
r.skipRaw();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes32() throws Exception {
|
||||
public void testReadRaw32() throws Exception {
|
||||
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
// 2^15 zero bytes
|
||||
setContents("54" + "00008000" + shortHex);
|
||||
assertArrayEquals(shortest, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(shortest, r.readRaw(Integer.MAX_VALUE));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBytes32ChecksMaxLength() throws Exception {
|
||||
public void testReadRaw32ChecksMaxLength() throws Exception {
|
||||
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
// 2^15 zero bytes, twice
|
||||
setContents("54" + "00008000" + shortHex +
|
||||
"54" + "00008000" + shortHex);
|
||||
assertArrayEquals(shortest, r.readBytes(Short.MAX_VALUE + 1));
|
||||
assertTrue(r.hasBytes());
|
||||
assertArrayEquals(shortest, r.readRaw(Short.MAX_VALUE + 1));
|
||||
assertTrue(r.hasRaw());
|
||||
try {
|
||||
r.readBytes(Short.MAX_VALUE);
|
||||
r.readRaw(Short.MAX_VALUE);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipBytes32() throws Exception {
|
||||
public void testSkipRaw32() throws Exception {
|
||||
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
// 2^15 zero bytes, twice
|
||||
setContents("54" + "00008000" + shortHex +
|
||||
"54" + "00008000" + shortHex);
|
||||
r.skipBytes();
|
||||
r.skipBytes();
|
||||
r.skipRaw();
|
||||
r.skipRaw();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBytesMustHaveMinimalLength() throws Exception {
|
||||
public void testRawMustHaveMinimalLength() throws Exception {
|
||||
// RAW_16 could be encoded as RAW_8
|
||||
byte[] longest8 = new byte[Byte.MAX_VALUE];
|
||||
String long8Hex = StringUtils.toHexString(longest8);
|
||||
setContents("51" + "7F" + long8Hex + "52" + "007F" + long8Hex);
|
||||
assertArrayEquals(longest8, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest8, r.readRaw(Integer.MAX_VALUE));
|
||||
try {
|
||||
r.readBytes(Integer.MAX_VALUE);
|
||||
r.readRaw(Integer.MAX_VALUE);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
// RAW_32 could be encoded as RAW_16
|
||||
byte[] longest16 = new byte[Short.MAX_VALUE];
|
||||
String long16Hex = StringUtils.toHexString(longest16);
|
||||
setContents("52" + "7FFF" + long16Hex + "54" + "00007FFF" + long16Hex);
|
||||
assertArrayEquals(longest16, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(longest16, r.readRaw(Integer.MAX_VALUE));
|
||||
try {
|
||||
r.readBytes(Integer.MAX_VALUE);
|
||||
r.readRaw(Integer.MAX_VALUE);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@@ -116,8 +116,8 @@ public class WriterImplTest extends BriarTestCase {
|
||||
public void testWriteBytes8() throws IOException {
|
||||
byte[] longest = new byte[Byte.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
w.writeBytes(new byte[] {1, 2, 3});
|
||||
w.writeBytes(longest);
|
||||
w.writeRaw(new byte[] {1, 2, 3});
|
||||
w.writeRaw(longest);
|
||||
// RAW_8 tag, length 3, bytes, RAW_8 tag, length 127, bytes
|
||||
checkContents("51" + "03" + "010203" + "51" + "7F" + longHex);
|
||||
}
|
||||
@@ -128,8 +128,8 @@ public class WriterImplTest extends BriarTestCase {
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
byte[] longest = new byte[Short.MAX_VALUE];
|
||||
String longHex = StringUtils.toHexString(longest);
|
||||
w.writeBytes(shortest);
|
||||
w.writeBytes(longest);
|
||||
w.writeRaw(shortest);
|
||||
w.writeRaw(longest);
|
||||
// RAW_16 tag, length 128, bytes, RAW_16 tag, length 2^15 - 1, bytes
|
||||
checkContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
|
||||
}
|
||||
@@ -138,7 +138,7 @@ public class WriterImplTest extends BriarTestCase {
|
||||
public void testWriteBytes32() throws IOException {
|
||||
byte[] shortest = new byte[Short.MAX_VALUE + 1];
|
||||
String shortHex = StringUtils.toHexString(shortest);
|
||||
w.writeBytes(shortest);
|
||||
w.writeRaw(shortest);
|
||||
// RAW_32 tag, length 2^15, bytes
|
||||
checkContents("54" + "00008000" + shortHex);
|
||||
}
|
||||
|
||||
@@ -144,9 +144,9 @@ public class PacketReaderImplTest extends BriarTestCase {
|
||||
w.writeListStart();
|
||||
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||
w.writeBytes(TestUtils.getRandomId());
|
||||
w.writeRaw(TestUtils.getRandomId());
|
||||
}
|
||||
if(tooBig) w.writeBytes(TestUtils.getRandomId());
|
||||
if(tooBig) w.writeRaw(TestUtils.getRandomId());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
@@ -178,9 +178,9 @@ public class PacketReaderImplTest extends BriarTestCase {
|
||||
w.writeListStart();
|
||||
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||
w.writeBytes(TestUtils.getRandomId());
|
||||
w.writeRaw(TestUtils.getRandomId());
|
||||
}
|
||||
if(tooBig) w.writeBytes(TestUtils.getRandomId());
|
||||
if(tooBig) w.writeRaw(TestUtils.getRandomId());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
@@ -212,9 +212,9 @@ public class PacketReaderImplTest extends BriarTestCase {
|
||||
w.writeListStart();
|
||||
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||
w.writeBytes(TestUtils.getRandomId());
|
||||
w.writeRaw(TestUtils.getRandomId());
|
||||
}
|
||||
if(tooBig) w.writeBytes(TestUtils.getRandomId());
|
||||
if(tooBig) w.writeRaw(TestUtils.getRandomId());
|
||||
w.writeListEnd();
|
||||
w.writeListEnd();
|
||||
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
|
||||
|
||||
Reference in New Issue
Block a user