Create DB tables for static keys.

This commit is contained in:
akwizgran
2019-04-09 14:48:45 +01:00
parent ff2f710495
commit b568405f59
17 changed files with 324 additions and 196 deletions

View File

@@ -40,7 +40,7 @@ public class KeyDerivationTest extends BrambleTestCase {
@Test
public void testCurrentKeysMatchCurrentKeysOfContact() {
// Start in rotation period 123
// Start in time period 123
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
master, 123, true, true);
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
@@ -72,7 +72,7 @@ public class KeyDerivationTest extends BrambleTestCase {
@Test
public void testPreviousKeysMatchPreviousKeysOfContact() {
// Start in rotation period 123
// Start in time period 123
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
master, 123, true, true);
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,
@@ -99,7 +99,7 @@ public class KeyDerivationTest extends BrambleTestCase {
@Test
public void testNextKeysMatchNextKeysOfContact() {
// Start in rotation period 123
// Start in time period 123
TransportKeys kA = transportCrypto.deriveTransportKeys(transportId,
master, 123, true, true);
TransportKeys kB = transportCrypto.deriveTransportKeys(transportId,

View File

@@ -653,10 +653,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testTransportKeys() throws Exception {
long rotationPeriod = 123, rotationPeriod1 = 234;
long timePeriod = 123, timePeriod1 = 234;
boolean active = random.nextBoolean();
TransportKeys keys = createTransportKeys(rotationPeriod, active);
TransportKeys keys1 = createTransportKeys(rotationPeriod1, active);
TransportKeys keys = createTransportKeys(timePeriod, active);
TransportKeys keys1 = createTransportKeys(timePeriod1, active);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -686,9 +686,9 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
}
// Rotate the transport keys
TransportKeys rotated = createTransportKeys(rotationPeriod + 1, active);
TransportKeys rotated = createTransportKeys(timePeriod + 1, active);
TransportKeys rotated1 =
createTransportKeys(rotationPeriod1 + 1, active);
createTransportKeys(timePeriod1 + 1, active);
db.updateTransportKeys(txn, new KeySet(keySetId, contactId, rotated));
db.updateTransportKeys(txn, new KeySet(keySetId1, contactId, rotated1));
@@ -716,7 +716,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
private void assertKeysEquals(TransportKeys expected,
TransportKeys actual) {
assertEquals(expected.getTransportId(), actual.getTransportId());
assertEquals(expected.getRotationPeriod(), actual.getRotationPeriod());
assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
assertKeysEquals(expected.getPreviousIncomingKeys(),
actual.getPreviousIncomingKeys());
assertKeysEquals(expected.getCurrentIncomingKeys(),
@@ -732,7 +732,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
actual.getTagKey().getBytes());
assertArrayEquals(expected.getHeaderKey().getBytes(),
actual.getHeaderKey().getBytes());
assertEquals(expected.getRotationPeriod(), actual.getRotationPeriod());
assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
assertEquals(expected.getWindowBase(), actual.getWindowBase());
assertArrayEquals(expected.getWindowBitmap(), actual.getWindowBitmap());
}
@@ -742,15 +742,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
actual.getTagKey().getBytes());
assertArrayEquals(expected.getHeaderKey().getBytes(),
actual.getHeaderKey().getBytes());
assertEquals(expected.getRotationPeriod(), actual.getRotationPeriod());
assertEquals(expected.getTimePeriod(), actual.getTimePeriod());
assertEquals(expected.getStreamCounter(), actual.getStreamCounter());
assertEquals(expected.isActive(), actual.isActive());
}
@Test
public void testIncrementStreamCounter() throws Exception {
long rotationPeriod = 123;
TransportKeys keys = createTransportKeys(rotationPeriod, true);
long timePeriod = 123;
TransportKeys keys = createTransportKeys(timePeriod, true);
long streamCounter = keys.getCurrentOutgoingKeys().getStreamCounter();
Database<Connection> db = open(false);
@@ -774,7 +774,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
TransportKeys k = ks.getTransportKeys();
assertEquals(transportId, k.getTransportId());
OutgoingKeys outCurr = k.getCurrentOutgoingKeys();
assertEquals(rotationPeriod, outCurr.getRotationPeriod());
assertEquals(timePeriod, outCurr.getTimePeriod());
assertEquals(streamCounter + 2, outCurr.getStreamCounter());
// The rest of the keys should be unaffected
@@ -791,8 +791,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
@Test
public void testSetReorderingWindow() throws Exception {
boolean active = random.nextBoolean();
long rotationPeriod = 123;
TransportKeys keys = createTransportKeys(rotationPeriod, active);
long timePeriod = 123;
TransportKeys keys = createTransportKeys(timePeriod, active);
long base = keys.getCurrentIncomingKeys().getWindowBase();
byte[] bitmap = keys.getCurrentIncomingKeys().getWindowBitmap();
@@ -808,7 +808,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
// Update the reordering window and retrieve the transport keys
random.nextBytes(bitmap);
db.setReorderingWindow(txn, keySetId, transportId, rotationPeriod,
db.setReorderingWindow(txn, keySetId, transportId, timePeriod,
base + 1, bitmap);
Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId);
assertEquals(1, newKeys.size());
@@ -818,7 +818,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
TransportKeys k = ks.getTransportKeys();
assertEquals(transportId, k.getTransportId());
IncomingKeys inCurr = k.getCurrentIncomingKeys();
assertEquals(rotationPeriod, inCurr.getRotationPeriod());
assertEquals(timePeriod, inCurr.getTimePeriod());
assertEquals(base + 1, inCurr.getWindowBase());
assertArrayEquals(bitmap, inCurr.getWindowBitmap());
@@ -1978,24 +1978,23 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
return db;
}
private TransportKeys createTransportKeys(long rotationPeriod,
boolean active) {
private TransportKeys createTransportKeys(long timePeriod, boolean active) {
SecretKey inPrevTagKey = getSecretKey();
SecretKey inPrevHeaderKey = getSecretKey();
IncomingKeys inPrev = new IncomingKeys(inPrevTagKey, inPrevHeaderKey,
rotationPeriod - 1, 123, new byte[4]);
timePeriod - 1, 123, new byte[4]);
SecretKey inCurrTagKey = getSecretKey();
SecretKey inCurrHeaderKey = getSecretKey();
IncomingKeys inCurr = new IncomingKeys(inCurrTagKey, inCurrHeaderKey,
rotationPeriod, 234, new byte[4]);
timePeriod, 234, new byte[4]);
SecretKey inNextTagKey = getSecretKey();
SecretKey inNextHeaderKey = getSecretKey();
IncomingKeys inNext = new IncomingKeys(inNextTagKey, inNextHeaderKey,
rotationPeriod + 1, 345, new byte[4]);
timePeriod + 1, 345, new byte[4]);
SecretKey outCurrTagKey = getSecretKey();
SecretKey outCurrHeaderKey = getSecretKey();
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
rotationPeriod, 456, active);
timePeriod, 456, active);
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
}

View File

@@ -57,7 +57,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
private final TransportId transportId = getTransportId();
private final long maxLatency = 30 * 1000; // 30 seconds
private final long rotationPeriodLength = maxLatency + MAX_CLOCK_DIFFERENCE;
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);
@@ -79,9 +79,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
Transaction txn = new Transaction(null, false);
context.checking(new Expectations() {{
// Get the current time (1 ms after start of rotation period 1000)
// Get the current time (1 ms after start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000 + 1));
will(returnValue(timePeriodLength * 1000 + 1));
// Load the transport keys
oneOf(db).getTransportKeys(txn, transportId);
will(returnValue(loaded));
@@ -100,9 +100,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
// Save the keys that were rotated
oneOf(db).updateTransportKeys(txn,
singletonList(new KeySet(keySetId, contactId, rotated)));
// Schedule key rotation at the start of the next rotation period
// Schedule key rotation at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)),
with(rotationPeriodLength - 1), with(MILLISECONDS));
with(timePeriodLength - 1), with(MILLISECONDS));
}});
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
@@ -123,9 +123,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
oneOf(transportCrypto).deriveTransportKeys(transportId, masterKey,
999, alice, true);
will(returnValue(transportKeys));
// Get the current time (1 ms after start of rotation period 1000)
// Get the current time (1 ms after start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000 + 1));
will(returnValue(timePeriodLength * 1000 + 1));
// Rotate the transport keys
oneOf(transportCrypto).rotateTransportKeys(transportKeys, 1000);
will(returnValue(rotated));
@@ -144,8 +144,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is 1 ms before the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000 - 1;
// The timestamp is 1 ms before the start of time period 1000
long timestamp = timePeriodLength * 1000 - 1;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, true));
assertTrue(transportKeyManager.canSendOutgoingStreams(contactId));
@@ -177,8 +177,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, true));
assertFalse(transportKeyManager.canSendOutgoingStreams(contactId));
@@ -203,8 +203,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, true));
// The first request should return a stream context
@@ -235,8 +235,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, active));
assertEquals(active,
@@ -259,9 +259,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
oneOf(transportCrypto).deriveTransportKeys(transportId, masterKey,
1000, alice, true);
will(returnValue(transportKeys));
// Get the current time (the start of rotation period 1000)
// Get the current time (the start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000));
will(returnValue(timePeriodLength * 1000));
// Encode the tags (3 sets)
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
exactly(3).of(transportCrypto).encodeTag(
@@ -280,7 +280,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
with(tagKey), with(PROTOCOL_VERSION),
with((long) REORDERING_WINDOW_SIZE));
will(new EncodeTagAction(tags));
// Save the reordering window (previous rotation period, base 1)
// Save the reordering window (previous time period, base 1)
oneOf(db).setReorderingWindow(txn, keySetId, transportId, 999,
1, new byte[REORDERING_WINDOW_SIZE / 8]);
}});
@@ -288,12 +288,12 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, true));
assertTrue(transportKeyManager.canSendOutgoingStreams(contactId));
// Use the first tag (previous rotation period, stream number 0)
// Use the first tag (previous time period, stream number 0)
assertEquals(REORDERING_WINDOW_SIZE * 3, tags.size());
byte[] tag = tags.get(0);
// The first request should return a stream context
@@ -320,9 +320,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
Transaction txn1 = new Transaction(null, false);
context.checking(new DbExpectations() {{
// Get the current time (the start of rotation period 1000)
// Get the current time (the start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000));
will(returnValue(timePeriodLength * 1000));
// Load the transport keys
oneOf(db).getTransportKeys(txn, transportId);
will(returnValue(loaded));
@@ -336,17 +336,17 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
with(PROTOCOL_VERSION), with(i));
will(new EncodeTagAction());
}
// Schedule key rotation at the start of the next rotation period
// Schedule key rotation at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)),
with(rotationPeriodLength), with(MILLISECONDS));
with(timePeriodLength), with(MILLISECONDS));
will(new RunAction());
oneOf(dbExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
// Start a transaction for key rotation
oneOf(db).transaction(with(false), withDbRunnable(txn1));
// Get the current time (the start of rotation period 1001)
// Get the current time (the start of time period 1001)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1001));
will(returnValue(timePeriodLength * 1001));
// Rotate the transport keys
oneOf(transportCrypto).rotateTransportKeys(
with(any(TransportKeys.class)), with(1001L));
@@ -361,9 +361,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
// Save the keys that were rotated
oneOf(db).updateTransportKeys(txn1,
singletonList(new KeySet(keySetId, contactId, rotated)));
// Schedule key rotation at the start of the next rotation period
// Schedule key rotation at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)),
with(rotationPeriodLength), with(MILLISECONDS));
with(timePeriodLength), with(MILLISECONDS));
}});
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
@@ -391,8 +391,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, false));
// The keys are inactive so no stream context should be returned
@@ -424,9 +424,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
oneOf(transportCrypto).deriveTransportKeys(transportId, masterKey,
1000, alice, false);
will(returnValue(transportKeys));
// Get the current time (the start of rotation period 1000)
// Get the current time (the start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000));
will(returnValue(timePeriodLength * 1000));
// Encode the tags (3 sets)
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
exactly(3).of(transportCrypto).encodeTag(
@@ -445,7 +445,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
with(tagKey), with(PROTOCOL_VERSION),
with((long) REORDERING_WINDOW_SIZE));
will(new EncodeTagAction(tags));
// Save the reordering window (previous rotation period, base 1)
// Save the reordering window (previous time period, base 1)
oneOf(db).setReorderingWindow(txn, keySetId, transportId, 999,
1, new byte[REORDERING_WINDOW_SIZE / 8]);
// Activate the keys
@@ -457,8 +457,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
db, transportCrypto, dbExecutor, scheduler, clock, transportId,
maxLatency);
// The timestamp is at the start of rotation period 1000
long timestamp = rotationPeriodLength * 1000;
// The timestamp is at the start of time period 1000
long timestamp = timePeriodLength * 1000;
assertEquals(keySetId, transportKeyManager.addContact(txn, contactId,
masterKey, timestamp, alice, false));
// The keys are inactive so no stream context should be returned
@@ -491,9 +491,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
oneOf(transportCrypto).deriveTransportKeys(transportId, masterKey,
1000, alice, active);
will(returnValue(transportKeys));
// Get the current time (the start of rotation period 1000)
// Get the current time (the start of time period 1000)
oneOf(clock).currentTimeMillis();
will(returnValue(rotationPeriodLength * 1000));
will(returnValue(timePeriodLength * 1000));
// Encode the tags (3 sets)
for (long i = 0; i < REORDERING_WINDOW_SIZE; i++) {
exactly(3).of(transportCrypto).encodeTag(
@@ -510,16 +510,16 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
}});
}
private TransportKeys createTransportKeys(long rotationPeriod,
private TransportKeys createTransportKeys(long timePeriod,
long streamCounter, boolean active) {
IncomingKeys inPrev = new IncomingKeys(tagKey, headerKey,
rotationPeriod - 1);
timePeriod - 1);
IncomingKeys inCurr = new IncomingKeys(tagKey, headerKey,
rotationPeriod);
timePeriod);
IncomingKeys inNext = new IncomingKeys(tagKey, headerKey,
rotationPeriod + 1);
timePeriod + 1);
OutgoingKeys outCurr = new OutgoingKeys(tagKey, headerKey,
rotationPeriod, streamCounter, active);
timePeriod, streamCounter, active);
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
}