KeyManagerImpl implements KeyManager<PrimitiveT> { @Override public final KeyData newKeyData(ByteString serializedKeyFormat) throws GeneralSecurityException { try { KeyProtoT key = keyFactoryHelper().parseValidateCreate(serializedKeyFormat); return KeyData.newBuilder() .setTypeUrl(getKeyType()) .setValue(key.toByteString()) .setKeyMaterialType(keyTypeManager.keyMaterialType()) .build(); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException("Unexpected proto", e); } } KeyManagerImpl(
      KeyTypeManager<KeyProtoT> keyTypeManager, Class<PrimitiveT> primitiveClass); @Override final PrimitiveT getPrimitive(ByteString serializedKey); @Override final PrimitiveT getPrimitive(MessageLite key); @Override final MessageLite newKey(ByteString serializedKeyFormat); @Override final MessageLite newKey(MessageLite keyFormat); @Override final boolean doesSupport(String typeUrl); @Override final String getKeyType(); @Override int getVersion(); @Override final KeyData newKeyData(ByteString serializedKeyFormat); @Override final Class<PrimitiveT> getPrimitiveClass();  }
@Test public void newKeyData_works() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); keyManager.newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString()); }
@Test public void newKeyData_typeUrlCorrect() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); assertThat( keyManager .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString()) .getTypeUrl()) .isEqualTo("type.googleapis.com/google.crypto.tink.AesGcmKey"); }
@Test public void newKeyData_valueLengthCorrect() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); assertThat( keyManager .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString()) .getValue() .size()) .isAtLeast(16); }
@Test public void newKeyData_wrongKeySize_throws() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); try { keyManager.newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(17).build().toByteString()); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("validateKeyFormat(AesGcmKeyFormat) failed"); } }
@Test public void newKeyData_keyMaterialTypeCorrect() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); assertThat( keyManager .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString()) .getKeyMaterialType()) .isEqualTo(KeyMaterialType.SYMMETRIC); }
@Test public void newKeyData_byteString_throwsUnsupportedOperation() throws Exception { KeyManager<Void> keyManager = new KeyManagerImpl<>(new TestKeyTypeManagerWithoutKeyFactory(), Void.class); try { keyManager.newKeyData(ByteString.copyFromUtf8("")); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException e) { } }