KeyManagerImpl implements KeyManager<PrimitiveT> { @Override public final MessageLite newKey(ByteString serializedKeyFormat) throws GeneralSecurityException { try { return keyFactoryHelper().parseValidateCreate(serializedKeyFormat); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException( "Failures parsing proto of type " + keyTypeManager.keyFactory().getKeyFormatClass().getName(), 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 newKey_ByteString_works() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString()); }
@Test public void newKey_ByteString_throwsInvalidKeySize() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); try { keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(17).build().toByteString()); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("validateKeyFormat(AesGcmKeyFormat) failed"); } }
@Test public void newKey_MessageLite_works() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build()); }
@Test public void newKey_MessageLite_throwsWrongProto() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); try { keyManager.newKey(AesGcmKey.getDefaultInstance()); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("Expected proto of type"); } }
@Test public void newKey_MessageLite_throwsInvalidKeySize() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); try { keyManager.newKey((MessageLite) AesGcmKeyFormat.getDefaultInstance()); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("validateKeyFormat(AesGcmKeyFormat) failed"); } }
@Test public void newKey_ByteString_throwsUnsupportedOperation() throws Exception { KeyManager<Void> keyManager = new KeyManagerImpl<>(new TestKeyTypeManagerWithoutKeyFactory(), Void.class); try { keyManager.newKey(ByteString.copyFromUtf8("")); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException e) { } }
@Test public void newKey_byteString_throwsUnsupportedOperation() throws Exception { KeyManager<Void> keyManager = new KeyManagerImpl<>(new TestKeyTypeManagerWithoutKeyFactory(), Void.class); try { keyManager.newKey(ByteString.copyFromUtf8("")); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException e) { } }
@Test public void newKey_messageList_throwsUnsupportedOperation() throws Exception { KeyManager<Void> keyManager = new KeyManagerImpl<>(new TestKeyTypeManagerWithoutKeyFactory(), Void.class); try { keyManager.newKey(AesGcmKey.getDefaultInstance()); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException e) { } }