use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAdd_shouldAddNewKey_proto.
@Test
public void testAdd_shouldAddNewKey_proto() throws Exception {
// Create a keyset that contains a single HmacKey.
KeyTemplate template = KeyTemplates.get("HMAC_SHA256_128BITTAG");
Keyset keyset = KeysetManager.withEmptyKeyset().add(template).getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(1);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(0);
TestUtil.assertHmacKey(template, keyset.getKey(0));
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testThreadSafety_disableEnableSetPrimaryKey_shouldWork.
@Test
public void testThreadSafety_disableEnableSetPrimaryKey_shouldWork() throws Exception {
final int primaryKeyId = 42;
final int keyId2 = 43;
final int keyId3 = 44;
KeysetHandle handle = KeysetHandle.fromKeyset(TestUtil.createKeyset(createEnabledKey(primaryKeyId), createEnabledKey(keyId2), createDisabledKey(keyId3)));
final KeysetManager manager = KeysetManager.withKeysetHandle(handle);
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
disableEnableSetPrimaryKey(manager, keyId2);
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
disableEnableSetPrimaryKey(manager, keyId3);
}
});
thread2.start();
thread3.start();
// Wait until all threads finished.
thread2.join();
thread3.join();
Keyset keyset = manager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(3);
assertThat(keyset.getKey(0).getStatus()).isEqualTo(KeyStatusType.ENABLED);
assertThat(keyset.getKey(1).getStatus()).isEqualTo(KeyStatusType.ENABLED);
assertThat(keyset.getKey(2).getStatus()).isEqualTo(KeyStatusType.ENABLED);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAddNewKey_primaryThenNonPrimary.
@Test
public void testAddNewKey_primaryThenNonPrimary() throws Exception {
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
int primaryKeyId = keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, false);
Keyset keyset = keysetManager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(primaryKeyId);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testAddNewKey_onePrimaryAnotherPrimary.
@Test
public void testAddNewKey_onePrimaryAnotherPrimary() throws Exception {
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
int primaryKeyId = keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
Keyset keyset = keysetManager.getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(primaryKeyId);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetManagerTest method testSetPrimary_shouldSetPrimary.
@Test
public void testSetPrimary_shouldSetPrimary() throws Exception {
int primaryKeyId = 42;
int newPrimaryKeyId = 43;
KeysetHandle handle = KeysetHandle.fromKeyset(TestUtil.createKeyset(createEnabledKey(primaryKeyId), createEnabledKey(newPrimaryKeyId)));
Keyset keyset = KeysetManager.withKeysetHandle(handle).setPrimary(newPrimaryKeyId).getKeysetHandle().getKeyset();
assertThat(keyset.getKeyCount()).isEqualTo(2);
assertThat(keyset.getPrimaryKeyId()).isEqualTo(newPrimaryKeyId);
}
Aggregations