Add key set and key set ID classes for static keys.

This commit is contained in:
akwizgran
2019-04-16 16:56:50 +01:00
parent 7acbe56197
commit 0f5f440f1c
23 changed files with 313 additions and 184 deletions

View File

@@ -45,9 +45,9 @@ import org.briarproject.bramble.api.sync.event.MessageToRequestEvent;
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.KeySet;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.CaptureArgumentAction;
@@ -111,7 +111,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
private final int maxLatency;
private final ContactId contactId;
private final Contact contact;
private final KeySetId keySetId;
private final TransportKeySetId keySetId;
public DatabaseComponentImplTest() {
clientId = getClientId();
@@ -132,7 +132,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
contactId = new ContactId(234);
contact = new Contact(contactId, author, localAuthor.getId(), alias,
true, true);
keySetId = new KeySetId(345);
keySetId = new TransportKeySetId(345);
}
private DatabaseComponent createDatabaseComponent(Database<Object> database,
@@ -1117,8 +1117,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
@Test
public void testTransportKeys() throws Exception {
TransportKeys transportKeys = createTransportKeys();
KeySet ks = new KeySet(keySetId, contactId, transportKeys);
Collection<KeySet> keys = singletonList(ks);
TransportKeySet ks =
new TransportKeySet(keySetId, contactId, transportKeys);
Collection<TransportKeySet> keys = singletonList(ks);
context.checking(new Expectations() {{
// startTransaction()

View File

@@ -21,9 +21,9 @@ import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.KeySet;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
import org.briarproject.bramble.system.SystemClock;
import org.briarproject.bramble.test.BrambleTestCase;
@@ -103,7 +103,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
private final MessageId messageId;
private final TransportId transportId;
private final ContactId contactId;
private final KeySetId keySetId, keySetId1;
private final TransportKeySetId keySetId, keySetId1;
private final Random random = new Random();
JdbcDatabaseTest() {
@@ -117,8 +117,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
messageId = message.getId();
transportId = getTransportId();
contactId = new ContactId(1);
keySetId = new KeySetId(1);
keySetId1 = new KeySetId(2);
keySetId = new TransportKeySetId(1);
keySetId1 = new TransportKeySetId(2);
}
protected abstract JdbcDatabase createDatabase(DatabaseConfig config,
@@ -673,15 +673,16 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
assertEquals(keySetId1, db.addTransportKeys(txn, contactId, keys1));
// Retrieve the transport keys
Collection<KeySet> allKeys = db.getTransportKeys(txn, transportId);
Collection<TransportKeySet> allKeys =
db.getTransportKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (KeySet ks : allKeys) {
for (TransportKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId());
if (ks.getKeySetId().equals(keySetId)) {
assertKeysEquals(keys, ks.getTransportKeys());
assertKeysEquals(keys, ks.getKeys());
} else {
assertEquals(keySetId1, ks.getKeySetId());
assertKeysEquals(keys1, ks.getTransportKeys());
assertKeysEquals(keys1, ks.getKeys());
}
}
@@ -689,19 +690,21 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
TransportKeys rotated = createTransportKeys(timePeriod + 1, active);
TransportKeys rotated1 =
createTransportKeys(timePeriod1 + 1, active);
db.updateTransportKeys(txn, new KeySet(keySetId, contactId, rotated));
db.updateTransportKeys(txn, new KeySet(keySetId1, contactId, rotated1));
db.updateTransportKeys(txn, new TransportKeySet(keySetId, contactId,
rotated));
db.updateTransportKeys(txn, new TransportKeySet(keySetId1, contactId,
rotated1));
// Retrieve the transport keys again
allKeys = db.getTransportKeys(txn, transportId);
assertEquals(2, allKeys.size());
for (KeySet ks : allKeys) {
for (TransportKeySet ks : allKeys) {
assertEquals(contactId, ks.getContactId());
if (ks.getKeySetId().equals(keySetId)) {
assertKeysEquals(rotated, ks.getTransportKeys());
assertKeysEquals(rotated, ks.getKeys());
} else {
assertEquals(keySetId1, ks.getKeySetId());
assertKeysEquals(rotated1, ks.getTransportKeys());
assertKeysEquals(rotated1, ks.getKeys());
}
}
@@ -766,12 +769,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
// Increment the stream counter twice and retrieve the transport keys
db.incrementStreamCounter(txn, transportId, keySetId);
db.incrementStreamCounter(txn, transportId, keySetId);
Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId);
Collection<TransportKeySet> newKeys =
db.getTransportKeys(txn, transportId);
assertEquals(1, newKeys.size());
KeySet ks = newKeys.iterator().next();
TransportKeySet ks = newKeys.iterator().next();
assertEquals(keySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId());
TransportKeys k = ks.getTransportKeys();
TransportKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId());
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
assertEquals(timePeriod, outCurr.getTimePeriod());
@@ -810,12 +814,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
random.nextBytes(bitmap);
db.setReorderingWindow(txn, keySetId, transportId, timePeriod,
base + 1, bitmap);
Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId);
Collection<TransportKeySet> newKeys =
db.getTransportKeys(txn, transportId);
assertEquals(1, newKeys.size());
KeySet ks = newKeys.iterator().next();
TransportKeySet ks = newKeys.iterator().next();
assertEquals(keySetId, ks.getKeySetId());
assertEquals(contactId, ks.getContactId());
TransportKeys k = ks.getTransportKeys();
TransportKeys k = ks.getKeys();
assertEquals(transportId, k.getTransportId());
IncomingKeys inCurr = k.getCurrentIncomingKeys();
assertEquals(timePeriod, inCurr.getTimePeriod());

View File

@@ -12,8 +12,8 @@ import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.StreamContext;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations;
import org.jmock.Expectations;
@@ -51,7 +51,7 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
private final Transaction txn = new Transaction(null, false);
private final ContactId contactId = new ContactId(123);
private final ContactId inactiveContactId = new ContactId(234);
private final KeySetId keySetId = new KeySetId(345);
private final TransportKeySetId keySetId = new TransportKeySetId(345);
private final TransportId transportId = getTransportId();
private final TransportId unknownTransportId = getTransportId();
private final StreamContext streamContext =
@@ -113,8 +113,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
will(returnValue(keySetId));
}});
Map<TransportId, KeySetId> ids = keyManager.addContact(txn, contactId,
secretKey, timestamp, alice, active);
Map<TransportId, TransportKeySetId> ids = keyManager.addContact(txn,
contactId, secretKey, timestamp, alice, active);
assertEquals(singletonMap(transportId, keySetId), ids);
}

View File

@@ -8,10 +8,10 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.KeySet;
import org.briarproject.bramble.api.transport.KeySetId;
import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.StreamContext;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations;
@@ -60,8 +60,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
private final long timePeriodLength = maxLatency + MAX_CLOCK_DIFFERENCE;
private final ContactId contactId = new ContactId(123);
private final ContactId contactId1 = new ContactId(234);
private final KeySetId keySetId = new KeySetId(345);
private final KeySetId keySetId1 = new KeySetId(456);
private final TransportKeySetId keySetId = new TransportKeySetId(345);
private final TransportKeySetId keySetId1 = new TransportKeySetId(456);
private final SecretKey tagKey = TestUtils.getSecretKey();
private final SecretKey headerKey = TestUtils.getSecretKey();
private final SecretKey rootKey = TestUtils.getSecretKey();
@@ -71,9 +71,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
public void testKeysAreRotatedAtStartup() throws Exception {
TransportKeys shouldRotate = createTransportKeys(900, 0, true);
TransportKeys shouldNotRotate = createTransportKeys(1000, 0, true);
Collection<KeySet> loaded = asList(
new KeySet(keySetId, contactId, shouldRotate),
new KeySet(keySetId1, contactId1, shouldNotRotate)
Collection<TransportKeySet> loaded = asList(
new TransportKeySet(keySetId, contactId, shouldRotate),
new TransportKeySet(keySetId1, contactId1, shouldNotRotate)
);
TransportKeys rotated = createTransportKeys(1000, 0, true);
Transaction txn = new Transaction(null, false);
@@ -98,8 +98,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
will(new EncodeTagAction());
}
// Save the keys that were rotated
oneOf(db).updateTransportKeys(txn,
singletonList(new KeySet(keySetId, contactId, rotated)));
oneOf(db).updateTransportKeys(txn, singletonList(
new TransportKeySet(keySetId, contactId, rotated)));
// Schedule key rotation at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)),
with(timePeriodLength - 1), with(MILLISECONDS));
@@ -313,8 +313,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
@Test
public void testKeysAreRotatedToCurrentPeriod() throws Exception {
TransportKeys transportKeys = createTransportKeys(1000, 0, true);
Collection<KeySet> loaded =
singletonList(new KeySet(keySetId, contactId, transportKeys));
Collection<TransportKeySet> loaded = singletonList(
new TransportKeySet(keySetId, contactId, transportKeys));
TransportKeys rotated = createTransportKeys(1001, 0, true);
Transaction txn = new Transaction(null, false);
Transaction txn1 = new Transaction(null, false);
@@ -359,8 +359,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
will(new EncodeTagAction());
}
// Save the keys that were rotated
oneOf(db).updateTransportKeys(txn1,
singletonList(new KeySet(keySetId, contactId, rotated)));
oneOf(db).updateTransportKeys(txn1, singletonList(
new TransportKeySet(keySetId, contactId, rotated)));
// Schedule key rotation at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)),
with(timePeriodLength), with(MILLISECONDS));