mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Never reuse contact IDs.
This commit is contained in:
@@ -38,7 +38,8 @@ class H2Database extends JdbcDatabase {
|
|||||||
@DatabaseMaxSize long maxSize,
|
@DatabaseMaxSize long maxSize,
|
||||||
ConnectionWindowFactory connectionWindowFactory,
|
ConnectionWindowFactory connectionWindowFactory,
|
||||||
GroupFactory groupFactory) {
|
GroupFactory groupFactory) {
|
||||||
super(connectionWindowFactory, groupFactory, "BINARY(32)", "BINARY");
|
super(connectionWindowFactory, groupFactory, "BINARY(32)", "BINARY",
|
||||||
|
"INT NOT NULL AUTO_INCREMENT");
|
||||||
home = new File(dir, "db");
|
home = new File(dir, "db");
|
||||||
this.password = password;
|
this.password = password;
|
||||||
url = "jdbc:h2:split:" + home.getPath()
|
url = "jdbc:h2:split:" + home.getPath()
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
|
|
||||||
private static final String CREATE_CONTACTS =
|
private static final String CREATE_CONTACTS =
|
||||||
"CREATE TABLE contacts"
|
"CREATE TABLE contacts"
|
||||||
+ " (contactId INT NOT NULL,"
|
+ " (contactId COUNTER,"
|
||||||
+ " secret BINARY NOT NULL,"
|
+ " secret BINARY NOT NULL,"
|
||||||
+ " PRIMARY KEY (contactId))";
|
+ " PRIMARY KEY (contactId))";
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
Logger.getLogger(JdbcDatabase.class.getName());
|
Logger.getLogger(JdbcDatabase.class.getName());
|
||||||
|
|
||||||
// Different database libraries use different names for certain types
|
// Different database libraries use different names for certain types
|
||||||
private final String hashType, binaryType;
|
private final String hashType, binaryType, counterType;
|
||||||
private final ConnectionWindowFactory connectionWindowFactory;
|
private final ConnectionWindowFactory connectionWindowFactory;
|
||||||
private final GroupFactory groupFactory;
|
private final GroupFactory groupFactory;
|
||||||
private final LinkedList<Connection> connections =
|
private final LinkedList<Connection> connections =
|
||||||
@@ -240,11 +240,13 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
protected abstract Connection createConnection() throws SQLException;
|
protected abstract Connection createConnection() throws SQLException;
|
||||||
|
|
||||||
JdbcDatabase(ConnectionWindowFactory connectionWindowFactory,
|
JdbcDatabase(ConnectionWindowFactory connectionWindowFactory,
|
||||||
GroupFactory groupFactory, String hashType, String binaryType) {
|
GroupFactory groupFactory, String hashType, String binaryType,
|
||||||
|
String counterType) {
|
||||||
this.connectionWindowFactory = connectionWindowFactory;
|
this.connectionWindowFactory = connectionWindowFactory;
|
||||||
this.groupFactory = groupFactory;
|
this.groupFactory = groupFactory;
|
||||||
this.hashType = hashType;
|
this.hashType = hashType;
|
||||||
this.binaryType = binaryType;
|
this.binaryType = binaryType;
|
||||||
|
this.counterType = counterType;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void open(boolean resume, File dir, String driverClass)
|
protected void open(boolean resume, File dir, String driverClass)
|
||||||
@@ -321,6 +323,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
private String insertTypeNames(String s) {
|
private String insertTypeNames(String s) {
|
||||||
s = s.replaceAll("HASH", hashType);
|
s = s.replaceAll("HASH", hashType);
|
||||||
s = s.replaceAll("BINARY", binaryType);
|
s = s.replaceAll("BINARY", binaryType);
|
||||||
|
s = s.replaceAll("COUNTER", counterType);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,25 +470,24 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
// Get the highest existing contact ID
|
// Create a new contact row
|
||||||
String sql = "SELECT contactId FROM contacts"
|
String sql = "INSERT INTO contacts (secret) VALUES (?)";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, secret);
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if(affected != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
// Get the new (highest) contact ID
|
||||||
|
sql = "SELECT contactId FROM contacts"
|
||||||
+ " ORDER BY contactId DESC LIMIT ?";
|
+ " ORDER BY contactId DESC LIMIT ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setInt(1, 1);
|
ps.setInt(1, 1);
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
int nextId = rs.next() ? rs.getInt(1) + 1 : 1;
|
if(!rs.next()) throw new DbStateException();
|
||||||
ContactId c = new ContactId(nextId);
|
ContactId c = new ContactId(rs.getInt(1));
|
||||||
if(rs.next()) throw new DbStateException();
|
if(rs.next()) throw new DbStateException();
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
// Create a new contact row
|
|
||||||
sql = "INSERT INTO contacts (contactId, secret) VALUES (?, ?)";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
ps.setBytes(2, secret);
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if(affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
// Store the contact's transport properties
|
// Store the contact's transport properties
|
||||||
sql = "INSERT INTO contactTransports"
|
sql = "INSERT INTO contactTransports"
|
||||||
+ " (contactId, transportId, key, value)"
|
+ " (contactId, transportId, key, value)"
|
||||||
|
|||||||
@@ -186,9 +186,9 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
assertFalse(db.containsContact(txn, contactId2));
|
assertFalse(db.containsContact(txn, contactId2));
|
||||||
assertEquals(contactId2, db.addContact(txn, transports, secret));
|
assertEquals(contactId2, db.addContact(txn, transports, secret));
|
||||||
assertTrue(db.containsContact(txn, contactId2));
|
assertTrue(db.containsContact(txn, contactId2));
|
||||||
// Delete one of the contacts
|
// Delete the contact with the highest ID
|
||||||
db.removeContact(txn, contactId1);
|
db.removeContact(txn, contactId2);
|
||||||
assertFalse(db.containsContact(txn, contactId1));
|
assertFalse(db.containsContact(txn, contactId2));
|
||||||
// Add another contact - a new ID should be created
|
// Add another contact - a new ID should be created
|
||||||
assertFalse(db.containsContact(txn, contactId3));
|
assertFalse(db.containsContact(txn, contactId3));
|
||||||
assertEquals(contactId3, db.addContact(txn, transports, secret));
|
assertEquals(contactId3, db.addContact(txn, transports, secret));
|
||||||
|
|||||||
Reference in New Issue
Block a user