KeyManagerImpl implements KeyManager<PrimitiveT> { @Override public final PrimitiveT getPrimitive(ByteString serializedKey) throws GeneralSecurityException { try { KeyProtoT keyProto = keyTypeManager.parseKey(serializedKey); return validateKeyAndGetPrimitive(keyProto); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException( "Failures parsing proto of type " + keyTypeManager.getKeyClass().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 getPrimitive_ByteString_throwsInvalidKey() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); MessageLite notAKey = AesGcmKey.getDefaultInstance(); try { keyManager.getPrimitive(notAKey.toByteString()); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("validateKey(AesGcmKey) failed"); } }
@Test public void getPrimitive_MessageLite_throwsWrongProto() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); MessageLite notAKey = AesGcmKeyFormat.getDefaultInstance(); try { keyManager.getPrimitive(notAKey); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("Expected proto of type"); } }
@Test public void getPrimitive_MessageLite_throwsInvalidKey() throws Exception { KeyManager<Aead> keyManager = new KeyManagerImpl<>(new TestKeyTypeManager(), Aead.class); MessageLite notAKey = AesGcmKey.getDefaultInstance(); try { keyManager.getPrimitive(notAKey); fail("expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertThat(e.toString()).contains("validateKey(AesGcmKey) failed"); } }