From 3697fc6eb18e05dd8f81a41c6d13d71f14d91599 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Tue, 2 Feb 2016 16:40:50 +0000
Subject: [PATCH] Allow different identities to have the same contact.

---
 .../src/org/briarproject/db/JdbcDatabase.java |  1 -
 .../org/briarproject/db/H2DatabaseTest.java   | 30 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java
index 045cb198f8..850ad42487 100644
--- a/briar-core/src/org/briarproject/db/JdbcDatabase.java
+++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java
@@ -100,7 +100,6 @@ abstract class JdbcDatabase implements Database<Connection> {
 					+ " localAuthorId HASH NOT NULL,"
 					+ " status INT NOT NULL,"
 					+ " PRIMARY KEY (contactId),"
-					+ " UNIQUE (authorId),"
 					+ " FOREIGN KEY (localAuthorId)"
 					+ " REFERENCES localAuthors (authorId)"
 					+ " ON DELETE CASCADE)";
diff --git a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
index eb1bbb336c..3b127a88bc 100644
--- a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
+++ b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
@@ -51,6 +51,7 @@ import static org.briarproject.api.sync.ValidationManager.Validity.VALID;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -1157,6 +1158,35 @@ public class H2DatabaseTest extends BriarTestCase {
 		db.close();
 	}
 
+	@Test
+	public void testDifferentLocalPseudonymsCanHaveTheSameContact()
+			throws Exception {
+		AuthorId localAuthorId1 = new AuthorId(TestUtils.getRandomId());
+		LocalAuthor localAuthor1 = new LocalAuthor(localAuthorId1, "Carol",
+				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
+				StorageStatus.ACTIVE);
+
+		Database<Connection> db = open(false);
+		Connection txn = db.startTransaction();
+
+		// Add two local pseudonyms
+		db.addLocalAuthor(txn, localAuthor);
+		db.addLocalAuthor(txn, localAuthor1);
+
+		// Add the same contact for each local pseudonym
+		ContactId contactId = db.addContact(txn, author, localAuthorId);
+		ContactId contactId1 = db.addContact(txn, author, localAuthorId1);
+
+		// The contacts should be distinct
+		assertNotEquals(contactId, contactId1);
+		assertEquals(2, db.getContacts(txn).size());
+		assertEquals(1, db.getContacts(txn, localAuthorId).size());
+		assertEquals(1, db.getContacts(txn, localAuthorId1).size());
+
+		db.commitTransaction(txn);
+		db.close();
+	}
+
 	@Test
 	public void testExceptionHandling() throws Exception {
 		Database<Connection> db = open(false);
-- 
GitLab