Merge branch 'add-contact-transaction' into 'master'

Add contact and transport keys in the same transaction

This avoids a potential problem where the app crashes after adding the contact but before adding the transport keys, leaving the contact unusable.

See merge request !112
This commit is contained in:
Torsten Grote
2016-02-29 14:07:33 +00:00
13 changed files with 50 additions and 91 deletions

View File

@@ -31,6 +31,7 @@ import org.briarproject.event.EventModule;
import org.briarproject.forum.ForumModule;
import org.briarproject.identity.IdentityModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.transport.TransportModule;
import org.junit.Test;
import java.util.Random;
@@ -57,7 +58,8 @@ public class ConstantsTest extends BriarTestCase {
new TestLifecycleModule(), new TestSystemModule(),
new ContactModule(), new CryptoModule(), new DatabaseModule(),
new DataModule(), new EventModule(), new ForumModule(),
new IdentityModule(), new MessagingModule(), new SyncModule());
new IdentityModule(), new MessagingModule(), new SyncModule(),
new TransportModule());
crypto = i.getInstance(CryptoComponent.class);
authorFactory = i.getInstance(AuthorFactory.class);
privateMessageFactory = i.getInstance(PrivateMessageFactory.class);

View File

@@ -135,9 +135,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
Author bobAuthor = new Author(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = contactManager.addContact(bobAuthor, aliceId,
true);
// Derive and store the transport keys
keyManager.addContact(contactId, master, timestamp, true);
master, timestamp, true, true);
// Send Bob a message
GroupId groupId = messagingManager.getConversationId(contactId);
@@ -206,9 +204,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
Author aliceAuthor = new Author(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = contactManager.addContact(aliceAuthor, bobId,
true);
// Derive and store the transport keys
keyManager.addContact(contactId, master, timestamp, false);
master, timestamp, false, true);
// Set up an event listener
MessageListener listener = new MessageListener();

View File

@@ -133,17 +133,15 @@ public class TransportKeyManagerTest extends BriarTestCase {
will(new EncodeTagAction());
}
// Save the keys
oneOf(db).startTransaction();
will(returnValue(txn));
oneOf(db).addTransportKeys(txn, contactId, rotated);
oneOf(db).endTransaction(txn);
}});
TransportKeyManager transportKeyManager = new TransportKeyManager(db,
crypto, timer, clock, transportId, maxLatency);
// The timestamp is 1 ms before the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000 - 1;
transportKeyManager.addContact(contactId, masterKey, timestamp, alice);
transportKeyManager.addContact(txn, contactId, masterKey, timestamp,
alice);
context.assertIsSatisfied();
}
@@ -194,17 +192,15 @@ public class TransportKeyManagerTest extends BriarTestCase {
oneOf(crypto).rotateTransportKeys(transportKeys, 1000);
will(returnValue(transportKeys));
// Save the keys
oneOf(db).startTransaction();
will(returnValue(txn));
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
oneOf(db).endTransaction(txn);
}});
TransportKeyManager transportKeyManager = new TransportKeyManager(db,
crypto, timer, clock, transportId, maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
transportKeyManager.addContact(contactId, masterKey, timestamp, alice);
transportKeyManager.addContact(txn, contactId, masterKey, timestamp,
alice);
assertNull(transportKeyManager.getStreamContext(contactId));
context.assertIsSatisfied();
@@ -240,10 +236,7 @@ public class TransportKeyManagerTest extends BriarTestCase {
oneOf(crypto).rotateTransportKeys(transportKeys, 1000);
will(returnValue(transportKeys));
// Save the keys
oneOf(db).startTransaction();
will(returnValue(txn));
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
oneOf(db).endTransaction(txn);
// Increment the stream counter
oneOf(db).startTransaction();
will(returnValue(txn1));
@@ -256,7 +249,8 @@ public class TransportKeyManagerTest extends BriarTestCase {
crypto, timer, clock, transportId, maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
transportKeyManager.addContact(contactId, masterKey, timestamp, alice);
transportKeyManager.addContact(txn, contactId, masterKey, timestamp,
alice);
// The first request should return a stream context
StreamContext ctx = transportKeyManager.getStreamContext(contactId);
assertNotNull(ctx);
@@ -299,17 +293,15 @@ public class TransportKeyManagerTest extends BriarTestCase {
oneOf(crypto).rotateTransportKeys(transportKeys, 1000);
will(returnValue(transportKeys));
// Save the keys
oneOf(db).startTransaction();
will(returnValue(txn));
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
oneOf(db).endTransaction(txn);
}});
TransportKeyManager transportKeyManager = new TransportKeyManager(db,
crypto, timer, clock, transportId, maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
transportKeyManager.addContact(contactId, masterKey, timestamp, alice);
transportKeyManager.addContact(txn, contactId, masterKey, timestamp,
alice);
assertNull(transportKeyManager.getStreamContext(new byte[TAG_LENGTH]));
context.assertIsSatisfied();
@@ -345,10 +337,7 @@ public class TransportKeyManagerTest extends BriarTestCase {
oneOf(crypto).rotateTransportKeys(transportKeys, 1000);
will(returnValue(transportKeys));
// Save the keys
oneOf(db).startTransaction();
will(returnValue(txn));
oneOf(db).addTransportKeys(txn, contactId, transportKeys);
oneOf(db).endTransaction(txn);
// Encode a new tag after sliding the window
oneOf(crypto).encodeTag(with(any(byte[].class)),
with(tagKey), with((long) REORDERING_WINDOW_SIZE));
@@ -365,7 +354,8 @@ public class TransportKeyManagerTest extends BriarTestCase {
crypto, timer, clock, transportId, maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
transportKeyManager.addContact(contactId, masterKey, timestamp, alice);
transportKeyManager.addContact(txn, contactId, masterKey, timestamp,
alice);
// Use the first tag (previous rotation period, stream number 0)
assertEquals(REORDERING_WINDOW_SIZE * 3, tags.size());
byte[] tag = tags.get(0);