mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Merge branch '2038-db-methods' into '1802-sync-via-removable-storage'
New DB methods to support transport key agreement client See merge request briar/briar!1472
This commit is contained in:
@@ -150,6 +150,16 @@ public interface DatabaseComponent extends TransactionManager {
|
|||||||
boolean containsPendingContact(Transaction txn, PendingContactId p)
|
boolean containsPendingContact(Transaction txn, PendingContactId p)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the database contains keys for communicating with the
|
||||||
|
* given contact over the given transport. Handshake mode and rotation mode
|
||||||
|
* keys are included, whether activated or not.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
boolean containsTransportKeys(Transaction txn, ContactId c, TransportId t)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the message with the given ID. Unlike
|
* Deletes the message with the given ID. Unlike
|
||||||
* {@link #removeMessage(Transaction, MessageId)}, the message ID,
|
* {@link #removeMessage(Transaction, MessageId)}, the message ID,
|
||||||
@@ -493,6 +503,16 @@ public interface DatabaseComponent extends TransactionManager {
|
|||||||
Collection<TransportKeySet> getTransportKeys(Transaction txn, TransportId t)
|
Collection<TransportKeySet> getTransportKeys(Transaction txn, TransportId t)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the contact IDs and transport IDs for which the DB contains
|
||||||
|
* at least one set of transport keys. Handshake mode and rotation mode
|
||||||
|
* keys are included, whether activated or not.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Map<ContactId, Collection<TransportId>> getTransportsWithKeys(
|
||||||
|
Transaction txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increments the outgoing stream counter for the given transport keys.
|
* Increments the outgoing stream counter for the given transport keys.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -215,6 +215,16 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
boolean containsTransport(T txn, TransportId t) throws DbException;
|
boolean containsTransport(T txn, TransportId t) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the database contains keys for communicating with the
|
||||||
|
* given contact over the given transport. Handshake mode and rotation mode
|
||||||
|
* keys are included, whether activated or not.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
boolean containsTransportKeys(T txn, ContactId c, TransportId t)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the database contains the given message, the message is
|
* Returns true if the database contains the given message, the message is
|
||||||
* shared, and the visibility of the message's group to the given contact
|
* shared, and the visibility of the message's group to the given contact
|
||||||
@@ -590,6 +600,16 @@ interface Database<T> {
|
|||||||
Collection<TransportKeySet> getTransportKeys(T txn, TransportId t)
|
Collection<TransportKeySet> getTransportKeys(T txn, TransportId t)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the contact IDs and transport IDs for which the DB contains
|
||||||
|
* at least one set of transport keys. Handshake mode and rotation mode
|
||||||
|
* keys are included, whether activated or not.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
Map<ContactId, Collection<TransportId>> getTransportsWithKeys(T txn)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increments the outgoing stream counter for the given transport keys.
|
* Increments the outgoing stream counter for the given transport keys.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -371,6 +371,13 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.containsPendingContact(txn, p);
|
return db.containsPendingContact(txn, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsTransportKeys(Transaction transaction, ContactId c,
|
||||||
|
TransportId t) throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
return db.containsTransportKeys(txn, c, t);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteMessage(Transaction transaction, MessageId m)
|
public void deleteMessage(Transaction transaction, MessageId m)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -780,6 +787,13 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.getTransportKeys(txn, t);
|
return db.getTransportKeys(txn, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<ContactId, Collection<TransportId>> getTransportsWithKeys(
|
||||||
|
Transaction transaction) throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
return db.getTransportsWithKeys(txn);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void incrementStreamCounter(Transaction transaction, TransportId t,
|
public void incrementStreamCounter(Transaction transaction, TransportId t,
|
||||||
KeySetId k) throws DbException {
|
KeySetId k) throws DbException {
|
||||||
|
|||||||
@@ -1277,6 +1277,29 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsTransportKeys(Connection txn, ContactId c,
|
||||||
|
TransportId t) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT NULL FROM outgoingKeys"
|
||||||
|
+ " WHERE contactId = ? AND transportId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
ps.setString(2, t.getString());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
boolean found = rs.next();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return found;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsVisibleMessage(Connection txn, ContactId c,
|
public boolean containsVisibleMessage(Connection txn, ContactId c,
|
||||||
MessageId m) throws DbException {
|
MessageId m) throws DbException {
|
||||||
@@ -2605,6 +2628,38 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<ContactId, Collection<TransportId>> getTransportsWithKeys(
|
||||||
|
Connection txn) throws DbException {
|
||||||
|
Statement s = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT DISTINCT contactId, transportId"
|
||||||
|
+ " FROM outgoingKeys";
|
||||||
|
s = txn.createStatement();
|
||||||
|
rs = s.executeQuery(sql);
|
||||||
|
Map<ContactId, Collection<TransportId>> ids = new HashMap<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
ContactId c = new ContactId(rs.getInt(1));
|
||||||
|
TransportId t = new TransportId(rs.getString(2));
|
||||||
|
Collection<TransportId> transportIds = ids.get(c);
|
||||||
|
if (transportIds == null) {
|
||||||
|
transportIds = new ArrayList<>();
|
||||||
|
ids.put(c, transportIds);
|
||||||
|
}
|
||||||
|
transportIds.add(t);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
s.close();
|
||||||
|
return ids;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(s, LOG, WARNING);
|
||||||
|
tryToClose(s, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void incrementStreamCounter(Connection txn, TransportId t,
|
public void incrementStreamCounter(Connection txn, TransportId t,
|
||||||
KeySetId k) throws DbException {
|
KeySetId k) throws DbException {
|
||||||
|
|||||||
@@ -698,7 +698,9 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Initially there should be no transport keys in the database
|
// Initially there should be no transport keys in the database
|
||||||
|
assertFalse(db.containsTransportKeys(txn, contactId, transportId));
|
||||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||||
|
assertTrue(db.getTransportsWithKeys(txn).isEmpty());
|
||||||
|
|
||||||
// Add the contact, the transport and the transport keys
|
// Add the contact, the transport and the transport keys
|
||||||
db.addIdentity(txn, identity);
|
db.addIdentity(txn, identity);
|
||||||
@@ -709,6 +711,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
assertEquals(keySetId1, db.addTransportKeys(txn, contactId, keys1));
|
assertEquals(keySetId1, db.addTransportKeys(txn, contactId, keys1));
|
||||||
|
|
||||||
// Retrieve the transport keys
|
// Retrieve the transport keys
|
||||||
|
assertTrue(db.containsTransportKeys(txn, contactId, transportId));
|
||||||
Collection<TransportKeySet> allKeys =
|
Collection<TransportKeySet> allKeys =
|
||||||
db.getTransportKeys(txn, transportId);
|
db.getTransportKeys(txn, transportId);
|
||||||
assertEquals(2, allKeys.size());
|
assertEquals(2, allKeys.size());
|
||||||
@@ -721,6 +724,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
assertKeysEquals(keys1, ks.getKeys());
|
assertKeysEquals(keys1, ks.getKeys());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals(singletonMap(contactId, singletonList(transportId)),
|
||||||
|
db.getTransportsWithKeys(txn));
|
||||||
|
|
||||||
// Update the transport keys
|
// Update the transport keys
|
||||||
TransportKeys updated = createTransportKeys(timePeriod + 1, active);
|
TransportKeys updated = createTransportKeys(timePeriod + 1, active);
|
||||||
@@ -732,6 +737,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
null, updated1));
|
null, updated1));
|
||||||
|
|
||||||
// Retrieve the transport keys again
|
// Retrieve the transport keys again
|
||||||
|
assertTrue(db.containsTransportKeys(txn, contactId, transportId));
|
||||||
allKeys = db.getTransportKeys(txn, transportId);
|
allKeys = db.getTransportKeys(txn, transportId);
|
||||||
assertEquals(2, allKeys.size());
|
assertEquals(2, allKeys.size());
|
||||||
for (TransportKeySet ks : allKeys) {
|
for (TransportKeySet ks : allKeys) {
|
||||||
@@ -743,10 +749,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
assertKeysEquals(updated1, ks.getKeys());
|
assertKeysEquals(updated1, ks.getKeys());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals(singletonMap(contactId, singletonList(transportId)),
|
||||||
|
db.getTransportsWithKeys(txn));
|
||||||
|
|
||||||
// Removing the contact should remove the transport keys
|
// Removing the contact should remove the transport keys
|
||||||
db.removeContact(txn, contactId);
|
db.removeContact(txn, contactId);
|
||||||
|
assertFalse(db.containsTransportKeys(txn, contactId, transportId));
|
||||||
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
assertEquals(emptyList(), db.getTransportKeys(txn, transportId));
|
||||||
|
assertTrue(db.getTransportsWithKeys(txn).isEmpty());
|
||||||
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
db.close();
|
db.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user