Renamed raw data type.

This commit is contained in:
akwizgran
2015-05-02 21:05:23 +01:00
parent b8e37a5421
commit 41c4c4d808
16 changed files with 92 additions and 92 deletions

View File

@@ -30,9 +30,9 @@ public interface Reader {
String readString(int maxLength) throws IOException; String readString(int maxLength) throws IOException;
void skipString() throws IOException; void skipString() throws IOException;
boolean hasBytes() throws IOException; boolean hasRaw() throws IOException;
byte[] readBytes(int maxLength) throws IOException; byte[] readRaw(int maxLength) throws IOException;
void skipBytes() throws IOException; void skipRaw() throws IOException;
boolean hasList() throws IOException; boolean hasList() throws IOException;
void readListStart() throws IOException; void readListStart() throws IOException;

View File

@@ -17,7 +17,7 @@ public interface Writer {
void writeInteger(long l) throws IOException; void writeInteger(long l) throws IOException;
void writeFloat(double d) throws IOException; void writeFloat(double d) throws IOException;
void writeString(String s) 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 writeList(Collection<?> c) throws IOException;
void writeListStart() throws IOException; void writeListStart() throws IOException;

View File

@@ -90,7 +90,7 @@ class ReaderImpl implements Reader {
else if(hasInteger()) skipInteger(); else if(hasInteger()) skipInteger();
else if(hasFloat()) skipFloat(); else if(hasFloat()) skipFloat();
else if(hasString()) skipString(); else if(hasString()) skipString();
else if(hasBytes()) skipBytes(); else if(hasRaw()) skipRaw();
else if(hasList()) skipList(); else if(hasList()) skipList();
else if(hasMap()) skipMap(); else if(hasMap()) skipMap();
else if(hasNull()) skipNull(); else if(hasNull()) skipNull();
@@ -262,16 +262,16 @@ class ReaderImpl implements Reader {
hasLookahead = false; hasLookahead = false;
} }
public boolean hasBytes() throws IOException { public boolean hasRaw() throws IOException {
if(!hasLookahead) readLookahead(); if(!hasLookahead) readLookahead();
if(eof) return false; if(eof) return false;
return next == RAW_8 || next == RAW_16 || next == RAW_32; return next == RAW_8 || next == RAW_16 || next == RAW_32;
} }
public byte[] readBytes(int maxLength) throws IOException { public byte[] readRaw(int maxLength) throws IOException {
if(!hasBytes()) throw new FormatException(); if(!hasRaw()) throw new FormatException();
consumeLookahead(); consumeLookahead();
int length = readBytesLength(true); int length = readRawLength(true);
if(length < 0 || length > maxLength) throw new FormatException(); if(length < 0 || length > maxLength) throw new FormatException();
if(length == 0) return EMPTY_BUFFER; if(length == 0) return EMPTY_BUFFER;
byte[] b = new byte[length]; byte[] b = new byte[length];
@@ -279,16 +279,16 @@ class ReaderImpl implements Reader {
return b; 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_8) return readInt8(consume);
if(next == RAW_16) return readInt16(consume); if(next == RAW_16) return readInt16(consume);
if(next == RAW_32) return readInt32(consume); if(next == RAW_32) return readInt32(consume);
throw new FormatException(); throw new FormatException();
} }
public void skipBytes() throws IOException { public void skipRaw() throws IOException {
if(!hasBytes()) throw new FormatException(); if(!hasRaw()) throw new FormatException();
int length = readBytesLength(false); int length = readRawLength(false);
if(length < 0) throw new FormatException(); if(length < 0) throw new FormatException();
skip(length); skip(length);
hasLookahead = false; hasLookahead = false;

View File

@@ -124,7 +124,7 @@ class WriterImpl implements Writer {
write(b); write(b);
} }
public void writeBytes(byte[] b) throws IOException { public void writeRaw(byte[] b) throws IOException {
if(b.length <= Byte.MAX_VALUE) { if(b.length <= Byte.MAX_VALUE) {
write(RAW_8); write(RAW_8);
write((byte) b.length); write((byte) b.length);
@@ -153,8 +153,8 @@ class WriterImpl implements Writer {
else if(o instanceof Float) writeFloat((Float) o); else if(o instanceof Float) writeFloat((Float) o);
else if(o instanceof Double) writeFloat((Double) o); else if(o instanceof Double) writeFloat((Double) o);
else if(o instanceof String) writeString((String) o); else if(o instanceof String) writeString((String) o);
else if(o instanceof byte[]) writeBytes((byte[]) o); else if(o instanceof byte[]) writeRaw((byte[]) o);
else if(o instanceof Bytes) writeBytes(((Bytes) o).getBytes()); else if(o instanceof Bytes) writeRaw(((Bytes) o).getBytes());
else if(o instanceof List<?>) writeList((List<?>) o); else if(o instanceof List<?>) writeList((List<?>) o);
else if(o instanceof Map<?, ?>) writeMap((Map<?, ?>) o); else if(o instanceof Map<?, ?>) writeMap((Map<?, ?>) o);
else if(o == null) writeNull(); else if(o == null) writeNull();

View File

@@ -124,14 +124,14 @@ abstract class Connector extends Thread {
} }
protected void sendPublicKeyHash(Writer w) throws IOException { protected void sendPublicKeyHash(Writer w) throws IOException {
w.writeBytes(messageDigest.digest(keyPair.getPublic().getEncoded())); w.writeRaw(messageDigest.digest(keyPair.getPublic().getEncoded()));
w.flush(); w.flush();
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent hash"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent hash");
} }
protected byte[] receivePublicKeyHash(Reader r) throws IOException { protected byte[] receivePublicKeyHash(Reader r) throws IOException {
int hashLength = messageDigest.getDigestLength(); int hashLength = messageDigest.getDigestLength();
byte[] b = r.readBytes(hashLength); byte[] b = r.readRaw(hashLength);
if(b.length < hashLength) throw new FormatException(); if(b.length < hashLength) throw new FormatException();
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received hash"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received hash");
return b; return b;
@@ -139,14 +139,14 @@ abstract class Connector extends Thread {
protected void sendPublicKey(Writer w) throws IOException { protected void sendPublicKey(Writer w) throws IOException {
byte[] key = keyPair.getPublic().getEncoded(); byte[] key = keyPair.getPublic().getEncoded();
w.writeBytes(key); w.writeRaw(key);
w.flush(); w.flush();
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent key"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent key");
} }
protected byte[] receivePublicKey(Reader r) throws GeneralSecurityException, protected byte[] receivePublicKey(Reader r) throws GeneralSecurityException,
IOException { IOException {
byte[] b = r.readBytes(MAX_PUBLIC_KEY_LENGTH); byte[] b = r.readRaw(MAX_PUBLIC_KEY_LENGTH);
keyParser.parsePublicKey(b); keyParser.parsePublicKey(b);
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received key"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received key");
return b; return b;
@@ -192,8 +192,8 @@ abstract class Connector extends Thread {
byte[] sig = signature.sign(); byte[] sig = signature.sign();
// Write the name, public key and signature // Write the name, public key and signature
w.writeString(localAuthor.getName()); w.writeString(localAuthor.getName());
w.writeBytes(localAuthor.getPublicKey()); w.writeRaw(localAuthor.getPublicKey());
w.writeBytes(sig); w.writeRaw(sig);
w.flush(); w.flush();
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent pseudonym"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " sent pseudonym");
} }
@@ -202,8 +202,8 @@ abstract class Connector extends Thread {
throws GeneralSecurityException, IOException { throws GeneralSecurityException, IOException {
// Read the name, public key and signature // Read the name, public key and signature
String name = r.readString(MAX_AUTHOR_NAME_LENGTH); String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = r.readBytes(MAX_PUBLIC_KEY_LENGTH); byte[] publicKey = r.readRaw(MAX_PUBLIC_KEY_LENGTH);
byte[] sig = r.readBytes(MAX_SIGNATURE_LENGTH); byte[] sig = r.readRaw(MAX_SIGNATURE_LENGTH);
if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received pseudonym"); if(LOG.isLoggable(INFO)) LOG.info(pluginName + " received pseudonym");
// Verify the signature // Verify the signature
Signature signature = crypto.getSignature(); Signature signature = crypto.getSignature();

View File

@@ -45,7 +45,7 @@ class AuthorFactoryImpl implements AuthorFactory {
try { try {
w.writeListStart(); w.writeListStart();
w.writeString(name); w.writeString(name);
w.writeBytes(publicKey); w.writeRaw(publicKey);
w.writeListEnd(); w.writeListEnd();
} catch(IOException e) { } catch(IOException e) {
// Shouldn't happen with ByteArrayOutputStream // Shouldn't happen with ByteArrayOutputStream

View File

@@ -29,7 +29,7 @@ class AuthorReader implements ObjectReader<Author> {
r.readListStart(); r.readListStart();
String name = r.readString(MAX_AUTHOR_NAME_LENGTH); String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
if(name.length() == 0) throw new FormatException(); 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(); r.readListEnd();
// Reset the reader // Reset the reader
r.removeConsumer(digesting); r.removeConsumer(digesting);

View File

@@ -38,7 +38,7 @@ class GroupFactoryImpl implements GroupFactory {
try { try {
w.writeListStart(); w.writeListStart();
w.writeString(name); w.writeString(name);
w.writeBytes(salt); w.writeRaw(salt);
w.writeListEnd(); w.writeListEnd();
} catch(IOException e) { } catch(IOException e) {
// Shouldn't happen with ByteArrayOutputStream // Shouldn't happen with ByteArrayOutputStream

View File

@@ -28,7 +28,7 @@ class GroupReader implements ObjectReader<Group> {
r.readListStart(); r.readListStart();
String name = r.readString(MAX_GROUP_NAME_LENGTH); String name = r.readString(MAX_GROUP_NAME_LENGTH);
if(name.length() == 0) throw new FormatException(); 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(); if(salt.length != GROUP_SALT_LENGTH) throw new FormatException();
r.readListEnd(); r.readListEnd();
r.removeConsumer(digesting); r.removeConsumer(digesting);

View File

@@ -84,7 +84,7 @@ class MessageFactoryImpl implements MessageFactory {
// Write the message // Write the message
w.writeListStart(); w.writeListStart();
if(parent == null) w.writeNull(); if(parent == null) w.writeNull();
else w.writeBytes(parent.getBytes()); else w.writeRaw(parent.getBytes());
writeGroup(w, group); writeGroup(w, group);
if(author == null) w.writeNull(); if(author == null) w.writeNull();
else writeAuthor(w, author); else writeAuthor(w, author);
@@ -92,8 +92,8 @@ class MessageFactoryImpl implements MessageFactory {
w.writeInteger(timestamp); w.writeInteger(timestamp);
byte[] salt = new byte[MESSAGE_SALT_LENGTH]; byte[] salt = new byte[MESSAGE_SALT_LENGTH];
random.nextBytes(salt); random.nextBytes(salt);
w.writeBytes(salt); w.writeRaw(salt);
w.writeBytes(body); w.writeRaw(body);
int bodyStart = (int) counting.getCount() - body.length; int bodyStart = (int) counting.getCount() - body.length;
// Sign the message with the author's private key, if there is one // Sign the message with the author's private key, if there is one
if(privateKey == null) { if(privateKey == null) {
@@ -103,7 +103,7 @@ class MessageFactoryImpl implements MessageFactory {
byte[] sig = signature.sign(); byte[] sig = signature.sign();
if(sig.length > MAX_SIGNATURE_LENGTH) if(sig.length > MAX_SIGNATURE_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
w.writeBytes(sig); w.writeRaw(sig);
} }
w.writeListEnd(); w.writeListEnd();
// Hash the message, including the signature, to get the message ID // 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 { private void writeGroup(Writer w, Group g) throws IOException {
w.writeListStart(); w.writeListStart();
w.writeString(g.getName()); w.writeString(g.getName());
w.writeBytes(g.getSalt()); w.writeRaw(g.getSalt());
w.writeListEnd(); w.writeListEnd();
} }
private void writeAuthor(Writer w, Author a) throws IOException { private void writeAuthor(Writer w, Author a) throws IOException {
w.writeListStart(); w.writeListStart();
w.writeString(a.getName()); w.writeString(a.getName());
w.writeBytes(a.getPublicKey()); w.writeRaw(a.getPublicKey());
w.writeListEnd(); w.writeListEnd();
} }
} }

View File

@@ -40,7 +40,7 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
if(r.hasNull()) { if(r.hasNull()) {
r.readNull(); r.readNull();
} else { } else {
byte[] b = r.readBytes(UniqueId.LENGTH); byte[] b = r.readRaw(UniqueId.LENGTH);
if(b.length < UniqueId.LENGTH) throw new FormatException(); if(b.length < UniqueId.LENGTH) throw new FormatException();
parent = new MessageId(b); parent = new MessageId(b);
} }
@@ -56,10 +56,10 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
long timestamp = r.readInteger(); long timestamp = r.readInteger();
if(timestamp < 0) throw new FormatException(); if(timestamp < 0) throw new FormatException();
// Read the salt // 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(); if(salt.length < MESSAGE_SALT_LENGTH) throw new FormatException();
// Read the message body // 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 // Record the offset of the body within the message
int bodyStart = (int) counting.getCount() - body.length; int bodyStart = (int) counting.getCount() - body.length;
// Record the length of the data covered by the author's signature // 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 // Read the author's signature, if there is one
byte[] signature = null; byte[] signature = null;
if(author == null) r.readNull(); 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 // Read the end of the message
r.readListEnd(); r.readListEnd();
// Reset the reader // Reset the reader

View File

@@ -122,7 +122,7 @@ class PacketReaderImpl implements PacketReader {
List<MessageId> acked = new ArrayList<MessageId>(); List<MessageId> acked = new ArrayList<MessageId>();
r.readListStart(); r.readListStart();
while(!r.hasListEnd()) { while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH); byte[] b = r.readRaw(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) if(b.length != UniqueId.LENGTH)
throw new FormatException(); throw new FormatException();
acked.add(new MessageId(b)); acked.add(new MessageId(b));
@@ -168,7 +168,7 @@ class PacketReaderImpl implements PacketReader {
List<MessageId> offered = new ArrayList<MessageId>(); List<MessageId> offered = new ArrayList<MessageId>();
r.readListStart(); r.readListStart();
while(!r.hasListEnd()) { while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH); byte[] b = r.readRaw(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) if(b.length != UniqueId.LENGTH)
throw new FormatException(); throw new FormatException();
offered.add(new MessageId(b)); offered.add(new MessageId(b));
@@ -198,7 +198,7 @@ class PacketReaderImpl implements PacketReader {
r.readListStart(); r.readListStart();
List<MessageId> requested = new ArrayList<MessageId>(); List<MessageId> requested = new ArrayList<MessageId>();
while(!r.hasListEnd()) { while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH); byte[] b = r.readRaw(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) if(b.length != UniqueId.LENGTH)
throw new FormatException(); throw new FormatException();
requested.add(new MessageId(b)); requested.add(new MessageId(b));

View File

@@ -85,7 +85,7 @@ class PacketWriterImpl implements PacketWriter {
Writer w = writerFactory.createWriter(payload); Writer w = writerFactory.createWriter(payload);
w.writeListStart(); w.writeListStart();
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();
w.writeListEnd(); w.writeListEnd();
writePacket(ACK); writePacket(ACK);
@@ -103,7 +103,7 @@ class PacketWriterImpl implements PacketWriter {
Writer w = writerFactory.createWriter(payload); Writer w = writerFactory.createWriter(payload);
w.writeListStart(); w.writeListStart();
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();
w.writeListEnd(); w.writeListEnd();
writePacket(OFFER); writePacket(OFFER);
@@ -114,7 +114,7 @@ class PacketWriterImpl implements PacketWriter {
Writer w = writerFactory.createWriter(payload); Writer w = writerFactory.createWriter(payload);
w.writeListStart(); w.writeListStart();
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();
w.writeListEnd(); w.writeListEnd();
writePacket(REQUEST); writePacket(REQUEST);
@@ -157,7 +157,7 @@ class PacketWriterImpl implements PacketWriter {
for(Group g : u.getGroups()) { for(Group g : u.getGroups()) {
w.writeListStart(); w.writeListStart();
w.writeString(g.getName()); w.writeString(g.getName());
w.writeBytes(g.getSalt()); w.writeRaw(g.getSalt());
w.writeListEnd(); w.writeListEnd();
} }
w.writeListEnd(); w.writeListEnd();

View File

@@ -330,138 +330,138 @@ public class ReaderImplTest extends BriarTestCase {
} }
@Test @Test
public void testReadBytes8() throws Exception { public void testReadRaw8() throws Exception {
byte[] longest = new byte[Byte.MAX_VALUE]; byte[] longest = new byte[Byte.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
// {1, 2, 3}, {}, and 127 zero bytes // {1, 2, 3}, {}, and 127 zero bytes
setContents("51" + "03" + "010203" + "51" + "00" + setContents("51" + "03" + "010203" + "51" + "00" +
"51" + "7F" + longHex); "51" + "7F" + longHex);
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(Integer.MAX_VALUE));
assertArrayEquals(new byte[0], r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(new byte[0], r.readRaw(Integer.MAX_VALUE));
assertArrayEquals(longest, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(longest, r.readRaw(Integer.MAX_VALUE));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadBytes8ChecksMaxLength() throws Exception { public void testReadRaw8ChecksMaxLength() throws Exception {
// {1, 2, 3} twice // {1, 2, 3} twice
setContents("51" + "03" + "010203" + "51" + "03" + "010203"); setContents("51" + "03" + "010203" + "51" + "03" + "010203");
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(3)); assertArrayEquals(new byte[] {1, 2, 3}, r.readRaw(3));
assertTrue(r.hasBytes()); assertTrue(r.hasRaw());
try { try {
r.readBytes(2); r.readRaw(2);
fail(); fail();
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }
@Test @Test
public void testSkipBytes8() throws Exception { public void testSkipRaw8() throws Exception {
byte[] longest = new byte[Byte.MAX_VALUE]; byte[] longest = new byte[Byte.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
// {1, 2, 3}, {}, and 127 zero bytes // {1, 2, 3}, {}, and 127 zero bytes
setContents("51" + "03" + "010203" + "51" + "00" + setContents("51" + "03" + "010203" + "51" + "00" +
"51" + "7F" + longHex); "51" + "7F" + longHex);
r.skipBytes(); r.skipRaw();
r.skipBytes(); r.skipRaw();
r.skipBytes(); r.skipRaw();
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadBytes16() throws Exception { public void testReadRaw16() throws Exception {
byte[] shortest = new byte[Byte.MAX_VALUE + 1]; byte[] shortest = new byte[Byte.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
byte[] longest = new byte[Short.MAX_VALUE]; byte[] longest = new byte[Short.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
// 128 zero bytes and 2^15 - 1 zero bytes // 128 zero bytes and 2^15 - 1 zero bytes
setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex); setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
assertArrayEquals(shortest, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(shortest, r.readRaw(Integer.MAX_VALUE));
assertArrayEquals(longest, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(longest, r.readRaw(Integer.MAX_VALUE));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadBytes16ChecksMaxLength() throws Exception { public void testReadRaw16ChecksMaxLength() throws Exception {
byte[] shortest = new byte[Byte.MAX_VALUE + 1]; byte[] shortest = new byte[Byte.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
// 128 zero bytes, twice // 128 zero bytes, twice
setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex); setContents("52" + "0080" + shortHex + "52" + "0080" + shortHex);
assertArrayEquals(shortest, r.readBytes(Byte.MAX_VALUE + 1)); assertArrayEquals(shortest, r.readRaw(Byte.MAX_VALUE + 1));
assertTrue(r.hasBytes()); assertTrue(r.hasRaw());
try { try {
r.readBytes(Byte.MAX_VALUE); r.readRaw(Byte.MAX_VALUE);
fail(); fail();
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }
@Test @Test
public void testSkipBytes16() throws Exception { public void testSkipRaw16() throws Exception {
byte[] shortest = new byte[Byte.MAX_VALUE + 1]; byte[] shortest = new byte[Byte.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
byte[] longest = new byte[Short.MAX_VALUE]; byte[] longest = new byte[Short.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
// 128 zero bytes and 2^15 - 1 zero bytes // 128 zero bytes and 2^15 - 1 zero bytes
setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex); setContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
r.skipBytes(); r.skipRaw();
r.skipBytes(); r.skipRaw();
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadBytes32() throws Exception { public void testReadRaw32() throws Exception {
byte[] shortest = new byte[Short.MAX_VALUE + 1]; byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
// 2^15 zero bytes // 2^15 zero bytes
setContents("54" + "00008000" + shortHex); setContents("54" + "00008000" + shortHex);
assertArrayEquals(shortest, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(shortest, r.readRaw(Integer.MAX_VALUE));
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testReadBytes32ChecksMaxLength() throws Exception { public void testReadRaw32ChecksMaxLength() throws Exception {
byte[] shortest = new byte[Short.MAX_VALUE + 1]; byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
// 2^15 zero bytes, twice // 2^15 zero bytes, twice
setContents("54" + "00008000" + shortHex + setContents("54" + "00008000" + shortHex +
"54" + "00008000" + shortHex); "54" + "00008000" + shortHex);
assertArrayEquals(shortest, r.readBytes(Short.MAX_VALUE + 1)); assertArrayEquals(shortest, r.readRaw(Short.MAX_VALUE + 1));
assertTrue(r.hasBytes()); assertTrue(r.hasRaw());
try { try {
r.readBytes(Short.MAX_VALUE); r.readRaw(Short.MAX_VALUE);
fail(); fail();
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }
@Test @Test
public void testSkipBytes32() throws Exception { public void testSkipRaw32() throws Exception {
byte[] shortest = new byte[Short.MAX_VALUE + 1]; byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
// 2^15 zero bytes, twice // 2^15 zero bytes, twice
setContents("54" + "00008000" + shortHex + setContents("54" + "00008000" + shortHex +
"54" + "00008000" + shortHex); "54" + "00008000" + shortHex);
r.skipBytes(); r.skipRaw();
r.skipBytes(); r.skipRaw();
assertTrue(r.eof()); assertTrue(r.eof());
} }
@Test @Test
public void testBytesMustHaveMinimalLength() throws Exception { public void testRawMustHaveMinimalLength() throws Exception {
// RAW_16 could be encoded as RAW_8 // RAW_16 could be encoded as RAW_8
byte[] longest8 = new byte[Byte.MAX_VALUE]; byte[] longest8 = new byte[Byte.MAX_VALUE];
String long8Hex = StringUtils.toHexString(longest8); String long8Hex = StringUtils.toHexString(longest8);
setContents("51" + "7F" + long8Hex + "52" + "007F" + long8Hex); setContents("51" + "7F" + long8Hex + "52" + "007F" + long8Hex);
assertArrayEquals(longest8, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(longest8, r.readRaw(Integer.MAX_VALUE));
try { try {
r.readBytes(Integer.MAX_VALUE); r.readRaw(Integer.MAX_VALUE);
fail(); fail();
} catch(FormatException expected) {} } catch(FormatException expected) {}
// RAW_32 could be encoded as RAW_16 // RAW_32 could be encoded as RAW_16
byte[] longest16 = new byte[Short.MAX_VALUE]; byte[] longest16 = new byte[Short.MAX_VALUE];
String long16Hex = StringUtils.toHexString(longest16); String long16Hex = StringUtils.toHexString(longest16);
setContents("52" + "7FFF" + long16Hex + "54" + "00007FFF" + long16Hex); setContents("52" + "7FFF" + long16Hex + "54" + "00007FFF" + long16Hex);
assertArrayEquals(longest16, r.readBytes(Integer.MAX_VALUE)); assertArrayEquals(longest16, r.readRaw(Integer.MAX_VALUE));
try { try {
r.readBytes(Integer.MAX_VALUE); r.readRaw(Integer.MAX_VALUE);
fail(); fail();
} catch(FormatException expected) {} } catch(FormatException expected) {}
} }

View File

@@ -116,8 +116,8 @@ public class WriterImplTest extends BriarTestCase {
public void testWriteBytes8() throws IOException { public void testWriteBytes8() throws IOException {
byte[] longest = new byte[Byte.MAX_VALUE]; byte[] longest = new byte[Byte.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
w.writeBytes(new byte[] {1, 2, 3}); w.writeRaw(new byte[] {1, 2, 3});
w.writeBytes(longest); w.writeRaw(longest);
// RAW_8 tag, length 3, bytes, RAW_8 tag, length 127, bytes // RAW_8 tag, length 3, bytes, RAW_8 tag, length 127, bytes
checkContents("51" + "03" + "010203" + "51" + "7F" + longHex); checkContents("51" + "03" + "010203" + "51" + "7F" + longHex);
} }
@@ -128,8 +128,8 @@ public class WriterImplTest extends BriarTestCase {
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
byte[] longest = new byte[Short.MAX_VALUE]; byte[] longest = new byte[Short.MAX_VALUE];
String longHex = StringUtils.toHexString(longest); String longHex = StringUtils.toHexString(longest);
w.writeBytes(shortest); w.writeRaw(shortest);
w.writeBytes(longest); w.writeRaw(longest);
// RAW_16 tag, length 128, bytes, RAW_16 tag, length 2^15 - 1, bytes // RAW_16 tag, length 128, bytes, RAW_16 tag, length 2^15 - 1, bytes
checkContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex); checkContents("52" + "0080" + shortHex + "52" + "7FFF" + longHex);
} }
@@ -138,7 +138,7 @@ public class WriterImplTest extends BriarTestCase {
public void testWriteBytes32() throws IOException { public void testWriteBytes32() throws IOException {
byte[] shortest = new byte[Short.MAX_VALUE + 1]; byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest); String shortHex = StringUtils.toHexString(shortest);
w.writeBytes(shortest); w.writeRaw(shortest);
// RAW_32 tag, length 2^15, bytes // RAW_32 tag, length 2^15, bytes
checkContents("54" + "00008000" + shortHex); checkContents("54" + "00008000" + shortHex);
} }

View File

@@ -144,9 +144,9 @@ public class PacketReaderImplTest extends BriarTestCase {
w.writeListStart(); w.writeListStart();
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2 while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) { < 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();
w.writeListEnd(); w.writeListEnd();
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH); assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
@@ -178,9 +178,9 @@ public class PacketReaderImplTest extends BriarTestCase {
w.writeListStart(); w.writeListStart();
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2 while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) { < 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();
w.writeListEnd(); w.writeListEnd();
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH); assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);
@@ -212,9 +212,9 @@ public class PacketReaderImplTest extends BriarTestCase {
w.writeListStart(); w.writeListStart();
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2 while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) { < 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();
w.writeListEnd(); w.writeListEnd();
assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH); assertEquals(tooBig, out.size() > HEADER_LENGTH + MAX_PAYLOAD_LENGTH);