From 5bb8e95b26b1393dcdb30db4a6fd13d7fb309400 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Tue, 8 Mar 2016 17:03:22 +0000
Subject: [PATCH] Pass original message to BDF validators and hooks.

---
 .../clients/BdfIncomingMessageHook.java       |  9 ++++----
 .../clients/BdfMessageValidator.java          |  8 +++----
 .../forum/ForumListValidator.java             | 11 +++++----
 .../forum/ForumPostValidator.java             | 23 ++++++++++---------
 .../messaging/PrivateMessageValidator.java    | 17 +++++++-------
 .../TransportPropertyValidator.java           | 15 ++++++------
 6 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/briar-core/src/org/briarproject/clients/BdfIncomingMessageHook.java b/briar-core/src/org/briarproject/clients/BdfIncomingMessageHook.java
index 9d9f0d691c..5380652c6d 100644
--- a/briar-core/src/org/briarproject/clients/BdfIncomingMessageHook.java
+++ b/briar-core/src/org/briarproject/clients/BdfIncomingMessageHook.java
@@ -29,8 +29,9 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
 		this.metadataParser = metadataParser;
 	}
 
-	protected abstract void incomingMessage(Transaction txn, BdfList message,
-			BdfDictionary meta) throws DbException, FormatException;
+	protected abstract void incomingMessage(Transaction txn, Message m,
+			BdfList body, BdfDictionary meta) throws DbException,
+			FormatException;
 
 	@Override
 	public void incomingMessage(Transaction txn, Message m, Metadata meta)
@@ -48,10 +49,10 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
 			int headerLength) throws DbException {
 		try {
 			byte[] raw = m.getRaw();
-			BdfList message = clientHelper.toList(raw, headerLength,
+			BdfList body = clientHelper.toList(raw, headerLength,
 					raw.length - headerLength);
 			BdfDictionary metaDictionary = metadataParser.parse(meta);
-			incomingMessage(txn, message, metaDictionary);
+			incomingMessage(txn, m, body, metaDictionary);
 		} catch (FormatException e) {
 			throw new DbException(e);
 		}
diff --git a/briar-core/src/org/briarproject/clients/BdfMessageValidator.java b/briar-core/src/org/briarproject/clients/BdfMessageValidator.java
index a2d97cfd31..19bc55db00 100644
--- a/briar-core/src/org/briarproject/clients/BdfMessageValidator.java
+++ b/briar-core/src/org/briarproject/clients/BdfMessageValidator.java
@@ -37,8 +37,8 @@ public abstract class BdfMessageValidator implements MessageValidator,
 		this.clock = clock;
 	}
 
-	protected abstract BdfDictionary validateMessage(BdfList message, Group g,
-			long timestamp) throws FormatException;
+	protected abstract BdfDictionary validateMessage(Message m, Group g,
+			BdfList body) throws FormatException;
 
 	@Override
 	public Metadata validateMessage(Message m, Group g) {
@@ -63,9 +63,9 @@ public abstract class BdfMessageValidator implements MessageValidator,
 			return null;
 		}
 		try {
-			BdfList message = clientHelper.toList(raw, headerLength,
+			BdfList body = clientHelper.toList(raw, headerLength,
 					raw.length - headerLength);
-			BdfDictionary meta = validateMessage(message, g, m.getTimestamp());
+			BdfDictionary meta = validateMessage(m, g, body);
 			if (meta == null) {
 				LOG.info("Invalid message");
 				return null;
diff --git a/briar-core/src/org/briarproject/forum/ForumListValidator.java b/briar-core/src/org/briarproject/forum/ForumListValidator.java
index 94a5ca684f..9e6947a377 100644
--- a/briar-core/src/org/briarproject/forum/ForumListValidator.java
+++ b/briar-core/src/org/briarproject/forum/ForumListValidator.java
@@ -6,6 +6,7 @@ import org.briarproject.api.data.BdfDictionary;
 import org.briarproject.api.data.BdfList;
 import org.briarproject.api.data.MetadataEncoder;
 import org.briarproject.api.sync.Group;
+import org.briarproject.api.sync.Message;
 import org.briarproject.api.system.Clock;
 import org.briarproject.clients.BdfMessageValidator;
 
@@ -20,15 +21,15 @@ class ForumListValidator extends BdfMessageValidator {
 	}
 
 	@Override
-	protected BdfDictionary validateMessage(BdfList message, Group g,
-			long timestamp) throws FormatException {
+	protected BdfDictionary validateMessage(Message m, Group g,
+			BdfList body) throws FormatException {
 		// Version, forum list
-		checkSize(message, 2);
+		checkSize(body, 2);
 		// Version
-		long version = message.getLong(0);
+		long version = body.getLong(0);
 		if (version < 0) throw new FormatException();
 		// Forum list
-		BdfList forumList = message.getList(1);
+		BdfList forumList = body.getList(1);
 		for (int i = 0; i < forumList.size(); i++) {
 			BdfList forum = forumList.getList(i);
 			// Name, salt
diff --git a/briar-core/src/org/briarproject/forum/ForumPostValidator.java b/briar-core/src/org/briarproject/forum/ForumPostValidator.java
index 8bc2734502..aac7b6265d 100644
--- a/briar-core/src/org/briarproject/forum/ForumPostValidator.java
+++ b/briar-core/src/org/briarproject/forum/ForumPostValidator.java
@@ -13,6 +13,7 @@ import org.briarproject.api.data.MetadataEncoder;
 import org.briarproject.api.identity.Author;
 import org.briarproject.api.identity.AuthorFactory;
 import org.briarproject.api.sync.Group;
+import org.briarproject.api.sync.Message;
 import org.briarproject.api.system.Clock;
 import org.briarproject.clients.BdfMessageValidator;
 
@@ -38,16 +39,16 @@ class ForumPostValidator extends BdfMessageValidator {
 	}
 
 	@Override
-	protected BdfDictionary validateMessage(BdfList message, Group g,
-			long timestamp) throws FormatException {
+	protected BdfDictionary validateMessage(Message m, Group g,
+			BdfList body) throws FormatException {
 		// Parent ID, author, content type, forum post body, signature
-		checkSize(message, 5);
+		checkSize(body, 5);
 		// Parent ID is optional
-		byte[] parent = message.getOptionalRaw(0);
+		byte[] parent = body.getOptionalRaw(0);
 		checkLength(parent, UniqueId.LENGTH);
 		// Author is optional
 		Author author = null;
-		BdfList authorList = message.getOptionalList(1);
+		BdfList authorList = body.getOptionalList(1);
 		if (authorList != null) {
 			// Name, public key
 			checkSize(authorList, 2);
@@ -58,13 +59,13 @@ class ForumPostValidator extends BdfMessageValidator {
 			author = authorFactory.createAuthor(name, publicKey);
 		}
 		// Content type
-		String contentType = message.getString(2);
+		String contentType = body.getString(2);
 		checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
 		// Forum post body
-		byte[] body = message.getRaw(3);
-		checkLength(body, 0, MAX_FORUM_POST_BODY_LENGTH);
+		byte[] forumPostBody = body.getRaw(3);
+		checkLength(forumPostBody, 0, MAX_FORUM_POST_BODY_LENGTH);
 		// Signature is optional
-		byte[] sig = message.getOptionalRaw(4);
+		byte[] sig = body.getOptionalRaw(4);
 		checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
 		// If there's an author there must be a signature and vice versa
 		if (author != null && sig == null) {
@@ -82,7 +83,7 @@ class ForumPostValidator extends BdfMessageValidator {
 				KeyParser keyParser = crypto.getSignatureKeyParser();
 				PublicKey key = keyParser.parsePublicKey(author.getPublicKey());
 				// Serialise the data to be signed
-				BdfList signed = BdfList.of(g.getId(), timestamp, parent,
+				BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), parent,
 						authorList, contentType, body);
 				// Verify the signature
 				Signature signature = crypto.getSignature();
@@ -99,7 +100,7 @@ class ForumPostValidator extends BdfMessageValidator {
 		}
 		// Return the metadata
 		BdfDictionary meta = new BdfDictionary();
-		meta.put("timestamp", timestamp);
+		meta.put("timestamp", m.getTimestamp());
 		if (parent != null) meta.put("parent", parent);
 		if (author != null) {
 			BdfDictionary authorMeta = new BdfDictionary();
diff --git a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java
index 0475da174b..550da3f9c7 100644
--- a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java
+++ b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java
@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
 import org.briarproject.api.data.BdfList;
 import org.briarproject.api.data.MetadataEncoder;
 import org.briarproject.api.sync.Group;
+import org.briarproject.api.sync.Message;
 import org.briarproject.api.system.Clock;
 import org.briarproject.clients.BdfMessageValidator;
 
@@ -21,22 +22,22 @@ class PrivateMessageValidator extends BdfMessageValidator {
 	}
 
 	@Override
-	protected BdfDictionary validateMessage(BdfList message, Group g,
-			long timestamp) throws FormatException {
+	protected BdfDictionary validateMessage(Message m, Group g,
+			BdfList body) throws FormatException {
 		// Parent ID, content type, private message body
-		checkSize(message, 3);
+		checkSize(body, 3);
 		// Parent ID is optional
-		byte[] parentId = message.getOptionalRaw(0);
+		byte[] parentId = body.getOptionalRaw(0);
 		checkLength(parentId, UniqueId.LENGTH);
 		// Content type
-		String contentType = message.getString(1);
+		String contentType = body.getString(1);
 		checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
 		// Private message body
-		byte[] body = message.getRaw(2);
-		checkLength(body, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
+		byte[] privateMessageBody = body.getRaw(2);
+		checkLength(privateMessageBody, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
 		// Return the metadata
 		BdfDictionary meta = new BdfDictionary();
-		meta.put("timestamp", timestamp);
+		meta.put("timestamp", m.getTimestamp());
 		if (parentId != null) meta.put("parent", parentId);
 		meta.put("contentType", contentType);
 		meta.put("local", false);
diff --git a/briar-core/src/org/briarproject/properties/TransportPropertyValidator.java b/briar-core/src/org/briarproject/properties/TransportPropertyValidator.java
index e6fcd69a6c..55b913c7dd 100644
--- a/briar-core/src/org/briarproject/properties/TransportPropertyValidator.java
+++ b/briar-core/src/org/briarproject/properties/TransportPropertyValidator.java
@@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfDictionary;
 import org.briarproject.api.data.BdfList;
 import org.briarproject.api.data.MetadataEncoder;
 import org.briarproject.api.sync.Group;
+import org.briarproject.api.sync.Message;
 import org.briarproject.api.system.Clock;
 import org.briarproject.clients.BdfMessageValidator;
 
@@ -22,21 +23,21 @@ class TransportPropertyValidator extends BdfMessageValidator {
 	}
 
 	@Override
-	protected BdfDictionary validateMessage(BdfList message, Group g,
-			long timestamp) throws FormatException {
+	protected BdfDictionary validateMessage(Message m, Group g,
+			BdfList body) throws FormatException {
 		// Device ID, transport ID, version, properties
-		checkSize(message, 4);
+		checkSize(body, 4);
 		// Device ID
-		byte[] deviceId = message.getRaw(0);
+		byte[] deviceId = body.getRaw(0);
 		checkLength(deviceId, UniqueId.LENGTH);
 		// Transport ID
-		String transportId = message.getString(1);
+		String transportId = body.getString(1);
 		checkLength(transportId, 1, MAX_TRANSPORT_ID_LENGTH);
 		// Version
-		long version = message.getLong(2);
+		long version = body.getLong(2);
 		if (version < 0) throw new FormatException();
 		// Properties
-		BdfDictionary dictionary = message.getDictionary(3);
+		BdfDictionary dictionary = body.getDictionary(3);
 		checkSize(dictionary, 0, MAX_PROPERTIES_PER_TRANSPORT);
 		for (String key : dictionary.keySet()) {
 			checkLength(key, 0, MAX_PROPERTY_LENGTH);
-- 
GitLab