Every encoded object should include its identifying tag.

This commit is contained in:
akwizgran
2011-07-19 14:21:07 +01:00
parent ff984c69fb
commit a9e7cbd05c
6 changed files with 22 additions and 9 deletions

View File

@@ -123,7 +123,6 @@ class BundleReaderImpl implements BundleReader {
List<Message> messages = new ArrayList<Message>();
reader.readListStart();
while(!reader.hasListEnd()) {
reader.readUserDefinedTag(Tags.MESSAGE);
messages.add(messageReader.readMessage(reader));
}
reader.readListEnd();

View File

@@ -90,11 +90,8 @@ class BundleWriterImpl implements BundleWriter {
out.setSigning(true);
writer.writeUserDefinedTag(Tags.BATCH);
writer.writeListStart();
for(Raw message : messages) {
writer.writeUserDefinedTag(Tags.MESSAGE);
// Bypass the writer and write the raw message directly
out.write(message.getBytes());
}
// Bypass the writer and write the raw messages directly
for(Raw message : messages) out.write(message.getBytes());
writer.writeListEnd();
out.setSigning(false);
// Create and write the signature

View File

@@ -5,7 +5,8 @@ import java.io.IOException;
import net.sf.briar.api.serial.Consumer;
public class CopyingConsumer implements Consumer {
/** A consumer that makes a copy of the bytes consumed. */
class CopyingConsumer implements Consumer {
private final ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@@ -36,6 +36,7 @@ class MessageEncoderImpl implements MessageEncoder {
byte[] encodedKey = keyPair.getPublic().getEncoded();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeUserDefinedTag(Tags.MESSAGE);
parent.writeTo(w);
group.writeTo(w);
w.writeUserDefinedTag(Tags.TIMESTAMP);
@@ -60,6 +61,7 @@ class MessageEncoderImpl implements MessageEncoder {
// The author ID is the hash of the author's nick and public key
out.reset();
w = writerFactory.createWriter(out);
w.writeUserDefinedTag(Tags.AUTHOR);
w.writeString(nick);
w.writeRaw(encodedKey);
w.close();

View File

@@ -39,7 +39,9 @@ class MessageReaderImpl implements MessageReader {
messageDigest.reset();
reader.addConsumer(copying);
reader.addConsumer(counting);
// Read the parent message ID
// Read the initial tag
reader.readUserDefinedTag(Tags.MESSAGE);
// Read the parent's message ID
reader.readUserDefinedTag(Tags.MESSAGE_ID);
byte[] b = reader.readRaw();
if(b.length != UniqueId.LENGTH) throw new FormatException();
@@ -54,8 +56,8 @@ class MessageReaderImpl implements MessageReader {
long timestamp = reader.readInt64();
if(timestamp < 0L) throw new FormatException();
// Hash the author's nick and public key to get the author ID
reader.readUserDefinedTag(Tags.AUTHOR);
reader.addConsumer(digesting);
reader.readUserDefinedTag(Tags.AUTHOR);
reader.readString();
byte[] encodedKey = reader.readRaw();
reader.removeConsumer(digesting);

View File

@@ -70,4 +70,16 @@ public class ConsumersTest extends TestCase {
assertTrue(false);
} catch(FormatException expected) {}
}
@Test
public void testCopyingConsumer() throws Exception {
byte[] data = new byte[1234];
new Random().nextBytes(data);
// Check that a CopyingConsumer creates a faithful copy
CopyingConsumer cc = new CopyingConsumer();
cc.write(data[0]);
cc.write(data, 1, data.length - 2);
cc.write(data[data.length - 1]);
assertTrue(Arrays.equals(data, cc.getCopy()));
}
}