mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Add database methods for converting a pending contact.
This commit is contained in:
@@ -763,16 +763,25 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the pending contact is in the DB (which it's not)
|
||||
exactly(2).of(database).startTransaction();
|
||||
exactly(3).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(2).of(database).containsPendingContact(txn,
|
||||
exactly(3).of(database).containsPendingContact(txn,
|
||||
pendingContactId);
|
||||
will(returnValue(false));
|
||||
exactly(2).of(database).abortTransaction(txn);
|
||||
exactly(3).of(database).abortTransaction(txn);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addContact(transaction, pendingContactId, author,
|
||||
localAuthor.getId(), true));
|
||||
fail();
|
||||
} catch (NoSuchPendingContactException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addTransportKeys(transaction, pendingContactId,
|
||||
@@ -1494,6 +1503,69 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCannotAddLocalIdentityAsContactFromPendingContact()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsPendingContact(txn, pendingContactId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsIdentity(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
// Contact is a local identity
|
||||
oneOf(database).containsIdentity(txn, author.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).abortTransaction(txn);
|
||||
}});
|
||||
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addContact(transaction, pendingContactId, author,
|
||||
localAuthor.getId(), true));
|
||||
fail();
|
||||
} catch (ContactExistsException expected) {
|
||||
assertEquals(localAuthor.getId(), expected.getLocalAuthorId());
|
||||
assertEquals(author, expected.getRemoteAuthor());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCannotAddDuplicateContactFromPendingContact()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsPendingContact(txn, pendingContactId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsIdentity(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsIdentity(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
// Contact already exists for this local identity
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).abortTransaction(txn);
|
||||
}});
|
||||
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.addContact(transaction, pendingContactId, author,
|
||||
localAuthor.getId(), true));
|
||||
fail();
|
||||
} catch (ContactExistsException expected) {
|
||||
assertEquals(localAuthor.getId(), expected.getLocalAuthorId());
|
||||
assertEquals(author, expected.getRemoteAuthor());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageDependencies() throws Exception {
|
||||
int shutdownHandle = 12345;
|
||||
|
||||
@@ -2204,12 +2204,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(emptyList(), db.getPendingContacts(txn));
|
||||
|
||||
db.addPendingContact(txn, pendingContact);
|
||||
Collection<PendingContact> pendingContacts =
|
||||
db.getPendingContacts(txn);
|
||||
Collection<PendingContact> pendingContacts = db.getPendingContacts(txn);
|
||||
assertEquals(1, pendingContacts.size());
|
||||
PendingContact retrieved = pendingContacts.iterator().next();
|
||||
assertEquals(pendingContact.getId(), retrieved.getId());
|
||||
assertEquals(pendingContact.getAlias(), retrieved.getAlias());
|
||||
assertArrayEquals(pendingContact.getPublicKey().getEncoded(),
|
||||
retrieved.getPublicKey().getEncoded());
|
||||
assertEquals(pendingContact.getTimestamp(), retrieved.getTimestamp());
|
||||
|
||||
db.removePendingContact(txn, pendingContact.getId());
|
||||
@@ -2219,6 +2220,60 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransferKeys() throws Exception {
|
||||
boolean alice = random.nextBoolean();
|
||||
TransportKeys transportKeys =
|
||||
createHandshakeKeys(1000, getSecretKey(), alice);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add the pending contact, the transport and the handshake keys
|
||||
db.addPendingContact(txn, pendingContact);
|
||||
db.addTransport(txn, transportId, 123);
|
||||
assertEquals(keySetId, db.addTransportKeys(txn, pendingContact.getId(),
|
||||
transportKeys));
|
||||
|
||||
Collection<TransportKeySet> allKeys =
|
||||
db.getTransportKeys(txn, transportId);
|
||||
assertEquals(1, allKeys.size());
|
||||
TransportKeySet ks = allKeys.iterator().next();
|
||||
assertEquals(keySetId, ks.getKeySetId());
|
||||
assertNull(ks.getContactId());
|
||||
assertEquals(pendingContact.getId(), ks.getPendingContactId());
|
||||
|
||||
// Add a contact
|
||||
db.addIdentity(txn, identity);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), true));
|
||||
|
||||
// The contact shouldn't have a handshake public key
|
||||
Contact contact = db.getContact(txn, contactId);
|
||||
assertNull(contact.getHandshakePublicKey());
|
||||
|
||||
// Transfer the keys to the contact
|
||||
db.transferKeys(txn, pendingContact.getId(), contactId);
|
||||
|
||||
// The handshake public key should have been copied to the contact
|
||||
contact = db.getContact(txn, contactId);
|
||||
PublicKey handshakePublicKey = contact.getHandshakePublicKey();
|
||||
assertNotNull(handshakePublicKey);
|
||||
assertArrayEquals(pendingContact.getPublicKey().getEncoded(),
|
||||
handshakePublicKey.getEncoded());
|
||||
|
||||
// The transport keys should have been transferred to the contact
|
||||
allKeys = db.getTransportKeys(txn, transportId);
|
||||
assertEquals(1, allKeys.size());
|
||||
ks = allKeys.iterator().next();
|
||||
assertEquals(keySetId, ks.getKeySetId());
|
||||
assertEquals(contactId, ks.getContactId());
|
||||
assertNull(ks.getPendingContactId());
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHandshakeKeyPair() throws Exception {
|
||||
Identity withoutKeys = new Identity(localAuthor, null, null,
|
||||
|
||||
Reference in New Issue
Block a user