mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Every encoded object should include its identifying tag.
This commit is contained in:
@@ -123,7 +123,6 @@ class BundleReaderImpl implements BundleReader {
|
|||||||
List<Message> messages = new ArrayList<Message>();
|
List<Message> messages = new ArrayList<Message>();
|
||||||
reader.readListStart();
|
reader.readListStart();
|
||||||
while(!reader.hasListEnd()) {
|
while(!reader.hasListEnd()) {
|
||||||
reader.readUserDefinedTag(Tags.MESSAGE);
|
|
||||||
messages.add(messageReader.readMessage(reader));
|
messages.add(messageReader.readMessage(reader));
|
||||||
}
|
}
|
||||||
reader.readListEnd();
|
reader.readListEnd();
|
||||||
|
|||||||
@@ -90,11 +90,8 @@ class BundleWriterImpl implements BundleWriter {
|
|||||||
out.setSigning(true);
|
out.setSigning(true);
|
||||||
writer.writeUserDefinedTag(Tags.BATCH);
|
writer.writeUserDefinedTag(Tags.BATCH);
|
||||||
writer.writeListStart();
|
writer.writeListStart();
|
||||||
for(Raw message : messages) {
|
// Bypass the writer and write the raw messages directly
|
||||||
writer.writeUserDefinedTag(Tags.MESSAGE);
|
for(Raw message : messages) out.write(message.getBytes());
|
||||||
// Bypass the writer and write the raw message directly
|
|
||||||
out.write(message.getBytes());
|
|
||||||
}
|
|
||||||
writer.writeListEnd();
|
writer.writeListEnd();
|
||||||
out.setSigning(false);
|
out.setSigning(false);
|
||||||
// Create and write the signature
|
// Create and write the signature
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import net.sf.briar.api.serial.Consumer;
|
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();
|
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class MessageEncoderImpl implements MessageEncoder {
|
|||||||
byte[] encodedKey = keyPair.getPublic().getEncoded();
|
byte[] encodedKey = keyPair.getPublic().getEncoded();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
Writer w = writerFactory.createWriter(out);
|
Writer w = writerFactory.createWriter(out);
|
||||||
|
w.writeUserDefinedTag(Tags.MESSAGE);
|
||||||
parent.writeTo(w);
|
parent.writeTo(w);
|
||||||
group.writeTo(w);
|
group.writeTo(w);
|
||||||
w.writeUserDefinedTag(Tags.TIMESTAMP);
|
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
|
// The author ID is the hash of the author's nick and public key
|
||||||
out.reset();
|
out.reset();
|
||||||
w = writerFactory.createWriter(out);
|
w = writerFactory.createWriter(out);
|
||||||
|
w.writeUserDefinedTag(Tags.AUTHOR);
|
||||||
w.writeString(nick);
|
w.writeString(nick);
|
||||||
w.writeRaw(encodedKey);
|
w.writeRaw(encodedKey);
|
||||||
w.close();
|
w.close();
|
||||||
|
|||||||
@@ -39,7 +39,9 @@ class MessageReaderImpl implements MessageReader {
|
|||||||
messageDigest.reset();
|
messageDigest.reset();
|
||||||
reader.addConsumer(copying);
|
reader.addConsumer(copying);
|
||||||
reader.addConsumer(counting);
|
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);
|
reader.readUserDefinedTag(Tags.MESSAGE_ID);
|
||||||
byte[] b = reader.readRaw();
|
byte[] b = reader.readRaw();
|
||||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||||
@@ -54,8 +56,8 @@ class MessageReaderImpl implements MessageReader {
|
|||||||
long timestamp = reader.readInt64();
|
long timestamp = reader.readInt64();
|
||||||
if(timestamp < 0L) throw new FormatException();
|
if(timestamp < 0L) throw new FormatException();
|
||||||
// Hash the author's nick and public key to get the author ID
|
// Hash the author's nick and public key to get the author ID
|
||||||
reader.readUserDefinedTag(Tags.AUTHOR);
|
|
||||||
reader.addConsumer(digesting);
|
reader.addConsumer(digesting);
|
||||||
|
reader.readUserDefinedTag(Tags.AUTHOR);
|
||||||
reader.readString();
|
reader.readString();
|
||||||
byte[] encodedKey = reader.readRaw();
|
byte[] encodedKey = reader.readRaw();
|
||||||
reader.removeConsumer(digesting);
|
reader.removeConsumer(digesting);
|
||||||
|
|||||||
@@ -70,4 +70,16 @@ public class ConsumersTest extends TestCase {
|
|||||||
assertTrue(false);
|
assertTrue(false);
|
||||||
} catch(FormatException expected) {}
|
} 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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user