From 71a31c2a7aafd65f1d6944a0b53fdecd03e5462e Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Mon, 10 Feb 2014 12:14:09 +0000
Subject: [PATCH] Include creation time in LocalAuthor.

This allows the oldest LocalAuthor to be used as the default.
---
 .../src/org/briarproject/api/LocalAuthor.java |  8 +++++-
 .../src/org/briarproject/db/JdbcDatabase.java | 26 ++++++++++++-------
 .../messaging/AuthorFactoryImpl.java          |  8 ++++--
 .../db/DatabaseComponentTest.java             |  2 +-
 .../org/briarproject/db/H2DatabaseTest.java   |  2 +-
 .../SimplexMessagingIntegrationTest.java      |  4 +--
 6 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/briar-api/src/org/briarproject/api/LocalAuthor.java b/briar-api/src/org/briarproject/api/LocalAuthor.java
index 16da123693..68520b6752 100644
--- a/briar-api/src/org/briarproject/api/LocalAuthor.java
+++ b/briar-api/src/org/briarproject/api/LocalAuthor.java
@@ -4,15 +4,21 @@ package org.briarproject.api;
 public class LocalAuthor extends Author {
 
 	private final byte[] privateKey;
+	private final long created;
 
 	public LocalAuthor(AuthorId id, String name, byte[] publicKey,
-			byte[] privateKey) {
+			byte[] privateKey, long created) {
 		super(id, name, publicKey);
 		this.privateKey = privateKey;
+		this.created = created;
 	}
 
 	/**  Returns the private key used to generate the pseudonym's signatures. */
 	public byte[] getPrivateKey() {
 		return privateKey;
 	}
+
+	public long getTimeCreated() {
+		return created;
+	}
 }
diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java
index 9c66010797..7f4588d10d 100644
--- a/briar-core/src/org/briarproject/db/JdbcDatabase.java
+++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java
@@ -60,8 +60,8 @@ import org.briarproject.api.transport.TemporarySecret;
  */
 abstract class JdbcDatabase implements Database<Connection> {
 
-	private static final int SCHEMA_VERSION = 4;
-	private static final int MIN_SCHEMA_VERSION = 4;
+	private static final int SCHEMA_VERSION = 5;
+	private static final int MIN_SCHEMA_VERSION = 5;
 
 	private static final String CREATE_SETTINGS =
 			"CREATE TABLE settings"
@@ -77,6 +77,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 					+ " name VARCHAR NOT NULL,"
 					+ " publicKey BINARY NOT NULL,"
 					+ " privateKey BINARY NOT NULL,"
+					+ " created BIGINT NOT NULL,"
 					+ " PRIMARY KEY (authorId))";
 
 	// Locking: contact
@@ -789,13 +790,14 @@ abstract class JdbcDatabase implements Database<Connection> {
 		PreparedStatement ps = null;
 		try {
 			String sql = "INSERT INTO localAuthors"
-					+ " (authorId, name, publicKey, privateKey)"
-					+ " VALUES (?, ?, ?, ?)";
+					+ " (authorId, name, publicKey, privateKey, created)"
+					+ " VALUES (?, ?, ?, ?, ?)";
 			ps = txn.prepareStatement(sql);
 			ps.setBytes(1, a.getId().getBytes());
 			ps.setString(2, a.getName());
 			ps.setBytes(3, a.getPublicKey());
 			ps.setBytes(4, a.getPrivateKey());
+			ps.setLong(5, a.getTimeCreated());
 			int affected = ps.executeUpdate();
 			if(affected != 1) throw new DbStateException();
 			ps.close();
@@ -1584,14 +1586,19 @@ abstract class JdbcDatabase implements Database<Connection> {
 		PreparedStatement ps = null;
 		ResultSet rs = null;
 		try {
-			String sql = "SELECT name, publicKey, privateKey FROM localAuthors"
+			String sql = "SELECT name, publicKey, privateKey, created"
+					+ " FROM localAuthors"
 					+ " WHERE authorId = ?";
 			ps = txn.prepareStatement(sql);
 			ps.setBytes(1, a.getBytes());
 			rs = ps.executeQuery();
 			if(!rs.next()) throw new DbStateException();
-			LocalAuthor localAuthor = new LocalAuthor(a, rs.getString(1),
-					rs.getBytes(2), rs.getBytes(3));
+			String name = rs.getString(1);
+			byte[] publicKey = rs.getBytes(2);
+			byte[] privateKey = rs.getBytes(3);
+			long created = rs.getLong(4);
+			LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey,
+					privateKey, created);
 			if(rs.next()) throw new DbStateException();
 			rs.close();
 			ps.close();
@@ -1608,7 +1615,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 		PreparedStatement ps = null;
 		ResultSet rs = null;
 		try {
-			String sql = "SELECT authorId, name, publicKey, privateKey"
+			String sql = "SELECT authorId, name, publicKey, privateKey, created"
 					+ " FROM localAuthors";
 			ps = txn.prepareStatement(sql);
 			rs = ps.executeQuery();
@@ -1618,8 +1625,9 @@ abstract class JdbcDatabase implements Database<Connection> {
 				String name = rs.getString(2);
 				byte[] publicKey = rs.getBytes(3);
 				byte[] privateKey = rs.getBytes(4);
+				long created = rs.getLong(5);
 				authors.add(new LocalAuthor(authorId, name, publicKey,
-						privateKey));
+						privateKey, created));
 			}
 			rs.close();
 			ps.close();
diff --git a/briar-core/src/org/briarproject/messaging/AuthorFactoryImpl.java b/briar-core/src/org/briarproject/messaging/AuthorFactoryImpl.java
index b40dc04ed3..6fca9a6c8e 100644
--- a/briar-core/src/org/briarproject/messaging/AuthorFactoryImpl.java
+++ b/briar-core/src/org/briarproject/messaging/AuthorFactoryImpl.java
@@ -15,16 +15,20 @@ import org.briarproject.api.crypto.CryptoComponent;
 import org.briarproject.api.crypto.MessageDigest;
 import org.briarproject.api.serial.Writer;
 import org.briarproject.api.serial.WriterFactory;
+import org.briarproject.api.system.Clock;
 
 class AuthorFactoryImpl implements AuthorFactory {
 
 	private final CryptoComponent crypto;
 	private final WriterFactory writerFactory;
+	private final Clock clock;
 
 	@Inject
-	AuthorFactoryImpl(CryptoComponent crypto, WriterFactory writerFactory) {
+	AuthorFactoryImpl(CryptoComponent crypto, WriterFactory writerFactory,
+			Clock clock) {
 		this.crypto = crypto;
 		this.writerFactory = writerFactory;
+		this.clock = clock;
 	}
 
 	public Author createAuthor(String name, byte[] publicKey) {
@@ -34,7 +38,7 @@ class AuthorFactoryImpl implements AuthorFactory {
 	public LocalAuthor createLocalAuthor(String name, byte[] publicKey,
 			byte[] privateKey) {
 		return new LocalAuthor(getId(name, publicKey), name, publicKey,
-				privateKey);
+				privateKey, clock.currentTimeMillis());
 	}
 
 	private AuthorId getId(String name, byte[] publicKey) {
diff --git a/briar-tests/src/org/briarproject/db/DatabaseComponentTest.java b/briar-tests/src/org/briarproject/db/DatabaseComponentTest.java
index 44bca26f2f..98d2b8f97a 100644
--- a/briar-tests/src/org/briarproject/db/DatabaseComponentTest.java
+++ b/briar-tests/src/org/briarproject/db/DatabaseComponentTest.java
@@ -86,7 +86,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 		author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
 		localAuthorId = new AuthorId(TestUtils.getRandomId());
 		localAuthor = new LocalAuthor(localAuthorId, "Bob",
-				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
+				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
 		messageId = new MessageId(TestUtils.getRandomId());
 		messageId1 = new MessageId(TestUtils.getRandomId());
 		contentType = "text/plain";
diff --git a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
index 103d12e252..3b4e344cb2 100644
--- a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
+++ b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java
@@ -74,7 +74,7 @@ public class H2DatabaseTest extends BriarTestCase {
 		author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
 		localAuthorId = new AuthorId(TestUtils.getRandomId());
 		localAuthor = new LocalAuthor(localAuthorId, "Bob",
-				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
+				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
 		messageId = new MessageId(TestUtils.getRandomId());
 		contentType = "text/plain";
 		subject = "Foo";
diff --git a/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java
index 1e65c57a0b..a32327b98d 100644
--- a/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java
+++ b/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java
@@ -114,7 +114,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
 		// Add a local pseudonym for Alice
 		AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
 		LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
-				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
+				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
 		db.addLocalAuthor(aliceAuthor);
 		// Add Bob as a contact
 		AuthorId bobId = new AuthorId(TestUtils.getRandomId());
@@ -173,7 +173,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
 		// Add a local pseudonym for Bob
 		AuthorId bobId = new AuthorId(TestUtils.getRandomId());
 		LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
-				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
+				new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
 		db.addLocalAuthor(bobAuthor);
 		// Add Alice as a contact
 		AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
-- 
GitLab