Registry { public static synchronized <P> void registerKeyManager(final KeyManager<P> manager) throws GeneralSecurityException { registerKeyManager(manager, true); } private  Registry(); @Deprecated static synchronized void addCatalogue(String catalogueName, Catalogue<?> catalogue); @Deprecated static Catalogue<?> getCatalogue(String catalogueName); static synchronized void registerKeyManager(final KeyManager<P> manager); static synchronized void registerKeyManager(
      final KeyManager<P> manager, boolean newKeyAllowed); static synchronized void registerKeyManager(
      final KeyTypeManager<KeyProtoT> manager, boolean newKeyAllowed); static synchronized void registerAsymmetricKeyManagers(
          final PrivateKeyTypeManager<KeyProtoT, PublicKeyProtoT> privateKeyTypeManager,
          final KeyTypeManager<PublicKeyProtoT> publicKeyTypeManager,
          boolean newKeyAllowed); @Deprecated static synchronized void registerKeyManager(
      String typeUrl, final KeyManager<P> manager); @Deprecated static synchronized void registerKeyManager(
      String typeUrl, final KeyManager<P> manager, boolean newKeyAllowed); static synchronized void registerPrimitiveWrapper(
      final PrimitiveWrapper<B, P> wrapper); @Deprecated static KeyManager<P> getKeyManager(String typeUrl); static KeyManager<?> getUntypedKeyManager(String typeUrl); static KeyManager<P> getKeyManager(String typeUrl, Class<P> primitiveClass); static synchronized KeyData newKeyData(KeyTemplate keyTemplate); static synchronized MessageLite newKey(KeyTemplate keyTemplate); static synchronized MessageLite newKey(String typeUrl, MessageLite format); static KeyData getPublicKeyData(String typeUrl, ByteString serializedPrivateKey); @Deprecated @SuppressWarnings("TypeParameterUnusedInFormals") static P getPrimitive(String typeUrl, MessageLite key); static P getPrimitive(String typeUrl, MessageLite key, Class<P> primitiveClass); @Deprecated @SuppressWarnings("TypeParameterUnusedInFormals") static P getPrimitive(String typeUrl, ByteString serializedKey); static P getPrimitive(
      String typeUrl, ByteString serializedKey, Class<P> primitiveClass); @Deprecated @SuppressWarnings("TypeParameterUnusedInFormals") static P getPrimitive(String typeUrl, byte[] serializedKey); static P getPrimitive(String typeUrl, byte[] serializedKey, Class<P> primitiveClass); @Deprecated @SuppressWarnings("TypeParameterUnusedInFormals") static P getPrimitive(KeyData keyData); static P getPrimitive(KeyData keyData, Class<P> primitiveClass); @Deprecated static PrimitiveSet<P> getPrimitives(KeysetHandle keysetHandle); static PrimitiveSet<P> getPrimitives(
      KeysetHandle keysetHandle, Class<P> primitiveClass); @Deprecated static PrimitiveSet<P> getPrimitives(
      KeysetHandle keysetHandle, final KeyManager<P> customManager); static PrimitiveSet<P> getPrimitives(
      KeysetHandle keysetHandle, final KeyManager<P> customManager, Class<P> primitiveClass); static P wrap(PrimitiveSet<B> primitiveSet, Class<P> clazz); static P wrap(PrimitiveSet<P> primitiveSet); static Class<?> getInputPrimitive(Class<?> wrappedPrimitive);  }
@Test public void testRegisterKeyManager_keyManagerIsNull_shouldThrowException() throws Exception { try { Registry.registerKeyManager(null); fail("Expected IllegalArgumentException."); } catch (IllegalArgumentException e) { assertThat(e.toString()).contains("must be non-null"); } }
@Test public void testRegisterKeyManager_MoreRestrictedNewKeyAllowed_shouldWork() throws Exception { String typeUrl = "someTypeUrl"; Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl)); Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl), false); }
@Test public void testRegisterKeyManager_SameNewKeyAllowed_shouldWork() throws Exception { String typeUrl = "someOtherTypeUrl"; Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl)); Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl), true); }
@Test public void testRegisterKeyManager_LessRestrictedNewKeyAllowed_shouldThrowException() throws Exception { String typeUrl = "yetAnotherTypeUrl"; Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl), false); try { Registry.registerKeyManager(new CustomAeadKeyManager(typeUrl), true); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { } }
@Test public void testRegisterKeyManager_deprecated_keyManagerIsNull_shouldThrowException() throws Exception { try { Registry.registerKeyManager(AeadConfig.AES_CTR_HMAC_AEAD_TYPE_URL, null); fail("Expected IllegalArgumentException."); } catch (IllegalArgumentException e) { assertThat(e.toString()).contains("must be non-null"); } }
@Test public void testRegisterKeyManager_deprecated_WithKeyTypeNotSupported_shouldThrowException() throws Exception { String typeUrl = "yetSomeOtherTypeUrl"; String differentTypeUrl = "differentTypeUrl"; try { Registry.registerKeyManager(differentTypeUrl, new CustomAeadKeyManager(typeUrl)); fail("Should throw an exception."); } catch (GeneralSecurityException e) { assertExceptionContains(e, "Manager does not support key type " + differentTypeUrl); } }
@Test public void testRegisterKeyManager_deprecated_MoreRestrictedNewKeyAllowed_shouldWork() throws Exception { String typeUrl = "typeUrl"; Registry.registerKeyManager(typeUrl, new CustomAeadKeyManager(typeUrl)); try { Registry.registerKeyManager(typeUrl, new CustomAeadKeyManager(typeUrl), false); } catch (GeneralSecurityException e) { fail("repeated registrations of the same key manager should work"); } }
@Test public void testRegisterKeyManager_deprecated_LessRestrictedNewKeyAllowed_shouldThrowException() throws Exception { String typeUrl = "totallyDifferentTypeUrl"; Registry.registerKeyManager(typeUrl, new CustomAeadKeyManager(typeUrl), false); try { Registry.registerKeyManager(typeUrl, new CustomAeadKeyManager(typeUrl), true); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { } }