From 64aa121c9cd632470f4da1d0ed8469d38ff85552 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Fri, 26 Apr 2019 11:06:33 +0100
Subject: [PATCH] Reuse UnsupportedVersionException for handshake links.

---
 .../bramble/api/contact/ContactManager.java     | 17 ++++++++++++-----
 .../bramble/contact/PendingContactFactory.java  |  8 ++++++++
 .../contact/PendingContactFactoryImpl.java      |  5 ++++-
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java
index f131a1d2f4..75e61ccc14 100644
--- a/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java
+++ b/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java
@@ -1,8 +1,10 @@
 package org.briarproject.bramble.api.contact;
 
 import org.briarproject.bramble.api.FormatException;
+import org.briarproject.bramble.api.UnsupportedVersionException;
 import org.briarproject.bramble.api.crypto.SecretKey;
 import org.briarproject.bramble.api.db.DbException;
+import org.briarproject.bramble.api.db.NoSuchContactException;
 import org.briarproject.bramble.api.db.Transaction;
 import org.briarproject.bramble.api.identity.Author;
 import org.briarproject.bramble.api.identity.AuthorId;
@@ -65,10 +67,15 @@ public interface ContactManager {
 	String getHandshakeLink() throws DbException;
 
 	/**
-	 * Adds a new pending contact identified by the given handshake link.
+	 * Creates a {@link PendingContact} from the given handshake link and
+	 * alias, adds it to the database and returns it.
 	 *
-	 * @param link The handshake link received from the contact we want to add.
-	 * @param alias The alias the user has given this contact.
+	 * @param link The handshake link received from the contact we want to add
+	 * @param alias The alias the user has given this contact
+	 * @return A PendingContact representing the contact to be added
+	 * @throws UnsupportedVersionException If the link uses a format version
+	 * that is not supported
+	 * @throws FormatException If the link is invalid
 	 */
 	PendingContact addPendingContact(String link, String alias)
 			throws DbException, FormatException;
@@ -92,7 +99,7 @@ public interface ContactManager {
 	 * Returns the contact with the given remoteAuthorId
 	 * that was added by the LocalAuthor with the given localAuthorId
 	 *
-	 * @throws org.briarproject.bramble.api.db.NoSuchContactException
+	 * @throws NoSuchContactException If the contact is not in the database
 	 */
 	Contact getContact(AuthorId remoteAuthorId, AuthorId localAuthorId)
 			throws DbException;
@@ -101,7 +108,7 @@ public interface ContactManager {
 	 * Returns the contact with the given remoteAuthorId
 	 * that was added by the LocalAuthor with the given localAuthorId
 	 *
-	 * @throws org.briarproject.bramble.api.db.NoSuchContactException
+	 * @throws NoSuchContactException If the contact is not in the database
 	 */
 	Contact getContact(Transaction txn, AuthorId remoteAuthorId,
 			AuthorId localAuthorId) throws DbException;
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactory.java
index eab17d59d5..cfa5555655 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactory.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactory.java
@@ -1,10 +1,18 @@
 package org.briarproject.bramble.contact;
 
 import org.briarproject.bramble.api.FormatException;
+import org.briarproject.bramble.api.UnsupportedVersionException;
 import org.briarproject.bramble.api.contact.PendingContact;
 
 interface PendingContactFactory {
 
+	/**
+	 * Creates a {@link PendingContact} from the given handshake link and alias.
+	 *
+	 * @throws UnsupportedVersionException If the link uses a format version
+	 * that is not supported
+	 * @throws FormatException If the link is invalid
+	 */
 	PendingContact createPendingContact(String link, String alias)
 			throws FormatException;
 }
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java
index 4e4ac27828..1b7b149ea1 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java
@@ -1,6 +1,7 @@
 package org.briarproject.bramble.contact;
 
 import org.briarproject.bramble.api.FormatException;
+import org.briarproject.bramble.api.UnsupportedVersionException;
 import org.briarproject.bramble.api.contact.PendingContact;
 import org.briarproject.bramble.api.contact.PendingContactId;
 import org.briarproject.bramble.api.contact.PendingContactState;
@@ -49,7 +50,9 @@ class PendingContactFactoryImpl implements PendingContactFactory {
 		if (link.startsWith("briar://")) link = link.substring(8);
 		byte[] base32 = Base32.decode(link, false);
 		if (base32.length != RAW_LINK_BYTES) throw new AssertionError();
-		if (base32[0] != FORMAT_VERSION) throw new FormatException();
+		byte version = base32[0];
+		if (version != FORMAT_VERSION)
+			throw new UnsupportedVersionException(version < FORMAT_VERSION);
 		byte[] publicKeyBytes = new byte[base32.length - 1];
 		arraycopy(base32, 1, publicKeyBytes, 0, publicKeyBytes.length);
 		try {
-- 
GitLab