From b2ee1b533236e699a19b509a04df77630d7183ed Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Tue, 25 Sep 2012 17:09:59 +0100
Subject: [PATCH] Unit tests and a bug fix for key management methods in
 JdbcDatabase.

---
 components/net/sf/briar/db/JdbcDatabase.java |   2 +-
 test/net/sf/briar/db/H2DatabaseTest.java     | 123 ++++++++++++++++++-
 2 files changed, 122 insertions(+), 3 deletions(-)

diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java
index 52e7a2c8de..fd7aa47cc4 100644
--- a/components/net/sf/briar/db/JdbcDatabase.java
+++ b/components/net/sf/briar/db/JdbcDatabase.java
@@ -2347,7 +2347,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 			long period, long centre, byte[] bitmap) throws DbException {
 		PreparedStatement ps = null;
 		try {
-			String sql = "UPDATE secrets SET centre = ? AND bitmap = ?"
+			String sql = "UPDATE secrets SET centre = ?, bitmap = ?"
 					+ " WHERE contactId = ? AND transportId = ? AND period = ?";
 			ps = txn.prepareStatement(sql);
 			ps.setLong(1, centre);
diff --git a/test/net/sf/briar/db/H2DatabaseTest.java b/test/net/sf/briar/db/H2DatabaseTest.java
index 732d384560..d9a983cf67 100644
--- a/test/net/sf/briar/db/H2DatabaseTest.java
+++ b/test/net/sf/briar/db/H2DatabaseTest.java
@@ -1720,8 +1720,6 @@ public class H2DatabaseTest extends BriarTestCase {
 		db.close();
 	}
 
-	// FIXME: Test new methods
-
 	@Test
 	public void testTemporarySecrets() throws Exception {
 		// Create some temporary secrets
@@ -1814,6 +1812,127 @@ public class H2DatabaseTest extends BriarTestCase {
 		db.close();
 	}
 
+	@Test
+	public void testIncrementConnectionCounter() throws Exception {
+		// Create a temporary secret
+		Random random = new Random();
+		byte[] secret = new byte[32], bitmap = new byte[4];
+		random.nextBytes(secret);
+		TemporarySecret ts = new TemporarySecret(contactId, transportId, 0L,
+				secret, 0L, 0L, bitmap);
+
+		Database<Connection> db = open(false);
+		Connection txn = db.startTransaction();
+
+		// Add a contact and the temporary secret
+		assertEquals(contactId, db.addContact(txn));
+		db.addSecrets(txn, Arrays.asList(ts));
+
+		// Retrieve the secret
+		Collection<TemporarySecret> secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(0L, ts.getOutgoingConnectionCounter());
+		assertEquals(0L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		// Increment the connection counter twice and retrieve the secret again
+		db.incrementConnectionCounter(txn, contactId, transportId, 0L);
+		db.incrementConnectionCounter(txn, contactId, transportId, 0L);
+		secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(2L, ts.getOutgoingConnectionCounter());
+		assertEquals(0L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		// Incrementing a nonexistent counter should not throw an exception
+		db.incrementConnectionCounter(txn, contactId, transportId, 1L);
+		// The nonexistent counter should not have been created
+		secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(2L, ts.getOutgoingConnectionCounter());
+		assertEquals(0L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		db.commitTransaction(txn);
+		db.close();
+	}
+
+	@Test
+	public void testSetConnectionWindow() throws Exception {
+		// Create a temporary secret
+		Random random = new Random();
+		byte[] secret = new byte[32], bitmap = new byte[4];
+		random.nextBytes(secret);
+		TemporarySecret ts = new TemporarySecret(contactId, transportId, 0L,
+				secret, 0L, 0L, bitmap);
+
+		Database<Connection> db = open(false);
+		Connection txn = db.startTransaction();
+
+		// Add a contact and the temporary secret
+		assertEquals(contactId, db.addContact(txn));
+		db.addSecrets(txn, Arrays.asList(ts));
+
+		// Retrieve the secret
+		Collection<TemporarySecret> secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(0L, ts.getOutgoingConnectionCounter());
+		assertEquals(0L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		// Update the connection window and retrieve the secret again
+		db.setConnectionWindow(txn, contactId, transportId, 0L, 1L, bitmap);
+		bitmap[0] = 4;
+		db.setConnectionWindow(txn, contactId, transportId, 0L, 1L, bitmap);
+		secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(0L, ts.getOutgoingConnectionCounter());
+		assertEquals(1L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		// Updating a nonexistent window should not throw an exception
+		db.setConnectionWindow(txn, contactId, transportId, 1L, 1L, bitmap);
+		// The nonexistent window should not have been created
+		secrets = db.getSecrets(txn);
+		assertEquals(1, secrets.size());
+		ts = secrets.iterator().next();
+		assertEquals(contactId, ts.getContactId());
+		assertEquals(transportId, ts.getTransportId());
+		assertEquals(0L, ts.getPeriod());
+		assertArrayEquals(secret, ts.getSecret());
+		assertEquals(0L, ts.getOutgoingConnectionCounter());
+		assertEquals(1L, ts.getWindowCentre());
+		assertArrayEquals(bitmap, ts.getWindowBitmap());
+
+		db.commitTransaction(txn);
+		db.close();
+	}
+
 	@Test
 	public void testContactTransports() throws Exception {
 		// Create some contact transports
-- 
GitLab